### Question:
AmbariLdapAuthoritiesPopulator implements LdapAuthoritiesPopulator { @Override public Collection<? extends GrantedAuthority> getGrantedAuthorities(DirContextOperations userData, String username) { username = AuthorizationHelper.resolveLoginAliasToUserName(username); log.info("Get authorities for user " + username + " from local DB"); UserEntity user; user = userDAO.findUserByName(username); if (user == null) { log.error("Can't get authorities for user " + username + ", he is not present in local DB"); return Collections.emptyList(); } if(!user.getActive()){ throw new InvalidUsernamePasswordCombinationException(username); } Collection<PrivilegeEntity> privilegeEntities = users.getUserPrivileges(user); return authorizationHelper.convertPrivilegesToAuthorities(privilegeEntities); } @Inject  AmbariLdapAuthoritiesPopulator(AuthorizationHelper authorizationHelper,
                                        UserDAO userDAO, MemberDAO memberDAO, PrivilegeDAO privilegeDAO,
                                        Users users); @Override Collection<? extends GrantedAuthority> getGrantedAuthorities(DirContextOperations userData, String username);  }### Answer:
@Test public void testGetGrantedAuthorities() throws Exception { String username = "user"; AmbariLdapAuthoritiesPopulator populator = createMockBuilder(AmbariLdapAuthoritiesPopulator.class) .withConstructor(helper, userDAO, memberDAO, privilegeDAO, users).createMock(); expect(userEntity.getActive()).andReturn(true); expect(users.getUserPrivileges(userEntity)).andReturn(Collections.singletonList(privilegeEntity)); expect(userDAO.findUserByName(username)).andReturn(userEntity); replayAll(); populator.getGrantedAuthorities(userData, username); verifyAll(); }
@Test public void testGetGrantedAuthoritiesWithLoginAlias() throws Exception { String loginAlias = "testLoginAlias@testdomain.com"; String ambariUserName = "user"; PowerMock.mockStatic(AuthorizationHelper.class); expect(AuthorizationHelper.resolveLoginAliasToUserName(loginAlias)).andReturn(ambariUserName); PowerMock.replay(AuthorizationHelper.class); AmbariLdapAuthoritiesPopulator populator = createMockBuilder(AmbariLdapAuthoritiesPopulator.class) .withConstructor(helper, userDAO, memberDAO, privilegeDAO, users).createMock(); expect(userEntity.getActive()).andReturn(true); expect(users.getUserPrivileges(userEntity)).andReturn(Collections.singletonList(privilegeEntity)); expect(userDAO.findUserByName(ambariUserName)).andReturn(userEntity); replayAll(); populator.getGrantedAuthorities(userData, loginAlias); PowerMock.verify(AuthorizationHelper.class); verifyAll(); }