Search in sources :

Example 1 with Javers

use of org.javers.core.Javers in project backend by CatalogueOfLife.

the class ResourceTestBase method printDiff.

protected void printDiff(Object o1, Object o2) {
    Javers javers = JaversBuilder.javers().build();
    Diff diff = javers.compare(o1, o2);
    System.out.println(diff);
}
Also used : Javers(org.javers.core.Javers) Diff(org.javers.core.diff.Diff)

Example 2 with Javers

use of org.javers.core.Javers in project tutorials by eugenp.

the class JaversUnitTest method givenListOfPersons_whenCompare_ThenShouldDetectChanges.

@Test
public void givenListOfPersons_whenCompare_ThenShouldDetectChanges() {
    // given
    Javers javers = JaversBuilder.javers().build();
    Person personThatWillBeRemoved = new Person(2, "Thomas Link");
    List<Person> oldList = Lists.asList(new Person(1, "Michael Program"), personThatWillBeRemoved);
    List<Person> newList = Lists.asList(new Person(1, "Michael Not Program"));
    // when
    Diff diff = javers.compareCollections(oldList, newList, Person.class);
    // then
    assertThat(diff.getChanges()).hasSize(3);
    ValueChange valueChange = diff.getChangesByType(ValueChange.class).get(0);
    assertThat(valueChange.getPropertyName()).isEqualTo("name");
    assertThat(valueChange.getLeft()).isEqualTo("Michael Program");
    assertThat(valueChange.getRight()).isEqualTo("Michael Not Program");
    ObjectRemoved objectRemoved = diff.getChangesByType(ObjectRemoved.class).get(0);
    assertThat(objectRemoved.getAffectedObject().get().equals(personThatWillBeRemoved)).isTrue();
    ListChange listChange = diff.getChangesByType(ListChange.class).get(0);
    assertThat(listChange.getValueRemovedChanges().size()).isEqualTo(1);
}
Also used : ValueChange(org.javers.core.diff.changetype.ValueChange) Javers(org.javers.core.Javers) Diff(org.javers.core.diff.Diff) ObjectRemoved(org.javers.core.diff.changetype.ObjectRemoved) ListChange(org.javers.core.diff.changetype.container.ListChange) Test(org.junit.Test)

Example 3 with Javers

use of org.javers.core.Javers in project tutorials by eugenp.

the class JaversUnitTest method givenListOfPerson_whenPersonRemovedAddress_thenDetectThatChange.

@Test
public void givenListOfPerson_whenPersonRemovedAddress_thenDetectThatChange() {
    // given
    Javers javers = JaversBuilder.javers().build();
    PersonWithAddress person = new PersonWithAddress(1, "Tom", Arrays.asList(new Address("England")));
    PersonWithAddress personWithNewAddress = new PersonWithAddress(1, "Tom", Collections.emptyList());
    // when
    Diff diff = javers.compare(person, personWithNewAddress);
    List objectsByChangeType = diff.getObjectsByChangeType(ObjectRemoved.class);
    // then
    assertThat(objectsByChangeType).hasSize(1);
    assertThat(objectsByChangeType.get(0).equals(new Address("England")));
}
Also used : Javers(org.javers.core.Javers) Diff(org.javers.core.diff.Diff) List(java.util.List) Test(org.junit.Test)

Example 4 with Javers

use of org.javers.core.Javers in project concord by walmartlabs.

the class DiffUtils method compare.

public static Map<String, Object> compare(Object left, Object right) {
    Javers javers = JaversBuilder.javers().build();
    Diff diff = javers.compare(left, right);
    CustomChangeProcessor changeProcessor = new CustomChangeProcessor();
    javers.processChangeList(diff.getChanges(), changeProcessor);
    Map<String, Object> m = changeProcessor.result();
    removeIfEmpty(m, KEY_PREVIOUS);
    removeIfEmpty(m, KEY_NEW);
    return m;
}
Also used : Javers(org.javers.core.Javers) Diff(org.javers.core.diff.Diff)

Example 5 with Javers

use of org.javers.core.Javers in project tutorials by eugenp.

the class JaversUnitTest method givenListOfPerson_whenPersonHasNewAddress_thenDetectThatChange.

@Test
public void givenListOfPerson_whenPersonHasNewAddress_thenDetectThatChange() {
    // given
    Javers javers = JaversBuilder.javers().build();
    PersonWithAddress person = new PersonWithAddress(1, "Tom", Arrays.asList(new Address("England")));
    PersonWithAddress personWithNewAddress = new PersonWithAddress(1, "Tom", Arrays.asList(new Address("England"), new Address("USA")));
    // when
    Diff diff = javers.compare(person, personWithNewAddress);
    List objectsByChangeType = diff.getObjectsByChangeType(NewObject.class);
    // then
    assertThat(objectsByChangeType).hasSize(1);
    assertThat(objectsByChangeType.get(0).equals(new Address("USA")));
}
Also used : Javers(org.javers.core.Javers) Diff(org.javers.core.diff.Diff) List(java.util.List) Test(org.junit.Test)

Aggregations

Javers (org.javers.core.Javers)9 Diff (org.javers.core.diff.Diff)9 Test (org.junit.Test)5 List (java.util.List)2 ValueChange (org.javers.core.diff.changetype.ValueChange)2 Taxon (life.catalogue.api.model.Taxon)1 ObjectRemoved (org.javers.core.diff.changetype.ObjectRemoved)1 ListChange (org.javers.core.diff.changetype.container.ListChange)1