RepositoryUserService implements UserService { @Override public UserData getUserData(@NonNull User user) { var data = dataRepo.findByUser(user) .orElseThrow(() -> new UserDataNotFoundException(user.getId())); return initializeThumbnailPhotos(Collections.singletonList(data)) .get(0); } 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 getUserDataReturnsInitializedDataWhenPresent() { given(userDataRepoMock.findByUser(userMock)).willReturn(Optional.of(userDataMock)); given(userDataMock.getUser()).willReturn(userMock); given(userDataMock.isPictureInitialized()).willReturn(true); var actualData = repoUserService.getUserData(userMock); assertThat(actualData).isEqualTo(userDataMock); }
@Test public void getUserDataReturnsDataAfterInitializationWhenPresent() { 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 expectedData = new UserData(userMock, expectedFirstName, expectedLastName, expectedMail, expectedLob, expectedThumbnail) .setId(expectedId) .setPictureInitialized(true); var uninitializedData = new UserData(userMock, expectedFirstName, expectedLastName, expectedMail, expectedLob) .setId(expectedId) .setPictureInitialized(false); given(userDataRepoMock.findByUser(userMock)).willReturn(Optional.of(uninitializedData)); given(userDataRepoMock.saveAll(List.of(expectedData))).willReturn(List.of(expectedData)); given(ldapAdapterMock.getThumbnailPhotos(List.of(USER_ID))).willReturn(Map.of( USER_ID, expectedThumbnail )); var actualData = repoUserService.getUserData(userMock); assertThat(actualData).isEqualTo(expectedData); }
@Test public void getUserDataThrowsExceptionWhenDataNotPresent() { var expectedMessage = String.format("Data for User with ID '%s' not found!", USER_ID); given(userDataRepoMock.findByUser(userMock)).willReturn(Optional.empty()); assertThatThrownBy(() -> repoUserService.getUserData(userMock)) .isInstanceOf(UserDataNotFoundException.class) .hasMessage(expectedMessage); }