@Test
public void testRefreshGroupMembers() throws Exception {

  User user1 = createNiceMock(User.class);
  User user2 = createNiceMock(User.class);
  User user3 = createNiceMock(User.class);
  User user4 = createNiceMock(User.class);
  expect(user1.getUserName()).andReturn("user1").anyTimes();
  expect(user2.getUserName()).andReturn("user2").anyTimes();
  expect(user3.getUserName()).andReturn("user3").anyTimes();
  expect(user4.getUserName()).andReturn("user4").anyTimes();
  expect(user1.isLdapUser()).andReturn(false).anyTimes();
  expect(user2.isLdapUser()).andReturn(true).anyTimes();
  expect(user3.isLdapUser()).andReturn(true).anyTimes();
  expect(user4.isLdapUser()).andReturn(false).anyTimes();
  Group group1 = createNiceMock(Group.class);
  Group group2 = createNiceMock(Group.class);
  expect(group1.isLdapGroup()).andReturn(true).anyTimes();
  expect(group2.isLdapGroup()).andReturn(true).anyTimes();
  expect(group1.getGroupName()).andReturn("group1").anyTimes();
  expect(group2.getGroupName()).andReturn("group2").anyTimes();
  Configuration configuration = createNiceMock(Configuration.class);
  Users users = createNiceMock(Users.class);
  LdapTemplate ldapTemplate = createNiceMock(LdapTemplate.class);
  LdapServerProperties ldapServerProperties = createNiceMock(LdapServerProperties.class);
  expect(ldapServerProperties.getGroupNamingAttr()).andReturn("cn").anyTimes();
  expect(ldapServerProperties.getUsernameAttribute()).andReturn("uid").anyTimes();
  replay(ldapTemplate, ldapServerProperties, users, configuration);
  replay(user1, user2, user3, user4);
  replay(group1, group2);
  AmbariLdapDataPopulatorTestInstance populator = createMockBuilder(AmbariLdapDataPopulatorTestInstance.class)
    .addMockedMethod("getLdapUserByMemberAttr")
    .addMockedMethod("getLdapGroupByMemberAttr")
    .addMockedMethod("getInternalMembers")
    .withConstructor(configuration, users)
    .createNiceMock();
  LdapGroupDto externalGroup = createNiceMock(LdapGroupDto.class);
  expect(externalGroup.getGroupName()).andReturn("group1").anyTimes();
  expect(externalGroup.getMemberAttributes()).andReturn(createSet("user1", "user2", "user4", "user6")).anyTimes();
  replay(externalGroup);
  Map<String, User> internalMembers = new HashMap<String, User>();
  internalMembers.put("user1", user1);
  internalMembers.put("user3", user3);
  internalMembers.put("user4", user4);
  LdapBatchDto batchInfo = new LdapBatchDto();
  LdapUserDto externalUser1 = createNiceMock(LdapUserDto.class);
  LdapUserDto externalUser2 = createNiceMock(LdapUserDto.class);
  LdapUserDto externalUser3 = createNiceMock(LdapUserDto.class);
  LdapUserDto externalUser4 = createNiceMock(LdapUserDto.class);
  expect(externalUser1.getUserName()).andReturn("user1").anyTimes();
  expect(externalUser2.getUserName()).andReturn("user2").anyTimes();
  expect(externalUser3.getUserName()).andReturn("user4").anyTimes();
  expect(externalUser4.getUserName()).andReturn("user6").anyTimes();
  replay(externalUser1, externalUser2, externalUser3, externalUser4);
  expect(populator.getLdapUserByMemberAttr("user1")).andReturn(externalUser1).anyTimes();
  expect(populator.getLdapUserByMemberAttr("user2")).andReturn(externalUser2).anyTimes();
  expect(populator.getLdapUserByMemberAttr("user4")).andReturn(null).anyTimes();
  expect(populator.getLdapGroupByMemberAttr("user4")).andReturn(externalGroup).anyTimes();
  expect(populator.getLdapUserByMemberAttr("user6")).andReturn(externalUser4).anyTimes();
  expect(populator.getInternalMembers("group1")).andReturn(internalMembers).anyTimes();
  replay(populator);
  populator.setLdapTemplate(ldapTemplate);
  populator.setLdapServerProperties(ldapServerProperties);
  Map<String, User> internalUsers = new HashMap<String, User>();
  internalUsers.putAll(internalMembers);
  internalUsers.put("user2", user2);
  Map<String, Group> internalGroups = new HashMap<String, Group>();
  internalGroups.put("group2", group2);
  populator.refreshGroupMembers(batchInfo, externalGroup, internalUsers, internalGroups, null, true);
  Set<String> groupMembersToAdd = new HashSet<String>();
  for (LdapUserGroupMemberDto ldapUserGroupMemberDto : batchInfo.getMembershipToAdd()) {
    groupMembersToAdd.add(ldapUserGroupMemberDto.getUserName());
  }
  assertEquals(3, groupMembersToAdd.size());
  assertTrue(groupMembersToAdd.contains("user2"));
  assertTrue(groupMembersToAdd.contains("user6"));
  Set<String> groupMembersToRemove = new HashSet<String>();
  for (LdapUserGroupMemberDto ldapUserGroupMemberDto : batchInfo.getMembershipToRemove()) {
    groupMembersToRemove.add(ldapUserGroupMemberDto.getUserName());
  }
  assertEquals(2, groupMembersToRemove.size());
  assertTrue(groupMembersToRemove.contains("user3"));
  assertEquals(1, batchInfo.getUsersToBeCreated().size());
  assertTrue(batchInfo.getUsersToBeCreated().contains("user6"));
  assertEquals(1, batchInfo.getUsersToBecomeLdap().size());
  assertTrue(batchInfo.getUsersToBecomeLdap().contains("user1"));
  assertTrue(!batchInfo.getUsersToBecomeLdap().contains("user4"));
  assertTrue(batchInfo.getGroupsToBecomeLdap().isEmpty());
  assertEquals(1, batchInfo.getGroupsToBeCreated().size());
  assertTrue(batchInfo.getGroupsToBeRemoved().isEmpty());
  assertTrue(batchInfo.getUsersToBeRemoved().isEmpty());
  verify(populator.loadLdapTemplate(), populator);
}