<commit_before>package gldapo

import java.io.File

import javax.naming.Context
import javax.naming.InitialContext
import javax.naming.NamingException
import javax.naming.directory.Attribute
import javax.naming.directory.Attributes
import javax.naming.directory.BasicAttribute
import javax.naming.directory.BasicAttributes
import javax.naming.directory.DirContext

import com.novell.ldap.LDAPConnection
import com.novell.ldap.util.LDIFWriter

import org.apache.directory.server.core.configuration.MutablePartitionConfiguration
import org.apache.directory.server.unit.AbstractServerTest

abstract public class AbstractGldapoIntegrationTest extends AbstractServerTest
{
    def gldapo
    def partitionName = "gldapo"
    def partitionSuffix = "o=$partitionName"
    def jldapConnection
    
     public void setUp() {
         def pcfg = new MutablePartitionConfiguration()
         pcfg.name = partitionName
         pcfg.suffix = partitionSuffix
         pcfg.indexedAttributes = ["objectClass", "o"] as Set

         def attrs = new BasicAttributes(true)
         def attr = new BasicAttribute("objectClass")
         attr.add("top")
         attr.add("organization")
         attrs.put(attr)

         attr = new BasicAttribute("o")
         attr.add(partitionName)
         attrs.put(attr)
         pcfg.contextEntry = attrs
         
         def pcfgs = new HashSet<MutablePartitionConfiguration>()
         pcfgs.add(pcfg)

         configuration.contextPartitionConfigurations = pcfgs
         configuration.workingDirectory = new File("target/ldap-server")

         super.setUp()
         
         initGldapo()
         initJldapConnection()
    }

    def initGldapo() {
        gldapo = new Gldapo(
            directories: [
                local: [
                    url: "ldap://localhost:${configuration.ldapPort}",
                    base: partitionSuffix,
                    userDn: "uid=admin,ou=system",
                    password: "<PASSWORD>"
                ]
            ],
            schemas: getSchemaClasses(),
            typeMappings: getTypeMappings()
        )
    }
    
    def getSchemaClasses() {
        []
    }
    
    def getTypeMappings() {
        []
    }
    
    void importLdif(ldif) {
        importLdif(this.class.getResourceAsStream(ldif));
    }
    
    void initJldapConnection() {
        jldapConnection = new LDAPConnection()
        jldapConnection.connect("localhost", configuration.ldapPort)
        jldapConnection.bind(3, "uid=admin,ou=system", "secret" as byte[])
    }
    
    
    def getEntry(dn) {
        jldapConnection.read("$dn,$partitionSuffix")
    }<commit_msg>[GLDP-48] - getEntry() - If the entry doesn't exist, an LDAPException with code 32 will be thrown. To return null for no entry, we catch this particular exception and return null.<commit_after>package gldapo

import java.io.File

import javax.naming.Context
import javax.naming.InitialContext
import javax.naming.NamingException
import javax.naming.directory.Attribute
import javax.naming.directory.Attributes
import javax.naming.directory.BasicAttribute
import javax.naming.directory.BasicAttributes
import javax.naming.directory.DirContext

import com.novell.ldap.LDAPConnection
import com.novell.ldap.util.LDIFWriter
import com.novell.ldap.LDAPException

import org.apache.directory.server.core.configuration.MutablePartitionConfiguration
import org.apache.directory.server.unit.AbstractServerTest

abstract public class AbstractGldapoIntegrationTest extends AbstractServerTest
{
    def gldapo
    def partitionName = "gldapo"
    def partitionSuffix = "o=$partitionName"
    def jldapConnection
    
     public void setUp() {
         def pcfg = new MutablePartitionConfiguration()
         pcfg.name = partitionName
         pcfg.suffix = partitionSuffix
         pcfg.indexedAttributes = ["objectClass", "o"] as Set

         def attrs = new BasicAttributes(true)
         def attr = new BasicAttribute("objectClass")
         attr.add("top")
         attr.add("organization")
         attrs.put(attr)

         attr = new BasicAttribute("o")
         attr.add(partitionName)
         attrs.put(attr)
         pcfg.contextEntry = attrs
         
         def pcfgs = new HashSet<MutablePartitionConfiguration>()
         pcfgs.add(pcfg)

         configuration.contextPartitionConfigurations = pcfgs
         configuration.workingDirectory = new File("target/ldap-server")

         super.setUp()
         
         initGldapo()
         initJldapConnection()
    }

    def initGldapo() {
        gldapo = new Gldapo(
            directories: [
                local: [
                    url: "ldap://localhost:${configuration.ldapPort}",
                    base: partitionSuffix,
                    userDn: "uid=admin,ou=system",
                    password: "<PASSWORD>"
                ]
            ],
            schemas: getSchemaClasses(),
            typeMappings: getTypeMappings()
        )
    }
    
    def getSchemaClasses() {
        []
    }
    
    def getTypeMappings() {
        []
    }
    
    void importLdif(ldif) {
        importLdif(this.class.getResourceAsStream(ldif));
    }
    
    void initJldapConnection() {
        jldapConnection = new LDAPConnection()
        jldapConnection.connect("localhost", configuration.ldapPort)
        jldapConnection.bind(3, "uid=admin,ou=system", "secret" as byte[])
    }
    
    def getEntry(dn) {
        try {
            return jldapConnection.read("$dn,$partitionSuffix")
        } catch (LDAPException e) {
            if (e.resultCode == LDAPException.NO_SUCH_OBJECT) 
                return null
            else
                throw e
        }
    }
{'fraction_non_alphanumeric': 0.08988566402814424, 'fraction_numerical': 0.0010554089709762533, 'mean_word_length': 2.330990041007616, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '415257', 'n_tokens_mistral': 1612, 'n_tokens_neox': 1556, 'n_words': 307}