/*
 * Copyright (c) 2017 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.esb.mediator.test.aggregate;

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.integration.common.admin.client.LogViewerClient;
import org.wso2.esb.integration.common.utils.ESBIntegrationTest;
import static org.wso2.esb.integration.common.utils.Utils.assertIfSystemLogContains;

import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 * Test for aggregating no content json responses at aggregate mediator
 */
public class AggregateEmptyJsonPayloadTestCase extends ESBIntegrationTest {
    
    private static final String PROXY_NAME = "aggregateEmptyJsonPayloadTestProxy";
    private LogViewerClient logViewerClient;

    @BeforeClass(alwaysRun = true)
    public void init() throws Exception {
        super.init();
        logViewerClient = new LogViewerClient(contextUrls.getBackEndUrl(), getSessionCookie());
        isProxyDeployed(PROXY_NAME);
    }

    /**
     * At the proxy service, requests will be sent iteratively for the no content response backend, and recieved
     * responses will be aggregated
     *
     * @throws Exception
     */
    @Test(groups = "wso2.esb", description = "Test CorrelateOn in Aggregate mediator ")
    public void testAggregateEmptyJsonPayload() throws Exception {

        String inputPayload = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
                "<soapenv:Header/>\n" +
                "<soapenv:Body>\n" +
                "<m0:getQuote xmlns:m0=\"http://services.samples\">\n" +
                " <m0:request>IBM\n" +
                " </m0:request>\n" +
                "   <m0:request>WSO2\n" +
                " </m0:request>\n" +
                "</m0:getQuote>\n" +
                "</soapenv:Body>\n" +
                "</soapenv:Envelope>";

        String expectedOutput = "<OverallResponse " +
                "xmlns=\"http://ws.apache.org/ns/synapse\"><jsonObject xmlns=\"\"/><jsonObject " +
                "xmlns=\"\"/></OverallResponse>";

        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("Content-type", "text/xml");
        requestHeader.put("SOAPAction", "urn:mediate");
        requestHeader.put("Accept", "application/json");
        HttpRequestUtil.doPost(new URL(getProxyServiceURLHttp(PROXY_NAME)), inputPayload, requestHeader);

        Assert.assertTrue(assertIfSystemLogContains(logViewerClient, expectedOutput),
                          "No content 204 responses are not properly aggregated at the aggregate mediator.");
    }

    @AfterClass(alwaysRun = true)
    public void stop() throws Exception {
        super.cleanup();
    }
}

@@ -2221,26 +2221,24 @@
 elope/%5C%22%3E%5Cn%22
- +
 %0A           
@@ -2233,32 +2233,34 @@
 %0A               
+ +
  %22%3Csoapenv:Heade
@@ -2261,26 +2261,24 @@
 :Header/%3E%5Cn%22
- +
 %0A           
@@ -2281,16 +2281,18 @@
         
+ +
  %22%3Csoape
@@ -2294,34 +2294,32 @@
 soapenv:Body%3E%5Cn%22
- +
 %0A               
@@ -2318,16 +2318,18 @@
         
+ +
  %22%3Cm0:ge
@@ -2371,26 +2371,24 @@
 amples%5C%22%3E%5Cn%22
- +
 %0A           
@@ -2387,24 +2387,26 @@
             
+ +
  %22 %3Cm0:reque
@@ -2414,18 +2414,16 @@
 t%3EIBM%5Cn%22
- +
 %0A       
@@ -2422,32 +2422,34 @@
 %0A               
+ +
  %22 %3C/m0:request%3E
@@ -2443,34 +2443,32 @@
 %3C/m0:request%3E%5Cn%22
- +
 %0A               
@@ -2467,16 +2467,18 @@
         
+ +
  %22   %3Cm0
@@ -2493,18 +2493,16 @@
 %3EWSO2%5Cn%22
- +
 %0A       
@@ -2509,16 +2509,18 @@
         
+ +
  %22 %3C/m0:
@@ -2526,26 +2526,24 @@
 :request%3E%5Cn%22
- +
 %0A           
@@ -2542,24 +2542,26 @@
             
+ +
  %22%3C/m0:getQu
@@ -2563,26 +2563,24 @@
 getQuote%3E%5Cn%22
- +
 %0A           
@@ -2575,32 +2575,34 @@
 %0A               
+ +
  %22%3C/soapenv:Body
@@ -2605,18 +2605,16 @@
 Body%3E%5Cn%22
- +
 %0A       
@@ -2621,16 +2621,18 @@
         
+ +
  %22%3C/soap
@@ -2699,18 +2699,16 @@
 sponse %22
- +
 %0A       
@@ -2707,32 +2707,34 @@
 %0A               
+ +
  %22xmlns=%5C%22http:/
@@ -2798,18 +2798,16 @@
 Object %22
- +
 %0A       
@@ -2814,16 +2814,18 @@
         
+ +
  %22xmlns=

correct formatting in AggregateEmptyJsonPayloadTest