Skip to content

Commit

Permalink
Merge pull request #3929 from arunans23/templatetest
Browse files Browse the repository at this point in the history
  • Loading branch information
arunans23 authored Jan 29, 2025
2 parents 7af2ed0 + f8fe1d1 commit 3ef367c
Show file tree
Hide file tree
Showing 6 changed files with 279 additions and 0 deletions.
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);
}
}
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>
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>
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>
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>
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@
</packages>
</test>

<test name ="Invoke-mediator-Test" preserve-order="true" verbose="2">
<classes>
<class name="org.wso2.carbon.esb.mediator.test.invoke.InvokeTemplateIntegrationTest"/>
</classes>
</test>

<test name="Class-mediator-Test" preserve-order="true" verbose="2">
<packages>
<package name="org.wso2.carbon.esb.mediator.test.classMediator"/>
Expand Down

0 comments on commit 3ef367c

Please sign in to comment.