From 8d16f1ef80784676d9ab34cb12b6954da96d3276 Mon Sep 17 00:00:00 2001
From: Matthew Swift <matthew.swift@forgerock.com>
Date: Fri, 04 Mar 2016 18:18:12 +0000
Subject: [PATCH] OPENDJ-2738 DN validation fails when RDN uses a custom attribute

---
 opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldap/schema/SchemaBuilder.java         |   44 ++++++++--------------
 opendj-sdk/opendj-core/src/test/java/org/forgerock/opendj/ldap/schema/SchemaBuilderTestCase.java |   21 ++++++++++
 2 files changed, 37 insertions(+), 28 deletions(-)

diff --git a/opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldap/schema/SchemaBuilder.java b/opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldap/schema/SchemaBuilder.java
index afb7925..1a19fca 100644
--- a/opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldap/schema/SchemaBuilder.java
+++ b/opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldap/schema/SchemaBuilder.java
@@ -1189,13 +1189,7 @@
      * @return A builder to continue building the MatchingRule.
      */
     public MatchingRule.Builder buildMatchingRule(final MatchingRule matchingRule) {
-        return buildMatchingRule(matchingRule, true);
-    }
-
-    private MatchingRule.Builder buildMatchingRule(final MatchingRule matchingRule, final boolean initialize) {
-        if (initialize) {
-            lazyInitBuilder();
-        }
+        lazyInitBuilder();
         return new MatchingRule.Builder(matchingRule, this);
     }
 
@@ -1255,13 +1249,7 @@
      * @return A builder to continue building the Syntax.
      */
     public Syntax.Builder buildSyntax(final Syntax syntax) {
-        return buildSyntax(syntax, true);
-    }
-
-    private Syntax.Builder buildSyntax(final Syntax syntax, final boolean initialize) {
-        if (initialize) {
-            lazyInitBuilder();
-        }
+        lazyInitBuilder();
         return new Syntax.Builder(syntax, this);
     }
 
@@ -2346,35 +2334,35 @@
         // unlikely, may be different in the new schema.
 
         for (final Syntax syntax : schema.getSyntaxes()) {
-            buildSyntax(syntax, false).addToSchema(overwrite);
+            buildSyntax(syntax).addToSchema(overwrite);
         }
 
         for (final MatchingRule matchingRule : schema.getMatchingRules()) {
-            buildMatchingRule(matchingRule, false).addToSchema(overwrite);
+            buildMatchingRule(matchingRule).addToSchema(overwrite);
         }
 
         for (final MatchingRuleUse matchingRuleUse : schema.getMatchingRuleUses()) {
-            addMatchingRuleUse(matchingRuleUse, overwrite);
+            buildMatchingRuleUse(matchingRuleUse).addToSchema(overwrite);
         }
 
         for (final AttributeType attributeType : schema.getAttributeTypes()) {
-            addAttributeType(attributeType, overwrite);
+            buildAttributeType(attributeType).addToSchema(overwrite);
         }
 
         for (final ObjectClass objectClass : schema.getObjectClasses()) {
-            addObjectClass(objectClass, overwrite);
+            buildObjectClass(objectClass).addToSchema(overwrite);
         }
 
         for (final NameForm nameForm : schema.getNameForms()) {
-            addNameForm(nameForm, overwrite);
+            buildNameForm(nameForm).addToSchema(overwrite);
         }
 
         for (final DITContentRule contentRule : schema.getDITContentRules()) {
-            addDITContentRule(contentRule, overwrite);
+            buildDITContentRule(contentRule).addToSchema(overwrite);
         }
 
         for (final DITStructureRule structureRule : schema.getDITStuctureRules()) {
-            addDITStructureRule(structureRule, overwrite);
+            buildDITStructureRule(structureRule).addToSchema(overwrite);
         }
     }
 
@@ -2421,13 +2409,13 @@
             nameForm2StructureRules = new HashMap<>();
             name2OIDs = new HashMap<>();
             warnings = new LinkedList<>();
-        }
 
-        if (copyOnWriteSchema != null) {
-            // Copy the schema.
-            addSchema0(copyOnWriteSchema, true);
-            options = Options.copyOf(copyOnWriteSchema.getOptions());
-            copyOnWriteSchema = null;
+            if (copyOnWriteSchema != null) {
+                // Copy the schema.
+                addSchema0(copyOnWriteSchema, true);
+                options = Options.copyOf(copyOnWriteSchema.getOptions());
+                copyOnWriteSchema = null;
+            }
         }
     }
 
diff --git a/opendj-sdk/opendj-core/src/test/java/org/forgerock/opendj/ldap/schema/SchemaBuilderTestCase.java b/opendj-sdk/opendj-core/src/test/java/org/forgerock/opendj/ldap/schema/SchemaBuilderTestCase.java
index 29c2e07..af5cf03 100644
--- a/opendj-sdk/opendj-core/src/test/java/org/forgerock/opendj/ldap/schema/SchemaBuilderTestCase.java
+++ b/opendj-sdk/opendj-core/src/test/java/org/forgerock/opendj/ldap/schema/SchemaBuilderTestCase.java
@@ -27,6 +27,7 @@
 
 import java.util.ArrayList;
 
+import org.forgerock.i18n.LocalizableMessageBuilder;
 import org.forgerock.i18n.LocalizedIllegalArgumentException;
 import org.forgerock.opendj.ldap.ByteString;
 import org.forgerock.opendj.ldap.Connection;
@@ -2137,4 +2138,24 @@
             .as("Expected the enum ordering matching rule to be removed at the same time as the enum syntax")
             .hasSize(coreSchema.getMatchingRules().size());
     }
+
+    @Test
+    public void attributeTypesUseNewlyBuiltSyntaxes() throws Exception {
+        final Schema coreSchema = Schema.getCoreSchema();
+        final Schema schema = new SchemaBuilder(coreSchema)
+                .addAttributeType("( 1.2.3.4.5.7 NAME 'associateoid'  "
+                                          + "EQUALITY 2.5.13.2 ORDERING 2.5.13.3 SUBSTR 2.5.13.4 "
+                                          + "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 USAGE userApplications "
+                                          + "X-APPROX '1.3.6.1.4.1.26027.1.4.1' )", false)
+                .toSchema();
+
+        Syntax dnSyntax = schema.getAttributeType("distinguishedName").getSyntax();
+        assertThat(dnSyntax).isSameAs(schema.getSyntax("1.3.6.1.4.1.1466.115.121.1.12"));
+
+        LocalizableMessageBuilder invalidReason = new LocalizableMessageBuilder();
+        boolean isValid = dnSyntax.valueIsAcceptable(ByteString.valueOfUtf8("associateoid=test"), invalidReason);
+        assertThat(isValid)
+                .as("Value should have been valid, but it is not: " + invalidReason.toString())
+                .isTrue();
+    }
 }

--
Gitblit v1.10.0