diff --git a/integration/mediation-tests/tests-mediator-1/src/test/java/org/wso2/carbon/esb/mediator/test/v2/ForEachMediatorTestCase.java b/integration/mediation-tests/tests-mediator-1/src/test/java/org/wso2/carbon/esb/mediator/test/v2/ForEachMediatorTestCase.java new file mode 100644 index 0000000000..22c2ac2ab4 --- /dev/null +++ b/integration/mediation-tests/tests-mediator-1/src/test/java/org/wso2/carbon/esb/mediator/test/v2/ForEachMediatorTestCase.java @@ -0,0 +1,278 @@ +/* + * 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.v2; + +import com.google.gson.JsonElement; +import com.google.gson.JsonParser; +import org.apache.http.HttpResponse; +import org.apache.http.util.EntityUtils; +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.CarbonLogReader; +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.ByteArrayInputStream; +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; + +public class ForEachMediatorTestCase extends ESBIntegrationTest { + + private final SimpleHttpClient httpClient = new SimpleHttpClient(); + + private final String requestPayloadXML = "applefruitcarrot" + + "vegetable"; + + private final String requestPayload = "{\n" + + " \"data\": {\n" + + " \"list\": [\n" + + " {\n" + + " \"name\": \"apple\",\n" + + " \"type\": \"fruit\"\n" + + " },\n" + + " {\n" + + " \"name\": \"carrot\",\n" + + " \"type\": \"vegetable\"\n" + + " }\n" + + " ]\n" + + " }\n" + + "}"; + + private final String expected = "{\n" + + " \"data\": {\n" + + " \"list\": [\n" + + " {\n" + + " \"_name\": \"apple\",\n" + + " \"age\": 5,\n" + + " \"status\": true\n" + + " },\n" + + " {\n" + + " \"_name\": \"carrot\",\n" + + " \"age\": 5,\n" + + " \"status\": false\n" + + " }\n" + + " ]\n" + + " }\n" + + "}"; + + @BeforeClass(alwaysRun = true) + public void init() throws Exception { + + super.init(); + } + + @Test(groups = {"wso2.esb"}, description = "Testing ForEach mediator with JSON array replace") + public void testForEachJSONBodyReplace() throws IOException { + + String serviceURL = getMainSequenceURL() + "foreach/json-body-replace-1"; + HttpResponse httpResponse = httpClient.doPost(serviceURL, null, requestPayload, "application/json"); + String responsePayload = httpClient.getResponsePayload(httpResponse); + + JsonElement responseJSON = JsonParser.parseString(responsePayload); + JsonElement expectedJSON = JsonParser.parseString(expected); + assertEquals(responseJSON, expectedJSON, "Response payload mismatched"); + } + + @Test(groups = {"wso2.esb"}, description = "Testing ForEach mediator with JSON array replace with external call") + public void testForEachJSONBodyReplaceWithCallMediator() throws IOException { + + String serviceURL = getMainSequenceURL() + "foreach/json-body-replace-2"; + HttpResponse httpResponse = httpClient.doPost(serviceURL, null, requestPayload, "application/json"); + String responsePayload = httpClient.getResponsePayload(httpResponse); + + JsonElement responseJSON = JsonParser.parseString(responsePayload); + JsonElement expectedJSON = JsonParser.parseString(expected); + assertEquals(responseJSON, expectedJSON, "Response payload mismatched"); + } + + @Test(groups = {"wso2.esb"}, description = "Testing ForEach mediator with JSON array output to variable") + public void testForEachJSONBody_VariableOutput() throws IOException { + + String serviceURL = getMainSequenceURL() + "foreach/json-body-variable-output"; + HttpResponse httpResponse = httpClient.doPost(serviceURL, null, requestPayload, "application/json"); + String responsePayload = httpClient.getResponsePayload(httpResponse); + + String expected = "{\n" + + " \"foreachResult\": [\n" + + " {\n" + + " \"_name\": \"apple\",\n" + + " \"age\": 5\n" + + " },\n" + + " {\n" + + " \"_name\": \"carrot\",\n" + + " \"age\": 5\n" + + " }\n" + + " ]\n" + + "}"; + + JsonElement responseJSON = JsonParser.parseString(responsePayload); + JsonElement expectedJSON = JsonParser.parseString(expected); + assertEquals(responseJSON, expectedJSON, "Response payload mismatched"); + } + + @Test(groups = {"wso2.esb"}, description = "Testing ForEach mediator with JSON array defined as a variable") + public void testForEachJSONVariable() throws IOException, InterruptedException { + + CarbonLogReader carbonLogReader = new CarbonLogReader(); + carbonLogReader.start(); + + String serviceURL = getMainSequenceURL() + "foreach/json-var-replace"; + HttpResponse httpResponse = httpClient.doGet(serviceURL, null); + Assert.assertEquals(httpResponse.getStatusLine().getStatusCode(), 200, "Response code mismatched"); + EntityUtils.consumeQuietly(httpResponse.getEntity()); + + boolean logLine1 = carbonLogReader + .checkForLog("Processed message : [{\"_name\":\"guava\",\"age\":5},{\"_name\":\"beet\",\"age\":5}]", + DEFAULT_TIMEOUT); + Assert.assertTrue(logLine1, "JSON array defined as a variable not replaced"); + carbonLogReader.stop(); + } + + @Test(groups = {"wso2.esb"}, description = "Testing ForEach mediator with JSON array defined as a variable and set output to a variable") + public void testForEachJSONVariable_VariableOutput() throws IOException, InterruptedException { + + CarbonLogReader carbonLogReader = new CarbonLogReader(); + carbonLogReader.start(); + + String requestPayload = "{\"data\":\"abc\"}"; + + String serviceURL = getMainSequenceURL() + "foreach/json-var-variable-output"; + HttpResponse httpResponse = httpClient.doPost(serviceURL, null, requestPayload, "application/json"); + Assert.assertEquals(httpResponse.getStatusLine().getStatusCode(), 200, "Response code mismatched"); + EntityUtils.consumeQuietly(httpResponse.getEntity()); + + boolean logLine1 = carbonLogReader + .checkForLog("Original list : {\"data\":{\"list\":[{\"name\":\"apple\",\"type\":\"fruit\"}," + + "{\"name\":\"carrot\",\"type\":\"vegetable\"}]}}", + DEFAULT_TIMEOUT); + Assert.assertTrue(logLine1, "Original list not logged"); + + boolean logLine2 = carbonLogReader + .checkForLog("Foreach output : [{\"_name\":\"apple\",\"age\":5},{\"_name\":\"carrot\",\"age\":5}]", + DEFAULT_TIMEOUT); + Assert.assertTrue(logLine2, "Foreach output not logged"); + + boolean logLine3 = carbonLogReader + .checkForLog("Request Body : {\"data\":\"abc\"}", + DEFAULT_TIMEOUT); + Assert.assertTrue(logLine3, "Request body not logged"); + + carbonLogReader.stop(); + } + + @Test(groups = {"wso2.esb"}, description = "Testing ForEach mediator with XML list derived from body") + public void testForEachXMLListFromBody() throws IOException, ParserConfigurationException, SAXException { + + String expectedResponse = "apple10" + + "carrot10"; + + String serviceURL = getMainSequenceURL() + "foreach/xml-body-replace"; + HttpResponse httpResponse = httpClient.doPost(serviceURL, null, requestPayloadXML, "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 ForEach mediator with XML list derived from body and set output to a variable") + public void testForEachXMLListFromBody_VariableOutput() throws IOException, ParserConfigurationException, SAXException, InterruptedException { + + CarbonLogReader carbonLogReader = new CarbonLogReader(); + carbonLogReader.start(); + + String serviceURL = getMainSequenceURL() + "foreach/xml-body-variable-output"; + HttpResponse httpResponse = httpClient.doPost(serviceURL, null, requestPayloadXML, "application/xml"); + + boolean logLine = carbonLogReader + .checkForLog("Processed result : apple2" + + "carrot2", + DEFAULT_TIMEOUT); + Assert.assertTrue(logLine, "Foreach output not logged"); + + carbonLogReader.stop(); + + String responsePayload = httpClient.getResponsePayload(httpResponse); + Document document1 = parseXML(requestPayloadXML); + Document document2 = parseXML(responsePayload); + + if (!document1.isEqualNode(document2)) { + Assert.fail("Response payload mismatched: " + responsePayload + " expected: " + requestPayloadXML); + } + } + + @Test(groups = {"wso2.esb"}, description = "Testing ForEach mediator with XML list derived from a variable and set output to a variable") + public void testForEachXMLListFromVariable_VariableOutput() throws IOException, ParserConfigurationException, SAXException, InterruptedException { + + CarbonLogReader carbonLogReader = new CarbonLogReader(); + carbonLogReader.start(); + + String serviceURL = getMainSequenceURL() + "foreach/xml-var-variable-output"; + HttpResponse httpResponse = httpClient.doPost(serviceURL, null, requestPayloadXML, "application/xml"); + + boolean originalList = carbonLogReader + .checkForLog("Original collection : applefruit" + + "carrotvegetable", + DEFAULT_TIMEOUT); + Assert.assertTrue(originalList, "Original XML list not logged"); + + boolean forEachOutput = carbonLogReader + .checkForLog("Processed collection : apple2" + + "carrot2", + DEFAULT_TIMEOUT); + Assert.assertTrue(forEachOutput, "Foreach output not logged"); + + carbonLogReader.stop(); + + String responsePayload = httpClient.getResponsePayload(httpResponse); + Document document1 = parseXML(requestPayloadXML); + Document document2 = parseXML(responsePayload); + + if (!document1.isEqualNode(document2)) { + Assert.fail("Response payload mismatched: " + responsePayload + " expected: " + requestPayloadXML); + } + } + + private static Document parseXML(String xml) throws IOException, ParserConfigurationException, SAXException { + + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + factory.setIgnoringElementContentWhitespace(true); + factory.setNamespaceAware(true); + DocumentBuilder builder = factory.newDocumentBuilder(); + Document document = builder.parse(new ByteArrayInputStream(xml.getBytes())); + document.normalizeDocument(); + return document; + } + + @AfterClass(alwaysRun = true) + private void destroy() throws Exception { + + super.cleanup(); + } +} diff --git a/integration/mediation-tests/tests-mediator-1/src/test/java/org/wso2/carbon/esb/mediator/test/v2/ScatterGatherTestCase.java b/integration/mediation-tests/tests-mediator-1/src/test/java/org/wso2/carbon/esb/mediator/test/v2/ScatterGatherTestCase.java index b4e92a7a67..a88c60f992 100644 --- a/integration/mediation-tests/tests-mediator-1/src/test/java/org/wso2/carbon/esb/mediator/test/v2/ScatterGatherTestCase.java +++ b/integration/mediation-tests/tests-mediator-1/src/test/java/org/wso2/carbon/esb/mediator/test/v2/ScatterGatherTestCase.java @@ -37,17 +37,18 @@ import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; -import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertTrue; public class ScatterGatherTestCase extends ESBIntegrationTest { SimpleHttpClient httpClient = new SimpleHttpClient(); + CarbonLogReader carbonLogReader = new CarbonLogReader(); @BeforeClass(alwaysRun = true) public void init() throws Exception { super.init(); + carbonLogReader.start(); } @Test(groups = {"wso2.esb"}, description = "Testing Scatter-Gather mediator with JSON body replace") @@ -87,27 +88,41 @@ public void testScatterGatherJSONBodyReplace() throws IOException { @Test(groups = {"wso2.esb"}, description = "Testing Scatter-Gather mediator with JSON and variable output") public void testScatterGatherJSONVariableOutput() throws IOException, InterruptedException { - CarbonLogReader carbonLogReader = new CarbonLogReader(); - carbonLogReader.start(); - String requestPayload = "{\n" + " \"requestId\": 1114567\n" + "}"; + String expected = "{\n" + + " \"response\":{\n" + + " \"requestData\":{\n" + + " \"requestId\":1114567\n" + + " },\n" + + " \"scatterGatherOutput\":[\n" + + " {\n" + + " \"name\":\"pet1\",\n" + + " \"type\":\"dog\",\n" + + " \"requestId\":1114567\n" + + " },\n" + + " {\n" + + " \"name\":\"pet2\",\n" + + " \"type\":\"cat\",\n" + + " \"requestId\":1114567\n" + + " },\n" + + " {\n" + + " \"name\":\"pet3\",\n" + + " \"type\":\"mock-backend\",\n" + + " \"requestId\":1114567\n" + + " }\n" + + " ]\n" + + " }\n" + + "}"; String serviceURL = getMainSequenceURL() + "scatter-gather/json-variable-output"; HttpResponse httpResponse = httpClient.doPost(serviceURL, null, requestPayload, "application/json"); String responsePayload = httpClient.getResponsePayload(httpResponse); JsonElement responseJSON = JsonParser.parseString(responsePayload); - JsonElement expectedJSON = JsonParser.parseString(requestPayload); - assertEquals(responseJSON, expectedJSON, "Response payload mismatched"); - - boolean logFound = carbonLogReader - .checkForLog("Scatter Gather output = [{\"name\":\"pet1\",\"type\":\"dog\",\"requestId\":1114567}," + - "{\"name\":\"pet2\",\"type\":\"cat\",\"requestId\":1114567},{\"name\":\"pet3\",\"type\":\"mock-backend\"," + - "\"requestId\":1114567}]", DEFAULT_TIMEOUT); - Assert.assertTrue(logFound, "Scatter Gather result not set to variable"); - carbonLogReader.stop(); + JsonElement expectedJSON = JsonParser.parseString(expected); + assertTrue(areJsonElementsEquivalent(expectedJSON, responseJSON), "Response payload mismatched"); } @Test(groups = {"wso2.esb"}, description = "Testing Scatter-Gather mediator with XML body replace") @@ -135,9 +150,7 @@ public void testScatterGatherXMLBodyReplace() throws IOException, ParserConfigur @Test(groups = {"wso2.esb"}, description = "Testing Scatter-Gather mediator with XML and variable output") public void testScatterGatherXMLVariableOutput() throws IOException, InterruptedException, ParserConfigurationException, SAXException { - CarbonLogReader carbonLogReader = new CarbonLogReader(); - carbonLogReader.start(); - + carbonLogReader.clearLogs(); String requestPayload = "\n" + " 78658\n" + ""; @@ -158,7 +171,6 @@ public void testScatterGatherXMLVariableOutput() throws IOException, Interrupted "78658pet2dog78658" + "", DEFAULT_TIMEOUT); Assert.assertTrue(logFound, "Scatter Gather result not set to variable"); - carbonLogReader.stop(); } @Test(groups = {"wso2.esb"}, description = "Testing Scatter-Gather mediator with Aggregation condition") @@ -262,6 +274,7 @@ private static Document parseXML(String xml) throws IOException, ParserConfigura @AfterClass(alwaysRun = true) private void destroy() throws Exception { + carbonLogReader.stop(); super.cleanup(); } } diff --git a/integration/mediation-tests/tests-mediator-1/src/test/resources/artifacts/ESB/server/repository/deployment/server/synapse-configs/default/api/TestForEachMediator.xml b/integration/mediation-tests/tests-mediator-1/src/test/resources/artifacts/ESB/server/repository/deployment/server/synapse-configs/default/api/TestForEachMediator.xml new file mode 100644 index 0000000000..9b1d4bc46e --- /dev/null +++ b/integration/mediation-tests/tests-mediator-1/src/test/resources/artifacts/ESB/server/repository/deployment/server/synapse-configs/default/api/TestForEachMediator.xml @@ -0,0 +1,236 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Processed message : ${var.list} + + + + + + + + + + + + + + + + + + + Original list : ${var.var_1} + + + Foreach output : ${var.result} + + + Request Body : ${payload} + + + + + + + + + + + + Processing message : ${xpath('/')} + + + + + ${xpath('//list/name/text()')} + 10 + + + + + + + + + + + + + + + + Processing message : ${xpath('/')} + + + + + + + + + + Processed message body : ${xpath('/')} + + + Processed result : ${var.foreach_result} + + + + + + + + + + + + + + + + + + + + Original collection : ${var.xml_collection} + + + Processed collection : ${var.foreach_result} + + + + + + + + + + + + + + + + + + + + + + + + ${xpath('//list/name/text()')} + 2 + + + + + + + + + diff --git a/integration/mediation-tests/tests-mediator-1/src/test/resources/artifacts/ESB/server/repository/deployment/server/synapse-configs/default/api/TestScatterGatherMediator.xml b/integration/mediation-tests/tests-mediator-1/src/test/resources/artifacts/ESB/server/repository/deployment/server/synapse-configs/default/api/TestScatterGatherMediator.xml index 255ac63ba4..6b002d76dc 100644 --- a/integration/mediation-tests/tests-mediator-1/src/test/resources/artifacts/ESB/server/repository/deployment/server/synapse-configs/default/api/TestScatterGatherMediator.xml +++ b/integration/mediation-tests/tests-mediator-1/src/test/resources/artifacts/ESB/server/repository/deployment/server/synapse-configs/default/api/TestScatterGatherMediator.xml @@ -94,6 +94,15 @@ Scatter Gather output = ${var.output_var} + + { + "response": { + "requestData": ${payload}, + "scatterGatherOutput": ${var.output_var} + } + } + +