/*
 * 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();
        }

    }
}

/*
 * 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;

/**
 * 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));
        }
    }
}

delete method for soapmessage compaire