From d0c93cdf30d4a446c772341c93c79f46c059cb4f Mon Sep 17 00:00:00 2001 From: Jack Fox Date: Sun, 6 Mar 2022 18:06:28 -0500 Subject: [PATCH 1/4] Fixed issue where CustomComparator is ignored when matching certain regular expressions that traverse JSON arrays --- .../comparator/AbstractComparator.java | 16 ++++++++++++-- .../jsonassert/JSONCustomComparatorTest.java | 22 +++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/skyscreamer/jsonassert/comparator/AbstractComparator.java b/src/main/java/org/skyscreamer/jsonassert/comparator/AbstractComparator.java index 4f68e2d1..7b824dfb 100644 --- a/src/main/java/org/skyscreamer/jsonassert/comparator/AbstractComparator.java +++ b/src/main/java/org/skyscreamer/jsonassert/comparator/AbstractComparator.java @@ -43,6 +43,12 @@ public final JSONCompareResult compareJSON(JSONObject expected, JSONObject actua return result; } + public final JSONCompareResult compareJSONPreserveContext(String prefix, JSONObject expected, JSONObject actual) throws JSONException{ + JSONCompareResult result = new JSONCompareResult(); + compareJSON(prefix, expected, actual, result); + return result; + } + /** * Compares JSONArray provided to the expected JSONArray, and returns the results of the comparison. * @@ -57,6 +63,12 @@ public final JSONCompareResult compareJSON(JSONArray expected, JSONArray actual) return result; } + public final JSONCompareResult compareJSONPreserveContext(String prefix, JSONArray expected, JSONArray actual) throws JSONException{ + JSONCompareResult result = new JSONCompareResult(); + compareJSONArray(prefix, expected, actual, result); + return result; + } + protected void checkJsonObjectKeysActualInExpected(String prefix, JSONObject expected, JSONObject actual, JSONCompareResult result) { Set actualKeys = getKeys(actual); for (String key : actualKeys) { @@ -153,13 +165,13 @@ protected void recursivelyCompareJSONArray(String key, JSONArray expected, JSONA continue; } if (expectedElement instanceof JSONObject) { - if (compareJSON((JSONObject) expectedElement, (JSONObject) actualElement).passed()) { + if (compareJSONPreserveContext(key + "[" + i + "]",(JSONObject) expectedElement, (JSONObject) actualElement).passed()) { matched.add(j); matchFound = true; break; } } else if (expectedElement instanceof JSONArray) { - if (compareJSON((JSONArray) expectedElement, (JSONArray) actualElement).passed()) { + if (compareJSONPreserveContext(key + "[" + i + "]", (JSONArray) expectedElement, (JSONArray) actualElement).passed()) { matched.add(j); matchFound = true; break; diff --git a/src/test/java/org/skyscreamer/jsonassert/JSONCustomComparatorTest.java b/src/test/java/org/skyscreamer/jsonassert/JSONCustomComparatorTest.java index 5f4c36ca..def6b5ff 100644 --- a/src/test/java/org/skyscreamer/jsonassert/JSONCustomComparatorTest.java +++ b/src/test/java/org/skyscreamer/jsonassert/JSONCustomComparatorTest.java @@ -21,6 +21,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertFalse; import static org.skyscreamer.jsonassert.JSONCompare.compareJSON; public class JSONCustomComparatorTest { @@ -118,6 +119,15 @@ public class JSONCustomComparatorTest { " }\n" + "}"; + String rootDeepWildcardWithArray = "{\n" + + " \"addresses\": [{\n" + + " \"address\": {\n" + + " \"num\": \"not_a_number\"" + + " }\n" + + " }]\n" + + "}"; + + int comparatorCallCount = 0; ValueMatcher comparator = new ValueMatcher() { @Override @@ -166,4 +176,16 @@ public void whenRootDeepWildcardPathMatchesCallCustomMatcher() throws JSONExcept assertTrue(result.getMessage(), result.passed()); assertEquals(4, comparatorCallCount); } + + @Test + public void whenRootDeepWildcardPathWithArrayMatchesCallCustomMatcher() throws JSONException { + JSONComparator jsonCmpStrict = new CustomComparator(JSONCompareMode.STRICT, new Customization("addresses[*].address.num", new RegularExpressionValueMatcher("\\d"))); + JSONComparator jsonCmpNonExtensible = new CustomComparator(JSONCompareMode.NON_EXTENSIBLE, new Customization("addresses[*].address.num", new RegularExpressionValueMatcher("\\d"))); + + JSONCompareResult resultStrict = compareJSON(rootDeepWildcardWithArray, rootDeepWildcardWithArray, jsonCmpStrict); + assertFalse(resultStrict.getMessage(), resultStrict.passed()); + + JSONCompareResult resultNonExtensible = compareJSON(rootDeepWildcardWithArray, rootDeepWildcardWithArray, jsonCmpNonExtensible); + assertFalse(resultNonExtensible.getMessage(), resultNonExtensible.passed()); + } } From e80fb1d801ca2d2ad5e3ecf060a0d7b87e00fc9c Mon Sep 17 00:00:00 2001 From: Jack Fox Date: Sun, 6 Mar 2022 18:10:44 -0500 Subject: [PATCH 2/4] Added lenient test --- .../org/skyscreamer/jsonassert/JSONCustomComparatorTest.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/test/java/org/skyscreamer/jsonassert/JSONCustomComparatorTest.java b/src/test/java/org/skyscreamer/jsonassert/JSONCustomComparatorTest.java index def6b5ff..dbf35d01 100644 --- a/src/test/java/org/skyscreamer/jsonassert/JSONCustomComparatorTest.java +++ b/src/test/java/org/skyscreamer/jsonassert/JSONCustomComparatorTest.java @@ -181,11 +181,15 @@ public void whenRootDeepWildcardPathMatchesCallCustomMatcher() throws JSONExcept public void whenRootDeepWildcardPathWithArrayMatchesCallCustomMatcher() throws JSONException { JSONComparator jsonCmpStrict = new CustomComparator(JSONCompareMode.STRICT, new Customization("addresses[*].address.num", new RegularExpressionValueMatcher("\\d"))); JSONComparator jsonCmpNonExtensible = new CustomComparator(JSONCompareMode.NON_EXTENSIBLE, new Customization("addresses[*].address.num", new RegularExpressionValueMatcher("\\d"))); + JSONComparator jsonCmpNonLenient = new CustomComparator(JSONCompareMode.NON_EXTENSIBLE, new Customization("addresses[*].address.num", new RegularExpressionValueMatcher("\\d"))); JSONCompareResult resultStrict = compareJSON(rootDeepWildcardWithArray, rootDeepWildcardWithArray, jsonCmpStrict); assertFalse(resultStrict.getMessage(), resultStrict.passed()); JSONCompareResult resultNonExtensible = compareJSON(rootDeepWildcardWithArray, rootDeepWildcardWithArray, jsonCmpNonExtensible); assertFalse(resultNonExtensible.getMessage(), resultNonExtensible.passed()); + + JSONCompareResult resultLenient = compareJSON(rootDeepWildcardWithArray, rootDeepWildcardWithArray, jsonCmpNonExtensible); + assertFalse(resultLenient.getMessage(), resultLenient.passed()); } } From b1e91b85aba374ede88d682e5cdc1ce8ce94e37d Mon Sep 17 00:00:00 2001 From: Jack Fox Date: Sun, 6 Mar 2022 19:14:57 -0500 Subject: [PATCH 3/4] Fixed typo in unit test case --- .../org/skyscreamer/jsonassert/JSONCustomComparatorTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/org/skyscreamer/jsonassert/JSONCustomComparatorTest.java b/src/test/java/org/skyscreamer/jsonassert/JSONCustomComparatorTest.java index dbf35d01..b98fd01e 100644 --- a/src/test/java/org/skyscreamer/jsonassert/JSONCustomComparatorTest.java +++ b/src/test/java/org/skyscreamer/jsonassert/JSONCustomComparatorTest.java @@ -189,7 +189,7 @@ public void whenRootDeepWildcardPathWithArrayMatchesCallCustomMatcher() throws J JSONCompareResult resultNonExtensible = compareJSON(rootDeepWildcardWithArray, rootDeepWildcardWithArray, jsonCmpNonExtensible); assertFalse(resultNonExtensible.getMessage(), resultNonExtensible.passed()); - JSONCompareResult resultLenient = compareJSON(rootDeepWildcardWithArray, rootDeepWildcardWithArray, jsonCmpNonExtensible); + JSONCompareResult resultLenient = compareJSON(rootDeepWildcardWithArray, rootDeepWildcardWithArray, jsonCmpNonLenient); assertFalse(resultLenient.getMessage(), resultLenient.passed()); } } From 7c251617465dd7894cc664dc720581a0b257869f Mon Sep 17 00:00:00 2001 From: Jack Fox Date: Sun, 6 Mar 2022 19:23:06 -0500 Subject: [PATCH 4/4] Fixed test case to test for edge case in lenient mode --- .../jsonassert/JSONCustomComparatorTest.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/test/java/org/skyscreamer/jsonassert/JSONCustomComparatorTest.java b/src/test/java/org/skyscreamer/jsonassert/JSONCustomComparatorTest.java index b98fd01e..d66d1e6c 100644 --- a/src/test/java/org/skyscreamer/jsonassert/JSONCustomComparatorTest.java +++ b/src/test/java/org/skyscreamer/jsonassert/JSONCustomComparatorTest.java @@ -120,8 +120,8 @@ public class JSONCustomComparatorTest { "}"; String rootDeepWildcardWithArray = "{\n" + - " \"addresses\": [{\n" + - " \"address\": {\n" + + " \"foo\": [{\n" + + " \"bar\": {\n" + " \"num\": \"not_a_number\"" + " }\n" + " }]\n" + @@ -178,10 +178,10 @@ public void whenRootDeepWildcardPathMatchesCallCustomMatcher() throws JSONExcept } @Test - public void whenRootDeepWildcardPathWithArrayMatchesCallCustomMatcher() throws JSONException { - JSONComparator jsonCmpStrict = new CustomComparator(JSONCompareMode.STRICT, new Customization("addresses[*].address.num", new RegularExpressionValueMatcher("\\d"))); - JSONComparator jsonCmpNonExtensible = new CustomComparator(JSONCompareMode.NON_EXTENSIBLE, new Customization("addresses[*].address.num", new RegularExpressionValueMatcher("\\d"))); - JSONComparator jsonCmpNonLenient = new CustomComparator(JSONCompareMode.NON_EXTENSIBLE, new Customization("addresses[*].address.num", new RegularExpressionValueMatcher("\\d"))); + public void whenDeepWildcardPathWithArrayMatchesCallCustomMatcher() throws JSONException { + JSONComparator jsonCmpStrict = new CustomComparator(JSONCompareMode.STRICT, new Customization("foo[*].bar.num", new RegularExpressionValueMatcher("\\d"))); + JSONComparator jsonCmpNonExtensible = new CustomComparator(JSONCompareMode.NON_EXTENSIBLE, new Customization("foo[*].bar.num", new RegularExpressionValueMatcher("\\d"))); + JSONComparator jsonCmpNonLenient = new CustomComparator(JSONCompareMode.LENIENT, new Customization("foo[*].bar.num", new RegularExpressionValueMatcher("\\d"))); JSONCompareResult resultStrict = compareJSON(rootDeepWildcardWithArray, rootDeepWildcardWithArray, jsonCmpStrict); assertFalse(resultStrict.getMessage(), resultStrict.passed());