-
Notifications
You must be signed in to change notification settings - Fork 225
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3929 from arunans23/templatetest
- Loading branch information
Showing
6 changed files
with
279 additions
and
0 deletions.
There are no files selected for viewing
147 changes: 147 additions & 0 deletions
147
...src/test/java/org/wso2/carbon/esb/mediator/test/invoke/InvokeTemplateIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
/* | ||
* Copyright (c) 2025, WSO2 LLC. (http://www.wso2.org) All Rights Reserved. | ||
* | ||
* WSO2 LLC. licenses this file to you under the Apache License, | ||
* Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.wso2.carbon.esb.mediator.test.invoke; | ||
|
||
import org.apache.commons.lang.StringUtils; | ||
import org.testng.Assert; | ||
import org.testng.annotations.BeforeClass; | ||
import org.testng.annotations.Test; | ||
import org.wso2.carbon.automation.test.utils.http.client.HttpRequestUtil; | ||
import org.wso2.carbon.automation.test.utils.http.client.HttpResponse; | ||
import org.wso2.esb.integration.common.utils.CarbonLogReader; | ||
import org.wso2.esb.integration.common.utils.ESBIntegrationTest; | ||
|
||
import java.net.URL; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.testng.Assert.assertTrue; | ||
|
||
/** | ||
* This class tests Invoke Mediator with Template support to validate connector response | ||
*/ | ||
public class InvokeTemplateIntegrationTest extends ESBIntegrationTest { | ||
|
||
CarbonLogReader carbonLogReader; | ||
|
||
@BeforeClass(alwaysRun = true) | ||
public void uploadSynapseConfig() throws Exception { | ||
|
||
super.init(); | ||
carbonLogReader = new CarbonLogReader(); | ||
carbonLogReader.start(); | ||
} | ||
|
||
@Test(groups = "wso2.esb", description = "Testing Invoke mediator with Template support with GET method when " + | ||
"overwriteBody is false") | ||
public void testInvokeMediatorWithOverwriteBodyTrueAndGetMethod() throws Exception { | ||
|
||
carbonLogReader.clearLogs(); | ||
executeSequenceAndAssertResponse("weatherintegration", "/overwrite/false", "", "GET", | ||
"The return payload does not match the expectedPayload", null , null); | ||
assertTrue(carbonLogReader.checkForLog("The weather template returned city in header as LONDON. " + | ||
"Status code : 200.", DEFAULT_TIMEOUT)); | ||
} | ||
|
||
@Test(groups = "wso2.esb", description = "Testing Invoke mediator with Template support with GET method when " + | ||
"overwriteBody is true") | ||
public void testInvokeMediatorWithOverwriteBodyFalseAndGetMethod() throws Exception { | ||
|
||
carbonLogReader.clearLogs(); | ||
String expectedOutput = "{\"city\": \"LONDON\", \"temperature - C\" : \"23\", \"weather\" : \"cloudy\"}"; | ||
executeSequenceAndAssertResponse("weatherintegration", "/overwrite/true", | ||
expectedOutput, "GET", | ||
"The return payload does not match the expectedPayload", null, null); | ||
assertTrue(carbonLogReader.checkForLog("The weather template returned city in header as LONDON. " + | ||
"Status code : 200.", DEFAULT_TIMEOUT)); | ||
} | ||
|
||
@Test(groups = "wso2.esb", description = "Testing Invoke mediator with Template support with POST method when " + | ||
"overwriteBody is false") | ||
public void testInvokeMediatorWithOverwriteBodyTrueAndPostMethod() throws Exception { | ||
|
||
carbonLogReader.clearLogs(); | ||
String inputPayload = "{\"city\":\"LONDON\"}"; | ||
executeSequenceAndAssertResponse("weatherintegration", "/overwrite/false", inputPayload, "POST", | ||
"The return payload does not match the expectedPayload", inputPayload, "application/json"); | ||
assertTrue(carbonLogReader.checkForLog("The weather template returned city in header as LONDON. " + | ||
"Status code : 200.", DEFAULT_TIMEOUT)); | ||
} | ||
|
||
@Test(groups = "wso2.esb", description = "Testing Invoke mediator with Template support with POST method when " + | ||
"overwriteBody is true") | ||
public void testInvokeMediatorWithOverwriteBodyFalseAndPostMethod() throws Exception { | ||
|
||
carbonLogReader.clearLogs(); | ||
String inputPayload = "{\"city\":\"LONDON\"}"; | ||
String expectedOutput = "{\"city\": \"LONDON\", \"temperature - C\" : \"23\", \"weather\" : \"cloudy\"}"; | ||
executeSequenceAndAssertResponse("weatherintegration", "/overwrite/true", | ||
expectedOutput, "POST", | ||
"The return payload does not match the expectedPayload", inputPayload, "application/json"); | ||
assertTrue(carbonLogReader.checkForLog("The weather template returned city in header as LONDON. " + | ||
"Status code : 200.", DEFAULT_TIMEOUT)); | ||
} | ||
|
||
@Test(groups = "wso2.esb", description = "Testing Invoke mediator with Template support with POST method when " + | ||
"overwriteBody is false and the input payload is xml") | ||
public void testInvokeMediatorWithOverwriteBodyTrueAndPostMethodWithXMLPayload() throws Exception { | ||
|
||
carbonLogReader.clearLogs(); | ||
String inputPayload = "<city>LONDON</city>"; | ||
executeSequenceAndAssertResponse("weatherintegration", "/overwrite/false", inputPayload, "POST", | ||
"The return payload does not match the expectedPayload", inputPayload, "application/xml"); | ||
assertTrue(carbonLogReader.checkForLog("The weather template returned city in header as LONDON. " + | ||
"Status code : 200.", DEFAULT_TIMEOUT)); | ||
} | ||
|
||
@Test(groups = "wso2.esb", description = "Testing Invoke mediator with Template support with POST method when " + | ||
"overwriteBody is true and the input payload is xml") | ||
public void testInvokeMediatorWithOverwriteBodyFalseAndPostMethodWithXMLPayload() throws Exception { | ||
|
||
carbonLogReader.clearLogs(); | ||
String inputPayload = "<city>LONDON</city>"; | ||
String expectedOutput = "{\"city\": \"LONDON\", \"temperature - C\" : \"23\", \"weather\" : \"cloudy\"}"; | ||
executeSequenceAndAssertResponse("weatherintegration", "/overwrite/true", | ||
expectedOutput, "POST", | ||
"The return payload does not match the expectedPayload", inputPayload, "application/xml"); | ||
assertTrue(carbonLogReader.checkForLog("The weather template returned city in header as LONDON. " + | ||
"Status code : 200.", DEFAULT_TIMEOUT)); | ||
} | ||
|
||
|
||
|
||
private void executeSequenceAndAssertResponse(String apiName, String resource, String expectedOutput, | ||
String httpMethod, String errorMessage, String inputPayload, | ||
String contentType) throws Exception { | ||
|
||
String invocationURL = getApiInvocationURL(apiName) + resource; | ||
Map<String, String> headers = new HashMap<String, String>(); | ||
if (StringUtils.isNotEmpty(contentType)) { | ||
headers.put("Content-Type", contentType); | ||
} | ||
HttpResponse httpResponse; | ||
if (httpMethod.equalsIgnoreCase("POST")) { | ||
URL endpoint = new URL(invocationURL); | ||
httpResponse = HttpRequestUtil.doPost(endpoint, inputPayload, headers); | ||
} else { | ||
httpResponse = HttpRequestUtil.doGet(invocationURL, headers); | ||
} | ||
Assert.assertEquals(httpResponse.getResponseCode(), 200, "Response code mismatched"); | ||
Assert.assertEquals(httpResponse.getData(), expectedOutput, errorMessage); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...ifacts/ESB/server/repository/deployment/server/synapse-configs/default/api/WeatherAPI.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
~ Copyright (c) 2025, WSO2 LLC. (https://www.wso2.com) All Rights Reserved. | ||
~ | ||
~ WSO2 LLC. licenses this file to you under the Apache License, | ||
~ Version 2.0 (the "License"); you may not use this file except | ||
~ in compliance with the License. | ||
~ You may obtain a copy of the License at | ||
~ | ||
~ http://www.apache.org/licenses/LICENSE-2.0 | ||
~ | ||
~ Unless required by applicable law or agreed to in writing, | ||
~ software distributed under the License is distributed on an | ||
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
~ KIND, either express or implied. See the License for the | ||
~ specific language governing permissions and limitations | ||
~ under the License. | ||
~ | ||
--> | ||
<api context="/weather" name="WeatherAPI" xmlns="http://ws.apache.org/ns/synapse"> | ||
<resource methods="GET" uri-template="/?q={q}"> | ||
<inSequence> | ||
<payloadFactory media-type="json" template-type="default"> | ||
<format>{"city": "${toUpper(params.queryParams.q)}", "temperature - C" : "23", "weather" : "cloudy"}</format> | ||
</payloadFactory> | ||
<header name="city" action="set" scope="transport" | ||
expression="${toUpper(params.queryParams.q)}" /> | ||
<respond /> | ||
</inSequence> | ||
<faultSequence> | ||
</faultSequence> | ||
</resource> | ||
</api> |
35 changes: 35 additions & 0 deletions
35
...SB/server/repository/deployment/server/synapse-configs/default/api/WeatherIntegration.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
~ Copyright (c) 2025, WSO2 LLC. (https://www.wso2.com) All Rights Reserved. | ||
~ | ||
~ WSO2 LLC. licenses this file to you under the Apache License, | ||
~ Version 2.0 (the "License"); you may not use this file except | ||
~ in compliance with the License. | ||
~ You may obtain a copy of the License at | ||
~ | ||
~ http://www.apache.org/licenses/LICENSE-2.0 | ||
~ | ||
~ Unless required by applicable law or agreed to in writing, | ||
~ software distributed under the License is distributed on an | ||
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
~ KIND, either express or implied. See the License for the | ||
~ specific language governing permissions and limitations | ||
~ under the License. | ||
~ | ||
--> | ||
<api context="/weatherintegration" name="WeatherIntegration" xmlns="http://ws.apache.org/ns/synapse"> | ||
<resource methods="GET POST" uri-template="/overwrite/{flag}"> | ||
<inSequence> | ||
<call-template target="readweather"> | ||
<with-param name="responseVariable" value="read_weather_1" /> | ||
<with-param name="overwriteBody" value="{${params.pathParams.flag}}" /> | ||
</call-template> | ||
<log category="INFO"> | ||
<message>The weather template returned city in header as ${vars.read_weather_1.headers.city}. Status code : ${vars.read_weather_1.attributes.statusCode}.</message> | ||
</log> | ||
<respond /> | ||
</inSequence> | ||
<faultSequence> | ||
</faultSequence> | ||
</resource> | ||
</api> |
30 changes: 30 additions & 0 deletions
30
...s/ESB/server/repository/deployment/server/synapse-configs/default/endpoints/WeatherEP.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
~ Copyright (c) 2025, WSO2 LLC. (https://www.wso2.com) All Rights Reserved. | ||
~ | ||
~ WSO2 LLC. licenses this file to you under the Apache License, | ||
~ Version 2.0 (the "License"); you may not use this file except | ||
~ in compliance with the License. | ||
~ You may obtain a copy of the License at | ||
~ | ||
~ http://www.apache.org/licenses/LICENSE-2.0 | ||
~ | ||
~ Unless required by applicable law or agreed to in writing, | ||
~ software distributed under the License is distributed on an | ||
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
~ KIND, either express or implied. See the License for the | ||
~ specific language governing permissions and limitations | ||
~ under the License. | ||
~ | ||
--> | ||
<endpoint name="WeatherEP" xmlns="http://ws.apache.org/ns/synapse"> | ||
<http method="get" uri-template="http://localhost:8480/weather/?q=london"> | ||
<suspendOnFailure> | ||
<initialDuration>-1</initialDuration> | ||
<progressionFactor>1</progressionFactor> | ||
</suspendOnFailure> | ||
<markForSuspension> | ||
<retriesBeforeSuspension>0</retriesBeforeSuspension> | ||
</markForSuspension> | ||
</http> | ||
</endpoint> |
28 changes: 28 additions & 0 deletions
28
...ESB/server/repository/deployment/server/synapse-configs/default/templates/readweather.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
~ Copyright (c) 2025, WSO2 LLC. (https://www.wso2.com) All Rights Reserved. | ||
~ | ||
~ WSO2 LLC. licenses this file to you under the Apache License, | ||
~ Version 2.0 (the "License"); you may not use this file except | ||
~ in compliance with the License. | ||
~ You may obtain a copy of the License at | ||
~ | ||
~ http://www.apache.org/licenses/LICENSE-2.0 | ||
~ | ||
~ Unless required by applicable law or agreed to in writing, | ||
~ software distributed under the License is distributed on an | ||
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
~ KIND, either express or implied. See the License for the | ||
~ specific language governing permissions and limitations | ||
~ under the License. | ||
~ | ||
--> | ||
<template name="readweather" xmlns="http://ws.apache.org/ns/synapse"> | ||
<parameter isMandatory="false" name="responseVariable" /> | ||
<parameter isMandatory="false" name="overwriteBody" /> | ||
<sequence> | ||
<call> | ||
<endpoint key="WeatherEP" /> | ||
</call> | ||
</sequence> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters