/** * Returns the DN for the context representing this LDAP record. By default this is * using {@link javax.naming.Context#getNameInNamespace()} instead of * {@link org.springframework.ldap.core.DirContextAdapter#getDn()} since the latter * returns a partial DN if a base has been specified. * @param adapter - the Context to extract the DN from * @return - the String representing the full DN */ private String getAdapterDN(DirContextAdapter adapter) { // returns the full DN rather than the sub DN if a base is specified return adapter.getNameInNamespace(); }
/** * Returns the DN for the context representing this LDAP record. By default this is * using {@link javax.naming.Context#getNameInNamespace()} instead of * {@link org.springframework.ldap.core.DirContextAdapter#getDn()} since the latter * returns a partial DN if a base has been specified. * @param adapter - the Context to extract the DN from * @return - the String representing the full DN */ private String getAdapterDN(DirContextAdapter adapter) { // returns the full DN rather than the sub DN if a base is specified return adapter.getNameInNamespace(); }
/** * This method depends on a DirObjectFactory ( * {@link org.springframework.ldap.core.support.DefaultDirObjectFactory}) * being set in the ContextSource. */ @Test public void testLookupContextRoot() { DirContextAdapter result = (DirContextAdapter) tested.lookup(""); assertThat(result.getDn().toString()).isEqualTo(""); assertThat(result.getNameInNamespace()).isEqualTo(base); }
@Test @Category(NoAdTest.class) public void testLookup_GetNameInNamespace_MultiRdn() { DirContextAdapter result = (DirContextAdapter) tested.lookup("cn=Some Person+sn=Person,ou=company1,ou=Norway"); assertThat(result.getDn().toString()).isEqualTo("cn=Some Person+sn=Person,ou=company1,ou=Norway"); assertThat(result.getNameInNamespace()).isEqualTo("cn=Some Person+sn=Person,ou=company1,ou=Norway," + base); } }
@Test public void testGetNameInNamespace() { DirContextAdapter tested = new DirContextAdapter(DUMMY_NAME); String result = tested.getNameInNamespace(); assertThat(result).isEqualTo(DUMMY_NAME.toString()); }
@Test public void testLookup_GetNameInNamespace_Plain() { String expectedDn = "cn=Some Person2, ou=company1,ou=Sweden"; DirContextAdapter result = (DirContextAdapter) tested.lookup(expectedDn); LdapName expectedName = LdapUtils.newLdapName(expectedDn); assertThat(result.getDn()).isEqualTo(expectedName); assertThat(result.getNameInNamespace()).isEqualTo("cn=Some Person2,ou=company1,ou=Sweden," + base); } }
/** * This method depends on a DirObjectFactory ({@link org.springframework.ldap.core.support.DefaultDirObjectFactory}) * being set in the ContextSource. */ @Test public void testLookup_Plain() { String expectedDn = "cn=Some Person2, ou=company1, ou=Sweden," + base; DirContextAdapter result = (DirContextAdapter) tested.lookup(expectedDn); assertThat(result.getStringAttribute("cn")).isEqualTo("Some Person2"); assertThat(result.getStringAttribute("sn")).isEqualTo("Person2"); assertThat(result.getStringAttribute("description")).isEqualTo("Sweden, Company1, Some Person2"); LdapName expectedName = LdapUtils.newLdapName(expectedDn); assertThat(result.getDn()).isEqualTo(expectedName); assertThat(result.getNameInNamespace()).isEqualTo(expectedDn); }
@Test public void testGetNameInNamespace_BasePath() { DirContextAdapter tested = new DirContextAdapter(null, LdapUtils.newLdapName("c=SE"), BASE_NAME); String result = tested.getNameInNamespace(); assertThat(result).isEqualTo(DUMMY_NAME.toString()); }
/** * Make sure that the base suffix is stripped off from the DN. * * @throws Exception */ @Test public void testGetObjectInstance_BaseSet() throws Exception { Attributes expectedAttributes = new NameAwareAttributes(); expectedAttributes.put("someAttribute", "someValue"); when(contextMock2.getNameInNamespace()).thenReturn("dc=jayway, dc=se"); DirContextAdapter adapter = (DirContextAdapter) tested.getObjectInstance(contextMock, LdapUtils.newLdapName( "ou=some unit"), contextMock2, new Hashtable(), expectedAttributes); verify(contextMock).close(); assertThat(adapter.getDn().toString()).isEqualTo("ou=some unit"); assertThat(adapter.getNameInNamespace()).isEqualTo("ou=some unit,dc=jayway,dc=se"); assertThat(adapter.getAttributes()).isEqualTo(expectedAttributes); }
@Override public Object mapFromContext(Object obj) { DirContextAdapter ctx = (DirContextAdapter) obj; ExperimenterGroup group = new ExperimenterGroup(); group.setName(get("name", ctx)); group.setDescription(get("description", ctx)); group.setLdap(true); group.putAt(LDAP_DN, ctx.getNameInNamespace()); if (attribute != null) { group.putAt(LDAP_ATTR, ctx.getAttributeSortedStringSet(attribute)); } group.putAt(LDAP_PROPS, new AttributeSet(ctx)); return group; }
@Override public Object mapFromContext(Object obj) { DirContextAdapter ctx = (DirContextAdapter) obj; Experimenter person = new Experimenter(); person.setOmeName(get("omeName", ctx)); person.setFirstName(get("firstName", ctx)); person.setMiddleName(get("middleName", ctx)); person.setLastName(get("lastName", ctx)); person.setInstitution(get("institution", ctx)); person.setEmail(get("email", ctx)); person.setLdap(true); person.putAt(LDAP_DN, ctx.getNameInNamespace()); if (attribute != null) { person.putAt(LDAP_ATTR, ctx.getAttributeSortedStringSet(attribute)); } person.putAt(LDAP_PROPS, new AttributeSet(ctx)); return person; }