diff --git a/integration/mediation-tests/tests-mediator-1/src/test/java/org/wso2/carbon/esb/mediator/test/payload/factory/PayloadFactorySynapseExpressionTestCase.java b/integration/mediation-tests/tests-mediator-1/src/test/java/org/wso2/carbon/esb/mediator/test/payload/factory/PayloadFactorySynapseExpressionTestCase.java new file mode 100644 index 0000000000..24955ad91a --- /dev/null +++ b/integration/mediation-tests/tests-mediator-1/src/test/java/org/wso2/carbon/esb/mediator/test/payload/factory/PayloadFactorySynapseExpressionTestCase.java @@ -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 = "123<456abc\\\\ndef" + + "applecat&abc\\\\ndef" + + "123\\n456"; + + String requestPayload = "{\n" + + " \"customer_name\": \"Johncat\n" + + " abc\\nxyz\n" + + " "; + + String requestPayload = "\n" + + " John<\n" + + " \n" + + " cat\n" + + " abc\\nxyz\n" + + " \n" + + " true\n" + + ""; + + 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\": \"catemily\\\\ntini\",\n" + + " \"object\": {\n" + + " \"pet\": {\n" + + " \"type\": \"cat\",\n" + + " \"name\": \"emily\\\\ntini\"\n" + + " }\n" + + " },\n" + + " \"xml\": \"123<456\",\n" + + " \"json\": \"123\\n456\"\n" + + "}"; + + String requestPayload = "\n" + + " John<\n" + + " catemily\\\\ntini\n" + + " true\n" + + ""; + + 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(); + } +} diff --git a/integration/mediation-tests/tests-mediator-1/src/test/resources/artifacts/ESB/server/repository/deployment/server/synapse-configs/default/api/synapse_expressions_payload_factory.xml b/integration/mediation-tests/tests-mediator-1/src/test/resources/artifacts/ESB/server/repository/deployment/server/synapse-configs/default/api/synapse_expressions_payload_factory.xml new file mode 100644 index 0000000000..a2e05362a4 --- /dev/null +++ b/integration/mediation-tests/tests-mediator-1/src/test/resources/artifacts/ESB/server/repository/deployment/server/synapse-configs/default/api/synapse_expressions_payload_factory.xml @@ -0,0 +1,116 @@ + + + + + + + + + + + + + + + + + + + + + + + ${var.user_name} + ${payload.pet.name} + ${var.xml_data} + ${payload.pet} + ${var.special_json_str} + + + + + + + + + + + + + + + + + ${xpath('//root/customer_name/text()')} + ${var.id} + ${var.special_json} + ${xpath('//root/pet')} + + + + + + + + + + + + + + { + "string": "${xpath('//root/customer_name/text()')}", + "escapedObject": "${xpath('//root/pet')}", + "object": ${xpath('//root/pet')}, + "xml": "${var.id}", + "json": "${var.special_json}" + } + + + + + + + + + + + + + + + + + + + + + diff --git a/pom.xml b/pom.xml index 7944442c58..44133dd90c 100644 --- a/pom.xml +++ b/pom.xml @@ -1606,7 +1606,7 @@ 2.4.0-b180830.0359 2.3.0 2.3.1 - 4.0.0-wso2v144 + 4.0.0-wso2v151 [4.0.0, 4.0.1) 4.7.215 1.1.3