I am trying to setup embedded ldap for unit test with Spring Ldap. But I need to use a custom schema for custom objectClasses/attributes definitions. How can I configure it with Spring Ldap test (LdapTestUtils?)
Actually if I run test, it fail saying that my custom objectClass "myOb" is not defined in the schema 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 objectClass: myOb
from ldif, the test fail with a null value (attribute is not read).
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
InMemoryDirectoryServer
server solution suggested below – Gaseous