@Test
public void testSynchronizeAllLdapUsers() 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("synced_user1").anyTimes();
  expect(user2.getUserName()).andReturn("synced_user2").anyTimes();
  expect(user3.getUserName()).andReturn("unsynced_user1").anyTimes();
  expect(user4.getUserName()).andReturn("unsynced_user2").anyTimes();
  expect(user1.isLdapUser()).andReturn(true).anyTimes();
  expect(user2.isLdapUser()).andReturn(true).anyTimes();
  expect(user3.isLdapUser()).andReturn(false).anyTimes();
  expect(user4.isLdapUser()).andReturn(false).anyTimes();
  List<User> userList = Arrays.asList(user1, user2, user3, user4);
  Configuration configuration = createNiceMock(Configuration.class);
  Users users = createNiceMock(Users.class);
  LdapTemplate ldapTemplate = createNiceMock(LdapTemplate.class);
  LdapServerProperties ldapServerProperties = createNiceMock(LdapServerProperties.class);
  expect(users.getAllUsers()).andReturn(userList);
  replay(ldapTemplate, ldapServerProperties, users, configuration);
  replay(user1, user3, user2, user4);
  AmbariLdapDataPopulatorTestInstance populator = createMockBuilder(AmbariLdapDataPopulatorTestInstance.class)
      .addMockedMethod("getExternalLdapUserInfo")
      .withConstructor(configuration, users)
      .createNiceMock();
  LdapUserDto externalUser1 = createNiceMock(LdapUserDto.class);
  LdapUserDto externalUser2 = createNiceMock(LdapUserDto.class);
  LdapUserDto externalUser3 = createNiceMock(LdapUserDto.class);
  LdapUserDto externalUser4 = createNiceMock(LdapUserDto.class);
  expect(externalUser1.getUserName()).andReturn("synced_user2").anyTimes();
  expect(externalUser2.getUserName()).andReturn("unsynced_user2").anyTimes();
  expect(externalUser3.getUserName()).andReturn("external_user1").anyTimes();
  expect(externalUser4.getUserName()).andReturn("external_user2").anyTimes();
  replay(externalUser1, externalUser2, externalUser3, externalUser4);
  expect(populator.getExternalLdapUserInfo()).andReturn(
      createSet(externalUser1, externalUser2, externalUser3, externalUser4));
  replay(populator);
  populator.setLdapTemplate(ldapTemplate);
  populator.setLdapServerProperties(ldapServerProperties);
  LdapBatchDto result = populator.synchronizeAllLdapUsers(new LdapBatchDto());
  assertEquals(1, result.getUsersToBeRemoved().size());
  assertTrue(result.getUsersToBeRemoved().contains("synced_user1"));
  assertEquals(2, result.getUsersToBeCreated().size());
  assertTrue(result.getUsersToBeCreated().contains("external_user1"));
  assertTrue(result.getUsersToBeCreated().contains("external_user2"));
  assertEquals(1, result.getUsersToBecomeLdap().size());
  assertTrue(result.getUsersToBecomeLdap().contains("unsynced_user2"));
  assertTrue(result.getGroupsToBeRemoved().isEmpty());
  assertTrue(result.getGroupsToBeCreated().isEmpty());
  assertTrue(result.getGroupsToBecomeLdap().isEmpty());
  assertTrue(result.getMembershipToAdd().isEmpty());
  assertTrue(result.getMembershipToRemove().isEmpty());
  verify(populator.loadLdapTemplate(), populator);
}