Skip to content

Commit

Permalink
#560 Add node method to JsonMapAssert
Browse files Browse the repository at this point in the history
  • Loading branch information
lukas-krecan committed Oct 2, 2022
1 parent 6c6f9f1 commit b85bc4a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import static java.util.Objects.deepEquals;
import static java.util.stream.Collectors.toList;
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.json;
import static net.javacrumbs.jsonunit.core.internal.JsonUtils.getNode;
import static net.javacrumbs.jsonunit.core.internal.JsonUtils.wrapDeserializedObject;
import static org.assertj.core.api.Assertions.entry;
import static org.assertj.core.error.ShouldContain.shouldContain;
Expand All @@ -57,6 +58,14 @@ public JsonMapAssert isEqualTo(@Nullable Object expected) {
return compare(expected, configuration);
}

/**
* Moves comparison to given node. Second call navigates from the last position in the JSON.
*/
@NotNull
public JsonAssert node(@NotNull String node) {
return new JsonAssert(path.to(node), configuration, getNode(actual, node));
}

@Override
@NotNull
public JsonMapAssert containsValue(@Nullable Object expected) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,18 @@ void shouldAssertInteger() {
assertThatJson("{\"a\":-10}").node("a").isIntegralNumber();
}

@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: <object> but was: <false>.");
}

@Test
void shouldAssertIntegerFailure() {
assertThatThrownBy(() -> assertThatJson("{\"a\":1.0}").node("a").isIntegralNumber())
Expand Down

0 comments on commit b85bc4a

Please sign in to comment.