Using Groovy to connect to Active Directory

I have made a previous post about connecting to an AD using Java here. In another setup I want to connect to an AD using Groovy as we can use this in SoapUI. Lets say you have made an AccountManagingService which creates account in an AD for you. Now you want a way to test this service using SoapUI.

The first thing you do is to make a testcase in SoapUI in which you call your AddAccount method for example. Once you have added your assertions of the response you want to make sure your service isn’t lying to you when it says ‘added successfully’ so you want to the check the AD by yourself. You can easily do this by making use of the GroovyLDAP library. For documentation and download of the Jar file see here

Download the Groovy-LDAP jar and put it into your eviwaresoapUI-3.6.1lib folder. If SoapUI is open, close it and start it again to have it pick the jar. Now for the programming.

Add a Groovy step to your TestCase. The only thing you have to do is to make an new LDAP instance. This can look like this:


LDAP connection = LDAP.newInstance('ldap://MY_SERVER:389','cn=MY_ACCOUNT, ou=MY_OU, dc=MY_DC', 'MY_PASSWORD')

You can also login anonymous if your server allows it by only entering a server with a port. You can also connect using https but be aware that you have to set the keystore you want to use in SoapUI->Preferences and import the certificate into the keystore or else you get the dreaded PKIX error. Also some AD’s require a https connection if you want to do things other then reading. I don’t know if this is configuration or default.

Next you can check if a certain user exists. You can do this by making the next call:

assert (connection.exists('cn=John Doe, ou=MY_OU, dc=MY_DC')):" Person John Doe does not exists"

You can also search, add, modify and delete. For a good overview see here.

From this you can start extending your tests script. Have fun with it.

Leave a Reply

Your email address will not be published. Required fields are marked *