@Test(expected = ConstrettoException.class) public void testAddUsingSearchKeyFailes() throws Exception { final String filter = "(objectClass=person)"; when(dirContext.search(any(Name.class), eq(filter), any(SearchControls.class))).thenThrow(new NamingException()); final String noneExistingKey = "NoneExistingKey"; try { ldapConfigurationStoreBuilder.addUsingSearch("dc=here,dc=com", filter, noneExistingKey); } finally { verify(dirContext).search(any(Name.class), eq(filter), any(SearchControls.class)); } }
public LdapConfigurationStoreBuilder addUsingSearch(final String searchBase, final String filter, final String keyAttribute, final String... tags) { SearchControls searchControls = new SearchControls(); searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE); try { final NamingEnumeration<SearchResult> searchResultNamingEnumeration = dirContext.search(createName( searchBase), filter, searchControls); while (searchResultNamingEnumeration.hasMore()) { final SearchResult result = searchResultNamingEnumeration.next(); final Attributes attributes = result.getAttributes(); final Attribute attribute = attributes.get(keyAttribute); if (attribute == null) { throw new ConstrettoException(String.format( "The LDAP object \"%1$s\" has no attribute value for attribute \"%2$s\"", result.getName(), keyAttribute)); } final String key = attribute.get().toString(); ldapConfigurationStore = new LdapConfigurationStore(ldapConfigurationStore, key, attributes, tags); } } catch (NamingException e) { throw new ConstrettoException( String.format("An error occurred while searching LDAP using searchBase \"%1$s\" and filter \"%2$s\"", searchBase, filter), e); } return this; }
LdapConfigurationStoreBuilder { public LdapConfigurationStoreBuilder addUsingSearch(final String searchBase, final String filter, final String keyAttribute, final String... tags) { SearchControls searchControls = new SearchControls(); searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE); try { final NamingEnumeration<SearchResult> searchResultNamingEnumeration = dirContext.search(createName( searchBase), filter, searchControls); while (searchResultNamingEnumeration.hasMore()) { final SearchResult result = searchResultNamingEnumeration.next(); final Attributes attributes = result.getAttributes(); final Attribute attribute = attributes.get(keyAttribute); if (attribute == null) { throw new ConstrettoException(String.format( "The LDAP object \"%1$s\" has no attribute value for attribute \"%2$s\"", result.getName(), keyAttribute)); } final String key = attribute.get().toString(); ldapConfigurationStore = new LdapConfigurationStore(ldapConfigurationStore, key, attributes, tags); } } catch (NamingException e) { throw new ConstrettoException( String.format("An error occurred while searching LDAP using searchBase \"%1$s\" and filter \"%2$s\"", searchBase, filter), e); } return this; } }
LdapConfigurationStoreBuilder { public LdapConfigurationStoreBuilder addUsingSearch(final String searchBase, final String filter, final String keyAttribute, final String... tags) { SearchControls searchControls = new SearchControls(); searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE); try { final NamingEnumeration<SearchResult> searchResultNamingEnumeration = dirContext.search(createName( searchBase), filter, searchControls); while (searchResultNamingEnumeration.hasMore()) { final SearchResult result = searchResultNamingEnumeration.next(); final Attributes attributes = result.getAttributes(); final Attribute attribute = attributes.get(keyAttribute); if (attribute == null) { throw new ConstrettoException(String.format( "The LDAP object \"%1$s\" has no attribute value for attribute \"%2$s\"", result.getName(), keyAttribute)); } final String key = attribute.get().toString(); ldapConfigurationStore = new LdapConfigurationStore(ldapConfigurationStore, key, attributes, tags); } } catch (NamingException e) { throw new ConstrettoException( String.format("An error occurred while searching LDAP using searchBase \"%1$s\" and filter \"%2$s\"", searchBase, filter), e); } return this; } private  LdapConfigurationStoreBuilder(final DirContext dirContext); }
LdapConfigurationStoreBuilder { public LdapConfigurationStoreBuilder addUsingSearch(final String searchBase, final String filter, final String keyAttribute, final String... tags) { SearchControls searchControls = new SearchControls(); searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE); try { final NamingEnumeration<SearchResult> searchResultNamingEnumeration = dirContext.search(createName( searchBase), filter, searchControls); while (searchResultNamingEnumeration.hasMore()) { final SearchResult result = searchResultNamingEnumeration.next(); final Attributes attributes = result.getAttributes(); final Attribute attribute = attributes.get(keyAttribute); if (attribute == null) { throw new ConstrettoException(String.format( "The LDAP object \"%1$s\" has no attribute value for attribute \"%2$s\"", result.getName(), keyAttribute)); } final String key = attribute.get().toString(); ldapConfigurationStore = new LdapConfigurationStore(ldapConfigurationStore, key, attributes, tags); } } catch (NamingException e) { throw new ConstrettoException( String.format("An error occurred while searching LDAP using searchBase \"%1$s\" and filter \"%2$s\"", searchBase, filter), e); } return this; } private  LdapConfigurationStoreBuilder(final DirContext dirContext); static LdapConfigurationStoreBuilder usingDirContext(final DirContext dirContext); LdapConfigurationStoreBuilder addDsnWithKey(final String key,
                                                       final String distinguishedName,
                                                       String... tags); LdapConfigurationStoreBuilder addDsn(final String distinguishedName, final String... tags); LdapConfigurationStoreBuilder addUsingSearch(final String searchBase,
                                                        final String filter,
                                                        final String keyAttribute,
                                                        final String... tags); LdapConfigurationStore done(); }
LdapConfigurationStoreBuilder { public LdapConfigurationStoreBuilder addUsingSearch(final String searchBase, final String filter, final String keyAttribute, final String... tags) { SearchControls searchControls = new SearchControls(); searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE); try { final NamingEnumeration<SearchResult> searchResultNamingEnumeration = dirContext.search(createName( searchBase), filter, searchControls); while (searchResultNamingEnumeration.hasMore()) { final SearchResult result = searchResultNamingEnumeration.next(); final Attributes attributes = result.getAttributes(); final Attribute attribute = attributes.get(keyAttribute); if (attribute == null) { throw new ConstrettoException(String.format( "The LDAP object \"%1$s\" has no attribute value for attribute \"%2$s\"", result.getName(), keyAttribute)); } final String key = attribute.get().toString(); ldapConfigurationStore = new LdapConfigurationStore(ldapConfigurationStore, key, attributes, tags); } } catch (NamingException e) { throw new ConstrettoException( String.format("An error occurred while searching LDAP using searchBase \"%1$s\" and filter \"%2$s\"", searchBase, filter), e); } return this; } private  LdapConfigurationStoreBuilder(final DirContext dirContext); static LdapConfigurationStoreBuilder usingDirContext(final DirContext dirContext); LdapConfigurationStoreBuilder addDsnWithKey(final String key,
                                                       final String distinguishedName,
                                                       String... tags); LdapConfigurationStoreBuilder addDsn(final String distinguishedName, final String... tags); LdapConfigurationStoreBuilder addUsingSearch(final String searchBase,
                                                        final String filter,
                                                        final String keyAttribute,
                                                        final String... tags); LdapConfigurationStore done(); static final String NULL_ARGUMENT; }