Skip to content

Commit

Permalink
#465 Fix array assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
lukas-krecan committed Feb 3, 2022
1 parent eae4e64 commit 044e9fb
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -181,7 +180,7 @@ private BigDecimalAssert createBigDecimalAssert(BigDecimal value) {
* @return
*/
@NotNull
public ListAssert<Object> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,12 +27,12 @@

import static net.javacrumbs.jsonunit.core.internal.JsonUtils.wrapDeserializedObject;

class JsonListAssert extends ListAssert<Object> {
public class JsonListAssert extends FactoryBasedNavigableListAssert<JsonListAssert, List<?>, 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));
Expand Down
Original file line number Diff line number Diff line change
@@ -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<JsonObjectAssert, Object> {
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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\"}}";
Expand Down

0 comments on commit 044e9fb

Please sign in to comment.