0

Attempting to fire-up an AD-like server for verifying ldap queries for 'user' entities.

The test:

import org.zapodot.junit.ldap.EmbeddedLdapRule
import org.zapodot.junit.ldap.EmbeddedLdapRuleBuilder

class FooSpec extends Specification {

    @Rule
    public EmbeddedLdapRule embeddedLdapRule = EmbeddedLdapRuleBuilder
            .newInstance()
            .withSchema('schema.ldif')
            .importingLdifs('import.ldif')
            //...
}

schema.ldif:

dn: cn=user,cn=schema,cn=configuration,dc=example,dc=com
changetype: add
objectclass: classSchema
governsId: 1.2.840.113556.1.5.9
objectClassCategory: 1
subClassOf: organizationalPerson
lDAPDisplayName: user
description: a user

import.ldif:

dn: CN=alice,DC=example,dc=com
objectClass: inetOrgPerson
objectClass: organizationalPerson
objectClass: person
objectClass: user
cn: alice
sn: alice

Getting:

Unable to add entry 'CN=alice,DC=example,dc=com' because it violates the provided schema: The entry contains object class user which is not defined in the schema.

What am I doing wrong?

Thanks!

pointyhat
  • 568
  • 5
  • 16

1 Answers1

0

This does not seem to be a Spock-specific question, I guess you are having an LDAP syntax problem here. I have no idea about LDAP, I just got curious and used this LDIF file provided by the tool used by you (embedded-ldap-junit) in order to show you how it works beautifully with Spock if you just use correct LDAP definitions in your resource file:

package de.scrum_master.stackoverflow

import com.unboundid.ldap.sdk.SearchScope
import org.junit.Rule
import org.zapodot.junit.ldap.EmbeddedLdapRule
import org.zapodot.junit.ldap.EmbeddedLdapRuleBuilder
import spock.lang.Specification
import spock.lang.Unroll

class EmbeddedLDAPServerTest extends Specification {
  static final String BASE_DN = "dc=zapodot,dc=org"

  @Rule
  public EmbeddedLdapRule embeddedLdapRule = EmbeddedLdapRuleBuilder
    .newInstance()
    .usingDomainDsn(BASE_DN)
    .importingLdifs('example.ldif')
    .build()

  @Unroll
  def "Search for #searchFor"() {
    given:
    def connection = embeddedLdapRule.ldapConnection()
    when:
    def searchResult = connection.search(BASE_DN, SearchScope.SUB, "($searchFor)")
    then:
    searchResult.entryCount == entryCount

    where:
    searchFor                        | entryCount
    "objectClass=person"             | 1
    "cn=Sondre Eikanger Kvalo"       | 1
    "ou=people"                      | 1
    "ou=groups"                      | 1
    "objectclass=organizationalUnit" | 2
    "objectclass=top"                | 4
  }
}
kriegaex
  • 63,017
  • 15
  • 111
  • 202
  • thanks, @kriegaex, for putting an example together. I am not after making the org.zapodot sample work, though. I specifically want to populate embeddedLdapRule so that it will return entities when queried for `(objectClass=user)` – pointyhat May 01 '18 at 06:23
  • I know you were not after this example. I just wanted to help you debug insofar as now it is clear that Spock is not the problem but your LDIF data are. Well, or you hit a bug in the LDAP library, I cannot say for sure, not knowing squat about the syntax rules or language semantics. My guess would be that your data are somehow incomplete, missing some kind of definition. – kriegaex May 01 '18 at 08:22