AmbariLdapDataPopulator { public LdapBatchDto synchronizeExistingLdapGroups(LdapBatchDto batchInfo) throws AmbariException { LOG.trace("Synchronize Existing LDAP groups..."); final Map<String, Group> internalGroupsMap = getInternalGroups(); final Map<String, User> internalUsersMap = getInternalUsers(); final Set<Group> internalGroupSet = Sets.newHashSet(internalGroupsMap.values()); for (Group group : internalGroupSet) { if (group.isLdapGroup()) { Set<LdapGroupDto> groupDtos = getLdapGroups(group.getGroupName()); if (groupDtos.isEmpty()) { batchInfo.getGroupsToBeRemoved().add(group.getGroupName()); } else { LdapGroupDto groupDto = groupDtos.iterator().next(); refreshGroupMembers(batchInfo, groupDto, internalUsersMap, internalGroupsMap, null, true); } } } 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 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.<User>emptyList()); 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.<LdapGroupDto>emptySet()); LdapGroupDto externalGroup1 = createNiceMock(LdapGroupDto.class); LdapBatchDto batchInfo = new LdapBatchDto(); populator.refreshGroupMembers(eq(batchInfo), eq(externalGroup1), EasyMock.<Map<String, User>>anyObject(), EasyMock.<Map<String, Group>>anyObject(), EasyMock.<Set<String>>anyObject(), anyBoolean()); expectLastCall(); expect(populator.getLdapGroups("group4")).andReturn(Collections.singleton(externalGroup1)); expect(populator.getLdapGroups("group5")).andReturn(Collections.<LdapGroupDto>emptySet()); 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); }