diff --git a/json-unit-core/src/main/java/net/javacrumbs/jsonunit/core/internal/Diff.java b/json-unit-core/src/main/java/net/javacrumbs/jsonunit/core/internal/Diff.java index cc62cc7a..6a1cc7e7 100644 --- a/json-unit-core/src/main/java/net/javacrumbs/jsonunit/core/internal/Diff.java +++ b/json-unit-core/src/main/java/net/javacrumbs/jsonunit/core/internal/Diff.java @@ -72,6 +72,7 @@ public class Diff { private static final Pattern ANY_NUMBER_PLACEHOLDER = Pattern.compile("[$#]\\{json-unit.any-number\\}"); private static final Pattern ANY_BOOLEAN_PLACEHOLDER = Pattern.compile("[$#]\\{json-unit.any-boolean\\}"); private static final Pattern ANY_STRING_PLACEHOLDER = Pattern.compile("[$#]\\{json-unit.any-string\\}"); + private static final Pattern ANY_ARRAY_PLACEHOLDER = Pattern.compile("[$#]\\{json-unit.any-array\\}"); private static final Pattern REGEX_PLACEHOLDER = Pattern.compile("[$#]\\{json-unit.regex\\}(.*)"); private static final Pattern MATCHER_PLACEHOLDER_PATTERN = @@ -349,6 +350,11 @@ private void compareNodes(Context context) { return; } + // Any array + if (checkAny(NodeType.ARRAY, ANY_ARRAY_PLACEHOLDER, "an array", context)) { + return; + } + if (checkMatcher(context)) { return; } diff --git a/tests/test-base/src/main/java/net/javacrumbs/jsonunit/test/base/AbstractAssertJTest.java b/tests/test-base/src/main/java/net/javacrumbs/jsonunit/test/base/AbstractAssertJTest.java index b82972c5..e7a31327 100644 --- a/tests/test-base/src/main/java/net/javacrumbs/jsonunit/test/base/AbstractAssertJTest.java +++ b/tests/test-base/src/main/java/net/javacrumbs/jsonunit/test/base/AbstractAssertJTest.java @@ -119,8 +119,8 @@ void shouldAssertNullValueNode() { @Test void shouldFailCorrectlyOnNull() { assertThatThrownBy(() -> assertThatJson(null).isEqualTo(1)) - .hasMessage( - "JSON documents are different:\nDifferent value found in node \"\", expected: <1> but was: .\n"); + .hasMessage( + "JSON documents are different:\nDifferent value found in node \"\", expected: <1> but was: .\n"); } @Test @@ -130,23 +130,23 @@ void containsEntryShouldWork() { String input = "[{\"allowedActions\":" + entryValue + "}]"; assertThatJson( - input, - body -> body.isArray().hasSize(1), - body -> body.inPath("[0]").isObject().containsEntry("allowedActions", json(entryValue)), - body -> body.inPath("[0]").isObject().contains(entry("allowedActions", json(entryValue))), - body -> body.inPath("[0]") - .isObject() - .containsAllEntriesOf(singletonMap("allowedActions", json(entryValue))), - body -> body.inPath("[0]") - .isObject() - .containsAnyOf(entry("allowedActions", json(entryValue)), entry("test", 1)), - body -> body.inPath("[0]") - .isObject() - .containsExactlyInAnyOrderEntriesOf(singletonMap("allowedActions", json(entryValue))), - body -> body.inPath("[0]").isObject().containsOnly(entry("allowedActions", json(entryValue))), - body -> body.inPath("[0]").isObject().containsValues(json(entryValue)), - body -> body.inPath("[0]").isObject().containsValue(json(entryValue)), - body -> body.inPath("[0].allowedActions").isObject().isEqualTo(json(entryValue))); + input, + body -> body.isArray().hasSize(1), + body -> body.inPath("[0]").isObject().containsEntry("allowedActions", json(entryValue)), + body -> body.inPath("[0]").isObject().contains(entry("allowedActions", json(entryValue))), + body -> body.inPath("[0]") + .isObject() + .containsAllEntriesOf(singletonMap("allowedActions", json(entryValue))), + body -> body.inPath("[0]") + .isObject() + .containsAnyOf(entry("allowedActions", json(entryValue)), entry("test", 1)), + body -> body.inPath("[0]") + .isObject() + .containsExactlyInAnyOrderEntriesOf(singletonMap("allowedActions", json(entryValue))), + body -> body.inPath("[0]").isObject().containsOnly(entry("allowedActions", json(entryValue))), + body -> body.inPath("[0]").isObject().containsValues(json(entryValue)), + body -> body.inPath("[0]").isObject().containsValue(json(entryValue)), + body -> body.inPath("[0].allowedActions").isObject().isEqualTo(json(entryValue))); } @Test @@ -160,10 +160,10 @@ void containsEntryShouldWorkWithMatcher() { void containsOnlyShouldWorkWithMatcher() { String json = "{\"a\": 1, \"b\": 2}"; assertThatJson(json) - .isObject() - .containsOnly( - entry("a", json("\"${json-unit.any-number}\"")), - entry("b", json("\"${json-unit.any-number}\""))); + .isObject() + .containsOnly( + entry("a", json("\"${json-unit.any-number}\"")), + entry("b", json("\"${json-unit.any-number}\""))); } @Test @@ -171,12 +171,12 @@ void containsEntryShouldFailWithMatcher() { String json = "{\"a\": 1, \"b\": 2}"; assertThatThrownBy(() -> assertThatJson(json) - .isObject() - .contains( - entry("a", json("\"${json-unit.any-string}\"")), - entry("b", json("\"${json-unit.any-number}\"")))) - .hasMessage( - """ + .isObject() + .contains( + entry("a", json("\"${json-unit.any-string}\"")), + entry("b", json("\"${json-unit.any-number}\"")))) + .hasMessage( + """ [Different value found in node ""]\s Expecting map: {"a":1,"b":2} @@ -191,10 +191,10 @@ void containsEntryShouldFailWithMatcher() { void containsAnyOfShouldWorkWithMatcher() { String json = "{\"a\": 1, \"b\": 2}"; assertThatJson(json) - .isObject() - .containsAnyOf( - entry("a", json("\"${json-unit.any-string}\"")), - entry("a", json("\"${json-unit.any-number}\""))); + .isObject() + .containsAnyOf( + entry("a", json("\"${json-unit.any-string}\"")), + entry("a", json("\"${json-unit.any-number}\""))); } @Test @@ -202,12 +202,12 @@ void containsAnyOfShouldFailWithMatcher() { String json = "{\"a\": 1, \"b\": 2}"; assertThatThrownBy(() -> assertThatJson(json) - .isObject() - .containsAnyOf( - entry("a", json("\"${json-unit.any-string}\"")), - entry("b", json("\"${json-unit.any-string}\"")))) - .hasMessage( - """ + .isObject() + .containsAnyOf( + entry("a", json("\"${json-unit.any-string}\"")), + entry("b", json("\"${json-unit.any-string}\"")))) + .hasMessage( + """ [Different value found in node ""]\s Expecting actual: {"a":1,"b":2} @@ -226,10 +226,10 @@ void containsValuesShouldPass() { void containsValuesShouldFail() { String json = "{\"a\": 1, \"b\": 2}"; assertThatThrownBy(() -> assertThatJson(json) - .isObject() - .containsValues(valueOf(1), valueOf(2), json("\"${json-unit.any-string}\""))) - .hasMessage( - """ + .isObject() + .containsValues(valueOf(1), valueOf(2), json("\"${json-unit.any-string}\""))) + .hasMessage( + """ [Different value found in node ""]\s Expecting actual: {"a":1,"b":2} @@ -247,7 +247,7 @@ void absentOnArray() { @Test void invalidExpectedValue() { assertThatThrownBy(() -> assertThatJson(json).isEqualTo("{\"broken\":")) - .hasMessage("Can not parse expected value: '{\"broken\":'"); + .hasMessage("Can not parse expected value: '{\"broken\":'"); } @Test @@ -258,18 +258,18 @@ void objectShouldContainValue() { @Test void objectShouldContainComplexValue() { assertThatJson("{\"a\":1, \"b\": {\"c\" :3}}") - .isObject() - .containsValue(json("{\"c\" :\"${json-unit.any-number}\"}")); + .isObject() + .containsValue(json("{\"c\" :\"${json-unit.any-number}\"}")); } @Test void objectShouldContainComplexValueError() { assertThatThrownBy(() -> assertThatJson("{\"root\":{\"a\":1, \"b\": {\"c\" :3}}}") - .node("root") - .isObject() - .containsValue(json("{\"c\" :5}"))) - .hasMessage( - """ + .node("root") + .isObject() + .containsValue(json("{\"c\" :5}"))) + .hasMessage( + """ [Different value found in node "root"]\s Expecting actual: {"a":1,"b":{"c":3}} @@ -280,26 +280,26 @@ void objectShouldContainComplexValueError() { @Test protected void objectFieldsShouldBeKeptInOrder() { assertThatJson("{\"root\":{\"key3\": 3, \"key2\": 2, \"key1\": 1 }}") - .node("root") - .isObject() - .containsExactly(entry("key3", valueOf(3)), entry("key2", valueOf(2)), entry("key1", valueOf(1))); + .node("root") + .isObject() + .containsExactly(entry("key3", valueOf(3)), entry("key2", valueOf(2)), entry("key1", valueOf(1))); } @Test void objectDoesContainComplexValue() { assertThatJson("{\"a\":1, \"b\": {\"c\" :3}}") - .isObject() - .doesNotContainValue(json("{\"c\" :\"${json-unit.any-string}\"}")); + .isObject() + .doesNotContainValue(json("{\"c\" :\"${json-unit.any-string}\"}")); } @Test void objectDoesContainComplexValueError() { assertThatThrownBy(() -> assertThatJson("{\"root\":{\"a\":1, \"b\": {\"c\" :3}}}") - .node("root") - .isObject() - .doesNotContainValue(json("{\"c\" :3}"))) - .hasMessage( - """ + .node("root") + .isObject() + .doesNotContainValue(json("{\"c\" :3}"))) + .hasMessage( + """ [Different value found in node "root"]\s Expecting actual: {"a":1,"b":{"c":3}} @@ -310,9 +310,9 @@ void objectDoesContainComplexValueError() { @Test void compareJsonInJsonPathArray() { assertThatJson("{\"root\": [{\"target\": 450} ]}") - .inPath("$.root") - .isArray() - .containsExactly("{\"target\": 450 }"); + .inPath("$.root") + .isArray() + .containsExactly("{\"target\": 450 }"); } @Test @@ -323,11 +323,11 @@ void compareJsonInJsonPathShallowArray() { @Test void compareJsonInJsonPathShallowArrayString() { assertThatThrownBy(() -> assertThatJson("{\"root\": [450]}") - .inPath("$.root") - .isArray() - .containsExactly(value("450"))) - .hasMessage( - """ + .inPath("$.root") + .isArray() + .containsExactly(value("450"))) + .hasMessage( + """ [Node "$.root"]\s Expecting actual: [450] @@ -343,17 +343,17 @@ to contain exactly (and in same order): @Test void compareJsonInPathArrayOfArrays() { assertThatJson("{\"root\": [[{\"target\": 450} ]]}") - .inPath("$.root") - .isArray() - .containsExactly("[{\"target\": 450 }]"); + .inPath("$.root") + .isArray() + .containsExactly("[{\"target\": 450 }]"); } @Test void compareJsonInNodeArray() { assertThatJson("{\"root\": [{\"target\": 450} ]}") - .node("root") - .isArray() - .containsExactly("{\"target\": 450 }"); + .node("root") + .isArray() + .containsExactly("{\"target\": 450 }"); } @Test @@ -374,16 +374,16 @@ void compareJsonInNodeShallowArrayBoolean() { @Test void compareJsonInNodeArrayOfArrays() { assertThatJson("{\"root\": [[{\"target\": 450} ]]}") - .node("root") - .isArray() - .containsExactly("[{\"target\": 450 }]"); + .node("root") + .isArray() + .containsExactly("[{\"target\": 450 }]"); } @Test void compareJsonArray() { assertThatJson("{\"root\": [{\"target\": 450} ]}") - .node("root") - .isEqualTo(singletonList(singletonMap("target", 450))); + .node("root") + .isEqualTo(singletonList(singletonMap("target", 450))); } @Test @@ -394,9 +394,9 @@ void shouldAssertDirectEqual() { @Test void shouldIgnoreIfMissing() { assertThatThrownBy(() -> assertThatJson("{\"root\":{\"test\":1}}") - .isEqualTo("{\"root\":{\"test\":1, \"ignored\": \"${json-unit.ignore}\"}}")) - .hasMessage( - """ + .isEqualTo("{\"root\":{\"test\":1, \"ignored\": \"${json-unit.ignore}\"}}")) + .hasMessage( + """ JSON documents are different: Different keys found in node "root", missing: "root.ignored", expected: <{"ignored":"${json-unit.ignore}","test":1}> but was: <{"test":1}> """); @@ -405,41 +405,41 @@ void shouldIgnoreIfMissing() { @Test void shouldIgnoreIfNull() { assertThatJson("{\"root\":{\"test\":1, \"ignored\": null}}") - .isEqualTo("{\"root\":{\"test\":1, \"ignored\": \"${json-unit.ignore}\"}}"); + .isEqualTo("{\"root\":{\"test\":1, \"ignored\": \"${json-unit.ignore}\"}}"); } @Test void shouldIgnoreIfObject() { assertThatJson("{\"root\":{\"test\":1, \"ignored\": {\"a\": 1}}}") - .isEqualTo("{\"root\":{\"test\":1, \"ignored\": \"${json-unit.ignore}\"}}"); + .isEqualTo("{\"root\":{\"test\":1, \"ignored\": \"${json-unit.ignore}\"}}"); } @Test void shouldIgnoreElementIfMissing() { assertThatJson("{\"root\":{\"test\":1}}") - .isEqualTo("{\"root\":{\"test\":1, \"ignored\": \"${json-unit.ignore-element}\"}}"); + .isEqualTo("{\"root\":{\"test\":1, \"ignored\": \"${json-unit.ignore-element}\"}}"); } @Test void shouldIgnoreElementIfNull() { assertThatJson("{\"root\":{\"test\":1, \"ignored\": null}}") - .isEqualTo("{\"root\":{\"test\":1, \"ignored\": \"${json-unit.ignore-element}\"}}"); + .isEqualTo("{\"root\":{\"test\":1, \"ignored\": \"${json-unit.ignore-element}\"}}"); } @Test void shouldIgnoreElementIfObject() { assertThatJson("{\"root\":{\"test\":1, \"ignored\": {\"a\": 1}}}") - .isEqualTo("{\"root\":{\"test\":1, \"ignored\": \"${json-unit.ignore-element}\"}}"); + .isEqualTo("{\"root\":{\"test\":1, \"ignored\": \"${json-unit.ignore-element}\"}}"); } @Test void shouldAssertObjectJson() { assertThatThrownBy(() -> assertThatJson("{\"a\":{\"b\": 1}}") - .node("a") - .isObject() - .isEqualTo(json("{\"b\": 2}"))) - .hasMessage( - """ + .node("a") + .isObject() + .isEqualTo(json("{\"b\": 2}"))) + .hasMessage( + """ JSON documents are different: Different value found in node "a.b", expected: <2> but was: <1>. """); @@ -453,11 +453,11 @@ void shouldAssertContainsEntry() { @Test void shouldAssertContainsJsonError() { assertThatThrownBy(() -> assertThatJson("{\"a\":{\"b\": 1}}") - .node("a") - .isObject() - .contains(entry("b", valueOf(2)))) - .hasMessage( - """ + .node("a") + .isObject() + .contains(entry("b", valueOf(2)))) + .hasMessage( + """ [Different value found in node "a"]\s Expecting map: {"b":1} @@ -476,11 +476,11 @@ void shouldAssertContainsOnlyKeys() { @Test void shouldAssertContainsOnlyKeysError() { assertThatThrownBy(() -> assertThatJson("{\"a\":{\"b\": 1, \"c\": true}}") - .node("a") - .isObject() - .containsOnlyKeys("b", "c", "d")) - .hasMessage( - """ + .node("a") + .isObject() + .containsOnlyKeys("b", "c", "d")) + .hasMessage( + """ [Different value found in node "a"]\s Expecting actual: {"b":1,"c":true} @@ -494,19 +494,19 @@ void shouldAssertContainsOnlyKeysError() { @Test void shouldAssertContainsAllEntries() { assertThatJson("{\"a\":{\"b\": 1, \"c\": true}}") - .node("a") - .isObject() - .containsAllEntriesOf(singletonMap("c", true)); + .node("a") + .isObject() + .containsAllEntriesOf(singletonMap("c", true)); } @Test void shouldAssertContainsAllEntriesError() { assertThatThrownBy(() -> assertThatJson("{\"a\":{\"b\": 1, \"c\": true}}") - .node("a") - .isObject() - .containsAllEntriesOf(singletonMap("c", false))) - .hasMessage( - """ + .node("a") + .isObject() + .containsAllEntriesOf(singletonMap("c", false))) + .hasMessage( + """ [Different value found in node "a"]\s Expecting map: {"b":1,"c":true} @@ -520,8 +520,8 @@ void shouldAssertContainsAllEntriesError() { @Test void shouldAssertJson() { assertThatThrownBy(() -> assertThatJson("{\"a\":{\"b\": 1}}").node("a").isEqualTo(json("{\"b\": 2}"))) - .hasMessage( - """ + .hasMessage( + """ JSON documents are different: Different value found in node "a.b", expected: <2> but was: <1>. """); @@ -530,27 +530,27 @@ void shouldAssertJson() { @Test void shouldAssertObjectJsonWithPlaceholder() { assertThatJson("{\"a\":{\"b\": \"ignored\"}}") - .node("a") - .isObject() - .isEqualTo(json("{\"b\":\"${json-unit.any-string}\"}")); + .node("a") + .isObject() + .isEqualTo(json("{\"b\":\"${json-unit.any-string}\"}")); } @Test void shouldAssertObjectIsNotEqualToJsonWithPlaceholder() { assertThatJson("{\"a\":{\"b\": 1}}") - .node("a") - .isObject() - .isNotEqualTo(json("{\"b\":\"${json-unit.any-string}\"}")); + .node("a") + .isObject() + .isNotEqualTo(json("{\"b\":\"${json-unit.any-string}\"}")); } @Test void shouldAssertObjectIsNotEqualToJsonWithPlaceholderError() { assertThatThrownBy(() -> assertThatJson("{\"a\":{\"b\": \"string\"}}") - .node("a") - .isObject() - .isNotEqualTo(json("{\"b\":\"${json-unit.any-string}\"}"))) - .hasMessage( - """ + .node("a") + .isObject() + .isNotEqualTo(json("{\"b\":\"${json-unit.any-string}\"}"))) + .hasMessage( + """ [Different value found in node "a"]\s Expecting actual: {"b":"string"} @@ -562,11 +562,11 @@ void shouldAssertObjectIsNotEqualToJsonWithPlaceholderError() { @Test void shouldAssertObjectJsonWithPlaceholderFailure() { assertThatThrownBy(() -> assertThatJson("{\"a\":{\"b\": 1}}") - .node("a") - .isObject() - .isEqualTo(json("{\"b\":\"${json-unit.any-string}\"}"))) - .hasMessage( - """ + .node("a") + .isObject() + .isEqualTo(json("{\"b\":\"${json-unit.any-string}\"}"))) + .hasMessage( + """ JSON documents are different: Different value found in node "a.b", expected: but was: <1>. """); @@ -575,11 +575,11 @@ void shouldAssertObjectJsonWithPlaceholderFailure() { @Test void shouldAssertString() { assertThatThrownBy(() -> assertThatJson("{\"a\":{\"b\": \"foo\"}}") - .node("a.b") - .isString() - .startsWith("bar")) - .hasMessage( - """ + .node("a.b") + .isString() + .startsWith("bar")) + .hasMessage( + """ [Different value found in node "a.b"]\s Expecting actual: "foo" @@ -591,12 +591,12 @@ void shouldAssertString() { @Test void shouldAssertStringCustomDescription() { assertThatThrownBy(() -> assertThatJson("{\"a\":{\"b\": \"foo\"}}") - .node("a.b") - .isString() - .as("Sad!") - .startsWith("bar")) - .hasMessage( - """ + .node("a.b") + .isString() + .as("Sad!") + .startsWith("bar")) + .hasMessage( + """ [Sad!]\s Expecting actual: "foo" @@ -613,17 +613,17 @@ void shouldAssertArray() { @Test void shouldFindObjectInArray() { assertThatJson("{\"a\":[{\"b\": 1}, {\"c\": 1}, {\"d\": 1}]}") - .node("a") - .isArray() - .contains(json("{\"c\": 1}")); + .node("a") + .isArray() + .contains(json("{\"c\": 1}")); } @Test void shouldFindObjectInArrayWithPlaceholder() { assertThatJson("{\"a\":[{\"b\": 1}, {\"c\": 1}, {\"d\": 1}]}") - .node("a") - .isArray() - .contains(json("{\"c\": \"${json-unit.any-number}\"}")); + .node("a") + .isArray() + .contains(json("{\"c\": \"${json-unit.any-number}\"}")); } @Test @@ -634,29 +634,29 @@ void arraySimpleIgnoringOrderComparisonExample() { @Test void arrayIgnoringOrderComparison() { assertThatJson("{\"a\":[{\"b\": 1}, {\"c\": 1}, {\"d\": 1}]}") - .node("a") - .isArray() - .containsExactlyInAnyOrder(json("{\"c\": 1}"), json("{\"b\": 1}"), json("{\"d\": 1}")); + .node("a") + .isArray() + .containsExactlyInAnyOrder(json("{\"c\": 1}"), json("{\"b\": 1}"), json("{\"d\": 1}")); } @Test void arraySimpleIgnoringOrderComparison() { assertThatJson("{\"a\":[{\"b\": 1}, {\"c\": 1}, {\"d\": 1}]}") - .when(Option.IGNORING_ARRAY_ORDER) - .node("a") - .isArray() - .isEqualTo(json("[{\"c\": 1}, {\"b\": 1} ,{\"d\": 1}]")); + .when(Option.IGNORING_ARRAY_ORDER) + .node("a") + .isArray() + .isEqualTo(json("[{\"c\": 1}, {\"b\": 1} ,{\"d\": 1}]")); } @Test void arraySimpleIgnoringOrderComparisonError() { assertThatThrownBy(() -> assertThatJson("{\"a\":[{\"b\": 1}, {\"c\": 1}, {\"d\": 1}]}") - .when(Option.IGNORING_ARRAY_ORDER) - .node("a") - .isArray() - .isEqualTo(json("[{\"c\": 2}, {\"b\": 1} ,{\"d\": 1}]"))) - .hasMessage( - """ + .when(Option.IGNORING_ARRAY_ORDER) + .node("a") + .isArray() + .isEqualTo(json("[{\"c\": 2}, {\"b\": 1} ,{\"d\": 1}]"))) + .hasMessage( + """ JSON documents are different: Different value found when comparing expected array element a[0] to actual element a[1]. Different value found in node "a[1].c", expected: <2> but was: <1>. @@ -666,26 +666,26 @@ void arraySimpleIgnoringOrderComparisonError() { @Test void multipleFailuresErrorShouldBeCorrectlyFormatted() { assertThatExceptionOfType(MultipleFailuresError.class) - .isThrownBy(() -> assertThatJson("{\"a\":[{\"b\": 1}, {\"c\": 1}, {\"d\": 1}]}") - .when(Option.IGNORING_ARRAY_ORDER) - .node("a") - .isArray() - .isEqualTo(json("[{\"c\": 2}, {\"b\": 1} ,{\"d\": 1}]"))) - .satisfies(e -> { - List failures = e.getFailures(); - assertThat(failures.get(0).getMessage()) - .isEqualTo( - "Different value found when comparing expected array element a[0] to actual element a[1]."); - assertThat(failures.get(1).getMessage()) - .isEqualTo("Different value found in node \"a[1].c\", expected: <2> but was: <1>."); - }); + .isThrownBy(() -> assertThatJson("{\"a\":[{\"b\": 1}, {\"c\": 1}, {\"d\": 1}]}") + .when(Option.IGNORING_ARRAY_ORDER) + .node("a") + .isArray() + .isEqualTo(json("[{\"c\": 2}, {\"b\": 1} ,{\"d\": 1}]"))) + .satisfies(e -> { + List failures = e.getFailures(); + assertThat(failures.get(0).getMessage()) + .isEqualTo( + "Different value found when comparing expected array element a[0] to actual element a[1]."); + assertThat(failures.get(1).getMessage()) + .isEqualTo("Different value found in node \"a[1].c\", expected: <2> but was: <1>."); + }); } @Test void shouldIgnoreMissingPathEvenIfItIsInExpectedValue() { assertThatJson("{\"root\":{\"foo\":1}}") - .whenIgnoringPaths("root.bar", "missing") - .isEqualTo("{\"root\":{\"foo\":1, \"bar\":2}, \"missing\":{\"quux\":\"test\"}}"); + .whenIgnoringPaths("root.bar", "missing") + .isEqualTo("{\"root\":{\"foo\":1, \"bar\":2}, \"missing\":{\"quux\":\"test\"}}"); } @Test @@ -704,21 +704,21 @@ void shouldIgnoreJsonPaths() { @Test void arraySimpleIgnoringOrderNotEqualComparison() { assertThatJson("{\"a\":[{\"b\": 1}, {\"c\": 1}, {\"d\": 1}]}") - .when(Option.IGNORING_ARRAY_ORDER) - .node("a") - .isArray() - .isNotEqualTo(json("[{\"c\": 2}, {\"b\": 1} ,{\"d\": 1}]")); + .when(Option.IGNORING_ARRAY_ORDER) + .node("a") + .isArray() + .isNotEqualTo(json("[{\"c\": 2}, {\"b\": 1} ,{\"d\": 1}]")); } @Test void arraySimpleIgnoringOrderNotEqualComparisonError() { assertThatThrownBy(() -> assertThatJson("{\"a\":[{\"b\": 1}, {\"c\": 1}, {\"d\": 1}]}") - .when(Option.IGNORING_ARRAY_ORDER) - .node("a") - .isArray() - .isNotEqualTo(json("[{\"c\": 1}, {\"b\": 1} ,{\"d\": 1}]"))) - .hasMessage( - """ + .when(Option.IGNORING_ARRAY_ORDER) + .node("a") + .isArray() + .isNotEqualTo(json("[{\"c\": 1}, {\"b\": 1} ,{\"d\": 1}]"))) + .hasMessage( + """ [Node "a"]\s Expecting: <[{"b":1}, {"c":1}, {"d":1}]> @@ -730,10 +730,10 @@ void arraySimpleIgnoringOrderNotEqualComparisonError() { @Test void shouldAssertBoolean() { assertThatThrownBy(() -> assertThatJson("{\"a\":{\"b\": true}}") - .node("a.b") - .isBoolean() - .isFalse()) - .hasMessage("[Different value found in node \"a.b\"] \n" + "Expecting value to be false but was true"); + .node("a.b") + .isBoolean() + .isFalse()) + .hasMessage("[Different value found in node \"a.b\"] \n" + "Expecting value to be false but was true"); } @Test @@ -744,37 +744,37 @@ void shouldAssertNull() { @Test void shouldAssertNullFail() { assertThatThrownBy( - () -> assertThatJson("{\"a\":{\"b\": 1}}").node("a.b").isNull()) - .hasMessage("Node \"a.b\" has invalid type, expected: but was: <1>."); + () -> assertThatJson("{\"a\":{\"b\": 1}}").node("a.b").isNull()) + .hasMessage("Node \"a.b\" has invalid type, expected: but was: <1>."); } @Test void shouldAssertNotNull() { assertThatThrownBy(() -> - assertThatJson("{\"a\":{\"b\": null}}").node("a.b").isNotNull()) - .hasMessage("Node \"a.b\" has invalid type, expected: but was: ."); + assertThatJson("{\"a\":{\"b\": null}}").node("a.b").isNotNull()) + .hasMessage("Node \"a.b\" has invalid type, expected: but was: ."); } @Test void shouldAssertNotNullChain() { assertThatThrownBy(() -> assertThatJson("{\"a\":{\"b\": null}}") - .node("a") - .isPresent() - .node("b") - .isNotNull()) - .hasMessage("Node \"a.b\" has invalid type, expected: but was: ."); + .node("a") + .isPresent() + .node("b") + .isNotNull()) + .hasMessage("Node \"a.b\" has invalid type, expected: but was: ."); } @Test void shouldAssertNotNullChaining() { assertThatThrownBy(() -> assertThatJson("{\"a\":{\"b\": 1}}") - .node("a") - .isNotNull() - .node("b") - .isNumber() - .isEqualByComparingTo("2")) - .hasMessage( - """ + .node("a") + .isNotNull() + .node("b") + .isNumber() + .isEqualByComparingTo("2")) + .hasMessage( + """ [Different value found in node "a.b"]\s expected: 2 but was: 1"""); @@ -783,30 +783,30 @@ void shouldAssertNotNullChaining() { @Test void shouldAssertNotNullChainingSuccess() { assertThatJson("{\"a\":{\"b\": 1}}") - .node("a") - .isNotNull() - .node("b") - .isNumber() - .isEqualByComparingTo("1"); + .node("a") + .isNotNull() + .node("b") + .isNumber() + .isEqualByComparingTo("1"); } @Test void shouldAssertUri() { assertThatJson("{\"a\":{\"b\":\"http://exampl.org?a=1\"}}") - .node("a.b") - .isUri() - .hasScheme("http") - .hasParameter("a", "1"); + .node("a.b") + .isUri() + .hasScheme("http") + .hasParameter("a", "1"); } @Test void shouldAssertUriFail() { assertThatThrownBy(() -> assertThatJson("{\"a\":{\"b\":\"test\"}}") - .node("a.b") - .isUri() - .hasScheme("http")) - .hasMessage( - """ + .node("a.b") + .isUri() + .hasScheme("http")) + .hasMessage( + """ [Different value found in node "a.b"]\s Expecting scheme of @@ -819,8 +819,8 @@ void shouldAssertUriFail() { @Test void shouldAssertUriFailOnType() { assertThatThrownBy( - () -> assertThatJson("{\"a\":{\"b\": 1}}").node("a.b").isUri()) - .hasMessage("Node \"a.b\" has invalid type, expected: but was: <1>."); + () -> assertThatJson("{\"a\":{\"b\": 1}}").node("a.b").isUri()) + .hasMessage("Node \"a.b\" has invalid type, expected: but was: <1>."); } @Test @@ -832,38 +832,38 @@ void canNotConfigureAfterAssertion() { @Test void shouldAssertNotNullMissing() { assertThatThrownBy(() -> - assertThatJson("{\"a\":{\"b\": null}}").node("a.c").isNotNull()) - .hasMessage("Different value found in node \"a.c\", expected: but was: ."); + assertThatJson("{\"a\":{\"b\": null}}").node("a.c").isNotNull()) + .hasMessage("Different value found in node \"a.c\", expected: but was: ."); } @Test void shouldAssertObjectFailure() { assertThatThrownBy(() -> assertThatJson("true").isObject()) - .hasMessage("Node \"\" has invalid type, expected: but was: ."); + .hasMessage("Node \"\" has invalid type, expected: but was: ."); } @Test void absentInPathShouldFailOnSimpleJson() { assertThatThrownBy(() -> - assertThatJson("{\"a\":{\"b\": 1}}").inPath("$.a.b").isAbsent()) - .hasMessage("Different value found in node \"$.a.b\", expected: but was: <1>."); + assertThatJson("{\"a\":{\"b\": 1}}").inPath("$.a.b").isAbsent()) + .hasMessage("Different value found in node \"$.a.b\", expected: but was: <1>."); } @Test void absentInPathShouldFailOnArray() { assertThatThrownBy(() -> assertThatJson("[{\"b\": 1}, {\"c\": 1}]") - .inPath("[*].c") - .isAbsent()) - .hasMessage("Different value found in node \"$[1].c\", expected: but was: <[1]>."); + .inPath("[*].c") + .isAbsent()) + .hasMessage("Different value found in node \"$[1].c\", expected: but was: <[1]>."); } @Test void absentInPathShouldFailOnMultipleMatches() { assertThatThrownBy(() -> assertThatJson("[{\"c\": {\"x\": 2}}, {\"b\": {\"x\": 2}}, {\"c\": {\"x\": 2}}]") - .inPath("$.[*].c") - .isAbsent()) - .hasMessage( - "Different value found in nodes \"[$[0].c, $[2].c]\", expected: but was: <[{\"x\":2},{\"x\":2}]>."); + .inPath("$.[*].c") + .isAbsent()) + .hasMessage( + "Different value found in nodes \"[$[0].c, $[2].c]\", expected: but was: <[{\"x\":2},{\"x\":2}]>."); } @Test @@ -887,36 +887,36 @@ protected void shouldAssertInteger() { @Test void shouldAllowNodeInJsonMapAssert() { assertThatThrownBy(() -> assertThatJson("{\"data\":{\"id\": \"1234\", \"relationships\": false}}") - .inPath("$.data") - .isObject() - .containsEntry("id", "1234") - .node("relationships") - .isObject()) - .hasMessage("Node \"$.data.relationships\" has invalid type, expected: but was: ."); + .inPath("$.data") + .isObject() + .containsEntry("id", "1234") + .node("relationships") + .isObject()) + .hasMessage("Node \"$.data.relationships\" has invalid type, expected: but was: ."); } @Test protected void shouldAssertIntegerFailure() { assertThatThrownBy(() -> assertThatJson("{\"a\":1.0}").node("a").isIntegralNumber()) - .hasMessage("Node \"a\" has invalid type, expected: but was: <1.0>."); + .hasMessage("Node \"a\" has invalid type, expected: but was: <1.0>."); assertThatThrownBy(() -> assertThatJson("{\"a\":0.0}").node("a").isIntegralNumber()) - .hasMessage("Node \"a\" has invalid type, expected: but was: <0.0>."); + .hasMessage("Node \"a\" has invalid type, expected: but was: <0.0>."); assertThatThrownBy(() -> assertThatJson("{\"a\":10.0}").node("a").isIntegralNumber()) - .hasMessage("Node \"a\" has invalid type, expected: but was: <10.0>."); + .hasMessage("Node \"a\" has invalid type, expected: but was: <10.0>."); assertThatThrownBy(() -> assertThatJson("{\"a\":-10.0}").node("a").isIntegralNumber()) - .hasMessage("Node \"a\" has invalid type, expected: but was: <-10.0>."); + .hasMessage("Node \"a\" has invalid type, expected: but was: <-10.0>."); assertThatThrownBy(() -> assertThatJson("{\"a\":1.1}").node("a").isIntegralNumber()) - .hasMessage("Node \"a\" has invalid type, expected: but was: <1.1>."); + .hasMessage("Node \"a\" has invalid type, expected: but was: <1.1>."); assertThatThrownBy(() -> assertThatJson("{\"a\":1e3}").node("a").isIntegralNumber()) - .hasMessageStartingWith("Node \"a\" has invalid type, expected: but was:"); + .hasMessageStartingWith("Node \"a\" has invalid type, expected: but was:"); assertThatThrownBy(() -> assertThatJson("{\"a\":1e-3}").node("a").isIntegralNumber()) - .hasMessage("Node \"a\" has invalid type, expected: but was: <0.001>."); + .hasMessage("Node \"a\" has invalid type, expected: but was: <0.001>."); } @Test @@ -930,21 +930,21 @@ protected void shouldUseCustomNumberComparator() { } }; assertThatJson("{\"a\":1.0}") - .withConfiguration(c -> c.withNumberComparator(numberComparator)) - .isEqualTo("{\"a\":1.00}"); + .withConfiguration(c -> c.withNumberComparator(numberComparator)) + .isEqualTo("{\"a\":1.00}"); assertThatThrownBy(() -> assertThatJson("{\"a\":1.0}") - .withConfiguration(c -> c.withNumberComparator(numberComparator)) - .isEqualTo("{\"a\":1}")) - .hasMessage( - "JSON documents are different:\nDifferent value found in node \"a\", expected: <1> but was: <1.0>.\n"); + .withConfiguration(c -> c.withNumberComparator(numberComparator)) + .isEqualTo("{\"a\":1}")) + .hasMessage( + "JSON documents are different:\nDifferent value found in node \"a\", expected: <1> but was: <1.0>.\n"); } @Test @Disabled protected void shouldAssert1e0() { assertThatThrownBy(() -> assertThatJson("{\"a\":1e0}").node("a").isIntegralNumber()) - .hasMessageStartingWith("Node \"a\" has invalid type, expected: but was:"); + .hasMessageStartingWith("Node \"a\" has invalid type, expected: but was:"); } @Test @@ -955,30 +955,30 @@ protected void shouldFailOnTrainingToken() { @Test void arrayExtractingShouldPass() { assertThatJson( - """ + """ [ {"id": 1, "name":{"first":"Aaron"}}, {"id": 2, "name":{"first":"Paul"}} ]""") - .isArray() - .extracting("id", "name") - .contains(tuple(valueOf(1), "{\"first\":\"Aaron\"}"), tuple(valueOf(2), "{\"first\":\"Paul\"}")); + .isArray() + .extracting("id", "name") + .contains(tuple(valueOf(1), "{\"first\":\"Aaron\"}"), tuple(valueOf(2), "{\"first\":\"Paul\"}")); } @Test void arrayExtractingShouldFail() { assertThatThrownBy(() -> assertThatJson( - """ + """ [ {"id": 1, "name":{"first":"Aaron"}}, {"id": 2, "name":{"first":"John"}} ]""") - .isArray() - .extracting("id", "name") - .contains( - tuple(valueOf(1), "{\"first\":\"Aaron\"}"), tuple(valueOf(2), "{\"first\":\"Paul\"}"))) - .hasMessage( - """ + .isArray() + .extracting("id", "name") + .contains( + tuple(valueOf(1), "{\"first\":\"Aaron\"}"), tuple(valueOf(2), "{\"first\":\"Paul\"}"))) + .hasMessage( + """ [Node ""]\s Expecting ArrayList: [(1, {"first":"Aaron"}), (2, {"first":"John"})] @@ -992,18 +992,18 @@ but could not find the following element(s): @Test void arrayExtractingShouldFailOnDifferentLengthTuple() { assertThatThrownBy(() -> assertThatJson( - """ + """ [ {"id": 1, "name":{"first":"Aaron"}}, {"id": 2, "name":{"first":"John"}} ]""") - .isArray() - .extracting("id", "name") - .contains( - tuple(valueOf(1), "{\"first\":\"Aaron\"}", 3), - tuple(valueOf(2), "{\"first\":\"John\"}"))) - .hasMessage( - """ + .isArray() + .extracting("id", "name") + .contains( + tuple(valueOf(1), "{\"first\":\"Aaron\"}", 3), + tuple(valueOf(2), "{\"first\":\"John\"}"))) + .hasMessage( + """ [Node ""]\s Expecting ArrayList: [(1, {"first":"Aaron"}), (2, {"first":"John"})] @@ -1018,10 +1018,10 @@ but could not find the following element(s): void shouldNotParseTwice() { String result = "{\"bundles\":[\"http://localhost:33621/rms/framework/bundle/0\"]}"; assertThatJson(result).and(JsonAssert::isObject, j -> j.node("bundles") - .isArray() - .element(0) - .asString() - .contains("http://", "/framework/bundle/0")); + .isArray() + .element(0) + .asString() + .contains("http://", "/framework/bundle/0")); } @Test @@ -1029,12 +1029,12 @@ void elementWithTypeAssertShouldWork() { String result = "{\"bundles\":[\"http://localhost:33621/rms/framework/bundle/0\"]}"; // FIXME: Better path in the message assertThatThrownBy(() -> assertThatJson(result) - .node("bundles") - .isArray() - .element(0) - .isNumber()) - .hasMessage( - "Node \"bundles\" element at index 0 has invalid type, expected: but was: <\"http://localhost:33621/rms/framework/bundle/0\">."); + .node("bundles") + .isArray() + .element(0) + .isNumber()) + .hasMessage( + "Node \"bundles\" element at index 0 has invalid type, expected: but was: <\"http://localhost:33621/rms/framework/bundle/0\">."); } @Test @@ -1045,23 +1045,23 @@ void shouldAssertStringNumber() { @Test void shouldAssertStringNumberFailure() { assertThatThrownBy(() -> - assertThatJson("{\"a\":\"x\"}").node("a").asNumber().isEqualByComparingTo("1")) - .hasMessage("Node \"a\" can not be converted to number expected: but was: <\"x\">."); + assertThatJson("{\"a\":\"x\"}").node("a").asNumber().isEqualByComparingTo("1")) + .hasMessage("Node \"a\" can not be converted to number expected: but was: <\"x\">."); } @Test protected void shouldDiffCloseNumbers() { assertThatThrownBy( - () -> assertThatJson("{\"result\":{\"a\": 0.299999999999999988897769753748434595763683319091796875}}") - .isEqualTo("{result: {a: 0.3}}")); + () -> assertThatJson("{\"result\":{\"a\": 0.299999999999999988897769753748434595763683319091796875}}") + .isEqualTo("{result: {a: 0.3}}")); } // https://github.com/assertj/assertj-core/issues/2111 @Test void containsValue() { assertThatThrownBy(() -> assertThatJson("{\"a\": 1}").isObject().containsKey("lastModified2")) - .hasMessage( - """ + .hasMessage( + """ [Different value found in node ""]\s Expecting actual: {"a":1} @@ -1069,8 +1069,8 @@ void containsValue() { "lastModified2\""""); assertThatThrownBy(() -> assertThatJson("{\"a\": 1}").isObject().contains(entry("lastModified2", null))) - .hasMessage( - """ + .hasMessage( + """ [Different value found in node ""]\s Expecting map: {"a":1} @@ -1084,9 +1084,9 @@ void containsValue() { @Test void shouldAssertNumberFailure() { assertThatThrownBy( - () -> assertThatJson("{\"a\":1}").node("a").isNumber().isEqualByComparingTo("2")) - .hasMessage( - """ + () -> assertThatJson("{\"a\":1}").node("a").isNumber().isEqualByComparingTo("2")) + .hasMessage( + """ [Different value found in node "a"]\s expected: 2 but was: 1"""); @@ -1100,22 +1100,22 @@ void testAssertToleranceSimple() { @Test void testAssertTolerance() { assertThatJson("{\"test\":1.00001}") - .withConfiguration(c -> c.withTolerance(0.001)) - .isEqualTo("{\"test\":1}"); + .withConfiguration(c -> c.withTolerance(0.001)) + .isEqualTo("{\"test\":1}"); } @Test void shouldFailCorrectlyOnMissingNode() { assertThatThrownBy(() -> assertThatJson("[{\"a\":\"01\", \"b\":\"text\"}]") - .node("[0].a") - .isEqualTo("\"01\"") - .node("[0].b") - .isEqualTo("text")) - .hasMessage("JSON documents are different:\nMissing node in path \"[0].a[0].b\".\n") - .matches(e -> ((AssertionFailedError) e) - .getActual() - .getStringRepresentation() - .equals("")); + .node("[0].a") + .isEqualTo("\"01\"") + .node("[0].b") + .isEqualTo("text")) + .hasMessage("JSON documents are different:\nMissing node in path \"[0].a[0].b\".\n") + .matches(e -> ((AssertionFailedError) e) + .getActual() + .getStringRepresentation() + .equals("")); } @Test @@ -1126,40 +1126,40 @@ public void shouldAllowUnquotedKeysAndCommentInExpectedValue() { @Test void testAssertNode() { assertThatThrownBy(() -> assertThatJson(readValue("{\"test\":1}")).isEqualTo(readValue("{\"test\":2}"))) - .hasMessage( - "JSON documents are different:\nDifferent value found in node \"test\", expected: <2> but was: <1>.\n"); + .hasMessage( + "JSON documents are different:\nDifferent value found in node \"test\", expected: <2> but was: <1>.\n"); } @Test void testAssertNodeInExpectOnly() { assertThatThrownBy(() -> assertThatJson("{\"test\":1}").isEqualTo(readValue("{\"test\":2}"))) - .hasMessage( - "JSON documents are different:\nDifferent value found in node \"test\", expected: <2> but was: <1>.\n"); + .hasMessage( + "JSON documents are different:\nDifferent value found in node \"test\", expected: <2> but was: <1>.\n"); } @Test void testAssertPathWithDescription() { assertThatThrownBy(() -> assertThatJson(jsonSource("{\"test\":1}", "$")) - .node("test") - .isEqualTo("2")) - .hasMessage( - "JSON documents are different:\nDifferent value found in node \"$.test\", expected: <2> but was: <1>.\n"); + .node("test") + .isEqualTo("2")) + .hasMessage( + "JSON documents are different:\nDifferent value found in node \"$.test\", expected: <2> but was: <1>.\n"); } @Test void testPresentWithDescription() { assertThatThrownBy(() -> assertThatJson(jsonSource("{\"test\":1}", "$")) - .node("test2") - .isPresent()) - .hasMessage( - "Different value found in node \"$.test2\", expected: but was: ."); + .node("test2") + .isPresent()) + .hasMessage( + "Different value found in node \"$.test2\", expected: but was: ."); } @Test protected void testNotEqualTo() { assertThatThrownBy(() -> assertThatJson("{\"test\":1}").isNotEqualTo("{\"test\": \"${json-unit.any-number}\"}")) - .hasMessage( - """ + .hasMessage( + """ Expecting actual: {"test":1} @@ -1171,42 +1171,42 @@ protected void testNotEqualTo() { @Test void testAssertPathArray() { assertThatThrownBy(() -> assertThatJson("{\"root\":{\"test\":[1,2,3]}}") - .node("root.test[0]") - .isEqualTo(2)) - .hasMessage( - "JSON documents are different:\nDifferent value found in node \"root.test[0]\", expected: <2> but was: <1>.\n"); + .node("root.test[0]") + .isEqualTo(2)) + .hasMessage( + "JSON documents are different:\nDifferent value found in node \"root.test[0]\", expected: <2> but was: <1>.\n"); } @Test void testLongRegexp() { assertThatJson("{\"test\": \"This is some text followed by: ABCD, followed by this\"}") - .isEqualTo( - "{\"test\": \"${json-unit.regex}^\\\\QThis is some text followed by: \\\\E[A-Z]+\\\\Q, followed by this\\\\E$\"}"); + .isEqualTo( + "{\"test\": \"${json-unit.regex}^\\\\QThis is some text followed by: \\\\E[A-Z]+\\\\Q, followed by this\\\\E$\"}"); } @Test void testLongRegexpBuilder() { assertThatJson("{\"test\": \"This is some text followed by: ABCD, followed by this\"}") - .isEqualTo("{\"test\": " - + regex().str("This is some text followed by: ") - .exp("[A-Z]+") - .str(", followed by this") + "}"); + .isEqualTo("{\"test\": " + + regex().str("This is some text followed by: ") + .exp("[A-Z]+") + .str(", followed by this") + "}"); } @Test public void verifyIsArrayContainsString() { assertThatJson("{\"id\":\"1\", \"children\":[{\"parentId\":\"1\"}]}") - .inPath("children[*].parentId") - .isArray() - .containsOnly(value("1")); + .inPath("children[*].parentId") + .isArray() + .containsOnly(value("1")); } @Test void testNodeAbsent() { assertThatThrownBy(() -> assertThatJson("{\"test1\":2, \"test2\":1}") - .node("test2") - .isAbsent()) - .hasMessage("Different value found in node \"test2\", expected: but was: <1>."); + .node("test2") + .isAbsent()) + .hasMessage("Different value found in node \"test2\", expected: but was: <1>."); } @Test @@ -1217,25 +1217,25 @@ void testNodeAbsentOk() { @Test void shouldTreatNullAsAbsent() { assertThatJson("{\"a\":1, \"b\": null}") - .when(Option.TREATING_NULL_AS_ABSENT) - .node("b") - .isAbsent(); + .when(Option.TREATING_NULL_AS_ABSENT) + .node("b") + .isAbsent(); } @Test void shouldNotTreatNullAsAbsent() { assertThatThrownBy( - () -> assertThatJson("{\"a\":1, \"b\": null}").node("b").isAbsent()) - .hasMessage("Different value found in node \"b\", expected: but was: ."); + () -> assertThatJson("{\"a\":1, \"b\": null}").node("b").isAbsent()) + .hasMessage("Different value found in node \"b\", expected: but was: ."); } @Test void testNodePresent() { assertThatThrownBy(() -> assertThatJson("{\"test1\":2, \"test2\":1}") - .node("test3") - .isPresent()) - .hasMessage( - "Different value found in node \"test3\", expected: but was: ."); + .node("test3") + .isPresent()) + .hasMessage( + "Different value found in node \"test3\", expected: but was: ."); } @Test @@ -1246,8 +1246,8 @@ void testNodePresentOk() { @Test void testAnd() { assertThatJson( - "{\"test1\":2, \"test2\":1}", json -> json.inPath("test1").isEqualTo(2), json -> json.inPath("test2") - .isEqualTo(1)); + "{\"test1\":2, \"test2\":1}", json -> json.inPath("test1").isEqualTo(2), json -> json.inPath("test2") + .isEqualTo(1)); } @Test @@ -1258,41 +1258,62 @@ void testNodePresentNull() { @Test void isPresentShouldTreatNullAsAbsentWhenSpecified() { assertThatThrownBy(() -> assertThatJson("{\"test1\":2, \"test2\":null}") - .when(Option.TREATING_NULL_AS_ABSENT) - .node("test2") - .isPresent()) - .hasMessage( - "Different value found in node \"test2\", expected: but was: ."); + .when(Option.TREATING_NULL_AS_ABSENT) + .node("test2") + .isPresent()) + .hasMessage( + "Different value found in node \"test2\", expected: but was: ."); } @Test void testMessage() { assertThatThrownBy(() -> - assertThatJson("{\"test\":1}").as("Test is different").isEqualTo("{\"test\":2}")) - .hasMessage( - "[Test is different] JSON documents are different:\nDifferent value found in node \"test\", expected: <2> but was: <1>.\n"); + assertThatJson("{\"test\":1}").as("Test is different").isEqualTo("{\"test\":2}")) + .hasMessage( + "[Test is different] JSON documents are different:\nDifferent value found in node \"test\", expected: <2> but was: <1>.\n"); } @Test void testIgnoreDifferent() { assertThatJson("{\"test\":1}") - .withConfiguration(c -> c.withIgnorePlaceholder("##IGNORE##")) - .isEqualTo("{\"test\":\"##IGNORE##\"}"); + .withConfiguration(c -> c.withIgnorePlaceholder("##IGNORE##")) + .isEqualTo("{\"test\":\"##IGNORE##\"}"); } @Test void anyNumberShouldFailOnNull() { assertThatThrownBy(() -> assertThatJson("{\"test\":null}").isEqualTo("{\"test\":\"${json-unit.any-number}\"}")) - .hasMessage( - "JSON documents are different:\nDifferent value found in node \"test\", expected: but was: .\n"); + .hasMessage( + "JSON documents are different:\nDifferent value found in node \"test\", expected: but was: .\n"); } @Test void anyNumberShouldFailOnObject() { assertThatThrownBy(() -> - assertThatJson("{\"test\":{\"a\":1}}").isEqualTo("{\"test\":\"${json-unit.any-number}\"}")) - .hasMessage( - "JSON documents are different:\nDifferent value found in node \"test\", expected: but was: <{\"a\":1}>.\n"); + assertThatJson("{\"test\":{\"a\":1}}").isEqualTo("{\"test\":\"${json-unit.any-number}\"}")) + .hasMessage( + "JSON documents are different:\nDifferent value found in node \"test\", expected: but was: <{\"a\":1}>.\n"); + } + + @Test + void anyArrayShouldAcceptArray() { + assertThatJson("{\"test\":[1, 2]}").isEqualTo("{\"test\":\"${json-unit.any-array}\"}"); + } + + @Test + void anyArrayShouldFailOnObject() { + assertThatThrownBy(() -> + assertThatJson("{\"test\":{\"a\":1}}").isEqualTo("{\"test\":\"${json-unit.any-array}\"}")) + .hasMessage( + "JSON documents are different:\nDifferent value found in node \"test\", expected: but was: <{\"a\":1}>.\n"); + } + + @Test + void anyArrayShouldFailOnNull() { + assertThatThrownBy(() -> + assertThatJson("{\"test\": null}").isEqualTo("{\"test\":\"${json-unit.any-array}\"}")) + .hasMessage( + "JSON documents are different:\nDifferent value found in node \"test\", expected: but was: .\n"); } @Test @@ -1314,15 +1335,15 @@ void emptinessCheck2() { void ifMatcherDoesNotMatchReportDifference() { RecordingDifferenceListener listener = new RecordingDifferenceListener(); assertThatThrownBy(() -> assertThatJson("{\"test\":-1}") - .withMatcher("positive", greaterThan(valueOf(0))) - .withDifferenceListener(listener) - .isEqualTo("{\"test\": \"${json-unit.matches:positive}\"}")) - .hasMessage( - "JSON documents are different:\nMatcher \"positive\" does not match value -1 in node \"test\". Expected a value greater than <0> but <-1> was less than <0>\n"); + .withMatcher("positive", greaterThan(valueOf(0))) + .withDifferenceListener(listener) + .isEqualTo("{\"test\": \"${json-unit.matches:positive}\"}")) + .hasMessage( + "JSON documents are different:\nMatcher \"positive\" does not match value -1 in node \"test\". Expected a value greater than <0> but <-1> was less than <0>\n"); assertThat(listener.getDifferenceList()).hasSize(1); assertThat(listener.getDifferenceList().get(0).toString()) - .isEqualTo("DIFFERENT Expected ${json-unit.matches:positive} in test got -1 in test"); + .isEqualTo("DIFFERENT Expected ${json-unit.matches:positive} in test got -1 in test"); } @Test @@ -1330,44 +1351,44 @@ void parametrizedMatcherShouldFail() { Matcher divisionMatcher = new DivisionMatcher(); try { assertThatJson("{\"test\":5}") - .withMatcher("isDivisibleBy", divisionMatcher) - .isEqualTo("{\"test\": \"${json-unit.matches:isDivisibleBy}3\"}"); + .withMatcher("isDivisibleBy", divisionMatcher) + .isEqualTo("{\"test\": \"${json-unit.matches:isDivisibleBy}3\"}"); } catch (AssertionError e) { assertEquals( - "JSON documents are different:\nMatcher \"isDivisibleBy\" does not match value 5 in node \"test\". Expected value divisible by <3> but was <5>\n", - e.getMessage()); + "JSON documents are different:\nMatcher \"isDivisibleBy\" does not match value 5 in node \"test\". Expected value divisible by <3> but was <5>\n", + e.getMessage()); } } @Test void shoulEscapeDot() { assertThatJson("{\"name.with.dot\": \"value\"}") - .node("name\\.with\\.dot") - .isEqualTo("value"); + .node("name\\.with\\.dot") + .isEqualTo("value"); } @Test void pathShouldBeIgnoredForExtraKey() { assertThatJson("{\"root\":{\"test\":1, \"ignored\": 1}}") - .withConfiguration(c -> c.whenIgnoringPaths("root.ignored")) - .isEqualTo("{\"root\":{\"test\":1}}"); + .withConfiguration(c -> c.whenIgnoringPaths("root.ignored")) + .isEqualTo("{\"root\":{\"test\":1}}"); } @Test void withFailMessageShouldWork() { assertThatThrownBy(() -> assertThatJson("{\"a\":{\"b\": 1}}") - .withFailMessage("It's broken") - .isEqualTo("{\"b\": 2}")) - .hasMessage("It's broken"); + .withFailMessage("It's broken") + .isEqualTo("{\"b\": 2}")) + .hasMessage("It's broken"); } @Test void describedAsShouldWork() { assertThatThrownBy(() -> assertThatJson("{\"a\":{\"b\": 1}}") - .describedAs("It's broken") - .isEqualTo("{\"b\": 2}")) - .hasMessage( - """ + .describedAs("It's broken") + .isEqualTo("{\"b\": 2}")) + .hasMessage( + """ [It's broken] JSON documents are different: Different keys found in node "", missing: "b", extra: "a", expected: <{"b":2}> but was: <{"a":{"b":1}}> """); @@ -1376,15 +1397,15 @@ void describedAsShouldWork() { @Test void pathShouldBeIgnoredForDifferentValue() { assertThatJson("{\"root\":{\"test\":1, \"ignored\": 1}}") - .whenIgnoringPaths("root.ignored") - .isEqualTo("{\"root\":{\"test\":1, \"ignored\": 2}}"); + .whenIgnoringPaths("root.ignored") + .isEqualTo("{\"root\":{\"test\":1, \"ignored\": 2}}"); } @Test void pathShouldBeIgnoredForArrayExample() { assertThatJson("[{\"a\":1, \"b\":2},{\"a\":1, \"b\":3}]") - .whenIgnoringPaths("[*].b") - .isEqualTo("[{\"a\":1, \"b\":0},{\"a\":1, \"b\":0}]"); + .whenIgnoringPaths("[*].b") + .isEqualTo("[{\"a\":1, \"b\":0},{\"a\":1, \"b\":0}]"); } private static class TestBean { @@ -1408,26 +1429,26 @@ protected void shouldEqualNumberInObject() { @Test void testEqualsToArray() { - assertThatJson("{\"test\":[1,2,3]}").node("test").isEqualTo(new int[] {1, 2, 3}); + assertThatJson("{\"test\":[1,2,3]}").node("test").isEqualTo(new int[]{1, 2, 3}); } @Test void assertContainsEntryNumber() { assertThatJson("{\"a\":1, \"b\":2.0}") - .withTolerance(0) - .isObject() - .containsEntry("a", 1) - .containsEntry("b", 2); + .withTolerance(0) + .isObject() + .containsEntry("a", 1) + .containsEntry("b", 2); } @Test protected void assertContainsEntryNumberFailure() { assertThatThrownBy(() -> assertThatJson("{\"a\":1, \"b\":2.0}") - .isObject() - .containsEntry("a", 1) - .containsEntry("b", 2)) - .hasMessage( - """ + .isObject() + .containsEntry("a", 1) + .containsEntry("b", 2)) + .hasMessage( + """ [Different value found in node ""]\s Expecting map: {"a":1,"b":2.0} @@ -1446,47 +1467,47 @@ void testEqualsToList() { @Test void testEqualsToObjectList() { assertThatJson("{\"test\":[{\"a\":1}, {\"b\":2}]}") - .node("test") - .isEqualTo(asList(readValue("{\"a\":1}"), readValue("{\"b\":2}"))); + .node("test") + .isEqualTo(asList(readValue("{\"a\":1}"), readValue("{\"b\":2}"))); } @Test void testEqualsToDoubleArray() { - assertThatJson("{\"test\":[1.0,2.0,3.0]}").node("test").isEqualTo(new double[] {1, 2, 3}); + assertThatJson("{\"test\":[1.0,2.0,3.0]}").node("test").isEqualTo(new double[]{1, 2, 3}); } @Test void testEqualsToBooleanArray() { - assertThatJson("{\"test\":[true, false]}").node("test").isEqualTo(new boolean[] {true, false}); + assertThatJson("{\"test\":[true, false]}").node("test").isEqualTo(new boolean[]{true, false}); } @Test void testEqualsToObjectArray() { assertThatJson("{\"test\":[{\"a\":1}, {\"b\":2}]}") - .node("test") - .isEqualTo(new Object[] {readValue("{\"a\":1}"), readValue("{\"b\":2}")}); + .node("test") + .isEqualTo(new Object[]{readValue("{\"a\":1}"), readValue("{\"b\":2}")}); } @Test void isArrayShouldFailIfArrayDoesNotExist() { assertThatThrownBy(() -> assertThatJson("{\"test\":1}").node("test2").isArray()) - .hasMessage("Different value found in node \"test2\", expected: but was: ."); + .hasMessage("Different value found in node \"test2\", expected: but was: ."); } @Test void isArrayShouldFailIfItIsNotArray() { assertThatThrownBy(() -> assertThatJson("{\"test\":\"1\"}").node("test").isArray()) - .hasMessage("Node \"test\" has invalid type, expected: but was: <\"1\">."); + .hasMessage("Node \"test\" has invalid type, expected: but was: <\"1\">."); } @Test void arrayOfLengthShouldFailOnIncorrectSize() { assertThatThrownBy(() -> assertThatJson("{\"test\":[1,2,3]}") - .node("test") - .isArray() - .hasSize(2)) - .hasMessage( - """ + .node("test") + .isArray() + .hasSize(2)) + .hasMessage( + """ [Node "test"]\s Expected size: 2 but was: 3 in: [1, 2, 3]"""); @@ -1495,9 +1516,9 @@ void arrayOfLengthShouldFailOnIncorrectSize() { @Test void shouldReportExtraArrayItemsWhenNotIgnoringOrder() { assertThatThrownBy( - () -> assertThatJson("{\"test\":[1,2,3]}").node("test").isEqualTo("[1]")) - .hasMessage( - """ + () -> assertThatJson("{\"test\":[1,2,3]}").node("test").isEqualTo("[1]")) + .hasMessage( + """ JSON documents are different: Array "test" has different length, expected: <1> but was: <3>. Array "test" has different content. Extra values: [2, 3], expected: <[1]> but was: <[1,2,3]> @@ -1507,11 +1528,11 @@ void shouldReportExtraArrayItemsWhenNotIgnoringOrder() { @Test void shouldReportExtraArrayItemsWhenIgnoringOrder() { assertThatThrownBy(() -> assertThatJson("{\"test\":[1,2,3]}") - .when(IGNORING_ARRAY_ORDER) - .node("test") - .isEqualTo("[1]")) - .hasMessage( - """ + .when(IGNORING_ARRAY_ORDER) + .node("test") + .isEqualTo("[1]")) + .hasMessage( + """ JSON documents are different: Array "test" has different length, expected: <1> but was: <3>. Array "test" has different content. Missing values: [], extra values: [2, 3], expected: <[1]> but was: <[1,2,3]> @@ -1521,8 +1542,8 @@ void shouldReportExtraArrayItemsWhenIgnoringOrder() { @Test void shouldReportMissingArrayItemsWhenNotIgnoringOrder() { assertThatThrownBy(() -> assertThatJson("{\"test\":[1]}").node("test").isEqualTo("[1, 2, 3]")) - .hasMessage( - """ + .hasMessage( + """ JSON documents are different: Array "test" has different length, expected: <3> but was: <1>. Array "test" has different content. Missing values: [2, 3], expected: <[1,2,3]> but was: <[1]> @@ -1532,11 +1553,11 @@ void shouldReportMissingArrayItemsWhenNotIgnoringOrder() { @Test void shouldReportMissingArrayItemsWhenIgnoringOrder() { assertThatThrownBy(() -> assertThatJson("{\"test\":[1]}") - .when(IGNORING_ARRAY_ORDER) - .node("test") - .isEqualTo("[1, 2, 3]")) - .hasMessage( - """ + .when(IGNORING_ARRAY_ORDER) + .node("test") + .isEqualTo("[1, 2, 3]")) + .hasMessage( + """ JSON documents are different: Array "test" has different length, expected: <3> but was: <1>. Array "test" has different content. Missing values: [2, 3], extra values: [], expected: <[1,2,3]> but was: <[1]> @@ -1546,10 +1567,10 @@ void shouldReportMissingArrayItemsWhenIgnoringOrder() { @Test void shouldReportExtraArrayItemsAndDifferencesWhenNotIgnoringOrder() { assertThatThrownBy(() -> assertThatJson("{\"test\":[\"x\",\"b\",\"c\"]}") - .node("test") - .isEqualTo("[\"a\"]")) - .hasMessage( - """ + .node("test") + .isEqualTo("[\"a\"]")) + .hasMessage( + """ JSON documents are different: Array "test" has different length, expected: <1> but was: <3>. Array "test" has different content. Extra values: ["b", "c"], expected: <["a"]> but was: <["x","b","c"]> @@ -1565,10 +1586,10 @@ void negativeArrayIndexShouldCountBackwards() { @Test void negativeArrayIndexShouldCountBackwardsAndReportFailure() { assertThatThrownBy(() -> assertThatJson("{\"root\":{\"test\":[1,2,3]}}") - .node("root.test[-3]") - .isEqualTo(3)) - .hasMessage( - """ + .node("root.test[-3]") + .isEqualTo(3)) + .hasMessage( + """ JSON documents are different: Different value found in node "root.test[-3]", expected: <3> but was: <1>. """); @@ -1577,10 +1598,10 @@ void negativeArrayIndexShouldCountBackwardsAndReportFailure() { @Test void negativeArrayIndexOutOfBounds() { assertThatThrownBy(() -> assertThatJson("{\"root\":{\"test\":[1,2,3]}}") - .node("root.test[-5]") - .isEqualTo(3)) - .hasMessage( - """ + .node("root.test[-5]") + .isEqualTo(3)) + .hasMessage( + """ JSON documents are different: Missing node in path "root.test[-5]". """); @@ -1589,10 +1610,10 @@ void negativeArrayIndexOutOfBounds() { @Test void positiveArrayIndexOutOfBounds() { assertThatThrownBy(() -> assertThatJson("{\"root\":{\"test\":[1,2,3]}}") - .node("root.test[5]") - .isEqualTo(3)) - .hasMessage( - """ + .node("root.test[5]") + .isEqualTo(3)) + .hasMessage( + """ JSON documents are different: Missing node in path "root.test[5]". """); @@ -1601,11 +1622,11 @@ void positiveArrayIndexOutOfBounds() { @Test void arrayThatContainsShouldFailOnMissingNode() { assertThatThrownBy(() -> assertThatJson("{\"test\":[{\"id\":36},{\"id\":37},{\"id\":38}]}") - .node("test") - .isArray() - .contains("{\"id\":42}")) - .hasMessage( - """ + .node("test") + .isArray() + .contains("{\"id\":42}")) + .hasMessage( + """ [Node "test"]\s Expecting JsonList: [{"id":36}, {"id":37}, {"id":38}] @@ -1619,9 +1640,9 @@ but could not find the following element(s): @Test void testContains() { assertThatJson("[\"foo\", \"bar\", null, false, 1]") - .isArray() - .hasSize(5) - .contains("foo", "bar", null, false, valueOf(1)); + .isArray() + .hasSize(5) + .contains("foo", "bar", null, false, valueOf(1)); } @Test @@ -1632,9 +1653,9 @@ void arrayContainsShouldMatch() { @Test void shouldNotParseValueTwice() { assertThatJson("{\"json\": \"{\\\"a\\\" : 1}\"}") - .node("json") - .isString() - .isEqualTo("{\"a\" : 1}"); + .node("json") + .isString() + .isEqualTo("{\"a\" : 1}"); } @Test @@ -1651,19 +1672,19 @@ void pathEscapingWorks() { assertThatJson(json).isObject().containsKey(pomPath); assertThatJson(json) - .node(pomPath.replace(".", "\\.")) - .isPresent() - .isObject() - .contains(entry("Status", "OK")); + .node(pomPath.replace(".", "\\.")) + .isPresent() + .isObject() + .contains(entry("Status", "OK")); } @Test void testArrayShouldMatchRegardlessOfOrder() { final String actual = - "{\"response\":[{\"attributes\":null,\"empolyees\":[{\"dob\":\"1987-03-21\",\"firstName\":\"Joe\",\"lastName\":\"Doe\"},{\"dob\":\"1986-02-12\",\"firstName\":\"Jason\",\"lastName\":\"Kowalski\"},{\"dob\":\"1985-01-11\",\"firstName\":\"Kate\",\"lastName\":\"Smith\"}],\"id\":123}]}"; + "{\"response\":[{\"attributes\":null,\"empolyees\":[{\"dob\":\"1987-03-21\",\"firstName\":\"Joe\",\"lastName\":\"Doe\"},{\"dob\":\"1986-02-12\",\"firstName\":\"Jason\",\"lastName\":\"Kowalski\"},{\"dob\":\"1985-01-11\",\"firstName\":\"Kate\",\"lastName\":\"Smith\"}],\"id\":123}]}"; final String expected = - "{\"response\":[{\"attributes\":null,\"empolyees\":[{\"dob\":\"1985-01-11\",\"firstName\":\"Kate\",\"lastName\":\"Smith\"},{\"dob\":\"1986-02-12\",\"firstName\":\"Jason\",\"lastName\":\"Kowalski\"},{\"dob\":\"1987-03-21\",\"firstName\":\"Joe\",\"lastName\":\"Doe\"}],\"id\":123}]}"; + "{\"response\":[{\"attributes\":null,\"empolyees\":[{\"dob\":\"1985-01-11\",\"firstName\":\"Kate\",\"lastName\":\"Smith\"},{\"dob\":\"1986-02-12\",\"firstName\":\"Jason\",\"lastName\":\"Kowalski\"},{\"dob\":\"1987-03-21\",\"firstName\":\"Joe\",\"lastName\":\"Doe\"}],\"id\":123}]}"; assertThatJson(actual).when(Option.IGNORING_ARRAY_ORDER).isEqualTo(expected); } @@ -1671,15 +1692,15 @@ void testArrayShouldMatchRegardlessOfOrder() { @Test void objectShouldMatch() { assertThatJson("{\"test\":[{\"value\":1},{\"value\":2},{\"value\":3}]}") - .node("test") - .isArray() - .allSatisfy(v -> assertThatJson(v).node("value").isNumber().isLessThan(valueOf(4))); + .node("test") + .isArray() + .allSatisfy(v -> assertThatJson(v).node("value").isNumber().isLessThan(valueOf(4))); } @Test void isStringShouldFailIfItIsNotAString() { assertThatThrownBy(() -> assertThatJson("{\"test\":1}").node("test").isString()) - .hasMessage("Node \"test\" has invalid type, expected: but was: <1>."); + .hasMessage("Node \"test\" has invalid type, expected: but was: <1>."); } @Test @@ -1695,31 +1716,31 @@ void testIssue3SpaceStrings() { @Test void testTreatNullAsAbsent() { assertThatJson("{\"test\":{\"a\":1, \"b\": null}}") - .when(TREATING_NULL_AS_ABSENT) - .isEqualTo("{\"test\":{\"a\":1}}"); + .when(TREATING_NULL_AS_ABSENT) + .isEqualTo("{\"test\":{\"a\":1}}"); } @Test void shouldIgnoreExtraFields() { assertThatJson("{\"test\":{\"a\":1, \"b\":2, \"c\":3}}") - .when(IGNORING_EXTRA_FIELDS) - .isEqualTo("{\"test\":{\"b\":2}}"); + .when(IGNORING_EXTRA_FIELDS) + .isEqualTo("{\"test\":{\"b\":2}}"); } @Test void shouldIgnoreExtraFieldsAndOrderExample() { assertThatJson("{\"test\":[{\"key\":3},{\"key\":2, \"extraField\":2},{\"key\":1}]}") - .when(IGNORING_EXTRA_FIELDS, IGNORING_ARRAY_ORDER) - .isEqualTo("{\"test\":[{\"key\":1},{\"key\":2},{\"key\":3}]}"); + .when(IGNORING_EXTRA_FIELDS, IGNORING_ARRAY_ORDER) + .isEqualTo("{\"test\":[{\"key\":1},{\"key\":2},{\"key\":3}]}"); } @Test void andFailure() { assertThatThrownBy(() -> assertThatJson("{\"test\":{\"a\":1, \"b\":2, \"c\":3}}") - .and(a -> a.node("test").isObject(), a -> a.node("test.b") - .isEqualTo(3))) - .hasMessage( - """ + .and(a -> a.node("test").isObject(), a -> a.node("test.b") + .isEqualTo(3))) + .hasMessage( + """ JSON documents are different: Different value found in node "test.b", expected: <3> but was: <2>. """); @@ -1728,16 +1749,16 @@ void andFailure() { @Test void andSuccess() { assertThatJson("{\"test\":{\"a\":1, \"b\":2, \"c\":3}}") - .and(a -> a.node("test").isObject(), a -> a.node("test.b").isEqualTo(2)); + .and(a -> a.node("test").isObject(), a -> a.node("test.b").isEqualTo(2)); } @Test void andNestedFailure() { assertThatThrownBy(() -> assertThatJson("{\"test\":{\"a\":1, \"b\":2, \"c\":3}}") - .node("test") - .and(a -> a.node("a").isEqualTo(1), a -> a.node("b").isEqualTo(3))) - .hasMessage( - """ + .node("test") + .and(a -> a.node("a").isEqualTo(1), a -> a.node("b").isEqualTo(3))) + .hasMessage( + """ JSON documents are different: Different value found in node "test.b", expected: <3> but was: <2>. """); @@ -1746,8 +1767,8 @@ void andNestedFailure() { @Test void andNestedSuccess() { assertThatJson("{\"test\":{\"a\":1, \"b\":2, \"c\":3}}") - .node("test") - .and(a -> a.node("a").isEqualTo(1), a -> a.node("b").isEqualTo(2)); + .node("test") + .and(a -> a.node("a").isEqualTo(1), a -> a.node("b").isEqualTo(2)); } @Test @@ -1763,10 +1784,10 @@ void shouldAcceptEscapedPathWithTwoDots() { @Test void shouldAcceptEscapedPathAndShowCorrectErrorMessage() { assertThatThrownBy(() -> assertThatJson("{\"foo.bar\":\"boo\"}") - .node("foo\\.bar") - .isEqualTo("baz")) - .hasMessage( - """ + .node("foo\\.bar") + .isEqualTo("baz")) + .hasMessage( + """ JSON documents are different: Different value found in node "foo\\.bar", expected: <"baz"> but was: <"boo">. """); @@ -1786,8 +1807,8 @@ void asStringShouldWork() { @Test void shouldWorkWithPercentSign() { assertThatThrownBy(() -> assertThatJson("{\"a\": \"1\"}").isEqualTo("{\"%\": \"2\"}")) - .hasMessage( - """ + .hasMessage( + """ JSON documents are different: Different keys found in node "", missing: "%", extra: "a", expected: <{"%":"2"}> but was: <{"a":"1"}> """); @@ -1796,8 +1817,8 @@ void shouldWorkWithPercentSign() { @Test void shouldIgnoreFields() { assertThatJson("{\"root\":{\"test\":1, \"ignored\": 1}}") - .isObject() - .isEqualToIgnoringGivenFields("{\"root\":{\"test\":1, \"ignored\": 2}}", "root.ignored"); + .isObject() + .isEqualToIgnoringGivenFields("{\"root\":{\"test\":1, \"ignored\": 2}}", "root.ignored"); } @Test @@ -1810,9 +1831,9 @@ void stringArray() { assertThatJson(""" ["abc"] """) - .isArray() - .first() - .isEqualTo(value("abc")); + .isArray() + .first() + .isEqualTo(value("abc")); } // ***************************************************************************************************************** @@ -1821,11 +1842,11 @@ void stringArray() { @Test void jsonPathShouldBeAbleToUseObjects() { assertThatThrownBy( - () -> assertThatJson(json) - .describedAs("My little assert") - .inPath("$.store.book[0]") - .isEqualTo( - """ + () -> assertThatJson(json) + .describedAs("My little assert") + .inPath("$.store.book[0]") + .isEqualTo( + """ { "category": "reference", "author": "Nigel Rees", @@ -1833,8 +1854,8 @@ void jsonPathShouldBeAbleToUseObjects() { "price": 8.96 }\ """)) - .hasMessage( - """ + .hasMessage( + """ JSON documents are different: Different value found in node "$.store.book[0].price", expected: <8.96> but was: <8.95>. """); @@ -1843,10 +1864,10 @@ void jsonPathShouldBeAbleToUseObjects() { @Test void jsonPathWithIgnoredPaths() { assertThatJson(json) - .withConfiguration(c -> c.whenIgnoringPaths("$.store.book[*].price")) - .inPath("$.store.book[0]") - .isEqualTo( - """ + .withConfiguration(c -> c.whenIgnoringPaths("$.store.book[*].price")) + .inPath("$.store.book[0]") + .isEqualTo( + """ { "category": "reference", "author": "Nigel Rees", @@ -1859,16 +1880,16 @@ void jsonPathWithIgnoredPaths() { @Test void ignoredJsonPaths() { assertThatJson( - """ + """ { "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 1111 }""") - .withConfiguration(c -> c.whenIgnoringPaths("$..price")) - .isEqualTo( - """ + .withConfiguration(c -> c.whenIgnoringPaths("$..price")) + .isEqualTo( + """ { "category": "reference", "author": "Nigel Rees", @@ -1881,23 +1902,23 @@ void ignoredJsonPaths() { @Test public void ignoredJsonPathComplex() { assertThatJson("{\"fields\":[" + "{\"key\":1, \"name\":\"AA\"}," - + "{\"key\":2, \"name\":\"AB\"}," - + "{\"key\":3, \"name\":\"AC\"}" - + "]}") - .whenIgnoringPaths("$.fields[?(@.name=='AA')].key") - .isEqualTo("{\"fields\":[" + "{\"key\":2, \"name\":\"AA\"}," - + "{\"key\":2, \"name\":\"AB\"}," - + "{\"key\":3, \"name\":\"AC\"}" - + "]}"); + + "{\"key\":2, \"name\":\"AB\"}," + + "{\"key\":3, \"name\":\"AC\"}" + + "]}") + .whenIgnoringPaths("$.fields[?(@.name=='AA')].key") + .isEqualTo("{\"fields\":[" + "{\"key\":2, \"name\":\"AA\"}," + + "{\"key\":2, \"name\":\"AB\"}," + + "{\"key\":3, \"name\":\"AC\"}" + + "]}"); } @Test void jsonPathWithIgnoredNonexistentPaths() { assertThatJson(json) - .withConfiguration(c -> c.whenIgnoringPaths("$.rubbish")) - .inPath("$.store.book[0]") - .isEqualTo( - """ + .withConfiguration(c -> c.whenIgnoringPaths("$.rubbish")) + .inPath("$.store.book[0]") + .isEqualTo( + """ { "category": "reference", "author": "Nigel Rees", @@ -1910,11 +1931,11 @@ void jsonPathWithIgnoredNonexistentPaths() { @Test void jsonPathWithIgnoredPathsDeep() { assertThatJson(json) - .whenIgnoringPaths("$..price") - .withTolerance(0.01) - .inPath("$.store.book[0]") - .isEqualTo( - """ + .whenIgnoringPaths("$..price") + .withTolerance(0.01) + .inPath("$.store.book[0]") + .isEqualTo( + """ { "category": "reference", "author": "Nigel Rees", @@ -1927,10 +1948,10 @@ void jsonPathWithIgnoredPathsDeep() { @Test void jsonPathWithIgnoredPathsDeepBracketNotation() { assertThatJson(json) - .whenIgnoringPaths("$..price") - .inPath("$['store']['book'][0]") - .isEqualTo( - """ + .whenIgnoringPaths("$..price") + .inPath("$['store']['book'][0]") + .isEqualTo( + """ { "category": "reference", "author": "Nigel Rees", @@ -1948,11 +1969,11 @@ void jsonPathWithNode() { @Test void jsonPathWithNodeError() { assertThatThrownBy(() -> assertThatJson(json) - .inPath("$.store.book[0]") - .node("title") - .isEqualTo("Sayings of the Century2")) - .hasMessage( - "JSON documents are different:\nDifferent value found in node \"$.store.book[0].title\", expected: <\"Sayings of the Century2\"> but was: <\"Sayings of the Century\">.\n"); + .inPath("$.store.book[0]") + .node("title") + .isEqualTo("Sayings of the Century2")) + .hasMessage( + "JSON documents are different:\nDifferent value found in node \"$.store.book[0].title\", expected: <\"Sayings of the Century2\"> but was: <\"Sayings of the Century\">.\n"); } @Test @@ -1963,9 +1984,9 @@ void jsonPathNumber() { @Test void booleanArrayExtractionShouldWork() { assertThatJson("{\"fields\": [{ \"id\": \"test\", \"value\": \"true\" }]}") - .inPath("$.fields[?(@.id==\"test\")].value") - .isArray() - .containsExactly(value("true")); + .inPath("$.fields[?(@.id==\"test\")].value") + .isArray() + .containsExactly(value("true")); } @Test @@ -1974,9 +1995,9 @@ void stringComparisonShouldWork() { map.put("FOO", 4); map.put("BAR", 3); assertThatJson("{\"fields\": [{ \"id\": \"test\", \"value\": \"{FOO=4, BAR=3}\" }]}") - .inPath("$.fields[?(@.id==\"test\")].value") - .isArray() - .containsExactly(value(map.toString())); + .inPath("$.fields[?(@.id==\"test\")].value") + .isArray() + .containsExactly(value(map.toString())); } @Test @@ -1987,8 +2008,8 @@ void testAbsentInJsonPath() { @Test void testAbsentInJsonPathEquals() { assertThatThrownBy(() -> assertThatJson("{}").inPath("$.abc").isEqualTo("value")) - .hasMessage( - """ + .hasMessage( + """ JSON documents are different: Missing node in path "$.abc". """); @@ -1997,24 +2018,24 @@ void testAbsentInJsonPathEquals() { @Test void testAbsentInJsonPathIsArray() { assertThatThrownBy(() -> assertThatJson("{}").inPath("$.abc").isArray()) - .hasMessage("Different value found in node \"$.abc\", expected: but was: ."); + .hasMessage("Different value found in node \"$.abc\", expected: but was: ."); } @Test void testAbsentInJsonPathNotAbsent() { assertThatThrownBy(() -> assertThatJson("{\"abc\": 1}").inPath("$.abc").isAbsent()) - .hasMessage("Different value found in node \"$.abc\", expected: but was: <1>."); + .hasMessage("Different value found in node \"$.abc\", expected: but was: <1>."); } @Test protected void jsonPathShouldBeAbleToUseArrays() { assertThatThrownBy( - () -> assertThatJson(json) - .inPath("$.store.book") - .isArray() - .contains( - json( - """ + () -> assertThatJson(json) + .inPath("$.store.book") + .isArray() + .contains( + json( + """ { "category": "reference", "author": "Nigel Rees", @@ -2022,8 +2043,8 @@ protected void jsonPathShouldBeAbleToUseArrays() { "price": 8.96 }\ """))) - .hasMessage( - """ + .hasMessage( + """ [Node "$.store.book"]\s Expecting JsonList: [{"author":"Nigel Rees","category":"reference","price":8.95,"title":"Sayings of the Century"}, @@ -2040,17 +2061,17 @@ but could not find the following element(s): @Test void jsonPathShouldBeAbleToUseArraysDeep() { assertThatJson(json) - .inPath("$.store.book[*].category") - .isArray() - .containsExactlyInAnyOrder("fiction", "reference", "fiction", "fiction"); + .inPath("$.store.book[*].category") + .isArray() + .containsExactlyInAnyOrder("fiction", "reference", "fiction", "fiction"); } @Test void jsonPathShouldBeAbleToUseArraysFromObject() { assertThatJson(readValue(json)) - .inPath("$.store.book[*].category") - .isArray() - .containsExactlyInAnyOrder("fiction", "reference", "fiction", "fiction"); + .inPath("$.store.book[*].category") + .isArray() + .containsExactlyInAnyOrder("fiction", "reference", "fiction", "fiction"); } @Test @@ -2061,42 +2082,42 @@ void ignoreExtraArrayItemsExample() { @Test void ignoreExtraArrayItemsAndOrderExample() { assertThatJson("{\"test\":[5,5,4,4,3,3,2,2,1,1]}") - .when(IGNORING_EXTRA_ARRAY_ITEMS, IGNORING_ARRAY_ORDER) - .isEqualTo("{\"test\":[1,2,3]}"); + .when(IGNORING_EXTRA_ARRAY_ITEMS, IGNORING_ARRAY_ORDER) + .isEqualTo("{\"test\":[1,2,3]}"); } @Test void testArrayBug() { assertThatJson( - """ + """ [ {"value": "1", "title": "Entity", "info": "Entity info"}, {"value": "2", "title": "Column", "info": "Column info"}, {"value": "3", "title": "Table", "info": "Table info"}, {"value": "4", "title": "Schema", "info": "Schema info"} ]""") - .inPath("$[?(@.value =='1')]") - .isArray() - .last() - .isEqualTo(json("{\"value\": \"1\", \"title\": \"Entity\", \"info\": \"Entity info\"}")); + .inPath("$[?(@.value =='1')]") + .isArray() + .last() + .isEqualTo(json("{\"value\": \"1\", \"title\": \"Entity\", \"info\": \"Entity info\"}")); } @Test void testArrayNode() { assertThatJson( - """ + """ [ {"value": "1", "title": "Entity", "info": "Entity info"}, {"value": "2", "title": "Column", "info": "Column info"}, {"value": "3", "title": "Table", "info": "Table info"}, {"value": "4", "title": "Schema", "info": "Schema info"} ]""") - .inPath("$[?(@.value =='1')]") - .isArray() - .first() - .node("title") - .isString() - .isEqualTo("Entity"); + .inPath("$[?(@.value =='1')]") + .isArray() + .first() + .node("title") + .isString() + .isEqualTo("Entity"); } @Test @@ -2110,10 +2131,10 @@ void testArrayElements() { String json = "[ [ \"a\", \"b\", \"c\", \"d\"," + " \"e\", \"f\", \"g\", \"h\", \"i\" ] ]"; assertThatJson(json) - .inPath("$.[0]") - .isArray() - .elements(3 /* d */, 4 /* e */, 5 /* f */, 6 /* g */, 7 /* h */, 8 /* i */) - .containsExactly("d", "e", "f", "g", "h", "i"); + .inPath("$.[0]") + .isArray() + .elements(3 /* d */, 4 /* e */, 5 /* f */, 6 /* g */, 7 /* h */, 8 /* i */) + .containsExactly("d", "e", "f", "g", "h", "i"); } @Test @@ -2144,48 +2165,48 @@ void shouldUseInPathInAnd() { @Test void shouldIgnoreArrayOrderInSpecificPath() { assertThatJson("{\"obj\":{\"a\": [1, 2], \"b\": [3, 4]}}") - .when(path("obj.a"), then(IGNORING_ARRAY_ORDER)) - .isEqualTo("{\"obj\":{\"a\": [2, 1], \"b\": [3, 4]}}"); + .when(path("obj.a"), then(IGNORING_ARRAY_ORDER)) + .isEqualTo("{\"obj\":{\"a\": [2, 1], \"b\": [3, 4]}}"); } @Test void shouldNotIgnoreArrayOrderWhenNotSpecified() { assertThatJson("{\"obj\":{\"a\": [1, 2], \"b\": [3, 4]}}") - .when(path("obj.a"), then(IGNORING_ARRAY_ORDER)) - .isNotEqualTo("{\"obj\":{\"a\": [2, 1], \"b\": [4, 3]}}"); + .when(path("obj.a"), then(IGNORING_ARRAY_ORDER)) + .isNotEqualTo("{\"obj\":{\"a\": [2, 1], \"b\": [4, 3]}}"); } @Test void shouldExcludeIgnoringArrayOrderFromPath() { assertThatJson("[{\"b\":[4,5,6]},{\"b\":[1,2,3]}]") - .when(IGNORING_ARRAY_ORDER) - .when(path("[*].b"), thenNot(IGNORING_ARRAY_ORDER)) - .isEqualTo("[{\"b\":[1,2,3]},{\"b\":[4,5,6]}]"); + .when(IGNORING_ARRAY_ORDER) + .when(path("[*].b"), thenNot(IGNORING_ARRAY_ORDER)) + .isEqualTo("[{\"b\":[1,2,3]},{\"b\":[4,5,6]}]"); } @Test void shouldIgnoreArrayOrderInSpecificJsonPath() { assertThatJson("{\"obj\":{\"a\": [1, 2], \"b\": [3, 4]}}") - .when(path("$..a"), then(IGNORING_ARRAY_ORDER)) - .isEqualTo("{\"obj\":{\"a\": [2, 1], \"b\": [3, 4]}}"); + .when(path("$..a"), then(IGNORING_ARRAY_ORDER)) + .isEqualTo("{\"obj\":{\"a\": [2, 1], \"b\": [3, 4]}}"); } @Test void shouldExcludeIgnoringArrayOrderFromPathAndIgnoreInRoot() { assertThatJson("[{\"b\":[4,5,6]},{\"b\":[1,2,3]},{\"b\":[7,8,9]}]") - .when(rootPath(), then(IGNORING_ARRAY_ORDER, IGNORING_EXTRA_ARRAY_ITEMS)) - .when(path("[*].b"), thenNot(IGNORING_ARRAY_ORDER)) - .isEqualTo("[{\"b\":[1,2,3]},{\"b\":[4,5,6]}]"); + .when(rootPath(), then(IGNORING_ARRAY_ORDER, IGNORING_EXTRA_ARRAY_ITEMS)) + .when(path("[*].b"), thenNot(IGNORING_ARRAY_ORDER)) + .isEqualTo("[{\"b\":[1,2,3]},{\"b\":[4,5,6]}]"); } @Test void shouldIgnoreArrayOrderEverywhereButTheFirstElement() { assertThatThrownBy(() -> assertThatJson("[{\"b\":[1,3,2]},{\"b\":[5,4,6]},{\"b\":[8,7,9]}]") - .when(path("[*].b"), then(IGNORING_ARRAY_ORDER)) - .when(path("[0].b"), thenNot(IGNORING_ARRAY_ORDER)) - .isEqualTo("[{\"b\":[1,2,3]},{\"b\":[4,5,6]},{\"b\":[7,8,9]}]")) - .hasMessage( - """ + .when(path("[*].b"), then(IGNORING_ARRAY_ORDER)) + .when(path("[0].b"), thenNot(IGNORING_ARRAY_ORDER)) + .isEqualTo("[{\"b\":[1,2,3]},{\"b\":[4,5,6]},{\"b\":[7,8,9]}]")) + .hasMessage( + """ JSON documents are different: Different value found in node "[0].b[1]", expected: <2> but was: <3>. Different value found in node "[0].b[2]", expected: <3> but was: <2>. @@ -2195,24 +2216,24 @@ void shouldIgnoreArrayOrderEverywhereButTheFirstElement() { @Test void shouldIgnoreArrayOrderInSeveralSpecificPaths() { assertThatJson("[{\"b\":[3,1,2],\"c\":[-2,-3,-1]},{\"b\":[6,5,4],\"c\":[-5,-4,-6]}]") - .when(paths("[*].b", "[*].c"), then(IGNORING_ARRAY_ORDER)) - .isEqualTo("[{\"b\":[1,2,3],\"c\":[-1,-2,-3]},{\"b\":[4,5,6],\"c\":[-4,-5,-6]}]"); + .when(paths("[*].b", "[*].c"), then(IGNORING_ARRAY_ORDER)) + .isEqualTo("[{\"b\":[1,2,3],\"c\":[-1,-2,-3]},{\"b\":[4,5,6],\"c\":[-4,-5,-6]}]"); } @Test void shouldTreatNullAsAbsentInSpecificPath() { assertThatJson("{\"a\":1,\"b\":null}") - .when(path("b"), then(TREATING_NULL_AS_ABSENT)) - .isEqualTo("{\"a\":1}"); + .when(path("b"), then(TREATING_NULL_AS_ABSENT)) + .isEqualTo("{\"a\":1}"); } @Test void shouldNotTreatNullAsAbsentWhenNotSpecified() { assertThatThrownBy(() -> assertThatJson("{\"a\":1,\"b\":null,\"c\":null}") - .when(path("b"), then(TREATING_NULL_AS_ABSENT)) - .isEqualTo("{\"a\":1}")) - .hasMessage( - """ + .when(path("b"), then(TREATING_NULL_AS_ABSENT)) + .isEqualTo("{\"a\":1}")) + .hasMessage( + """ JSON documents are different: Different keys found in node "", extra: "c", expected: <{"a":1}> but was: <{"a":1,"b":null,"c":null}> """); @@ -2221,17 +2242,17 @@ void shouldNotTreatNullAsAbsentWhenNotSpecified() { @Test void shouldIgnoreExtraFieldsInSpecificPath() { assertThatJson("{\"a\":{\"a1\":1,\"a2\":2}}") - .when(path("a"), then(IGNORING_EXTRA_FIELDS)) - .isEqualTo("{\"a\":{\"a1\":1}}"); + .when(path("a"), then(IGNORING_EXTRA_FIELDS)) + .isEqualTo("{\"a\":{\"a1\":1}}"); } @Test void shouldNotIgnoreExtraFieldsWhenNotSpecified() { assertThatThrownBy(() -> assertThatJson("{\"a\":{\"a1\":1,\"a2\":2},\"b\":{\"b1\":1,\"b2\":2}}") - .when(path("a"), then(IGNORING_EXTRA_FIELDS)) - .isEqualTo("{\"a\":{\"a1\":1},\"b\":{\"b1\":1}}")) - .hasMessage( - """ + .when(path("a"), then(IGNORING_EXTRA_FIELDS)) + .isEqualTo("{\"a\":{\"a1\":1},\"b\":{\"b1\":1}}")) + .hasMessage( + """ JSON documents are different: Different keys found in node "b", extra: "b.b2", expected: <{"b1":1}> but was: <{"b1":1,"b2":2}> """); @@ -2240,63 +2261,64 @@ void shouldNotIgnoreExtraFieldsWhenNotSpecified() { @Test void shouldIgnoreExtraArrayItemsInSpecificPath() { assertThatJson("{\"a\":[1,2,3]}") - .when(path("a"), then(IGNORING_EXTRA_ARRAY_ITEMS)) - .isEqualTo("{\"a\":[1,2]}"); + .when(path("a"), then(IGNORING_EXTRA_ARRAY_ITEMS)) + .isEqualTo("{\"a\":[1,2]}"); } @Test void shouldUseAsInstanceOfToMoveToJsonUnit() { - record DummyResponse(String trackingId, String json) {} + record DummyResponse(String trackingId, String json) { + } DummyResponse resp = new DummyResponse("abcd-0001", "{ \"foo\": \"bar\" }"); assertThat(resp) - .hasFieldOrPropertyWithValue("trackingId", "abcd-0001") // <- Assertj API - .extracting("json") - .asInstanceOf(JSON) // <- JsonUnit API - .isObject() - .containsEntry("foo", "bar"); + .hasFieldOrPropertyWithValue("trackingId", "abcd-0001") // <- Assertj API + .extracting("json") + .asInstanceOf(JSON) // <- JsonUnit API + .isObject() + .containsEntry("foo", "bar"); } @Test void shouldUseAsInstanceOfToMoveFromJsonUnit() { assertThatJson("{\"a\":[1, 2, 3]}") - .inPath("a") - .isArray() - .first(BIG_DECIMAL) - .isEqualTo("1"); + .inPath("a") + .isArray() + .first(BIG_DECIMAL) + .isEqualTo("1"); assertThatJson("{\"a\":[1, 2, true]}") - .inPath("a") - .isArray() - .last(BOOLEAN) - .isEqualTo(true); + .inPath("a") + .isArray() + .last(BOOLEAN) + .isEqualTo(true); assertThatJson("{\"a\":[1, \"s\", true]}") - .inPath("a") - .isArray() - .element(1, STRING) - .startsWith("s"); + .inPath("a") + .isArray() + .element(1, STRING) + .startsWith("s"); assertThatJson("{\"a\":{\"b\": \"c\"}}") - .inPath("a") - .isObject() - .extracting("b", STRING) - .endsWith("c"); + .inPath("a") + .isObject() + .extracting("b", STRING) + .endsWith("c"); assertThatJson("{\"a\":[1, 2, 3]}").inPath("a").asInstanceOf(LIST).hasSize(3); assertThatJson("{\"a\":[1, 2, 3]}") - .inPath("a") - .asInstanceOf(LIST) - .first(BIG_DECIMAL) - .isEqualTo("1"); + .inPath("a") + .asInstanceOf(LIST) + .first(BIG_DECIMAL) + .isEqualTo("1"); assertThatThrownBy(() -> assertThatJson("{\"a\": null}").inPath("a").asInstanceOf(BIG_DECIMAL)) - .isInstanceOf(AssertionError.class) - .hasMessage("\nExpecting actual not to be null"); + .isInstanceOf(AssertionError.class) + .hasMessage("\nExpecting actual not to be null"); } @Test void shouldNotIgnoreExtraArrayItemsWhenNotSpecified() { assertThatThrownBy(() -> assertThatJson("{\"a\":[1,2,3],\"b\":[1,2,3]}") - .when(path("a"), then(IGNORING_EXTRA_ARRAY_ITEMS)) - .isEqualTo("{\"a\":[1,2],\"b\":[1,2]}")) - .hasMessage( - """ + .when(path("a"), then(IGNORING_EXTRA_ARRAY_ITEMS)) + .isEqualTo("{\"a\":[1,2],\"b\":[1,2]}")) + .hasMessage( + """ JSON documents are different: Array "b" has different length, expected: <2> but was: <3>. Array "b" has different content. Extra values: [3], expected: <[1,2]> but was: <[1,2,3]> @@ -2306,32 +2328,32 @@ void shouldNotIgnoreExtraArrayItemsWhenNotSpecified() { @Test void shouldIgnoreValuesInSpecificPath() { assertThatJson("{\"a\":2,\"b\":\"string2\"}") - .when(paths("a", "b"), then(IGNORING_VALUES)) - .isEqualTo("{\"a\":1,\"b\":\"string\"}"); + .when(paths("a", "b"), then(IGNORING_VALUES)) + .isEqualTo("{\"a\":1,\"b\":\"string\"}"); } @Test void shouldIgnoreValuesInSpecificPathWrittenSeparately() { assertThatJson("{\"a\":2,\"b\":\"string2\"}") - .when(path("a"), then(IGNORING_VALUES)) - .when(path("b"), then(IGNORING_VALUES)) - .isEqualTo("{\"a\":1,\"b\":\"string\"}"); + .when(path("a"), then(IGNORING_VALUES)) + .when(path("b"), then(IGNORING_VALUES)) + .isEqualTo("{\"a\":1,\"b\":\"string\"}"); } @Test void shouldIgnoreAbsentB() { assertThatJson("{\"A\":1,\"B\":null}") - .when(path("B"), then(TREATING_NULL_AS_ABSENT)) - .isEqualTo("{\"A\":1}"); + .when(path("B"), then(TREATING_NULL_AS_ABSENT)) + .isEqualTo("{\"A\":1}"); } @Test void shouldNotIgnoreValuesWhenNotSpecified() { assertThatThrownBy(() -> assertThatJson("{\"a\":2,\"b\":\"string2\",\"c\":3}") - .when(paths("a", "b"), then(IGNORING_VALUES)) - .isEqualTo("{\"a\":1,\"b\":\"string\",\"c\":2}")) - .hasMessage( - """ + .when(paths("a", "b"), then(IGNORING_VALUES)) + .isEqualTo("{\"a\":1,\"b\":\"string\",\"c\":2}")) + .hasMessage( + """ JSON documents are different: Different value found in node "c", expected: <2> but was: <3>. """); @@ -2340,25 +2362,25 @@ void shouldNotIgnoreValuesWhenNotSpecified() { @Test void shouldIgnoreMultiplePaths() { assertThatJson("{\"a\":1,\"b\":2,\"c\":3}") - .when(paths("a", "b"), thenIgnore()) - .isEqualTo("{\"c\":3}"); + .when(paths("a", "b"), thenIgnore()) + .isEqualTo("{\"c\":3}"); } @Test void shouldIgnoreMultiplePathsWrittenSeparately() { assertThatJson("{\"a\":1,\"b\":2,\"c\":3}") - .when(path("a"), thenIgnore()) - .when(path("b"), thenIgnore()) - .isEqualTo("{\"c\":3}"); + .when(path("a"), thenIgnore()) + .when(path("b"), thenIgnore()) + .isEqualTo("{\"c\":3}"); } @Test void shouldFailFast() { assertThatThrownBy(() -> assertThatJson("{\"a\":{\"a1\": 1},\"b\":{\"b1\": 1}}") - .withOptions(Option.FAIL_FAST) - .isEqualTo("{\"a\":{\"a1\": 2},\"b\":{\"b1\": 2}}")) - .hasMessage( - """ + .withOptions(Option.FAIL_FAST) + .isEqualTo("{\"a\":{\"a1\": 2},\"b\":{\"b1\": 2}}")) + .hasMessage( + """ JSON documents are different: Different value found in node "a.a1", expected: <2> but was: <1>. """); @@ -2367,23 +2389,23 @@ void shouldFailFast() { @Test void hamcrestMessageTest() { assertThatThrownBy( - () -> assertThatJson(""" - { - "someText": "abc123" - } - """) - .withMatcher("exampleMatcher", org.hamcrest.Matchers.equalTo("def456")) - .isEqualTo( - """ - { - "someText": "${json-unit.matches:exampleMatcher}" - } - """)) - .hasMessage( - """ - JSON documents are different: - Matcher "exampleMatcher" does not match value "abc123" in node "someText". Expected "def456" but was "abc123" - """); + () -> assertThatJson(""" + { + "someText": "abc123" + } + """) + .withMatcher("exampleMatcher", org.hamcrest.Matchers.equalTo("def456")) + .isEqualTo( + """ + { + "someText": "${json-unit.matches:exampleMatcher}" + } + """)) + .hasMessage( + """ + JSON documents are different: + Matcher "exampleMatcher" does not match value "abc123" in node "someText". Expected "def456" but was "abc123" + """); } @Nested @@ -2391,60 +2413,60 @@ protected class ReportAsString { @Test protected void shouldNormalizeComplexJsons() { assertThatThrownBy(() -> assertThatJson("{\"a\": {\"c\": [{\"e\": 2, \"f\": 3}, 3]}, \"b\": false}") - .when(REPORTING_DIFFERENCE_AS_NORMALIZED_STRING) - .isEqualTo("{\"b\": true, \"a\": {\"c\": [{\"f\": 2, \"e\": 3}, 5]}}")) - .hasMessage( - """ - JSON documents are different: expected <{ - "b": true, - "a": { - "c": [ - { - "f": 2, - "e": 3 - }, - 5 - ] - } - }>but was <{ - "b": false, - "a": { - "c": [ - { - "f": 3, - "e": 2 - }, - 3 - ] - } - }>"""); + .when(REPORTING_DIFFERENCE_AS_NORMALIZED_STRING) + .isEqualTo("{\"b\": true, \"a\": {\"c\": [{\"f\": 2, \"e\": 3}, 5]}}")) + .hasMessage( + """ + JSON documents are different: expected <{ + "b": true, + "a": { + "c": [ + { + "f": 2, + "e": 3 + }, + 5 + ] + } + }>but was <{ + "b": false, + "a": { + "c": [ + { + "f": 3, + "e": 2 + }, + 3 + ] + } + }>"""); } @Test void shouldPrintMapKeysInTheSameOrderAsExpected() { assertThatThrownBy(() -> assertThatJson("{\"d\": 1, \"c\": 2, \"b\": 3}") - .when(REPORTING_DIFFERENCE_AS_NORMALIZED_STRING) - .isEqualTo("{\"a\": 1, \"b\": 3, \"c\": 2}")) - .hasMessage( - """ - JSON documents are different: expected <{ - "a": 1, - "b": 3, - "c": 2 - }>but was <{ - "b": 3, - "c": 2, - "d": 1 - }>"""); + .when(REPORTING_DIFFERENCE_AS_NORMALIZED_STRING) + .isEqualTo("{\"a\": 1, \"b\": 3, \"c\": 2}")) + .hasMessage( + """ + JSON documents are different: expected <{ + "a": 1, + "b": 3, + "c": 2 + }>but was <{ + "b": 3, + "c": 2, + "d": 1 + }>"""); } @Test void shouldPrintMapKeysInTheSameOrderAsInArrays() { assertThatThrownBy(() -> assertThatJson("{\"root\":[{\"d\": 1, \"c\": 2, \"b\": 3}]}") - .when(REPORTING_DIFFERENCE_AS_NORMALIZED_STRING) - .isEqualTo("{\"root\":[{\"a\": 1, \"b\": 3, \"c\": 2}]}")) - .hasMessage( - """ + .when(REPORTING_DIFFERENCE_AS_NORMALIZED_STRING) + .isEqualTo("{\"root\":[{\"a\": 1, \"b\": 3, \"c\": 2}]}")) + .hasMessage( + """ JSON documents are different: expected <{ "root": [ { @@ -2467,30 +2489,30 @@ void shouldPrintMapKeysInTheSameOrderAsInArrays() { @Test void shouldNotBreakOnDifferentType() { assertThatThrownBy(() -> assertThatJson("{\"a\": {\"b\": 1}}") - .when(REPORTING_DIFFERENCE_AS_NORMALIZED_STRING) - .isEqualTo("{\"a\": [\"b\", 1]}")) - .hasMessage( - """ - JSON documents are different: expected <{ - "a": [ - "b", - 1 - ] - }>but was <{ - "a": { - "b": 1 - } - }>"""); + .when(REPORTING_DIFFERENCE_AS_NORMALIZED_STRING) + .isEqualTo("{\"a\": [\"b\", 1]}")) + .hasMessage( + """ + JSON documents are different: expected <{ + "a": [ + "b", + 1 + ] + }>but was <{ + "a": { + "b": 1 + } + }>"""); } @Test void shouldWorkWithMissingPath() { assertThatThrownBy(() -> assertThatJson("{\"a\": 1}") - .when(REPORTING_DIFFERENCE_AS_NORMALIZED_STRING) - .inPath("c") - .isEqualTo("{\"b\": true}")) - .hasMessage( - """ + .when(REPORTING_DIFFERENCE_AS_NORMALIZED_STRING) + .inPath("c") + .isEqualTo("{\"b\": true}")) + .hasMessage( + """ JSON documents are different: Missing node in path "c". """); @@ -2499,11 +2521,11 @@ void shouldWorkWithMissingPath() { @Test void shouldWorkWithPaths() { assertThatThrownBy(() -> assertThatJson("{\"a\": {\"c\": [{\"e\": 2}, 3]}, \"b\": false}") - .when(REPORTING_DIFFERENCE_AS_NORMALIZED_STRING) - .inPath("a.c") - .isEqualTo("[{\"e\": 3}, 5]")) - .hasMessage( - """ + .when(REPORTING_DIFFERENCE_AS_NORMALIZED_STRING) + .inPath("a.c") + .isEqualTo("[{\"e\": 3}, 5]")) + .hasMessage( + """ JSON documents are different: expected <[ { "e": 3 @@ -2519,7 +2541,7 @@ void shouldWorkWithPaths() { } private static final String json = - """ + """ { "store": { "book": [