embedded-ldap-junit

Library that provides a JUnit rule for setting up an in-memory LDAP server by using the glorious Unboundid LDAP SDK

License

License

Categories

Categories

JUnit Unit Testing
GroupId

GroupId

org.zapodot
ArtifactId

ArtifactId

embedded-ldap-junit
Last Version

Last Version

0.8.1
Release Date

Release Date

Type

Type

jar
Description

Description

embedded-ldap-junit
Library that provides a JUnit rule for setting up an in-memory LDAP server by using the glorious Unboundid LDAP SDK
Project URL

Project URL

https://github.com/zapodot/embedded-ldap-junit
Source Code Management

Source Code Management

https://github.com/zapodot/embedded-ldap-junit

Download embedded-ldap-junit

How to add to project

<!-- https://jarcasting.com/artifacts/org.zapodot/embedded-ldap-junit/ -->
<dependency>
    <groupId>org.zapodot</groupId>
    <artifactId>embedded-ldap-junit</artifactId>
    <version>0.8.1</version>
</dependency>
// https://jarcasting.com/artifacts/org.zapodot/embedded-ldap-junit/
implementation 'org.zapodot:embedded-ldap-junit:0.8.1'
// https://jarcasting.com/artifacts/org.zapodot/embedded-ldap-junit/
implementation ("org.zapodot:embedded-ldap-junit:0.8.1")
'org.zapodot:embedded-ldap-junit:jar:0.8.1'
<dependency org="org.zapodot" name="embedded-ldap-junit" rev="0.8.1">
  <artifact name="embedded-ldap-junit" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.zapodot', module='embedded-ldap-junit', version='0.8.1')
)
libraryDependencies += "org.zapodot" % "embedded-ldap-junit" % "0.8.1"
[org.zapodot/embedded-ldap-junit "0.8.1"]

Dependencies

compile (5)

Group / Artifact Type Version
com.unboundid : unboundid-ldapsdk jar 5.0.1
junit : junit jar 4.13
com.google.guava : guava jar 29.0-jre
net.bytebuddy : byte-buddy jar 1.10.10
org.slf4j : slf4j-api jar 1.7.30

test (3)

Group / Artifact Type Version
ch.qos.logback : logback-classic jar 1.2.3
org.bouncycastle : bcprov-jdk15on jar 1.65
org.bouncycastle : bcpkix-jdk15on jar 1.65

Project Modules

There are no modules declared in this project.

embedded-ldap-junit

Build Status Coverage Status Maven Central Libraries.io for GitHub GitHub Analytics

A JUnit Rule for running an embedded LDAP server in your JUnit test based on the wonderful UnboundID LDAP SDK. Inspired by the Embedded Database JUnit Rule.

Why?

  • you want to test your LDAP integration code without affecting your LDAP server
  • you are working with LDAP schema changes that you would like to test without changing the schema at the shared LDAP server
  • you are refactoring legacy code where LDAP calls are tightly coupled with your business logic and wants to start by testing the legacy code from the "outside" (as suggested by Michael Feathers)
    • for this exact reason most instances returned from the EmbeddedLdapRule is instrumented to suppress "close" calls so that your legacy code will not destroy the current Context/DirContext or LdapInterface
    • note: if you used the new unsharedLdapConnection() method, the returned instance will not have this guarantee as it returns a instance of UnboundID LDAPConnection which is declared 'final'.

Status

This library is distributed through the Sonatype OSS repo and should thus be widely available. Java 8 or higher is required. It has proven pretty useful for several users and should be considered safe for running tests for all kinds of LDAP integrating code.

Changelog

  • version 0.8.1: switched license to MIT (was Apache v2) changelog
  • version 0.8: changelog
  • version 0.7: changelog
  • version 0.6: changelog
  • version 0.5.2: changelog
  • version 0.5.1: changelog
  • version 0.5: changelog
  • version 0.4: changelog
  • version 0.3: solves issue #1 - give access to the LDAPConnection object from the UnboundID LDAP SDK
  • version 0.2:
    • support for defining schema definitions using the "withSchema(String...)" method on the EmbeddedLdapRuleBuilder
    • added the possibility to define multiple root DSNs
  • version 0.1: support for both UnboundID LDAP SDK and traditional JNDI LDAP integrations

Usage

Add dependency

Maven

<dependency>
    <groupId>org.zapodot</groupId>
    <artifactId>embedded-ldap-junit</artifactId>
    <version>0.8.1</version>
</dependency>

SBT

libraryDependencies += "org.zapodot" % "embedded-ldap-junit" % "0.8.1"

Add to Junit test

import com.unboundid.ldap.sdk.LDAPInterface;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
...

@Rule
public EmbeddedLdapRule embeddedLdapRule = EmbeddedLdapRuleBuilder
        .newInstance()
        .usingDomainDsn("dc=example,dc=com")
        .importingLdifs("example.ldif")
        .build();

@Test
public void testLdapInteface() throws Exception {
    // Test using the UnboundID LDAP SDK directly
    final LdapInterface ldapConnection = embeddedLdapRule.ldapConnection();
    final SearchResult searchResult = ldapConnection.search(DOMAIN_DSN, SearchScope.SUB, "(objectClass=person)");
    assertEquals(1, searchResult.getEntryCount());
}

@Test
public void testUnsharedLdapConnection() throws Exception {
    // Test using the UnboundID LDAP SDK directly by using the UnboundID LDAPConnection type
    final LDAPConnection ldapConnection = embeddedLdapRule.unsharedLdapConnection();
    final SearchResult searchResult = ldapConnection.search(DOMAIN_DSN, SearchScope.SUB, "(objectClass=person)");
    assertEquals(1, searchResult.getEntryCount());
}

@Test
public void testDirContext() throws Exception {

    // Test using the good ol' JDNI-LDAP integration
    final DirContext dirContext = embeddedLdapRule.dirContext();
    final SearchControls searchControls = new SearchControls();
    searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    final NamingEnumeration<javax.naming.directory.SearchResult> resultNamingEnumeration =
            dirContext.search(DOMAIN_DSN, "(objectClass=person)", searchControls);
    assertEquals(1, Iterators.size(Iterators.forEnumeration(resultNamingEnumeration)));
}
@Test
public void testContext() throws Exception {

    // Another test using the good ol' JDNI-LDAP integration, this time with the Context interface
    final Context context = embeddedLdapRule.context();
    final Object user = context.lookup("cn=John Doe,ou=people,dc=example,dc=com");
    assertNotNull(user);
}

Versions

Version
0.8.1
0.8
0.7
0.6
0.5.2
0.5.1
0.5
0.4
0.3
0.2
0.1