RepositoryUserService implements UserService { @Override public User getManagerOfUser(@NonNull User user) { var hierarchy = getHierarchyForUser(user); if(hierarchy.isRoot()) { return hierarchy.getUser(); } return hierarchy .getManager() .getUser(); } 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 getManagerOfUserReturnsManagerWhenHierarchyPresent() { given(hierarchyTreeNodeRepoMock.findByUser(userMock)).willReturn(Optional.of(hierarchyTreeNodeMock)); given(hierarchyTreeNodeMock.getManager()).willReturn(otherHierarchyTreeNodeMock); given(otherHierarchyTreeNodeMock.getUser()).willReturn(otherUserMock); var actualManager = repoUserService.getManagerOfUser(userMock); assertThat(actualManager).isEqualTo(otherUserMock); }
@Test public void getManagerOfUserReturnsHierarchyUserWhenManagerIsNull() { given(hierarchyTreeNodeRepoMock.findByUser(userMock)).willReturn(Optional.of(hierarchyTreeNodeMock)); given(hierarchyTreeNodeMock.isRoot()).willReturn(true); given(hierarchyTreeNodeMock.getUser()).willReturn(userMock); var actualManager = repoUserService.getManagerOfUser(userMock); assertThat(actualManager).isEqualTo(userMock); }
@Test public void getManagerOfUserThrowsExceptionWhenHierarchyNotPresent() { var expectedMessage = String.format("Hierarchy for User with ID '%s' not found!", USER_ID); given(hierarchyTreeNodeRepoMock.findByUser(userMock)).willReturn(Optional.empty()); assertThatThrownBy(() -> repoUserService.getManagerOfUser(userMock)) .isInstanceOf(HierarchyNotFoundException.class) .hasMessage(expectedMessage); }