Example usage for javax.naming.directory SearchResult setNameInNamespace

List of usage examples for javax.naming.directory SearchResult setNameInNamespace

Introduction

In this page you can find the example usage for javax.naming.directory SearchResult setNameInNamespace.

Prototype

public void setNameInNamespace(String fullName) 

Source Link

Document

Sets the full name of this binding.

Usage

From source file:net.officefloor.plugin.web.http.security.store.JndiLdapCredentialStoreTest.java

/**
 * Ensure correct roles.// w ww. ja v  a  2s.  com
 */
@SuppressWarnings("unchecked")
public void testRoles() throws Exception {

    // Mocks
    final NamingEnumeration<SearchResult> searchResults = this.createMock(NamingEnumeration.class);
    final Attributes attributes = this.createMock(Attributes.class);
    final Attribute attribute = this.createMock(Attribute.class);

    // Objects
    final SearchResult searchResult = new SearchResult("uid=daniel", null, attributes);
    searchResult.setNameInNamespace("uid=daniel,ou=People,dc=officefloor,dc=net");

    // Record obtaining the Credential Entry
    this.recordReturn(this.context, this.context.search("ou=People,dc=officefloor,dc=net",
            "(&(objectClass=inetOrgPerson)(uid=daniel))", null), searchResults);
    this.recordReturn(searchResults, searchResults.hasMore(), true);
    this.recordReturn(searchResults, searchResults.next(), searchResult);

    // Record obtaining the Groups
    this.recordReturn(this.context, this.context.search("ou=Groups,dc=officefloor,dc=net",
            "(&(objectClass=groupOfNames)" + "(member=uid=daniel,ou=People,dc=officefloor,dc=net))", null),
            searchResults);
    this.recordReturn(searchResults, searchResults.hasMore(), true);
    this.recordReturn(searchResults, searchResults.next(), new SearchResult("cn=developers", null, attributes));
    this.recordReturn(attributes, attributes.get("ou"), attribute);
    this.recordReturn(attribute, attribute.get(), "developer");
    this.recordReturn(searchResults, searchResults.hasMore(), true);
    this.recordReturn(searchResults, searchResults.next(), new SearchResult("cn=founders", null, attributes));
    this.recordReturn(attributes, attributes.get("ou"), attribute);
    this.recordReturn(attribute, attribute.get(), "founder");
    this.recordReturn(searchResults, searchResults.hasMore(), false);

    // Test
    this.replayMockObjects();
    CredentialEntry entry = this.store.retrieveCredentialEntry("daniel", "REALM");
    Set<String> roles = entry.retrieveRoles();
    this.verifyMockObjects();

    // Ensure correct roles
    assertEquals("Incorrect number of roles", 2, roles.size());
    assertTrue("Must have developer role", roles.contains("developer"));
    assertTrue("Must have founder role", roles.contains("founder"));
}

From source file:net.officefloor.plugin.web.http.security.store.JndiLdapCredentialStoreTest.java

/**
 * Ensure correct credentials.//from  w  w w .  j a v  a2 s . c om
 */
@SuppressWarnings("unchecked")
public void testCredentials() throws Exception {

    // Create the expected credentials
    final String expectedRaw = "daniel:officefloor:password";
    MessageDigest digest = MessageDigest.getInstance("MD5");
    digest.update(expectedRaw.getBytes(US_ASCII));
    final byte[] expectedCredentials = digest.digest();

    // Obtain the encoded credentials
    final String encodedCredentials = Base64.encodeBase64String(expectedCredentials).trim();
    assertEquals("Incorrect encoded credentials", "msu723GSLovbwuaPnaLcnQ==", encodedCredentials);

    // Mocks
    final NamingEnumeration<SearchResult> searchResults = this.createMock(NamingEnumeration.class);
    final Attributes attributes = this.createMock(Attributes.class);
    final Attribute attribute = this.createMock(Attribute.class);
    final NamingEnumeration<?> userPasswords = this.createMock(NamingEnumeration.class);

    // Objects
    final SearchResult searchResult = new SearchResult("uid=daniel", null, attributes);
    searchResult.setNameInNamespace("uid=daniel,ou=People,dc=officefloor,dc=net");

    // Record
    this.recordReturn(this.context, this.context.search("ou=People,dc=officefloor,dc=net",
            "(&(objectClass=inetOrgPerson)(uid=daniel))", null), searchResults);
    this.recordReturn(searchResults, searchResults.hasMore(), true);
    this.recordReturn(searchResults, searchResults.next(), searchResult);
    this.recordReturn(this.context, this.context.getAttributes("uid=daniel,ou=People,dc=officefloor,dc=net"),
            attributes);
    this.recordReturn(attributes, attributes.get("userPassword"), attribute);
    this.recordReturn(attribute, attribute.getAll(), userPasswords);
    this.recordReturn(userPasswords, userPasswords.hasMore(), true);
    this.recordReturn(userPasswords, userPasswords.next(), "Plain Text Password".getBytes(US_ASCII));
    this.recordReturn(userPasswords, userPasswords.hasMore(), true);
    this.recordReturn(userPasswords, userPasswords.next(), ("{MD5}" + encodedCredentials).getBytes(US_ASCII));

    // Test
    this.replayMockObjects();
    CredentialEntry entry = this.store.retrieveCredentialEntry("daniel", "REALM");
    byte[] actualCredentials = entry.retrieveCredentials();
    this.verifyMockObjects();

    // Validate correct value
    assertEquals("Incorrect credential byte length", expectedCredentials.length, actualCredentials.length);
    for (int i = 0; i < expectedCredentials.length; i++) {
        assertEquals("Incorrect credential byte " + i, expectedCredentials[i], actualCredentials[i]);
    }
}