Skip to content

Commit

Permalink
Support for InstanceOfAssertFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
lukas-krecan committed Nov 11, 2024
1 parent b833b50 commit 2a1ff6d
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -132,8 +133,7 @@ public JsonAssert isEqualTo(@Nullable Object expected) {
@SuppressWarnings("unchecked")
public JsonMapAssert isObject() {
Node node = assertType(OBJECT);
return new JsonMapAssert((Map<String, Object>) node.getValue(), path.asPrefix(), configuration)
.as("Different value found in node \"%s\"", path);
return describe(new JsonMapAssert((Map<String, Object>) node.getValue(), path.asPrefix(), configuration));
}

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

/**
Expand All @@ -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() {
Expand All @@ -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 extends AbstractAssert<T, ?>> T describe(T ass) {
return ass.as("Different value found in node \"%s\"", path);
}

@Override
Expand All @@ -227,8 +236,6 @@ public AbstractStringAssert<?> asString() {

/**
* Asserts that given node is present and is null.
*
* @return
*/
@Override
public void isNull() {
Expand All @@ -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())));
}

/**
Expand Down Expand Up @@ -276,6 +281,25 @@ private Node assertType(Node.NodeType type) {
return internalMatcher().assertType(type);
}

@Override
public <ASSERT extends AbstractAssert<?, ?>> ASSERT asInstanceOf(
InstanceOfAssertFactory<?, ASSERT> 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
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 extends AbstractAssert<?, ?>> ASSERT element(
int index, InstanceOfAssertFactory<?, ASSERT> assertFactory) {
return super.element(index, assertFactory);
}

/**
* @deprecated InstanceOfAssertFactory is not supported with JsonUnit
*/
@Override
@Deprecated
public <ASSERT extends AbstractAssert<?, ?>> ASSERT singleElement(
InstanceOfAssertFactory<?, ASSERT> assertFactory) {
return super.singleElement(assertFactory);
}

@Override
public <ASSERT extends AbstractAssert<?, ?>> ASSERT asInstanceOf(
InstanceOfAssertFactory<?, ASSERT> 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 extends AbstractAssert<?, ?>> ASSERT first(InstanceOfAssertFactory<?, ASSERT> assertFactory) {
return super.first(assertFactory);
}

/**
* @deprecated InstanceOfAssertFactory is not supported with JsonUnit
*/
@Override
@Deprecated
public <ASSERT extends AbstractAssert<?, ?>> ASSERT last(InstanceOfAssertFactory<?, ASSERT> assertFactory) {
return super.last(assertFactory);
}

@NotNull
private Diff createDiff(Object other) {
return Diff.create(other, wrapDeserializedObject(actual), "fullJson", path, configuration);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ public void isNotNull() {
}
}

private Node getActualNode() {
public Node getActualNode() {
return getNode(actual, path);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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]}")
Expand Down

0 comments on commit 2a1ff6d

Please sign in to comment.