Search in sources :

Example 1 with ValueChange

use of org.javers.core.diff.changetype.ValueChange 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 2 with ValueChange

use of org.javers.core.diff.changetype.ValueChange in project tutorials by eugenp.

the class JaversUnitTest method givenPersonObject_whenApplyModificationOnIt_thenShouldDetectChange.

@Test
public void givenPersonObject_whenApplyModificationOnIt_thenShouldDetectChange() {
    // given
    Javers javers = JaversBuilder.javers().build();
    Person person = new Person(1, "Michael Program");
    Person personAfterModification = new Person(1, "Michael Java");
    // when
    Diff diff = javers.compare(person, personAfterModification);
    // then
    ValueChange change = diff.getChangesByType(ValueChange.class).get(0);
    assertThat(diff.getChanges()).hasSize(1);
    assertThat(change.getPropertyName()).isEqualTo("name");
    assertThat(change.getLeft()).isEqualTo("Michael Program");
    assertThat(change.getRight()).isEqualTo("Michael Java");
}
Also used : ValueChange(org.javers.core.diff.changetype.ValueChange) Javers(org.javers.core.Javers) Diff(org.javers.core.diff.Diff) Test(org.junit.Test)

Example 3 with ValueChange

use of org.javers.core.diff.changetype.ValueChange in project jag-pcss-civil by bcgov.

the class TestService method printDiff.

private void printDiff(Diff diff) {
    int diffSize = diff.getChanges().size();
    if (diffSize == 0) {
        return;
    }
    String[] header = new String[] { "Property", "API Response", "WM Response" };
    String[][] table = new String[diffSize + 1][3];
    table[0] = header;
    for (int i = 0; i < diffSize; ++i) {
        Change ch = diff.getChanges().get(i);
        if (ch instanceof ListChange) {
            String apiVal = ((ListChange) ch).getLeft() == null ? "null" : ((ListChange) ch).getLeft().toString();
            String wmVal = ((ListChange) ch).getRight() == null ? "null" : ((ListChange) ch).getRight().toString();
            table[i + 1][0] = ((ListChange) ch).getPropertyNameWithPath();
            table[i + 1][1] = apiVal;
            table[i + 1][2] = wmVal;
        } else if (ch instanceof ValueChange) {
            String apiVal = ((ValueChange) ch).getLeft() == null ? "null" : ((ValueChange) ch).getLeft().toString();
            String wmVal = ((ValueChange) ch).getRight() == null ? "null" : ((ValueChange) ch).getRight().toString();
            table[i + 1][0] = ((ValueChange) ch).getPropertyNameWithPath();
            table[i + 1][1] = apiVal;
            table[i + 1][2] = wmVal;
        }
    }
    boolean leftJustifiedRows = false;
    int totalColumnLength = 10;
    /*
         * Calculate appropriate Length of each column by looking at width of data in
         * each column.
         *
         * Map columnLengths is <column_number, column_length>
         */
    Map<Integer, Integer> columnLengths = new HashMap<>();
    Arrays.stream(table).forEach(a -> Stream.iterate(0, (i -> i < a.length), (i -> ++i)).forEach(i -> {
        if (columnLengths.get(i) == null) {
            columnLengths.put(i, 0);
        }
        if (columnLengths.get(i) < a[i].length()) {
            columnLengths.put(i, a[i].length());
        }
    }));
    for (Map.Entry<Integer, Integer> e : columnLengths.entrySet()) {
        totalColumnLength += e.getValue();
    }
    fileOutput.println("=".repeat(totalColumnLength));
    System.out.println("=".repeat(totalColumnLength));
    final StringBuilder formatString = new StringBuilder("");
    String flag = leftJustifiedRows ? "-" : "";
    columnLengths.entrySet().stream().forEach(e -> formatString.append("| %" + flag + e.getValue() + "s "));
    formatString.append("|\n");
    Stream.iterate(0, (i -> i < table.length), (i -> ++i)).forEach(a -> {
        fileOutput.printf(formatString.toString(), table[a]);
        System.out.printf(formatString.toString(), table[a]);
    });
    fileOutput.println("=".repeat(totalColumnLength));
    System.out.println("=".repeat(totalColumnLength));
}
Also used : ValueChange(org.javers.core.diff.changetype.ValueChange) Diff(org.javers.core.diff.Diff) java.util(java.util) Jaxb2Marshaller(org.springframework.oxm.jaxb.Jaxb2Marshaller) Javers(org.javers.core.Javers) JaversBuilder(org.javers.core.JaversBuilder) Autowired(org.springframework.beans.factory.annotation.Autowired) Instant(java.time.Instant) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) Value(org.springframework.beans.factory.annotation.Value) ValueChange(org.javers.core.diff.changetype.ValueChange) Stream(java.util.stream.Stream) java.io(java.io) WebServiceTemplate(org.springframework.ws.client.core.WebServiceTemplate) Service(org.springframework.stereotype.Service) ListChange(org.javers.core.diff.changetype.container.ListChange) DualProtocolSaajSoapMessageFactory(ca.bc.gov.open.pcss.civil.comparison.config.DualProtocolSaajSoapMessageFactory) YesNoType(ca.bc.gov.open.pcss.two.YesNoType) Change(org.javers.core.diff.Change) WebServiceSenderWithAuth(ca.bc.gov.open.pcss.civil.comparison.config.WebServiceSenderWithAuth) ca.bc.gov.open.pcss.three(ca.bc.gov.open.pcss.three) HttpComponentsMessageSender(org.springframework.ws.transport.http.HttpComponentsMessageSender) SoapVersion(org.springframework.ws.soap.SoapVersion) ValueChange(org.javers.core.diff.changetype.ValueChange) ListChange(org.javers.core.diff.changetype.container.ListChange) Change(org.javers.core.diff.Change) ListChange(org.javers.core.diff.changetype.container.ListChange)

Aggregations

Javers (org.javers.core.Javers)3 Diff (org.javers.core.diff.Diff)3 ValueChange (org.javers.core.diff.changetype.ValueChange)3 ListChange (org.javers.core.diff.changetype.container.ListChange)2 Test (org.junit.Test)2 DualProtocolSaajSoapMessageFactory (ca.bc.gov.open.pcss.civil.comparison.config.DualProtocolSaajSoapMessageFactory)1 WebServiceSenderWithAuth (ca.bc.gov.open.pcss.civil.comparison.config.WebServiceSenderWithAuth)1 ca.bc.gov.open.pcss.three (ca.bc.gov.open.pcss.three)1 YesNoType (ca.bc.gov.open.pcss.two.YesNoType)1 java.io (java.io)1 Instant (java.time.Instant)1 java.util (java.util)1 Stream (java.util.stream.Stream)1 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)1 JaversBuilder (org.javers.core.JaversBuilder)1 Change (org.javers.core.diff.Change)1 ObjectRemoved (org.javers.core.diff.changetype.ObjectRemoved)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 Value (org.springframework.beans.factory.annotation.Value)1 Jaxb2Marshaller (org.springframework.oxm.jaxb.Jaxb2Marshaller)1