|
| 1 | +package com.baeldung.javers; |
| 2 | + |
| 3 | + |
| 4 | +import org.javers.common.collections.Lists; |
| 5 | +import org.javers.core.Javers; |
| 6 | +import org.javers.core.JaversBuilder; |
| 7 | +import org.javers.core.diff.Diff; |
| 8 | +import org.javers.core.diff.changetype.NewObject; |
| 9 | +import org.javers.core.diff.changetype.ObjectRemoved; |
| 10 | +import org.javers.core.diff.changetype.ValueChange; |
| 11 | +import org.javers.core.diff.changetype.container.ListChange; |
| 12 | +import org.junit.Test; |
| 13 | + |
| 14 | +import java.util.Arrays; |
| 15 | +import java.util.Collections; |
| 16 | +import java.util.List; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | + |
| 20 | +public class JaversTest { |
| 21 | + |
| 22 | + @Test |
| 23 | + public void givenPersonObject_whenApplyModificationOnIt_thenShouldDetectChange() { |
| 24 | + //given |
| 25 | + Javers javers = JaversBuilder.javers().build(); |
| 26 | + |
| 27 | + Person person = new Person(1, "Michael Program"); |
| 28 | + Person personAfterModification = new Person(1, "Michael Java"); |
| 29 | + |
| 30 | + //when |
| 31 | + Diff diff = javers.compare(person, personAfterModification); |
| 32 | + |
| 33 | + //then |
| 34 | + ValueChange change = diff.getChangesByType(ValueChange.class).get(0); |
| 35 | + |
| 36 | + assertThat(diff.getChanges()).hasSize(1); |
| 37 | + assertThat(change.getPropertyName()).isEqualTo("name"); |
| 38 | + assertThat(change.getLeft()).isEqualTo("Michael Program"); |
| 39 | + assertThat(change.getRight()).isEqualTo("Michael Java"); |
| 40 | + } |
| 41 | + |
| 42 | + |
| 43 | + @Test |
| 44 | + public void givenListOfPersons_whenCompare_ThenShouldDetectChanges() { |
| 45 | + //given |
| 46 | + Javers javers = JaversBuilder.javers().build(); |
| 47 | + Person personThatWillBeRemoved = new Person(2, "Thomas Link"); |
| 48 | + List<Person> oldList = Lists.asList(new Person(1, "Michael Program"), personThatWillBeRemoved); |
| 49 | + List<Person> newList = Lists.asList(new Person(1, "Michael Not Program")); |
| 50 | + |
| 51 | + //when |
| 52 | + Diff diff = javers.compareCollections(oldList, newList, Person.class); |
| 53 | + |
| 54 | + //then |
| 55 | + assertThat(diff.getChanges()).hasSize(3); |
| 56 | + |
| 57 | + |
| 58 | + ValueChange valueChange = diff.getChangesByType(ValueChange.class).get(0); |
| 59 | + assertThat(valueChange.getPropertyName()).isEqualTo("name"); |
| 60 | + assertThat(valueChange.getLeft()).isEqualTo("Michael Program"); |
| 61 | + assertThat(valueChange.getRight()).isEqualTo("Michael Not Program"); |
| 62 | + |
| 63 | + ObjectRemoved objectRemoved = diff.getChangesByType(ObjectRemoved.class).get(0); |
| 64 | + assertThat(objectRemoved.getAffectedObject().get().equals(personThatWillBeRemoved)).isTrue(); |
| 65 | + |
| 66 | + ListChange listChange = diff.getChangesByType(ListChange.class).get(0); |
| 67 | + assertThat(listChange.getValueRemovedChanges().size()).isEqualTo(1); |
| 68 | + |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void givenListOfPerson_whenPersonHasNewAddress_thenDetectThatChange() { |
| 73 | + //given |
| 74 | + Javers javers = JaversBuilder.javers().build(); |
| 75 | + |
| 76 | + PersonWithAddress person = |
| 77 | + new PersonWithAddress(1, "Tom", Arrays.asList(new Address("England"))); |
| 78 | + |
| 79 | + PersonWithAddress personWithNewAddress = |
| 80 | + new PersonWithAddress(1, "Tom", |
| 81 | + Arrays.asList(new Address("England"), new Address("USA"))); |
| 82 | + |
| 83 | + |
| 84 | + //when |
| 85 | + Diff diff = javers.compare(person, personWithNewAddress); |
| 86 | + List objectsByChangeType = diff.getObjectsByChangeType(NewObject.class); |
| 87 | + |
| 88 | + //then |
| 89 | + assertThat(objectsByChangeType).hasSize(1); |
| 90 | + assertThat(objectsByChangeType.get(0).equals(new Address("USA"))); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void givenListOfPerson_whenPersonRemovedAddress_thenDetectThatChange() { |
| 95 | + //given |
| 96 | + Javers javers = JaversBuilder.javers().build(); |
| 97 | + |
| 98 | + PersonWithAddress person = |
| 99 | + new PersonWithAddress(1, "Tom", Arrays.asList(new Address("England"))); |
| 100 | + |
| 101 | + PersonWithAddress personWithNewAddress = |
| 102 | + new PersonWithAddress(1, "Tom", Collections.emptyList()); |
| 103 | + |
| 104 | + |
| 105 | + //when |
| 106 | + Diff diff = javers.compare(person, personWithNewAddress); |
| 107 | + List objectsByChangeType = diff.getObjectsByChangeType(ObjectRemoved.class); |
| 108 | + |
| 109 | + //then |
| 110 | + assertThat(objectsByChangeType).hasSize(1); |
| 111 | + assertThat(objectsByChangeType.get(0).equals(new Address("England"))); |
| 112 | + } |
| 113 | +} |
0 commit comments