How to use compareUnorderedIterables method of org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator class

Best Assertj code snippet using org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator.compareUnorderedIterables

Source:RecursiveComparisonDifferenceCalculator.java Github

copy

Full Screen

...206 compareOrderedCollections(dualValue, comparisonState);207 continue;208 }209 if (dualValue.isExpectedFieldAnIterable()) {210 compareUnorderedIterables(dualValue, comparisonState);211 continue;212 }213 if (dualValue.isExpectedFieldAnOptional()) {214 compareOptional(dualValue, comparisonState);215 continue;216 }217 // Compare two SortedMaps taking advantage of the fact that these Maps can be compared in O(N) time due to their ordering218 if (dualValue.isExpectedFieldASortedMap()) {219 compareSortedMap(dualValue, comparisonState);220 continue;221 }222 // Compare two Unordered Maps. This is a slightly more expensive comparison because order cannot be assumed, therefore a223 // temporary Map must be created, however the comparison still runs in O(N) time.224 if (dualValue.isExpectedFieldAMap()) {225 compareUnorderedMap(dualValue, comparisonState);226 continue;227 }228 if (shouldCompareDualValue(recursiveComparisonConfiguration, dualValue)) {229 if (!actualFieldValue.equals(expectedFieldValue)) comparisonState.addDifference(dualValue);230 continue;231 }232 Class<?> actualFieldValueClass = actualFieldValue.getClass();233 Class<?> expectedFieldClass = expectedFieldValue.getClass();234 if (recursiveComparisonConfiguration.isInStrictTypeCheckingMode() && expectedTypeIsNotSubtypeOfActualType(dualValue)) {235 comparisonState.addDifference(dualValue, STRICT_TYPE_ERROR, expectedFieldClass.getName(),236 actualFieldValueClass.getName());237 continue;238 }239 Set<String> actualNonIgnoredFieldsNames = recursiveComparisonConfiguration.getNonIgnoredActualFieldNames(dualValue);240 Set<String> expectedFieldsNames = getFieldsNames(expectedFieldClass);241 // Check if expected has more fields than actual, in that case the additional fields are reported as difference242 if (!expectedFieldsNames.containsAll(actualNonIgnoredFieldsNames)) {243 // report missing fields in actual244 Set<String> actualFieldsNamesNotInExpected = newHashSet(actualNonIgnoredFieldsNames);245 actualFieldsNamesNotInExpected.removeAll(expectedFieldsNames);246 String missingFields = actualFieldsNamesNotInExpected.toString();247 String expectedClassName = expectedFieldClass.getName();248 String actualClassName = actualFieldValueClass.getName();249 String missingFieldsDescription = format(MISSING_FIELDS, actualClassName, expectedClassName,250 expectedFieldClass.getSimpleName(), actualFieldValueClass.getSimpleName(),251 missingFields);252 comparisonState.addDifference(dualValue, missingFieldsDescription);253 } else { // TODO remove else to report more diff254 // compare actual's fields against expected :255 // - if actual has more fields than expected, the additional fields are ignored as expected is the reference256 for (String actualFieldName : actualNonIgnoredFieldsNames) {257 if (expectedFieldsNames.contains(actualFieldName)) {258 DualValue newDualValue = new DualValue(currentPath, actualFieldName,259 COMPARISON.getSimpleValue(actualFieldName, actualFieldValue),260 COMPARISON.getSimpleValue(actualFieldName, expectedFieldValue));261 comparisonState.registerForComparison(newDualValue);262 }263 }264 }265 }266 return comparisonState.getDifferences();267 }268 private static boolean shouldCompareDualValue(RecursiveComparisonConfiguration recursiveComparisonConfiguration,269 final DualValue dualValue) {270 return !recursiveComparisonConfiguration.shouldIgnoreOverriddenEqualsOf(dualValue)271 && hasOverriddenEquals(dualValue.actual.getClass());272 }273 // avoid comparing enum recursively since they contain static fields which are ignored in recursive comparison274 // this would make different field enum value to be considered the same!275 private static void compareAsEnums(final DualValue dualValue,276 ComparisonState comparisonState,277 RecursiveComparisonConfiguration recursiveComparisonConfiguration) {278 if (recursiveComparisonConfiguration.isInStrictTypeCheckingMode()) {279 // we can use == for comparison which checks both actual and expected values and types are the same280 if (dualValue.actual != dualValue.expected) comparisonState.addDifference(dualValue);281 return;282 }283 if (!dualValue.isActualAnEnum()) {284 comparisonState.addDifference(dualValue, differentTypeErrorMessage(dualValue, "an enum"));285 return;286 }287 // both actual and expected are enums288 Enum<?> actualEnum = (Enum<?>) dualValue.actual;289 Enum<?> expectedEnum = (Enum<?>) dualValue.expected;290 // we must only compare actual and expected enum by value but not by type291 if (!actualEnum.name().equals(expectedEnum.name())) comparisonState.addDifference(dualValue);292 }293 private static boolean shouldHonorOverriddenEquals(DualValue dualValue,294 RecursiveComparisonConfiguration recursiveComparisonConfiguration) {295 boolean shouldNotIgnoreOverriddenEqualsIfAny = !recursiveComparisonConfiguration.shouldIgnoreOverriddenEqualsOf(dualValue);296 return shouldNotIgnoreOverriddenEqualsIfAny && dualValue.actual != null && hasOverriddenEquals(dualValue.actual.getClass());297 }298 private static void compareArrays(DualValue dualValue, ComparisonState comparisonState) {299 if (!dualValue.isActualFieldAnArray()) {300 // at the moment we only allow comparing arrays with arrays but we might allow comparing to collections later on301 // but only if we are not in strict type mode.302 comparisonState.addDifference(dualValue, differentTypeErrorMessage(dualValue, "an array"));303 return;304 }305 // both values in dualValue are arrays306 int actualArrayLength = Array.getLength(dualValue.actual);307 int expectedArrayLength = Array.getLength(dualValue.expected);308 if (actualArrayLength != expectedArrayLength) {309 comparisonState.addDifference(dualValue, DIFFERENT_SIZE_ERROR, "arrays", actualArrayLength, expectedArrayLength);310 // no need to inspect elements, arrays are not equal as they don't have the same size311 return;312 }313 // register each pair of actual/expected elements for recursive comparison314 List<String> arrayFieldPath = dualValue.getPath();315 for (int i = 0; i < actualArrayLength; i++) {316 Object actualElement = Array.get(dualValue.actual, i);317 Object expectedElement = Array.get(dualValue.expected, i);318 // TODO add [i] to the path ?319 comparisonState.registerForComparison(new DualValue(arrayFieldPath, actualElement, expectedElement));320 }321 }322 /*323 * Deeply compare two Collections that must be same length and in same order.324 */325 private static void compareOrderedCollections(DualValue dualValue, ComparisonState comparisonState) {326 if (!dualValue.isActualFieldAnOrderedCollection()) {327 // at the moment if expected is an ordered collection then actual should also be one328 comparisonState.addDifference(dualValue, ACTUAL_NOT_ORDERED_COLLECTION, dualValue.actual.getClass().getCanonicalName());329 return;330 }331 Collection<?> actualCollection = (Collection<?>) dualValue.actual;332 Collection<?> expectedCollection = (Collection<?>) dualValue.expected;333 if (actualCollection.size() != expectedCollection.size()) {334 comparisonState.addDifference(dualValue, DIFFERENT_SIZE_ERROR,335 "collections", actualCollection.size(), expectedCollection.size());336 // no need to inspect elements, arrays are not equal as they don't have the same size337 return;338 }339 // register pair of elements with same index for later comparison as we compare elements in order340 Iterator<?> expectedIterator = expectedCollection.iterator();341 List<String> path = dualValue.getPath();342 actualCollection.stream()343 .map(element -> new DualValue(path, element, expectedIterator.next()))344 .forEach(comparisonState::registerForComparison);345 }346 private static String differentTypeErrorMessage(DualValue dualValue, String actualTypeDescription) {347 return format(DIFFERENT_ACTUAL_AND_EXPECTED_FIELD_TYPES,348 actualTypeDescription, dualValue.actual.getClass().getCanonicalName());349 }350 private static void compareUnorderedIterables(DualValue dualValue, ComparisonState comparisonState) {351 if (!dualValue.isActualFieldAnIterable()) {352 // at the moment we only compare iterable with iterables (but we might allow arrays too)353 comparisonState.addDifference(dualValue, differentTypeErrorMessage(dualValue, "an iterable"));354 return;355 }356 Iterable<?> actual = (Iterable<?>) dualValue.actual;357 Iterable<?> expected = (Iterable<?>) dualValue.expected;358 int actualSize = sizeOf(actual);359 int expectedSize = sizeOf(expected);360 if (actualSize != expectedSize) {361 comparisonState.addDifference(dualValue, DIFFERENT_SIZE_ERROR, "collections", actualSize, expectedSize);362 // no need to inspect elements, iterables are not equal as they don't have the same size363 return;364 // TODO instead we could register the diff between expected and actual that is:...

Full Screen

Full Screen

compareUnorderedIterables

Using AI Code Generation

copy

Full Screen

1org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator.compareUnorderedIterables(java.lang.Iterable<?>, java.lang.Iterable<?>):[2org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator.compareUnorderedIterables(java.lang.Iterable<?>, java.lang.Iterable<?>):[3org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator.compareUnorderedIterables(java.lang.Iterable<?>, java.lang.Iterable<?>):[4org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator.compareUnorderedIterables(java.lang.Iterable<?>, java.lang.Iterable<?>):[5org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator.compareUnorderedIterables(java.lang.Iterable<?>, java.lang.Iterable<?>):[6org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator.compareUnorderedIterables(java.lang.Iterable<?>, java.lang.Iterable<?>):[7org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator.compareUnorderedIterables(java.lang.Iterable<?>, java.lang.Iterable<?>):[8org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator.compareUnorderedIterables(java.lang.Iterable<?>, java.lang.Iterable<?>):[9org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator.compareUnorderedIterables(java.lang.Iterable<?>, java.lang.Iterable<?>):[10org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator.compareUnorderedIterables(java.lang.Iterable<?>, java.lang.Iterable<?>):[11org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator.compareUnorderedIterables(java.lang.Iterable<?>, java.lang.Iterable<?>):[12org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator.compareUnorderedIterables(java.lang.Iterable<?>, java.lang.Iterable<?>):[13org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator.compareUnorderedIterables(java.lang.Iterable<?>, java.lang.Iterable<?>):[14org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator.compareUnorderedIterables(java.lang.Iterable<?>, java.lang.Iterable<?>):

Full Screen

Full Screen

compareUnorderedIterables

Using AI Code Generation

copy

Full Screen

1import static org.assertj.core.api.Assertions.assertThat;2import static org.assertj.core.api.Assertions.assertThatThrownBy;3import static org.assertj.core.api.Assertions.within;4import static org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator.compareUnorderedIterables;5import java.util.ArrayList;6import java.util.List;7import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifference;8import org.junit.jupiter.api.Test;9class RecursiveComparisonDifferenceCalculatorTest {10 void should_find_difference_in_unordered_iterables() {11 List<String> actual = new ArrayList<>();12 actual.add("Luke");13 actual.add("Leia");14 actual.add("Yoda");15 List<String> expected = new ArrayList<>();16 expected.add("Luke");17 expected.add("Yoda");18 expected.add("Leia");19 List<RecursiveComparisonDifference> differences = compareUnorderedIterables(actual, expected);20 assertThat(differences).hasSize(2);21 assertThat(differences.get(0).toString()).isEqualTo("expected: 'Leia' but was: 'Yoda'");22 assertThat(differences.get(1).toString()).isEqualTo("expected: 'Yoda' but was: 'Leia'");23 }24 void should_find_difference_in_unordered_iterables_with_comparator() {25 List<Double> actual = new ArrayList<>();26 actual.add(1.0);27 actual.add(2.0);28 actual.add(3.0);29 List<Double> expected = new ArrayList<>();30 expected.add(1.0);31 expected.add(3.0);32 expected.add(2.0);33 List<RecursiveComparisonDifference> differences = compareUnorderedIterables(actual, expected, within(0.1));34 assertThat(differences).hasSize(2);35 assertThat(differences.get(0).toString()).isEqualTo("expected: '2.0' but was: '3.0'");36 assertThat(differences.get(1).toString()).isEqualTo("expected: '3.0' but was: '2.0'");37 }38 void should_find_difference_in_unordered_iterables_with_comparator_using_comparator() {39 List<Double> actual = new ArrayList<>();40 actual.add(1.0);41 actual.add(2.0);42 actual.add(3.0);43 List<Double> expected = new ArrayList<>();44 expected.add(1.0);45 expected.add(3.0);

Full Screen

Full Screen

compareUnorderedIterables

Using AI Code Generation

copy

Full Screen

1package org.assertj.core.api.recursive.comparison;2import org.assertj.core.api.Assertions;3import org.assertj.core.util.Lists;4import org.assertj.core.util.Maps;5import org.junit.Test;6import java.util.Arrays;7import java.util.List;8import java.util.Map;9public class RecursiveComparisonDifferenceCalculatorTest {10 public void testCompareUnorderedIterables() {11 List<Map<String, Object>> actual = Lists.newArrayList();12 Map<String, Object> map1 = Maps.newHashMap("a", "b");13 Map<String, Object> map2 = Maps.newHashMap("c", "d");14 actual.add(map1);15 actual.add(map2);16 List<Map<String, Object>> expected = Lists.newArrayList();17 Map<String, Object> map3 = Maps.newHashMap("c", "d");18 Map<String, Object> map4 = Maps.newHashMap("a", "b");19 expected.add(map3);20 expected.add(map4);21 RecursiveComparisonDifferenceCalculator calculator = new RecursiveComparisonDifferenceCalculator();22 List<RecursiveComparisonDifference> differences = calculator.compareUnorderedIterables(actual, expected, Arrays.asList("a", "c"));23 Assertions.assertThat(differences).isEmpty();24 }25}

Full Screen

Full Screen

compareUnorderedIterables

Using AI Code Generation

copy

Full Screen

1org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator.compareUnorderedIterables(Iterable<? extends Object> expectedIterable, Iterable<? extends Object> actualIterable, RecursiveComparisonConfiguration recursiveComparisonConfiguration, RecursiveComparisonDifference difference) 2org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator.compareIterables(Iterable<? extends Object> expectedIterable, Iterable<? extends Object> actualIterable, RecursiveComparisonConfiguration recursiveComparisonConfiguration, RecursiveComparisonDifference difference) 3org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator.compareObjects(Object expectedObject, Object actualObject, RecursiveComparisonConfiguration recursiveComparisonConfiguration, RecursiveComparisonDifference difference) 4org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator.compareMaps(Map<?, ?> expectedMap, Map<?, ?> actualMap, RecursiveComparisonConfiguration recursiveComparisonConfiguration, RecursiveComparisonDifference difference) 5org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator.compareEntries(Map.Entry<?, ?> expectedEntry, Map.Entry<?, ?> actualEntry, RecursiveComparisonConfiguration recursiveComparisonConfiguration, RecursiveComparisonDifference difference) 6org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator.compareValues(Object expectedValue, Object actualValue, RecursiveComparisonConfiguration recursiveComparisonConfiguration, RecursiveComparisonDifference difference) 7org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator.compareArrays(Object expectedArray, Object actualArray, RecursiveComparisonConfiguration recursiveComparisonConfiguration, RecursiveComparisonDifference difference) 8org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator.compareUnorderedArrays(Object expectedArray, Object actualArray, RecursiveComparisonConfiguration recursiveComparisonConfiguration, RecursiveComparisonDifference difference)

Full Screen

Full Screen

compareUnorderedIterables

Using AI Code Generation

copy

Full Screen

1import static org.assertj.core.api.Assertions.assertThat;2import static org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator.compareUnorderedIterables;3import java.util.List;4import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifference;5public class Test {6 public static void main(String[] args) {7 List<Employee> actual = List.of(8 new Employee("John", 123),9 new Employee("Jane", 456)10 );11 List<Employee> expected = List.of(12 new Employee("Jane", 456),13 new Employee("John", 123)14 );15 List<RecursiveComparisonDifference> differences = compareUnorderedIterables(actual, expected);16 assertThat(differences).isEmpty();17 }18 public static class Employee {19 private String name;20 private int id;21 public Employee(String name, int id) {22 this.name = name;23 this.id = id;24 }25 public String getName() {26 return name;27 }28 public int getId() {29 return id;30 }31 public String toString() {32 return "Employee{" +33 '}';34 }35 }36}

Full Screen

Full Screen

compareUnorderedIterables

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator;2import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifference;3import java.util.*;4public class CompareUnorderableIterables {5 public static void main(String[] args) {6 List<Integer> list1 = Arrays.asList(1, 2, 3);7 List<Integer> list2 = Arrays.asList(3, 2, 1);8 RecursiveComparisonDifferenceCalculator recursiveComparisonDifferenceCalculator = new RecursiveComparisonDifferenceCalculator();9 RecursiveComparisonDifference recursiveComparisonDifference = recursiveComparisonDifferenceCalculator.compareUnorderedIterables(list1, list2);10 System.out.println(recursiveComparisonDifference);11 }12}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful