DirectoryTester Example
DirectoryTester is used to connect to an LDAP directory server (embedded or external) and make assertions about or verify the contents of the LDAP directory.
- JUnit 4
import com.buralotech.oss.ldapunit.DirectoryTester; import org.junit.After; import org.junit.Before; import org.junit.Test; public class Test { private DirectoryTester tester; @Before public void setUp() { tester = new DirectoryTester("localhost", 10389, "uid=admin,ou=system", "secret"); } @After public void tearDown() { tester.disconnect();) } @Test public void testSomething() { // Do something that affects the LDAP directory // Check outcomes with tester.assertXXX() and tester.verifyXXX() methods } } - JUnit 5
import com.buralotech.oss.ldapunit.DirectoryTester; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; public class Test { private DirectoryTester tester; @Before public void setUp() { tester = new DirectoryTester("localhost", 10389, "uid=admin,ou=system", "secret"); } @AfterEach public void tearDown() { tester.disconnect();) } @Test public void testSomething() { // Do something that affects the LDAP directory // Check outcomes with tester.assertXXX() and tester.verifyXXX() methods } }There are two variants of the DirectoryTester constructor:
- DirectoryTester(String hostname, int port) - connects anonymously to the directory server on the host with name or IP address specified by hostname that is listening on the TCP port specified by port.
- DirectoryTester(String hostname, int port, String bindDN, String password) - connects to the directory server on the host with name or IP address specified by hostname that is listening on the TCP port specified by port. Then it binds to the using the authentication identifier and credentials specified by bindDN and password.
The following methods can be used to make assertions about or verify the contents of the LDAP directory:
- assertDNExists(String dn) - asserts that an entry exists in the LDAP directory with the distinguished name of dn.
- verifyDNExists(String dn) - tests to see if an entry exists in the LDAP directory with the distinguished name of dn.
- assertDNIsA(String dn, String objectclass) - asserts that an entry exists in the LDAP directory with the distinguished name of dn and that it is of type objectclass.
- verifyDNIsA(String dn, String objectclass) - tests to see if an entry exists in the LDAP directory with the distinguished name of dn and check that it is of type objectclass.
- assertDNHasAttribute(String dn, String attributeName) - asserts that an entry exists in the LDAP directory with the distinguished name of dn and that it has an attribute named attributeName.
- verifyDNHasAttribute(String dn, String attributeName) - tests to see if an entry exists in the LDAP directory with the distinguished name of dn and check that it has an attribute named attributeName.
- assertDNHasAttributeValue(String dn, String attributeName, String... attributeValue) - assert that an entry exists in the LDAP directory with the distinguished name of dn and that it has an attribute named attributeName with value(s) attributeValues.
- verifyDNHasAttributeValue(String dn, String attributeName, String... attributeValue) - tests to see if an entry exists in the LDAP directory with the distinguished name of dn and check that it has an attribute named attributeName with value(s) attributeValues.
The connection should be closed by calling disconnect().
DirectoryTester implements AutoCloseable so it can be used with try-with-resources to avoid the need to explicitly call disconnect() or close().
- JUnit 4
import com.buralotech.oss.ldapunit.DirectoryTester; import org.junit.Test; public class Test { @Test public void testSomething() { try (final DirectoryTester tester = new DirectoryTester("localhost", 10389, "uid=admin,ou=system", "secret")) { // Do something that affects the LDAP directory // Check outcomes with tester.assertXXX() and tester.verifyXXX() methods } } } - JUnit 5
import com.buralotech.oss.ldapunit.DirectoryTester; import org.junit.jupiter.api.Test; public class Test { @Test public void testSomething() { try (final DirectoryTester tester = new DirectoryTester("localhost", 10389, "uid=admin,ou=system", "secret")) { // Do something that affects the LDAP directory // Check outcomes with tester.assertXXX() and tester.verifyXXX() methods } } }
Maven