From 5f1384323aec729f219111a884037596351553d6 Mon Sep 17 00:00:00 2001 From: Sanoj Punchihewa Date: Tue, 7 May 2024 09:32:55 +0530 Subject: [PATCH] Add test case to verify continue loop improvement added to Foreach mediator --- .../foreach/ForEachContinueLoopTestCase.java | 129 ++++++++++++++++++ .../default/api/ForEachContinueLoop.xml | 48 +++++++ .../src/test/resources/testng.xml | 1 + 3 files changed, 178 insertions(+) create mode 100644 integration/mediation-tests/tests-patches/src/test/java/org/wso2/carbon/esb/mediators/foreach/ForEachContinueLoopTestCase.java create mode 100644 integration/mediation-tests/tests-patches/src/test/resources/artifacts/ESB/server/repository/deployment/server/synapse-configs/default/api/ForEachContinueLoop.xml diff --git a/integration/mediation-tests/tests-patches/src/test/java/org/wso2/carbon/esb/mediators/foreach/ForEachContinueLoopTestCase.java b/integration/mediation-tests/tests-patches/src/test/java/org/wso2/carbon/esb/mediators/foreach/ForEachContinueLoopTestCase.java new file mode 100644 index 0000000000..9716f074b8 --- /dev/null +++ b/integration/mediation-tests/tests-patches/src/test/java/org/wso2/carbon/esb/mediators/foreach/ForEachContinueLoopTestCase.java @@ -0,0 +1,129 @@ +/* + * Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org) 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.mediators.foreach; + +import org.testng.Assert; +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; +import org.wso2.carbon.automation.test.utils.http.client.HttpRequestUtil; +import org.wso2.carbon.automation.test.utils.http.client.HttpResponse; +import org.wso2.esb.integration.common.utils.CarbonLogReader; +import org.wso2.esb.integration.common.utils.ESBIntegrationTest; +import org.wso2.esb.integration.common.utils.Utils; + +import java.net.URL; +import java.util.HashMap; +import java.util.Map; + +import static org.testng.Assert.assertTrue; + +/** + * Tests ForEach mediator with continue loop improvement + * Public issue https://github.com/wso2/micro-integrator/issues/3318 + */ +public class ForEachContinueLoopTestCase extends ESBIntegrationTest { + + private CarbonLogReader carbonLogReader; + + @BeforeClass(alwaysRun = true) + public void setEnvironment() throws Exception { + + super.init(); + carbonLogReader = new CarbonLogReader(); + carbonLogReader.start(); + } + + @Test(groups = "wso2.esb", description = "Tests ForEach mediator with continue loop improvement for JSON payload") + public void testForEachMediatorContinueLoopWithJSON() throws Exception { + + URL jsonUrl = new URL(getApiInvocationURL("ForEachTestAPI") + "/json"); + + String jsonPayload = "{ \n" + + " \"info\":[ \n" + + " { \n" + + " \"id\":\"IDABC1\",\n" + + " \"classid\":1,\n" + + " \"name\":\"ABC\"\n" + + " },\n" + + " { \n" + + " \"id\":\"IDEFG2\",\n" + + " \"classid\":2,\n" + + " \"name\":\"EFG\"\n" + + " },\n" + + " { \n" + + " \"id\":\"IDHIJ3\",\n" + + " \"classid\":3,\n" + + " \"name\":\"HIJ\"\n" + + " }\n" + + " ]\n" + + "}"; + + // JSON request + Map jsonHeaders = new HashMap<>(); + jsonHeaders.put("Content-Type", "application/json"); + HttpResponse response = HttpRequestUtil.doPost(jsonUrl, jsonPayload, jsonHeaders); + Assert.assertEquals(response.getResponseCode(), 200, "Expected response didn't receive"); + + assertTrue(Utils.checkForLog(carbonLogReader, "Error occurred while mediating the sequence for the " + + "foreach mediator, Sequence named Value {name ='null', keyValue ='asdf'} cannot be found. " + + "Continuing with the remaining", 60), + "Continue loop didn't work as expected for JSON payload"); + } + + @Test(groups = "wso2.esb", description = "Tests ForEach mediator with continue loop improvement for XML payload") + public void testForEachMediatorContinueLoopWithXML() throws Exception { + + URL xmlUrl = new URL(getApiInvocationURL("ForEachTestAPI") + "/xml"); + + String xmlPayload = "\n" + + "\t\n" + + "\t\tIDABC1\n" + + "\t\t1\n" + + "\t\tABC\n" + + "\t\n" + + "\t\n" + + "\t\tIDEFG2\n" + + "\t\t2\n" + + "\t\tEFG\n" + + "\t\n" + + "\t\n" + + "\t\tIDHIJ3\n" + + "\t\t3\n" + + "\t\tHIJ\n" + + "\t\n" + + ""; + + // XML request + Map xmlHeaders = new HashMap<>(); + xmlHeaders.put("Content-Type", "application/xml"); + HttpResponse response = HttpRequestUtil.doPost(xmlUrl, xmlPayload, xmlHeaders); + Assert.assertEquals(response.getResponseCode(), 200, "Expected response didn't receive"); + + assertTrue(Utils.checkForLog(carbonLogReader, "Error occurred while mediating the sequence for the " + + "foreach mediator, Sequence named Value {name ='null', keyValue ='ffff'} cannot be found." + + " Continuing with the remaining.", 60), + "Continue loop didn't work as expected for XML payload"); + } + + @AfterClass(alwaysRun = true) + public void destroy() throws Exception { + carbonLogReader.stop(); + } +} diff --git a/integration/mediation-tests/tests-patches/src/test/resources/artifacts/ESB/server/repository/deployment/server/synapse-configs/default/api/ForEachContinueLoop.xml b/integration/mediation-tests/tests-patches/src/test/resources/artifacts/ESB/server/repository/deployment/server/synapse-configs/default/api/ForEachContinueLoop.xml new file mode 100644 index 0000000000..bcdbac3c8c --- /dev/null +++ b/integration/mediation-tests/tests-patches/src/test/resources/artifacts/ESB/server/repository/deployment/server/synapse-configs/default/api/ForEachContinueLoop.xml @@ -0,0 +1,48 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/integration/mediation-tests/tests-patches/src/test/resources/testng.xml b/integration/mediation-tests/tests-patches/src/test/resources/testng.xml index db9ca54cdb..c4137caa23 100644 --- a/integration/mediation-tests/tests-patches/src/test/resources/testng.xml +++ b/integration/mediation-tests/tests-patches/src/test/resources/testng.xml @@ -67,6 +67,7 @@ +