Skip to content

Commit

Permalink
Fix intermittent test failures in Scatter Gather tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SanojPunchihewa committed Dec 16, 2024
1 parent db02fd2 commit 8c02571
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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 = "<root>\n" +
" <requestId>78658</requestId>\n" +
"</root>";
Expand All @@ -158,7 +171,6 @@ public void testScatterGatherXMLVariableOutput() throws IOException, Interrupted
"<requestId>78658</requestId></pet><pet><name>pet2</name><type>dog</type><requestId>78658</requestId>" +
"</pet></scatter_response>", 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")
Expand Down Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@
<log>
<message>Scatter Gather output = ${var.output_var}</message>
</log>
<payloadFactory media-type="json" template-type="default">
<format>{
"response": {
"requestData": ${payload},
"scatterGatherOutput": ${var.output_var}
}
}
</format>
</payloadFactory>
<respond/>
</inSequence>
</resource>
Expand Down

0 comments on commit 8c02571

Please sign in to comment.