AmbariLdapDataPopulator { public LdapBatchDto synchronizeAllLdapUsers(LdapBatchDto batchInfo) throws AmbariException { LOG.trace("Synchronize All LDAP users..."); Set<LdapUserDto> externalLdapUserInfo = getExternalLdapUserInfo(); Map<String, User> internalUsersMap = getInternalUsers(); for (LdapUserDto userDto : externalLdapUserInfo) { String userName = userDto.getUserName(); if (internalUsersMap.containsKey(userName)) { final User user = internalUsersMap.get(userName); if (user != null && !user.isLdapUser()) { if (Configuration.LdapUsernameCollisionHandlingBehavior.SKIP == configuration.getLdapSyncCollisionHandlingBehavior()) { LOG.info("User '{}' skipped because it is local user", userName); batchInfo.getUsersSkipped().add(userName); } else { batchInfo.getUsersToBecomeLdap().add(userName); LOG.trace("Convert user '{}' to LDAP user.", userName); } } internalUsersMap.remove(userName); } else { batchInfo.getUsersToBeCreated().add(userName); } } for (Entry<String, User> internalUser : internalUsersMap.entrySet()) { if (internalUser.getValue().isLdapUser()) { batchInfo.getUsersToBeRemoved().add(internalUser.getValue().getUserName()); } } 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 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); }