@Test
@SuppressWarnings("serial")
public void testCleanUpLdapUsersWithoutGroup() throws AmbariException {

  final Configuration configuration = createNiceMock(Configuration.class);
  final Users users = createNiceMock(Users.class);
  final GroupEntity ldapGroup = new GroupEntity();
  ldapGroup.setGroupId(1);
  ldapGroup.setGroupName("ldapGroup");
  ldapGroup.setGroupType(GroupType.LDAP);
  ldapGroup.setMemberEntities(new HashSet<MemberEntity>());
  final User ldapUserWithoutGroup = createLdapUserWithoutGroup();
  final User ldapUserWithGroup = createLdapUserWithGroup(ldapGroup);
  final User localUserWithoutGroup = createLocalUserWithoutGroup();
  final User localUserWithGroup = createLocalUserWithGroup(ldapGroup);
  final List<User> allUsers = new ArrayList<User>() {
    {
      add(ldapUserWithoutGroup);
      add(ldapUserWithGroup);
      add(localUserWithoutGroup);
      add(localUserWithGroup);
    }
  };
  expect(users.getAllUsers()).andReturn(new ArrayList<User>(allUsers));
  final List<User> removedUsers = new ArrayList<User>();
  final Capture<User> userCapture = EasyMock.newCapture();
  users.removeUser(capture(userCapture));
  expectLastCall().andAnswer(new IAnswer<Void>() {
    @Override
    public Void answer() throws Throwable {
      removedUsers.add(userCapture.getValue());
      allUsers.remove(userCapture.getValue());
      return null;
    }
  });
  replay(users);
  final AmbariLdapDataPopulatorTestInstance populator = new AmbariLdapDataPopulatorTestInstance(configuration, users);
  populator.setLdapTemplate(createNiceMock(LdapTemplate.class));
  populator.setLdapServerProperties(createNiceMock(LdapServerProperties.class));
  populator.cleanUpLdapUsersWithoutGroup();
  assertEquals(removedUsers.size(), 1);
  assertEquals(allUsers.size(), 3);
  assertTrue(allUsers.contains(ldapUserWithGroup));
  assertTrue(allUsers.contains(localUserWithoutGroup));
  assertTrue(allUsers.contains(localUserWithGroup));
  assertEquals(removedUsers.get(0), ldapUserWithoutGroup);
  verify(users);
}