-
Notifications
You must be signed in to change notification settings - Fork 223
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 #3820 from SanojPunchihewa/payload-test
Add test case for Inline expression support in Payload factory
- Loading branch information
Showing
3 changed files
with
326 additions
and
1 deletion.
There are no files selected for viewing
209 changes: 209 additions & 0 deletions
209
...so2/carbon/esb/mediator/test/payload/factory/PayloadFactorySynapseExpressionTestCase.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,209 @@ | ||
/* | ||
* Copyright (c) 2024, 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. | ||
* | ||
*/ | ||
|
||
package org.wso2.carbon.esb.mediator.test.payload.factory; | ||
|
||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonParser; | ||
import org.apache.http.HttpResponse; | ||
import org.testng.Assert; | ||
import org.testng.annotations.AfterClass; | ||
import org.testng.annotations.BeforeClass; | ||
import org.testng.annotations.Test; | ||
import org.w3c.dom.Document; | ||
import org.wso2.esb.integration.common.utils.ESBIntegrationTest; | ||
import org.wso2.esb.integration.common.utils.clients.SimpleHttpClient; | ||
import org.xml.sax.SAXException; | ||
|
||
import java.io.IOException; | ||
import javax.xml.parsers.DocumentBuilder; | ||
import javax.xml.parsers.DocumentBuilderFactory; | ||
import javax.xml.parsers.ParserConfigurationException; | ||
|
||
import static org.testng.Assert.assertEquals; | ||
|
||
/** | ||
* Test case for payload factory mediator with synapse expressions | ||
*/ | ||
public class PayloadFactorySynapseExpressionTestCase extends ESBIntegrationTest { | ||
|
||
SimpleHttpClient httpClient = new SimpleHttpClient(); | ||
|
||
@BeforeClass(alwaysRun = true) | ||
public void uploadSynapseConfig() throws Exception { | ||
|
||
super.init(); | ||
} | ||
|
||
@Test(groups = {"wso2.esb"}, description = "Testing Payload factory mediator with json to json transformation") | ||
public void testPayloadFactoryJsonToJson() throws IOException { | ||
|
||
String expectedResponse = "{\n" + | ||
" \"variable\": 123,\n" + | ||
" \"username\": \"[]John&\\nDoe]\",\n" + | ||
" \"escapedVariable\": \"123\",\n" + | ||
" \"bool\": true,\n" + | ||
" \"escapedBool\": \"true\",\n" + | ||
" \"escapedObject\": \"{\\\"type\\\":\\\"cat\\\\n\\\\rdog\\\",\\\"name\\\":\\\"john\\\"}\",\n" + | ||
" \"object\": {\n" + | ||
" \"type\": \"cat\\n\\rdog\",\n" + | ||
" \"name\": \"john\"\n" + | ||
" }\n" + | ||
"}"; | ||
|
||
String requestPayload = "{\n" + | ||
" \"customer_name\": \"[]John&\\nDoe]\",\n" + | ||
" \"pet\": {\n" + | ||
" \"type\":\"cat\\n\\rdog\",\n" + | ||
" \"name\":\"john\"\n" + | ||
" },\n" + | ||
" \"path\": true\n" + | ||
"}"; | ||
|
||
String serviceURL = getMainSequenceURL() + "synapseExpressionPayload/json-json"; | ||
HttpResponse httpResponse = httpClient.doPost(serviceURL, null, requestPayload, "application/json"); | ||
String responsePayload = httpClient.getResponsePayload(httpResponse); | ||
JsonElement responseJSON = JsonParser.parseString(responsePayload); | ||
JsonElement expectedJSON = JsonParser.parseString(expectedResponse); | ||
assertEquals(responseJSON, expectedJSON, "Response payload mismatched"); | ||
} | ||
|
||
@Test(groups = {"wso2.esb"}, description = "Testing Payload factory mediator with json to xml transformation") | ||
public void testPayloadFactoryJsonToXML() throws IOException, ParserConfigurationException, SAXException { | ||
|
||
String expectedResponse = "<sleepOperation><username>123<456</username><name>abc\\\\ndef</name>" + | ||
"<load><fruit>apple</fruit></load><data><type>cat&</type><name>abc\\\\ndef</name></data>" + | ||
"<special_json_str>123\\n456</special_json_str></sleepOperation>"; | ||
|
||
String requestPayload = "{\n" + | ||
" \"customer_name\": \"John<Doe\",\n" + | ||
" \"pet\": {\n" + | ||
" \"type\":\"cat&\",\n" + | ||
" \"name\":\"abc\\\\ndef\"\n" + | ||
" },\n" + | ||
" \"path\": true\n" + | ||
"}"; | ||
|
||
String serviceURL = getMainSequenceURL() + "synapseExpressionPayload/payload-json-xml"; | ||
HttpResponse httpResponse = httpClient.doPost(serviceURL, null, requestPayload, "application/json"); | ||
String responsePayload = httpClient.getResponsePayload(httpResponse); | ||
|
||
Document document1 = parseXML(expectedResponse); | ||
Document document2 = parseXML(responsePayload); | ||
|
||
if (!document1.isEqualNode(document2)) { | ||
Assert.fail("Response payload mismatched: " + responsePayload + " expected: " + expectedResponse); | ||
} | ||
} | ||
|
||
@Test(groups = {"wso2.esb"}, description = "Testing Payload factory mediator with xml to xml transformation") | ||
public void testPayloadFactoryXMLToXML() throws IOException, ParserConfigurationException, SAXException { | ||
|
||
String expectedResponse = "<sleepOperation><username>John<</username><load>123<456</load><json>123\\n456</json><data><pet>\n" + | ||
" <type>cat</type>\n" + | ||
" <name>abc\\nxyz</name>\n" + | ||
" </pet></data></sleepOperation>"; | ||
|
||
String requestPayload = "<root>\n" + | ||
" <customer_name>John<</customer_name>\n" + | ||
" <pet>\n" + | ||
" <type>cat</type>\n" + | ||
" <name>abc\\nxyz</name>\n" + | ||
" </pet>\n" + | ||
" <path>true</path>\n" + | ||
"</root>"; | ||
|
||
String serviceURL = getMainSequenceURL() + "synapseExpressionPayload/payload-xml-xml"; | ||
HttpResponse httpResponse = httpClient.doPost(serviceURL, null, requestPayload, "application/xml"); | ||
String responsePayload = httpClient.getResponsePayload(httpResponse); | ||
|
||
Document document1 = parseXML(expectedResponse); | ||
Document document2 = parseXML(responsePayload); | ||
|
||
if (!document1.isEqualNode(document2)) { | ||
Assert.fail("Response payload mismatched: " + responsePayload + " expected: " + expectedResponse); | ||
} | ||
} | ||
|
||
@Test(groups = {"wso2.esb"}, description = "Testing Payload factory mediator with xml to json transformation") | ||
public void testPayloadFactoryXMLToJson() throws IOException { | ||
|
||
String expectedResponse = "{\n" + | ||
" \"string\": \"John<\",\n" + | ||
" \"escapedObject\": \"<pet><type>cat</type><name>emily\\\\ntini</name></pet>\",\n" + | ||
" \"object\": {\n" + | ||
" \"pet\": {\n" + | ||
" \"type\": \"cat\",\n" + | ||
" \"name\": \"emily\\\\ntini\"\n" + | ||
" }\n" + | ||
" },\n" + | ||
" \"xml\": \"123<456\",\n" + | ||
" \"json\": \"123\\n456\"\n" + | ||
"}"; | ||
|
||
String requestPayload = "<root>\n" + | ||
" <customer_name>John<</customer_name>\n" + | ||
" <pet><type>cat</type><name>emily\\\\ntini</name></pet>\n" + | ||
" <path>true</path>\n" + | ||
"</root>"; | ||
|
||
String serviceURL = getMainSequenceURL() + "synapseExpressionPayload/payload-xml-json"; | ||
HttpResponse httpResponse = httpClient.doPost(serviceURL, null, requestPayload, "application/xml"); | ||
String responsePayload = httpClient.getResponsePayload(httpResponse); | ||
JsonElement responseJSON = JsonParser.parseString(responsePayload); | ||
JsonElement expectedJSON = JsonParser.parseString(expectedResponse); | ||
assertEquals(responseJSON, expectedJSON, "Response payload mismatched"); | ||
} | ||
|
||
@Test(groups = {"wso2.esb"}, description = "Testing Payload factory mediator with Freemarker template") | ||
public void testPayloadFactoryFreemarker() throws IOException { | ||
|
||
String expectedResponse = "{\n" + | ||
" \"name\": \"Johe Doe\",\n" + | ||
" \"id\": 123\n" + | ||
"}"; | ||
|
||
String requestPayload = "{\n" + | ||
" \"firstname\": \"Johe\",\n" + | ||
" \"lastname\": \"Doe\",\n" + | ||
" \"path\": true\n" + | ||
"}"; | ||
|
||
String serviceURL = getMainSequenceURL() + "synapseExpressionPayload/payload-freemarker"; | ||
HttpResponse httpResponse = httpClient.doPost(serviceURL, null, requestPayload, "application/json"); | ||
String responsePayload = httpClient.getResponsePayload(httpResponse); | ||
JsonElement responseJSON = JsonParser.parseString(responsePayload); | ||
JsonElement expectedJSON = JsonParser.parseString(expectedResponse); | ||
assertEquals(responseJSON, expectedJSON, "Response payload mismatched"); | ||
} | ||
|
||
private static Document parseXML(String xml) throws IOException, ParserConfigurationException, SAXException { | ||
|
||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); | ||
factory.setIgnoringElementContentWhitespace(true); | ||
DocumentBuilder builder = factory.newDocumentBuilder(); | ||
return builder.parse(new java.io.ByteArrayInputStream(xml.getBytes())); | ||
} | ||
|
||
@AfterClass(alwaysRun = true) | ||
private void destroy() throws Exception { | ||
|
||
super.cleanup(); | ||
} | ||
} |
116 changes: 116 additions & 0 deletions
116
...ory/deployment/server/synapse-configs/default/api/synapse_expressions_payload_factory.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,116 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
~ Copyright (c) 2024, 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 xmlns="http://ws.apache.org/ns/synapse" name="SynapseExpressionPayloadAPI" context="/synapseExpressionPayload"> | ||
<resource methods="POST" url-mapping="/json-json"> | ||
<inSequence> | ||
<variable name="id" value="123" type="INTEGER"/> | ||
<payloadFactory media-type="json"> | ||
<format><![CDATA[{ | ||
"variable": ${var.id}, | ||
"username": "${payload.customer_name}", | ||
"escapedVariable": "${var.id}", | ||
"bool": ${payload.path}, | ||
"escapedBool": "${payload.path}", | ||
"escapedObject": "${payload.pet}", | ||
"object": ${payload.pet} | ||
}]]> | ||
</format> | ||
</payloadFactory> | ||
<respond/> | ||
<respond/> | ||
</inSequence> | ||
</resource> | ||
<resource methods="POST GET" uri-template="/payload-json-xml"> | ||
<inSequence> | ||
<variable name="user_name" value="123<456"/> | ||
<variable name="special_json_str" value="123\n456"/> | ||
<variable name="xml_data" value="<fruit>apple</fruit>" type="OM"/> | ||
<payloadFactory media-type="xml"> | ||
<format> | ||
<sleepOperation xmlns=""> | ||
<username>${var.user_name}</username> | ||
<name>${payload.pet.name}</name> | ||
<load>${var.xml_data}</load> | ||
<data>${payload.pet}</data> | ||
<special_json_str>${var.special_json_str}</special_json_str> | ||
</sleepOperation> | ||
</format> | ||
</payloadFactory> | ||
<respond/> | ||
</inSequence> | ||
<faultSequence> | ||
</faultSequence> | ||
</resource> | ||
<resource methods="POST GET" uri-template="/payload-xml-xml"> | ||
<inSequence> | ||
<variable name="load" value="123.34"/> | ||
<variable name="id" value="123<456"/> | ||
<variable name="special_json" value="123\n456"/> | ||
<payloadFactory media-type="xml"> | ||
<format> | ||
<sleepOperation xmlns=""> | ||
<username>${xpath('//root/customer_name/text()')}</username> | ||
<load>${var.id}</load> | ||
<json>${var.special_json}</json> | ||
<data>${xpath('//root/pet')}</data> | ||
</sleepOperation> | ||
</format> | ||
</payloadFactory> | ||
<respond/> | ||
</inSequence> | ||
<faultSequence> | ||
</faultSequence> | ||
</resource> | ||
<resource methods="POST GET" uri-template="/payload-xml-json"> | ||
<inSequence> | ||
<variable name="id" value="123<456"/> | ||
<variable name="special_json" value="123\n456"/> | ||
<payloadFactory media-type="json"> | ||
<format>{ | ||
"string": "${xpath('//root/customer_name/text()')}", | ||
"escapedObject": "${xpath('//root/pet')}", | ||
"object": ${xpath('//root/pet')}, | ||
"xml": "${var.id}", | ||
"json": "${var.special_json}" | ||
} | ||
</format> | ||
</payloadFactory> | ||
<respond/> | ||
</inSequence> | ||
<faultSequence> | ||
</faultSequence> | ||
</resource> | ||
<resource methods="POST GET" uri-template="/payload-freemarker"> | ||
<inSequence> | ||
<variable name="user_name" value="123" type="INTEGER"/> | ||
<variable name="userID" value="123" type="INTEGER"/> | ||
<payloadFactory media-type="json" template-type="freemarker"> | ||
<format><![CDATA[{ | ||
"name": "${payload.firstname} ${payload.lastname}", | ||
"id": ${var.userID} | ||
}]]> | ||
</format> | ||
</payloadFactory> | ||
<respond/> | ||
</inSequence> | ||
<faultSequence> | ||
</faultSequence> | ||
</resource> | ||
</api> |
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