public static Attributes mockEmptyAttributes() throws NamingException { return mockAttributes(); }
public static Attributes mockAttributes(String name, String value) throws NamingException { return mockAttributes(new NameValues(name, value)); }
public static Attributes mockAttributes(String name1, String value1, String name2, String value2) throws NamingException { if (name1.equals(name2)) { return mockAttributes(new NameValues(name1, value1, value2)); } else { return mockAttributes(new NameValues(name1, value1), new NameValues(name2, value2)); } }
@Test public void testExecuteCustomQuery() throws NamingException { conf.setVar(HiveConf.ConfVars.HIVE_SERVER2_PLAIN_LDAP_BASEDN, "dc=example,dc=com"); NamingEnumeration<SearchResult> customQueryResult = mockNamingEnumeration( mockSearchResult( "uid=group1,ou=Groups,dc=example,dc=com", mockAttributes("member", "uid=user1,ou=People,dc=example,dc=com")), mockSearchResult( "uid=group2,ou=Groups,dc=example,dc=com", mockAttributes("member", "uid=user2,ou=People,dc=example,dc=com")) ); when(ctx.search(eq("dc=example,dc=com"), anyString(), any(SearchControls.class))) .thenReturn(customQueryResult); search = new LdapSearch(conf, ctx); List<String> expected = Arrays.asList( "uid=group1,ou=Groups,dc=example,dc=com", "uid=user1,ou=People,dc=example,dc=com", "uid=group2,ou=Groups,dc=example,dc=com", "uid=user2,ou=People,dc=example,dc=com"); List<String> actual = search.executeCustomQuery("(&(objectClass=groupOfNames)(|(cn=group1)(cn=group2)))"); Collections.sort(expected); Collections.sort(actual); assertEquals(expected, actual); }
@Test public void testGetAllLdapNamesAndAttributes() throws NamingException { SearchResult searchResult1 = mockSearchResult("cn=a1,dc=b,dc=c", mockAttributes("attr1", "attr1value1")); SearchResult searchResult2 = mockSearchResult("cn=a2,dc=b,dc=c", mockAttributes("attr1", "attr1value2", "attr2", "attr2value1")); SearchResult searchResult3 = mockSearchResult("cn=a3,dc=b,dc=c", mockAttributes("attr1", "attr1value3", "attr1", "attr1value4")); SearchResult searchResult4 = mockSearchResult("cn=a4,dc=b,dc=c", mockEmptyAttributes()); MockResultCollection resultCollection = MockResultCollection.create() .addSearchResults(searchResult1) .addSearchResults(searchResult2, searchResult3) .addSearchResults(searchResult4); handler = new SearchResultHandler(resultCollection); List<String> expected = Arrays.asList( "cn=a1,dc=b,dc=c", "attr1value1", "cn=a2,dc=b,dc=c", "attr1value2", "attr2value1", "cn=a3,dc=b,dc=c", "attr1value3", "attr1value4", "cn=a4,dc=b,dc=c"); Collections.sort(expected); List<String> actual = handler.getAllLdapNamesAndAttributes(); Collections.sort(actual); assertEquals(expected, actual); assertAllNamingEnumerationsClosed(resultCollection); }