UserUpdater { void updateUserData(@NonNull Collection<LdapUserNode> nodes) { var userData = nodes.stream() .map(node -> { var user = repoUserService.getOrCreateUserById(node.getId()); return new UserData(user, node.getGivenName(), node.getSurname(), node.getMail(), node.getDivision()); }) .distinct() .collect(Collectors.toList()); normalizeAndSaveUserData(userData); } UserUpdater(HierarchyTreeNodeRepository hierarchyTreeNodeRepo,
                       RepositoryUserService repoUserService,
                       UserDataRepository userDataRepo,
                       LdapAdapter ldapAdapter,
                       List<Normalizer<UserData>> normalizers); void updateHierarchyAndUserData();  }
@Test public void updateUserDataDeletesAllAndSavesNew() { var firstUserId = "first-user"; var expectedFirstDepartment = "first-department"; var expectedFirstDivision = "first-division"; var expectedFirstGivenName = "first-given-name"; var expectedFirstSurname = "first-surname"; var expectedFirstName = "first-full-name"; var expectedFirstMail = "first@mail.com"; var secondUserId = "second-user"; var expectedSecondDepartment = "second-department"; var expectedSecondDivision = "second-division"; var expectedSecondGivenName = "second-given-name"; var expectedSecondSurname = "second-surname"; var expectedSecondName = "second-full-name"; var expectedSecondMail = "second@mail.com"; var firstUser = new User(firstUserId); var secondUser = new User(secondUserId); var firstNode = new LdapUserNode() .setId(firstUserId) .setDepartment(expectedFirstDepartment) .setDivision(expectedFirstDivision) .setGivenName(expectedFirstGivenName) .setSurname(expectedFirstSurname) .setName(expectedFirstName) .setMail(expectedFirstMail); var secondNode = new LdapUserNode() .setId(secondUserId) .setDepartment(expectedSecondDepartment) .setDivision(expectedSecondDivision) .setGivenName(expectedSecondGivenName) .setSurname(expectedSecondSurname) .setName(expectedSecondName) .setMail(expectedSecondMail); var firstExpected = new UserData(firstUser, expectedFirstGivenName, expectedFirstSurname, expectedFirstMail, expectedFirstDivision); var secondExpected = new UserData(secondUser, expectedSecondGivenName, expectedSecondSurname, expectedSecondMail, expectedSecondDivision); given(repoUserServiceMock.getOrCreateUserById(firstUserId)).willReturn(firstUser); given(repoUserServiceMock.getOrCreateUserById(secondUserId)).willReturn(secondUser); given(normalizerMock.normalize(List.of(firstExpected, secondExpected))).willReturn(List.of(firstExpected, secondExpected)); userUpdater.updateUserData(List.of(firstNode, secondNode)); verify(userDataRepoMock).deleteAll(); verify(userDataRepoMock).flush(); verify(userDataRepoMock).saveAll(userDataCaptor.capture()); verify(normalizerMock).normalize(List.of(firstExpected, secondExpected)); assertThat(userDataCaptor.getValue()).containsExactlyInAnyOrder(firstExpected, secondExpected); }