diff --git a/json-unit-json-path/src/main/java/net/javacrumbs/jsonunit/jsonpath/JsonPathAdapter.java b/json-unit-json-path/src/main/java/net/javacrumbs/jsonunit/jsonpath/JsonPathAdapter.java index fdc7fac3..b7faf267 100644 --- a/json-unit-json-path/src/main/java/net/javacrumbs/jsonunit/jsonpath/JsonPathAdapter.java +++ b/json-unit-json-path/src/main/java/net/javacrumbs/jsonunit/jsonpath/JsonPathAdapter.java @@ -41,7 +41,8 @@ public static Object inPath(@Nullable Object json, @NotNull String path) { try { MatchRecordingListener recordingListener = new MatchRecordingListener(); Object value = readValue(defaultConfiguration().addEvaluationListeners(recordingListener), json, path); - return jsonSource(wrapDeserializedObject(value), concatJsonPaths(json, path), recordingListener.getMatchingPaths()); + return jsonSource( + wrapDeserializedObject(value), concatJsonPaths(json, path), recordingListener.getMatchingPaths()); } catch (PathNotFoundException e) { return jsonSource(missingNode(), concatJsonPaths(json, path)); } @@ -55,9 +56,11 @@ public static Object inPath(@Nullable Object json, @NotNull String path) { } if (newPathSegment.startsWith("$.")) { return pathPrefix + newPathSegment.substring(1); - } else { + } + if (newPathSegment.startsWith("[")) { return pathPrefix + newPathSegment; } + return pathPrefix + "." + newPathSegment; } private static class MatchRecordingListener implements EvaluationListener { diff --git a/json-unit-kotest/src/test/kotlin/net/javacrumbs/jsonunit/kotest/test/KotestTest.kt b/json-unit-kotest/src/test/kotlin/net/javacrumbs/jsonunit/kotest/test/KotestTest.kt index 6f48eebf..ecaf2635 100644 --- a/json-unit-kotest/src/test/kotlin/net/javacrumbs/jsonunit/kotest/test/KotestTest.kt +++ b/json-unit-kotest/src/test/kotlin/net/javacrumbs/jsonunit/kotest/test/KotestTest.kt @@ -62,6 +62,17 @@ Different value found in node "test", expected: <2> but was: <1>.""" ) } + @Test + fun `Should assert chained inPath`() { + assertThrows { + """{"test": {"nested": 1}}""".inPath("test").inPath("nested") should equalJson("2") + } + .shouldHaveMessage( + """JSON documents are different: +Different value found in node "test.nested", expected: <2> but was: <1>.""" + ) + } + @Test fun `Should assert nested`() { assertThrows { @@ -70,7 +81,7 @@ Different value found in node "test", expected: <2> but was: <1>.""" .shouldHaveMessage( """JSON in path "test" JSON documents are different: -Different value found in node "nested", expected: <2> but was: <1>.""" +Different value found in node "test.nested", expected: <2> but was: <1>.""" ) } diff --git a/json-unit-spring/src/main/java/net/javacrumbs/jsonunit/spring/AbstractSpringMatcher.java b/json-unit-spring/src/main/java/net/javacrumbs/jsonunit/spring/AbstractSpringMatcher.java index 259e991b..e8715831 100644 --- a/json-unit-spring/src/main/java/net/javacrumbs/jsonunit/spring/AbstractSpringMatcher.java +++ b/json-unit-spring/src/main/java/net/javacrumbs/jsonunit/spring/AbstractSpringMatcher.java @@ -23,20 +23,15 @@ import net.javacrumbs.jsonunit.core.internal.matchers.InternalMatcher; import org.jetbrains.annotations.NotNull; -import static net.javacrumbs.jsonunit.core.internal.JsonUtils.getPathPrefix; - abstract class AbstractSpringMatcher { - private final Path path; private final Configuration configuration; private final Consumer matcher; private final Function jsonTransformer; AbstractSpringMatcher( - @NotNull Path path, @NotNull Configuration configuration, @NotNull Consumer matcher, @NotNull Function jsonTransformer) { - this.path = path; this.configuration = configuration; this.matcher = matcher; this.jsonTransformer = jsonTransformer; @@ -45,6 +40,7 @@ abstract class AbstractSpringMatcher { void doMatch(Object actual) { Object json = jsonTransformer.apply(actual); String pathPrefix = JsonUtils.getPathPrefix(json); - matcher.accept(new InternalMatcher(json, pathPrefix.isEmpty() ? path : Path.create("", pathPrefix), "", configuration)); + Path path = Path.create("", pathPrefix); + matcher.accept(new InternalMatcher(json, path, "", configuration)); } } diff --git a/json-unit-spring/src/main/java/net/javacrumbs/jsonunit/spring/AbstractSpringMatchers.java b/json-unit-spring/src/main/java/net/javacrumbs/jsonunit/spring/AbstractSpringMatchers.java index 731927ce..56da1b6a 100644 --- a/json-unit-spring/src/main/java/net/javacrumbs/jsonunit/spring/AbstractSpringMatchers.java +++ b/json-unit-spring/src/main/java/net/javacrumbs/jsonunit/spring/AbstractSpringMatchers.java @@ -21,7 +21,6 @@ import net.javacrumbs.jsonunit.core.Configuration; import net.javacrumbs.jsonunit.core.ConfigurationWhen; import net.javacrumbs.jsonunit.core.Option; -import net.javacrumbs.jsonunit.core.internal.Path; import net.javacrumbs.jsonunit.core.internal.matchers.InternalMatcher; import net.javacrumbs.jsonunit.core.listener.DifferenceListener; import net.javacrumbs.jsonunit.jsonpath.JsonPathAdapter; @@ -35,13 +34,10 @@ * @param Type of the matcher */ abstract class AbstractSpringMatchers { - final Path path; final Configuration configuration; final Function jsonTransformer; - AbstractSpringMatchers( - @NotNull Path path, @NotNull Configuration configuration, Function jsonTransformer) { - this.path = path; + AbstractSpringMatchers(@NotNull Configuration configuration, Function jsonTransformer) { this.configuration = configuration; this.jsonTransformer = jsonTransformer; } @@ -50,13 +46,10 @@ abstract class AbstractSpringMatchers { abstract MATCHER matcher(@NotNull Consumer matcher); @NotNull - abstract ME matchers( - @NotNull Path path, - @NotNull Configuration configuration, - @NotNull Function jsonTransformer); + abstract ME matchers(@NotNull Configuration configuration, @NotNull Function jsonTransformer); - protected ME matchers(@NotNull Path path, @NotNull Configuration configuration) { - return matchers(path, configuration, jsonTransformer); + protected ME matchers(@NotNull Configuration configuration) { + return matchers(configuration, jsonTransformer); } /** @@ -70,8 +63,8 @@ protected ME matchers(@NotNull Path path, @NotNull Configuration configuration) * @return object comparing only node given by path. */ @NotNull - public ME node(String newPath) { - return matchers(path.copy(newPath), configuration); + public ME node(String path) { + return inPath(path); } /** @@ -79,7 +72,7 @@ public ME node(String newPath) { */ @NotNull public ME inPath(String path) { - return matchers(this.path, configuration, json -> JsonPathAdapter.inPath(jsonTransformer.apply(json), path)); + return matchers(configuration, json -> JsonPathAdapter.inPath(jsonTransformer.apply(json), path)); } /** @@ -88,7 +81,7 @@ public ME inPath(String path) { */ @NotNull public ME ignoring(@NotNull String ignorePlaceholder) { - return matchers(path, configuration.withIgnorePlaceholder(ignorePlaceholder)); + return matchers(configuration.withIgnorePlaceholder(ignorePlaceholder)); } /** @@ -105,7 +98,7 @@ public ME withTolerance(double tolerance) { */ @NotNull public ME withMatcher(@NotNull String matcherName, @NotNull Matcher matcher) { - return matchers(path, configuration.withMatcher(matcherName, matcher)); + return matchers(configuration.withMatcher(matcherName, matcher)); } /** @@ -114,12 +107,12 @@ public ME withMatcher(@NotNull String matcherName, @NotNull Matcher matcher) */ @NotNull public ME withTolerance(@Nullable BigDecimal tolerance) { - return matchers(path, configuration.withTolerance(tolerance)); + return matchers(configuration.withTolerance(tolerance)); } @NotNull public ME withDifferenceListener(@NotNull DifferenceListener differenceListener) { - return matchers(path, configuration.withDifferenceListener(differenceListener)); + return matchers(configuration.withDifferenceListener(differenceListener)); } /** @@ -130,7 +123,7 @@ public ME withDifferenceListener(@NotNull DifferenceListener differenceListener) */ @NotNull public ME when(@NotNull Option firstOption, @NotNull Option... otherOptions) { - return matchers(path, configuration.withOptions(firstOption, otherOptions)); + return matchers(configuration.withOptions(firstOption, otherOptions)); } /** @@ -141,7 +134,7 @@ public ME when(@NotNull Option firstOption, @NotNull Option... otherOptions) { @NotNull public ME when( @NotNull ConfigurationWhen.PathsParam object, @NotNull ConfigurationWhen.ApplicableForPath... actions) { - return matchers(path, configuration.when(object, actions)); + return matchers(configuration.when(object, actions)); } /** diff --git a/json-unit-spring/src/main/java/net/javacrumbs/jsonunit/spring/JsonUnitRequestMatchers.java b/json-unit-spring/src/main/java/net/javacrumbs/jsonunit/spring/JsonUnitRequestMatchers.java index e8356304..a86483ec 100644 --- a/json-unit-spring/src/main/java/net/javacrumbs/jsonunit/spring/JsonUnitRequestMatchers.java +++ b/json-unit-spring/src/main/java/net/javacrumbs/jsonunit/spring/JsonUnitRequestMatchers.java @@ -18,7 +18,6 @@ import java.util.function.Consumer; import java.util.function.Function; import net.javacrumbs.jsonunit.core.Configuration; -import net.javacrumbs.jsonunit.core.internal.Path; import net.javacrumbs.jsonunit.core.internal.matchers.InternalMatcher; import org.jetbrains.annotations.NotNull; import org.springframework.http.client.ClientHttpRequest; @@ -38,23 +37,21 @@ */ public class JsonUnitRequestMatchers extends AbstractSpringMatchers { - private JsonUnitRequestMatchers(Path path, Configuration configuration, Function jsonTransformer) { - super(path, configuration, jsonTransformer); + private JsonUnitRequestMatchers(Configuration configuration, Function jsonTransformer) { + super(configuration, jsonTransformer); } @NotNull @Override RequestMatcher matcher(@NotNull Consumer matcher) { - return new JsonRequestMatcher(path, configuration, matcher, jsonTransformer); + return new JsonRequestMatcher(configuration, matcher, jsonTransformer); } @Override @NotNull JsonUnitRequestMatchers matchers( - @NotNull Path path, - @NotNull Configuration configuration, - @NotNull Function jsonTransformer) { - return new JsonUnitRequestMatchers(path, configuration, jsonTransformer); + @NotNull Configuration configuration, @NotNull Function jsonTransformer) { + return new JsonUnitRequestMatchers(configuration, jsonTransformer); } /** @@ -62,16 +59,15 @@ JsonUnitRequestMatchers matchers( */ @NotNull public static JsonUnitRequestMatchers json() { - return new JsonUnitRequestMatchers(Path.root(), Configuration.empty(), Function.identity()); + return new JsonUnitRequestMatchers(Configuration.empty(), Function.identity()); } private static class JsonRequestMatcher extends AbstractSpringMatcher implements RequestMatcher { private JsonRequestMatcher( - @NotNull Path path, @NotNull Configuration configuration, @NotNull Consumer matcher, @NotNull Function jsonTransformer) { - super(path, configuration, matcher, jsonTransformer); + super(configuration, matcher, jsonTransformer); } @Override diff --git a/json-unit-spring/src/main/java/net/javacrumbs/jsonunit/spring/JsonUnitResultMatchers.java b/json-unit-spring/src/main/java/net/javacrumbs/jsonunit/spring/JsonUnitResultMatchers.java index 380fedcb..c08dae89 100644 --- a/json-unit-spring/src/main/java/net/javacrumbs/jsonunit/spring/JsonUnitResultMatchers.java +++ b/json-unit-spring/src/main/java/net/javacrumbs/jsonunit/spring/JsonUnitResultMatchers.java @@ -20,7 +20,6 @@ import java.util.function.Consumer; import java.util.function.Function; import net.javacrumbs.jsonunit.core.Configuration; -import net.javacrumbs.jsonunit.core.internal.Path; import net.javacrumbs.jsonunit.core.internal.matchers.InternalMatcher; import org.jetbrains.annotations.NotNull; import org.springframework.mock.web.MockHttpServletResponse; @@ -37,39 +36,36 @@ * */ public class JsonUnitResultMatchers extends AbstractSpringMatchers { - private JsonUnitResultMatchers(Path path, Configuration configuration, Function jsonTransformer) { - super(path, configuration, jsonTransformer); + private JsonUnitResultMatchers(Configuration configuration, Function jsonTransformer) { + super(configuration, jsonTransformer); } /** * Creates JsonUnitResultMatchers to be used for JSON assertions. */ public static JsonUnitResultMatchers json() { - return new JsonUnitResultMatchers(Path.root(), Configuration.empty(), Function.identity()); + return new JsonUnitResultMatchers(Configuration.empty(), Function.identity()); } @Override @NotNull ResultMatcher matcher(@NotNull Consumer matcher) { - return new JsonResultMatcher(path, configuration, matcher, jsonTransformer); + return new JsonResultMatcher(configuration, matcher, jsonTransformer); } @Override @NotNull JsonUnitResultMatchers matchers( - @NotNull Path path, - @NotNull Configuration configuration, - @NotNull Function jsonTransformer) { - return new JsonUnitResultMatchers(path, configuration, jsonTransformer); + @NotNull Configuration configuration, @NotNull Function jsonTransformer) { + return new JsonUnitResultMatchers(configuration, jsonTransformer); } private static class JsonResultMatcher extends AbstractSpringMatcher implements ResultMatcher { private JsonResultMatcher( - @NotNull Path path, @NotNull Configuration configuration, @NotNull Consumer matcher, @NotNull Function jsonTransformer) { - super(path, configuration, matcher, jsonTransformer); + super(configuration, matcher, jsonTransformer); } @Override diff --git a/json-unit-spring/src/main/java/net/javacrumbs/jsonunit/spring/WebTestClientJsonMatcher.java b/json-unit-spring/src/main/java/net/javacrumbs/jsonunit/spring/WebTestClientJsonMatcher.java index bcb82a1d..22934ed9 100644 --- a/json-unit-spring/src/main/java/net/javacrumbs/jsonunit/spring/WebTestClientJsonMatcher.java +++ b/json-unit-spring/src/main/java/net/javacrumbs/jsonunit/spring/WebTestClientJsonMatcher.java @@ -5,7 +5,6 @@ import java.util.function.Consumer; import java.util.function.Function; import net.javacrumbs.jsonunit.core.Configuration; -import net.javacrumbs.jsonunit.core.internal.Path; import net.javacrumbs.jsonunit.core.internal.matchers.InternalMatcher; import org.jetbrains.annotations.NotNull; import org.springframework.test.web.reactive.server.EntityExchangeResult; @@ -22,37 +21,34 @@ */ public class WebTestClientJsonMatcher extends AbstractSpringMatchers>> { - private WebTestClientJsonMatcher(Path path, Configuration configuration, Function jsonTransformer) { - super(path, configuration, jsonTransformer); + private WebTestClientJsonMatcher(Configuration configuration, Function jsonTransformer) { + super(configuration, jsonTransformer); } public static WebTestClientJsonMatcher json() { - return new WebTestClientJsonMatcher(Path.root(), Configuration.empty(), Function.identity()); + return new WebTestClientJsonMatcher(Configuration.empty(), Function.identity()); } @Override @NotNull Consumer> matcher(@NotNull Consumer matcher) { - return new JsonUnitWebTestClientMatcher(path, configuration, matcher, jsonTransformer); + return new JsonUnitWebTestClientMatcher(configuration, matcher, jsonTransformer); } @Override @NotNull WebTestClientJsonMatcher matchers( - @NotNull Path path, - @NotNull Configuration configuration, - @NotNull Function jsonTransformer) { - return new WebTestClientJsonMatcher(path, configuration, jsonTransformer); + @NotNull Configuration configuration, @NotNull Function jsonTransformer) { + return new WebTestClientJsonMatcher(configuration, jsonTransformer); } private static class JsonUnitWebTestClientMatcher extends AbstractSpringMatcher implements Consumer> { private JsonUnitWebTestClientMatcher( - @NotNull Path path, @NotNull Configuration configuration, @NotNull Consumer matcher, @NotNull Function jsonTransformer) { - super(path, configuration, matcher, jsonTransformer); + super(configuration, matcher, jsonTransformer); } @Override diff --git a/json-unit-spring/src/test/java/net/javacrumbs/jsonunit/spring/testit/MockMvcTest.java b/json-unit-spring/src/test/java/net/javacrumbs/jsonunit/spring/testit/MockMvcTest.java index ca77ae9b..9a266a88 100644 --- a/json-unit-spring/src/test/java/net/javacrumbs/jsonunit/spring/testit/MockMvcTest.java +++ b/json-unit-spring/src/test/java/net/javacrumbs/jsonunit/spring/testit/MockMvcTest.java @@ -31,6 +31,7 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import java.util.List; import net.javacrumbs.jsonunit.core.listener.Difference; import net.javacrumbs.jsonunit.core.listener.DifferenceContext; import net.javacrumbs.jsonunit.core.listener.DifferenceListener; @@ -47,7 +48,6 @@ import org.springframework.test.web.servlet.ResultActions; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; -import java.util.List; @ExtendWith(SpringExtension.class) @ContextConfiguration(classes = {SpringConfig.class}) @@ -79,11 +79,13 @@ void shouldAllowOptionsOnPath() throws Exception { void shouldSupportJsonPath() throws Exception { exec("/sampleProduces").andExpect(json().inPath("$.result.array[1]").isEqualTo(2)); } + @Test void shouldSupportJsonPathError() throws Exception { - assertThatThrownBy(() -> - exec("/sampleProduces").andExpect(json().inPath("$.result.array[1]").isEqualTo(3)) - ).hasMessage(""" + assertThatThrownBy(() -> exec("/sampleProduces") + .andExpect(json().inPath("$.result.array[1]").isEqualTo(3))) + .hasMessage( + """ JSON documents are different: Different value found in node "$.result.array[1]", expected: <3> but was: <2>. """); @@ -91,9 +93,11 @@ void shouldSupportJsonPathError() throws Exception { @Test void shouldSupportJsonPathChainedError() { - assertThatThrownBy(() -> - exec("/sampleProduces").andExpect(json().inPath("$.result").inPath("$.array[*]").isEqualTo(List.of(1, 3, 3))) - ).hasMessage(""" + assertThatThrownBy(() -> exec("/sampleProduces") + .andExpect( + json().inPath("$.result").inPath("$.array[*]").isEqualTo(List.of(1, 3, 3)))) + .hasMessage( + """ JSON documents are different: Different value found in node "$.result.array[*][1]", expected: <3> but was: <2>. """); @@ -101,11 +105,12 @@ void shouldSupportJsonPathChainedError() { @Test void shouldSupportJsonPathChainedWithNodeError() { - assertThatThrownBy(() -> - exec("/sampleProduces").andExpect(json().node("result").inPath("$.array[1]").isEqualTo(3)) - ).hasMessage(""" + assertThatThrownBy(() -> exec("/sampleProduces") + .andExpect(json().node("result").inPath("$.array[1]").isEqualTo(3))) + .hasMessage( + """ JSON documents are different: - Different value found in node "$.result.array[*][1]", expected: <3> but was: <2>. + Different value found in node "result.array[1]", expected: <3> but was: <2>. """); } @@ -212,7 +217,7 @@ void isStringEqualToShouldPassIfEquals() throws Exception { void isAbsentShouldFailIfNodeExists() { assertThatThrownBy(() -> exec().andExpect(json().node("result.string").isAbsent())) .hasMessage( - "Different value found in node \"result.string\", expected: but was: <\"stringValue\">."); + "Different value found in node \"$.result.string\", expected: but was: <\"stringValue\">."); } @Test diff --git a/json-unit-spring/src/test/java/net/javacrumbs/jsonunit/spring/testit/WebTestClientTest.java b/json-unit-spring/src/test/java/net/javacrumbs/jsonunit/spring/testit/WebTestClientTest.java index 30078459..cdf7208f 100644 --- a/json-unit-spring/src/test/java/net/javacrumbs/jsonunit/spring/testit/WebTestClientTest.java +++ b/json-unit-spring/src/test/java/net/javacrumbs/jsonunit/spring/testit/WebTestClientTest.java @@ -185,7 +185,7 @@ void isStringEqualToShouldPassIfEquals() { void isAbsentShouldFailIfNodeExists() { assertThatThrownBy(() -> exec().consumeWith(json().node("result.string").isAbsent())) .hasMessageStartingWith( - "Different value found in node \"result.string\", expected: but was: <\"stringValue\">."); + "Different value found in node \"$.result.string\", expected: but was: <\"stringValue\">."); } @Test