Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ignoringFields with different map size #3003

Open
wants to merge 1 commit into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import static java.lang.String.format;
import static java.util.Objects.deepEquals;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toMap;
import static org.assertj.core.api.recursive.comparison.ComparisonDifference.rootComparisonDifference;
import static org.assertj.core.api.recursive.comparison.DualValue.DEFAULT_ORDERED_COLLECTION_TYPES;
import static org.assertj.core.api.recursive.comparison.FieldLocation.rootFieldLocation;
Expand Down Expand Up @@ -44,7 +45,6 @@
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.atomic.AtomicReferenceArray;
import java.util.stream.Stream;

import org.assertj.core.internal.DeepDifference;

/**
Expand Down Expand Up @@ -506,9 +506,9 @@ private static <K, V> void compareSortedMap(DualValue dualValue, ComparisonState
return;
}

Map<?, ?> actualMap = (Map<?, ?>) dualValue.actual;
@SuppressWarnings("unchecked")
Map<K, V> expectedMap = (Map<K, V>) dualValue.expected;
Map<?, ?> actualMap = filterIgnoringFields((Map<?, ?>)dualValue.actual, comparisonState);
Map<K, V> expectedMap = (Map<K, V>) filterIgnoringFields((Map<?, ?>) dualValue.expected, comparisonState);

if (actualMap.size() != expectedMap.size()) {
comparisonState.addDifference(dualValue, format(DIFFERENT_SIZE_ERROR, "sorted maps", actualMap.size(), expectedMap.size()));
// no need to inspect entries, maps are not equal as they don't have the same size
Expand All @@ -535,8 +535,10 @@ private static void compareUnorderedMap(DualValue dualValue, ComparisonState com
return;
}

Map<?, ?> actualMap = (Map<?, ?>) dualValue.actual;
Map<?, ?> expectedMap = (Map<?, ?>) dualValue.expected;
Set<String> ignoredFields = comparisonState.recursiveComparisonConfiguration.getIgnoredFields();
Map<?, ?> actualMap = filterIgnoringFields((Map<?, ?>) dualValue.actual, comparisonState);
Map<?, ?> expectedMap = filterIgnoringFields((Map<?, ?>) dualValue.expected, comparisonState);

if (actualMap.size() != expectedMap.size()) {
comparisonState.addDifference(dualValue, format(DIFFERENT_SIZE_ERROR, "maps", actualMap.size(), expectedMap.size()));
// no need to inspect entries, maps are not equal as they don't have the same size
Expand All @@ -557,6 +559,15 @@ private static void compareUnorderedMap(DualValue dualValue, ComparisonState com
}
}

private static Map<?, ?> filterIgnoringFields(Map<?, ?> dualValue, ComparisonState comparisonState) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's rename dualValue to map since this is not a dualvalue

Set<String> ignoredFields = comparisonState.recursiveComparisonConfiguration.getIgnoredFields();
if(ignoredFields.isEmpty()){
return dualValue;
}
return dualValue.entrySet()
.stream().filter(e -> !ignoredFields.contains(e.getKey()))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename e to entry

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we are comparing the map key interpreted as field against the ignored fields, this is not entirely correct because we need to compare the field location (FieldLocation) to the ignored field.

Let's say we want to ignore the person.friends.address field, the code would only compare address so if it turns out that the root object as an address field then this will be ignored when it should not.

Moreover the recursive comparison supports ignoring fields by regex.

Have a look at matchesAnIgnoredFieldRegex and matchesAnIgnoredField in AbstractRecursiveOperationConfiguration

We should also add test cases in where we ignore for maps:

  • fields by regex
  • nested fields

.collect(toMap(Map.Entry::getKey, Map.Entry::getValue));}

private static FieldLocation keyFieldLocation(FieldLocation parentFieldLocation, Object key) {
return key == null ? parentFieldLocation : parentFieldLocation.field(key.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
*/
package org.assertj.core.api.recursive.comparison;

import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThat;
import static com.google.common.collect.ImmutableSortedMap.of;import static java.lang.String.format;
import static java.util.Collections.singletonMap;import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.BDDAssertions.entry;
import static org.assertj.core.api.BDDAssertions.then;
import static org.assertj.core.api.recursive.comparison.Color.BLUE;
Expand All @@ -37,11 +37,11 @@
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
import java.util.SortedMap;import java.util.stream.Stream;

import javax.xml.datatype.DatatypeFactory;

import org.assertj.core.api.RecursiveComparisonAssert_isEqualTo_BaseTest;
import com.google.common.collect.Maps;import org.assertj.core.api.Assertions;import org.assertj.core.api.RecursiveComparisonAssert_isEqualTo_BaseTest;
import org.assertj.core.internal.objects.data.AlwaysEqualPerson;
import org.assertj.core.internal.objects.data.FriendlyPerson;
import org.assertj.core.internal.objects.data.Giant;
Expand Down Expand Up @@ -564,8 +564,8 @@ void should_not_introspect_java_base_classes(Object actual, Object expected, @Su

private static Stream<Arguments> should_not_introspect_java_base_classes() throws Exception {

return Stream.of(arguments(DatatypeFactory.newInstance().newXMLGregorianCalendar(),
DatatypeFactory.newInstance().newXMLGregorianCalendar(),
return Stream.of(arguments(DatatypeFactory.newInstance().newXMLGregorianCalendar(),
DatatypeFactory.newInstance().newXMLGregorianCalendar(),
"com.sun.org.apache.xerces.internal.jaxp.datatype.XMLGregorianCalendarImpl"),
arguments(InetAddress.getByName("127.0.0.1"),
InetAddress.getByName("127.0.0.1"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@
package org.assertj.core.api.recursive.comparison;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.util.Arrays.array;
import static org.assertj.core.api.AssertionsForClassTypes.entry;import static org.assertj.core.test.Maps.mapOf;import static org.assertj.core.util.Arrays.array;
import static org.assertj.core.util.Lists.list;
import static org.junit.jupiter.params.provider.Arguments.arguments;

import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.Map;import java.util.Optional;
import java.util.OptionalDouble;
import java.util.OptionalInt;
import java.util.OptionalLong;
import java.util.stream.Stream;

import org.assertj.core.api.RecursiveComparisonAssert_isEqualTo_BaseTest;
import org.assertj.core.internal.objects.data.Address;
import org.assertj.core.data.MapEntry;import org.assertj.core.internal.objects.data.Address;
import org.assertj.core.internal.objects.data.Giant;
import org.assertj.core.internal.objects.data.Human;
import org.assertj.core.internal.objects.data.Person;
Expand Down Expand Up @@ -281,6 +281,56 @@ private static Stream<Arguments> recursivelyEqualObjectsIgnoringGivenFields() {
list("neighbour.neighbour.home.address.number", "neighbour.name")));
}

@SuppressWarnings("unused")
@ParameterizedTest(name = "{2}: actual={0} / expected={1} / ignored fields={3}")
@MethodSource("recursivelyEqualMapsWithVariousSizesIgnoringGivenFields")
void should_pass_for_maps_with_the_various_sizes_when_given_fields_are_ignored(Object actual,
Object expected,
String testDescription,
List<String> ignoredFields) {
assertThat(actual).usingRecursiveComparison()
.ignoringFields(arrayOf(ignoredFields))
.isEqualTo(expected);
}

private static Stream<Arguments> recursivelyEqualMapsWithVariousSizesIgnoringGivenFields() {
return Stream.of(
arguments(mapOf(
entry("firstName", "John"),
entry("lastName", "Doe")
), mapOf(
entry("firstName", "John"),
entry("lastName", "Wick")), "maps with same size, one common field ignored", list("lastName")),

arguments(mapOf(
entry("firstName", "John"),
entry("lastName", "Doe")
), mapOf(
entry("firstName", "John"),
entry("lastName", "Doe"),
entry("age", "25")
), "maps without same size, one ignored field 'age' added", list("age")),

arguments(mapOf(
entry("firstName", "John"),
entry("lastName", "Doe")
), mapOf(
entry("firstName", "John"),
entry("lastName", "Wick"),
entry("age", "25")
), "maps without same size, two ignored fields 'age', 'lastName' added", list("age", "lastName")),

arguments(mapOf(
entry("firstName", "John"),
entry("lastName", "Doe")
), mapOf(
entry("firstName", "John"),
entry("lastName", "Wick"),
entry("age", "25")
), "maps without same size, all field ignored", list("age", "lastName", "firstName"))
);
}

@Test
void should_fail_when_actual_differs_from_expected_even_when_some_fields_are_ignored() {
// GIVEN
Expand Down