From 8c02571971f714f6a4653206ac5aaa8a2d0320b0 Mon Sep 17 00:00:00 2001 From: Sanoj Punchihewa Date: Mon, 16 Dec 2024 18:09:34 +0530 Subject: [PATCH] Fix intermittent test failures in Scatter Gather tests --- .../test/v2/ScatterGatherTestCase.java | 47 ++++++++++++------- .../default/api/TestScatterGatherMediator.xml | 9 ++++ 2 files changed, 39 insertions(+), 17 deletions(-) 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/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} + } + } + +