Skip to content

Commit

Permalink
Use JonPath instead of node in Spring
Browse files Browse the repository at this point in the history
  • Loading branch information
lukas-krecan committed Nov 29, 2024
1 parent c5b4363 commit fd82707
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ Different value found in node "test", expected: <2> but was: <1>."""
)
}

@Test
fun `Should assert chained inPath`() {
assertThrows<AssertionError> {
"""{"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<AssertionError> {
Expand All @@ -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>."""
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<InternalMatcher> matcher;
private final Function<Object, Object> jsonTransformer;

AbstractSpringMatcher(
@NotNull Path path,
@NotNull Configuration configuration,
@NotNull Consumer<InternalMatcher> matcher,
@NotNull Function<Object, Object> jsonTransformer) {
this.path = path;
this.configuration = configuration;
this.matcher = matcher;
this.jsonTransformer = jsonTransformer;
Expand All @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -35,13 +34,10 @@
* @param <MATCHER> Type of the matcher
*/
abstract class AbstractSpringMatchers<ME, MATCHER> {
final Path path;
final Configuration configuration;
final Function<Object, Object> jsonTransformer;

AbstractSpringMatchers(
@NotNull Path path, @NotNull Configuration configuration, Function<Object, Object> jsonTransformer) {
this.path = path;
AbstractSpringMatchers(@NotNull Configuration configuration, Function<Object, Object> jsonTransformer) {
this.configuration = configuration;
this.jsonTransformer = jsonTransformer;
}
Expand All @@ -50,13 +46,10 @@ abstract class AbstractSpringMatchers<ME, MATCHER> {
abstract MATCHER matcher(@NotNull Consumer<InternalMatcher> matcher);

@NotNull
abstract ME matchers(
@NotNull Path path,
@NotNull Configuration configuration,
@NotNull Function<Object, Object> jsonTransformer);
abstract ME matchers(@NotNull Configuration configuration, @NotNull Function<Object, Object> 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);
}

/**
Expand All @@ -70,16 +63,16 @@ 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);
}

/**
* Uses JsonPath to extract values from the actual value.
*/
@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));
}

/**
Expand All @@ -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));
}

/**
Expand All @@ -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));
}

/**
Expand All @@ -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));
}

/**
Expand All @@ -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));
}

/**
Expand All @@ -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));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -38,40 +37,37 @@
*/
public class JsonUnitRequestMatchers extends AbstractSpringMatchers<JsonUnitRequestMatchers, RequestMatcher> {

private JsonUnitRequestMatchers(Path path, Configuration configuration, Function<Object, Object> jsonTransformer) {
super(path, configuration, jsonTransformer);
private JsonUnitRequestMatchers(Configuration configuration, Function<Object, Object> jsonTransformer) {
super(configuration, jsonTransformer);
}

@NotNull
@Override
RequestMatcher matcher(@NotNull Consumer<InternalMatcher> 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<Object, Object> jsonTransformer) {
return new JsonUnitRequestMatchers(path, configuration, jsonTransformer);
@NotNull Configuration configuration, @NotNull Function<Object, Object> jsonTransformer) {
return new JsonUnitRequestMatchers(configuration, jsonTransformer);
}

/**
* Creates JsonUnitResultMatchers to be used for JSON assertions.
*/
@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<InternalMatcher> matcher,
@NotNull Function<Object, Object> jsonTransformer) {
super(path, configuration, matcher, jsonTransformer);
super(configuration, matcher, jsonTransformer);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,39 +36,36 @@
* </code>
*/
public class JsonUnitResultMatchers extends AbstractSpringMatchers<JsonUnitResultMatchers, ResultMatcher> {
private JsonUnitResultMatchers(Path path, Configuration configuration, Function<Object, Object> jsonTransformer) {
super(path, configuration, jsonTransformer);
private JsonUnitResultMatchers(Configuration configuration, Function<Object, Object> 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<InternalMatcher> 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<Object, Object> jsonTransformer) {
return new JsonUnitResultMatchers(path, configuration, jsonTransformer);
@NotNull Configuration configuration, @NotNull Function<Object, Object> jsonTransformer) {
return new JsonUnitResultMatchers(configuration, jsonTransformer);
}

private static class JsonResultMatcher extends AbstractSpringMatcher implements ResultMatcher {
private JsonResultMatcher(
@NotNull Path path,
@NotNull Configuration configuration,
@NotNull Consumer<InternalMatcher> matcher,
@NotNull Function<Object, Object> jsonTransformer) {
super(path, configuration, matcher, jsonTransformer);
super(configuration, matcher, jsonTransformer);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,37 +21,34 @@
*/
public class WebTestClientJsonMatcher
extends AbstractSpringMatchers<WebTestClientJsonMatcher, Consumer<EntityExchangeResult<byte[]>>> {
private WebTestClientJsonMatcher(Path path, Configuration configuration, Function<Object, Object> jsonTransformer) {
super(path, configuration, jsonTransformer);
private WebTestClientJsonMatcher(Configuration configuration, Function<Object, Object> 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<EntityExchangeResult<byte[]>> matcher(@NotNull Consumer<InternalMatcher> 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<Object, Object> jsonTransformer) {
return new WebTestClientJsonMatcher(path, configuration, jsonTransformer);
@NotNull Configuration configuration, @NotNull Function<Object, Object> jsonTransformer) {
return new WebTestClientJsonMatcher(configuration, jsonTransformer);
}

private static class JsonUnitWebTestClientMatcher extends AbstractSpringMatcher
implements Consumer<EntityExchangeResult<byte[]>> {
private JsonUnitWebTestClientMatcher(
@NotNull Path path,
@NotNull Configuration configuration,
@NotNull Consumer<InternalMatcher> matcher,
@NotNull Function<Object, Object> jsonTransformer) {
super(path, configuration, matcher, jsonTransformer);
super(configuration, matcher, jsonTransformer);
}

@Override
Expand Down
Loading

0 comments on commit fd82707

Please sign in to comment.