LdapSettingsManager { public Map<String, LdapContextFactory> getContextFactories() { if (contextFactories == null) { contextFactories = new LinkedHashMap<>(); String[] serverKeys = settings.getStringArray(LDAP_SERVERS_PROPERTY); if (serverKeys.length > 0) { initMultiLdapConfiguration(serverKeys); } else { initSimpleLdapConfiguration(); } } return contextFactories; } LdapSettingsManager(Settings settings, LdapAutodiscovery ldapAutodiscovery); Map<String, LdapUserMapping> getUserMappings(); Map<String, LdapGroupMapping> getGroupMappings(); Map<String, LdapContextFactory> getContextFactories();  }
@Test public void shouldFailWhenNoLdapUrl() throws Exception { Settings settings = generateMultipleLdapSettingsWithUserAndGroupMapping(); settings.removeProperty("ldap.example.url"); LdapSettingsManager settingsManager = new LdapSettingsManager(settings, new LdapAutodiscovery()); thrown.expect(LdapException.class); thrown.expectMessage("The property 'ldap.example.url' property is empty while it is mandatory."); settingsManager.getContextFactories(); }
@Test public void shouldFailWhenMixingSingleAndMultipleConfiguration() throws Exception { Settings settings = generateMultipleLdapSettingsWithUserAndGroupMapping(); settings.setProperty("ldap.url", "ldap: LdapSettingsManager settingsManager = new LdapSettingsManager(settings, new LdapAutodiscovery()); thrown.expect(LdapException.class); thrown .expectMessage("When defining multiple LDAP servers with the property 'ldap.servers', all LDAP properties must be linked to one of those servers. Please remove properties like 'ldap.url', 'ldap.realm', ..."); settingsManager.getContextFactories(); }
@Test public void testContextFactoriesWithSingleLdap() throws Exception { LdapSettingsManager settingsManager = new LdapSettingsManager( generateSingleLdapSettingsWithUserAndGroupMapping(), new LdapAutodiscovery()); assertThat(settingsManager.getContextFactories().size()).isEqualTo(1); }
@Test public void testContextFactoriesWithMultipleLdap() throws Exception { LdapSettingsManager settingsManager = new LdapSettingsManager( generateMultipleLdapSettingsWithUserAndGroupMapping(), new LdapAutodiscovery()); assertThat(settingsManager.getContextFactories().size()).isEqualTo(2); assertThat(settingsManager.getContextFactories().size()).isEqualTo(2); }
@Test public void testAutodiscover() throws Exception { LdapAutodiscovery ldapAutodiscovery = mock(LdapAutodiscovery.class); LdapSrvRecord ldap1 = new LdapSrvRecord("ldap: LdapSrvRecord ldap2 = new LdapSrvRecord("ldap: when(ldapAutodiscovery.getLdapServers("example.org")).thenReturn(Arrays.asList(ldap1, ldap2)); LdapSettingsManager settingsManager = new LdapSettingsManager( generateAutodiscoverSettings(), ldapAutodiscovery); assertThat(settingsManager.getContextFactories().size()).isEqualTo(2); }
@Test public void testAutodiscoverFailed() throws Exception { LdapAutodiscovery ldapAutodiscovery = mock(LdapAutodiscovery.class); when(ldapAutodiscovery.getLdapServers("example.org")).thenReturn(Collections.<LdapSrvRecord>emptyList()); LdapSettingsManager settingsManager = new LdapSettingsManager( generateAutodiscoverSettings(), ldapAutodiscovery); thrown.expect(LdapException.class); thrown.expectMessage("The property 'ldap.url' is empty and SonarQube is not able to auto-discover any LDAP server."); settingsManager.getContextFactories(); }
@Test public void testEmptySettings() throws Exception { LdapSettingsManager settingsManager = new LdapSettingsManager( new Settings(), new LdapAutodiscovery()); thrown.expect(LdapException.class); thrown.expectMessage("The property 'ldap.url' is empty and no realm configured to try auto-discovery."); settingsManager.getContextFactories(); }