diff --git a/Changelog.md b/Changelog.md index 4f77ea7..e2f5432 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,6 +1,8 @@ # Changelog -## 6.19-SNAPSHOT +## 7.0 (2025-03-28) +- #### Changed + - Changed style for JSON diffs ## 6.18 (2024-09-26) - #### Changed diff --git a/pom.xml b/pom.xml index 7ff7571..b4ef225 100644 --- a/pom.xml +++ b/pom.xml @@ -22,7 +22,7 @@ com.github.fslev json-compare - 6.19-SNAPSHOT + 7.0-SNAPSHOT diff --git a/src/main/java/io/json/compare/JSONCompare.java b/src/main/java/io/json/compare/JSONCompare.java index 849ea15..36de4e6 100644 --- a/src/main/java/io/json/compare/JSONCompare.java +++ b/src/main/java/io/json/compare/JSONCompare.java @@ -8,6 +8,7 @@ import java.io.IOException; import java.util.List; import java.util.Set; +import java.util.stream.Collectors; /** @@ -72,7 +73,7 @@ public static void assertMatches(Object expected, Object actual, JsonComparator String defaultMessage = String.format("FOUND %s DIFFERENCE(S):%s%s%s", diffs.size(), System.lineSeparator(), diffs.stream().map(diff -> System.lineSeparator() + System.lineSeparator() + "_________________________DIFF__________________________" + - System.lineSeparator() + diff).reduce(String::concat).get(), System.lineSeparator()); + System.lineSeparator() + "$" + diff).reduce(String::concat).get(), System.lineSeparator()); if (comparator == null || comparator.getClass().equals(DefaultJsonComparator.class)) { defaultMessage += System.lineSeparator() + System.lineSeparator() + ASSERTION_ERROR_HINT_MESSAGE + System.lineSeparator(); } @@ -110,8 +111,9 @@ public static List diffs(Object expected, Object actual, JsonComparator public static List diffs(Object expected, Object actual, JsonComparator comparator, Set compareModes) { JsonNode expectedJson = toJson(expected); JsonNode actualJson = toJson(actual); - return new JsonMatcher(expectedJson, actualJson, + List diffs = new JsonMatcher(expectedJson, actualJson, comparator == null ? new DefaultJsonComparator(compareModes) : comparator, compareModes).match(); + return diffs.stream().map(diff -> "$" + diff).collect(Collectors.toList()); } public static String prettyPrint(JsonNode jsonNode) { diff --git a/src/main/java/io/json/compare/matcher/JsonArrayMatcher.java b/src/main/java/io/json/compare/matcher/JsonArrayMatcher.java index cd22040..3fc0aa4 100644 --- a/src/main/java/io/json/compare/matcher/JsonArrayMatcher.java +++ b/src/main/java/io/json/compare/matcher/JsonArrayMatcher.java @@ -34,7 +34,7 @@ public List match() { } } if (compareModes.contains(CompareMode.JSON_ARRAY_NON_EXTENSIBLE) && expected.size() - getDoNotMatchUseCases(expected) < actual.size()) { - diffs.add("Actual JSON ARRAY has extra elements"); + diffs.add(" -> Actual JSON ARRAY has extra elements"); } return diffs; } @@ -63,11 +63,9 @@ private List matchWithJsonArray(int expPosition, JsonNode expElement, Us return Collections.emptyList(); } else { if (compareModes.contains(CompareMode.JSON_ARRAY_STRICT_ORDER)) { - diffs.add(String.format("JSON ARRAY elements differ at position %s:" + - System.lineSeparator() + "%s" + System.lineSeparator() + - "________diffs________" + System.lineSeparator() + "%s", expPosition + 1, - MessageUtil.cropL(JSONCompare.prettyPrint(expElement)), String.join( - System.lineSeparator() + "_____________________" + System.lineSeparator(), elementDiffs))); + elementDiffs.forEach(elementDiff -> + diffs.add(String.format("[%s]%s", expPosition, elementDiff)) + ); return diffs; } } @@ -80,27 +78,26 @@ private List matchWithJsonArray(int expPosition, JsonNode expElement, Us if (areOfSameType(expElement, actElement)) { elementDiffs = new JsonMatcher(expElement, actElement, comparator, compareModes).match(); if (!elementDiffs.isEmpty()) { - diffs.add("Expected element from position " + (expPosition + 1) - + " was FOUND:" + System.lineSeparator() + MessageUtil.cropL(JSONCompare.prettyPrint(expElement))); + diffs.add("[" + expPosition + "]" + + " was found:" + System.lineSeparator() + MessageUtil.cropL(JSONCompare.prettyPrint(expElement))); return diffs; } } break; case DO_NOT_MATCH_ANY: if (expected.size() - getDoNotMatchUseCases(expected) < actual.size()) { - diffs.add(String.format("Expected condition %s from position %s was not met." + - " Actual JSON ARRAY has extra elements", - expElement, expPosition + 1)); + diffs.add(String.format("[%s] -> Expected condition %s was not met." + + " Actual JSON ARRAY has extra elements", expPosition, expElement)); } return diffs; } } if (useCase == UseCase.MATCH) { - diffs.add(System.lineSeparator() + "Expected element from position " + (expPosition + 1) + " was NOT FOUND:" + System.lineSeparator() + diffs.add("[" + expPosition + "] was not found:" + System.lineSeparator() + MessageUtil.cropL(JSONCompare.prettyPrint(expElement))); } else if (useCase == UseCase.MATCH_ANY) { - diffs.add(String.format("Expected condition %s from position %s was not met." + - " Actual JSON ARRAY has no extra elements", expElement, expPosition + 1)); + diffs.add(String.format("[%s] -> Expected condition %s was not met." + + " Actual JSON ARRAY has no extra elements", expPosition, expElement)); } return diffs; } diff --git a/src/main/java/io/json/compare/matcher/JsonMatcher.java b/src/main/java/io/json/compare/matcher/JsonMatcher.java index 648e962..3c0715e 100644 --- a/src/main/java/io/json/compare/matcher/JsonMatcher.java +++ b/src/main/java/io/json/compare/matcher/JsonMatcher.java @@ -29,7 +29,7 @@ public List match() { return Collections.emptyList(); } else { List diffs = new ArrayList<>(); - diffs.add("Different JSON types: expected " + expected.getClass().getSimpleName() + " but got " + actual.getClass().getSimpleName()); + diffs.add(" -> Different JSON types: expected " + expected.getClass().getSimpleName() + " but got " + actual.getClass().getSimpleName()); return diffs; } } diff --git a/src/main/java/io/json/compare/matcher/JsonObjectMatcher.java b/src/main/java/io/json/compare/matcher/JsonObjectMatcher.java index 3fe9a61..7a9ac0f 100644 --- a/src/main/java/io/json/compare/matcher/JsonObjectMatcher.java +++ b/src/main/java/io/json/compare/matcher/JsonObjectMatcher.java @@ -43,7 +43,7 @@ public List match() { case MATCH: if (!jsonPathExpression.isPresent()) { if (candidateEntries.isEmpty()) { - diffs.add(String.format("Field '%s' was NOT FOUND", expectedField)); + diffs.add(String.format(".%s was not found", expectedField)); } else { diffs.addAll(matchWithCandidates(expectedSanitizedField, expectedValue, candidateEntries)); } @@ -51,19 +51,19 @@ public List match() { try { diffs.addAll(new JsonPathMatcher(jsonPathExpression.get(), expectedValue, actual, comparator, compareModes).match()); } catch (PathNotFoundException e) { - diffs.add(String.format("Json path '%s' -> %s", jsonPathExpression.get(), e.getMessage())); + diffs.add(String.format("." + JSON_PATH_EXP_PREFIX + "%s" + JSON_PATH_EXP_SUFFIX + " -> Json path -> %s", jsonPathExpression.get(), e.getMessage())); } } break; case DO_NOT_MATCH_ANY: if (expected.size() - getDoNotMatchUseCases(expected) < actual.size()) { - diffs.add(String.format("Expected condition '%s' was not met. Actual JSON OBJECT has extra fields", expectedField)); + diffs.add(String.format(".\"%s\" condition was not met. Actual JSON OBJECT has extra fields", expectedField)); } break; case DO_NOT_MATCH: if (!jsonPathExpression.isPresent()) { if (!candidateEntries.isEmpty()) { - diffs.add(String.format("Field '%s' was FOUND", expectedField)); + diffs.add(String.format(".\"%s\" was found", expectedField)); } } else { try { @@ -71,13 +71,13 @@ public List match() { } catch (PathNotFoundException e) { break; } - diffs.add(String.format("Json path '%s' was FOUND", expectedField)); + diffs.add(String.format("." + JSON_PATH_EXP_PREFIX + "%s" + JSON_PATH_EXP_SUFFIX + " -> Json path was found", expectedField)); } break; } } if (compareModes.contains(CompareMode.JSON_OBJECT_NON_EXTENSIBLE) && expected.size() - getDoNotMatchUseCases(expected) < actual.size()) { - diffs.add("Actual JSON OBJECT has extra fields"); + diffs.add(" -> Actual JSON OBJECT has extra fields"); } return diffs; } @@ -101,7 +101,7 @@ private List matchWithCandidates(String expectedField, JsonNode expected matchedFieldNames.add(candidateField); return Collections.emptyList(); } else { - candidateDiffs.forEach(diff -> diffs.add(String.format("%s -> %s", expectedField, diff))); + candidateDiffs.forEach(diff -> diffs.add(String.format(".%s%s", expectedField, diff))); } } return diffs; diff --git a/src/main/java/io/json/compare/matcher/JsonPathMatcher.java b/src/main/java/io/json/compare/matcher/JsonPathMatcher.java index b25d37d..ce5d723 100644 --- a/src/main/java/io/json/compare/matcher/JsonPathMatcher.java +++ b/src/main/java/io/json/compare/matcher/JsonPathMatcher.java @@ -39,10 +39,9 @@ public List match() { List diffs = new ArrayList<>(); JsonNode result = MAPPER.convertValue(PARSE_CONTEXT.parse(actual).read(jsonPath), JsonNode.class); List jsonPathDiffs = new JsonMatcher(expected, result, comparator, compareModes).match(); - jsonPathDiffs.forEach(diff -> diffs.add(String.format("Json path '%s' -> Expected json path result:" + - System.lineSeparator() + "%s" + System.lineSeparator() + "But got:" + - System.lineSeparator() + "%s" + System.lineSeparator() + "________diffs________" + System.lineSeparator() + "%s", - jsonPath, expected, result, diff))); + jsonPathDiffs.forEach(diff -> diffs.add(String.format("." + JSON_PATH_EXP_PREFIX + "%s" + JSON_PATH_EXP_SUFFIX + "%s" + System.lineSeparator() + "Expected json path result:" + + System.lineSeparator() + "%s" + System.lineSeparator() + "But got:" + System.lineSeparator() + "%s" + System.lineSeparator(), + jsonPath, diff, expected, result))); return diffs; } } \ No newline at end of file diff --git a/src/test/java/io/json/compare/matcher/JSONCompareMessageTests.java b/src/test/java/io/json/compare/matcher/JSONCompareMessageTests.java index b37d99f..114c679 100644 --- a/src/test/java/io/json/compare/matcher/JSONCompareMessageTests.java +++ b/src/test/java/io/json/compare/matcher/JSONCompareMessageTests.java @@ -75,7 +75,7 @@ public void checkNullMessageFromJSONObjectFailedCompare() { try { JSONCompare.assertMatches(expected, actual, new HashSet<>(Arrays.asList(CompareMode.JSON_OBJECT_NON_EXTENSIBLE))); } catch (AssertionError e) { - assertTrue(e.getMessage().contains("Field 'a' was NOT FOUND")); + assertTrue(e.getMessage().contains("$.a was not found")); } } @@ -104,8 +104,8 @@ public void checkMessageFromFailedMatchingBetweenHighDepthJsons() { AssertionError error = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected, actual, new HashSet<>(Collections.singletonList(CompareMode.JSON_OBJECT_NON_EXTENSIBLE)))); assertTrue(error.getMessage().matches("(?s).*FOUND 2 DIFFERENCE.*" + - "@ -> instanceId2 -> Actual JSON OBJECT has extra fields.*" + - "@ -> Field 'version' was NOT FOUND.*")); + "\\Q$.@.instanceId2\\E -> Actual JSON OBJECT has extra fields.*" + + "\\Q$.@.version\\E was not found.*")); JSONCompare.assertNotMatches(expected, actual, new HashSet<>(Collections.singletonList(CompareMode.JSON_OBJECT_NON_EXTENSIBLE))); } diff --git a/src/test/java/io/json/compare/matcher/JSONPathCompareTests.java b/src/test/java/io/json/compare/matcher/JSONPathCompareTests.java index 2f08d66..459f2b9 100644 --- a/src/test/java/io/json/compare/matcher/JSONPathCompareTests.java +++ b/src/test/java/io/json/compare/matcher/JSONPathCompareTests.java @@ -9,24 +9,24 @@ import static org.junit.jupiter.api.Assertions.*; -public class JSONPathCompareTests { +class JSONPathCompareTests { @Test - public void compareJsonObjectsWithEscapedJsonPath() { + void compareJsonObjectsWithEscapedJsonPath() { String expected = "{\"\\\\#\\\\Q(\\\\Elorem ipsum\\\\Q)\\\\E\":\"val1\"}"; String actual = "{\"b\":\"val2\",\"#(lorem ipsum)\":\"val1\"}"; JSONCompare.assertMatches(expected, actual); } @Test - public void compareJsonObjectsWithEscapedJsonPath_negative() { + void compareJsonObjectsWithEscapedJsonPath_negative() { String expected = "{\"\\\\#\\\\Q(\\\\Elorem ipsum\\\\Q)\\\\E\":\"val1\"}"; String actual = "{\"b\":\"val2\",\"#(lorem ipsum2)\":\"val1\"}"; assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected, actual)); } @Test - public void compareJsonObjectsWithJsonPathPointingToValueNode() { + void compareJsonObjectsWithJsonPathPointingToValueNode() { String expected = "{\"#($.a)\":\"val1\"}"; String actual = "{\"b\":\"val2\",\"a\":\"val1\"}"; JSONCompare.assertMatches(expected, actual); @@ -45,7 +45,7 @@ public void compareJsonObjectsWithJsonPathPointingToValueNode() { } @Test - public void compareJsonObjectsWithJsonPathPointingToValueNode_negative() { + void compareJsonObjectsWithJsonPathPointingToValueNode_negative() { String expected = "{\"#($.a)\":\"val1\"}"; String actual = "{\"b\":\"val2\",\"a\":\"val2\"}"; assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected, actual)); @@ -64,7 +64,7 @@ public void compareJsonObjectsWithJsonPathPointingToValueNode_negative() { } @Test - public void compareJsonObjectWithJsonPathAgainstOtherJsonType() { + void compareJsonObjectWithJsonPathAgainstOtherJsonType() { String expected = "{\"#($.length())\":4}"; String actual = "[5,true,null,\"lorem ipsum\"]"; JSONCompare.assertMatches(expected, actual); @@ -93,7 +93,7 @@ public void compareJsonObjectWithJsonPathAgainstOtherJsonType() { } @Test - public void compareJsonObjectWithJsonPathAgainstOtherJsonType_negative() { + void compareJsonObjectWithJsonPathAgainstOtherJsonType_negative() { String expected = "{\"#($.length())\":1}"; String actual = "[5,true,null,\"lorem ipsum\"]"; assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected, actual)); @@ -108,21 +108,21 @@ public void compareJsonObjectWithJsonPathAgainstOtherJsonType_negative() { } @Test - public void compareJsonObjectsWithRelativeJsonPath() { + void compareJsonObjectsWithRelativeJsonPath() { String expected = "{\"a\":{\"a1\":{\"a11\":{\"#($.a)\":\"lorem\"}}}}"; String actual = "{\"b\":false,\"a\":{\"a2\":290.11,\"a1\":{\"b11\":null,\"a11\":{\"a\":\"lorem\"}}}}"; JSONCompare.assertMatches(expected, actual); } @Test - public void compareJsonObjectsWithRelativeJsonPath_negative() { + void compareJsonObjectsWithRelativeJsonPath_negative() { String expected = "{\"a\":{\"a1\":{\"a11\":{\"#($.a)\":true}}}}"; String actual = "{\"b\":false,\"a\":{\"a2\":290.11,\"a1\":{\"b11\":null,\"a11\":{\"a\":\"lorem\"}}}}"; assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected, actual)); } @Test - public void compareJsonObjectsWithRegexJsonPathExpression() { + void compareJsonObjectsWithRegexJsonPathExpression() { String expected = "{\"accounts\":[ {\n" + " \"#($.[?(@.note =~ /.*does not exist.*/)])\": [\n" + " \"!.*\"\n" + @@ -156,7 +156,7 @@ public void compareJsonObjectsWithRegexJsonPathExpression() { } @Test - public void compareJsonObjectsWithNegativeRegexJsonPathExpression() { + void compareJsonObjectsWithNegativeRegexJsonPathExpression() { String expected = "{\"accounts\":[ {\n" + " \"#($.[?(@.note =~ /(?!Second.*).*/)])\": [\n" + " \"!.*\"\n" + @@ -190,7 +190,7 @@ public void compareJsonObjectsWithNegativeRegexJsonPathExpression() { } @Test - public void checkInvalidOrNotFoundJsonPathErrorMessage() { + void checkInvalidOrNotFoundJsonPathErrorMessage() { String expected = "{\"a\":{\"a1\":{\"a11\":{\"#($.idontexist)\":\"lorem\"}}}}"; String actual = "{\"b\":false,\"a\":{\"a2\":290.11,\"a1\":{\"b11\":null,\"a11\":{\"a\":\"lorem\"}}}}"; try { @@ -198,27 +198,27 @@ public void checkInvalidOrNotFoundJsonPathErrorMessage() { } catch (AssertionError e) { assertTrue(e.getMessage().matches("(?s).*FOUND 1 DIFFERENCE.*" + "____DIFF_____.*" + - "a -> a1 -> a11 -> Json path \\Q'$.idontexist'\\E.*->.*No results for path.*idontexist.*")); + "\\Q$.a.a1.a11.#($.idontexist)\\E.*->.*No results for path.*idontexist.*")); return; } fail("No error thrown"); } @Test - public void incompleteDelimitationOfJsonPathExpression() { + void incompleteDelimitationOfJsonPathExpression() { String expected = "{\"#(a\":1}"; String actual = "{\"#(a\":1}"; JSONCompare.assertMatches(expected, actual); } @Test - public void checkJsonPathAssertionErrorMessage() { + void checkJsonPathAssertionErrorMessage() { String expected = "{\"a\":{\"a1\":{\"a11\":{\"#($.a)\":\"lorem1\"}}}}"; String actual = "{\"b\":false,\"a\":{\"a2\":290.11,\"a1\":{\"b11\":null,\"a11\":{\"a\":\"lorem\"}}}}"; try { JSONCompare.assertMatches(expected, actual); } catch (AssertionError e) { - assertTrue(e.getMessage().matches("(?s).*a -> a1 -> a11 -> \\QJson path '$.a' -> Expected json path result\\E.*" + + assertTrue(e.getMessage().matches("(?s).*\\Q$.a.a1.a11.#($.a)\\E.*Expected json path result.*" + "\"lorem1\".*" + "But got:.*" + "\"lorem\".*")); @@ -228,7 +228,7 @@ public void checkJsonPathAssertionErrorMessage() { } @Test - public void matchJsonObjectWithJsonPath_do_not_match_use_case() { + void matchJsonObjectWithJsonPath_do_not_match_use_case() { String expected = "{\"a\":{\"a1\":{\"a11\":{\"#($.a)\":\"!lorem1\"}}}}"; String actual = "{\"b\":false,\"a\":{\"a2\":290.11,\"a1\":{\"b11\":null,\"a11\":{\"a\":\"lorem\"}}}}"; JSONCompare.assertMatches(expected, actual); @@ -251,41 +251,41 @@ public void matchJsonObjectWithJsonPath_do_not_match_use_case() { } @Test - public void matchJsonObjectWithJsonPath_do_not_match_use_case_negative() { + void matchJsonObjectWithJsonPath_do_not_match_use_case_negative() { String expected = "{\"a\":{\"a1\":{\"a11\":{\"#($.a)\":\"!lorem1\"}}}}"; String actual = "{\"b\":false,\"a\":{\"a2\":290.11,\"a1\":{\"b11\":null,\"a11\":{\"a\":\"lorem1\"}}}}"; assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected, actual)); } @Test - public void matchJsonObjectWithJsonPath_do_not_match_use_case_on_field() { + void matchJsonObjectWithJsonPath_do_not_match_use_case_on_field() { String expected = "{\"a\":{\"a1\":{\"a11\":{\"!#($.b)\":\".*\"}}}}"; String actual = "{\"b\":false,\"a\":{\"a2\":290.11,\"a1\":{\"b11\":null,\"a11\":{\"a\":\"lorem1\"}}}}"; JSONCompare.assertMatches(expected, actual); } @Test - public void matchJsonObjectWithJsonPath_do_not_match_use_case_on_field_negative() { + void matchJsonObjectWithJsonPath_do_not_match_use_case_on_field_negative() { String expected = "{\"a\":{\"a1\":{\"a11\":{\"!#($.a)\":\".*\"}}}}"; String actual = "{\"b\":false,\"a\":{\"a2\":290.11,\"a1\":{\"b11\":null,\"a11\":{\"a\":\"lorem1\"}}}}"; try { JSONCompare.assertMatches(expected, actual); } catch (AssertionError e) { - assertTrue(e.getMessage().contains("Json path") && e.getMessage().contains("was FOUND")); + assertTrue(e.getMessage().contains("Json path") && e.getMessage().contains("was found")); return; } fail("JSONs match"); } @Test - public void matchJsonObjectWithJsonPath_do_not_match_use_case_on_field_negative1() { + void matchJsonObjectWithJsonPath_do_not_match_use_case_on_field_negative1() { String expected = "{\"a\":{\"a1\":{\"a11\":{\"!#($.a)\":\".*\"}}}}"; String actual = "{\"b\":false,\"a\":{\"a2\":290.11,\"a1\":{\"b11\":null,\"a11\":{\"a\":\"lorem1\"}}}}"; JSONCompare.assertNotMatches(expected, actual); } @Test - public void matchJsonObjectWithJsonPathAndRegex() { + void matchJsonObjectWithJsonPathAndRegex() { String expected = "{\"a\":{\"a1\":{\"a11\":{\"#($.a)\":\".*\"}}}}"; String actual = "{\"b\":false,\"a\":{\"a2\":290.11,\"a1\":{\"b11\":null,\"a11\":{\"a\":\"lorem\"}}}}"; JSONCompare.assertMatches(expected, actual); @@ -304,14 +304,14 @@ public void matchJsonObjectWithJsonPathAndRegex() { } @Test - public void matchJsonObjectWithJsonPathAndRegex_negative() { + void matchJsonObjectWithJsonPathAndRegex_negative() { final String expected = "{\"a\":{\"a1\":{\"a11\":{\"#($.a)\":\"lol.*\"}}}}"; final String actual = "{\"b\":false,\"a\":{\"a2\":290.11,\"a1\":{\"b11\":null,\"a11\":{\"a\":\"lorem\"}}}}"; assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected, actual)); } @Test - public void matchJsonObjectWithJsonPathAndCompareModes() { + void matchJsonObjectWithJsonPathAndCompareModes() { String expected = "{\"b\":false,\"a\":{\"a1\":\".*\", \"#($.a1)\":{\"b11\":null,\"a11\":{\"a\":\".*\"}},\"a2\":290.11}}"; String actual = "{\"b\":false,\"a\":{\"a2\":290.11,\"a1\":{\"b11\":null,\"a11\":{\"a\":\"lorem\"}}}}"; JSONCompare.assertMatches(expected, actual, new HashSet<>(Arrays.asList(CompareMode.JSON_OBJECT_NON_EXTENSIBLE))); @@ -321,7 +321,7 @@ public void matchJsonObjectWithJsonPathAndCompareModes() { } @Test - public void matchJsonArrayWithJsonPath() { + void matchJsonArrayWithJsonPath() { String expected = "[false,{\"#($.length())\":3},1]"; String actual = "[1,[false,245.2,null,\"test\"],false]"; JSONCompare.assertMatches(expected, actual); @@ -346,7 +346,7 @@ public void matchJsonArrayWithJsonPath() { } @Test - public void matchJsonObjectWithMultipleJsonPaths() { + void matchJsonObjectWithMultipleJsonPaths() { String expected = "{\"#($.b)\":false,\"a\":{\"#($.a1)\":{\"b11\":null,\"a11\":{\"a\":\".*\"}},\"#($.a2)\":290.11}}"; String actual = "{\"b\":false,\"a\":{\"a2\":290.11,\"a1\":{\"b11\":null,\"a11\":{\"a\":\"lorem\"}}}}"; JSONCompare.assertMatches(expected, actual); @@ -361,7 +361,7 @@ public void matchJsonObjectWithMultipleJsonPaths() { } @Test - public void matchJsonObjectWithDifferentJsonPaths() { + void matchJsonObjectWithDifferentJsonPaths() { String expected = "{\"#($.store..isbn)\":[\"0-395-19395-8\",\"0-553-21311-3\",\"!.*\"]}"; String actual = "{\n" + " \"store\": {\n" + @@ -425,7 +425,7 @@ public void matchJsonObjectWithDifferentJsonPaths() { } @Test - public void matchJsonObjectWithDifferentJsonPaths_negative() { + void matchJsonObjectWithDifferentJsonPaths_negative() { final String expected = "{\"#($.store..z)\":[\"0-395-19395-8\",\"0-553-21311-3\",\"!.*\"]}"; final String actual = "{\n" + " \"store\": {\n" + diff --git a/src/test/java/io/json/compare/matcher/diffs/JsonArrayDiffTests.java b/src/test/java/io/json/compare/matcher/diffs/JsonArrayDiffTests.java index 56e57f6..fb21e19 100644 --- a/src/test/java/io/json/compare/matcher/diffs/JsonArrayDiffTests.java +++ b/src/test/java/io/json/compare/matcher/diffs/JsonArrayDiffTests.java @@ -10,49 +10,49 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -public class JsonArrayDiffTests { +class JsonArrayDiffTests { @Test - public void compareJsonArraysAndCheckForFor1ElementNotFoundDifference() { + void compareJsonArraysAndCheckForFor1ElementNotFoundDifference() { String expected = "[\"a\",\"c\",1,2,true,false,12.091,null]"; String actual = "[\"a\",\"b\",1,2,true,false,12.091,null]"; AssertionError error = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected, actual)); - assertTrue(error.getMessage().matches("(?s).*FOUND 1 DIFFERENCE.*Expected element from position 2 was NOT FOUND.*\"c\".*")); + assertTrue(error.getMessage().matches("(?s).*FOUND 1 DIFFERENCE.*\\Q$[1]\\E was not found.*\"c\".*")); JSONCompare.assertNotMatches(expected, actual); } @Test - public void compareJsonArraysAndCheckForMultipleElementNotFoundDifferences() { + void compareJsonArraysAndCheckForMultipleElementNotFoundDifferences() { String expected = "[\"a\",\"c\",1,200,true,false,12.092,null,{\"lorem\":\"ipsum\"}]"; String actual = "[12.091,10,\"b\",1,\"a\",2,true,{\"lorem\":\"ipsum-updated\"},\"some text\",false]"; AssertionError error = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected, actual)); assertTrue(error.getMessage().matches("(?s).*FOUND 5 DIFFERENCE.*" + - "Expected element from position 2 was NOT FOUND.*\"c\".*" + - "Expected element from position 4 was NOT FOUND.*200.*" + - "Expected element from position 7 was NOT FOUND.*12.092.*" + - "Expected element from position 8 was NOT FOUND.*null.*" + - "Expected element from position 9 was NOT FOUND.*\\{.*\"lorem\".*\"ipsum\".*}.*")); + "\\Q$[1]\\E was not found.*\"c\".*" + + "\\Q$[3]\\E was not found.*200.*" + + "\\Q$[6]\\E was not found.*12.092.*" + + "\\Q$[7]\\E was not found.*null.*" + + "\\Q$[8]\\E was not found.*\\{.*\"lorem\".*\"ipsum\".*}.*")); JSONCompare.assertNotMatches(expected, actual); } @Test - public void compareJsonArraysAndCheckForOneMatchAnyDifferences() { + void compareJsonArraysAndCheckForOneMatchAnyDifferences() { String expected = "[\"a\",\".*\",1,\".*\",\".*\"]"; String actual = "[\"a\",true,1,{\"lorem\":\"ipsum\"}]"; AssertionError error = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected, actual)); assertTrue(error.getMessage().matches("(?s).*FOUND 1 DIFFERENCE.*" + - "Expected condition \"\\Q.*\\E\" from position 5 was not met. Actual JSON ARRAY has no extra elements.*")); + "\\Q$[4]\\E -> Expected condition \"\\Q.*\\E\" was not met. Actual JSON ARRAY has no extra elements.*")); JSONCompare.assertNotMatches(expected, actual); } @Test - public void compareJsonArraysAndCheckForMultipleMatchAnyDifferences() { + void compareJsonArraysAndCheckForMultipleMatchAnyDifferences() { String expected = "[\"a\",\".*\",1,\".*\",\".*\",\".*\"]"; String actual = "[\"a\",true,1,{\"lorem\":\"ipsum\"}]"; AssertionError error = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected, actual)); assertTrue(error.getMessage().matches("(?s).*FOUND 2 DIFFERENCE.*" + - "Expected condition \"\\Q.*\\E\" from position 5 was not met. Actual JSON ARRAY has no extra elements.*" + - "Expected condition \"\\Q.*\\E\" from position 6 was not met. Actual JSON ARRAY has no extra elements.*")); + "\\Q$[4]\\E -> Expected condition \"\\Q.*\\E\" was not met. Actual JSON ARRAY has no extra elements.*" + + "\\Q$[5]\\E -> Expected condition \"\\Q.*\\E\" was not met. Actual JSON ARRAY has no extra elements.*")); JSONCompare.assertNotMatches(expected, actual); // empty actual json array @@ -60,75 +60,74 @@ public void compareJsonArraysAndCheckForMultipleMatchAnyDifferences() { String actual1 = "[]"; AssertionError error1 = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected1, actual1)); assertTrue(error1.getMessage().matches("(?s).*FOUND 6 DIFFERENCE.*" + - "Expected element from position 1 was NOT FOUND.*\"a\".*" + - "Expected condition \".*\" from position 2 was not met. Actual JSON ARRAY has no extra elements.*" + - "Expected element from position 3 was NOT FOUND.*1.*" + - "Expected element from position 4 was NOT FOUND.*\"\\Q.*test\\E\".*" + - "Expected condition \".*\" from position 5 was not met. Actual JSON ARRAY has no extra elements.*" + - "Expected condition \".*\" from position 6 was not met. Actual JSON ARRAY has no extra elements.*")); + "\\Q$[0]\\E was not found.*\"a\".*" + + "\\Q$[1]\\E -> Expected condition \".*\" was not met. Actual JSON ARRAY has no extra elements.*" + + "\\Q$[2]\\E was not found.*1.*" + + "\\Q$[3]\\E was not found.*\"\\Q.*test\\E\".*" + + "\\Q$[4]\\E -> Expected condition \".*\" was not met. Actual JSON ARRAY has no extra elements.*" + + "\\Q$[5]\\E -> Expected condition \".*\" was not met. Actual JSON ARRAY has no extra elements.*")); JSONCompare.assertNotMatches(expected1, actual1); } @Test - public void compareJsonArraysAndCheckForOneDoNotMatchDifferences() { + void compareJsonArraysAndCheckForOneDoNotMatchDifferences() { String expected = "[1,\"!a\",true]"; String actual = "[\"a\",true,1,{\"lorem\":\"ipsum\"}]"; AssertionError error = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected, actual)); assertTrue(error.getMessage().matches("(?s).*FOUND 1 DIFFERENCE.*" + - "Expected element from position 2 was FOUND.*\"!a\".*")); + "\\Q$[1]\\E was found.*\"!a\".*")); JSONCompare.assertNotMatches(expected, actual); } @Test - public void compareJsonArraysAndCheckForMultipleDoNotMatchDifferences() { + void compareJsonArraysAndCheckForMultipleDoNotMatchDifferences() { String expected = "[{\"lorem\":\"ipsum\"},\"!1\",\"!a\",\"!true\"]"; String actual = "[\"a\",true,1,{\"lorem\":\"ipsum\"}]"; AssertionError error = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected, actual)); assertTrue(error.getMessage().matches("(?s).*FOUND 3 DIFFERENCE.*" + - "Expected element from position 2 was FOUND.*\"!1\".*" + - "Expected element from position 3 was FOUND.*\"!a\".*" + - "Expected element from position 4 was FOUND.*\"!true\".*")); + "\\Q$[1]\\E was found.*\"!1\".*" + + "\\Q$[2]\\E was found.*\"!a\".*" + + "\\Q$[3]\\E was found.*\"!true\".*")); JSONCompare.assertNotMatches(expected, actual); } @Test - public void compareJsonArraysAndCheckForOneDoNotMatchAnyDifferences() { + void compareJsonArraysAndCheckForOneDoNotMatchAnyDifferences() { String expected = "[{\"lorem\":\"ipsum\"},\"1\",\"!.*\"]"; String actual = "[\"a\",true,1,{\"lorem\":\"ipsum\"}]"; AssertionError error = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected, actual)); assertTrue(error.getMessage().matches("(?s).*FOUND 1 DIFFERENCE.*" + - "Expected condition \"\\Q!.*\\E\" from position 3 was not met. Actual JSON ARRAY has extra elements.*")); + "\\Q$[2]\\E -> Expected condition \"\\Q!.*\\E\" was not met. Actual JSON ARRAY has extra elements.*")); JSONCompare.assertNotMatches(expected, actual); } @Test - public void compareJsonArraysAndCheckForMultipleDoNotMatchAnyDifferences() { + void compareJsonArraysAndCheckForMultipleDoNotMatchAnyDifferences() { String expected = "[{\"lorem\":\"ipsum\"},\"!.*\",\"1\",\"!.*\",\"!.*\"]"; String actual = "[\"a\",true,1,{\"lorem\":\"ipsum\"}]"; AssertionError error = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected, actual)); assertTrue(error.getMessage().matches("(?s).*FOUND 3 DIFFERENCE.*" + - "Expected condition \"\\Q!.*\\E\" from position 2 was not met. Actual JSON ARRAY has extra elements.*" + - "Expected condition \"\\Q!.*\\E\" from position 4 was not met. Actual JSON ARRAY has extra elements.*" + - "Expected condition \"\\Q!.*\\E\" from position 5 was not met. Actual JSON ARRAY has extra elements.*")); + "\\Q$[1]\\E -> Expected condition \"\\Q!.*\\E\" was not met. Actual JSON ARRAY has extra elements.*" + + "\\Q$[3]\\E -> Expected condition \"\\Q!.*\\E\" was not met. Actual JSON ARRAY has extra elements.*" + + "\\Q$[4]\\E -> Expected condition \"\\Q!.*\\E\" was not met. Actual JSON ARRAY has extra elements.*")); JSONCompare.assertNotMatches(expected, actual); } @Test - public void compareJsonArraysAndCheckForJsonStrictOrderDifferences() { + void compareJsonArraysAndCheckForJsonStrictOrderDifferences() { String expected = "[{\"lorem\":\"ipsum\"},\"!.*\",\"1\",{\"lorem2\":\"ipsum2\",\"lorem3\":\"ipsum3\"}]"; String actual = "[\"a\",true,1,{\"lorem2\":\"ipsum-updated\"}]"; AssertionError error = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected, actual, new HashSet<>(Arrays.asList(CompareMode.JSON_ARRAY_STRICT_ORDER)))); - assertTrue(error.getMessage().matches("(?s).*FOUND 3 DIFFERENCE.*" + - "JSON ARRAY elements differ at position 1:.*lorem.*ipsum.*diffs.*Different JSON types: expected ObjectNode but got TextNode.*" + - "Expected condition \"\\Q!.*\\E\" from position 2 was not met. Actual JSON ARRAY has extra elements.*" + - "JSON ARRAY elements differ at position 4:.*lorem2.*ipsum2.*lorem3.*ipsum3.*diffs.*" + - "lorem2 ->.*Expected value: \"ipsum2\" But got: \"ipsum-updated\".*" + - "Field 'lorem3' was NOT FOUND.*")); + assertTrue(error.getMessage().matches("(?s).*FOUND 4 DIFFERENCE.*" + + "\\Q$[0]\\E -> Different JSON types: expected ObjectNode but got TextNode.*" + + "\\Q$[1]\\E -> Expected condition \"\\Q!.*\\E\" was not met. Actual JSON ARRAY has extra elements.*" + + "\\Q$[3].lorem2\\E.*Expected value: \"ipsum2\" But got: \"ipsum-updated\".*" + + "\\Q$[3].lorem3\\E was not found.*")); JSONCompare.assertNotMatches(expected, actual); } @Test - public void compareJsonArraysAndCheckForJsonNonExtensibleDifferences() { + void compareJsonArraysAndCheckForJsonNonExtensibleDifferences() { String expected = "[{\"lorem\":\"ipsum\"},\"a\"]"; String actual = "[\"a\",true,1,{\"lorem\":\"ipsum\"}]"; AssertionError error = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected, actual, new HashSet<>(Arrays.asList(CompareMode.JSON_ARRAY_NON_EXTENSIBLE)))); @@ -138,41 +137,38 @@ public void compareJsonArraysAndCheckForJsonNonExtensibleDifferences() { } @Test - public void compareJsonArraysAndCheckForJsonNonExtensibleAndOtherDifferences() { + void compareJsonArraysAndCheckForJsonNonExtensibleAndOtherDifferences() { String expected = "[{\"lorem\":\"ipsum\"},\"c\",\"!1\",true]"; String actual = "[\"a\",true,1,{\"lorem\":\"ipsum\"}]"; AssertionError error = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected, actual, new HashSet<>(Arrays.asList(CompareMode.JSON_ARRAY_NON_EXTENSIBLE)))); assertTrue(error.getMessage().matches("(?s).*FOUND 3 DIFFERENCE.*" + - "Expected element from position 2 was NOT FOUND.*\"c\".*" + - "Expected element from position 3 was FOUND.*\"!1\".*" + - "Actual JSON ARRAY has extra elements.*")); + "\\Q$[1]\\E was not found.*\"c\".*" + + "\\Q$[2]\\E was found.*\"!1\".*" + + "\\Q$\\E -> Actual JSON ARRAY has extra elements.*")); JSONCompare.assertNotMatches(expected, actual, new HashSet<>(Arrays.asList(CompareMode.JSON_ARRAY_NON_EXTENSIBLE))); String expected1 = "[{\"lorem\":\"ipsum\"},\"c\",\"!1\",true]"; String actual1 = "[\"a\",true,1,{\"lorem\":\"ipsum\"},-10.02]"; AssertionError error1 = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected1, actual1, new HashSet<>(Arrays.asList(CompareMode.JSON_ARRAY_NON_EXTENSIBLE)))); assertTrue(error1.getMessage().matches("(?s).*FOUND 3 DIFFERENCE.*" + - "Expected element from position 2 was NOT FOUND.*\"c\".*" + - "Expected element from position 3 was FOUND.*\"!1\".*" + - "Actual JSON ARRAY has extra elements.*")); + "\\Q$[1]\\E was not found.*\"c\".*" + + "\\Q$[2]\\E was found.*\"!1\".*" + + "\\Q$\\E -> Actual JSON ARRAY has extra elements.*")); JSONCompare.assertNotMatches(expected1, actual1, new HashSet<>(Arrays.asList(CompareMode.JSON_ARRAY_NON_EXTENSIBLE))); } @Test - public void compareJsonArraysAndCheckForJsonNonExtensibleAndAndJsonStrictOrderDifferences() { + void compareJsonArraysAndCheckForJsonNonExtensibleAndAndJsonStrictOrderDifferences() { String expected = "[{\"lorem\":\"ipsum\"},\"c\",\"!1\",true]"; String actual = "[\"a\",true,1,{\"lorem\":\"ipsum\"},-10.02]"; AssertionError error = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected, actual, new HashSet<>(Arrays.asList(CompareMode.JSON_ARRAY_NON_EXTENSIBLE, CompareMode.JSON_ARRAY_STRICT_ORDER)))); assertTrue(error.getMessage().matches("(?s).*FOUND 5 DIFFERENCE.*" + - "JSON ARRAY elements differ at position 1.*\"lorem\".*\"ipsum\".*diffs.*" + - "Different JSON types: expected ObjectNode but got TextNode.*" + - "JSON ARRAY elements differ at position 2.*" + - "\"c\".*diffs.*Expected value: \"c\" But got: true.*" + - "Expected element from position 3 was FOUND.*\"!1\".*" + - "JSON ARRAY elements differ at position 4.*true.*diffs.*" + - "Different JSON types: expected BooleanNode but got ObjectNode.*" + - "Actual JSON ARRAY has extra elements.*")); + "\\Q$[0]\\E -> Different JSON types: expected ObjectNode but got TextNode.*" + + "\\Q$[1]\\E.*Expected value: \"c\" But got: true.*" + + "\\Q$[2]\\E was found.*\"!1\".*" + + "\\Q$[3]\\E.*Different JSON types: expected BooleanNode but got ObjectNode.*" + + "\\Q$\\E -> Actual JSON ARRAY has extra elements.*")); JSONCompare.assertNotMatches(expected, actual, new HashSet<>(Arrays.asList(CompareMode.JSON_ARRAY_NON_EXTENSIBLE, CompareMode.JSON_ARRAY_STRICT_ORDER))); } diff --git a/src/test/java/io/json/compare/matcher/diffs/JsonCustomComparatorDiffTests.java b/src/test/java/io/json/compare/matcher/diffs/JsonCustomComparatorDiffTests.java index 8c3db91..cc12e11 100644 --- a/src/test/java/io/json/compare/matcher/diffs/JsonCustomComparatorDiffTests.java +++ b/src/test/java/io/json/compare/matcher/diffs/JsonCustomComparatorDiffTests.java @@ -11,10 +11,10 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -public class JsonCustomComparatorDiffTests { +class JsonCustomComparatorDiffTests { @Test - public void compareJsons() { + void compareJsons() { String expected = "{\"name\":\"test\",\"records\":[3]}"; String actual = "{\"name\":\"test\",\"records\":[1,2,3], \"otherRecords\":[4]}"; JSONCompare.assertMatches(expected, actual, new CustomComparator()); @@ -23,12 +23,12 @@ public void compareJsons() { String actual1 = "{\"name\":\"test\",\"records\":[1,2,3], \"otherRecords\":[4]}"; AssertionError error = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected1, actual1, new CustomComparator())); assertTrue(error.getMessage().matches("(?s).*FOUND 2 DIFFERENCE.*" + - "name ->.*Expected value: \"test1\" But got: \"test\".*" + - "records ->.*Expected element from position 2 was NOT FOUND.*4.*")); + "\\Q$.name\\E.*Expected value: \"test1\" But got: \"test\".*" + + "\\Q$.records[1]\\E was not found.*4.*")); } @Test - public void compareJsonsWithCompareModes() { + void compareJsonsWithCompareModes() { String expected = "{\"name\":\"test\",\"records\":[1,2,3], \"otherRecords\":[4]}"; String actual = "{\"name\":\"test\",\"records\":[1,2,3], \"otherRecords\":[4]}"; JSONCompare.assertMatches(expected, actual, new CustomComparator(), @@ -41,10 +41,10 @@ public void compareJsonsWithCompareModes() { new HashSet<>(Arrays.asList(CompareMode.JSON_OBJECT_NON_EXTENSIBLE, CompareMode.JSON_ARRAY_NON_EXTENSIBLE, CompareMode.JSON_ARRAY_STRICT_ORDER)))); assertTrue(error.getMessage().matches("(?s).*FOUND 4 DIFFERENCE.*" + - "records -> JSON ARRAY elements differ at position 1.*2.*Expected value: 2 But got: 1.*" + - "records -> JSON ARRAY elements differ at position 2.*1.*Expected value: 1 But got: 2.*" + - "records -> Actual JSON ARRAY has extra elements.*" + - "Actual JSON OBJECT has extra fields.*")); + "\\Q$.records[0]\\E.*Expected value: 2 But got: 1.*" + + "\\Qrecords[1]\\E.*Expected value: 1 But got: 2.*" + + "\\Qrecords\\E -> Actual JSON ARRAY has extra elements.*" + + "\\Q$\\E -> Actual JSON OBJECT has extra fields.*")); String expected2 = "{\"name\":\"test\",\"records\":[1,2]}"; String actual2 = "{\"name\":\"test\",\"records\":[1,2,3], \"otherRecords\":[4]}"; @@ -52,8 +52,8 @@ public void compareJsonsWithCompareModes() { new HashSet<>(Arrays.asList(CompareMode.JSON_OBJECT_NON_EXTENSIBLE, CompareMode.JSON_ARRAY_NON_EXTENSIBLE, CompareMode.JSON_ARRAY_STRICT_ORDER)))); assertTrue(error.getMessage().matches("(?s).*FOUND 2 DIFFERENCE.*" + - "records -> Actual JSON ARRAY has extra elements.*" + - "Actual JSON OBJECT has extra fields.*")); + "\\Q$.records\\E -> Actual JSON ARRAY has extra elements.*" + + "\\Q$\\E -> Actual JSON OBJECT has extra fields.*")); String expected3 = "{\"name\":\"test\",\"records\":[1,2,3]}"; String actual3 = "{\"name\":\"test\",\"records\":[1,2,3], \"otherRecords\":[4]}"; @@ -61,11 +61,11 @@ public void compareJsonsWithCompareModes() { new HashSet<>(Arrays.asList(CompareMode.JSON_OBJECT_NON_EXTENSIBLE, CompareMode.JSON_ARRAY_NON_EXTENSIBLE, CompareMode.JSON_ARRAY_STRICT_ORDER)))); assertTrue(error.getMessage().matches("(?s).*FOUND 1 DIFFERENCE.*" + - "Actual JSON OBJECT has extra fields.*")); + "\\Q$\\E -> Actual JSON OBJECT has extra fields.*")); } @Test - public void compareJsonsWithUseCases() { + void compareJsonsWithUseCases() { String expected = "{\"!name\":\"test\",\"records\":[1,2,3, \"!.*\"], \"otherRecords\":[4, \"!.*\"]}"; String actual = "{\"names\":\"test\",\"records\":[1,2,3], \"otherRecords\":[4]}"; JSONCompare.assertMatches(expected, actual, new CustomComparator()); @@ -82,33 +82,33 @@ public void compareJsonsWithUseCases() { String actual1 = "{\"names\":\"test1\", \"records\":[2,1,4,3], \"otherRecords\":[1,2], \"another\":\"record\"}"; AssertionError error = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected1, actual1, new CustomComparator())); assertTrue(error.getMessage().matches("(?s).*FOUND 8 DIFFERENCE.*" + - "\\Q.*\\E ->.*Expected value: \"test\" But got: \"test1\".*" + - "\\Q.*\\E ->.*Different JSON types: expected TextNode but got ArrayNode.*" + - "\\Q.*\\E ->.*Different JSON types: expected TextNode but got ArrayNode.*" + - "\\Q.*\\E ->.*Expected value: \"test\" But got: \"record\".*" + - "records ->.*Expected condition \"\\Q!.*\\E\" from position 4 was not met. Actual JSON ARRAY has extra elements.*" + - "otherRecords ->.*Expected element from position 1 was NOT FOUND.*4.*" + - "otherRecords ->.*Expected condition \"\\Q!.*\\E\" from position 2 was not met. Actual JSON ARRAY has extra elements.*" + - "Expected condition '\\Q!.*\\E' was not met. Actual JSON OBJECT has extra fields.*")); + "\\Q$..*\\E.*Expected value: \"test\" But got: \"test1\".*" + + "\\Q$..*\\E.*Different JSON types: expected TextNode but got ArrayNode.*" + + "\\Q$..*\\E.*Different JSON types: expected TextNode but got ArrayNode.*" + + "\\Q$..*\\E.*Expected value: \"test\" But got: \"record\".*" + + "\\Q$.records[3]\\E ->.*Expected condition \"\\Q!.*\\E\" was not met. Actual JSON ARRAY has extra elements.*" + + "\\Q$.otherRecords[0]\\E was not found.*4.*" + + "\\Q$.otherRecords[1]\\E ->.*Expected condition \"\\Q!.*\\E\" was not met. Actual JSON ARRAY has extra elements.*" + + "\\Q$.\"!.*\"\\E condition was not met. Actual JSON OBJECT has extra fields.*")); String expected2 = "{\"name\":\"test\", \"records\":[1, \".*\", 3, 4, \".*\"], \"otherRecords\":[4, \"!.*\"], \".*\":\".*\"}"; String actual2 = "{\"names\":\"test1\", \"records\":[2,1,4,3], \"otherRecords\":[1,2, 4], \"another\":\"record\"}"; error = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected2, actual2, new CustomComparator())); assertTrue(error.getMessage().matches("(?s).*FOUND 3 DIFFERENCE.*" + - "Field 'name' was NOT FOUND.*" + - "records ->.*Expected condition \"\\Q.*\\E\" from position 5 was not met. Actual JSON ARRAY has no extra elements.*" + - "otherRecords ->.*Expected condition \"\\Q!.*\\E\" from position 2 was not met. Actual JSON ARRAY has extra elements.*")); + "\\Q$.name\\E was not found.*" + + "\\Q$.records[4]\\E ->.*Expected condition \"\\Q.*\\E\" was not met. Actual JSON ARRAY has no extra elements.*" + + "\\Q$.otherRecords[1]\\E ->.*Expected condition \"\\Q!.*\\E\" was not met. Actual JSON ARRAY has extra elements.*")); String expected3 = "{\"name\":\"test\", \"records\":[1, \".*\", 3, 4, \".*\"], \"otherRecords\":[4, 2, \"!.*\"], \".*\":\".*\"}"; String actual3 = "{\"name\":\"test\", \"records\":[2,1,5,4,3], \"otherRecords\":[2, 4]}"; error = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected3, actual3, new CustomComparator())); assertTrue(error.getMessage().matches("(?s).*FOUND 1 DIFFERENCE.*" + - "Field '\\Q.*\\E' was NOT FOUND.*")); - + "\\Q$..*\\E was not found.*")); } + @Test - public void compareJsonsWithRegexes() { + void compareJsonsWithRegexes() { String expected = "{\"na.*\":\"test\",\"records\":[1,\"\\\\d+\",3], \"otherRecords\":[4]}"; String actual = "{\"na.*\":\"test\",\"records\":[\"\\\\d+\",3,1], \"otherRecords\":[4]}"; JSONCompare.assertMatches(expected, actual, new CustomComparator()); @@ -117,8 +117,8 @@ public void compareJsonsWithRegexes() { String actual1 = "{\"name\":\"test\",\"records\":[1,2,3], \"otherRecords\":[4]}"; AssertionError error = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected1, actual1, new CustomComparator())); assertTrue(error.getMessage().matches("(?s).*FOUND 2 DIFFERENCE.*" + - "Field '\\Qna.*\\E' was NOT FOUND.*" + - "records ->.*Expected element from position 2 was NOT FOUND.*\"\\Q\\\\d+\\E\".*")); + "\\Q$.na.*\\E was not found.*" + + "\\Q$.records[1]\\E was not found.*\"\\Q\\\\d+\\E\".*")); } public static class CustomComparator implements JsonComparator { diff --git a/src/test/java/io/json/compare/matcher/diffs/JsonDiffsAsListTests.java b/src/test/java/io/json/compare/matcher/diffs/JsonDiffsAsListTests.java index 80fb458..54c9690 100644 --- a/src/test/java/io/json/compare/matcher/diffs/JsonDiffsAsListTests.java +++ b/src/test/java/io/json/compare/matcher/diffs/JsonDiffsAsListTests.java @@ -19,7 +19,7 @@ void compareJsonArraysAndCheckForFor1ElementNotFoundDifference() { String actual = "[\"a\",\"b\",1,2,true,false,12.091,null]"; List diffs = JSONCompare.diffs(expected, actual); assertEquals(1, diffs.size()); - assertTrue(diffs.get(0).matches("(?s).*Expected element from position 2 was NOT FOUND.*\"c\".*")); + assertTrue(diffs.get(0).matches("(?s).*\\Q$[1]\\E was not found.*\"c\".*")); } @Test @@ -28,11 +28,11 @@ void compareJsonArraysAndCheckForMultipleElementNotFoundDifferences() { String actual = "[12.091,10,\"b\",1,\"a\",2,true,{\"lorem\":\"ipsum-updated\"},\"some text\",false]"; List diffs = JSONCompare.diffs(expected, actual); assertEquals(5, diffs.size()); - assertTrue(diffs.get(0).matches("(?s).*Expected element from position 2 was NOT FOUND.*\"c\".*")); - assertTrue(diffs.get(1).matches("(?s).*Expected element from position 4 was NOT FOUND.*200.*")); - assertTrue(diffs.get(2).matches("(?s).*Expected element from position 7 was NOT FOUND.*12.092.*")); - assertTrue(diffs.get(3).matches("(?s).*Expected element from position 8 was NOT FOUND.*null.*")); - assertTrue(diffs.get(4).matches("(?s).*Expected element from position 9 was NOT FOUND.*\\{.*\"lorem\".*\"ipsum\".*}.*")); + assertTrue(diffs.get(0).matches("(?s).*\\Q$[1]\\E was not found.*\"c\".*")); + assertTrue(diffs.get(1).matches("(?s).*\\Q$[3]\\E was not found.*200.*")); + assertTrue(diffs.get(2).matches("(?s).*\\Q$[6]\\E was not found.*12.092.*")); + assertTrue(diffs.get(3).matches("(?s).*\\Q$[7]\\E was not found.*null.*")); + assertTrue(diffs.get(4).matches("(?s).*\\Q$[8]\\E was not found.*\\{.*\"lorem\".*\"ipsum\".*}.*")); } @Test @@ -41,7 +41,7 @@ void compareJsonArraysAndCheckForOneMatchAnyDifferences() { String actual = "[\"a\",true,1,{\"lorem\":\"ipsum\"}]"; List diffs = JSONCompare.diffs(expected, actual); assertEquals(1, diffs.size()); - assertTrue(diffs.get(0).matches("(?s).*Expected condition \"\\Q.*\\E\" from position 5 was not met. Actual JSON ARRAY has no extra elements.*")); + assertTrue(diffs.get(0).matches("(?s).*\\Q$[4]\\E -> Expected condition \"\\Q.*\\E\" was not met. Actual JSON ARRAY has no extra elements.*")); } @Test @@ -50,7 +50,7 @@ void compareJsonArraysAndCheckForOneDoNotMatchAnyDifferences() { String actual = "[\"a\",true,1,{\"lorem\":\"ipsum\"}]"; List diffs = JSONCompare.diffs(expected, actual); assertEquals(1, diffs.size()); - assertTrue(diffs.get(0).matches("(?s).*Expected condition \"\\Q!.*\\E\" from position 3 was not met. Actual JSON ARRAY has extra elements.*")); + assertTrue(diffs.get(0).matches("(?s).*\\Q$[2]\\E -> Expected condition \"\\Q!.*\\E\" was not met. Actual JSON ARRAY has extra elements.*")); } @Test @@ -60,13 +60,11 @@ void compareJsonArraysAndCheckForJsonNonExtensibleAndAndJsonStrictOrderDifferenc List diffs = JSONCompare.diffs(expected, actual, new HashSet<>(Arrays.asList(CompareMode.JSON_ARRAY_NON_EXTENSIBLE, CompareMode.JSON_ARRAY_STRICT_ORDER))); assertEquals(5, diffs.size()); - assertTrue(diffs.get(0).matches("(?s).*JSON ARRAY elements differ at position 1.*\"lorem\".*\"ipsum\".*diffs.*" + - "Different JSON types: expected ObjectNode but got TextNode.*")); - assertTrue(diffs.get(1).matches("(?s).*JSON ARRAY elements differ at position 2.*\"c\".*diffs.*Expected value: \"c\" But got: true.*")); - assertTrue(diffs.get(2).matches("(?s).*Expected element from position 3 was FOUND.*\"!1\".*")); - assertTrue(diffs.get(3).matches("(?s).*JSON ARRAY elements differ at position 4.*true.*diffs.*" + - "Different JSON types: expected BooleanNode but got ObjectNode.*")); - assertTrue(diffs.get(4).matches("(?s).*Actual JSON ARRAY has extra elements.*")); + assertTrue(diffs.get(0).matches("(?s).*\\Q$[0]\\E -> Different JSON types: expected ObjectNode but got TextNode.*")); + assertTrue(diffs.get(1).matches("(?s).*\\Q$[1]\\E.*Expected value: \"c\" But got: true.*")); + assertTrue(diffs.get(2).matches("(?s).*\\Q$[2]\\E was found.*\"!1\".*")); + assertTrue(diffs.get(3).matches("(?s).*\\Q$[3]\\E -> Different JSON types: expected BooleanNode but got ObjectNode.*")); + assertTrue(diffs.get(4).matches("(?s).*\\Q$\\E -> Actual JSON ARRAY has extra elements.*")); } @Test @@ -75,8 +73,8 @@ void compareJsonsWithCUstomComparator() { String actual = "{\"name\":\"test\",\"records\":[1,2,3], \"otherRecords\":[4]}"; List diffs = JSONCompare.diffs(expected, actual, new JsonCustomComparatorDiffTests.CustomComparator()); assertEquals(2, diffs.size()); - assertTrue(diffs.get(0).matches("(?s).*name ->.*Expected value: \"test1\" But got: \"test\".*")); - assertTrue(diffs.get(1).matches("(?s).*records ->.*Expected element from position 2 was NOT FOUND.*4.*")); + assertTrue(diffs.get(0).matches("(?s).*\\Q$.name\\E.*Expected value: \"test1\" But got: \"test\".*")); + assertTrue(diffs.get(1).matches("(?s).*\\Q$.records[1]\\E.* was not found.*4.*")); } @Test @@ -85,9 +83,9 @@ void compareJsonObjectsAndCheckForMultipleInDepthFieldNotFoundDifferences() { String actual = "{\"b\":{\"b3\":\"val1\",\"b2\":{\"b22\":10.432}},\"a\":100,\"c\":true}"; List diffs = JSONCompare.diffs(expected, actual); assertEquals(3, diffs.size()); - assertTrue(diffs.get(0).matches("(?s).*Field 'x' was NOT FOUND.*")); - assertTrue(diffs.get(1).matches("(?s).*b -> Field 'b1' was NOT FOUND.*")); - assertTrue(diffs.get(2).matches("(?s).*b -> b2 -> Field 'b21' was NOT FOUND.*")); + assertTrue(diffs.get(0).matches("(?s).*\\Q$.x\\E was not found.*")); + assertTrue(diffs.get(1).matches("(?s).*\\Q$.b.b1\\E was not found.*")); + assertTrue(diffs.get(2).matches("(?s).*\\Q$.b.b2.b21\\E was not found.*")); } @Test @@ -98,14 +96,13 @@ void checkMultipleJsonPathDifferences() { "\"u\":{\"u1\":{\"u11\":20000}}}"; List diffs = JSONCompare.diffs(expected, actual); assertEquals(5, diffs.size()); - assertTrue(diffs.get(0).matches("(?s).*\\QJson path '$.a.length()' -> Expected json path result\\E.*" + + assertTrue(diffs.get(0).matches("(?s).*\\Q$.#($.a.length())\\E.*Expected json path result.*" + "3.*But got.*2.*")); - assertTrue(diffs.get(1).matches("(?s).*Expected value: \"val1\" But got: \"val2\".*")); - assertTrue(diffs.get(2).matches("(?s).*x -> x1 -> y11 -> \\QJson path '$.www'\\E -> No results for path.*")); - assertTrue(diffs.get(3).matches("(?s).*z -> \\QJson path '$.length()' -> Expected json path result\\E.*" + - "1.*But got.*3.*")); - assertTrue(diffs.get(4).matches("(?s).*u -> \\QJson path '$.u1' -> Expected json path result\\E.*" + - "u11 ->.*Expected value: 20209 But got: 20000.*")); + assertTrue(diffs.get(1).matches("(?s).*\\Q$.b\\E.*Expected value: \"val1\" But got: \"val2\".*")); + assertTrue(diffs.get(2).matches("(?s).*\\Q$.x.x1.y11.#($.www)\\E -> Json path -> No results for path.*")); + assertTrue(diffs.get(3).matches("(?s).*\\Q$.z.#($.length())\\E.*Expected json path result.*1.*But got.*3.*")); + assertTrue(diffs.get(4).matches("(?s).*\\Q$.u.#($.u1).u11\\E.*Expected value: 20209 But got: 20000" + + ".*Expected json path result.*u11.*20209.*20000.*")); } @@ -116,9 +113,9 @@ void compareJsonsWithCustomComparatorAndCompareModes() { List diffs = JSONCompare.diffs(expected, actual, new JsonCustomComparatorDiffTests.CustomComparator(), new HashSet<>(Arrays.asList(CompareMode.JSON_OBJECT_NON_EXTENSIBLE, CompareMode.JSON_ARRAY_NON_EXTENSIBLE))); assertEquals(3, diffs.size()); - assertTrue(diffs.get(0).matches("(?s).*name ->.*Expected value: \".*test\" But got: \"test\".*")); - assertTrue(diffs.get(1).matches("(?s).*records ->.*Actual JSON ARRAY has extra elements.*")); - assertTrue(diffs.get(2).matches("(?s).*Actual JSON OBJECT has extra fields.*")); + assertTrue(diffs.get(0).matches("(?s).*\\Q$.name\\E.*Expected value: \".*test\" But got: \"test\".*")); + assertTrue(diffs.get(1).matches("(?s).*\\Q$.records\\E ->.*Actual JSON ARRAY has extra elements.*")); + assertTrue(diffs.get(2).matches("(?s).*\\Q$\\E -> Actual JSON OBJECT has extra fields.*")); } @Test diff --git a/src/test/java/io/json/compare/matcher/diffs/JsonObjectDiffTests.java b/src/test/java/io/json/compare/matcher/diffs/JsonObjectDiffTests.java index bbda915..1b47209 100644 --- a/src/test/java/io/json/compare/matcher/diffs/JsonObjectDiffTests.java +++ b/src/test/java/io/json/compare/matcher/diffs/JsonObjectDiffTests.java @@ -10,83 +10,83 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -public class JsonObjectDiffTests { +class JsonObjectDiffTests { @Test - public void compareJsonObjectsAndCheckForSingleFieldNotFoundDifference() { + void compareJsonObjectsAndCheckForSingleFieldNotFoundDifference() { String expected = "{\"a\":100,\"e\":\"lorem100\"}"; String actual = "{\"a\":100,\"c\":true}"; AssertionError error = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected, actual)); assertTrue(error.getMessage().matches("(?s).*FOUND 1 DIFFERENCE.*" + - "Field 'e' was NOT FOUND.*")); + "\\Q$.e\\E was not found.*")); JSONCompare.assertNotMatches(expected, actual); } @Test - public void compareJsonObjectsAndCheckForMultipleFieldNotFoundDifferences() { + void compareJsonObjectsAndCheckForMultipleFieldNotFoundDifferences() { String expected = "{\"a\":100,\"e\":\"lorem100\",\"x\":true}"; String actual = "{\"a\":100,\"c\":true}"; AssertionError error = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected, actual)); assertTrue(error.getMessage().matches("(?s).*FOUND 2 DIFFERENCE.*" + - "Field 'e' was NOT FOUND.*" + - "Field 'x' was NOT FOUND.*")); + "\\Q$.e\\E was not found.*" + + "\\Q$.x\\E was not found.*")); JSONCompare.assertNotMatches(expected, actual); } @Test - public void compareJsonObjectsAndCheckForMultipleInDepthFieldNotFoundDifferences() { + void compareJsonObjectsAndCheckForMultipleInDepthFieldNotFoundDifferences() { String expected = "{\"a\":100,\"x\":51,\"b\":{\"b1\":\"val1\",\"b2\":{\"b21\":\"test\"}}}"; String actual = "{\"b\":{\"b3\":\"val1\",\"b2\":{\"b22\":10.432}},\"a\":100,\"c\":true}"; AssertionError error = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected, actual)); assertTrue(error.getMessage().matches("(?s).*FOUND 3 DIFFERENCE.*" + - "Field 'x' was NOT FOUND.*" + - "b -> Field 'b1' was NOT FOUND.*" + - "b -> b2 -> Field 'b21' was NOT FOUND.*")); + "\\Q$.x\\E was not found.*" + + "\\Q$.b.b1\\E was not found.*" + + "\\Q$.b.b2.b21\\E was not found.*")); JSONCompare.assertNotMatches(expected, actual); } @Test - public void compareJsonObjectsAndCheckForSingleDoNotMatchFieldDifference() { + void compareJsonObjectsAndCheckForSingleDoNotMatchFieldDifference() { String expected = "{\"c\":true,\"!a\":100}"; String actual = "{\"a\":200,\"c\":true}"; AssertionError error = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected, actual)); assertTrue(error.getMessage().matches("(?s).*FOUND 1 DIFFERENCE.*" + - "Field '!a' was FOUND.*")); + "\\Q$.\"!a\"\\E was found.*")); JSONCompare.assertNotMatches(expected, actual); } @Test - public void compareJsonObjectsAndCheckForMultipleDoNotMatchFieldDifferences() { + void compareJsonObjectsAndCheckForMultipleDoNotMatchFieldDifferences() { String expected = "{\"c\":true,\"!a\":100,\"!c\":200,\"!x\":200}"; String actual = "{\"a\":200,\"c\":true,\"x\":\"lorem\"}"; AssertionError error = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected, actual)); assertTrue(error.getMessage().matches("(?s).*FOUND 2 DIFFERENCE.*" + - "Field '!a' was FOUND.*" + - "Field '!x' was FOUND.*")); + "\\Q$.\"!a\"\\E was found.*" + + "\\Q$.\"!x\"\\E was found.*")); JSONCompare.assertNotMatches(expected, actual); } @Test - public void compareJsonObjectsAndCheckForInDepthDoNotMatchFieldDifferences() { + void compareJsonObjectsAndCheckForInDepthDoNotMatchFieldDifferences() { String expected = "{\"!c\":true,\"a\":200,\"x\":{\"!x1\":\"lorem\",\"x2\":{\"!x21\":\"test\"}}}"; String actual = "{\"c\":true,\"a\":200,\"x\":{\"x1\":\"lorem\",\"x2\":{\"x23\":\"pat\",\"x21\":\"lol\"}}}"; AssertionError error = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected, actual)); assertTrue(error.getMessage().matches("(?s).*FOUND 3 DIFFERENCE.*" + - "Field '!c' was FOUND.*" + - "x -> Field '!x1' was FOUND.*" + - "x -> x2 -> Field '!x21' was FOUND.*")); + "\\Q$.\"!c\"\\E was found.*" + + "\\Q$.x.\"!x1\"\\E was found.*" + + "\\Q$.x.x2.\"!x21\"\\E was found.*")); JSONCompare.assertNotMatches(expected, actual); } @Test - public void compareJsonObjectsAndCheckForMultipleCandidatesDifferences() { + void compareJsonObjectsAndCheckForMultipleCandidatesDifferences() { String expected = "{\".*\":{\"a\":1,\"x\":2}}"; String actual = "{\"test1\":{\"a\":10,\"b\":22},\"test2\":{\"a\":20,\"x\":2}}"; AssertionError error = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected, actual)); assertTrue(error.getMessage().matches("(?s).*FOUND 3 DIFFERENCE.*" + - "\\Q.*\\E -> a -> .*Expected value: 1 But got: 10.*" + - "\\Q.*\\E -> .*Field 'x' was NOT FOUND.*" + - "\\Q.*\\E -> a -> .*Expected value: 1 But got: 20.*")); + "\\Q$..*.a\\E.*Expected value: 1 But got: 10.*" + + "\\Q$..*.x\\E was not found.*" + + "\\Q$..*.a\\E.*Expected value: 1 But got: 20.*")); JSONCompare.assertNotMatches(expected, actual); String expected1 = "{\".*\":{\"a\":1,\"x\":2}}"; @@ -95,13 +95,13 @@ public void compareJsonObjectsAndCheckForMultipleCandidatesDifferences() { } @Test - public void compareJsonObjectsAndCheckForDoNotMatchDifferences() { + void compareJsonObjectsAndCheckForDoNotMatchDifferences() { String expected = "{\"b\":2,\"a\":1,\"c\":{\"c2\":\"lorem2\",\"!.*\":\".*\"},\"!.*\":\".*\"}"; String actual = "{\"a\":1,\"b\":2,\"c\":{\"c1\":\"lorem1\",\"c2\":\"lorem2\"},\"d\":1}"; AssertionError error = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected, actual)); assertTrue(error.getMessage().matches("(?s).*FOUND 2 DIFFERENCE.*" + - "c -> Expected condition '\\Q!.*\\E' was not met. Actual JSON OBJECT has extra fields.*" + - "Expected condition '\\Q!.*\\E' was not met. Actual JSON OBJECT has extra fields.*")); + "\\Q$.c.\"!.*\"\\E condition was not met. Actual JSON OBJECT has extra fields.*" + + "\\Q$.\"!.*\"\\E condition was not met. Actual JSON OBJECT has extra fields.*")); JSONCompare.assertNotMatches(expected, actual); String expected1 = "{\"b\":2,\"a\":1,\"c\":{\"c2\":\"lorem2\",\"!.*\":\".*\"},\"!.*\":\".*\"}"; @@ -110,49 +110,49 @@ public void compareJsonObjectsAndCheckForDoNotMatchDifferences() { } @Test - public void compareJsonObjectsAndCheckForMatchAnyDifferences() { + void compareJsonObjectsAndCheckForMatchAnyDifferences() { String expected = "{\"b\":2,\"a\":1,\"c\":{\"c1\":\"lorem2\",\".*\":\".*\"},\"!.*\":\".*\"}"; String actual = "{\"a\":1,\"b\":2,\"c\":{\"c1\":\"lorem2\"}}"; AssertionError error = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected, actual)); assertTrue(error.getMessage().matches("(?s).*FOUND 1 DIFFERENCE.*" + - "c -> Field \\Q'.*'\\E was NOT FOUND.*")); + "\\Q$.c..*\\E was not found.*")); JSONCompare.assertNotMatches(expected, actual); } @Test - public void compareJsonObjectsAndCheckForMatchAnyDifferences1() { + void compareJsonObjectsAndCheckForMatchAnyDifferences1() { String expected = "{\"a\":1,\"c\":{\"c1\":\"lorem2\",\".*\":\".*\"},\"!.*\":\".*\"}"; String actual = "{\"a\":1,\"b\":2,\"c\":{\"c1\":\"lorem2\"}}"; AssertionError error = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected, actual)); assertTrue(error.getMessage().matches("(?s).*FOUND 2 DIFFERENCE.*" + - "c -> Field \\Q'.*'\\E was NOT FOUND.*" + - "Expected condition \\Q'!.*'\\E was not met. Actual JSON OBJECT has extra fields.*")); + "\\Q$.c..*\\E was not found.*" + + "\\Q$.\"!.*\"\\E condition was not met. Actual JSON OBJECT has extra fields.*")); JSONCompare.assertNotMatches(expected, actual); } @Test - public void compareJsonObjectsAndCheckForNonExtensibleObjectDifferences() { + void compareJsonObjectsAndCheckForNonExtensibleObjectDifferences() { String expected = "{\"b\":2,\"a\":0,\"c\":{\"c1\":\"lorem1\"}}"; String actual = "{\"a\":1,\"b\":2,\"c\":{\"c2\":\"lorem2\",\"c1\":\"lorem1\"},\"x\":0}"; AssertionError error = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected, actual, new HashSet<>(Collections.singletonList(CompareMode.JSON_OBJECT_NON_EXTENSIBLE)))); assertTrue(error.getMessage().matches("(?s).*FOUND 3 DIFFERENCE.*" + - "a ->.*Expected value: 0 But got: 1.*" + - "c -> Actual JSON OBJECT has extra fields.*" + + "\\Q$.a\\E.*Expected value: 0 But got: 1.*" + + "\\Q$.c\\E -> Actual JSON OBJECT has extra fields.*" + "Actual JSON OBJECT has extra fields.*")); JSONCompare.assertNotMatches(expected, actual); } @Test - public void compareJsonObjectsAndCheckForDoNotMatchAnyDifferencesFromArrayObject() { + void compareJsonObjectsAndCheckForDoNotMatchAnyDifferencesFromArrayObject() { String expected = "{\"b\":2,\"a\":0,\"c\":{\"c1\":\"lorem1\"},\"u\":[3,1,\"!.*\"]}"; String actual = "{\"a\":1, \"u\":[3,1,false], \"b\":2,\"c\":{\"c2\":\"lorem2\",\"c1\":\"lorem1\"},\"x\":0}"; AssertionError error = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected, actual, new HashSet<>(Collections.singletonList(CompareMode.JSON_OBJECT_NON_EXTENSIBLE)))); assertTrue(error.getMessage().matches("(?s).*FOUND 4 DIFFERENCE.*" + - "a ->.*Expected value: 0 But got: 1.*" + - "c -> Actual JSON OBJECT has extra fields.*" + - "u ->.*Expected condition \\Q\"!.*\"\\E from position 3 was not met. Actual JSON ARRAY has extra elements.*" + + "\\Q$.a\\E.*Expected value: 0 But got: 1.*" + + "\\Q$.c\\E -> Actual JSON OBJECT has extra fields.*" + + "\\Q$.u[2]\\E -> Expected condition \"!.*\" was not met. Actual JSON ARRAY has extra elements.*" + "Actual JSON OBJECT has extra fields.*")); JSONCompare.assertNotMatches(expected, actual); @@ -161,15 +161,15 @@ public void compareJsonObjectsAndCheckForDoNotMatchAnyDifferencesFromArrayObject error = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected1, actual1, new HashSet<>(Collections.singletonList(CompareMode.JSON_OBJECT_NON_EXTENSIBLE)))); assertTrue(error.getMessage().matches("(?s).*FOUND 4 DIFFERENCE.*" + - "a ->.*Expected value: 0 But got: 1.*" + - "c -> c2 ->.*Expected value: \"incorrect\" But got: \"lorem2\".*" + - "u ->.*Expected condition \\Q\"!.*\"\\E from position 3 was not met. Actual JSON ARRAY has extra elements.*" + + "\\Q$.a\\E.*Expected value: 0 But got: 1.*" + + "\\Q$.c.c2\\E.*Expected value: \"incorrect\" But got: \"lorem2\".*" + + "\\Q$.u[2]\\E.*Expected condition \\Q\"!.*\"\\E was not met. Actual JSON ARRAY has extra elements.*" + "Actual JSON OBJECT has extra fields.*")); JSONCompare.assertNotMatches(expected, actual); } @Test - public void compareJsonObjectsAndCheckForMismatchDifferences() { + void compareJsonObjectsAndCheckForMismatchDifferences() { String expected = "{\n" + " \"branch\": -212276217.0580678,\n" + " \"wagon\": {\n" + @@ -245,31 +245,31 @@ public void compareJsonObjectsAndCheckForMismatchDifferences() { "}"; AssertionError error = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected, actual, null, "My custom message")); assertTrue(error.getMessage().matches("(?s).*FOUND 7 DIFFERENCE.*" + - "wagon -> being ->.*Expected value: true But got: false.*" + - "wagon -> position -> Field 'interior1' was NOT FOUND.*" + - "wagon -> position -> writer ->.*Expected value: -806595724.6570452 But got: -806595724.6570451.*" + - "wagon -> position -> difficulty ->.*Expected value: \"not equal\" But got: \"equal\".*" + - "wagon -> ill ->.*Expected value: -2976723441 But got: -297672344.*" + - "wagon -> particular ->.*Expected element from position 1 was NOT FOUND.*" + + "\\Q$.wagon.being\\E.*Expected value: true But got: false.*" + + "\\Q$.wagon.position.interior1\\E was not found.*" + + "\\Q$.wagon.position.writer\\E.*Expected value: -806595724.6570452 But got: -806595724.6570451.*" + + "\\Q$.wagon.position.difficulty\\E.*Expected value: \"not equal\" But got: \"equal\".*" + + "\\Q$.wagon.ill\\E.*Expected value: -2976723441 But got: -297672344.*" + + "\\Q$.wagon.particular[0]\\E was not found.*" + "true.*894216220.*" + - "master ->.*Expected value: -1005804822.0610056 But got: 1005804822.0610056.*" + + "\\Q$.master\\E.*Expected value: -1005804822.0610056 But got: 1005804822.0610056.*" + "My custom message.*")); JSONCompare.assertNotMatches(expected, actual); } @Test - public void compareJsonObjectsWithJsonArraysAndDoNotMatchAny() { + void compareJsonObjectsWithJsonArraysAndDoNotMatchAny() { String expected = "{\"name\":\"test\",\".*\":[4],\"!.*\":\".*\"}"; String actual = "{\"name\":\"test\",\"records\":[1,2,3], \"otherRecords\":[4]}"; AssertionError error = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected, actual)); assertTrue(error.getMessage().matches("(?s).*FOUND 1 DIFFERENCE.*" + - "Expected condition '\\Q!.*\\E' was not met. Actual JSON OBJECT has extra fields.*")); + "\\Q$.\"!.*\"\\E condition was not met. Actual JSON OBJECT has extra fields.*")); String expected0 = "{\"records\":[3], \"!name\":\"test\",\".*\":[4],\"!.*\":\".*\"}"; String actual0 = "{\"names\":\"test\",\"records\":[1,2,3], \"otherRecords\":[4]}"; error = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected0, actual0)); assertTrue(error.getMessage().matches("(?s).*FOUND 1 DIFFERENCE.*" + - "Expected condition '\\Q!.*\\E' was not met. Actual JSON OBJECT has extra fields.*")); + "\\Q$.\"!.*\"\\E condition was not met. Actual JSON OBJECT has extra fields.*")); String expected1 = "{\"name\":\"test\",\".*\":[4],\"records\":[3],\"!.*\":\".*\"}"; String actual1 = "{\"name\":\"test\",\"records\":[1,2,3], \"otherRecords\":[4]}"; @@ -277,7 +277,7 @@ public void compareJsonObjectsWithJsonArraysAndDoNotMatchAny() { } @Test - public void compareJsonsWithUseCases() { + void compareJsonsWithUseCases() { String expected = "{\"!name\":\"test\",\"records\":[1,2,3, \"!.*\"], \"otherRecords\":[4, \"!.*\"]}"; String actual = "{\"names\":\"test\",\"records\":[1,2,3], \"otherRecords\":[4]}"; JSONCompare.assertMatches(expected, actual); @@ -294,37 +294,37 @@ public void compareJsonsWithUseCases() { String actual1 = "{\"names\":\"test1\", \"records\":[2,1,4,3], \"otherRecords\":[1,2], \"another\":\"record\"}"; AssertionError error = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected1, actual1)); assertTrue(error.getMessage().matches("(?s).*FOUND 8 DIFFERENCE.*" + - "\\Q.*\\E ->.*Expected value: \"test\" But got: \"test1\".*" + - "\\Q.*\\E ->.*Different JSON types: expected TextNode but got ArrayNode.*" + - "\\Q.*\\E ->.*Different JSON types: expected TextNode but got ArrayNode.*" + - "\\Q.*\\E ->.*Expected value: \"test\" But got: \"record\".*" + - "records ->.*Expected condition \"\\Q!.*\\E\" from position 4 was not met. Actual JSON ARRAY has extra elements.*" + - "otherRecords ->.*Expected element from position 1 was NOT FOUND.*4.*" + - "otherRecords ->.*Expected condition \"\\Q!.*\\E\" from position 2 was not met. Actual JSON ARRAY has extra elements.*" + - "Expected condition '\\Q!.*\\E' was not met. Actual JSON OBJECT has extra fields.*")); + "\\Q$..*\\E.*Expected value: \"test\" But got: \"test1\".*" + + "\\Q$..*\\E.*Different JSON types: expected TextNode but got ArrayNode.*" + + "\\Q$..*\\E.*Different JSON types: expected TextNode but got ArrayNode.*" + + "\\Q$..*\\E.*Expected value: \"test\" But got: \"record\".*" + + "\\Q$.records[3]\\E ->.*Expected condition \"\\Q!.*\\E\" was not met. Actual JSON ARRAY has extra elements.*" + + "\\Q$.otherRecords[0]\\E was not found.*4.*" + + "\\Q$.otherRecords[1]\\E ->.*Expected condition \"\\Q!.*\\E\" was not met. Actual JSON ARRAY has extra elements.*" + + "\\Q$.\"!.*\"\\E condition was not met. Actual JSON OBJECT has extra fields.*")); String expected2 = "{\"name\":\"test\", \"records\":[1, \".*\", 3, 4, \".*\"], \"otherRecords\":[4, \"!.*\"], \".*\":\".*\"}"; String actual2 = "{\"names\":\"test1\", \"records\":[2,1,4,3], \"otherRecords\":[1,2, 4], \"another\":\"record\"}"; error = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected2, actual2)); assertTrue(error.getMessage().matches("(?s).*FOUND 3 DIFFERENCE.*" + - "Field 'name' was NOT FOUND.*" + - "records ->.*Expected condition \"\\Q.*\\E\" from position 5 was not met. Actual JSON ARRAY has no extra elements.*" + - "otherRecords ->.*Expected condition \"\\Q!.*\\E\" from position 2 was not met. Actual JSON ARRAY has extra elements.*")); + "\\Q$.name\\E was not found.*" + + "\\Q$.records[4]\\E ->.*Expected condition \"\\Q.*\\E\" was not met. Actual JSON ARRAY has no extra elements.*" + + "\\Q$.otherRecords[1]\\E ->.*Expected condition \"\\Q!.*\\E\" was not met. Actual JSON ARRAY has extra elements.*")); String expected3 = "{\"name\":\"test\", \"records\":[1, \".*\", 3, 4, \".*\"], \"otherRecords\":[4, 2, \"!.*\"], \".*\":\".*\"}"; String actual3 = "{\"name\":\"test\", \"records\":[2,1,5,4,3], \"otherRecords\":[2, 4]}"; error = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected3, actual3)); assertTrue(error.getMessage().matches("(?s).*FOUND 1 DIFFERENCE.*" + - "Field '\\Q.*\\E' was NOT FOUND.*")); + "\\Q$..*\\E was not found.*")); } @Test - public void compareJsonsWithNestedUseCases() { + void compareJsonsWithNestedUseCases() { String expected = "{\"name\":\"test\", \"x\":{\"b\":2, \"!.*\":\".*\"}, \"!.*\":\".*\"}"; String actual = "{\"names\":\"test\", \"x\":{\"a\":1, \"b\":2}}"; AssertionError error = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected, actual)); assertTrue(error.getMessage().matches("(?s).*FOUND 2 DIFFERENCE.*" + - "Field 'name' was NOT FOUND.*" + - "x ->.*Expected condition '\\Q!.*\\E' was not met. Actual JSON OBJECT has extra fields.*")); + "\\Q$.name\\E was not found.*" + + "\\Q$.x.\"!.*\"\\E condition was not met. Actual JSON OBJECT has extra fields.*")); } } diff --git a/src/test/java/io/json/compare/matcher/diffs/JsonPathDiffTests.java b/src/test/java/io/json/compare/matcher/diffs/JsonPathDiffTests.java index 439b892..44f538e 100644 --- a/src/test/java/io/json/compare/matcher/diffs/JsonPathDiffTests.java +++ b/src/test/java/io/json/compare/matcher/diffs/JsonPathDiffTests.java @@ -6,24 +6,23 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -public class JsonPathDiffTests { +class JsonPathDiffTests { @Test - public void checkMultipleJsonPathDifferences() { + void checkMultipleJsonPathDifferences() { String expected = "{\"#($.a.length())\":3,\"b\":\"val1\",\"x\":{\"x1\":{\"y11\":{\"#($.www)\":\"lorem1\"}}},\"z\":[{\"#($.length())\":1}]," + "\"u\":{\"#($.u1)\":{\"u11\":20209}}}"; String actual = "{\"z\":[2,3,4],\"x\":{\"x2\":290.11,\"x1\":{\"x11\":null,\"y11\":{\"a\":\"lorem2\"}}},\"b\":\"val2\",\"a\":[4,5]," + "\"u\":{\"u1\":{\"u11\":20000}}}"; AssertionError error = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected, actual)); assertTrue(error.getMessage().matches("(?s).*FOUND 5 DIFFERENCE.*" + - "\\QJson path '$.a.length()' -> Expected json path result\\E.*" + + "\\Q$.#($.a.length())\\E.*Expected json path result.*" + "3.*But got.*2.*" + - "b ->.*Expected value: \"val1\" But got: \"val2\".*" + - "x -> x1 -> y11 -> \\QJson path '$.www'\\E -> No results for path.*" + - "z -> \\QJson path '$.length()' -> Expected json path result\\E.*" + + "\\Q$.b\\E.*Expected value: \"val1\" But got: \"val2\".*" + + "\\Q$.x.x1.y11.#($.www)\\E.*No results for path.*" + + "\\Q$.z.#($.length())\\E.*Expected json path result.*" + "1.*But got.*3.*" + - "u -> \\QJson path '$.u1' -> Expected json path result\\E.*" + - "u11 ->.*Expected value: 20209 But got: 20000.*")); + "\\Qu.#($.u1).u11\\E.*Expected value: 20209 But got: 20000.*Expected json path result.*")); JSONCompare.assertNotMatches(expected, actual); String expected1 = "{\"#($.a.length())\":2,\"b\":\"val2\",\"x\":{\"x1\":{\"y11\":{\"#($.a)\":\"lorem2\"}}}," + @@ -34,21 +33,21 @@ public void checkMultipleJsonPathDifferences() { } @Test - public void checkMultipleJsonPathDifferencesFromArray() { + void checkMultipleJsonPathDifferencesFromArray() { String expected = "[false, {\"#($.length())\":1}, \"b\",{\"x\":{\"#($.length())\":2}}]"; String actual = "[\"b\",false,{\"x\":[1,2,5]}, {\"w\":\"yyyy\"}]"; AssertionError error = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected, actual)); assertTrue(error.getMessage().matches("(?s).*FOUND 2 DIFFERENCE.*" + - "\\QJson path '$.length()' -> Expected json path result\\E.*" + + "\\Q$.#($.length())\\E.*Expected json path result.*" + "1.*But got.*4.*" + - "Expected element from position 4 was NOT FOUND:.*")); + "\\Q$[3]\\E was not found:.*")); JSONCompare.assertNotMatches(expected, actual); String expected1 = "[false, {\"#($.length())\":1}, \"b\",{\"x\":{\"#($.length())\":3}}]"; String actual1 = "[\"b\",false,{\"x\":[1,2,5]}, {\"w\":\"yyyy\"}]"; AssertionError error1 = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected1, actual1)); assertTrue(error1.getMessage().matches("(?s).*FOUND 1 DIFFERENCE.*" + - "\\QJson path '$.length()' -> Expected json path result\\E.*" + + "\\Q$.#($.length())\\E.*Expected json path result.*" + "1.*But got.*4.*")); JSONCompare.assertNotMatches(expected1, actual1); @@ -58,15 +57,15 @@ public void checkMultipleJsonPathDifferencesFromArray() { } @Test - public void checkDoNotMatchAnyAndJsonPathsFromArray() { + void checkDoNotMatchAnyAndJsonPathsFromArray() { String expected = "[false, {\"#($.length())\":1}, \"b\",{\"x\":{\"#($.length())\":2}}, \"!.*\"]"; String actual = "[\"b\",false,{\"x\":[1,2,5]}, {\"w\":\"yyyy\"}]"; AssertionError error = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected, actual)); assertTrue(error.getMessage().matches("(?s).*FOUND 3 DIFFERENCE.*" + - "\\QJson path '$.length()' -> Expected json path result\\E.*" + + "\\Q$.#($.length())\\E.*" + "1.*But got.*4.*" + - "Expected element from position 4 was NOT FOUND:.*" + - "Expected condition \"\\Q!.*\\E\" from position 5 was not met. Actual JSON ARRAY has extra elements.*")); + "\\Q$[3]\\E was not found:.*" + + "\\Q$[4]\\E -> Expected condition \"!.*\" was not met. Actual JSON ARRAY has extra elements.*")); JSONCompare.assertNotMatches(expected, actual); String expected1 = "[false, {\"#($.length())\":3}, \"b\",{\"x\":{\"#($.length())\":3}}, \"!.*\"]"; @@ -75,12 +74,12 @@ public void checkDoNotMatchAnyAndJsonPathsFromArray() { } @Test - public void checkDoNotMatchAnyAndJsonPathsFromObject() { + void checkDoNotMatchAnyAndJsonPathsFromObject() { String expected = "{\"#($.length())\":2, \"b\":\"val1\", \"!.*\":\".*\"}"; String actual = "{\"b\":\"val1\", \"a\":\"val2\"}"; AssertionError error = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected, actual)); assertTrue(error.getMessage().matches("(?s).*FOUND 1 DIFFERENCE.*" + - "Expected condition '\\Q!.*\\E' was not met. Actual JSON OBJECT has extra fields.*")); + "\\Q$.\"!.*\"\\E condition was not met. Actual JSON OBJECT has extra fields.*")); JSONCompare.assertNotMatches(expected, actual); @@ -88,7 +87,7 @@ public void checkDoNotMatchAnyAndJsonPathsFromObject() { String actual1 = "{\"b\":\"val1\", \"a\":\"val2\"}"; error = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected, actual)); assertTrue(error.getMessage().matches("(?s).*FOUND 1 DIFFERENCE.*" + - "Expected condition '\\Q!.*\\E' was not met. Actual JSON OBJECT has extra fields.*")); + "\\Q$.\"!.*\"\\E condition was not met. Actual JSON OBJECT has extra fields.*")); JSONCompare.assertNotMatches(expected1, actual1); expected1 = "{\"#($.length())\":1, \"b\":\"val1\", \"!name\":\".*\", \"!.*\":\".*\"}"; diff --git a/src/test/java/io/json/compare/matcher/diffs/JsonRealWorldDiffTests.java b/src/test/java/io/json/compare/matcher/diffs/JsonRealWorldDiffTests.java index afbbfb2..ca64314 100644 --- a/src/test/java/io/json/compare/matcher/diffs/JsonRealWorldDiffTests.java +++ b/src/test/java/io/json/compare/matcher/diffs/JsonRealWorldDiffTests.java @@ -6,10 +6,10 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -public class JsonRealWorldDiffTests { +class JsonRealWorldDiffTests { @Test - public void compareJsonObjectsAndCheckForDifferences() { + void compareJsonObjectsAndCheckForDifferences() { String expected = "{\n" + " \"caught\": false,\n" + " \"pain\": {\n" + @@ -37,10 +37,10 @@ public void compareJsonObjectsAndCheckForDifferences() { "}"; AssertionError error = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected, actual)); assertTrue(error.getMessage().matches("(?s).*FOUND 4 DIFFERENCE.*" + - "caught ->.*Expected value: false But got: true.*" + - "pain -> range ->.*Expected element from position 2 was NOT FOUND.*\"blue\".*" + - "pain -> range ->.*Expected element from position 3 was NOT FOUND.*-2059921070.*" + - "pain -> Field 'not_anyone' was NOT FOUND.*")); + "\\Q$.caught\\E.*Expected value: false But got: true.*" + + "\\Q$.pain.range[1]\\E was not found.*\"blue\".*" + + "\\Q$.pain.range[2]\\E was not found.*-2059921070.*" + + "\\Q$.pain.not_anyone\\E was not found.*")); JSONCompare.assertNotMatches(expected, actual); } diff --git a/src/test/java/io/json/compare/matcher/diffs/JsonValueDiffTests.java b/src/test/java/io/json/compare/matcher/diffs/JsonValueDiffTests.java index a53f61f..011c60d 100644 --- a/src/test/java/io/json/compare/matcher/diffs/JsonValueDiffTests.java +++ b/src/test/java/io/json/compare/matcher/diffs/JsonValueDiffTests.java @@ -13,7 +13,7 @@ public void compareNulls() { String expected = "{\"a\":null}"; String actual = "{\"a\":\"null\"}"; AssertionError error = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected, actual)); - assertTrue(error.getMessage().matches("(?s).*FOUND 1 DIFFERENCE.*a ->.*Expected null: But got: \"null\".*")); + assertTrue(error.getMessage().matches("(?s).*FOUND 1 DIFFERENCE.*\\Q$.a\\E.*Expected null: But got: \"null\".*")); JSONCompare.assertNotMatches(expected, actual); } @@ -22,7 +22,7 @@ public void compareBooleans() { String expected = "{\"a\":false}"; String actual = "{\"a\":\"false\"}"; AssertionError error = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected, actual)); - assertTrue(error.getMessage().matches("(?s).*FOUND 1 DIFFERENCE.*a ->.*Expected boolean: false But got: \"false\".*")); + assertTrue(error.getMessage().matches("(?s).*FOUND 1 DIFFERENCE.*\\Q$.a\\E.*Expected boolean: false But got: \"false\".*")); JSONCompare.assertNotMatches(expected, actual); } @@ -31,7 +31,7 @@ public void compareNumbers() { String expected = "{\"a\":2}"; String actual = "{\"a\":\"2\"}"; AssertionError error = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected, actual)); - assertTrue(error.getMessage().matches("(?s).*FOUND 1 DIFFERENCE.*a ->.*Expected number: 2 But got: \"2\".*")); + assertTrue(error.getMessage().matches("(?s).*FOUND 1 DIFFERENCE.*\\Q$.a\\E.*Expected number: 2 But got: \"2\".*")); JSONCompare.assertNotMatches(expected, actual); } @@ -41,11 +41,11 @@ public void compareAll() { String actual = "{\"a\":\"null\",\"b\":\"1\",\"c\":\"false\",\"d\":false,\"e\":false,\"f\":13432.543,\"f1\":\"13432.543\"}"; AssertionError error = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected, actual)); assertTrue(error.getMessage().matches("(?s).*FOUND 5 DIFFERENCE.*" + - "a ->.*Expected null: But got: \"null\".*" + - "b ->.*Expected number: 1 But got: \"1\".*" + - "c ->.*Expected boolean: false But got: \"false\".*" + - "e ->.*Expected value: \"text\" But got: false.*" + - "f1 ->.*Expected number: 13432.543 But got: \"13432.543\".*")); + "\\Q$.a\\E.*Expected null: But got: \"null\".*" + + "\\Q$.b\\E.*Expected number: 1 But got: \"1\".*" + + "\\Q$.c\\E.*Expected boolean: false But got: \"false\".*" + + "\\Q$.e\\E.*Expected value: \"text\" But got: false.*" + + "\\Q$.f1\\E.*Expected number: 13432.543 But got: \"13432.543\".*")); JSONCompare.assertNotMatches(expected, actual); } diff --git a/src/test/java/io/json/compare/matcher/issues/Issue11Test.java b/src/test/java/io/json/compare/matcher/issues/Issue11Test.java index 05f8217..162501f 100644 --- a/src/test/java/io/json/compare/matcher/issues/Issue11Test.java +++ b/src/test/java/io/json/compare/matcher/issues/Issue11Test.java @@ -9,21 +9,21 @@ import static org.junit.jupiter.api.Assertions.assertTrue; -public class Issue11Test { +class Issue11Test { @Test - public void testSimpleJsonArrayStrictOrderWithRegexFieldsThrowsCorrectMessage() { + void testSimpleJsonArrayStrictOrderWithRegexFieldsThrowsCorrectMessage() { String expected = "{\".*\":{\"eventLogs\":[{\"id\":2},{\"id\":4},{\"id\":1},{\"id\":3}]}}"; String actual = "{\"_embedded\":{\"eventLogs\":[{\"id\":1},{\"id\":2},{\"id\":3},{\"id\":4}]}}"; try { JSONCompare.assertMatches(expected, actual, new HashSet<>(Arrays.asList(CompareMode.JSON_ARRAY_STRICT_ORDER))); } catch (AssertionError e) { - assertTrue(e.getMessage().contains("elements differ at position")); + assertTrue(e.getMessage().contains("$..*.eventLogs[0].id")); } } @Test - public void testJsonArrayStrictOrderThrowsCorrectMessage() { + void testJsonArrayStrictOrderThrowsCorrectMessage() { String expected = "{\"_embedded\":{\"eventLogs\":[{\"id\":2},{\"id\":4},{\"id\":1},{\"id\":3}]}}"; String actual = "{\n" + " \"_embedded\": {\n" + @@ -125,12 +125,12 @@ public void testJsonArrayStrictOrderThrowsCorrectMessage() { try { JSONCompare.assertMatches(expected, actual, new HashSet<>(Arrays.asList(CompareMode.JSON_ARRAY_STRICT_ORDER))); } catch (AssertionError e) { - assertTrue(e.getMessage().contains("elements differ at position")); + assertTrue(e.getMessage().contains("$._embedded.eventLogs[0].id")); } } @Test - public void testJsonArrayStrictOrderWithRegexFieldsThrowsCorrectMessage() { + void testJsonArrayStrictOrderWithRegexFieldsThrowsCorrectMessage() { String expected = "{\".*\":{\"eventLogs\":[{\"id\":2},{\"id\":4},{\"id\":1},{\"id\":3}]}}"; String actual = "{\n" + " \"_embedded\": {\n" + @@ -232,8 +232,7 @@ public void testJsonArrayStrictOrderWithRegexFieldsThrowsCorrectMessage() { try { JSONCompare.assertMatches(expected, actual, new HashSet<>(Arrays.asList(CompareMode.JSON_ARRAY_STRICT_ORDER))); } catch (AssertionError e) { - assertTrue(e.getMessage().contains("was NOT FOUND")); + assertTrue(e.getMessage().contains("was not found")); } } - } diff --git a/src/test/java/io/json/compare/matcher/readme/ReadmeTests.java b/src/test/java/io/json/compare/matcher/readme/ReadmeTests.java index 8291d81..5d7da8c 100644 --- a/src/test/java/io/json/compare/matcher/readme/ReadmeTests.java +++ b/src/test/java/io/json/compare/matcher/readme/ReadmeTests.java @@ -13,10 +13,10 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -public class ReadmeTests { +class ReadmeTests { @Test - public void matchJsonConvertibleJavaObjects() { + void matchJsonConvertibleJavaObjects() { String expectedString = "{\"a\":1, \"b\": [4, \"ipsum\", \"\\\\d+\"]}"; String actualString = "{\"a\":1, \"b\":[\"ipsum\", 4, 5], \"c\":true}"; JSONCompare.assertMatches(expectedString, actualString); // assertion passes @@ -33,12 +33,12 @@ public void matchJsonConvertibleJavaObjects() { JSONCompare.assertNotMatches(expectedString, anotherActualString); // assertion passes AssertionError error = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expectedString, anotherActualString)); assertTrue(error.getMessage().matches("(?s).*FOUND 2 DIFFERENCE.*" + - "a ->.*Expected value: 1 But got: 2.*" + - "b ->.*Expected element from position 2 was NOT FOUND.*")); + "\\Q$.a\\E.*Expected value: 1 But got: 2.*" + + "\\Q$.b[1]\\E was not found.*")); } @Test - public void checkJsonInclusion() { + void checkJsonInclusion() { // Check JSON object inclusion String expected = "{\"b\":\"val1\"}"; String actual = "{\"a\":\"val2\",\"b\":\"val1\"}"; @@ -55,7 +55,7 @@ public void checkJsonInclusion() { } @Test - public void checkJsonElementsOrder() { + void checkJsonElementsOrder() { // JSON Array strict order is by default ignored String expected = "[\"lorem\", 2, false]"; String actual = "[false, 2, \"lorem\", 5, 4]"; @@ -68,33 +68,33 @@ public void checkJsonElementsOrder() { AssertionError error = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected1, actual1, new HashSet<>(Arrays.asList(CompareMode.JSON_ARRAY_STRICT_ORDER)))); assertTrue(error.getMessage().matches("(?s).*FOUND 2 DIFFERENCE.*" + - "JSON ARRAY elements differ at position 1.*" + - "JSON ARRAY elements differ at position 3.*")); + "\\Q$[0]\\E.*" + + "\\Q$[2]\\E.*")); } @Test - public void matchJsonRegexValues() { + void matchJsonRegexValues() { String expected = "{\"a\": \".*me.*\"}"; String actual = "{\"a\": \"some text\"}"; JSONCompare.assertMatches(expected, actual); // assertion passes } @Test - public void matchJsonRegexFields() { + void matchJsonRegexFields() { String expected = "{\".*oba.*\": \"some value\"}"; String actual = "{\"foobar\": \"some value\"}"; JSONCompare.assertMatches(expected, actual); // assertion passes } @Test - public void matchJsonRegexQuote() { + void matchJsonRegexQuote() { String expected = "{\"a\":\"\\\\Qd+\\\\E\"}"; String actual = "{\"a\":\"d+\"}"; JSONCompare.assertMatches(expected, actual); // assertion passes } @Test - public void matchJsonRegexCustomComparator() { + void matchJsonRegexCustomComparator() { String expected = "{\"a\": \"\\\\d+\"}"; String actual = "{\"a\": \"\\\\d+\"}"; JSONCompare.assertMatches(expected, actual, new JsonComparator() { @@ -109,56 +109,56 @@ public boolean compareFields(String expected, String actual) { } @Test - public void matchJsonTweaksDoNotMatchValues() { + void matchJsonTweaksDoNotMatchValues() { String expected = "{\"a\": \"!test\"}"; String actual = "{\"a\": \"testing\"}"; JSONCompare.assertMatches(expected, actual); // assertion passes } @Test - public void matchJsonTweaksDoNotMatchFields() { + void matchJsonTweaksDoNotMatchFields() { String expected = "{\"!a\": \"value does not matter\"}"; String actual = "{\"b\": \"of course value does not matter\"}"; JSONCompare.assertMatches(expected, actual); // assertion passes } @Test - public void matchJsonTweaksNegativeLookaround() { + void matchJsonTweaksNegativeLookaround() { String expected = "{\"(?!lorem.*).*\": \"valorem\"}"; String actual = "{\"ipsum\": \"valorem\"}"; JSONCompare.assertMatches(expected, actual); // assertion passes } @Test - public void matchJsonTweaksDoNotMatchAnyObjectFields() { + void matchJsonTweaksDoNotMatchAnyObjectFields() { String expected = "{\"b\": \"val1\", \"!.*\": \".*\"}"; String actual = "{\"a\": \"val2\", \"b\": \"val1\"}"; JSONCompare.assertNotMatches(expected, actual); } @Test - public void matchJsonTweaksDoNotMatchAnyArray() { + void matchJsonTweaksDoNotMatchAnyArray() { String expected = "[false, \"test\", 4, \"!.*\"]"; String actual = "[4, false, \"test\", 1]"; JSONCompare.assertNotMatches(expected, actual); } @Test - public void matchJsonTweaksMatchAnyObjectFields() { + void matchJsonTweaksMatchAnyObjectFields() { String expected = "{\"b\": \"val1\", \".*\": \".*\"}"; String actual = "{\"b\": \"val1\"}"; JSONCompare.assertNotMatches(expected, actual); } @Test - public void matchJsonTweaksMatchAnyArray() { + void matchJsonTweaksMatchAnyArray() { String expected = "[false, \"test\", 4, \".*\"]"; String actual = "[4, false, \"test\"]"; JSONCompare.assertNotMatches(expected, actual); } @Test - public void matchJsonTweaksJsonPath() { + void matchJsonTweaksJsonPath() { String expected = "{\"#($.store..isbn)\":[\"0-395-19395-8\",\"0-553-21311-3\",\"!.*\"]}"; String actual = "{\n" + " \"store\": {\n" +