@Test public void testGetAllLdapNamesWithExceptionInNamingEnumerationClose() throws NamingException { MockResultCollection resultCollection = MockResultCollection.create() .addSearchResultWithDns("1") .addSearchResultWithDns("2"); doThrow(NamingException.class).when(resultCollection.iterator().next()).close(); handler = new SearchResultHandler(resultCollection); List<String> actual = handler.getAllLdapNames(); assertEquals("Resultset size", 2, actual.size()); assertAllNamingEnumerationsClosed(resultCollection); }
@Test public void testGetAllLdapNames() throws NamingException { String objectDn1 = "cn=a1,dc=b,dc=c"; String objectDn2 = "cn=a2,dc=b,dc=c"; String objectDn3 = "cn=a3,dc=b,dc=c"; MockResultCollection resultCollection = MockResultCollection.create() .addSearchResultWithDns(objectDn1) .addSearchResultWithDns(objectDn2, objectDn3); handler = new SearchResultHandler(resultCollection); List<String> expected = Arrays.asList(objectDn1, objectDn2, objectDn3); Collections.sort(expected); List<String> actual = handler.getAllLdapNames(); Collections.sort(actual); assertEquals(expected, actual); assertAllNamingEnumerationsClosed(resultCollection); }
@Test public void testHandle() throws NamingException { MockResultCollection resultCollection = MockResultCollection.create() .addSearchResultWithDns("1") .addSearchResultWithDns("2", "3"); handler = new SearchResultHandler(resultCollection); List<String> expected = Arrays.asList("1", "2"); final List<String> actual = new ArrayList<>(); handler.handle(new SearchResultHandler.RecordProcessor() { @Override public boolean process(SearchResult record) throws NamingException { actual.add(record.getNameInNamespace()); return actual.size() < 2; } }); assertEquals(expected, actual); assertAllNamingEnumerationsClosed(resultCollection); }
@Test public void testHasSingleResultManyRecords() throws NamingException { MockResultCollection resultCollection = MockResultCollection.create() .addSearchResultWithDns("1") .addSearchResultWithDns("2"); handler = new SearchResultHandler(resultCollection); assertFalse(handler.hasSingleResult()); assertAllNamingEnumerationsClosed(resultCollection); }
@Test public void testHasSingleResult() throws NamingException { MockResultCollection resultCollection = MockResultCollection.create() .addSearchResultWithDns("1"); handler = new SearchResultHandler(resultCollection); assertTrue(handler.hasSingleResult()); assertAllNamingEnumerationsClosed(resultCollection); }
@Test public void testGetSingleLdapName() throws NamingException { String objectDn = "cn=a,dc=b,dc=c"; MockResultCollection resultCollection = MockResultCollection.create() .addEmptySearchResult() .addSearchResultWithDns(objectDn); handler = new SearchResultHandler(resultCollection); String expected = objectDn; String actual = handler.getSingleLdapName(); assertEquals(expected, actual); assertAllNamingEnumerationsClosed(resultCollection); }