@Test
public void testSynchronizeAllLdapGroups() throws Exception {

  Group group1 = createNiceMock(Group.class);
  Group group2 = createNiceMock(Group.class);
  Group group3 = createNiceMock(Group.class);
  Group group4 = createNiceMock(Group.class);
  Group group5 = createNiceMock(Group.class);
  expect(group1.getGroupName()).andReturn("group1").anyTimes();
  expect(group2.getGroupName()).andReturn("group2").anyTimes();
  expect(group3.getGroupName()).andReturn("group3").anyTimes();
  expect(group4.getGroupName()).andReturn("group4").anyTimes();
  expect(group5.getGroupName()).andReturn("group5").anyTimes();
  expect(group1.isLdapGroup()).andReturn(false).anyTimes();
  expect(group2.isLdapGroup()).andReturn(true).anyTimes();
  expect(group3.isLdapGroup()).andReturn(false).anyTimes();
  expect(group4.isLdapGroup()).andReturn(true).anyTimes();
  expect(group5.isLdapGroup()).andReturn(false).anyTimes();
  List<Group> groupList = Arrays.asList(group1, group2, group3, group4, group5);
  Configuration configuration = createNiceMock(Configuration.class);
  Users users = createNiceMock(Users.class);
  LdapTemplate ldapTemplate = createNiceMock(LdapTemplate.class);
  LdapServerProperties ldapServerProperties = createNiceMock(LdapServerProperties.class);
  expect(users.getAllGroups()).andReturn(groupList);
  expect(users.getAllUsers()).andReturn(Collections.<User>emptyList());
  replay(ldapTemplate, ldapServerProperties, users, configuration);
  replay(group1, group2, group3, group4, group5);
  AmbariLdapDataPopulatorTestInstance populator = createMockBuilder(AmbariLdapDataPopulatorTestInstance.class)
      .addMockedMethod("getExternalLdapGroupInfo")
      .addMockedMethod("refreshGroupMembers")
      .withConstructor(configuration, users)
      .createNiceMock();
  LdapGroupDto externalGroup1 = createNiceMock(LdapGroupDto.class);
  LdapGroupDto externalGroup2 = createNiceMock(LdapGroupDto.class);
  LdapGroupDto externalGroup3 = createNiceMock(LdapGroupDto.class);
  LdapGroupDto externalGroup4 = createNiceMock(LdapGroupDto.class);
  expect(externalGroup1.getGroupName()).andReturn("group4").anyTimes();
  expect(externalGroup2.getGroupName()).andReturn("group3").anyTimes();
  expect(externalGroup3.getGroupName()).andReturn("group6").anyTimes();
  expect(externalGroup4.getGroupName()).andReturn("group7").anyTimes();
  LdapBatchDto batchInfo = new LdapBatchDto();
  Set<LdapGroupDto> externalGroups = createSet(externalGroup1, externalGroup2, externalGroup3, externalGroup4);
  for (LdapGroupDto externalGroup : externalGroups) {
    populator.refreshGroupMembers(eq(batchInfo), eq(externalGroup), EasyMock.<Map<String, User>>anyObject(),
      EasyMock.<Map<String, Group>>anyObject(), EasyMock.<Set<String>>anyObject(), anyBoolean());
    expectLastCall();
  }
  expect(populator.getExternalLdapGroupInfo()).andReturn(externalGroups);
  replay(externalGroup1, externalGroup2, externalGroup3, externalGroup4);
  replay(populator);
  populator.setLdapTemplate(ldapTemplate);
  populator.setLdapServerProperties(ldapServerProperties);
  LdapBatchDto result = populator.synchronizeAllLdapGroups(batchInfo);
  assertEquals(1, result.getGroupsToBeRemoved().size());
  assertTrue(result.getGroupsToBeRemoved().contains("group2"));
  assertEquals(1, result.getGroupsToBecomeLdap().size());
  assertTrue(result.getGroupsToBecomeLdap().contains("group3"));
  assertEquals(2, result.getGroupsToBeCreated().size());
  assertTrue(result.getGroupsToBeCreated().contains("group6"));
  assertTrue(result.getGroupsToBeCreated().contains("group7"));
  assertTrue(result.getUsersToBeCreated().isEmpty());
  assertTrue(result.getMembershipToAdd().isEmpty());
  assertTrue(result.getMembershipToRemove().isEmpty());
  assertTrue(result.getUsersToBecomeLdap().isEmpty());
  assertTrue(result.getUsersToBeRemoved().isEmpty());
  verify(populator.loadLdapTemplate(), populator);
}