/*
 * Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.

 * WSO2 Inc. 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.ei.scenario.test;

import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.wso2.carbon.esb.scenario.test.common.http.HTTPUtils;
import org.wso2.carbon.esb.scenario.test.common.ScenarioTestBase;

import java.io.IOException;
import javax.xml.stream.XMLStreamException;


 //This class is to test if xml payload can be enriched before it goes to the backend server by using script mediator.
public class AlterRequestWithScriptTest extends ScenarioTestBase {

     private  String request =
             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
                     + "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
                     + "   <soap:Header />\n"
                     + "   <soap:Body>\n"
                     + "      <employees>\n"
                     + "         <employee>\n"
                     + "            <age>25</age>\n"
                     + "            <firstName>John</firstName>\n"
                     + "            <lastName>Doe</lastName>\n"
                     + "         </employee>\n"
                     + "         <employee>\n"
                     + "            <age>45</age>\n"
                     + "            <firstName>Anna</firstName>\n"
                     + "            <lastName>Smith</lastName>\n"
                     + "         </employee>\n"
                     + "         <employee>\n"
                     + "            <age>35</age>\n"
                     + "            <firstName>Peter</firstName>\n"
                     + "            <lastName>Jones</lastName>\n"
                     + "         </employee>\n"
                     + "      </employees>\n"
                     + "   </soap:Body>\n"
                     + "</soap:Envelope>";

     @BeforeClass
     public void init() throws Exception {
        super.init();
     }

    //This test is to verify if payload can be modified by removing first element using inline groovy script.
    @Test(description = "1.6.10.1")
    public void AlterPayloadByInlineGroovyScript() throws IOException, XMLStreamException {
        String url = getProxyServiceURLHttp("1_6_10_1_Proxy_AlterPayloadWithInlineGroovyScript");
        String testCaseID = "1.6.10.1";
        String expectedResponse =
                "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
                        + "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
                        + "   <soap:Header />\n"
                        + "   <soap:Body>\n"
                        + "      <employees>\n"
                        + "         <employee>\n"
                        + "            <age>45</age>\n"
                        + "            <firstName>Anna</firstName>\n"
                        + "            <lastName>Smith</lastName>\n"
                        + "         </employee>\n"
                        + "         <employee>\n"
                        + "            <age>35</age>\n"
                        + "            <firstName>Peter</firstName>\n"
                        + "            <lastName>Jones</lastName>\n"
                        + "         </employee>\n"
                        + "      </employees>\n"
                        + "   </soap:Body>\n"
                        + "</soap:Envelope>";

        HTTPUtils.invokeSoapActionAndAssert(url, request, testCaseID, expectedResponse, 200,
                "urn:mediate", "AlterPayloadByInlineGroovyScript");
    }

     //This test is to verify if payload can be modified by removing the last element using inline javascript.
     @Test(description = "1.6.10.2")
     public void AlterPayloadByInlineJavaScript() throws IOException, XMLStreamException {
         String url = getProxyServiceURLHttp("1_6_10_2_Proxy_AlterPayloadWithInlineJavaScript");
         String testCaseID = "1.6.10.2";
         String expectedResponse =
                 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
                         + "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
                         + "   <soap:Header />\n"
                         + "   <soap:Body>\n"
                         + "      <employees>\n"
                         + "         <employee>\n"
                         + "            <age>25</age>\n"
                         + "            <firstName>John</firstName>\n"
                         + "            <lastName>Doe</lastName>\n"
                         + "         </employee>\n"
                         + "         <employee>\n"
                         + "            <age>45</age>\n"
                         + "            <firstName>Anna</firstName>\n"
                         + "            <lastName>Smith</lastName>\n"
                         + "         </employee>\n"
                         + "      </employees>\n"
                         + "   </soap:Body>\n"
                         + "</soap:Envelope>";

         HTTPUtils.invokeSoapActionAndAssert(url, request, testCaseID, expectedResponse, 200,
                 "urn:mediate", "AlterPayloadByInlineJavaScript");
     }

    @AfterClass(description = "Server Cleanup",
                alwaysRun = true)
    public void cleanup() throws Exception {
        super.cleanup();
    }

}


@@ -2715,24 +2715,144 @@
 ovy script.%0A
+    // This testcase has been disabled since we need to manually paste the groovy-all-dependency jar into $EI_HOME/lib.%0A
     @Test(de
@@ -2873,16 +2873,32 @@
 .6.10.1%22
+,enabled = false
 )%0A    pu
@@ -2899,33 +2899,33 @@
     public void 
-A
+a
 lterPayloadByInl
@@ -4315,33 +4315,33 @@
 %22urn:mediate%22, %22
-A
+a
 lterPayloadByInl
@@ -4531,17 +4531,17 @@
 ic void 
-A
+a
 lterPayl
@@ -5963,17 +5963,17 @@
 iate%22, %22
-A
+a
 lterPayl

disable the testcase 1.6.10.1