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

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

Source:RecursiveComparisonDifferenceCalculator.java Github

copy

Full Screen

...202 // we compare ordered collections specifically as to be matching, each pair of elements at a given index must match.203 // concretely we compare: (col1[0] vs col2[0]), (col1[1] vs col2[1])...(col1[n] vs col2[n])204 if (dualValue.isExpectedFieldAnOrderedCollection()205 && !recursiveComparisonConfiguration.shouldIgnoreCollectionOrder(dualValue)) {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 order...

Full Screen

Full Screen

compareOrderedCollections

Using AI Code Generation

copy

Full Screen

1package com.baeldung.assertj;2import static org.assertj.core.api.Assertions.assertThat;3import java.util.ArrayList;4import java.util.List;5import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator;6import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator.Difference;7import org.junit.Test;8public class AssertJRecursiveComparisonUnitTest {9 public void givenCollections_whenCompareOrderedCollections_thenCorrect() {10 List<String> list1 = new ArrayList<>();11 list1.add("A");12 list1.add("B");13 list1.add("C");14 List<String> list2 = new ArrayList<>();15 list2.add("A");16 list2.add("B");17 list2.add("D");18 List<Difference> diff = RecursiveComparisonDifferenceCalculator.compareOrderedCollections(list1, list2);19 assertThat(diff).hasSize(1);20 assertThat(diff.get(0).getActual()).isEqualTo("C");21 assertThat(diff.get(0).getExpected()).isEqualTo("D");22 }23}

Full Screen

Full Screen

compareOrderedCollections

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.api.recursive.comparison.*;2import org.assertj.core.api.recursive.comparison.Difference.*;3import org.assertj.core.api.recursive.comparison.FieldLocation.*;4import org.assertj.core.api.recursive.comparison.FieldLocationContext.*;5import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifference.*;6import static org.assertj.core.api.Assertions.*;7import static org.assertj.core.api.recursive.comparison.RecursiveComparisonDifference.*;8import java.util.*;9public class RecursiveComparisonDifferenceCalculatorTest {10 public static void main(String[] args) {11 List<String> expected = Arrays.asList("a", "b", "c");12 List<String> actual = Arrays.asList("a", "d", "c");13 List<RecursiveComparisonDifference> differences = RecursiveComparisonDifferenceCalculator.compareOrderedCollections(expected, actual);14 assertThat(differences).containsExactly(15 new RecursiveComparisonDifference("b", "d", new FieldLocation(new FieldLocationContext(1, FieldLocationContextType.INDEX), "b", "d"))16 );17 }18}19[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ assertj-core-recursive-comparison ---20[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ assertj-core-recursive-comparison ---21[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ assertj-core-recursive-comparison ---22[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ assertj-core-recursive-comparison ---

Full Screen

Full Screen

compareOrderedCollections

Using AI Code Generation

copy

Full Screen

1package org.assertj.core.api.recursive.comparison;2import static org.assertj.core.api.Assertions.assertThat;3import java.util.ArrayList;4import java.util.List;5import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifference;6import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator;7import org.junit.Test;8public class RecursiveComparisonDifferenceCalculatorTest {9 public void testRecursiveComparisonDifferenceCalculator() {10 List<Integer> list1 = new ArrayList<>();11 list1.add(1);12 list1.add(2);13 List<Integer> list2 = new ArrayList<>();14 list2.add(1);15 list2.add(3);16 RecursiveComparisonDifferenceCalculator.compareOrderedCollections(list1, list2);17 assertThat(differences).hasSize(1);18 assertThat(differences.get(0).toString()).isEqualTo("index: 1, expected: 2 but was: 3");19 }20}21org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculatorTest > testRecursiveComparisonDifferenceCalculator() PASSED22public static List<RecursiveComparisonDifference> compareOrderedCollections(Iterable<?> expected, Iterable<?> actual)23public class RecursiveComparisonDifference {24 private final int index;25 private final Object expectedValue;26 private final Object actualValue;27 public String toString() {28 return String.format("index: %d, expected: %s but was: %s", index, expectedValue, actualValue);29 }30}

Full Screen

Full Screen

compareOrderedCollections

Using AI Code Generation

copy

Full Screen

1List<String> list1 = Arrays.asList("A", "B", "C");2List<String> list2 = Arrays.asList("A", "B", "C");3RecursiveComparisonDifferenceCalculator recursiveComparisonDifferenceCalculator = new RecursiveComparisonDifferenceCalculator();4List<RecursiveComparisonDifference> differences = recursiveComparisonDifferenceCalculator.compareOrderedCollections(list1, list2);5System.out.println(differences);6package org.kodejava.example.assertj;7import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifference;8import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator;9import java.util.Arrays;10import java.util.List;11public class RecursiveComparisonDifferenceCalculatorDemo2 {12 public static void main(String[] args) {13List<String> list1 = Arrays.asList("A", "B", "C");14List<String> list2 = Arrays.asList("A", "B", "C");15RecursiveComparisonDifferenceCalculator recursiveComparisonDifferenceCalculator = new RecursiveComparisonDifferenceCalculator();16List<RecursiveComparisonDifference> differences = recursiveComparisonDifferenceCalculator.compare(list1, list2);17System.out.println(differences);

Full Screen

Full Screen

compareOrderedCollections

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.api.recursive.comparison.*2import org.assertj.core.util.introspection.IntrospectionError3import org.assertj.core.util.introspection.Introspector4def recursiveComparisonConfiguration = RecursiveComparisonConfiguration.builder()5 .withIgnoredFields("id", "createdOn", "modifiedOn")6 .withIgnoredOverriddenEqualsForTypes(Instant, OffsetDateTime, UUID)7 .withIgnoredActualNullFields(true)8 .withIgnoredExpectedNullFields(true)9 .withExpectedTypeFilter(RecursiveComparisonConfiguration.ExpectedTypeFilter.INCLUDE)10 .withActualTypeFilter(RecursiveComparisonConfiguration.ActualTypeFilter.INCLUDE)11 .withOverriddenEqualsExclusionPredicate({ true })12 .withStrictTypeChecking(false)13 .withStrictDateComparison(false)14 .withStrictNumberComparison(false)15 .withStrictOrdering(false)16 .withIgnoredFieldsForTypes([String: ["id", "createdOn", "modifiedOn"]])17 .withIgnoredFieldsForTypes([Instant: ["id", "createdOn", "modifiedOn"]])18 .withIgnoredFieldsForTypes([OffsetDateTime: ["id", "createdOn", "modifiedOn"]])19 .withIgnoredFieldsForTypes([UUID: ["id", "createdOn", "modifiedOn"]])20 .build()21def recursiveComparisonDifferenceCalculator = RecursiveComparisonDifferenceCalculator.builder()22 .withRecursiveComparisonConfiguration(recursiveComparisonConfiguration)23 .build()24def compareOrderedCollections = { a, b ->25 def differences = recursiveComparisonDifferenceCalculator.compareOrderedCollections(a, b)26 if (differences.isEmpty()) {27 }28 throw new ComparisonFailure("Expected: $b but was: $a", b.toString(), a.toString())29}30def compareUnorderedCollections = { a, b ->31 def differences = recursiveComparisonDifferenceCalculator.compareUnorderedCollections(a, b)32 if (differences.isEmpty()) {33 }34 throw new ComparisonFailure("Expected: $b but was: $a", b.toString(), a.toString())35}36def compare = { a, b ->37 def differences = recursiveComparisonDifferenceCalculator.compare(a, b)38 if (differences.isEmpty()) {39 }40 throw new ComparisonFailure("Expected: $b but was: $a", b.toString(), a.toString())41}42def compareWithStrictTypeChecking = { a, b ->

Full Screen

Full Screen

compareOrderedCollections

Using AI Code Generation

copy

Full Screen

1RecursiveComparisonDifferenceCalculator calculator = new RecursiveComparisonDifferenceCalculator();2List<RecursiveComparisonDifference> differences = calculator.compareOrderedCollections(3 Arrays.asList("1", "2", "3"), Arrays.asList("1", "3", "2"));4assertThat(differences).hasSize(1);5assertThat(differences.get(0).getActual()).isEqualTo("2");6assertThat(differences.get(0).getExpected()).isEqualTo("3");7assertThat(differences.get(0).getComparison()).isEqualTo(Comparison.DIFFERENT);8assertThat(differences.get(0).getRecursiveComparisonDifference()).isNull();9assertThat(differences.get(0).getRecursiveComparisonDifference()).isNull();10assertThat(differences.get(0).getRecursiveComparisonDifference()).isNull();11RecursiveComparisonDifferenceCalculator calculator = new RecursiveComparisonDifferenceCalculator();12List<RecursiveComparisonDifference> differences = calculator.compareOrderedCollections(13 Arrays.asList("1", "2", "3"), Arrays.asList("1", "3", "2"));14assertThat(differences).hasSize(1);15assertThat(differences.get(0).getActual()).isEqualTo("2");16assertThat(differences.get(0).getExpected()).isEqualTo("3");17assertThat(differences.get(0).getComparison()).isEqualTo(Comparison.DIFFERENT);18assertThat(differences.get(0).getRecursiveComparisonDifference()).isNull();19assertThat(differences.get(0).getRecursiveComparisonDifference()).isNull();20assertThat(differences.get(0).getRecursiveComparisonDifference()).isNull();21RecursiveComparisonDifferenceCalculator calculator = new RecursiveComparisonDifferenceCalculator();22List<RecursiveComparisonDifference> differences = calculator.compareOrderedCollections(23 Arrays.asList("1", "2", "3"), Arrays.asList("1", "3", "2"));24assertThat(differences).hasSize(1);25assertThat(differences.get(0).getActual()).isEqualTo("2");26assertThat(differences.get(0).getExpected()).isEqualTo("3");27assertThat(differences.get(0).getComparison()).isEqualTo(Comparison.DIFFERENT);28assertThat(differences.get(0).getRecursiveComparisonDifference()).isNull();29assertThat(differences.get(0).getRecursiveComparisonDifference()).isNull();30assertThat(differences.get(0).get

Full Screen

Full Screen

compareOrderedCollections

Using AI Code Generation

copy

Full Screen

1List<String> firstOrderedCollection = new ArrayList<>();2firstOrderedCollection.add("first");3firstOrderedCollection.add("second");4List<String> secondOrderedCollection = new ArrayList<>();5secondOrderedCollection.add("second");6secondOrderedCollection.add("first");7RecursiveComparisonDifference recursiveComparisonDifference = RecursiveComparisonDifferenceCalculator.compareOrderedCollections(firstOrderedCollection, secondOrderedCollection);8List<RecursiveComparisonDifference> differences = recursiveComparisonDifference.getDifferences();9differences.stream().forEach(difference -> System.out.println(difference));10List<String> firstUnorderedCollection = new ArrayList<>();11firstUnorderedCollection.add("first");12firstUnorderedCollection.add("second");13List<String> secondUnorderedCollection = new ArrayList<>();14secondUnorderedCollection.add("second");15secondUnorderedCollection.add("first");16RecursiveComparisonDifference recursiveComparisonDifference = RecursiveComparisonDifferenceCalculator.compareUnorderedCollections(firstUnorderedCollection, secondUnorderedCollection);17List<RecursiveComparisonDifference> differences = recursiveComparisonDifference.getDifferences();18differences.stream().forEach(difference -> System.out.println(difference));19Map<String, String> firstMap = new HashMap<>();20firstMap.put("first", "first");21firstMap.put("second", "second");22Map<String, String> secondMap = new HashMap<>();23secondMap.put("second", "second");24secondMap.put("first", "first");25RecursiveComparisonDifference recursiveComparisonDifference = RecursiveComparisonDifferenceCalculator.compareMaps(firstMap, secondMap);26List<RecursiveComparisonDifference> differences = recursiveComparisonDifference.getDifferences();27differences.stream().forEach(difference

Full Screen

Full Screen

compareOrderedCollections

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator;2import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifference;3import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceType;4import java.util.ArrayList;5import java.util.List;6public class CompareTwoCollections {7 public static void main(String[] args) {8 List<Integer> list1 = new ArrayList<>();9 list1.add(1);10 list1.add(2);11 list1.add(3);12 List<Integer> list2 = new ArrayList<>();13 list2.add(2);14 list2.add(1);15 list2.add(3);16 List<RecursiveComparisonDifference> differences = RecursiveComparisonDifferenceCalculator.compareOrderedCollections(list1, list2);17 System.out.println("differences: " + differences);18 differences.forEach(difference -> {19 System.out.println("difference type: " + difference.getType());20 System.out.println("difference path: " + difference.getPath());21 System.out.println("difference actual: " + difference.getActual());22 System.out.println("difference expected: " + difference.getExpected());23 });24 }25}

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