RepositoryUserService implements UserService { List<UserData> initializeThumbnailPhotos(@NonNull List<UserData> userData) { var uninitializedUserIds = userData.stream() .filter(Predicate.not(UserData::isPictureInitialized)) .map(data -> data.getUser().getId()) .collect(Collectors.toList()); var userIdDataMap = userData.stream() .collect(Collectors.toMap( data -> data.getUser().getId(), Function.identity() )); var idPhotoMap = ldapAdapter.getThumbnailPhotos(uninitializedUserIds); var newlyInitialized = uninitializedUserIds.stream() .map(userId -> { var thumbnailPhoto = idPhotoMap.get(userId); var uninitialized = userIdDataMap.get(userId); return copyAndSetThumbnailPhoto(uninitialized, thumbnailPhoto); }) .collect(Collectors.toList()); var allData = new ArrayList<>(userData); dataRepo.saveAll(newlyInitialized) .forEach(newlyInit -> { var newlyInitializedUserId = newlyInit.getUser().getId(); var uninitializedIndex = allData.indexOf(userIdDataMap.get(newlyInitializedUserId)); allData.set(uninitializedIndex, newlyInit); }); return allData; } RepositoryUserService(UserRepository userRepo,
                                 UserDataRepository dataRepo,
                                 LdapAdapter ldapAdapter,
                                 HierarchyTreeNodeRepository hierarchyTreeNodeRepo,
                                 HibernateSearchService hibernateSearchService); @Override boolean userExists(@NonNull String userId); @Override boolean userIsManager(@NonNull User user); @Override HierarchyTreeNode getHierarchyForUser(@NonNull User user); @Override UserData getUserData(@NonNull User user); @Override User getUserById(@NonNull String userId); @Override boolean userHasStaffMember(@NonNull User user, @NonNull User staffMember); @Override User getManagerOfUser(@NonNull User user); @Override List<UserData> getStaffMemberUserDataOfUser(@NonNull User user, @NonNull Sort sort); @Override List<UserData> searchStaffMemberDataOfUser(User user, String query, Sort sort); @Override List<User> getStaffMembersOfUser(@NonNull User user); @Override User save(@NonNull User user); @Override void delete(User user); @Override void deleteUserById(String userId); @Override Map<User, Boolean> usersAreManagers(@NonNull Set<User> users); User getOrCreateUserById(@NonNull String userId); @Override void removeAllApplicationsOfUser(User user);  }
@Test public void initializeThumbnailPhotosOnlyInitializeNonInitialized() { var otherUserId = "other-user"; var expectedId = 1337L; var expectedFirstName = "First"; var expectedLastName = "Last"; var expectedMail = "mail@test.de"; var expectedLob = "LoB Test"; var expectedThumbnail = new byte[] {2, 1, 32, 4, 7, 42}; var uninitialized = new UserData(userMock, expectedFirstName, expectedLastName, expectedMail, expectedLob) .setId(expectedId) .setPictureInitialized(false); var expectedInitialized = new UserData(userMock, expectedFirstName, expectedLastName, expectedMail, expectedLob, expectedThumbnail) .setId(expectedId) .setPictureInitialized(true); given(userDataMock.isPictureInitialized()).willReturn(true); given(userDataMock.getUser()).willReturn(otherUserMock); given(otherUserMock.getId()).willReturn(otherUserId); given(ldapAdapterMock.getThumbnailPhotos(List.of(USER_ID))).willReturn(Map.of( USER_ID, expectedThumbnail )); given(userDataRepoMock.saveAll(List.of(expectedInitialized))).willReturn(List.of(expectedInitialized)); var actualInitialized = repoUserService.initializeThumbnailPhotos(List.of(uninitialized, userDataMock)); assertThat(actualInitialized).containsExactly(expectedInitialized, userDataMock); verify(ldapAdapterMock).getThumbnailPhotos(List.of(USER_ID)); verify(userDataRepoMock).saveAll(List.of(expectedInitialized)); }