@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)));
}