Users { public Collection<AmbariGrantedAuthority> getUserAuthorities(String userName, UserType userType) { UserEntity userEntity = userDAO.findUserByNameAndType(userName, userType); if (userEntity == null) { return Collections.emptyList(); } Collection<PrivilegeEntity> privilegeEntities = getUserPrivileges(userEntity); Set<AmbariGrantedAuthority> authorities = new HashSet<>(privilegeEntities.size()); for (PrivilegeEntity privilegeEntity : privilegeEntities) { authorities.add(new AmbariGrantedAuthority(privilegeEntity)); } return authorities; }  List<User> getAllUsers(); @Deprecated User getAnyUser(String userName); User getUser(String userName, UserType userType); User getUser(Integer userId); @Deprecated User getUserIfUnique(String userName); synchronized void modifyPassword(String userName, String currentUserPassword, String newPassword); synchronized void setUserActive(String userName, boolean active); synchronized void setUserLdap(String userName); synchronized void setGroupLdap(String groupName); void createUser(String userName, String password); synchronized void createUser(String userName, String password, UserType userType, Boolean active, Boolean
      admin); synchronized void removeUser(User user); Group getGroup(String groupName); Group getGroupByNameAndType(String groupName, GroupType groupType); Collection<User> getGroupMembers(String groupName); @Transactional synchronized void createGroup(String groupName, GroupType groupType); List<Group> getAllGroups(); List<String> getAllMembers(String groupName); @Transactional synchronized void removeGroup(Group group); synchronized void grantAdminPrivilege(Integer userId); synchronized void grantPrivilegeToGroup(Integer groupId, Long resourceId, ResourceType resourceType, String permissionName); synchronized void revokeAdminPrivilege(Integer userId); @Transactional synchronized void addMemberToGroup(String groupName, String userName); @Transactional synchronized void removeMemberFromGroup(String groupName, String userName); synchronized boolean isUserCanBeRemoved(UserEntity userEntity); void processLdapSync(LdapBatchDto batchInfo); Collection<PrivilegeEntity> getUserPrivileges(UserEntity userEntity); Collection<PrivilegeEntity> getGroupPrivileges(GroupEntity groupEntity); Collection<AmbariGrantedAuthority> getUserAuthorities(String userName, UserType userType);  }
@Test public void testGetUserAuthorities() throws Exception { createInjector(); PrincipalEntity userPrincipalEntity = createMock(PrincipalEntity.class); UserEntity userEntity = createMock(UserEntity.class); expect(userEntity.getPrincipal()).andReturn(userPrincipalEntity).times(1); UserDAO userDAO = injector.getInstance(UserDAO.class); expect(userDAO.findUserByNameAndType("user1", UserType.LOCAL)).andReturn(userEntity).times(1); PrincipalEntity groupPrincipalEntity = createMock(PrincipalEntity.class); GroupEntity groupEntity = createMock(GroupEntity.class); expect(groupEntity.getPrincipal()).andReturn(groupPrincipalEntity).times(1); MemberEntity memberEntity = createMock(MemberEntity.class); expect(memberEntity.getGroup()).andReturn(groupEntity).times(1); MemberDAO memberDAO = injector.getInstance(MemberDAO.class); expect(memberDAO.findAllMembersByUser(userEntity)).andReturn(Collections.singletonList(memberEntity)).times(1); PrincipalEntity clusterUserPrivilegePermissionPrincipalEntity = createMock(PrincipalEntity.class); PermissionEntity clusterUserPrivilegePermissionEntity = createMock(PermissionEntity.class); expect(clusterUserPrivilegePermissionEntity.getPrincipal()).andReturn(clusterUserPrivilegePermissionPrincipalEntity).times(1); PrivilegeEntity clusterUserPrivilegeEntity = createMock(PrivilegeEntity.class); expect(clusterUserPrivilegeEntity.getPermission()).andReturn(clusterUserPrivilegePermissionEntity).times(1); PrincipalEntity clusterOperatorPrivilegePermissionPrincipalEntity = createMock(PrincipalEntity.class); PermissionEntity clusterOperatorPrivilegePermissionEntity = createMock(PermissionEntity.class); expect(clusterOperatorPrivilegePermissionEntity.getPrincipal()).andReturn(clusterOperatorPrivilegePermissionPrincipalEntity).times(1); PrivilegeEntity clusterOperatorPrivilegeEntity = createMock(PrivilegeEntity.class); expect(clusterOperatorPrivilegeEntity.getPermission()).andReturn(clusterOperatorPrivilegePermissionEntity).times(1); List<PrivilegeEntity> privilegeEntities = new ArrayList<PrivilegeEntity>(); privilegeEntities.add(clusterUserPrivilegeEntity); privilegeEntities.add(clusterOperatorPrivilegeEntity); PrivilegeEntity clusterUserViewUserPrivilegeEntity = createMock(PrivilegeEntity.class); List<PrivilegeEntity> rolePrivilegeEntities = new ArrayList<PrivilegeEntity>(); rolePrivilegeEntities.add(clusterUserViewUserPrivilegeEntity); Capture<? extends List<PrincipalEntity>> principalEntitiesCapture = newCapture(); Capture<? extends List<PrincipalEntity>> rolePrincipalEntitiesCapture = newCapture(); PrivilegeDAO privilegeDAO = injector.getInstance(PrivilegeDAO.class); expect(privilegeDAO.findAllByPrincipal(capture(principalEntitiesCapture))).andReturn(privilegeEntities).times(1); expect(privilegeDAO.findAllByPrincipal(capture(rolePrincipalEntitiesCapture))).andReturn(rolePrivilegeEntities).times(1); replayAll(); Users user = injector.getInstance(Users.class); Collection<AmbariGrantedAuthority> authorities = user.getUserAuthorities("user1", UserType.LOCAL); verifyAll(); Assert.assertEquals(2, principalEntitiesCapture.getValue().size()); Assert.assertTrue(principalEntitiesCapture.getValue().contains(userPrincipalEntity)); Assert.assertTrue(principalEntitiesCapture.getValue().contains(groupPrincipalEntity)); Assert.assertEquals(2, rolePrincipalEntitiesCapture.getValue().size()); Assert.assertTrue(rolePrincipalEntitiesCapture.getValue().contains(clusterUserPrivilegePermissionPrincipalEntity)); Assert.assertTrue(rolePrincipalEntitiesCapture.getValue().contains(clusterOperatorPrivilegePermissionPrincipalEntity)); Assert.assertEquals(3, authorities.size()); Assert.assertTrue(authorities.contains(new AmbariGrantedAuthority(clusterUserPrivilegeEntity))); Assert.assertTrue(authorities.contains(new AmbariGrantedAuthority(clusterOperatorPrivilegeEntity))); Assert.assertTrue(authorities.contains(new AmbariGrantedAuthority(clusterUserViewUserPrivilegeEntity))); }