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);
}
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");
}
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));
}
Aggregations