From aaf3145bd0dfdac3aaac4ffa1dc2d4446c1cfff3 Mon Sep 17 00:00:00 2001
From: Jean-Noël Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Tue, 02 Feb 2016 08:30:45 +0000
Subject: [PATCH] When removing enum syntaxes, also remove the corresponding ordering matching rule.

---
 opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldap/schema/SchemaBuilder.java         |   42 +++++++++++----------
 opendj-sdk/opendj-core/src/test/java/org/forgerock/opendj/ldap/schema/SchemaBuilderTestCase.java |   24 ++++++++++++
 2 files changed, 46 insertions(+), 20 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 bb27db7..8f20112 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
@@ -71,11 +71,11 @@
 import org.forgerock.opendj.ldap.requests.SearchRequest;
 import org.forgerock.opendj.ldap.responses.SearchResultEntry;
 import org.forgerock.opendj.ldap.schema.DITContentRule.Builder;
-import org.forgerock.util.Options;
-import org.forgerock.util.Reject;
 import org.forgerock.util.AsyncFunction;
 import org.forgerock.util.Function;
 import org.forgerock.util.Option;
+import org.forgerock.util.Options;
+import org.forgerock.util.Reject;
 import org.forgerock.util.promise.Promise;
 
 import com.forgerock.opendj.util.StaticUtils;
@@ -625,20 +625,26 @@
     public SchemaBuilder addEnumerationSyntax(final String oid, final String description,
             final boolean overwrite, final String... enumerations) {
         Reject.ifNull((Object) enumerations);
-
         lazyInitBuilder();
 
-        final EnumSyntaxImpl enumImpl = new EnumSyntaxImpl(oid, Arrays.asList(enumerations));
-
+        final List<String> enumEntries = Arrays.asList(enumerations);
         final Syntax.Builder syntaxBuilder = buildSyntax(oid).description(description)
-                .extraProperties(Collections.singletonMap("X-ENUM", Arrays.asList(enumerations)))
-                .implementation(enumImpl);
+            .extraProperties(Collections.singletonMap("X-ENUM", enumEntries));
+        addEnumerationSyntax0(syntaxBuilder, oid, enumEntries, overwrite);
+        return this;
+    }
 
-        syntaxBuilder.addToSchema(overwrite);
+    private void addEnumerationSyntax0(final Syntax.Builder syntaxBuilder,
+            final String oid, final List<String> enumEntries, final boolean overwrite) {
+        final EnumSyntaxImpl enumImpl = new EnumSyntaxImpl(oid, enumEntries);
+
+        syntaxBuilder
+                .implementation(enumImpl)
+                .addToSchema(overwrite);
 
         try {
             buildMatchingRule(enumImpl.getOrderingMatchingRule())
-                    .names(OMR_GENERIC_ENUM_NAME + oid)
+                    .names(OMR_GENERIC_ENUM_NAME + "." + oid)
                     .syntaxOID(oid)
                     .extraProperties(CoreSchemaImpl.OPENDS_ORIGIN)
                     .implementation(new EnumOrderingMatchingRule(enumImpl))
@@ -646,7 +652,6 @@
         } catch (final ConflictingSchemaElementException e) {
             removeSyntax(oid);
         }
-        return this;
     }
 
     /**
@@ -1849,16 +1854,7 @@
             // See if it is a enum syntax
             for (final Map.Entry<String, List<String>> property : syntaxBuilder.getExtraProperties().entrySet()) {
                 if ("x-enum".equalsIgnoreCase(property.getKey())) {
-                    final EnumSyntaxImpl enumImpl = new EnumSyntaxImpl(oid, property.getValue());
-                    syntaxBuilder.implementation(enumImpl);
-                    syntaxBuilder.addToSchema(overwrite);
-
-                    buildMatchingRule(enumImpl.getOrderingMatchingRule())
-                        .names(OMR_GENERIC_ENUM_NAME + "." + oid)
-                        .syntaxOID(oid)
-                        .extraProperties(CoreSchemaImpl.OPENDS_ORIGIN)
-                        .implementation(new EnumOrderingMatchingRule(enumImpl))
-                        .addToSchemaOverwrite();
+                    addEnumerationSyntax0(syntaxBuilder, oid, property.getValue(), overwrite);
                     return this;
                 }
             }
@@ -2597,6 +2593,12 @@
     }
 
     private void removeSyntax(final Syntax syntax) {
+        for (Map.Entry<String, List<String>> property : syntax.getExtraProperties().entrySet()) {
+            if ("x-enum".equalsIgnoreCase(property.getKey())) {
+                removeMatchingRule(OMR_GENERIC_ENUM_NAME + "." + syntax.getOID());
+                break;
+            }
+        }
         numericOID2Syntaxes.remove(syntax.getOID());
     }
 
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 a8e00f8..362bd93 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
@@ -2123,4 +2123,28 @@
                 schema.getMatchingRule("9.9.9").normalizeAttributeValue(ByteString.valueOfUtf8("test")))
                 .isEqualTo(ByteString.valueOfUtf8("test"));
     }
+
+    @Test
+    public void enumSyntaxAddThenRemove() {
+        final Schema coreSchema = Schema.getCoreSchema();
+        final Schema schemaWithEnum = new SchemaBuilder(coreSchema)
+            .addSyntax("( 3.3.3  DESC 'Day Of The Week'  "
+                    + "X-ENUM  ( 'monday' 'tuesday'   'wednesday'  'thursday'  'friday'  'saturday' 'sunday') )",
+                    false)
+            .toSchema();
+
+        assertThat(schemaWithEnum.getWarnings()).isEmpty();
+        assertThat(schemaWithEnum.getMatchingRules())
+            .as("Expected an enum ordering matching rule to be added for the enum syntax")
+            .hasSize(coreSchema.getMatchingRules().size() + 1);
+
+        final SchemaBuilder builder = new SchemaBuilder(schemaWithEnum);
+        assertThat(builder.removeSyntax("3.3.3")).isTrue();
+        final Schema schemaNoEnum = builder.toSchema();
+
+        assertThat(schemaNoEnum.getWarnings()).isEmpty();
+        assertThat(schemaNoEnum.getMatchingRules())
+            .as("Expected the enum ordering matching rule to be removed at the same time as the enum syntax")
+            .hasSize(coreSchema.getMatchingRules().size());
+    }
 }

--
Gitblit v1.10.0