From 044e9fb446ada26119dcd8b990b56f7238039dc7 Mon Sep 17 00:00:00 2001 From: Lukas Krecan Date: Thu, 3 Feb 2022 17:25:33 +0100 Subject: [PATCH] #465 Fix array assertions --- .../jsonunit/assertj/JsonAssert.java | 3 +- .../jsonunit/assertj/JsonListAssert.java | 6 +- .../jsonunit/assertj/JsonObjectAssert.java | 144 ++++++++++++++++++ .../test/base/AbstractAssertJTest.java | 13 ++ 4 files changed, 161 insertions(+), 5 deletions(-) create mode 100644 json-unit-assertj/src/main/java/net/javacrumbs/jsonunit/assertj/JsonObjectAssert.java 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 d9c142169..3bf42fbb9 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 @@ -32,7 +32,6 @@ import org.assertj.core.api.BigDecimalAssert; import org.assertj.core.api.BigIntegerAssert; import org.assertj.core.api.BooleanAssert; -import org.assertj.core.api.ListAssert; import org.assertj.core.api.StringAssert; import org.assertj.core.api.UriAssert; import org.assertj.core.description.Description; @@ -181,7 +180,7 @@ private BigDecimalAssert createBigDecimalAssert(BigDecimal value) { * @return */ @NotNull - public ListAssert isArray() { + public JsonListAssert isArray() { Node node = assertType(ARRAY); return new JsonListAssert((List)node.getValue(), path.asPrefix(), configuration) .as("Different value found in node \"%s\"", path); 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 c7864234b..3a7c9c0ea 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 @@ -18,7 +18,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.ListAssert; +import org.assertj.core.api.FactoryBasedNavigableListAssert; import org.assertj.core.error.BasicErrorMessageFactory; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -27,12 +27,12 @@ import static net.javacrumbs.jsonunit.core.internal.JsonUtils.wrapDeserializedObject; -class JsonListAssert extends ListAssert { +public class JsonListAssert extends FactoryBasedNavigableListAssert, Object, JsonObjectAssert> { private final Configuration configuration; private final Path path; JsonListAssert(List actual, Path path, Configuration configuration) { - super(actual); + super(actual, JsonListAssert.class, t -> new JsonObjectAssert(t, path, configuration)); this.path = path; this.configuration = configuration; usingComparator(new JsonComparator(configuration, path, true)); diff --git a/json-unit-assertj/src/main/java/net/javacrumbs/jsonunit/assertj/JsonObjectAssert.java b/json-unit-assertj/src/main/java/net/javacrumbs/jsonunit/assertj/JsonObjectAssert.java new file mode 100644 index 000000000..aa5e22e71 --- /dev/null +++ b/json-unit-assertj/src/main/java/net/javacrumbs/jsonunit/assertj/JsonObjectAssert.java @@ -0,0 +1,144 @@ +/** + * Copyright 2009-2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package net.javacrumbs.jsonunit.assertj; + +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.AbstractObjectAssert; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public class JsonObjectAssert extends AbstractObjectAssert { + private final Configuration configuration; + private final Path path; + + JsonObjectAssert(Object actual, Path path, Configuration configuration) { + super(actual, JsonObjectAssert.class); + this.path = path; + this.configuration = configuration; + usingComparator(new JsonComparator(configuration, path.asPrefix(), true)); + } + + @Override + @NotNull + public JsonObjectAssert isEqualTo(@Nullable Object expected) { + return compare(expected, configuration); + } + + @Override + @NotNull + @Deprecated + public JsonObjectAssert isEqualToIgnoringGivenFields(@Nullable Object other, @NotNull String... propertiesOrFieldsToIgnore) { + return compare(other, configuration.whenIgnoringPaths(propertiesOrFieldsToIgnore)); + } + + @Override + @NotNull + @Deprecated + public JsonObjectAssert isEqualToComparingOnlyGivenFields(@Nullable Object other, @NotNull String... propertiesOrFieldsUsedInComparison) { + throw unsupportedOperation(); + } + + @Override + @NotNull + @Deprecated + public JsonObjectAssert isEqualToIgnoringNullFields(@Nullable Object other) { + throw unsupportedOperation(); + } + + @Override + @NotNull + @Deprecated + public JsonObjectAssert isEqualToComparingFieldByField(@Nullable Object other) { + throw unsupportedOperation(); + } + + @Override + @NotNull + @Deprecated + public JsonObjectAssert isEqualToComparingFieldByFieldRecursively(@Nullable Object other) { + throw unsupportedOperation(); + } + /** + * Does not work. + * https://github.com/lukas-krecan/JsonUnit/issues/324 + */ + @Override + @Deprecated + public JsonObjectAssert hasFieldOrProperty(String name) { + return super.hasFieldOrProperty(name); + } + + /** + * Does not work. + * https://github.com/lukas-krecan/JsonUnit/issues/324 + */ + @Override + @Deprecated + public JsonObjectAssert hasFieldOrPropertyWithValue(String name, Object value) { + return super.hasFieldOrPropertyWithValue(name, value); + } + + /** + * Does not work. https://github.com/lukas-krecan/JsonUnit/issues/324 + */ + @Override + @Deprecated + public JsonObjectAssert hasAllNullFieldsOrProperties() { + return super.hasAllNullFieldsOrProperties(); + } + + /** + * Does not work. https://github.com/lukas-krecan/JsonUnit/issues/324 + */ + @Override + @Deprecated + public JsonObjectAssert hasAllNullFieldsOrPropertiesExcept(String... propertiesOrFieldsToIgnore) { + return super.hasAllNullFieldsOrPropertiesExcept(propertiesOrFieldsToIgnore); + } + + /** + * Does not work. https://github.com/lukas-krecan/JsonUnit/issues/324 + */ + @Deprecated + @Override + public JsonObjectAssert hasNoNullFieldsOrProperties() { + return super.hasNoNullFieldsOrProperties(); + } + + /** + * Does not work. https://github.com/lukas-krecan/JsonUnit/issues/324 + */ + @Override + @Deprecated + public JsonObjectAssert hasNoNullFieldsOrPropertiesExcept(String... propertiesOrFieldsToIgnore) { + return super.hasNoNullFieldsOrPropertiesExcept(propertiesOrFieldsToIgnore); + } + + @NotNull + private UnsupportedOperationException unsupportedOperation() { + return new UnsupportedOperationException("Operation not supported for JSON documents"); + } + + @NotNull + private JsonObjectAssert compare(@Nullable Object other, @NotNull Configuration configuration) { + describedAs(null); + Diff diff = Diff.create(other, actual, "fullJson", path, configuration); + diff.failIfDifferent(); + return this; + } +} 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 5f02911a9..3f86045fe 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 @@ -1617,6 +1617,19 @@ void ignoreExtraArrayItemsAndOrderExample() { .isEqualTo("{\"test\":[1,2,3]}"); } + @Test + void testArrayBug() { + assertThatJson("[\n" + + " {\"value\": \"1\", \"title\": \"Entity\", \"info\": \"Entity info\"},\n" + + " {\"value\": \"2\", \"title\": \"Column\", \"info\": \"Column info\"},\n" + + " {\"value\": \"3\", \"title\": \"Table\", \"info\": \"Table info\"},\n" + + " {\"value\": \"4\", \"title\": \"Schema\", \"info\": \"Schema info\"}\n" + + " ]") + .inPath("$[?(@.value =='1')]") + .isArray().last() + .isEqualTo(json("{\"value\": \"1\", \"title\": \"Entity\", \"info\": \"Entity info\"}")); + } + @Test void testInnerString() { String json = "{\"myNode\":{\"inner\":\"foo\"}}";