From 8adb7af006bb6bf8a71f86bb520671de381c8e3e Mon Sep 17 00:00:00 2001
From: Matthew Swift <matthew.swift@forgerock.com>
Date: Wed, 13 Feb 2013 09:49:16 +0000
Subject: [PATCH] Minor API cleanup to make it easier to use.

---
 opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/controls/ServerSideSortRequestControl.java |   24 +--
 opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/Attribute.java                             |   12 -
 opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/LinkedAttribute.java                       |   50 ++++++-
 opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/SortKey.java                               |   41 ++----
 opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/AbstractAttribute.java                     |   15 +-
 opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/controls/MatchedValuesRequestControl.java  |   34 ++---
 opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/Attributes.java                            |    8 
 opendj3/opendj-ldap-sdk/src/test/java/org/forgerock/opendj/ldap/TestCaseUtils.java                         |   84 ++++++++++++++
 opendj3/opendj-ldap-sdk/src/test/java/org/forgerock/opendj/ldap/ConnectionPoolTestCase.java                |   56 ---------
 opendj3/opendj-rest2ldap/src/main/java/org/forgerock/opendj/rest2ldap/LDAPConstantAttributeMapper.java     |    7 
 opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/requests/ModifyRequestImpl.java            |    8 -
 11 files changed, 181 insertions(+), 158 deletions(-)

diff --git a/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/AbstractAttribute.java b/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/AbstractAttribute.java
index ce2086c..bb6d853 100644
--- a/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/AbstractAttribute.java
+++ b/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/AbstractAttribute.java
@@ -22,7 +22,7 @@
  *
  *
  *      Copyright 2009 Sun Microsystems, Inc.
- *      Portions copyright 2012 ForgeRock AS.
+ *      Portions copyright 2012-2013 ForgeRock AS.
  */
 
 package org.forgerock.opendj.ldap;
@@ -165,14 +165,11 @@
     /**
      * {@inheritDoc}
      */
-    public boolean add(final Object firstValue, final Object... remainingValues) {
-        Validator.ensureNotNull(firstValue);
-
-        boolean modified = add(ByteString.valueOf(firstValue));
-        if (remainingValues != null) {
-            for (final Object value : remainingValues) {
-                modified |= add(ByteString.valueOf(value));
-            }
+    public boolean add(final Object... values) {
+        Validator.ensureNotNull(values);
+        boolean modified = false;
+        for (final Object value : values) {
+            modified |= add(ByteString.valueOf(value));
         }
         return modified;
     }
diff --git a/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/Attribute.java b/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/Attribute.java
index c7ace49..5932a3d 100644
--- a/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/Attribute.java
+++ b/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/Attribute.java
@@ -22,7 +22,7 @@
  *
  *
  *      Copyright 2009 Sun Microsystems, Inc.
- *      Portions copyright 2012 ForgeRock AS.
+ *      Portions copyright 2012-2013 ForgeRock AS.
  */
 
 package org.forgerock.opendj.ldap;
@@ -69,18 +69,16 @@
      * Any attribute values which are not instances of {@code ByteString} will
      * be converted using the {@link ByteString#valueOf(Object)} method.
      *
-     * @param firstValue
-     *            The first attribute value to be added to this attribute.
-     * @param remainingValues
-     *            The remaining attribute values to be added to this attribute.
+     * @param values
+     *            The attribute values to be added to this attribute.
      * @return {@code true} if this attribute changed as a result of this call.
      * @throws UnsupportedOperationException
      *             If this attribute does not support addition of attribute
      *             values.
      * @throws NullPointerException
-     *             If {@code firstValue} was {@code null}.
+     *             If {@code values} was {@code null}.
      */
-    boolean add(Object firstValue, Object... remainingValues);
+    boolean add(Object... values);
 
     /**
      * Adds all of the attribute values contained in {@code values} to this
diff --git a/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/Attributes.java b/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/Attributes.java
index 5311bdc..9d3d416 100644
--- a/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/Attributes.java
+++ b/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/Attributes.java
@@ -22,7 +22,7 @@
  *
  *
  *      Copyright 2010 Sun Microsystems, Inc.
- *      Portions copyright 2011-2012 ForgeRock AS.
+ *      Portions copyright 2011-2013 ForgeRock AS.
  */
 
 package org.forgerock.opendj.ldap;
@@ -113,8 +113,8 @@
         }
 
         @Override
-        public boolean add(final Object firstValue, final Object... remainingValues) {
-            return attribute.add(firstValue, remainingValues);
+        public boolean add(final Object... values) {
+            return attribute.add(values);
         }
 
         @Override
@@ -320,7 +320,7 @@
         }
 
         @Override
-        public boolean add(final Object firstValue, final Object... remainingValues) {
+        public boolean add(final Object... values) {
             throw new UnsupportedOperationException();
         }
 
diff --git a/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/LinkedAttribute.java b/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/LinkedAttribute.java
index 1a4458e..ff8e0dc 100644
--- a/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/LinkedAttribute.java
+++ b/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/LinkedAttribute.java
@@ -22,12 +22,11 @@
  *
  *
  *      Copyright 2009-2010 Sun Microsystems, Inc.
- *      Portions copyright 2012 ForgeRock AS.
+ *      Portions copyright 2012-2013 ForgeRock AS.
  */
 
 package org.forgerock.opendj.ldap;
 
-import java.util.Arrays;
 import java.util.Collection;
 import java.util.ConcurrentModificationException;
 import java.util.HashMap;
@@ -506,6 +505,9 @@
     /**
      * Creates a new attribute having the specified attribute description and
      * single attribute value.
+     * <p>
+     * If {@code value} is not an instance of {@code ByteString} then it will be
+     * converted using the {@link ByteString#valueOf(Object)} method.
      *
      * @param attributeDescription
      *            The attribute description.
@@ -515,7 +517,7 @@
      *             If {@code attributeDescription} or {@code value} was
      *             {@code null} .
      */
-    public LinkedAttribute(final AttributeDescription attributeDescription, final ByteString value) {
+    public LinkedAttribute(final AttributeDescription attributeDescription, final Object value) {
         this(attributeDescription);
         add(value);
     }
@@ -523,6 +525,9 @@
     /**
      * Creates a new attribute having the specified attribute description and
      * attribute values.
+     * <p>
+     * Any attribute values which are not instances of {@code ByteString} will
+     * be converted using the {@link ByteString#valueOf(Object)} method.
      *
      * @param attributeDescription
      *            The attribute description.
@@ -533,14 +538,17 @@
      *             {@code null}.
      */
     public LinkedAttribute(final AttributeDescription attributeDescription,
-            final ByteString... values) {
+            final Object... values) {
         this(attributeDescription);
-        addAll(Arrays.asList(values));
+        add(values);
     }
 
     /**
      * Creates a new attribute having the specified attribute description and
      * attribute values.
+     * <p>
+     * Any attribute values which are not instances of {@code ByteString} will
+     * be converted using the {@link ByteString#valueOf(Object)} method.
      *
      * @param attributeDescription
      *            The attribute description.
@@ -551,9 +559,9 @@
      *             {@code null}.
      */
     public LinkedAttribute(final AttributeDescription attributeDescription,
-            final Collection<ByteString> values) {
+            final Collection<?> values) {
         this(attributeDescription);
-        addAll(values);
+        addAll(values, null);
     }
 
     /**
@@ -575,6 +583,30 @@
 
     /**
      * Creates a new attribute having the specified attribute description and
+     * attribute values. The attribute description will be decoded using the
+     * default schema.
+     * <p>
+     * Any attribute values which are not instances of {@code ByteString} will
+     * be converted using the {@link ByteString#valueOf(Object)} method.
+     *
+     * @param attributeDescription
+     *            The attribute description.
+     * @param values
+     *            The attribute values.
+     * @throws LocalizedIllegalArgumentException
+     *             If {@code attributeDescription} could not be decoded using
+     *             the default schema.
+     * @throws NullPointerException
+     *             If {@code attributeDescription} or {@code values} was
+     *             {@code null}.
+     */
+    public LinkedAttribute(final String attributeDescription, final Collection<?> values) {
+        this(attributeDescription);
+        addAll(values, null);
+    }
+
+    /**
+     * Creates a new attribute having the specified attribute description and
      * single attribute value. The attribute description will be decoded using
      * the default schema.
      * <p>
@@ -618,9 +650,7 @@
      */
     public LinkedAttribute(final String attributeDescription, final Object... values) {
         this(attributeDescription);
-        for (final Object value : values) {
-            add(ByteString.valueOf(value));
-        }
+        add(values);
     }
 
     /**
diff --git a/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/SortKey.java b/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/SortKey.java
index a8d0325..0c650be 100644
--- a/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/SortKey.java
+++ b/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/SortKey.java
@@ -22,13 +22,14 @@
  *
  *
  *      Copyright 2010 Sun Microsystems, Inc.
- *      Portions copyright 2012 ForgeRock AS.
+ *      Portions copyright 2012-2013 ForgeRock AS.
  */
 package org.forgerock.opendj.ldap;
 
 import static org.forgerock.opendj.ldap.CoreMessages.*;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.Comparator;
 import java.util.LinkedList;
@@ -223,28 +224,19 @@
      *
      * @param schema
      *            The schema which should be used for decoding the sort keys.
-     * @param firstKey
-     *            The first sort key.
-     * @param remainingKeys
-     *            The remaining sort keys.
+     * @param keys
+     *            The list of sort keys.
      * @return The {@code Comparator}.
      * @throws LocalizedIllegalArgumentException
      *             If one of the sort keys could not be converted to a
      *             comparator.
+     * @throws IllegalArgumentException
+     *             If {@code keys} was empty.
      * @throws NullPointerException
-     *             If {@code schema} or {@code firstKey} was {@code null}.
+     *             If {@code schema} or {@code keys} was {@code null}.
      */
-    public static Comparator<Entry> comparator(final Schema schema, final SortKey firstKey,
-            final SortKey... remainingKeys) {
-        Validator.ensureNotNull(schema, firstKey, remainingKeys);
-
-        final List<Comparator<Entry>> comparators =
-                new ArrayList<Comparator<Entry>>(1 + remainingKeys.length);
-        comparators.add(firstKey.comparator(schema));
-        for (final SortKey key : remainingKeys) {
-            comparators.add(key.comparator(schema));
-        }
-        return new CompositeEntryComparator(comparators);
+    public static Comparator<Entry> comparator(final Schema schema, final SortKey... keys) {
+        return comparator(schema, Arrays.asList(keys));
     }
 
     /**
@@ -252,20 +244,19 @@
      * the provided list of sort keys. The sort keys will be decoded using the
      * default schema.
      *
-     * @param firstKey
-     *            The first sort key.
-     * @param remainingKeys
-     *            The remaining sort keys.
+     * @param keys
+     *            The list of sort keys.
      * @return The {@code Comparator}.
      * @throws LocalizedIllegalArgumentException
      *             If one of the sort keys could not be converted to a
      *             comparator.
+     * @throws IllegalArgumentException
+     *             If {@code keys} was empty.
      * @throws NullPointerException
-     *             If {@code firstKey} was {@code null}.
+     *             If {@code keys} was {@code null}.
      */
-    public static Comparator<Entry> comparator(final SortKey firstKey,
-            final SortKey... remainingKeys) {
-        return comparator(Schema.getDefaultSchema(), firstKey, remainingKeys);
+    public static Comparator<Entry> comparator(final SortKey... keys) {
+        return comparator(Schema.getDefaultSchema(), keys);
     }
 
     /**
diff --git a/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/controls/MatchedValuesRequestControl.java b/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/controls/MatchedValuesRequestControl.java
index a46f70a..ac1cc12 100644
--- a/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/controls/MatchedValuesRequestControl.java
+++ b/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/controls/MatchedValuesRequestControl.java
@@ -22,7 +22,7 @@
  *
  *
  *      Copyright 2009 Sun Microsystems, Inc.
- *      Portions copyright 2012 ForgeRock AS.
+ *      Portions copyright 2012-2013 ForgeRock AS.
  */
 
 package org.forgerock.opendj.ldap.controls;
@@ -271,36 +271,28 @@
      *            {@code true} if it is unacceptable to perform the operation
      *            without applying the semantics of this control, or
      *            {@code false} if it can be ignored.
-     * @param firstFilter
-     *            The first matched values filter.
-     * @param remainingFilters
-     *            The remaining matched values filter, may be {@code null} or
-     *            empty.
+     * @param filters
+     *            The list of filters of which at least one must match an
+     *            attribute value in order for the attribute value to be
+     *            returned to the client. The list must not be empty.
      * @return The new control.
      * @throws LocalizedIllegalArgumentException
      *             If one or more filters could not be parsed, or if one or more
      *             filters failed to conform to the filter constraints defined
      *             in RFC 3876.
      * @throws NullPointerException
-     *             If {@code firstFilter} was {@code null}.
+     *             If {@code filters} was {@code null}.
      */
     public static MatchedValuesRequestControl newControl(final boolean isCritical,
-            final String firstFilter, final String... remainingFilters) {
-        Validator.ensureNotNull(firstFilter);
+            final String... filters) {
+        Validator.ensureTrue(filters.length > 0, "filters is empty");
 
-        List<Filter> filters;
-        if (remainingFilters == null || remainingFilters.length == 0) {
-            filters = Collections.singletonList(validateFilter(Filter.valueOf(firstFilter)));
-        } else {
-            filters = new ArrayList<Filter>(1 + remainingFilters.length);
-            filters.add(validateFilter(Filter.valueOf(firstFilter)));
-            for (final String filter : remainingFilters) {
-                filters.add(validateFilter(Filter.valueOf(filter)));
-            }
-            filters = Collections.unmodifiableList(filters);
+        final List<Filter> parsedFilters = new ArrayList<Filter>(filters.length);
+        for (final String filter : filters) {
+            parsedFilters.add(validateFilter(Filter.valueOf(filter)));
         }
-
-        return new MatchedValuesRequestControl(isCritical, filters);
+        return new MatchedValuesRequestControl(isCritical, Collections
+                .unmodifiableList(parsedFilters));
     }
 
     private static Filter validateFilter(final Filter filter) {
diff --git a/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/controls/ServerSideSortRequestControl.java b/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/controls/ServerSideSortRequestControl.java
index 231491d..20f7213 100644
--- a/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/controls/ServerSideSortRequestControl.java
+++ b/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/controls/ServerSideSortRequestControl.java
@@ -22,7 +22,7 @@
  *
  *
  *      Copyright 2010 Sun Microsystems, Inc.
- *      Portions copyright 2012 ForgeRock AS.
+ *      Portions copyright 2012-2013 ForgeRock AS.
  */
 package org.forgerock.opendj.ldap.controls;
 
@@ -31,6 +31,7 @@
 
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.LinkedList;
@@ -209,24 +210,17 @@
      *            {@code true} if it is unacceptable to perform the operation
      *            without applying the semantics of this control, or
      *            {@code false} if it can be ignored.
-     * @param firstKey
-     *            The first sort key.
-     * @param remainingKeys
-     *            The remaining sort keys.
+     * @param keys
+     *            The list of sort keys.
      * @return The new control.
+     * @throws IllegalArgumentException
+     *             If {@code keys} was empty.
      * @throws NullPointerException
-     *             If {@code firstKey} was {@code null}.
+     *             If {@code keys} was {@code null}.
      */
     public static ServerSideSortRequestControl newControl(final boolean isCritical,
-            final SortKey firstKey, final SortKey... remainingKeys) {
-        Validator.ensureNotNull(firstKey, remainingKeys);
-
-        final List<SortKey> keys = new ArrayList<SortKey>(1 + remainingKeys.length);
-        keys.add(firstKey);
-        for (final SortKey key : remainingKeys) {
-            keys.add(key);
-        }
-        return new ServerSideSortRequestControl(isCritical, Collections.unmodifiableList(keys));
+            final SortKey... keys) {
+        return newControl(isCritical, Arrays.asList(keys));
     }
 
     /**
diff --git a/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/requests/ModifyRequestImpl.java b/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/requests/ModifyRequestImpl.java
index a3f589d..22155f6 100644
--- a/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/requests/ModifyRequestImpl.java
+++ b/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/requests/ModifyRequestImpl.java
@@ -22,7 +22,7 @@
  *
  *
  *      Copyright 2010 Sun Microsystems, Inc.
- *      Portions copyright 2012 ForgeRock AS.
+ *      Portions copyright 2012-2013 ForgeRock AS.
  */
 
 package org.forgerock.opendj.ldap.requests;
@@ -88,12 +88,6 @@
         return v.visitChangeRecord(p, this);
     }
 
-    public ModifyRequest addChange(final ModificationType type, final String attributeDescription,
-            final Object firstValue, final Object... remainingValues) {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
     /**
      * {@inheritDoc}
      */
diff --git a/opendj3/opendj-ldap-sdk/src/test/java/org/forgerock/opendj/ldap/ConnectionPoolTestCase.java b/opendj3/opendj-ldap-sdk/src/test/java/org/forgerock/opendj/ldap/ConnectionPoolTestCase.java
index be92d7c..226d8e7 100644
--- a/opendj3/opendj-ldap-sdk/src/test/java/org/forgerock/opendj/ldap/ConnectionPoolTestCase.java
+++ b/opendj3/opendj-ldap-sdk/src/test/java/org/forgerock/opendj/ldap/ConnectionPoolTestCase.java
@@ -30,11 +30,11 @@
 import static org.fest.assertions.Assertions.assertThat;
 import static org.forgerock.opendj.ldap.Connections.newFixedConnectionPool;
 import static org.forgerock.opendj.ldap.ErrorResultException.newErrorResult;
+import static org.forgerock.opendj.ldap.TestCaseUtils.*;
 import static org.mockito.Matchers.any;
 import static org.mockito.Matchers.anyBoolean;
 import static org.mockito.Matchers.eq;
 import static org.mockito.Matchers.isA;
-import static org.mockito.Mockito.doAnswer;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
@@ -49,12 +49,8 @@
 import org.forgerock.opendj.ldap.requests.Requests;
 import org.forgerock.opendj.ldap.responses.ExtendedResult;
 import org.forgerock.opendj.ldap.responses.Responses;
-import org.mockito.invocation.InvocationOnMock;
-import org.mockito.stubbing.Answer;
 import org.testng.annotations.Test;
 
-import com.forgerock.opendj.util.CompletedFutureResult;
-
 /**
  * Tests the connection pool implementation..
  */
@@ -418,54 +414,4 @@
         pool.close();
     }
 
-    private Connection mockConnection(final List<ConnectionEventListener> listeners) {
-        final Connection mockConnection = mock(Connection.class);
-
-        // Handle listener registration / deregistration in mock connection.
-        doAnswer(new Answer<Void>() {
-            @Override
-            public Void answer(final InvocationOnMock invocation) throws Throwable {
-                final ConnectionEventListener listener =
-                        (ConnectionEventListener) invocation.getArguments()[0];
-                listeners.add(listener);
-                return null;
-            }
-        }).when(mockConnection).addConnectionEventListener(any(ConnectionEventListener.class));
-
-        doAnswer(new Answer<Void>() {
-            @Override
-            public Void answer(final InvocationOnMock invocation) throws Throwable {
-                final ConnectionEventListener listener =
-                        (ConnectionEventListener) invocation.getArguments()[0];
-                listeners.remove(listener);
-                return null;
-            }
-        }).when(mockConnection).removeConnectionEventListener(any(ConnectionEventListener.class));
-
-        return mockConnection;
-    }
-
-    @SuppressWarnings("unchecked")
-    private ConnectionFactory mockConnectionFactory(final Connection first,
-            final Connection... remaining) throws ErrorResultException {
-        final ConnectionFactory factory = mock(ConnectionFactory.class);
-        when(factory.getConnection()).thenReturn(first, remaining);
-        when(factory.getConnectionAsync(any(ResultHandler.class))).thenAnswer(
-                new Answer<FutureResult<Connection>>() {
-                    @Override
-                    public FutureResult<Connection> answer(final InvocationOnMock invocation)
-                            throws Throwable {
-                        final Connection connection = factory.getConnection();
-                        // Execute handler and return future.
-                        final ResultHandler<? super Connection> handler =
-                                (ResultHandler<? super Connection>) invocation.getArguments()[0];
-                        if (handler != null) {
-                            handler.handleResult(connection);
-                        }
-                        return new CompletedFutureResult<Connection>(connection);
-                    }
-                });
-        return factory;
-    }
-
 }
diff --git a/opendj3/opendj-ldap-sdk/src/test/java/org/forgerock/opendj/ldap/TestCaseUtils.java b/opendj3/opendj-ldap-sdk/src/test/java/org/forgerock/opendj/ldap/TestCaseUtils.java
index a07a24f..33b34f5 100644
--- a/opendj3/opendj-ldap-sdk/src/test/java/org/forgerock/opendj/ldap/TestCaseUtils.java
+++ b/opendj3/opendj-ldap-sdk/src/test/java/org/forgerock/opendj/ldap/TestCaseUtils.java
@@ -22,16 +22,28 @@
  *
  *
  *      Copyright 2009-2010 Sun Microsystems, Inc.
+ *      Portions copyright 2012 ForgeRock AS.
  */
 
 package org.forgerock.opendj.ldap;
 
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
 import java.io.File;
 import java.io.FileWriter;
 import java.io.IOException;
 import java.net.InetSocketAddress;
 import java.net.ServerSocket;
 import java.net.SocketAddress;
+import java.util.List;
+
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
+
+import com.forgerock.opendj.util.CompletedFutureResult;
 
 /**
  * This class defines some utility functions which can be used by test cases.
@@ -118,4 +130,76 @@
     public static SocketAddress getServerSocketAddress() {
         return LDAPServer.getInstance().getSocketAddress();
     }
+
+    /**
+     * Creates a mock connection factory which will return the provided
+     * connections in order.
+     *
+     * @param first
+     *            The first connection to return.
+     * @param remaining
+     *            The remaining connections to return.
+     * @return The connection factory.
+     */
+    @SuppressWarnings("unchecked")
+    public static ConnectionFactory mockConnectionFactory(final Connection first,
+            final Connection... remaining) {
+        final ConnectionFactory factory = mock(ConnectionFactory.class);
+        try {
+            when(factory.getConnection()).thenReturn(first, remaining);
+        } catch (ErrorResultException ignored) {
+            // Cannot happen.
+        }
+        when(factory.getConnectionAsync(any(ResultHandler.class))).thenAnswer(
+                new Answer<FutureResult<Connection>>() {
+                    @Override
+                    public FutureResult<Connection> answer(final InvocationOnMock invocation)
+                            throws Throwable {
+                        final Connection connection = factory.getConnection();
+                        // Execute handler and return future.
+                        final ResultHandler<? super Connection> handler =
+                                (ResultHandler<? super Connection>) invocation.getArguments()[0];
+                        if (handler != null) {
+                            handler.handleResult(connection);
+                        }
+                        return new CompletedFutureResult<Connection>(connection);
+                    }
+                });
+        return factory;
+    }
+
+    /**
+     * Creates a mock connection which will store connection event listeners in
+     * the provided list.
+     *
+     * @param listeners
+     *            The list which should be used for storing event listeners.
+     * @return The mock connection.
+     */
+    public static Connection mockConnection(final List<ConnectionEventListener> listeners) {
+        final Connection mockConnection = mock(Connection.class);
+
+        // Handle listener registration / deregistration in mock connection.
+        doAnswer(new Answer<Void>() {
+            @Override
+            public Void answer(final InvocationOnMock invocation) throws Throwable {
+                final ConnectionEventListener listener =
+                        (ConnectionEventListener) invocation.getArguments()[0];
+                listeners.add(listener);
+                return null;
+            }
+        }).when(mockConnection).addConnectionEventListener(any(ConnectionEventListener.class));
+
+        doAnswer(new Answer<Void>() {
+            @Override
+            public Void answer(final InvocationOnMock invocation) throws Throwable {
+                final ConnectionEventListener listener =
+                        (ConnectionEventListener) invocation.getArguments()[0];
+                listeners.remove(listener);
+                return null;
+            }
+        }).when(mockConnection).removeConnectionEventListener(any(ConnectionEventListener.class));
+
+        return mockConnection;
+    }
 }
diff --git a/opendj3/opendj-rest2ldap/src/main/java/org/forgerock/opendj/rest2ldap/LDAPConstantAttributeMapper.java b/opendj3/opendj-rest2ldap/src/main/java/org/forgerock/opendj/rest2ldap/LDAPConstantAttributeMapper.java
index eb5d0cb..e1b5ade 100644
--- a/opendj3/opendj-rest2ldap/src/main/java/org/forgerock/opendj/rest2ldap/LDAPConstantAttributeMapper.java
+++ b/opendj3/opendj-rest2ldap/src/main/java/org/forgerock/opendj/rest2ldap/LDAPConstantAttributeMapper.java
@@ -43,11 +43,8 @@
         if (attributeValues.length == 1) {
             attributes = singletonList(singletonAttribute(attributeName, attributeValues[0]));
         } else {
-            Attribute attribute = new LinkedAttribute(attributeName);
-            for (Object o : attributeValues) {
-                attribute.add(o);
-            }
-            attributes = singletonList(attribute);
+            attributes =
+                    singletonList((Attribute) new LinkedAttribute(attributeName, attributeValues));
         }
     }
 

--
Gitblit v1.10.0