RepositoryUserService implements UserService { @Override public Map<User, Boolean> usersAreManagers(@NonNull Set<User> users) { var userManagerMap = hierarchyTreeNodeRepo.findByUserIn(users).stream() .collect(Collectors.toMap( HierarchyTreeNode::getUser, HierarchyTreeNode::isManagingUser )); if(userManagerMap.keySet().size() != users.size()) { throw new IllegalStateException("Hierarchy for some users not present!"); } return userManagerMap; } 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 usersAreManagers() { var users = Set.of(userMock, otherUserMock); var hierarchies = List.of(hierarchyTreeNodeMock, otherHierarchyTreeNodeMock); given(hierarchyTreeNodeMock.getUser()).willReturn(userMock); given(hierarchyTreeNodeMock.isManagingUser()).willReturn(true); given(otherHierarchyTreeNodeMock.getUser()).willReturn(otherUserMock); given(otherHierarchyTreeNodeMock.isManagingUser()).willReturn(false); given(hierarchyTreeNodeRepoMock.findByUserIn(users)).willReturn(hierarchies); var actualMap = repoUserService.usersAreManagers(users); assertThat(actualMap) .containsOnly(MapEntry.entry(userMock, true), MapEntry.entry(otherUserMock, false)); }
@Test public void usersAreManagersThrowsExceptionWhenNoHierarchyPresentForSomeUser() { var expectedMessage = "Hierarchy for some users not present!"; var users = Set.of(userMock, otherUserMock); var hierarchies = List.of(hierarchyTreeNodeMock); given(hierarchyTreeNodeMock.getUser()).willReturn(userMock); given(hierarchyTreeNodeMock.isManagingUser()).willReturn(true); given(hierarchyTreeNodeRepoMock.findByUserIn(users)).willReturn(hierarchies); assertThatThrownBy(() -> repoUserService.usersAreManagers(users)) .isInstanceOf(IllegalStateException.class) .hasMessage(expectedMessage); }