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

Nicolas Capponi
04.53.2013 d1f95e8c6e6fcd090ffc0e814aca5f06c518fce0
* Add methods to test for object class presence in an entry:
Entries#containsObjectClass

* Add test case
2 files modified
57 ■■■■■ changed files
opendj-core/src/main/java/org/forgerock/opendj/ldap/Entries.java 42 ●●●●● patch | view | raw | blame | history
opendj-core/src/test/java/org/forgerock/opendj/ldap/EntriesTestCase.java 15 ●●●●● patch | view | raw | blame | history
opendj-core/src/main/java/org/forgerock/opendj/ldap/Entries.java
@@ -364,6 +364,48 @@
    }
    /**
     * Check if the provided entry contains the provided object class.
     * <p>
     * This method uses the default schema for decoding the object class
     * attribute values.
     * <p>
     * The provided object class must be recognized by the schema, otherwise the
     * method returns false.
     *
     * @param entry
     *            The entry which is checked against the object class.
     * @param objectClass
     *            The object class to check.
     * @return {@code true} if and only if entry contains the object class and
     *         the object class is recognized by the default schema,
     *         {@code false} otherwise
     */
    public static boolean containsObjectClass(final Entry entry, final ObjectClass objectClass) {
        return containsObjectClass(entry, Schema.getDefaultSchema(), objectClass);
    }
    /**
     * Check if the provided entry contains the provided object class.
     * <p>
     * The provided object class must be recognized by the provided schema,
     * otherwise the method returns false.
     *
     * @param entry
     *            The entry which is checked against the object class.
     * @param schema
     *            The schema which should be used for decoding the object class
     *            attribute values.
     * @param objectClass
     *            The object class to check.
     * @return {@code true} if and only if entry contains the object class and
     *         the object class is recognized by the provided schema,
     *         {@code false} otherwise
     */
    public static boolean containsObjectClass(final Entry entry, final Schema schema, final ObjectClass objectClass) {
        return getObjectClasses(entry, schema).contains(objectClass);
    }
    /**
     * Creates a new modify request containing a list of modifications which can
     * be used to transform {@code fromEntry} into entry {@code toEntry}.
     * <p>
opendj-core/src/test/java/org/forgerock/opendj/ldap/EntriesTestCase.java
@@ -26,10 +26,14 @@
package org.forgerock.opendj.ldap;
import static org.testng.AssertJUnit.assertFalse;
import static org.testng.AssertJUnit.assertTrue;
import java.util.Iterator;
import org.forgerock.opendj.ldap.requests.ModifyRequest;
import org.forgerock.opendj.ldap.requests.Requests;
import org.forgerock.opendj.ldap.schema.Schema;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
@@ -37,6 +41,7 @@
/**
 * Test {@code Entries}.
 */
@SuppressWarnings("javadoc")
public final class EntriesTestCase extends SdkTestCase {
    /**
     * Creates test data for {@link #testDiffEntries}.
@@ -212,4 +217,14 @@
            Assert.assertEquals(m1.getAttribute(), m2.getAttribute());
        }
    }
    @Test
    public void testContainsObjectClass() throws Exception {
        Entry entry = new LinkedHashMapEntry("dn: cn=test", "objectClass: top", "objectClass: person");
        Schema schema = Schema.getDefaultSchema();
        assertTrue("should contain top", Entries.containsObjectClass(entry, schema.getObjectClass("top")));
        assertTrue("should contain person", Entries.containsObjectClass(entry, schema.getObjectClass("person")));
        assertFalse("should not contain country", Entries.containsObjectClass(entry, schema.getObjectClass("country")));
    }
}