mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Nicolas Capponi
20.49.2013 379b85d1ec1f3145de3959d559599bbd6f5295d9

* Move makeEntries and makeEntry methods from TestCaseUtils to LDIF class.

* Add both methods to Entries class as well (indirection to LDIF implementations).

* Add test for both methods in LDIFTestCase.
4 files modified
258 ■■■■ changed files
opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldap/Entries.java 57 ●●●●● patch | view | raw | blame | history
opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldif/LDIF.java 65 ●●●●● patch | view | raw | blame | history
opendj-sdk/opendj-core/src/test/java/org/forgerock/opendj/ldap/TestCaseUtils.java 64 ●●●●● patch | view | raw | blame | history
opendj-sdk/opendj-core/src/test/java/org/forgerock/opendj/ldif/LDIFTestCase.java 72 ●●●●● patch | view | raw | blame | history
opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldap/Entries.java
@@ -28,15 +28,19 @@
package org.forgerock.opendj.ldap;
import static org.forgerock.opendj.ldap.AttributeDescription.objectClass;
import static com.forgerock.opendj.ldap.CoreMessages.*;
import static org.forgerock.opendj.ldap.ErrorResultException.newErrorResult;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.forgerock.i18n.LocalizableMessage;
@@ -48,6 +52,7 @@
import org.forgerock.opendj.ldap.schema.Schema;
import org.forgerock.opendj.ldap.schema.SchemaValidationPolicy;
import org.forgerock.opendj.ldap.schema.UnknownSchemaElementException;
import org.forgerock.opendj.ldif.LDIF;
import org.forgerock.util.Reject;
import com.forgerock.opendj.util.Iterables;
@@ -581,6 +586,58 @@
    }
    /**
     * Builds an entry from the provided lines of LDIF.
     * <p>
     * Sample usage:
     * <pre>
     * Entry john = makeEntry(
     *   "dn: cn=John Smith,dc=example,dc=com",
     *   "objectclass: inetorgperson",
     *   "cn: John Smith",
     *   "sn: Smith",
     *   "givenname: John");
     * </pre>
     *
     * @param ldifLines
     *          LDIF lines that contains entry definition.
     * @return an entry
     * @throws IOException
     *          If an error occurs.
     */
    public static Entry makeEntry(String... ldifLines) throws IOException {
        return LDIF.makeEntry(ldifLines);
    }
    /**
     * Builds a list of entries from the provided lines of LDIF.
     * <p>
     * Sample usage:
     * <pre>
     * List<Entry> smiths = TestCaseUtils.makeEntries(
     *   "dn: cn=John Smith,dc=example,dc=com",
     *   "objectclass: inetorgperson",
     *   "cn: John Smith",
     *   "sn: Smith",
     *   "givenname: John",
     *   "",
     *   "dn: cn=Jane Smith,dc=example,dc=com",
     *   "objectclass: inetorgperson",
     *   "cn: Jane Smith",
     *   "sn: Smith",
     *   "givenname: Jane");
     * </pre>
     * @param ldifLines
     *          LDIF lines that contains entries definition.
     *          Entries are separated by an empty string: {@code ""}.
     * @return a list of entries
     * @throws IOException
     *          If an error occurs.
     */
    public static List<Entry> makeEntries(String... ldifLines) throws IOException {
        return LDIF.makeEntries(ldifLines);
    }
    /**
     * Returns the structural object class associated with the provided entry,
     * or {@code null} if none was found. If the entry contains multiple
     * structural object classes then the first will be returned. This method
opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldif/LDIF.java
@@ -70,6 +70,8 @@
import org.forgerock.opendj.ldap.schema.AttributeUsage;
import org.forgerock.opendj.ldap.schema.Schema;
import com.forgerock.opendj.util.StaticUtils;
/**
 * This class contains common utility methods for creating and manipulating
 * readers and writers.
@@ -241,6 +243,69 @@
    }
    /**
     * Builds an entry from the provided lines of LDIF.
     * <p>
     * Sample usage:
     * <pre>
     * Entry john = makeEntry(
     *   "dn: cn=John Smith,dc=example,dc=com",
     *   "objectclass: inetorgperson",
     *   "cn: John Smith",
     *   "sn: Smith",
     *   "givenname: John");
     * </pre>
     *
     * @param ldifLines
     *          LDIF lines that contains entry definition.
     * @return an entry, or {@code null} if no ldif line is provided
     * @throws IOException
     *          If an error occurs.
     */
    public static Entry makeEntry(String... ldifLines) throws IOException {
        List<Entry> entries = makeEntries(ldifLines);
        return entries.isEmpty() ? null : entries.get(0);
    }
    /**
     * Builds a list of entries from the provided lines of LDIF.
     * <p>
     * Sample usage:
     * <pre>
     * List<Entry> smiths = TestCaseUtils.makeEntries(
     *   "dn: cn=John Smith,dc=example,dc=com",
     *   "objectclass: inetorgperson",
     *   "cn: John Smith",
     *   "sn: Smith",
     *   "givenname: John",
     *   "",
     *   "dn: cn=Jane Smith,dc=example,dc=com",
     *   "objectclass: inetorgperson",
     *   "cn: Jane Smith",
     *   "sn: Smith",
     *   "givenname: Jane");
     * </pre>
     * @param ldifLines
     *          LDIF lines that contains entries definition.
     *          Entries are separated by an empty string: {@code ""}.
     * @return a list of entries
     * @throws IOException
     *          If an error occurs.
     */
    public static List<Entry> makeEntries(String... ldifLines) throws IOException {
        List<Entry> entries = new ArrayList<Entry>();
        LDIFEntryReader reader = null;
        try {
            reader = new LDIFEntryReader(ldifLines);
            while (reader.hasNext()) {
                entries.add(reader.readEntry());
            }
        } finally {
            StaticUtils.closeSilently(reader);
        }
        return entries;
    }
    /**
     * Returns an entry reader over the provided entry collection.
     *
     * @param entries
opendj-sdk/opendj-core/src/test/java/org/forgerock/opendj/ldap/TestCaseUtils.java
@@ -37,16 +37,13 @@
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.SocketAddress;
import java.util.ArrayList;
import java.util.List;
import org.forgerock.opendj.ldif.LDIFEntryReader;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.mockito.stubbing.OngoingStubbing;
import com.forgerock.opendj.util.CompletedFutureResult;
import com.forgerock.opendj.util.StaticUtils;
import com.forgerock.opendj.util.TimeSource;
/**
@@ -244,65 +241,4 @@
        return mock;
    }
    /**
     * Builds an entry from the provided lines of LDIF.
     * <p>
     * Here's a sample usage:
     * <pre>
     * Entry john = makeEntry(
     *   "dn: cn=John Smith,dc=example,dc=com",
     *   "objectclass: inetorgperson",
     *   "cn: John Smith",
     *   "sn: Smith",
     *   "givenname: John");
     * </pre>
     *
     * @param lines
     *          LDIF lines that contains entry definition.
     * @return an entry
     * @throws IOException
     *          If an error occurs.
     */
    public static Entry makeEntry(String... lines) throws IOException {
        return makeEntries(lines).get(0);
    }
    /**
     * Builds a list of entries from the provided lines of LDIF.
     * <p>
     * Here's a sample usage
     * <pre>
     * List<Entry> smiths = TestCaseUtils.makeEntries(
     *   "dn: cn=John Smith,dc=example,dc=com",
     *   "objectclass: inetorgperson",
     *   "cn: John Smith",
     *   "sn: Smith",
     *   "givenname: John",
     *   "",
     *   "dn: cn=Jane Smith,dc=example,dc=com",
     *   "objectclass: inetorgperson",
     *   "cn: Jane Smith",
     *   "sn: Smith",
     *   "givenname: Jane");
     * </pre>
     * @param ldifLines
     *          LDIF lines that contains entries definition.
     * @return a list of entries
     * @throws IOException
     *          If an error occurs.
     */
    public static List<Entry> makeEntries(String... ldifLines) throws IOException {
        List<Entry> entries = new ArrayList<Entry>();
        LDIFEntryReader reader = null;
        try {
            reader = new LDIFEntryReader(ldifLines);
            while (reader.hasNext()) {
                entries.add(reader.readEntry());
            }
        } finally {
            StaticUtils.closeSilently(reader);
        }
        return entries;
    }
}
opendj-sdk/opendj-core/src/test/java/org/forgerock/opendj/ldif/LDIFTestCase.java
@@ -2744,4 +2744,76 @@
    public final void testLdifPatchDoesntAllowNull() throws Exception {
        LDIF.patch(null, null);
    }
    @Test
    public void testMakeEntry() throws Exception {
        // @formatter:off
        final Entry entry = LDIF.makeEntry(
            "dn: uid=user.1,ou=People,dc=example,dc=com",
            "objectClass: top",
            "objectClass: person",
            "objectClass: organizationalperson",
            "objectClass: inetorgperson",
            "givenName: Eniko",
            "sn: Atpco",
            "cn: Eniko Atpco",
            "uid: user.1"
        );
        // @formatter:on
        assertThat(entry.getName().toString()).isEqualTo("uid=user.1,ou=People,dc=example,dc=com");
        assertThat(entry.getAttribute("objectClass").firstValueAsString()).isEqualTo("top");
        assertThat(entry.getAttribute("uid").firstValueAsString()).isEqualTo("user.1");
        assertThat(entry.getAttribute("givenName").firstValueAsString()).isEqualTo("Eniko");
        assertThat(entry.getAttribute("sn").firstValueAsString()).isEqualTo("Atpco");
    }
    @Test
    public void testMakeEntries() throws Exception {
        // @formatter:off
        final List<Entry> entries = LDIF.makeEntries(
            "dn: uid=user.1,ou=People,dc=example,dc=com",
            "objectClass: top",
            "objectClass: person",
            "objectClass: organizationalperson",
            "objectClass: inetorgperson",
            "givenName: Eniko",
            "sn: Atpco",
            "uid: user.1",
            "",
            "dn: uid=user.2,ou=People,dc=example,dc=com",
            "objectClass: top",
            "objectClass: person",
            "objectClass: organizationalperson",
            "objectClass: inetorgperson",
            "givenName: Aaaron",
            "sn: Atp",
            "uid: user.2"
        );
        // @formatter:on
        assertThat(entries).hasSize(2);
        assertThat(entries.get(0).getName().toString()).isEqualTo("uid=user.1,ou=People,dc=example,dc=com");
        assertThat(entries.get(1).getName().toString()).isEqualTo("uid=user.2,ou=People,dc=example,dc=com");
    }
    @Test
    public void testMakeEntryEmpty() throws Exception {
        final Entry entry = LDIF.makeEntry();
        assertThat(entry).isNull();
    }
    @Test(expectedExceptions = DecodeException.class)
    public void testMakeEntryBadLDif() throws Exception {
        LDIF.makeEntry("dummy: uid=user.1,ou=People,dc=example,dc=com");
    }
    @Test
    public void testMakeEntriesEmpty() throws Exception {
        final List<Entry> entries = LDIF.makeEntries();
        assertThat(entries).hasSize(0);
    }
    @Test(expectedExceptions = DecodeException.class)
    public void testMakeEntriesBadLDif() throws Exception {
        LDIF.makeEntries("dummy: uid=user.1,ou=People,dc=example,dc=com");
    }
}