/*
 * Copyright (c) 2015, 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.mdm.integration.common;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.testng.Assert;

import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * This class contains methods to make assertions easier and which are not available out of the box, from testing.
 */
public class AssertUtil {
    /**
     * This can be used to compare if to json strings are matched or not.
     *
     * @param expectedJsonPayload the expected json string.
     * @param realPayload         real json string.
     * @param mustMatch           If the real and expected must match, in order to become the test successful or not.
     */
    public static void jsonPayloadCompare(String expectedJsonPayload, String realPayload, boolean mustMatch) {
        JsonElement jsonElement = new JsonParser().parse(expectedJsonPayload);
        JsonObject expectedPayloadObject = jsonElement.getAsJsonObject();
        jsonElement = new JsonParser().parse(realPayload);
        JsonObject realPayloadObject = jsonElement.getAsJsonObject();
        if (mustMatch) {
            Assert.assertTrue(realPayloadObject.equals(expectedPayloadObject));
        } else {
            Assert.assertFalse(realPayloadObject.equals(expectedPayloadObject));
        }
    }

    /**
     * This can be used to compare expected soap payloads are compare with real payload.
     *
     * @param expectedXMLPayload the expected soap payload.
     * @param realPayload        real soap payload.
     * @param mustMatch          If the real soap and expected soap must match, in oder to become the test successful
     *                           or not
     */
    public static void soapPayloadCompare(String expectedXMLPayload, String realPayload, boolean mustMatch) {
        InputStream is = new ByteArrayInputStream(expectedXMLPayload.getBytes());
        InputStream os = new ByteArrayInputStream(realPayload.getBytes());
        try {
            SOAPMessage expectedPayloadObject = MessageFactory.newInstance().createMessage(null, is);
            SOAPMessage realPayloadObject = MessageFactory.newInstance().createMessage(null, os);
            if (mustMatch) {
                Assert.assertTrue(realPayloadObject.equals(expectedPayloadObject));
            } else {
                Assert.assertFalse(realPayloadObject.equals(expectedPayloadObject));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SOAPException e) {
            e.printStackTrace();
        }

    }
}

@@ -843,212 +843,8 @@
 t;%0A%0A
-import javax.xml.soap.MessageFactory;%0Aimport javax.xml.soap.SOAPException;%0Aimport javax.xml.soap.SOAPMessage;%0Aimport java.io.ByteArrayInputStream;%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0A%0A
 /**%0A
@@ -1930,1257 +1930,7 @@
    %7D
-%0A%0A    /**%0A     * This can be used to compare expected soap payloads are compare with real payload.%0A     *%0A     * @param expectedXMLPayload the expected soap payload.%0A     * @param realPayload        real soap payload.%0A     * @param mustMatch          If the real soap and expected soap must match, in oder to become the test successful%0A     *                           or not%0A     */%0A    public static void soapPayloadCompare(String expectedXMLPayload, String realPayload, boolean mustMatch) %7B%0A        InputStream is = new ByteArrayInputStream(expectedXMLPayload.getBytes());%0A        InputStream os = new ByteArrayInputStream(realPayload.getBytes());%0A        try %7B%0A            SOAPMessage expectedPayloadObject = MessageFactory.newInstance().createMessage(null, is);%0A            SOAPMessage realPayloadObject = MessageFactory.newInstance().createMessage(null, os);%0A            if (mustMatch) %7B%0A                Assert.assertTrue(realPayloadObject.equals(expectedPayloadObject));%0A            %7D else %7B%0A                Assert.assertFalse(realPayloadObject.equals(expectedPayloadObject));%0A            %7D%0A        %7D catch (IOException e) %7B%0A            e.printStackTrace();%0A        %7D catch (SOAPException e) %7B%0A            e.printStackTrace();%0A        %7D%0A%0A    %7D
 %0A%7D%0A

delete method for soapmessage compaire