From 2a1ff6d78c7e924c9eb6f2cbbcc573d80c038548 Mon Sep 17 00:00:00 2001 From: Lukas Krecan Date: Sun, 10 Nov 2024 18:13:54 +0100 Subject: [PATCH] Support for InstanceOfAssertFactory --- .../jsonunit/assertj/JsonAssert.java | 62 +++++++++++++------ .../jsonunit/assertj/JsonListAssert.java | 47 -------------- .../internal/matchers/InternalMatcher.java | 2 +- .../test/base/AbstractAssertJTest.java | 37 +++++++++++ 4 files changed, 81 insertions(+), 67 deletions(-) diff --git a/json-unit-assertj/src/main/java/net/javacrumbs/jsonunit/assertj/JsonAssert.java b/json-unit-assertj/src/main/java/net/javacrumbs/jsonunit/assertj/JsonAssert.java index f45c143b..d172e286 100644 --- a/json-unit-assertj/src/main/java/net/javacrumbs/jsonunit/assertj/JsonAssert.java +++ b/json-unit-assertj/src/main/java/net/javacrumbs/jsonunit/assertj/JsonAssert.java @@ -51,6 +51,7 @@ import org.assertj.core.api.BigDecimalAssert; import org.assertj.core.api.BigIntegerAssert; import org.assertj.core.api.BooleanAssert; +import org.assertj.core.api.InstanceOfAssertFactory; import org.assertj.core.api.StringAssert; import org.assertj.core.api.UriAssert; import org.assertj.core.description.Description; @@ -132,8 +133,7 @@ public JsonAssert isEqualTo(@Nullable Object expected) { @SuppressWarnings("unchecked") public JsonMapAssert isObject() { Node node = assertType(OBJECT); - return new JsonMapAssert((Map) node.getValue(), path.asPrefix(), configuration) - .as("Different value found in node \"%s\"", path); + return describe(new JsonMapAssert((Map) node.getValue(), path.asPrefix(), configuration)); } /** @@ -150,8 +150,7 @@ public BigDecimalAssert isNumber() { */ public BigIntegerAssert isIntegralNumber() { Node node = internalMatcher().assertIntegralNumber(); - return new BigIntegerAssert(node.decimalValue().toBigIntegerExact()) - .as("Different value found in node \"%s\"", path); + return describe(new BigIntegerAssert(node.decimalValue().toBigIntegerExact())); } /** @@ -178,7 +177,7 @@ public BigDecimalAssert asNumber() { } private BigDecimalAssert createBigDecimalAssert(BigDecimal value) { - return new BigDecimalAssert(value).as("Different value found in node \"%s\"", path); + return describe(new BigDecimalAssert(value)); } private InternalMatcher internalMatcher() { @@ -188,35 +187,45 @@ private InternalMatcher internalMatcher() { /** * Asserts that given node is present and is of type array. - * - * @return */ @NotNull public JsonListAssert isArray() { Node node = assertType(ARRAY); - return new JsonListAssert((List) node.getValue(), path.asPrefix(), configuration).as("Node \"%s\"", path); + return createListAssert(node).as("Node \"%s\"", path); + } + + private @NotNull JsonListAssert createListAssert(Node node) { + return new JsonListAssert((List) node.getValue(), path.asPrefix(), configuration); } /** * Asserts that given node is present and is of type boolean. - * - * @return */ @NotNull public BooleanAssert isBoolean() { Node node = assertType(BOOLEAN); - return new BooleanAssert((Boolean) node.getValue()).as("Different value found in node \"%s\"", path); + return createBooleanAssert(node); + } + + private BooleanAssert createBooleanAssert(Node node) { + return describe(new BooleanAssert((Boolean) node.getValue())); } /** * Asserts that given node is present and is of type string. - * - * @return */ @NotNull public StringAssert isString() { Node node = assertType(STRING); - return new StringAssert((String) node.getValue()).as("Different value found in node \"%s\"", path); + return createStringAssert(node); + } + + private StringAssert createStringAssert(Node node) { + return describe(new StringAssert((String) node.getValue())); + } + + private > T describe(T ass) { + return ass.as("Different value found in node \"%s\"", path); } @Override @@ -227,8 +236,6 @@ public AbstractStringAssert asString() { /** * Asserts that given node is present and is null. - * - * @return */ @Override public void isNull() { @@ -237,13 +244,11 @@ public void isNull() { /** * Asserts that given node is present and is URI. - * - * @return */ @NotNull public UriAssert isUri() { Node node = assertType(STRING); - return new UriAssert(URI.create((String) node.getValue())).as("Different value found in node \"%s\"", path); + return describe(new UriAssert(URI.create((String) node.getValue()))); } /** @@ -276,6 +281,25 @@ private Node assertType(Node.NodeType type) { return internalMatcher().assertType(type); } + @Override + public > ASSERT asInstanceOf( + InstanceOfAssertFactory instanceOfAssertFactory) { + Node node = internalMatcher().getActualNode(); + + var ass = + switch (node.getNodeType()) { + case OBJECT -> throw new UnsupportedOperationException( + "asInstanceOf is not supported for Object type"); + case ARRAY -> createListAssert(node); + case STRING -> createStringAssert(node); + case NUMBER -> createBigDecimalAssert(node.decimalValue()); + case BOOLEAN -> createBooleanAssert(node); + case NULL -> new StringAssert(null); + }; + + return ass.asInstanceOf(instanceOfAssertFactory); + } + /** * JsonAssert that can be configured to prevent mistakes like * diff --git a/json-unit-assertj/src/main/java/net/javacrumbs/jsonunit/assertj/JsonListAssert.java b/json-unit-assertj/src/main/java/net/javacrumbs/jsonunit/assertj/JsonListAssert.java index 8f67657a..10f54442 100644 --- a/json-unit-assertj/src/main/java/net/javacrumbs/jsonunit/assertj/JsonListAssert.java +++ b/json-unit-assertj/src/main/java/net/javacrumbs/jsonunit/assertj/JsonListAssert.java @@ -22,9 +22,7 @@ import net.javacrumbs.jsonunit.core.Configuration; import net.javacrumbs.jsonunit.core.internal.Diff; import net.javacrumbs.jsonunit.core.internal.Path; -import org.assertj.core.api.AbstractAssert; import org.assertj.core.api.FactoryBasedNavigableListAssert; -import org.assertj.core.api.InstanceOfAssertFactory; import org.assertj.core.description.Description; import org.assertj.core.error.BasicErrorMessageFactory; import org.jetbrains.annotations.NotNull; @@ -68,51 +66,6 @@ protected JsonListAssert newAbstractIterableAssert(Iterable iterable) { return new JsonListAssert(newArrayList(iterable), path, configuration); } - /** - * @deprecated InstanceOfAssertFactory is not supported with JsonUnit - */ - @Override - @Deprecated - public > ASSERT element( - int index, InstanceOfAssertFactory assertFactory) { - return super.element(index, assertFactory); - } - - /** - * @deprecated InstanceOfAssertFactory is not supported with JsonUnit - */ - @Override - @Deprecated - public > ASSERT singleElement( - InstanceOfAssertFactory assertFactory) { - return super.singleElement(assertFactory); - } - - @Override - public > ASSERT asInstanceOf( - InstanceOfAssertFactory instanceOfAssertFactory) { - throw failure("Please use isString(), isNumber(), isBoolean(), isNull(), isUri(), isArray() or isObject().%n" - + "This method will most likely not provide the result you expect it to."); - } - - /** - * @deprecated InstanceOfAssertFactory is not supported with JsonUnit - */ - @Override - @Deprecated - public > ASSERT first(InstanceOfAssertFactory assertFactory) { - return super.first(assertFactory); - } - - /** - * @deprecated InstanceOfAssertFactory is not supported with JsonUnit - */ - @Override - @Deprecated - public > ASSERT last(InstanceOfAssertFactory assertFactory) { - return super.last(assertFactory); - } - @NotNull private Diff createDiff(Object other) { return Diff.create(other, wrapDeserializedObject(actual), "fullJson", path, configuration); diff --git a/json-unit-core/src/main/java/net/javacrumbs/jsonunit/core/internal/matchers/InternalMatcher.java b/json-unit-core/src/main/java/net/javacrumbs/jsonunit/core/internal/matchers/InternalMatcher.java index 93503400..43820ae1 100644 --- a/json-unit-core/src/main/java/net/javacrumbs/jsonunit/core/internal/matchers/InternalMatcher.java +++ b/json-unit-core/src/main/java/net/javacrumbs/jsonunit/core/internal/matchers/InternalMatcher.java @@ -328,7 +328,7 @@ public void isNotNull() { } } - private Node getActualNode() { + public Node getActualNode() { return getNode(actual, path); } 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 00e0acd0..724643c6 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 @@ -42,6 +42,10 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.Assertions.entry; import static org.assertj.core.api.Assertions.tuple; +import static org.assertj.core.api.InstanceOfAssertFactories.BIG_DECIMAL; +import static org.assertj.core.api.InstanceOfAssertFactories.BOOLEAN; +import static org.assertj.core.api.InstanceOfAssertFactories.LIST; +import static org.assertj.core.api.InstanceOfAssertFactories.STRING; import static org.hamcrest.Matchers.greaterThan; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -2253,6 +2257,39 @@ record DummyResponse(String trackingId, String json) {} .containsEntry("foo", "bar"); // <- JsonUnit API } + @Test + void shouldUseAsInstanceOfToMoveFromJsonUnit() { + assertThatJson("{\"a\":[1, 2, 3]}") + .inPath("a") + .isArray() + .first(BIG_DECIMAL) + .isEqualTo("1"); + assertThatJson("{\"a\":[1, 2, true]}") + .inPath("a") + .isArray() + .last(BOOLEAN) + .isEqualTo(true); + assertThatJson("{\"a\":[1, \"s\", true]}") + .inPath("a") + .isArray() + .element(1, STRING) + .startsWith("s"); + assertThatJson("{\"a\":{\"b\": \"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"); + assertThatThrownBy(() -> assertThatJson("{\"a\": null}").inPath("a").asInstanceOf(BIG_DECIMAL)) + .isInstanceOf(AssertionError.class) + .hasMessage("\nExpecting actual not to be null"); + } + @Test void shouldNotIgnoreExtraArrayItemsWhenNotSpecified() { assertThatThrownBy(() -> assertThatJson("{\"a\":[1,2,3],\"b\":[1,2,3]}")