ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From swa...@apache.org
Subject [1/2] AMBARI-7417. Admin Views: Remove SYNC LDAP from the UI and create commandline ambari-server sync-ldap to call the API for syncing.
Date Fri, 19 Sep 2014 18:24:50 GMT
Repository: ambari
Updated Branches:
  refs/heads/trunk d018aa6a1 -> 21d784b68


http://git-wip-us.apache.org/repos/asf/ambari/blob/21d784b6/ambari-server/src/test/java/org/apache/ambari/server/security/ldap/AmbariLdapDataPopulatorTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/security/ldap/AmbariLdapDataPopulatorTest.java b/ambari-server/src/test/java/org/apache/ambari/server/security/ldap/AmbariLdapDataPopulatorTest.java
index 906d695..93a0b46 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/security/ldap/AmbariLdapDataPopulatorTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/security/ldap/AmbariLdapDataPopulatorTest.java
@@ -18,11 +18,14 @@
 package org.apache.ambari.server.security.ldap;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collections;
 import java.util.Date;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
-import junit.framework.Assert;
+import java.util.Map;
+import java.util.Set;
 
 import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.configuration.Configuration;
@@ -31,10 +34,10 @@ import org.apache.ambari.server.orm.entities.MemberEntity;
 import org.apache.ambari.server.orm.entities.PrincipalEntity;
 import org.apache.ambari.server.orm.entities.PrivilegeEntity;
 import org.apache.ambari.server.orm.entities.UserEntity;
+import org.apache.ambari.server.security.authorization.Group;
 import org.apache.ambari.server.security.authorization.LdapServerProperties;
 import org.apache.ambari.server.security.authorization.User;
 import org.apache.ambari.server.security.authorization.Users;
-import org.apache.ambari.server.security.ldap.AmbariLdapDataPopulator;
 import org.easymock.Capture;
 import org.easymock.EasyMock;
 import org.easymock.IAnswer;
@@ -42,76 +45,1312 @@ import org.junit.Test;
 import org.springframework.ldap.core.AttributesMapper;
 import org.springframework.ldap.core.LdapTemplate;
 
+import static junit.framework.Assert.*;
+import static org.easymock.EasyMock.*;
+
 public class AmbariLdapDataPopulatorTest {
-  private static class AmbariLdapDataPopulatorTestInstance extends AmbariLdapDataPopulator {
+  public static class AmbariLdapDataPopulatorTestInstance extends AmbariLdapDataPopulator {
 
-    public AmbariLdapDataPopulatorTestInstance(Configuration configuration,
-        Users users) {
+    public AmbariLdapDataPopulatorTestInstance(Configuration configuration, Users users) {
       super(configuration, users);
-      this.ldapServerProperties = EasyMock.createNiceMock(LdapServerProperties.class);
     }
 
-    final LdapTemplate ldapTemplate = EasyMock.createNiceMock(LdapTemplate.class);
+    private LdapTemplate ldapTemplate;
 
     @Override
     protected LdapTemplate loadLdapTemplate() {
       return ldapTemplate;
     }
 
+    public void setLdapTemplate(LdapTemplate ldapTemplate) {
+      this.ldapTemplate = ldapTemplate;
+    }
+
     public LdapServerProperties getLdapServerProperties() {
       return this.ldapServerProperties;
     }
+
+    public void setLdapServerProperties(LdapServerProperties ldapServerProperties) {
+      this.ldapServerProperties = ldapServerProperties;
+    }
   }
 
   @Test
   public void testIsLdapEnabled_badConfiguration() {
-    final Configuration configuration = EasyMock.createNiceMock(Configuration.class);
-    final Users users = EasyMock.createNiceMock(Users.class);
+    final Configuration configuration = createNiceMock(Configuration.class);
+    final Users users = createNiceMock(Users.class);
 
-    final AmbariLdapDataPopulator populator = new AmbariLdapDataPopulatorTestInstance(configuration, users);
+    LdapTemplate ldapTemplate = createNiceMock(LdapTemplate.class);
+    LdapServerProperties ldapServerProperties = createNiceMock(LdapServerProperties.class);
+    expect(configuration.isLdapConfigured()).andReturn(true);
+    expect(ldapTemplate.search(EasyMock.<String>anyObject(), EasyMock.<String>anyObject(), EasyMock.<AttributesMapper>anyObject())).andThrow(new NullPointerException()).once();
+    replay(ldapTemplate, configuration, ldapServerProperties);
 
-    EasyMock.expect(configuration.isLdapConfigured()).andReturn(true);
-    EasyMock.expect(populator.loadLdapTemplate().search(EasyMock. <String>anyObject(), EasyMock. <String>anyObject(), EasyMock. <AttributesMapper>anyObject())).andThrow(new NullPointerException()).once();
-    EasyMock.replay(populator.loadLdapTemplate(), configuration);
+    final AmbariLdapDataPopulatorTestInstance populator = new AmbariLdapDataPopulatorTestInstance(configuration, users);
+    populator.setLdapTemplate(ldapTemplate);
+    populator.setLdapServerProperties(ldapServerProperties);
 
-    Assert.assertFalse(populator.isLdapEnabled());
-    EasyMock.verify(populator.loadLdapTemplate(), configuration);
+    assertFalse(populator.isLdapEnabled());
+    verify(populator.loadLdapTemplate(), configuration);
   }
 
   @Test
   public void testIsLdapEnabled_reallyEnabled() {
-    final Configuration configuration = EasyMock.createNiceMock(Configuration.class);
-    final Users users = EasyMock.createNiceMock(Users.class);
+    final Configuration configuration = createNiceMock(Configuration.class);
+    final Users users = createNiceMock(Users.class);
 
-    final AmbariLdapDataPopulator populator = new AmbariLdapDataPopulatorTestInstance(configuration, users);
+    LdapTemplate ldapTemplate = createNiceMock(LdapTemplate.class);
+    LdapServerProperties ldapServerProperties = createNiceMock(LdapServerProperties.class);
+    expect(configuration.isLdapConfigured()).andReturn(true);
+    expect(ldapTemplate.search(EasyMock.<String>anyObject(), EasyMock.<String>anyObject(), EasyMock.<AttributesMapper>anyObject())).andReturn(Collections.emptyList()).once();
+    replay(ldapTemplate, configuration);
 
-    EasyMock.expect(configuration.isLdapConfigured()).andReturn(true);
-    EasyMock.expect(populator.loadLdapTemplate().search(EasyMock. <String>anyObject(), EasyMock. <String>anyObject(), EasyMock. <AttributesMapper>anyObject())).andReturn(Collections.emptyList()).once();
-    EasyMock.replay(populator.loadLdapTemplate(), configuration);
+    final AmbariLdapDataPopulatorTestInstance populator = new AmbariLdapDataPopulatorTestInstance(configuration, users);
+    populator.setLdapTemplate(ldapTemplate);
+    populator.setLdapServerProperties(ldapServerProperties);
 
-    Assert.assertTrue(populator.isLdapEnabled());
-    EasyMock.verify(populator.loadLdapTemplate(), configuration);
+    assertTrue(populator.isLdapEnabled());
+    verify(populator.loadLdapTemplate(), configuration);
   }
 
   @Test
   public void testIsLdapEnabled_reallyDisabled() {
-    final Configuration configuration = EasyMock.createNiceMock(Configuration.class);
-    final Users users = EasyMock.createNiceMock(Users.class);
+    final Configuration configuration = createNiceMock(Configuration.class);
+    final Users users = createNiceMock(Users.class);
+
+    LdapTemplate ldapTemplate = createNiceMock(LdapTemplate.class);
+    LdapServerProperties ldapServerProperties = createNiceMock(LdapServerProperties.class);
+    expect(configuration.isLdapConfigured()).andReturn(false);
+    expect(configuration.getLdapServerProperties()).andReturn(ldapServerProperties);
+    replay(ldapTemplate, ldapServerProperties, configuration);
+
+    final AmbariLdapDataPopulatorTestInstance populator = new AmbariLdapDataPopulatorTestInstance(configuration, users);
+    populator.setLdapTemplate(ldapTemplate);
+    populator.setLdapServerProperties(ldapServerProperties);
+
+    assertFalse(populator.isLdapEnabled());
+    verify(populator.loadLdapTemplate(), populator.getLdapServerProperties(), configuration);
+  }
+
+  private <T> Set<T> createSet(T...elements) {
+    return new HashSet<T>(Arrays.asList(elements));
+  }
+
+  @Test
+  public void synchronizeExistingLdapGroups() 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(true).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.EMPTY_LIST);
+
+    replay(ldapTemplate, ldapServerProperties, users, configuration);
+    replay(group1, group2, group3, group4, group5);
+
+    AmbariLdapDataPopulatorTestInstance populator = createMockBuilder(AmbariLdapDataPopulatorTestInstance.class)
+        .addMockedMethod("getLdapGroups")
+        .addMockedMethod("refreshGroupMembers")
+        .withConstructor(configuration, users)
+        .createNiceMock();
+
+    expect(populator.getLdapGroups("group2")).andReturn(Collections.EMPTY_SET);
+    LdapGroupDto externalGroup1 = createNiceMock(LdapGroupDto.class);
+    LdapBatchDto batchInfo = new LdapBatchDto();
+    populator.refreshGroupMembers(eq(batchInfo), eq(externalGroup1), anyObject(Map.class));
+    expectLastCall();
+    expect(populator.getLdapGroups("group4")).andReturn(Collections.singleton(externalGroup1));
+    expect(populator.getLdapGroups("group5")).andReturn(Collections.EMPTY_SET);
+    replay(populator);
+
+    populator.setLdapTemplate(ldapTemplate);
+    populator.setLdapServerProperties(ldapServerProperties);
+
+    LdapBatchDto result = populator.synchronizeExistingLdapGroups(batchInfo);
+
+    assertEquals(2, result.getGroupsToBeRemoved().size());
+    assertTrue(result.getGroupsToBeRemoved().contains("group2"));
+    assertTrue(result.getGroupsToBeRemoved().contains("group5"));
+    assertTrue(result.getGroupsToBecomeLdap().isEmpty());
+    assertTrue(result.getGroupsToBeCreated().isEmpty());
+    assertTrue(result.getUsersToBeCreated().isEmpty());
+    assertTrue(result.getMembershipToAdd().isEmpty());
+    assertTrue(result.getMembershipToRemove().isEmpty());
+    assertTrue(result.getUsersToBecomeLdap().isEmpty());
+    assertTrue(result.getUsersToBeRemoved().isEmpty());
+    verify(populator.loadLdapTemplate(), populator);
+  }
+
+  @Test
+  public void testSynchronizeLdapGroups_allExist() throws Exception {
+
+    Group group1 = createNiceMock(Group.class);
+    Group group2 = createNiceMock(Group.class);
+    Group group3 = createNiceMock(Group.class);
+    Group group4 = 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(group1.isLdapGroup()).andReturn(false).anyTimes();
+    expect(group2.isLdapGroup()).andReturn(true).anyTimes();
+    expect(group3.isLdapGroup()).andReturn(true).anyTimes();
+    expect(group4.isLdapGroup()).andReturn(false).anyTimes();
+
+    List<Group> groupList = Arrays.asList(group1, group2, group3, group4);
+
+    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.EMPTY_LIST);
+
+    replay(ldapTemplate, ldapServerProperties, users, configuration);
+    replay(group1, group2, group3, group4);
+
+    AmbariLdapDataPopulatorTestInstance populator = createMockBuilder(AmbariLdapDataPopulatorTestInstance.class)
+        .addMockedMethod("getLdapGroups")
+        .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("group1").anyTimes();
+    expect(externalGroup2.getGroupName()).andReturn("group2").anyTimes();
+    expect(externalGroup3.getGroupName()).andReturn("xgroup1").anyTimes();
+    expect(externalGroup4.getGroupName()).andReturn("xgroup2").anyTimes();
+    replay(externalGroup1, externalGroup2, externalGroup3, externalGroup4);
+
+    LdapBatchDto batchInfo = new LdapBatchDto();
+    Set<LdapGroupDto> externalGroups = createSet(externalGroup3, externalGroup4);
+    for (LdapGroupDto externalGroup : externalGroups) {
+      populator.refreshGroupMembers(eq(batchInfo), eq(externalGroup), anyObject(Map.class));
+      expectLastCall();
+    }
+    populator.refreshGroupMembers(eq(batchInfo), eq(externalGroup1), anyObject(Map.class));
+    expectLastCall();
+    populator.refreshGroupMembers(eq(batchInfo), eq(externalGroup2), anyObject(Map.class));
+    expectLastCall();
+    expect(populator.getLdapGroups("x*")).andReturn(externalGroups);
+    expect(populator.getLdapGroups("group1")).andReturn(Collections.singleton(externalGroup1));
+    expect(populator.getLdapGroups("group2")).andReturn(Collections.singleton(externalGroup2));
+    replay(populator);
+
+    populator.setLdapTemplate(ldapTemplate);
+    populator.setLdapServerProperties(ldapServerProperties);
+
+    LdapBatchDto result = populator.synchronizeLdapGroups(createSet("x*", "group1", "group2"), batchInfo);
+
+    assertEquals(1, result.getGroupsToBecomeLdap().size());
+    assertTrue(result.getGroupsToBecomeLdap().contains("group1"));
+    assertEquals(2, result.getGroupsToBeCreated().size());
+    assertTrue(result.getGroupsToBeCreated().contains("xgroup1"));
+    assertTrue(result.getGroupsToBeCreated().contains("xgroup2"));
+    assertTrue(result.getGroupsToBeRemoved().isEmpty());
+    assertTrue(result.getUsersToBeCreated().isEmpty());
+    assertTrue(result.getMembershipToAdd().isEmpty());
+    assertTrue(result.getMembershipToRemove().isEmpty());
+    assertTrue(result.getUsersToBecomeLdap().isEmpty());
+    assertTrue(result.getUsersToBeRemoved().isEmpty());
+    verify(populator.loadLdapTemplate(), populator);
+  }
+
+  @Test
+  public void testSynchronizeLdapGroups_add() throws Exception {
+
+    Group group1 = createNiceMock(Group.class);
+    Group group2 = createNiceMock(Group.class);
+    Group group3 = createNiceMock(Group.class);
+    Group group4 = 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(group1.isLdapGroup()).andReturn(false).anyTimes();
+    expect(group2.isLdapGroup()).andReturn(true).anyTimes();
+    expect(group3.isLdapGroup()).andReturn(true).anyTimes();
+    expect(group4.isLdapGroup()).andReturn(false).anyTimes();
+
+    List<Group> groupList = Arrays.asList(group1, group2, group3, group4);
+
+    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.EMPTY_LIST);
+
+    replay(ldapTemplate, ldapServerProperties, users, configuration);
+    replay(group1, group2, group3, group4);
+
+    AmbariLdapDataPopulatorTestInstance populator = createMockBuilder(AmbariLdapDataPopulatorTestInstance.class)
+        .addMockedMethod("getLdapGroups")
+        .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("group1").anyTimes();
+    expect(externalGroup2.getGroupName()).andReturn("group2").anyTimes();
+    expect(externalGroup3.getGroupName()).andReturn("xgroup1").anyTimes();
+    expect(externalGroup4.getGroupName()).andReturn("xgroup2").anyTimes();
+    replay(externalGroup1, externalGroup2, externalGroup3, externalGroup4);
+
+
+    LdapBatchDto batchInfo = new LdapBatchDto();
+    Set<LdapGroupDto> externalGroups = createSet(externalGroup3, externalGroup4);
+    for (LdapGroupDto externalGroup : externalGroups) {
+      populator.refreshGroupMembers(eq(batchInfo), eq(externalGroup), anyObject(Map.class));
+      expectLastCall();
+    }
+    populator.refreshGroupMembers(eq(batchInfo), eq(externalGroup2), anyObject(Map.class));
+    expectLastCall();
+    expect(populator.getLdapGroups("x*")).andReturn(externalGroups);
+    expect(populator.getLdapGroups("group2")).andReturn(Collections.singleton(externalGroup2));
+    replay(populator);
+
+    populator.setLdapTemplate(ldapTemplate);
+    populator.setLdapServerProperties(ldapServerProperties);
+
+    LdapBatchDto result = populator.synchronizeLdapGroups(createSet("x*", "group2"), batchInfo);
+
+    assertEquals(2, result.getGroupsToBeCreated().size());
+    assertTrue(result.getGroupsToBeCreated().contains("xgroup1"));
+    assertTrue(result.getGroupsToBeCreated().contains("xgroup2"));
+    assertTrue(result.getGroupsToBeRemoved().isEmpty());
+    assertTrue(result.getGroupsToBecomeLdap().isEmpty());
+    assertTrue(result.getUsersToBeCreated().isEmpty());
+    assertTrue(result.getMembershipToAdd().isEmpty());
+    assertTrue(result.getMembershipToRemove().isEmpty());
+    assertTrue(result.getUsersToBecomeLdap().isEmpty());
+    assertTrue(result.getUsersToBeRemoved().isEmpty());
+    verify(populator.loadLdapTemplate(), populator);
+  }
+
+  @Test
+  public void testSynchronizeLdapGroups_update() throws Exception {
+
+    Group group1 = createNiceMock(Group.class);
+    Group group2 = createNiceMock(Group.class);
+    Group group3 = createNiceMock(Group.class);
+    Group group4 = 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(group1.isLdapGroup()).andReturn(false).anyTimes();
+    expect(group2.isLdapGroup()).andReturn(true).anyTimes();
+    expect(group3.isLdapGroup()).andReturn(true).anyTimes();
+    expect(group4.isLdapGroup()).andReturn(false).anyTimes();
+
+    List<Group> groupList = Arrays.asList(group1, group2, group3, group4);
+
+    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.EMPTY_LIST);
+
+    replay(ldapTemplate, ldapServerProperties, users, configuration);
+    replay(group1, group2, group3, group4);
+
+    AmbariLdapDataPopulatorTestInstance populator = createMockBuilder(AmbariLdapDataPopulatorTestInstance.class)
+        .addMockedMethod("getLdapGroups")
+        .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("group1").anyTimes();
+    expect(externalGroup2.getGroupName()).andReturn("group2").anyTimes();
+    expect(externalGroup3.getGroupName()).andReturn("group3").anyTimes();
+    expect(externalGroup4.getGroupName()).andReturn("group4").anyTimes();
+    replay(externalGroup1, externalGroup2, externalGroup3, externalGroup4);
+
+    LdapBatchDto batchInfo = new LdapBatchDto();
+    Set<LdapGroupDto> externalGroups = createSet(externalGroup1, externalGroup2, externalGroup3, externalGroup4);
+    for (LdapGroupDto externalGroup : externalGroups) {
+      populator.refreshGroupMembers(eq(batchInfo), eq(externalGroup), anyObject(Map.class));
+      expectLastCall();
+    }
+    expect(populator.getLdapGroups("group*")).andReturn(externalGroups);
+    replay(populator);
+
+    populator.setLdapTemplate(ldapTemplate);
+    populator.setLdapServerProperties(ldapServerProperties);
+
+    LdapBatchDto result = populator.synchronizeLdapGroups(createSet("group*"), batchInfo);
+
+    assertEquals(2, result.getGroupsToBecomeLdap().size());
+    assertTrue(result.getGroupsToBecomeLdap().contains("group1"));
+    assertTrue(result.getGroupsToBecomeLdap().contains("group4"));
+    assertTrue(result.getGroupsToBeCreated().isEmpty());
+    assertTrue(result.getGroupsToBeRemoved().isEmpty());
+    assertTrue(result.getUsersToBeCreated().isEmpty());
+    assertTrue(result.getMembershipToAdd().isEmpty());
+    assertTrue(result.getMembershipToRemove().isEmpty());
+    assertTrue(result.getUsersToBecomeLdap().isEmpty());
+    assertTrue(result.getUsersToBeRemoved().isEmpty());
+    verify(populator.loadLdapTemplate(), populator);
+  }
+
+  @Test(expected = AmbariException.class)
+  public void testSynchronizeLdapGroups_absent() throws Exception {
+
+    Group group1 = createNiceMock(Group.class);
+    Group group2 = createNiceMock(Group.class);
+    Group group3 = createNiceMock(Group.class);
+    Group group4 = 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(group1.isLdapGroup()).andReturn(false).anyTimes();
+    expect(group2.isLdapGroup()).andReturn(true).anyTimes();
+    expect(group3.isLdapGroup()).andReturn(true).anyTimes();
+    expect(group4.isLdapGroup()).andReturn(false).anyTimes();
+
+    List<Group> groupList = Arrays.asList(group1, group2, group3, group4);
+
+    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.EMPTY_LIST);
+
+    replay(ldapTemplate, ldapServerProperties, users, configuration);
+    replay(group1, group2, group3, group4);
+
+    AmbariLdapDataPopulatorTestInstance populator = createMockBuilder(AmbariLdapDataPopulatorTestInstance.class)
+        .addMockedMethod("getLdapGroups")
+        .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("group1").anyTimes();
+    expect(externalGroup2.getGroupName()).andReturn("group2").anyTimes();
+    expect(externalGroup3.getGroupName()).andReturn("xgroup1").anyTimes();
+    expect(externalGroup4.getGroupName()).andReturn("xgroup2").anyTimes();
+    replay(externalGroup1, externalGroup2, externalGroup3, externalGroup4);
+
+    LdapBatchDto batchInfo = new LdapBatchDto();
+    Set<LdapGroupDto> externalGroups = createSet(externalGroup3, externalGroup4);
+    expect(populator.getLdapGroups("x*")).andReturn(externalGroups);
+    expect(populator.getLdapGroups("group1")).andReturn(Collections.EMPTY_SET);
+    expect(populator.getLdapGroups("group2")).andReturn(Collections.singleton(externalGroup2));
+    replay(populator);
+
+    populator.setLdapTemplate(ldapTemplate);
+    populator.setLdapServerProperties(ldapServerProperties);
+
+    populator.synchronizeLdapGroups(createSet("x*", "group1", "group2"), batchInfo);
+  }
+
+  @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.EMPTY_LIST);
+
+    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), anyObject(Map.class));
+      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);
+  }
+
+  @Test
+  public void testSynchronizeAllLdapGroups_add() throws Exception {
+
+    Group group1 = createNiceMock(Group.class);
+    Group group2 = createNiceMock(Group.class);
+    expect(group1.getGroupName()).andReturn("group1").anyTimes();
+    expect(group2.getGroupName()).andReturn("group2").anyTimes();
+    expect(group1.isLdapGroup()).andReturn(false).anyTimes();
+    expect(group2.isLdapGroup()).andReturn(false).anyTimes();
+
+    Configuration configuration = createNiceMock(Configuration.class);
+    Users users = createNiceMock(Users.class);
+    LdapTemplate ldapTemplate = createNiceMock(LdapTemplate.class);
+    LdapServerProperties ldapServerProperties = createNiceMock(LdapServerProperties.class);
+    expect(users.getAllGroups()).andReturn(Arrays.asList(group1, group2));
+    expect(users.getAllUsers()).andReturn(Collections.EMPTY_LIST);
+
+    replay(ldapTemplate, ldapServerProperties, users, configuration);
+    replay(group1, group2);
+
+    AmbariLdapDataPopulatorTestInstance populator = createMockBuilder(AmbariLdapDataPopulatorTestInstance.class)
+        .addMockedMethod("refreshGroupMembers")
+        .addMockedMethod("getExternalLdapGroupInfo")
+        .withConstructor(configuration, users)
+        .createNiceMock();
+
+    LdapGroupDto externalGroup1 = createNiceMock(LdapGroupDto.class);
+    LdapGroupDto externalGroup2 = createNiceMock(LdapGroupDto.class);
+    expect(externalGroup1.getGroupName()).andReturn("group4").anyTimes();
+    expect(externalGroup2.getGroupName()).andReturn("group3").anyTimes();
+    LdapBatchDto batchInfo = new LdapBatchDto();
+    Set<LdapGroupDto> externalGroups = createSet(externalGroup1, externalGroup2);
+    for (LdapGroupDto externalGroup : externalGroups) {
+      populator.refreshGroupMembers(eq(batchInfo), eq(externalGroup), anyObject(Map.class));
+      expectLastCall();
+    }
+    expect(populator.getExternalLdapGroupInfo()).andReturn(externalGroups);
+    replay(externalGroup1, externalGroup2);
+    replay(populator);
+
+    populator.setLdapTemplate(ldapTemplate);
+    populator.setLdapServerProperties(ldapServerProperties);
+
+    LdapBatchDto result = populator.synchronizeAllLdapGroups(batchInfo);
+
+    assertEquals(2, result.getGroupsToBeCreated().size());
+    assertTrue(result.getGroupsToBeCreated().contains("group3"));
+    assertTrue(result.getGroupsToBeCreated().contains("group4"));
+    assertTrue(result.getGroupsToBecomeLdap().isEmpty());
+    assertTrue(result.getGroupsToBeRemoved().isEmpty());
+    assertTrue(result.getUsersToBeCreated().isEmpty());
+    assertTrue(result.getMembershipToAdd().isEmpty());
+    assertTrue(result.getMembershipToRemove().isEmpty());
+    assertTrue(result.getUsersToBecomeLdap().isEmpty());
+    assertTrue(result.getUsersToBeRemoved().isEmpty());
+    verify(populator.loadLdapTemplate(), populator);
+  }
+
+  @Test
+  public void testSynchronizeAllLdapGroups_remove() throws Exception {
+
+    Group group1 = createNiceMock(Group.class);
+    Group group2 = createNiceMock(Group.class);
+    Group group3 = createNiceMock(Group.class);
+    Group group4 = 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(group1.isLdapGroup()).andReturn(false).anyTimes();
+    expect(group2.isLdapGroup()).andReturn(true).anyTimes();
+    expect(group3.isLdapGroup()).andReturn(true).anyTimes();
+    expect(group4.isLdapGroup()).andReturn(true).anyTimes();
+
+    List<Group> groupList = Arrays.asList(group1, group2, group3, group4);
+
+    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.EMPTY_LIST);
+
+    replay(ldapTemplate, ldapServerProperties, users, configuration);
+    replay(group1, group2, group3, group4);
+
+    AmbariLdapDataPopulatorTestInstance populator = createMockBuilder(AmbariLdapDataPopulatorTestInstance.class)
+        .addMockedMethod("refreshGroupMembers")
+        .addMockedMethod("getExternalLdapGroupInfo")
+        .withConstructor(configuration, users)
+        .createNiceMock();
+
+    LdapGroupDto externalGroup1 = createNiceMock(LdapGroupDto.class);
+    expect(externalGroup1.getGroupName()).andReturn("group3").anyTimes();
+    LdapBatchDto batchInfo = new LdapBatchDto();
+    Set<LdapGroupDto> externalGroups = createSet(externalGroup1);
+    for (LdapGroupDto externalGroup : externalGroups) {
+      populator.refreshGroupMembers(eq(batchInfo), eq(externalGroup), anyObject(Map.class));
+      expectLastCall();
+    }
+    expect(populator.getExternalLdapGroupInfo()).andReturn(externalGroups);
+    replay(populator);
+    replay(externalGroup1);
+
+    populator.setLdapTemplate(ldapTemplate);
+    populator.setLdapServerProperties(ldapServerProperties);
+
+    LdapBatchDto result = populator.synchronizeAllLdapGroups(batchInfo);
+
+    assertEquals(2, result.getGroupsToBeRemoved().size());
+    assertTrue(result.getGroupsToBeRemoved().contains("group2"));
+    assertTrue(result.getGroupsToBeRemoved().contains("group4"));
+    assertTrue(result.getGroupsToBeCreated().isEmpty());
+    assertTrue(result.getGroupsToBecomeLdap().isEmpty());
+    assertTrue(result.getUsersToBeCreated().isEmpty());
+    assertTrue(result.getMembershipToAdd().isEmpty());
+    assertTrue(result.getMembershipToRemove().isEmpty());
+    assertTrue(result.getUsersToBecomeLdap().isEmpty());
+    assertTrue(result.getUsersToBeRemoved().isEmpty());
+    verify(populator.loadLdapTemplate(), populator);
+  }
+
+  @Test
+  public void testSynchronizeAllLdapGroups_update() throws Exception {
+
+    Group group1 = createNiceMock(Group.class);
+    Group group2 = createNiceMock(Group.class);
+    Group group3 = createNiceMock(Group.class);
+    expect(group1.getGroupName()).andReturn("group1").anyTimes();
+    expect(group2.getGroupName()).andReturn("group2").anyTimes();
+    expect(group3.getGroupName()).andReturn("group3").anyTimes();
+    expect(group1.isLdapGroup()).andReturn(false).anyTimes();
+    expect(group2.isLdapGroup()).andReturn(false).anyTimes();
+    expect(group3.isLdapGroup()).andReturn(false).anyTimes();
+
+    List<Group> groupList = Arrays.asList(group1, group2, group3);
+
+    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.EMPTY_LIST);
+
+    replay(ldapTemplate, ldapServerProperties, users, configuration);
+    replay(group1, group2, group3);
+
+    AmbariLdapDataPopulatorTestInstance populator = createMockBuilder(AmbariLdapDataPopulatorTestInstance.class)
+        .addMockedMethod("refreshGroupMembers")
+        .addMockedMethod("getExternalLdapGroupInfo")
+        .withConstructor(configuration, users)
+        .createNiceMock();
+
+    LdapGroupDto externalGroup1 = createNiceMock(LdapGroupDto.class);
+    LdapGroupDto externalGroup2 = createNiceMock(LdapGroupDto.class);
+    expect(externalGroup1.getGroupName()).andReturn("group2").anyTimes();
+    expect(externalGroup2.getGroupName()).andReturn("group3").anyTimes();
+    LdapBatchDto batchInfo = new LdapBatchDto();
+    Set<LdapGroupDto> externalGroups = createSet(externalGroup1, externalGroup2);
+    for (LdapGroupDto externalGroup : externalGroups) {
+      populator.refreshGroupMembers(eq(batchInfo), eq(externalGroup), anyObject(Map.class));
+      expectLastCall();
+    }
+    expect(populator.getExternalLdapGroupInfo()).andReturn(externalGroups);
+    replay(populator);
+    replay(externalGroup1, externalGroup2);
+
+    populator.setLdapTemplate(ldapTemplate);
+    populator.setLdapServerProperties(ldapServerProperties);
+
+    LdapBatchDto result = populator.synchronizeAllLdapGroups(batchInfo);
 
-    final AmbariLdapDataPopulator populator = new AmbariLdapDataPopulatorTestInstance(configuration, users);
+    assertEquals(2, result.getGroupsToBecomeLdap().size());
+    assertTrue(result.getGroupsToBecomeLdap().contains("group2"));
+    assertTrue(result.getGroupsToBecomeLdap().contains("group3"));
+    assertTrue(result.getGroupsToBeCreated().isEmpty());
+    assertTrue(result.getGroupsToBeRemoved().isEmpty());
+    assertTrue(result.getUsersToBeCreated().isEmpty());
+    assertTrue(result.getMembershipToAdd().isEmpty());
+    assertTrue(result.getMembershipToRemove().isEmpty());
+    assertTrue(result.getUsersToBecomeLdap().isEmpty());
+    assertTrue(result.getUsersToBeRemoved().isEmpty());
+    verify(populator.loadLdapTemplate(), populator);
+  }
+
+  @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);
+  }
+
+  @Test
+  public void testSynchronizeAllLdapUsers_add() throws Exception {
+
+    User user1 = createNiceMock(User.class);
+    User user2 = createNiceMock(User.class);
+    expect(user1.getUserName()).andReturn("user1").anyTimes();
+    expect(user2.getUserName()).andReturn("user2").anyTimes();
+    expect(user1.isLdapUser()).andReturn(false).anyTimes();
+    expect(user2.isLdapUser()).andReturn(false).anyTimes();
+
+    List<User> userList = Arrays.asList(user1, user2);
+
+    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, user2);
+
+    AmbariLdapDataPopulatorTestInstance populator = createMockBuilder(AmbariLdapDataPopulatorTestInstance.class)
+        .addMockedMethod("getExternalLdapUserInfo")
+        .withConstructor(configuration, users)
+        .createNiceMock();
+
+    LdapUserDto externalUser1 = createNiceMock(LdapUserDto.class);
+    LdapUserDto externalUser2 = createNiceMock(LdapUserDto.class);
+    expect(externalUser1.getUserName()).andReturn("user3").anyTimes();
+    expect(externalUser2.getUserName()).andReturn("user4").anyTimes();
+    replay(externalUser1, externalUser2);
+
+    expect(populator.getExternalLdapUserInfo()).andReturn(
+        createSet(externalUser1, externalUser2));
+    replay(populator);
+
+    populator.setLdapTemplate(ldapTemplate);
+    populator.setLdapServerProperties(ldapServerProperties);
+
+    LdapBatchDto result = populator.synchronizeAllLdapUsers(new LdapBatchDto());
+
+    assertEquals(2, result.getUsersToBeCreated().size());
+    assertTrue(result.getUsersToBeCreated().contains("user3"));
+    assertTrue(result.getUsersToBeCreated().contains("user4"));
+    assertTrue(result.getUsersToBecomeLdap().isEmpty());
+    assertTrue(result.getUsersToBeRemoved().isEmpty());
+    assertTrue(result.getGroupsToBeRemoved().isEmpty());
+    assertTrue(result.getGroupsToBeCreated().isEmpty());
+    assertTrue(result.getGroupsToBecomeLdap().isEmpty());
+    assertTrue(result.getMembershipToAdd().isEmpty());
+    assertTrue(result.getMembershipToRemove().isEmpty());
+    verify(populator.loadLdapTemplate(), populator);
+  }
+
+  @Test
+  public void testSynchronizeAllLdapUsers_remove() throws Exception {
+
+    User user1 = createNiceMock(User.class);
+    User user2 = createNiceMock(User.class);
+    User user3 = createNiceMock(User.class);
+    expect(user1.getUserName()).andReturn("user1").anyTimes();
+    expect(user2.getUserName()).andReturn("user2").anyTimes();
+    expect(user3.getUserName()).andReturn("user3").anyTimes();
+    expect(user1.isLdapUser()).andReturn(true).anyTimes();
+    expect(user2.isLdapUser()).andReturn(false).anyTimes();
+    expect(user3.isLdapUser()).andReturn(true).anyTimes();
+
+    List<User> userList = Arrays.asList(user1, user2, user3);
+
+    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, user2, user3);
+
+    AmbariLdapDataPopulatorTestInstance populator = createMockBuilder(AmbariLdapDataPopulatorTestInstance.class)
+        .addMockedMethod("getExternalLdapUserInfo")
+        .withConstructor(configuration, users)
+        .createNiceMock();
+
+
+    expect(populator.getExternalLdapUserInfo()).andReturn(Collections.EMPTY_SET);
+    replay(populator);
+
+    populator.setLdapTemplate(ldapTemplate);
+    populator.setLdapServerProperties(ldapServerProperties);
+
+    LdapBatchDto result = populator.synchronizeAllLdapUsers(new LdapBatchDto());
+
+    assertEquals(2, result.getUsersToBeRemoved().size());
+    assertTrue(result.getUsersToBeRemoved().contains("user3"));
+    assertTrue(result.getUsersToBeRemoved().contains("user1"));
+    assertTrue(result.getUsersToBecomeLdap().isEmpty());
+    assertTrue(result.getUsersToBeCreated().isEmpty());
+    assertTrue(result.getGroupsToBeRemoved().isEmpty());
+    assertTrue(result.getGroupsToBeCreated().isEmpty());
+    assertTrue(result.getGroupsToBecomeLdap().isEmpty());
+    assertTrue(result.getMembershipToAdd().isEmpty());
+    assertTrue(result.getMembershipToRemove().isEmpty());
+    verify(populator.loadLdapTemplate(), populator);
+  }
+
+  @Test
+  public void testSynchronizeAllLdapUsers_update() throws Exception {
+
+    User user1 = createNiceMock(User.class);
+    User user2 = createNiceMock(User.class);
+    User user3 = createNiceMock(User.class);
+    expect(user1.getUserName()).andReturn("user1").anyTimes();
+    expect(user2.getUserName()).andReturn("user2").anyTimes();
+    expect(user3.getUserName()).andReturn("user3").anyTimes();
+    expect(user1.isLdapUser()).andReturn(true).anyTimes();
+    expect(user2.isLdapUser()).andReturn(false).anyTimes();
+    expect(user3.isLdapUser()).andReturn(false).anyTimes();
+
+    List<User> userList = Arrays.asList(user1, user2, user3);
+
+    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, user2, user3);
+
+    AmbariLdapDataPopulatorTestInstance populator = createMockBuilder(AmbariLdapDataPopulatorTestInstance.class)
+        .addMockedMethod("getExternalLdapUserInfo")
+        .withConstructor(configuration, users)
+        .createNiceMock();
+
+
+    LdapUserDto externalUser1 = createNiceMock(LdapUserDto.class);
+    LdapUserDto externalUser2 = createNiceMock(LdapUserDto.class);
+    expect(externalUser1.getUserName()).andReturn("user1").anyTimes();
+    expect(externalUser2.getUserName()).andReturn("user3").anyTimes();
+    replay(externalUser1, externalUser2);
+
+    expect(populator.getExternalLdapUserInfo()).andReturn(
+        createSet(externalUser1, externalUser2));
+
+    replay(populator);
+
+    populator.setLdapTemplate(ldapTemplate);
+    populator.setLdapServerProperties(ldapServerProperties);
+
+    LdapBatchDto result = populator.synchronizeAllLdapUsers(new LdapBatchDto());
+
+    assertEquals(1, result.getUsersToBecomeLdap().size());
+    assertTrue(result.getUsersToBecomeLdap().contains("user3"));
+    assertTrue(result.getUsersToBeRemoved().isEmpty());
+    assertTrue(result.getUsersToBeCreated().isEmpty());
+    assertTrue(result.getGroupsToBeRemoved().isEmpty());
+    assertTrue(result.getGroupsToBeCreated().isEmpty());
+    assertTrue(result.getGroupsToBecomeLdap().isEmpty());
+    assertTrue(result.getMembershipToAdd().isEmpty());
+    assertTrue(result.getMembershipToRemove().isEmpty());
+    verify(populator.loadLdapTemplate(), populator);
+  }
+
+  @Test
+  public void testSynchronizeExistingLdapUsers() 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, user2, user3, user4);
+
+    AmbariLdapDataPopulatorTestInstance populator = createMockBuilder(AmbariLdapDataPopulatorTestInstance.class)
+        .addMockedMethod("getLdapUsers")
+        .withConstructor(configuration, users)
+        .createNiceMock();
+
+    expect(populator.getLdapUsers("synced_user1")).andReturn(Collections.EMPTY_SET);
+    expect(populator.getLdapUsers("synced_user2")).andReturn(Collections.singleton(createNiceMock(LdapUserDto.class)));
+    replay(populator);
+
+    populator.setLdapTemplate(ldapTemplate);
+    populator.setLdapServerProperties(ldapServerProperties);
 
-    EasyMock.expect(configuration.isLdapConfigured()).andReturn(false);
-    EasyMock.replay(populator.loadLdapTemplate(), configuration);
+    LdapBatchDto result = populator.synchronizeExistingLdapUsers(new LdapBatchDto());
 
-    Assert.assertFalse(populator.isLdapEnabled());
-    EasyMock.verify(populator.loadLdapTemplate(), configuration);
+    assertEquals(1, result.getUsersToBeRemoved().size());
+    assertTrue(result.getUsersToBeRemoved().contains("synced_user1"));
+    assertTrue(result.getUsersToBeCreated().isEmpty());
+    assertTrue(result.getUsersToBecomeLdap().isEmpty());
+    assertTrue(result.getGroupsToBeRemoved().isEmpty());
+    assertTrue(result.getGroupsToBeCreated().isEmpty());
+    assertTrue(result.getGroupsToBecomeLdap().isEmpty());
+    assertTrue(result.getMembershipToAdd().isEmpty());
+    assertTrue(result.getMembershipToRemove().isEmpty());
+    verify(populator.loadLdapTemplate(), populator);
+  }
+
+  @Test
+  public void testSynchronizeLdapUsers_allExist() 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("user5").anyTimes();
+    expect(user4.getUserName()).andReturn("user6").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();
+
+    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, user2, user3, user4);
+
+    AmbariLdapDataPopulatorTestInstance populator = createMockBuilder(AmbariLdapDataPopulatorTestInstance.class)
+        .addMockedMethod("getLdapUsers")
+        .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("user1").anyTimes();
+    expect(externalUser2.getUserName()).andReturn("user2").anyTimes();
+    expect(externalUser3.getUserName()).andReturn("xuser3").anyTimes();
+    expect(externalUser4.getUserName()).andReturn("xuser4").anyTimes();
+    replay(externalUser1, externalUser2, externalUser3, externalUser4);
+
+    expect(populator.getLdapUsers("xuser*")).andReturn(
+        createSet(externalUser3, externalUser4));
+    expect(populator.getLdapUsers("user1")).andReturn(Collections.singleton(externalUser1));
+    expect(populator.getLdapUsers("user2")).andReturn(Collections.singleton(externalUser2));
+    replay(populator);
+
+    populator.setLdapTemplate(ldapTemplate);
+    populator.setLdapServerProperties(ldapServerProperties);
+
+    LdapBatchDto result = populator.synchronizeLdapUsers(createSet("user1", "user2", "xuser*"), new LdapBatchDto());
+
+    assertEquals(2, result.getUsersToBeCreated().size());
+    assertTrue(result.getUsersToBeCreated().contains("xuser3"));
+    assertTrue(result.getUsersToBeCreated().contains("xuser4"));
+    assertEquals(1, result.getUsersToBecomeLdap().size());
+    assertTrue(result.getUsersToBecomeLdap().contains("user1"));
+    assertTrue(result.getUsersToBeRemoved().isEmpty());
+    assertTrue(result.getGroupsToBeRemoved().isEmpty());
+    assertTrue(result.getGroupsToBeCreated().isEmpty());
+    assertTrue(result.getGroupsToBecomeLdap().isEmpty());
+    assertTrue(result.getMembershipToAdd().isEmpty());
+    assertTrue(result.getMembershipToRemove().isEmpty());
+    verify(populator.loadLdapTemplate(), populator);
+  }
+
+  @Test
+  public void testSynchronizeLdapUsers_add() 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("user5").anyTimes();
+    expect(user4.getUserName()).andReturn("user6").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();
+
+    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, user2, user3, user4);
+
+    AmbariLdapDataPopulatorTestInstance populator = createMockBuilder(AmbariLdapDataPopulatorTestInstance.class)
+        .addMockedMethod("getLdapUsers")
+        .withConstructor(configuration, users)
+        .createNiceMock();
+
+    LdapUserDto externalUser2 = createNiceMock(LdapUserDto.class);
+    LdapUserDto externalUser3 = createNiceMock(LdapUserDto.class);
+    LdapUserDto externalUser4 = createNiceMock(LdapUserDto.class);
+    expect(externalUser2.getUserName()).andReturn("user2").anyTimes();
+    expect(externalUser3.getUserName()).andReturn("xuser3").anyTimes();
+    expect(externalUser4.getUserName()).andReturn("xuser4").anyTimes();
+    replay(externalUser2, externalUser3, externalUser4);
+
+    expect(populator.getLdapUsers("xuser*")).andReturn(
+        createSet(externalUser3, externalUser4));
+    expect(populator.getLdapUsers("user2")).andReturn(Collections.singleton(externalUser2));
+    replay(populator);
+
+    populator.setLdapTemplate(ldapTemplate);
+    populator.setLdapServerProperties(ldapServerProperties);
+
+    LdapBatchDto result = populator.synchronizeLdapUsers(createSet("user2", "xuser*"), new LdapBatchDto());
+
+    assertEquals(2, result.getUsersToBeCreated().size());
+    assertTrue(result.getUsersToBeCreated().contains("xuser3"));
+    assertTrue(result.getUsersToBeCreated().contains("xuser4"));
+    assertTrue(result.getUsersToBecomeLdap().isEmpty());
+    assertTrue(result.getUsersToBeRemoved().isEmpty());
+    assertTrue(result.getGroupsToBeRemoved().isEmpty());
+    assertTrue(result.getGroupsToBeCreated().isEmpty());
+    assertTrue(result.getGroupsToBecomeLdap().isEmpty());
+    assertTrue(result.getMembershipToAdd().isEmpty());
+    assertTrue(result.getMembershipToRemove().isEmpty());
+    verify(populator.loadLdapTemplate(), populator);
+  }
+
+  @Test
+  public void testSynchronizeLdapUsers_update() 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("user5").anyTimes();
+    expect(user4.getUserName()).andReturn("user6").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();
+
+    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, user2, user3, user4);
+
+    AmbariLdapDataPopulatorTestInstance populator = createMockBuilder(AmbariLdapDataPopulatorTestInstance.class)
+        .addMockedMethod("getLdapUsers")
+        .withConstructor(configuration, users)
+        .createNiceMock();
+
+    LdapUserDto externalUser1 = createNiceMock(LdapUserDto.class);
+    LdapUserDto externalUser2 = createNiceMock(LdapUserDto.class);
+    LdapUserDto externalUser3 = createNiceMock(LdapUserDto.class);
+    expect(externalUser1.getUserName()).andReturn("user1").anyTimes();
+    expect(externalUser2.getUserName()).andReturn("user2").anyTimes();
+    expect(externalUser3.getUserName()).andReturn("user6").anyTimes();
+    replay(externalUser2, externalUser3, externalUser1);
+
+    expect(populator.getLdapUsers("user1")).andReturn(
+        Collections.singleton(externalUser1));
+    expect(populator.getLdapUsers("user2")).andReturn(Collections.singleton(externalUser2));
+    expect(populator.getLdapUsers("user6")).andReturn(Collections.singleton(externalUser3));
+    replay(populator);
+
+    populator.setLdapTemplate(ldapTemplate);
+    populator.setLdapServerProperties(ldapServerProperties);
+
+    LdapBatchDto result = populator.synchronizeLdapUsers(createSet("user2", "user1", "user6"), new LdapBatchDto());
+
+    assertEquals(2, result.getUsersToBecomeLdap().size());
+    assertTrue(result.getUsersToBecomeLdap().contains("user1"));
+    assertTrue(result.getUsersToBecomeLdap().contains("user6"));
+    assertTrue(result.getUsersToBeCreated().isEmpty());
+    assertTrue(result.getUsersToBeRemoved().isEmpty());
+    assertTrue(result.getGroupsToBeRemoved().isEmpty());
+    assertTrue(result.getGroupsToBeCreated().isEmpty());
+    assertTrue(result.getGroupsToBecomeLdap().isEmpty());
+    assertTrue(result.getMembershipToAdd().isEmpty());
+    assertTrue(result.getMembershipToRemove().isEmpty());
+    verify(populator.loadLdapTemplate(), populator);
+  }
+
+  @Test(expected = AmbariException.class)
+  public void testSynchronizeLdapUsers_absent() throws Exception {
+
+    Configuration configuration = createNiceMock(Configuration.class);
+    Users users = createNiceMock(Users.class);
+    LdapTemplate ldapTemplate = createNiceMock(LdapTemplate.class);
+    LdapServerProperties ldapServerProperties = createNiceMock(LdapServerProperties.class);
+
+    replay(ldapTemplate, ldapServerProperties, users, configuration);
+
+    AmbariLdapDataPopulatorTestInstance populator = createMockBuilder(AmbariLdapDataPopulatorTestInstance.class)
+        .addMockedMethod("getLdapUsers")
+        .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("user1").anyTimes();
+    expect(externalUser2.getUserName()).andReturn("user2").anyTimes();
+    expect(externalUser3.getUserName()).andReturn("xuser3").anyTimes();
+    expect(externalUser4.getUserName()).andReturn("xuser4").anyTimes();
+    replay(externalUser1, externalUser2, externalUser3, externalUser4);
+
+    expect(populator.getLdapUsers("xuser*")).andReturn(createSet(externalUser3, externalUser4));
+    expect(populator.getLdapUsers("user1")).andReturn(Collections.singleton(externalUser1));
+    expect(populator.getLdapUsers("user2")).andReturn(Collections.EMPTY_SET);
+    replay(populator);
+
+    populator.setLdapTemplate(ldapTemplate);
+    populator.setLdapServerProperties(ldapServerProperties);
+
+    populator.synchronizeLdapUsers(createSet("user1", "user2", "xuser*"), new LdapBatchDto());
+  }
+
+  @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();
+
+    Configuration configuration = createNiceMock(Configuration.class);
+    Users users = createNiceMock(Users.class);
+    LdapTemplate ldapTemplate = createNiceMock(LdapTemplate.class);
+    LdapServerProperties ldapServerProperties = createNiceMock(LdapServerProperties.class);
+
+    replay(ldapTemplate, ldapServerProperties, users, configuration);
+    replay(user1, user2, user3, user4);
+
+    AmbariLdapDataPopulatorTestInstance populator = createMockBuilder(AmbariLdapDataPopulatorTestInstance.class)
+        .addMockedMethod("getLdapUserByMemberAttr")
+        .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(externalUser3).anyTimes();
+    expect(populator.getLdapUserByMemberAttr("user6")).andReturn(externalUser4).anyTimes();
+    expect(populator.getInternalMembers("group1")).andReturn(internalMembers);
+    replay(populator);
+
+    populator.setLdapTemplate(ldapTemplate);
+    populator.setLdapServerProperties(ldapServerProperties);
+
+    Map<String, User> internalUsers = new HashMap<String, User>();
+    internalUsers.putAll(internalMembers);
+    internalUsers.put("user2", user2);
+
+    populator.refreshGroupMembers(batchInfo, externalGroup, internalUsers);
+
+    Set<String> groupMembersToAdd = new HashSet<String>();
+    for (LdapUserGroupMemberDto ldapUserGroupMemberDto : batchInfo.getMembershipToAdd()) {
+      groupMembersToAdd.add(ldapUserGroupMemberDto.getUserName());
+    }
+    assertEquals(2, 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(1, groupMembersToRemove.size());
+    assertTrue(groupMembersToRemove.contains("user3"));
+    assertEquals(1, batchInfo.getUsersToBeCreated().size());
+    assertTrue(batchInfo.getUsersToBeCreated().contains("user6"));
+    assertEquals(2, batchInfo.getUsersToBecomeLdap().size());
+    assertTrue(batchInfo.getUsersToBecomeLdap().contains("user1"));
+    assertTrue(batchInfo.getUsersToBecomeLdap().contains("user4"));
+    assertTrue(batchInfo.getGroupsToBecomeLdap().isEmpty());
+    assertTrue(batchInfo.getGroupsToBeCreated().isEmpty());
+    assertTrue(batchInfo.getGroupsToBeRemoved().isEmpty());
+    assertTrue(batchInfo.getUsersToBeRemoved().isEmpty());
+    verify(populator.loadLdapTemplate(), populator);
   }
 
   @Test
   @SuppressWarnings("serial")
   public void testCleanUpLdapUsersWithoutGroup() throws AmbariException {
-    final Configuration configuration = EasyMock.createNiceMock(Configuration.class);
-    final Users users = EasyMock.createNiceMock(Users.class);
+    final Configuration configuration = createNiceMock(Configuration.class);
+    final Users users = createNiceMock(Users.class);
 
     final GroupEntity ldapGroup = new GroupEntity();
     ldapGroup.setGroupId(1);
@@ -132,12 +1371,12 @@ public class AmbariLdapDataPopulatorTest {
         add(localUserWithGroup);
       }
     };
-    EasyMock.expect(users.getAllUsers()).andReturn(new ArrayList<User>(allUsers));
+    expect(users.getAllUsers()).andReturn(new ArrayList<User>(allUsers));
 
     final List<User> removedUsers = new ArrayList<User>();
     final Capture<User> userCapture = new Capture<User>();
-    users.removeUser(EasyMock.capture(userCapture));
-    EasyMock.expectLastCall().andAnswer(new IAnswer<Void>() {
+    users.removeUser(capture(userCapture));
+    expectLastCall().andAnswer(new IAnswer<Void>() {
       @Override
       public Void answer() throws Throwable {
         removedUsers.add(userCapture.getValue());
@@ -146,19 +1385,21 @@ public class AmbariLdapDataPopulatorTest {
       }
     });
 
-    EasyMock.replay(users);
+    replay(users);
 
-    final AmbariLdapDataPopulator populator = new AmbariLdapDataPopulatorTestInstance(configuration, users);
+    final AmbariLdapDataPopulatorTestInstance populator = new AmbariLdapDataPopulatorTestInstance(configuration, users);
+    populator.setLdapTemplate(createNiceMock(LdapTemplate.class));
+    populator.setLdapServerProperties(createNiceMock(LdapServerProperties.class));
     populator.cleanUpLdapUsersWithoutGroup();
 
-    Assert.assertEquals(removedUsers.size(), 1);
-    Assert.assertEquals(allUsers.size(), 3);
-    Assert.assertTrue(allUsers.contains(ldapUserWithGroup));
-    Assert.assertTrue(allUsers.contains(localUserWithoutGroup));
-    Assert.assertTrue(allUsers.contains(localUserWithGroup));
-    Assert.assertEquals(removedUsers.get(0), ldapUserWithoutGroup);
+    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);
 
-    EasyMock.verify(users);
+    verify(users);
   }
 
   private static int userIdCounter = 1;

http://git-wip-us.apache.org/repos/asf/ambari/blob/21d784b6/ambari-server/src/test/java/org/apache/ambari/server/security/ldap/LdapPerformanceTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/security/ldap/LdapPerformanceTest.java b/ambari-server/src/test/java/org/apache/ambari/server/security/ldap/LdapPerformanceTest.java
index a2cb388..a816da6 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/security/ldap/LdapPerformanceTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/security/ldap/LdapPerformanceTest.java
@@ -81,14 +81,16 @@ public class LdapPerformanceTest {
     }
     System.out.println("Data fetch: " + (System.currentTimeMillis() - time));
     time = System.currentTimeMillis();
-    LdapBatchDto batchDto = populator.synchronizeLdapUsersAndGroups(userNames, groupNames);
-    batchDto = populator.synchronizeLdapUsersAndGroups(userNames, groupNames);
+    LdapBatchDto batchDto = new LdapBatchDto();
+    populator.synchronizeLdapUsers(userNames, batchDto);
+    populator.synchronizeLdapGroups(groupNames, batchDto);
     this.users.processLdapSync(batchDto);
     System.out.println("Initial sync: " + (System.currentTimeMillis() - time));
     time = System.currentTimeMillis();
-    batchDto = populator.synchronizeLdapUsersAndGroups(userNames, groupNames);
+    batchDto = new LdapBatchDto();
+    populator.synchronizeLdapUsers(userNames, batchDto);
+    populator.synchronizeLdapGroups(groupNames, batchDto);
     this.users.processLdapSync(batchDto);
     System.out.println("Subsequent sync: " + (System.currentTimeMillis() - time));
-    time = System.currentTimeMillis();
   }
 }


Mime
View raw message