diff --git a/src/main/java/org/skyscreamer/jsonassert/JSONCompare.java b/src/main/java/org/skyscreamer/jsonassert/JSONCompare.java index b963be9f..ba11b4b9 100644 --- a/src/main/java/org/skyscreamer/jsonassert/JSONCompare.java +++ b/src/main/java/org/skyscreamer/jsonassert/JSONCompare.java @@ -65,6 +65,38 @@ else if (expected instanceof JSONObject) { } } + /** + * Compares JSON string provided to the expected JSON string using provided comparator, and returns the results of + * the comparison. + * @param expectedStr Expected JSON string + * @param actualStr JSON string to compare + * @param comparator Comparator to use + * @param verbose Showing the verbose diff if set to true + * @return result of the comparison + * @throws JSONException JSON parsing error + * @throws IllegalArgumentException when type of expectedStr doesn't match the type of actualStr + */ + public static JSONCompareResult compareJSON(String expectedStr, String actualStr, JSONComparator comparator, Boolean verbose) + throws JSONException { + Object expected = JSONParser.parseJSON(expectedStr); + Object actual = JSONParser.parseJSON(actualStr); + if ((expected instanceof JSONObject) && (actual instanceof JSONObject)) { + return compareJSON((JSONObject) expected, (JSONObject) actual, comparator, verbose); + } + else if ((expected instanceof JSONArray) && (actual instanceof JSONArray)) { + return compareJSON((JSONArray)expected, (JSONArray)actual, comparator); + } + else if (expected instanceof JSONString && actual instanceof JSONString) { + return compareJson((JSONString) expected, (JSONString) actual); + } + else if (expected instanceof JSONObject) { + return new JSONCompareResult().fail("", expected, actual); + } + else { + return new JSONCompareResult().fail("", expected, actual); + } + } + /** * Compares JSON object provided to the expected JSON object using provided comparator, and returns the results of * the comparison. @@ -79,6 +111,21 @@ public static JSONCompareResult compareJSON(JSONObject expected, JSONObject actu return comparator.compareJSON(expected, actual); } + /** + * Compares JSON object provided to the expected JSON object using provided comparator, and returns the results of + * the comparison. + * @param expected expected json object + * @param actual actual json object + * @param comparator comparator to use + * @param verbose showing the verbose diff if set to true + * @return result of the comparison + * @throws JSONException JSON parsing error + */ + public static JSONCompareResult compareJSON(JSONObject expected, JSONObject actual, JSONComparator comparator, boolean verbose) + throws JSONException { + return comparator.compareJSON(expected, actual, verbose); + } + /** * Compares JSON object provided to the expected JSON object using provided comparator, and returns the results of * the comparison. @@ -125,6 +172,21 @@ public static JSONCompareResult compareJSON(String expectedStr, String actualStr return compareJSON(expectedStr, actualStr, getComparatorForMode(mode)); } + /** + * Compares JSON string provided to the expected JSON string, and returns the results of the comparison. + * + * @param expectedStr Expected JSON string + * @param actualStr JSON string to compare + * @param mode Defines comparison behavior + * @param verbose Showing the verbose diff if set to true + * @return result of the comparison + * @throws JSONException JSON parsing error + */ + public static JSONCompareResult compareJSON(String expectedStr, String actualStr, JSONCompareMode mode, Boolean verbose) + throws JSONException { + return compareJSON(expectedStr, actualStr, getComparatorForMode(mode), verbose); + } + /** * Compares JSONObject provided to the expected JSONObject, and returns the results of the comparison. * diff --git a/src/main/java/org/skyscreamer/jsonassert/comparator/AbstractComparator.java b/src/main/java/org/skyscreamer/jsonassert/comparator/AbstractComparator.java index 4f68e2d1..d278beeb 100644 --- a/src/main/java/org/skyscreamer/jsonassert/comparator/AbstractComparator.java +++ b/src/main/java/org/skyscreamer/jsonassert/comparator/AbstractComparator.java @@ -43,6 +43,24 @@ public final JSONCompareResult compareJSON(JSONObject expected, JSONObject actua return result; } + /** + * Compares JSONObject provided to the expected JSONObject, and returns the results of the comparison. + * + * @param expected Expected JSONObject + * @param actual JSONObject to compare + * @param verbose Showing verbose diff if set to true + * @throws JSONException JSON parsing error + */ + @Override + public final JSONCompareResult compareJSON(JSONObject expected, JSONObject actual, boolean verbose) throws JSONException { + JSONCompareResult result = new JSONCompareResult(); + compareJSON("", expected, actual, result); + if (verbose) { + result.fail("Verbose Diff:", expected.toString(), actual.toString()); + } + return result; + } + /** * Compares JSONArray provided to the expected JSONArray, and returns the results of the comparison. * diff --git a/src/main/java/org/skyscreamer/jsonassert/comparator/JSONComparator.java b/src/main/java/org/skyscreamer/jsonassert/comparator/JSONComparator.java index 65ee88e5..c3c16822 100644 --- a/src/main/java/org/skyscreamer/jsonassert/comparator/JSONComparator.java +++ b/src/main/java/org/skyscreamer/jsonassert/comparator/JSONComparator.java @@ -37,6 +37,18 @@ public interface JSONComparator { */ JSONCompareResult compareJSON(JSONObject expected, JSONObject actual) throws JSONException; + /** + * Compares two {@link JSONObject}s and returns the result of the comparison in a {@link JSONCompareResult} object. + * + * @param expected the expected JSON object + * @param actual the actual JSON object + * @param verbose showing the verbose diff if set to true + * @return the result of the comparison + * @throws JSONException JSON parsing error + */ + JSONCompareResult compareJSON(JSONObject expected, JSONObject actual, boolean verbose) throws JSONException; + + /** * Compares two {@link JSONArray}s and returns the result of the comparison in a {@link JSONCompareResult} object. * diff --git a/src/test/java/org/skyscreamer/jsonassert/JSONCompareTest.java b/src/test/java/org/skyscreamer/jsonassert/JSONCompareTest.java index 450adfde..195f1424 100644 --- a/src/test/java/org/skyscreamer/jsonassert/JSONCompareTest.java +++ b/src/test/java/org/skyscreamer/jsonassert/JSONCompareTest.java @@ -21,6 +21,7 @@ import static org.skyscreamer.jsonassert.JSONCompare.compareJSON; import static org.skyscreamer.jsonassert.JSONCompareMode.LENIENT; import static org.skyscreamer.jsonassert.JSONCompareMode.NON_EXTENSIBLE; +import static org.skyscreamer.jsonassert.JSONCompareMode.STRICT; import org.hamcrest.Description; import org.hamcrest.Matcher; @@ -32,6 +33,42 @@ * Unit tests for {@code JSONCompare}. */ public class JSONCompareTest { + @Test + public void reportsUnmatchedJSONObjectWithVerboseMessage() throws JSONException { + String json1 = "{\n" + + " \"id\": \"101\",\n" + + " \"key\": {\n" + + " \"a\": \"value\",\n" + + " \"b\": \"xyz\",\n" + + " \"c\": 201\n" + + " }\n" + + "}"; + + String json2 = "{\n" + + " \"id\": \"102\",\n" + + " \"key\": {\n" + + " \"a\": \"value\",\n" + + " \"d\": [1, 2]\n" + + " }\n" + + "}"; + + JSONCompareResult result = compareJSON(json1, json2, STRICT, true); + assertThat(result, failsWithMessage(equalTo("id\n" + + "Expected: 101\n" + + " got: 102\n" + + " ; key\n" + + "Expected: b\n" + + " but none found\n" + + " ; key\n" + + "Expected: c\n" + + " but none found\n" + + " ; key\n" + + "Unexpected: d\n" + + " ; Verbose Diff:\n" + + "Expected: {\"id\":\"101\",\"key\":{\"a\":\"value\",\"b\":\"xyz\",\"c\":201}}\n" + + " got: {\"id\":\"102\",\"key\":{\"a\":\"value\",\"d\":[1,2]}}\n"))); + } + @Test public void succeedsWithEmptyArrays() throws JSONException { assertTrue(compareJSON("[]", "[]", LENIENT).passed());