I am trying to install the built-in ldap for unit test using Spring Ldap. But I need to use a custom schema for custom objectClasses / attributes definitions. How to configure it using Spring Ldap test ( LdapTestUtils ?)
Actually, if I run the test, it does not mean that my own objectClass "myOb" is not defined in the circuit with the following message:
org.springframework.ldap.UncategorizedLdapException: Failed to populate LDIF; nested exception is javax.naming.directory.NoSuchAttributeException: [LDAP: error code 16 - NO_SUCH_ATTRIBUTE: failed for Add Request : ... : OID for name 'myOb' was not found within the OID registry]; remaining name 'cn=123456, ou=MyUser, o=company.com'
If I comment on objectClass: myOb from ldif, the test fails with a null value (the attribute is not readable).
Here is my test class:
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = LdapConfiguration.class, loader = AnnotationConfigContextLoader.class) public class LdapTest { // Ldap port private static final int LDAP_PORT = 18880; // Base DN for test data private static final LdapName baseName = LdapUtils.newLdapName("o=company.com"); @Autowired LdapTemplate ldapTemplate; @BeforeClass public static void setupBeforeClass() { LdapTestUtils.startEmbeddedServer(LDAP_PORT, baseName.toString(), "ldaptest"); // How to load schema definition ? } @AfterClass public static void teardownAfterClass() throws Exception { LdapTestUtils.shutdownEmbeddedServer(); } @Before public void setup() throws Exception { LdapTestUtils.cleanAndSetup(ldapTemplate.getContextSource(), baseName, new ClassPathResource("ldap/test-users.ldif")); } @Test public void testSearchLdap() throws Exception { String myObId = ldapTemplate.lookup(LdapNameBuilder.newInstance("ou=MyUser, o=company.com").add("cn", "123456").build(), new AbstractContextMapper<String>() { @Override protected String doMapFromContext(DirContextOperations ctx) { return ctx.getStringAttribute("myObId"); // custom type } }); Assert.assertNotNull(myObId); // myObId is null if I comment `objectClass: myOb` ! } }
and my ldif:
dn: ou=MyUser, o=company.com ou: User description: MyUser objectClass: top objectClass: organizationalunit dn: cn=123456, ou=MyUser, o=company.com objectClass: top objectClass: person objectClass: myOb cn: 123456 sn: 823456 myObId: TEST
source share