From 3853a10b6b9c146804dbc9ba390cc39965c0b331 Mon Sep 17 00:00:00 2001
From: Jean-Noël Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Fri, 11 Mar 2016 12:13:01 +0000
Subject: [PATCH] OPENDJ-2703 Replication: when adding a new objectclass, 99-user.ldif files differ

---
 opendj-server-legacy/src/main/java/org/opends/server/backends/SchemaBackend.java         |   82 ++++++++++----------------
 opendj-server-legacy/src/test/java/org/opends/server/backends/SchemaBackendTestCase.java |   47 +++++++++++----
 2 files changed, 67 insertions(+), 62 deletions(-)

diff --git a/opendj-server-legacy/src/main/java/org/opends/server/backends/SchemaBackend.java b/opendj-server-legacy/src/main/java/org/opends/server/backends/SchemaBackend.java
index 8655edb..226eb47 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/backends/SchemaBackend.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/backends/SchemaBackend.java
@@ -22,7 +22,6 @@
 import static org.opends.messages.SchemaMessages.*;
 import static org.opends.server.config.ConfigConstants.*;
 import static org.opends.server.core.DirectoryServer.*;
-import static org.opends.server.schema.SchemaConstants.*;
 import static org.opends.server.types.CommonSchemaElements.*;
 import static org.opends.server.util.CollectionUtils.*;
 import static org.opends.server.util.ServerConstants.*;
@@ -65,7 +64,6 @@
 import org.forgerock.opendj.ldap.schema.CoreSchema;
 import org.forgerock.opendj.ldap.schema.MatchingRule;
 import org.forgerock.opendj.ldap.schema.ObjectClassType;
-import org.forgerock.opendj.ldap.schema.Syntax;
 import org.opends.server.admin.server.ConfigurationChangeListener;
 import org.opends.server.admin.std.server.SchemaBackendCfg;
 import org.opends.server.api.AlertGenerator;
@@ -3533,11 +3531,6 @@
     TreeSet<String> modifiedSchemaFiles = new TreeSet<>();
 
     // Get the attributeTypes attribute from the entry.
-    Syntax attrTypeSyntax = schema.getSyntax(SYNTAX_ATTRIBUTE_TYPE_OID);
-    if (attrTypeSyntax == null)
-    {
-      attrTypeSyntax = CoreSchema.getAttributeTypeDescriptionSyntax();
-    }
     AttributeType attributeAttrType = CoreSchema.getAttributeTypesAttributeType();
 
     // loop on the attribute types in the entry just received
@@ -3608,59 +3601,48 @@
 
     // loop on the objectClasses from the entry, search if they are
     // already in the current schema, add them if not.
-    Syntax ocSyntax = schema.getSyntax(SYNTAX_OBJECTCLASS_OID);
-    if (ocSyntax == null)
-    {
-      ocSyntax = CoreSchema.getObjectClassDescriptionSyntax();
-    }
-    AttributeType objectclassAttrType = CoreSchema.getObjectClassAttributeType();
+    AttributeType objectclassAttrType = CoreSchema.getObjectClassesAttributeType();
 
     oidList.clear();
-    List<Attribute> ocList = newSchemaEntry.getAttribute(objectclassAttrType);
-    if (ocList != null && !ocList.isEmpty())
+    for (Attribute a : newSchemaEntry.getAttribute(objectclassAttrType))
     {
-      for (Attribute a : ocList)
+      for (ByteString v : a)
       {
-        for (ByteString v : a)
+        // It IS important here to allow the unknown elements that could
+        // appear in the new config schema.
+        ObjectClass newObjectClass = ObjectClassSyntax.decodeObjectClass(v, newSchema, true);
+        String schemaFile = getSchemaFile(newObjectClass);
+        if (CONFIG_SCHEMA_ELEMENTS_FILE.equals(schemaFile))
         {
-          // It IS important here to allow the unknown elements that could
-          // appear in the new config schema.
-          ObjectClass newObjectClass = ObjectClassSyntax.decodeObjectClass(v, newSchema, true);
-          String schemaFile = getSchemaFile(newObjectClass);
-          if (CONFIG_SCHEMA_ELEMENTS_FILE.equals(schemaFile))
-          {
-            // Don't import the file containing the definitions of the
-            // Schema elements used for configuration because these
-            // definitions may vary between versions of OpenDJ.
-            continue;
-          }
+          // Don't import the file containing the definitions of the
+          // Schema elements used for configuration because these
+          // definitions may vary between versions of OpenDJ.
+          continue;
+        }
 
-          // Now we know we are not in the config schema, let's check
-          // the unknown elements ... sadly but simply by redoing the
-          // whole decoding.
-          newObjectClass = ObjectClassSyntax.decodeObjectClass(v, newSchema, false);
-          oidList.add(newObjectClass.getOID());
-          try
+        // Now we know we are not in the config schema, let's check
+        // the unknown elements ... sadly but simply by redoing the
+        // whole decoding.
+        newObjectClass = ObjectClassSyntax.decodeObjectClass(v, newSchema, false);
+        oidList.add(newObjectClass.getOID());
+        try
+        {
+          // Register this ObjectClass in the new schema
+          // unless it is already defined with the same syntax.
+          ObjectClass oldObjectClass = schema.getObjectClass(newObjectClass.getOID());
+          if (oldObjectClass == null || !oldObjectClass.toString().equals(newObjectClass.toString()))
           {
-            // Register this ObjectClass in the new schema
-            // unless it is already defined with the same syntax.
-            ObjectClass oldObjectClass =
-              schema.getObjectClass(newObjectClass.getOID());
-            if (oldObjectClass == null ||
-                !oldObjectClass.toString().equals(newObjectClass.toString()))
+            newSchema.registerObjectClass(newObjectClass, true);
+
+            if (schemaFile != null)
             {
-              newSchema.registerObjectClass(newObjectClass, true);
-
-              if (schemaFile != null)
-              {
-                modifiedSchemaFiles.add(schemaFile);
-              }
+              modifiedSchemaFiles.add(schemaFile);
             }
           }
-          catch (Exception e)
-          {
-            logger.info(NOTE_SCHEMA_IMPORT_FAILED, newObjectClass, e.getMessage());
-          }
+        }
+        catch (Exception e)
+        {
+          logger.info(NOTE_SCHEMA_IMPORT_FAILED, newObjectClass, e.getMessage());
         }
       }
     }
diff --git a/opendj-server-legacy/src/test/java/org/opends/server/backends/SchemaBackendTestCase.java b/opendj-server-legacy/src/test/java/org/opends/server/backends/SchemaBackendTestCase.java
index 4918bae..f88b042 100644
--- a/opendj-server-legacy/src/test/java/org/opends/server/backends/SchemaBackendTestCase.java
+++ b/opendj-server-legacy/src/test/java/org/opends/server/backends/SchemaBackendTestCase.java
@@ -20,6 +20,7 @@
 import static org.opends.server.core.DirectoryServer.*;
 import static org.opends.server.protocols.internal.InternalClientConnection.*;
 import static org.opends.server.protocols.internal.Requests.*;
+import static org.opends.server.types.ExistingFileBehavior.*;
 import static org.opends.server.util.StaticUtils.*;
 import static org.testng.Assert.*;
 
@@ -32,6 +33,7 @@
 
 import org.forgerock.opendj.config.server.ConfigException;
 import org.forgerock.opendj.ldap.ByteString;
+import org.forgerock.opendj.ldap.DN;
 import org.forgerock.opendj.ldap.ResultCode;
 import org.forgerock.opendj.ldap.SearchScope;
 import org.forgerock.opendj.ldap.schema.AttributeType;
@@ -45,19 +47,19 @@
 import org.opends.server.core.DirectoryServer;
 import org.opends.server.core.ModifyDNOperationBasis;
 import org.opends.server.core.SchemaConfigManager;
+import org.opends.server.core.ServerContext;
 import org.opends.server.protocols.internal.InternalClientConnection;
 import org.opends.server.protocols.internal.InternalSearchOperation;
 import org.opends.server.protocols.internal.SearchRequest;
 import org.opends.server.schema.SchemaConstants;
 import org.opends.server.tools.LDAPModify;
 import org.opends.server.types.DITContentRule;
-import org.forgerock.opendj.ldap.DN;
 import org.opends.server.types.DirectoryException;
 import org.opends.server.types.Entry;
-import org.opends.server.types.ExistingFileBehavior;
 import org.opends.server.types.InitializationException;
 import org.opends.server.types.LDIFExportConfig;
 import org.opends.server.types.LDIFImportConfig;
+import org.opends.server.types.LDIFImportResult;
 import org.opends.server.types.MatchingRuleUse;
 import org.opends.server.types.ObjectClass;
 import org.opends.server.types.SearchFilter;
@@ -4385,10 +4387,8 @@
   {
     File tempFile = File.createTempFile("schema", "testExportLDIF");
     tempFile.deleteOnExit();
-    LDIFExportConfig exportConfig =
-         new LDIFExportConfig(tempFile.getAbsolutePath(),
-                              ExistingFileBehavior.OVERWRITE);
 
+    LDIFExportConfig exportConfig = new LDIFExportConfig(tempFile.getAbsolutePath(), OVERWRITE);
     schemaBackend.exportLDIF(exportConfig);
 
     assertTrue(tempFile.exists());
@@ -4401,19 +4401,42 @@
    * @throws  Exception  If an unexpected problem occurs.
    */
   @Test
-  public void testImportLDIF() throws Exception
+  public void testImportLDIFFails() throws Exception
   {
-    File tempFile = File.createTempFile("schema", "testImportLDIF");
+    File tempFile = File.createTempFile("schema", "testImportLDIFFails");
     tempFile.deleteOnExit();
 
-    LDIFExportConfig exportConfig =
-      new LDIFExportConfig(tempFile.getAbsolutePath(),
-                           ExistingFileBehavior.OVERWRITE);
-
+    LDIFExportConfig exportConfig = new LDIFExportConfig(tempFile.getAbsolutePath(), OVERWRITE);
     schemaBackend.exportLDIF(exportConfig);
 
+    ServerContext serverContext = DirectoryServer.getInstance().getServerContext();
     LDIFImportConfig importConfig = new LDIFImportConfig(tempFile.getAbsolutePath());
-    schemaBackend.importLDIF(importConfig, DirectoryServer.getInstance().getServerContext());
+    LDIFImportResult importResult = schemaBackend.importLDIF(importConfig, serverContext);
+    assertEquals(importResult.getEntriesRead(), 1);
+    assertEquals(importResult.getEntriesImported(), 0);
+    assertEquals(importResult.getEntriesRejected(), 1);
+    assertEquals(importResult.getEntriesSkipped(), 0);
+  }
+
+  @Test
+  public void testImportLDIFSuccess() throws Exception
+  {
+    File tempFile = File.createTempFile("schema", "testImportLDIFSucceeds");
+    tempFile.deleteOnExit();
+
+    LDIFExportConfig exportConfig = new LDIFExportConfig(tempFile.getAbsolutePath(), OVERWRITE);
+    schemaBackend.exportLDIF(exportConfig);
+
+    // replication does not validate schema
+    LDIFImportConfig importConfig = new LDIFImportConfig(tempFile.getAbsolutePath());
+    importConfig.setValidateSchema(false);
+
+    ServerContext serverContext = DirectoryServer.getInstance().getServerContext();
+    LDIFImportResult importResult = schemaBackend.importLDIF(importConfig, serverContext);
+    assertEquals(importResult.getEntriesRead(), 1);
+    assertEquals(importResult.getEntriesImported(), 1);
+    assertEquals(importResult.getEntriesRejected(), 0);
+    assertEquals(importResult.getEntriesSkipped(), 0);
   }
 
   /**

--
Gitblit v1.10.0