From d1f95e8c6e6fcd090ffc0e814aca5f06c518fce0 Mon Sep 17 00:00:00 2001
From: Nicolas Capponi <nicolas.capponi@forgerock.com>
Date: Wed, 04 Dec 2013 15:53:00 +0000
Subject: [PATCH] * Add methods to test for object class presence in an entry:    Entries#containsObjectClass     * Add test case

---
 opendj-core/src/main/java/org/forgerock/opendj/ldap/Entries.java         |   42 ++++++++++++++++++++++++++++++++++++++++++
 opendj-core/src/test/java/org/forgerock/opendj/ldap/EntriesTestCase.java |   15 +++++++++++++++
 2 files changed, 57 insertions(+), 0 deletions(-)

diff --git a/opendj-core/src/main/java/org/forgerock/opendj/ldap/Entries.java b/opendj-core/src/main/java/org/forgerock/opendj/ldap/Entries.java
index 2398706..811ac97 100644
--- a/opendj-core/src/main/java/org/forgerock/opendj/ldap/Entries.java
+++ b/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>
diff --git a/opendj-core/src/test/java/org/forgerock/opendj/ldap/EntriesTestCase.java b/opendj-core/src/test/java/org/forgerock/opendj/ldap/EntriesTestCase.java
index b08f423..aa92855 100644
--- a/opendj-core/src/test/java/org/forgerock/opendj/ldap/EntriesTestCase.java
+++ b/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")));
+    }
 }

--
Gitblit v1.10.0