Commit 979f0aa4 authored by Brian Major's avatar Brian Major
Browse files

ac2 - Unit test for LdapConnections object

parent b387ff73
Loading
Loading
Loading
Loading
+15 −3
Original line number Diff line number Diff line
@@ -93,7 +93,7 @@ import com.unboundid.ldap.sdk.SimpleBindRequest;
 */
public class LdapConnectionPool
{
    private static final Logger logger = Logger.getLogger(LdapUserPersistence.class);
    private static final Logger logger = Logger.getLogger(LdapConnectionPool.class);

    private static final int POOL_CHECK_INTERVAL_MILLESCONDS = 10000; // 10 seconds

@@ -112,6 +112,13 @@ public class LdapConnectionPool
        profiler.checkpoint("Create pool");
    }

    LdapConnectionPool(LdapConfig config)
    {
        this.currentConfig = config;
        pool = createPool(currentConfig);
        profiler.checkpoint("Create pool");
    }

    protected LDAPConnection getReadOnlyConnection() throws LDAPException
    {
        synchronized (poolMonitor)
@@ -150,6 +157,11 @@ public class LdapConnectionPool
        }
    }

    protected LdapConfig getCurrentConfig()
    {
        return currentConfig;
    }

    protected void shutdown()
    {
        logger.debug("Shutting down pool");
@@ -190,7 +202,7 @@ public class LdapConnectionPool
        return System.currentTimeMillis() - lastPoolCheck > POOL_CHECK_INTERVAL_MILLESCONDS;
    }

    static LDAPReadWriteConnectionPool createPool(LdapConfig config)
    LDAPReadWriteConnectionPool createPool(LdapConfig config)
    {
        LDAPConnectionPool ro = createPool(config.getReadOnlyPool(), config);
        LDAPConnectionPool rw = createPool(config.getReadOnlyPool(), config);
@@ -198,7 +210,7 @@ public class LdapConnectionPool
        return pool;
    }

    private static LDAPConnectionPool createPool(LdapPool pool, LdapConfig config)
    private LDAPConnectionPool createPool(LdapPool pool, LdapConfig config)
    {
        try
        {
+14 −24
Original line number Diff line number Diff line
@@ -75,7 +75,6 @@ import ca.nrc.cadc.profiler.Profiler;

import com.unboundid.ldap.sdk.LDAPConnection;
import com.unboundid.ldap.sdk.LDAPException;
import com.unboundid.ldap.sdk.LDAPReadWriteConnectionPool;

/**
 * This class in the means by which the DAO classes obtain
@@ -99,20 +98,23 @@ class LdapConnections
    private LDAPConnection autoConfigReadOnlyConn;
    private LDAPConnection autoConfigReadWriteConn;

    private LdapConfig config;
    private LdapConnectionPool pool;

    private LDAPReadWriteConnectionPool manualConfigPool;
    private LDAPConnection manualConfigReadOnlyConn;
    private LDAPConnection manualConfigReadWriteConn;

    LdapConnections(LdapPersistence persistence)
    {
        if (persistence == null)
            throw new RuntimeException("persistence object is required");
        this.persistence = persistence;
    }

    LdapConnections(LdapConfig config)
    LdapConnections(LdapConnectionPool pool)
    {
        this.config = config;
        if (pool == null)
            throw new RuntimeException("pool object is required");
        this.pool = pool;
    }

    LDAPConnection getReadOnlyConnection() throws LDAPException
@@ -130,15 +132,9 @@ class LdapConnections
        else
        {
            log.debug("Obtaining manual config read only connection.");
            if (manualConfigPool == null)
            {
                log.debug("Creating manual config connection pool--should only see this " +
                        "message when running unit tests.");
                manualConfigPool = LdapConnectionPool.createPool(config);
            }
            if (manualConfigReadOnlyConn == null)
            {
                manualConfigReadOnlyConn = manualConfigPool.getReadConnection();
                manualConfigReadOnlyConn = pool.getReadOnlyConnection();
            }
            return manualConfigReadOnlyConn;
        }
@@ -159,15 +155,9 @@ class LdapConnections
        else
        {
            log.debug("Obtaining manual config read write connection.");
            if (manualConfigPool == null)
            {
                log.debug("Creating manual config connection pool--should only see this " +
                        "message when running unit tests.");
                manualConfigPool = LdapConnectionPool.createPool(config);
            }
            if (manualConfigReadWriteConn == null)
            {
                manualConfigReadWriteConn = manualConfigPool.getReadConnection();
                manualConfigReadWriteConn = pool.getReadWriteConnection();
            }
            return manualConfigReadWriteConn;
        }
@@ -194,11 +184,11 @@ class LdapConnections
            log.debug("Releasing manual config connections.");
            if (manualConfigReadOnlyConn != null)
            {
                manualConfigPool.releaseReadConnection(manualConfigReadOnlyConn);
                pool.releaseReadOnlyConnection(manualConfigReadOnlyConn);
            }
            if (manualConfigReadWriteConn != null)
            {
                manualConfigPool.releaseWriteConnection(manualConfigReadWriteConn);
                pool.releaseReadWriteConnection(manualConfigReadWriteConn);
            }
        }
    }
@@ -209,11 +199,11 @@ class LdapConnections
    @Override
    public void finalize()
    {
        if (manualConfigPool != null)
        if (pool != null)
        {
            log.debug("Closing manual config connection pool--should only see this " +
            		"message when running unit tests.");
            manualConfigPool.close();
            pool.shutdown();
        }
    }

@@ -222,7 +212,7 @@ class LdapConnections
        if (persistence != null)
            return persistence.getCurrentConfig();
        else
            return config;
            return pool.getCurrentConfig();

    }

+3 −1
Original line number Diff line number Diff line
@@ -68,14 +68,15 @@

package ca.nrc.cadc.ac.server.ldap;

import java.util.ArrayList;
import java.util.Arrays;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.junit.Assert;
import org.junit.Test;

import ca.nrc.cadc.ac.server.ldap.LdapConfig.PoolPolicy;
import ca.nrc.cadc.util.Log4jInit;
import ca.nrc.cadc.util.PropertiesReader;

/**
@@ -87,6 +88,7 @@ public class LdapConfigTest

    public LdapConfigTest()
    {
        Log4jInit.setLevel("ca.nrc.cadc.ac", Level.INFO);
    }

    @Test
+116 −0
Original line number Diff line number Diff line
package ca.nrc.cadc.ac.server.ldap;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.easymock.EasyMock;
import org.junit.Assert;
import org.junit.Test;

import com.unboundid.ldap.sdk.LDAPConnection;

import ca.nrc.cadc.util.Log4jInit;

public class LdapConnectionsTest
{

    private final static Logger log = Logger.getLogger(LdapConnectionsTest.class);

    public LdapConnectionsTest()
    {
        Log4jInit.setLevel("ca.nrc.cadc.ac", Level.DEBUG);
        Log4jInit.setLevel("ca.nrc.cadc.profiler", Level.DEBUG);
    }

    @Test
    public void testAutoConfig()
    {
        try
        {
            LDAPConnection readConn = new LDAPConnection();
            LDAPConnection writeConn = new LDAPConnection();
            LdapPersistence persistence = EasyMock.createMock(LdapPersistence.class);

            EasyMock.expect(persistence.getReadOnlyConnection()).andReturn(readConn).once();
            EasyMock.expect(persistence.getReadWriteConnection()).andReturn(writeConn).once();
            EasyMock.expect(persistence.getCurrentConfig()).andReturn(null).once();

            persistence.releaseReadOnlyConnection(readConn);
            EasyMock.expectLastCall().once();

            persistence.releaseReadWriteConnection(writeConn);
            EasyMock.expectLastCall().once();

            EasyMock.replay(persistence);

            LdapConnections connections = new LdapConnections(persistence);

            // multiple calls to get connections should only go to the pool once
            connections.getReadOnlyConnection();
            connections.getReadOnlyConnection();
            connections.getReadOnlyConnection();

            connections.getReadWriteConnection();
            connections.getReadWriteConnection();
            connections.getReadWriteConnection();

            connections.getCurrentConfig();

            connections.releaseConnections();

            EasyMock.verify(persistence);

        }
        catch (Exception e)
        {
            log.error("Unexpected exception", e);
            Assert.fail("Unexpected exception");
        }
    }

    @Test
    public void testManualConfig()
    {
        try
        {
            LDAPConnection readConn = new LDAPConnection();
            LDAPConnection writeConn = new LDAPConnection();
            LdapConnectionPool pool = EasyMock.createMock(LdapConnectionPool.class);

            EasyMock.expect(pool.getReadOnlyConnection()).andReturn(readConn).once();
            EasyMock.expect(pool.getReadWriteConnection()).andReturn(writeConn).once();
            EasyMock.expect(pool.getCurrentConfig()).andReturn(null).once();

            pool.releaseReadOnlyConnection(readConn);
            EasyMock.expectLastCall().once();

            pool.releaseReadWriteConnection(writeConn);
            EasyMock.expectLastCall().once();

            EasyMock.replay(pool);

            LdapConnections connections = new LdapConnections(pool);

            // multiple calls to get connections should only go to the pool once
            connections.getReadOnlyConnection();
            connections.getReadOnlyConnection();
            connections.getReadOnlyConnection();

            connections.getReadWriteConnection();
            connections.getReadWriteConnection();
            connections.getReadWriteConnection();

            connections.getCurrentConfig();

            connections.releaseConnections();

            EasyMock.verify(pool);

        }
        catch (Exception e)
        {
            log.error("Unexpected exception", e);
            Assert.fail("Unexpected exception");
        }
    }

}
+4 −2
Original line number Diff line number Diff line
@@ -117,7 +117,8 @@ public class LdapDAOTest extends AbstractLdapDAOTest

        subject.getPrincipals().add(httpPrincipal);

        LdapConnections connections = new LdapConnections(config);
        LdapConnectionPool pool = new LdapConnectionPool(config);
        LdapConnections connections = new LdapConnections(pool);
        final LdapDAOTestImpl ldapDao = new LdapDAOTestImpl(connections);

        Subject.doAs(subject, new PrivilegedExceptionAction<Object>()
@@ -191,7 +192,8 @@ public class LdapDAOTest extends AbstractLdapDAOTest
        subject.getPrincipals().add(dnPrincipal);

        LdapConfig config = LdapConfig.getLdapConfig("LdapConfig.test.properties");
        LdapConnections conn = new LdapConnections(config);
        LdapConnectionPool pool = new LdapConnectionPool(config);
        LdapConnections conn = new LdapConnections(pool);
        final LdapDAO ldapDAO = new LdapDAO(conn) { }; // abstract

        DN actual = Subject.doAs(subject, new PrivilegedAction<DN>()
Loading