package com.magnifyb.comparingcmsapi; import java.util.Iterator; import org.json.JSONException; import org.json.JSONObject; import org.testng.annotations.Test; import io.restassured.RestAssured; import io.restassured.response.Response; public class AboutUs1 { @Test public void aboutUs() { String devApiUrl = "https://magnifytest.machint.com/about_us"; String testApiUrl = "https://magnifyservice.machint.com/about_us"; Response devResponse = RestAssured.get(devApiUrl); Response testResponse = RestAssured.get(testApiUrl); // Extracting response bodies String devResponseBody = devResponse.getBody().asString(); String testResponseBody = testResponse.getBody().asString(); // Compare JSON responses try { if (compareJsonObjects(devResponseBody, testResponseBody)) { System.out.println("The responses are the same."); } else { System.out.println("The responses are different."); System.out.println("Response Differences:"); printDifferences(new JSONObject(devResponseBody), new JSONObject(testResponseBody)); } } catch (JSONException e) { e.printStackTrace(); } } private static boolean compareJsonObjects(String json1, String json2) throws JSONException { // Compare JSON objects JSONObject jsonObj1 = new JSONObject(json1); JSONObject jsonObj2 = new JSONObject(json2); return jsonObj1.equals(jsonObj2); } private static void printDifferences(JSONObject jsonObj1, JSONObject jsonObj2) throws JSONException { // Print differences between two JSON objects Iterator keys = jsonObj1.keys(); while (keys.hasNext()) { String key = keys.next(); if (!jsonObj2.has(key) || !jsonObj1.get(key).equals(jsonObj2.get(key))) { System.out.println( "Key: " + key + ",\n Dev Value: " + jsonObj1.get(key) + ",\n Test Value: " + jsonObj2.get(key)); } } } }