Giter Site home page Giter Site logo

xabiamu / ldapunit Goto Github PK

View Code? Open in Web Editor NEW

This project forked from bmatthews68/ldapunit

0.0 2.0 0.0 609 KB

Simplifies the task of creating unit tests that depend on an LDAP directory server.

Home Page: http://ldapunit.btmatthews.com

Java 100.00%

ldapunit's Introduction

LDAPUnit

LDAPUnit is a Java library help create unit tests for that depend on a LDAP directory server.

DirectoryServerRule

DirectoryServerRule is used launch an embedded LDAP directory server so that the unit tests do not have to be dependent on an external LDAP directory server with unpredictable state.

To use the DirectoryServerRule you need to instantiate the rule as a public member of the test class and annotate it with Rule. The rule will have no effect unless the class or methods have been annotated with DirectoryServerConfiguration.

import com.btmatthews.ldapunit.DirectoryServerConfiguration;
import com.btmatthews.ldapunit.DirectoryServerRule;
import org.junit.Rule;
import org.junit.Test;

@DirectoryServerConfiguration
public class Test {

    @Rule
    public DirectoryServerRule directoryServerRule = new DirectoryServerRule();

    @Test
    public void testSomething() {
        // Unit test code that depends on LDAP directory server
    }
}

The DirectoryServerConfiguration annotation is used to provide the configuration parameters for the embedded LDAP directory server. The following parameters can be configured:

Name Description Default
port The TCP port that the LDAP directory server will be configured to listen on. 10389
baseDN The DN that will be configured as the root of the LDAP directory. dc=btmatthews,dc=com
authDN The DN that will be configured as the administrator account identifier. uid=admin,ou=system
password The password that will be configured as the authentication credentials for the administrator account. secret
ldifFile The location of an optional LDIF file that can be used to seed the LDAP directory with an initial data set. The file may be located on the file system or the classpath. The classpath is checked first and then falls back to the file system if it was not found on the class path.
ldifFiles The location of optional LDIF files that can be used to seed the LDAP directory with an initial data set. The files may be located on the file system or the classpath. The classpath is checked first and then falls back to the file system if it was not found on the class path.

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 nane 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.

DirectoryTester

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.

import com.btmatthews.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
}
}

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 nane 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().

Maven Central Coordinates

LDAPUnit has been published in Maven Central at the following coordinates:

<dependency>
    <groupId>com.btmatthews.ldapunit</groupId>
    <artifactId>ldapunit</artifactId>
    <version>1.1.3</version>
</dependency>

Credits

The approach for implementing the LDAPUnit's DirectoryServerRule is based heavily on the OpenDJRule implemented in the https://github.com/ehsavoie/embedded-ldap project.

Internally LDAPUnit is using the UnboundID LDAP SDK to run the embedded LDAP directory server and as the API for communicating with an LDAP directory server.

License & Source Code

The LDAPUnit is made available under the Apache License and the source code is hosted on GitHub at https://github.com/bmatthews68/ldapunit.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.