Skip to content

Commit

Permalink
Merge pull request #3820 from SanojPunchihewa/payload-test
Browse files Browse the repository at this point in the history
Add test case for Inline expression support in Payload factory
  • Loading branch information
SanojPunchihewa authored Dec 11, 2024
2 parents 45aec21 + 56e7853 commit 912e299
Show file tree
Hide file tree
Showing 3 changed files with 326 additions and 1 deletion.
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&lt;456</username><name>abc\\\\ndef</name>" +
"<load><fruit>apple</fruit></load><data><type>cat&amp;</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&lt;</username><load>123&lt;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&lt;</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&lt;</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();
}
}
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&lt;456"/>
<variable name="special_json_str" value="123\n456"/>
<variable name="xml_data" value="&lt;fruit&gt;apple&lt;/fruit&gt;" 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&lt;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&lt;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>
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1606,7 +1606,7 @@
<version.jaxb.api>2.4.0-b180830.0359</version.jaxb.api>
<com.sun.jaxb.version>2.3.0</com.sun.jaxb.version>
<com.sun.jaxb.impl.version>2.3.1</com.sun.jaxb.impl.version>
<synapse.version>4.0.0-wso2v144</synapse.version>
<synapse.version>4.0.0-wso2v151</synapse.version>
<imp.pkg.version.synapse>[4.0.0, 4.0.1)</imp.pkg.version.synapse>
<carbon.mediation.version>4.7.215</carbon.mediation.version>
<carbon.crypto.version>1.1.3</carbon.crypto.version>
Expand Down

0 comments on commit 912e299

Please sign in to comment.