realqmaster
12/9/2019 - 10:02 PM

Detect removed & changed with Javers

    Javers javers = JaversBuilder.javers().build();
    Diff diff = javers.compare(a1, a5);
    Set<Object> toBeArchived = new HashSet<>();

    for (ChangesByObject change : diff.groupByObject()) {
      if (change.getPropertyChanges().size() > 0) {
        change
            .getPropertyChanges()
            .forEach(p -> p.getAffectedObject().ifPresent(o -> toBeArchived.add(o)));
      }

      if (change.getObjectsRemoved().size() > 0) {
        change
            .getObjectsRemoved()
            .forEach(p -> p.getAffectedObject().ifPresent(o -> toBeArchived.add(o)));
      }      
    }
    
    assertEquals(3, toBeArchived.size());
    
    Map<String, List<Object>> groupedList =
    toBeArchived.stream().collect(Collectors.groupingBy(o -> o.getClass().getSimpleName()));

    for (Entry<String, List<Object>> sublist : groupedList.entrySet()) {
      System.out.println(sublist.getValue().size() + " " + sublist.getKey() + " must be archived");
    }