AmbariLdapDataPopulator { public LdapBatchDto synchronizeAllLdapGroups(LdapBatchDto batchInfo) throws AmbariException { LOG.trace("Synchronize All LDAP groups..."); Set<LdapGroupDto> externalLdapGroupInfo = getExternalLdapGroupInfo(); final Map<String, Group> internalGroupsMap = getInternalGroups(); final Map<String, User> internalUsersMap = getInternalUsers(); for (LdapGroupDto groupDto : externalLdapGroupInfo) { String groupName = groupDto.getGroupName(); addLdapGroup(batchInfo, internalGroupsMap, groupName); refreshGroupMembers(batchInfo, groupDto, internalUsersMap, internalGroupsMap, null, false); } for (Entry<String, Group> internalGroup : internalGroupsMap.entrySet()) { if (internalGroup.getValue().isLdapGroup()) { batchInfo.getGroupsToBeRemoved().add(internalGroup.getValue().getGroupName()); } } return batchInfo; } @Inject  AmbariLdapDataPopulator(Configuration configuration, Users users); boolean isLdapEnabled(); LdapSyncDto getLdapSyncInfo(); LdapBatchDto synchronizeAllLdapGroups(LdapBatchDto batchInfo); LdapBatchDto synchronizeAllLdapUsers(LdapBatchDto batchInfo); LdapBatchDto synchronizeLdapGroups(Set<String> groups, LdapBatchDto batchInfo); LdapBatchDto synchronizeLdapUsers(Set<String> users, LdapBatchDto batchInfo); LdapBatchDto synchronizeExistingLdapGroups(LdapBatchDto batchInfo); LdapBatchDto synchronizeExistingLdapUsers(LdapBatchDto 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.<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); }