Search in sources :

Example 1 with ReadOnlySearchRequest

use of com.unboundid.ldap.sdk.ReadOnlySearchRequest in project ldapsdk by pingidentity.

the class InMemoryDirectoryServerLDAPInterfaceTestCase method testSearchForEntry.

/**
 * Provides test coverage for the methods that can be used to process search
 * operations that are only expected to return a single entry.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testSearchForEntry() throws Exception {
    ds.restoreSnapshot(snapshot);
    // Test the method that takes a base, scope, string filter, and attributes,
    // with no explicitly-requested attributes.
    SearchResultEntry entry = ds.searchForEntry("dc=example,dc=com", SearchScope.SUB, "(uid=test.user)");
    assertNotNull(entry);
    assertTrue(entry.hasAttribute("objectClass"));
    assertTrue(entry.hasAttribute("uid"));
    assertTrue(entry.hasAttribute("givenName"));
    assertTrue(entry.hasAttribute("sn"));
    assertTrue(entry.hasAttribute("cn"));
    assertFalse(entry.hasAttribute("entryDN"));
    assertFalse(entry.hasAttribute("entryUUID"));
    assertFalse(entry.hasAttribute("creatorsName"));
    assertFalse(entry.hasAttribute("createTimestamp"));
    assertFalse(entry.hasAttribute("modifiersName"));
    assertFalse(entry.hasAttribute("modifyTimestamp"));
    assertFalse(entry.hasAttribute("subschemaSubentry"));
    // Test the method that takes a base, scope, string filter, and attributes,
    // with all user and operational attributes.
    entry = ds.searchForEntry("dc=example,dc=com", SearchScope.SUB, Filter.createEqualityFilter("uid", "test.user"), "*", "+");
    assertNotNull(entry);
    assertTrue(entry.hasAttribute("objectClass"));
    assertTrue(entry.hasAttribute("uid"));
    assertTrue(entry.hasAttribute("givenName"));
    assertTrue(entry.hasAttribute("sn"));
    assertTrue(entry.hasAttribute("cn"));
    assertTrue(entry.hasAttribute("entryDN"));
    assertTrue(entry.hasAttribute("entryUUID"));
    assertTrue(entry.hasAttribute("creatorsName"));
    assertTrue(entry.hasAttribute("createTimestamp"));
    assertTrue(entry.hasAttribute("modifiersName"));
    assertTrue(entry.hasAttribute("modifyTimestamp"));
    assertTrue(entry.hasAttribute("subschemaSubentry"));
    // Test the method that takes all elements and a string filter with a
    // filter that doesn't match any entry.
    entry = ds.searchForEntry("dc=example,dc=com", SearchScope.SUB, DereferencePolicy.NEVER, 0, false, "(uid=does.not.match)");
    assertNull(entry);
    // Test the method that takes all elements and a filter object with a base
    // DN that targets a nonexistent entry.
    entry = ds.searchForEntry("cn=missing,dc=example,dc=com", SearchScope.SUB, DereferencePolicy.NEVER, 0, false, Filter.createPresenceFilter("objectClass"));
    assertNull(entry);
    // Test the method that takes a search request with criteria that matches a
    // single entry.
    entry = ds.searchForEntry(new SearchRequest("dc=example,dc=com", SearchScope.SUB, "(uid=test.user)", "objectClass", "cn", "entryUUID"));
    assertNotNull(entry);
    assertTrue(entry.hasAttribute("objectClass"));
    assertFalse(entry.hasAttribute("uid"));
    assertFalse(entry.hasAttribute("givenName"));
    assertFalse(entry.hasAttribute("sn"));
    assertTrue(entry.hasAttribute("cn"));
    assertFalse(entry.hasAttribute("entryDN"));
    assertTrue(entry.hasAttribute("entryUUID"));
    assertFalse(entry.hasAttribute("creatorsName"));
    assertFalse(entry.hasAttribute("createTimestamp"));
    assertFalse(entry.hasAttribute("modifiersName"));
    assertFalse(entry.hasAttribute("modifyTimestamp"));
    assertFalse(entry.hasAttribute("subschemaSubentry"));
    // multiple entries.
    try {
        final SearchRequest searchRequest = new SearchRequest("dc=example,dc=com", SearchScope.SUB, "(objectClass=*)");
        searchRequest.addControl(new ManageDsaITRequestControl());
        ds.searchForEntry(searchRequest);
        fail("Expected an exception from searchForEntry with criteria that " + "matches multiple entries");
    } catch (final LDAPSearchException lse) {
        // This was expected.
        assertEquals(lse.getResultCode(), ResultCode.SIZE_LIMIT_EXCEEDED);
    }
    // Test the method that takes a read-only search request that matches only a
    // single entry, including a search result listener.
    final TestSearchResultListener searchListener = new TestSearchResultListener();
    final ReadOnlySearchRequest readOnlySearchRequest = new SearchRequest(searchListener, "dc=example,dc=com", SearchScope.SUB, "(uid=test.user)", "1.1");
    entry = ds.searchForEntry(readOnlySearchRequest);
    assertNotNull(entry);
    assertFalse(entry.hasAttribute("objectClass"));
    assertFalse(entry.hasAttribute("uid"));
    assertFalse(entry.hasAttribute("givenName"));
    assertFalse(entry.hasAttribute("sn"));
    assertFalse(entry.hasAttribute("cn"));
    assertFalse(entry.hasAttribute("entryDN"));
    assertFalse(entry.hasAttribute("entryDN"));
    assertFalse(entry.hasAttribute("creatorsName"));
    assertFalse(entry.hasAttribute("createTimestamp"));
    assertFalse(entry.hasAttribute("modifiersName"));
    assertFalse(entry.hasAttribute("modifyTimestamp"));
    assertFalse(entry.hasAttribute("subschemaSubentry"));
}
Also used : SearchRequest(com.unboundid.ldap.sdk.SearchRequest) ReadOnlySearchRequest(com.unboundid.ldap.sdk.ReadOnlySearchRequest) ReadOnlySearchRequest(com.unboundid.ldap.sdk.ReadOnlySearchRequest) TestSearchResultListener(com.unboundid.ldap.sdk.TestSearchResultListener) LDAPSearchException(com.unboundid.ldap.sdk.LDAPSearchException) ManageDsaITRequestControl(com.unboundid.ldap.sdk.controls.ManageDsaITRequestControl) SearchResultEntry(com.unboundid.ldap.sdk.SearchResultEntry) Test(org.testng.annotations.Test)

Example 2 with ReadOnlySearchRequest

use of com.unboundid.ldap.sdk.ReadOnlySearchRequest in project ldapsdk by pingidentity.

the class InMemoryDirectoryServerLDAPInterfaceTestCase method testSearch.

/**
 * Provides test coverage for the methods that can be used to process search
 * operations.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testSearch() throws Exception {
    ds.restoreSnapshot(snapshot);
    ds.add("dn: ou=Users,dc=example,dc=com", "objectClass: top", "objectClass: referral", "objectClass: extensibleObject", "ou: Users", "ref: ldap://directory.example.com/ou=Users,dc=example,dc=com");
    // Test the method that takes a base, scope, string filter, and attributes,
    // with no explicitly-requested attributes.
    SearchResult searchResult = ds.search("dc=example,dc=com", SearchScope.SUB, "(objectClass=*)");
    assertNotNull(searchResult);
    assertEquals(searchResult.getResultCode(), ResultCode.SUCCESS);
    assertEquals(searchResult.getEntryCount(), 3);
    assertNotNull(searchResult.getSearchEntries());
    assertEquals(searchResult.getSearchEntries().size(), 3);
    for (final SearchResultEntry e : searchResult.getSearchEntries()) {
        assertTrue(e.hasAttribute("objectClass"));
        assertFalse(e.hasAttribute("entryDN"));
        assertFalse(e.hasAttribute("entryUUID"));
        assertFalse(e.hasAttribute("creatorsName"));
        assertFalse(e.hasAttribute("createTimestamp"));
        assertFalse(e.hasAttribute("modifiersName"));
        assertFalse(e.hasAttribute("modifyTimestamp"));
        assertFalse(e.hasAttribute("subschemaSubentry"));
    }
    assertEquals(searchResult.getReferenceCount(), 1);
    assertNotNull(searchResult.getSearchReferences());
    assertEquals(searchResult.getSearchReferences().size(), 1);
    // Test the method that takes a base, scope, filter object, and attributes,
    // with all user and operational attributes.
    searchResult = ds.search("dc=example,dc=com", SearchScope.SUB, Filter.createPresenceFilter("objectClass"), "*", "+");
    assertNotNull(searchResult);
    assertEquals(searchResult.getResultCode(), ResultCode.SUCCESS);
    assertEquals(searchResult.getEntryCount(), 3);
    assertNotNull(searchResult.getSearchEntries());
    assertEquals(searchResult.getSearchEntries().size(), 3);
    for (final SearchResultEntry e : searchResult.getSearchEntries()) {
        assertTrue(e.hasAttribute("objectClass"));
        assertTrue(e.hasAttribute("entryDN"));
        assertTrue(e.hasAttribute("entryUUID"));
        assertTrue(e.hasAttribute("creatorsName"));
        assertTrue(e.hasAttribute("createTimestamp"));
        assertTrue(e.hasAttribute("modifiersName"));
        assertTrue(e.hasAttribute("modifyTimestamp"));
        assertTrue(e.hasAttribute("subschemaSubentry"));
    }
    assertEquals(searchResult.getReferenceCount(), 1);
    assertNotNull(searchResult.getSearchReferences());
    assertEquals(searchResult.getSearchReferences().size(), 1);
    // Test the method that takes a search result listener, base, scope,
    // string filter, and attributes.
    final TestSearchResultListener searchListener = new TestSearchResultListener();
    searchResult = ds.search(searchListener, "dc=example,dc=com", SearchScope.SUB, "(objectClass=*)");
    assertNotNull(searchResult);
    assertEquals(searchResult.getResultCode(), ResultCode.SUCCESS);
    assertEquals(searchResult.getEntryCount(), 3);
    assertNull(searchResult.getSearchEntries());
    assertEquals(searchResult.getReferenceCount(), 1);
    assertNull(searchResult.getSearchReferences());
    // Test the method that takes a search result listener, base, scope,
    // object filter, and attributes.
    searchResult = ds.search(searchListener, "dc=example,dc=com", SearchScope.SUB, Filter.createPresenceFilter("objectClass"));
    assertNotNull(searchResult);
    assertEquals(searchResult.getResultCode(), ResultCode.SUCCESS);
    assertEquals(searchResult.getEntryCount(), 3);
    assertNull(searchResult.getSearchEntries());
    assertEquals(searchResult.getReferenceCount(), 1);
    assertNull(searchResult.getSearchReferences());
    // Test the method that takes all search elements with a string filter and
    // no listener.
    searchResult = ds.search("dc=example,dc=com", SearchScope.SUB, DereferencePolicy.NEVER, 0, 0, false, "(objectClass=*)", "objectClass", "entryUUID");
    assertNotNull(searchResult);
    assertEquals(searchResult.getResultCode(), ResultCode.SUCCESS);
    assertEquals(searchResult.getEntryCount(), 3);
    assertNotNull(searchResult.getSearchEntries());
    assertEquals(searchResult.getSearchEntries().size(), 3);
    for (final SearchResultEntry e : searchResult.getSearchEntries()) {
        assertTrue(e.hasAttribute("objectClass"));
        assertFalse(e.hasAttribute("entryDN"));
        assertTrue(e.hasAttribute("entryUUID"));
        assertFalse(e.hasAttribute("creatorsName"));
        assertFalse(e.hasAttribute("createTimestamp"));
        assertFalse(e.hasAttribute("modifiersName"));
        assertFalse(e.hasAttribute("modifyTimestamp"));
        assertFalse(e.hasAttribute("subschemaSubentry"));
    }
    assertEquals(searchResult.getReferenceCount(), 1);
    assertNotNull(searchResult.getSearchReferences());
    assertEquals(searchResult.getSearchReferences().size(), 1);
    // Test the method that takes all search elements with a filter object and
    // no listener.
    searchResult = ds.search("dc=example,dc=com", SearchScope.SUB, DereferencePolicy.NEVER, 0, 0, false, Filter.createPresenceFilter("objectClass"), "entryUUID");
    assertNotNull(searchResult);
    assertEquals(searchResult.getResultCode(), ResultCode.SUCCESS);
    assertEquals(searchResult.getEntryCount(), 3);
    assertNotNull(searchResult.getSearchEntries());
    assertEquals(searchResult.getSearchEntries().size(), 3);
    for (final SearchResultEntry e : searchResult.getSearchEntries()) {
        assertFalse(e.hasAttribute("objectClass"));
        assertFalse(e.hasAttribute("entryDN"));
        assertTrue(e.hasAttribute("entryUUID"));
        assertFalse(e.hasAttribute("creatorsName"));
        assertFalse(e.hasAttribute("createTimestamp"));
        assertFalse(e.hasAttribute("modifiersName"));
        assertFalse(e.hasAttribute("modifyTimestamp"));
        assertFalse(e.hasAttribute("subschemaSubentry"));
    }
    assertEquals(searchResult.getReferenceCount(), 1);
    assertNotNull(searchResult.getSearchReferences());
    assertEquals(searchResult.getSearchReferences().size(), 1);
    // Test the method that takes all search elements with a string filter and a
    // search listener.
    searchResult = ds.search(searchListener, "dc=example,dc=com", SearchScope.SUB, DereferencePolicy.NEVER, 0, 0, false, "(objectClass=*)");
    assertNotNull(searchResult);
    assertEquals(searchResult.getResultCode(), ResultCode.SUCCESS);
    assertEquals(searchResult.getEntryCount(), 3);
    assertNull(searchResult.getSearchEntries());
    assertEquals(searchResult.getReferenceCount(), 1);
    assertNull(searchResult.getSearchReferences());
    // Test the method that takes all search elements with a filter object and a
    // search listener.
    searchResult = ds.search(searchListener, "dc=example,dc=com", SearchScope.SUB, DereferencePolicy.NEVER, 0, 0, false, Filter.createPresenceFilter("objectClass"));
    assertNotNull(searchResult);
    assertEquals(searchResult.getResultCode(), ResultCode.SUCCESS);
    assertEquals(searchResult.getEntryCount(), 3);
    assertNull(searchResult.getSearchEntries());
    assertEquals(searchResult.getReferenceCount(), 1);
    assertNull(searchResult.getSearchReferences());
    // Test the method that takes a search request, including controls.
    final SearchRequest searchRequest = new SearchRequest("dc=example,dc=com", SearchScope.SUB, "(objectClass=*)", "1.1");
    searchRequest.addControl(new ManageDsaITRequestControl());
    searchResult = ds.search(searchRequest);
    assertNotNull(searchResult);
    assertEquals(searchResult.getResultCode(), ResultCode.SUCCESS);
    assertEquals(searchResult.getEntryCount(), 4);
    assertNotNull(searchResult.getSearchEntries());
    assertEquals(searchResult.getSearchEntries().size(), 4);
    for (final SearchResultEntry e : searchResult.getSearchEntries()) {
        assertFalse(e.hasAttribute("objectClass"));
        assertFalse(e.hasAttribute("entryDN"));
        assertFalse(e.hasAttribute("entryUUID"));
        assertFalse(e.hasAttribute("creatorsName"));
        assertFalse(e.hasAttribute("createTimestamp"));
        assertFalse(e.hasAttribute("modifiersName"));
        assertFalse(e.hasAttribute("modifyTimestamp"));
        assertFalse(e.hasAttribute("subschemaSubentry"));
    }
    assertEquals(searchResult.getReferenceCount(), 0);
    assertNotNull(searchResult.getSearchReferences());
    assertEquals(searchResult.getSearchReferences().size(), 0);
    // Test the method that takes a read-only search request.
    final ReadOnlySearchRequest readOnlySearchRequest = new SearchRequest(searchListener, "dc=example,dc=com", SearchScope.SUB, "(objectClass=*)");
    searchResult = ds.search(readOnlySearchRequest);
    assertNotNull(searchResult);
    assertEquals(searchResult.getResultCode(), ResultCode.SUCCESS);
    assertEquals(searchResult.getEntryCount(), 3);
    assertNull(searchResult.getSearchEntries());
    assertEquals(searchResult.getReferenceCount(), 1);
    assertNull(searchResult.getSearchReferences());
}
Also used : SearchRequest(com.unboundid.ldap.sdk.SearchRequest) ReadOnlySearchRequest(com.unboundid.ldap.sdk.ReadOnlySearchRequest) ReadOnlySearchRequest(com.unboundid.ldap.sdk.ReadOnlySearchRequest) TestSearchResultListener(com.unboundid.ldap.sdk.TestSearchResultListener) SearchResult(com.unboundid.ldap.sdk.SearchResult) ManageDsaITRequestControl(com.unboundid.ldap.sdk.controls.ManageDsaITRequestControl) SearchResultEntry(com.unboundid.ldap.sdk.SearchResultEntry) Test(org.testng.annotations.Test)

Aggregations

ReadOnlySearchRequest (com.unboundid.ldap.sdk.ReadOnlySearchRequest)2 SearchRequest (com.unboundid.ldap.sdk.SearchRequest)2 SearchResultEntry (com.unboundid.ldap.sdk.SearchResultEntry)2 TestSearchResultListener (com.unboundid.ldap.sdk.TestSearchResultListener)2 ManageDsaITRequestControl (com.unboundid.ldap.sdk.controls.ManageDsaITRequestControl)2 Test (org.testng.annotations.Test)2 LDAPSearchException (com.unboundid.ldap.sdk.LDAPSearchException)1 SearchResult (com.unboundid.ldap.sdk.SearchResult)1