AmbariLdapDataPopulator { protected LdapTemplate loadLdapTemplate() { final LdapServerProperties properties = configuration .getLdapServerProperties(); if (ldapTemplate == null || !properties.equals(ldapServerProperties)) { LOG.info("Reloading properties"); ldapServerProperties = properties; final LdapContextSource ldapContextSource = createLdapContextSource(); ldapContextSource.setPooled(true); final List<String> ldapUrls = ldapServerProperties.getLdapUrls(); ldapContextSource.setUrls(ldapUrls.toArray(new String[ldapUrls.size()])); if (!ldapServerProperties.isAnonymousBind()) { ldapContextSource.setUserDn(ldapServerProperties.getManagerDn()); ldapContextSource.setPassword(ldapServerProperties.getManagerPassword()); } try { ldapContextSource.afterPropertiesSet(); } catch (Exception e) { LOG.error("LDAP Context Source not loaded ", e); throw new UsernameNotFoundException("LDAP Context Source not loaded", e); } ldapContextSource.setReferral(ldapServerProperties.getReferralMethod()); ldapTemplate = createLdapTemplate(ldapContextSource); ldapTemplate.setIgnorePartialResultException(true); } return ldapTemplate; } @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 testReferralMethod() { final Configuration configuration = createNiceMock(Configuration.class); final Users users = createNiceMock(Users.class); LdapContextSource ldapContextSource = createNiceMock(LdapContextSource.class); List<String> ldapUrls = Collections.singletonList("url"); LdapTemplate ldapTemplate = createNiceMock(LdapTemplate.class); LdapServerProperties ldapServerProperties = createNiceMock(LdapServerProperties.class); expect(configuration.getLdapServerProperties()).andReturn(ldapServerProperties).anyTimes(); expect(ldapServerProperties.getLdapUrls()).andReturn(ldapUrls).anyTimes(); expect(ldapServerProperties.getReferralMethod()).andReturn("follow"); ldapContextSource.setReferral("follow"); ldapTemplate.setIgnorePartialResultException(true); replay(ldapTemplate, configuration, ldapServerProperties, ldapContextSource); final TestAmbariLdapDataPopulator populator = new TestAmbariLdapDataPopulator(configuration, users); populator.setLdapContextSource(ldapContextSource); populator.setLdapTemplate(ldapTemplate); populator.setLdapServerProperties(ldapServerProperties); populator.loadLdapTemplate(); verify(ldapTemplate, configuration, ldapServerProperties, ldapContextSource); }