Spring Boot + Spring LDAP Integration Testing Example
In this example we demonstrate how to use spring boot and spring ldap for writing ldap integration tests. We set up an embedded LDAP server and populate it with a custom schema.ldif file.
Project Structure
You can see we have separate configuration files for our test environments, located in the src/test/resources
folder.
Maven Dependencies
We use Apache Maven to manage our project dependencies. Add the following dependencies to your project.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.memorynotfound.ldap</groupId>
<artifactId>unit-test-ldap</artifactId>
<version>1.0.0-SNAPSHOT</version>
<url>https://memorynotfound.com</url>
<name>Spring LDAP - ${project.artifactId}</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.7.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-ldap</artifactId>
</dependency>
<dependency>
<groupId>com.unboundid</groupId>
<artifactId>unboundid-ldapsdk</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Configure Embedded LDAP Server using application-test.yml
For testing purposes, we create an embedded LDAP server and populate it test data, located in the test-schema.ldif
file. The embedded LDAP server is created on startup, before our integration tests are executed.
#Spring Boot + Spring LDAP Integration Testing Example
spring:
ldap:
embedded:
base-dn: dc=memorynotfound,dc=com
credential:
username: uid=admin
password: secret
ldif: classpath:test-schema.ldif
port: 12345
validation:
enabled: false
Populate LDAP Server
The LDAP servers gets populated using the following test-schema.ldif
file.
dn: dc=memorynotfound,dc=com
objectclass: top
objectclass: domain
objectclass: extensibleObject
dc: memorynotfound
# Organizational Units
dn: ou=groups,dc=memorynotfound,dc=com
objectclass: top
objectclass: organizationalUnit
ou: groups
dn: ou=people,dc=memorynotfound,dc=com
objectclass: top
objectclass: organizationalUnit
ou: people
# Create People
dn: uid=john,ou=people,dc=memorynotfound,dc=com
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: John Doe
sn: John
uid: john
password: secret
dn: uid=jihn,ou=people,dc=memorynotfound,dc=com
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: Jihn Die
sn: Jihn
uid: jihn
password: secret
dn: uid=jahn,ou=people,dc=memorynotfound,dc=com
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: Jahn Dae
sn: Jahn
uid: jahn
password: secret
# Create Groups
dn: cn=developers,ou=groups,dc=memorynotfound,dc=com
objectclass: top
objectclass: groupOfUniqueNames
cn: developers
ou: developer
uniqueMember: uid=john,ou=people,dc=memorynotfound,dc=com
uniqueMember: uid=jihn,ou=people,dc=memorynotfound,dc=com
dn: cn=managers,ou=groups,dc=memorynotfound,dc=com
objectclass: top
objectclass: groupOfUniqueNames
cn: managers
ou: manager
uniqueMember: uid=jahn,ou=people,dc=memorynotfound,dc=com
Person Object
We are using this Person
object to map our LDAP entries to.
package com.memorynotfound.ldap;
public class Person {
private String fullName;
private String lastName;
public Person() {
}
public Person(String fullName, String lastName) {
this.fullName = fullName;
this.lastName = lastName;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return "Person{" +
"fullName='" + fullName + '\'' +
", lastName='" + lastName + '\'' +
'}';
}
}
Using LdapTemplate to query the LDAP
For simplicity, we created some basic LDAP queries, which query the LDAP server.
package com.memorynotfound.ldap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ldap.core.AttributesMapper;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.stereotype.Service;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import java.util.List;
import static org.springframework.ldap.query.LdapQueryBuilder.query;
@Service
public class PersonRepository {
@Autowired
private LdapTemplate ldapTemplate;
public List<String> getAllPersonNames() {
return ldapTemplate.search(
query().where("objectclass").is("person"),
new AttributesMapper<String>() {
public String mapFromAttributes(Attributes attrs)
throws NamingException {
return (String) attrs.get("cn").get();
}
});
}
public List<Person> getAllPersons() {
return ldapTemplate.search(query()
.where("objectclass").is("person"), new PersonAttributesMapper());
}
public Person findPerson(String dn) {
return ldapTemplate.lookup(dn, new PersonAttributesMapper());
}
private class PersonAttributesMapper implements AttributesMapper<Person> {
public Person mapFromAttributes(Attributes attrs) throws NamingException {
Person person = new Person();
person.setFullName((String)attrs.get("cn").get());
person.setLastName((String)attrs.get("sn").get());
return person;
}
}
}
Spring Boot + Spring LDAP Integration Testing Example
By annotating the integration test with @SpringBootTest
, this will automatically search for classes annotated with @SpringBootConfiguration
and initialize the application using the appropriate configurations. We set the active profile to test using the @ActiveProfiles
annotation. This’ll load all appropriate test-configuration files. When the application executes the first test, the application context is already initialized and the embedded LDAP server is generated and populated.
package com.memorynotfound.ldap;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@SpringBootTest
@ActiveProfiles("test")
@RunWith(SpringRunner.class)
public class SpringLdapIntegrationTest {
@Autowired
private PersonRepository personRepository;
@Test
public void testGetAllPersons() {
List<Person> persons = personRepository.getAllPersons();
assertNotNull(persons);
assertEquals(persons.size(), 3);
}
@Test
public void testGetAllPersonsNames() {
List<String> persons = personRepository.getAllPersonNames();
assertNotNull(persons);
assertEquals(persons.size(), 3);
}
@Test
public void testFindPerson() {
Person person = personRepository.findPerson("uid=john,ou=people,dc=memorynotfound,dc=com");
assertNotNull(person);
assertEquals(person.getFullName(), "John Doe");
}
}
References
- Spring LDAP – Testing Documentation
- Spring LDAP – API JavaDoc
- LdapTemplate JavaDoc
- @SpringBootTest JavaDoc
- SpringRunner JavaDoc
- Spring Boot Application Properties
- UnboundId Official Website
- UnboundId API JavaDoc
First off thanks for the article it was a great help. I ran into one issue though .. In the LDIF file the user’s password should be specified with ‘userpassword’ and not ‘password’ as above.
My example using gradle based on the one presented above -> https://github.com/DameerGamlet/spring-ldap-example