From d9402bf4faff7b3727d2a500701a05c6d8d32080 Mon Sep 17 00:00:00 2001
From: sin <sin@localhost>
Date: Thu, 08 Mar 2007 22:30:14 +0000
Subject: [PATCH] issue 756:Import LDIF should allow groups of attributes to be included or excluded
---
opends/tests/unit-tests-testng/src/server/org/opends/server/tools/ImportLDIFTestCase.java | 483 +++++++++++++++++++++++++++++++++++++++++++
opends/src/server/org/opends/server/types/LDIFImportConfig.java | 113 ++++++++++
opends/src/server/org/opends/server/tools/ImportLDIF.java | 49 +++
3 files changed, 636 insertions(+), 9 deletions(-)
diff --git a/opends/src/server/org/opends/server/tools/ImportLDIF.java b/opends/src/server/org/opends/server/tools/ImportLDIF.java
index 2d334f2..eaba2bf 100644
--- a/opends/src/server/org/opends/server/tools/ImportLDIF.java
+++ b/opends/src/server/org/opends/server/tools/ImportLDIF.java
@@ -555,6 +555,8 @@
// See if there were any user-defined sets of include/exclude attributes or
// filters. If so, then process them.
HashSet<AttributeType> excludeAttributes;
+ boolean excludeAllUserAttributes = false;
+ boolean excludeAllOperationalAttributes = false;
if (excludeAttributeStrings == null)
{
excludeAttributes = null;
@@ -565,17 +567,30 @@
for (String attrName : excludeAttributeStrings.getValues())
{
String lowerName = attrName.toLowerCase();
- AttributeType attrType = DirectoryServer.getAttributeType(lowerName);
- if (attrType == null)
+ if(lowerName.equals("*"))
{
- attrType = DirectoryServer.getDefaultAttributeType(attrName);
+ excludeAllUserAttributes = true;
}
+ else if(lowerName.equals("+"))
+ {
+ excludeAllOperationalAttributes = true;
+ }
+ else
+ {
+ AttributeType attrType = DirectoryServer.getAttributeType(lowerName);
+ if (attrType == null)
+ {
+ attrType = DirectoryServer.getDefaultAttributeType(attrName);
+ }
- excludeAttributes.add(attrType);
+ excludeAttributes.add(attrType);
+ }
}
}
HashSet<AttributeType> includeAttributes;
+ boolean includeAllUserAttributes = false;
+ boolean includeAllOperationalAttributes = false;
if (includeAttributeStrings == null)
{
includeAttributes = null;
@@ -586,13 +601,24 @@
for (String attrName : includeAttributeStrings.getValues())
{
String lowerName = attrName.toLowerCase();
- AttributeType attrType = DirectoryServer.getAttributeType(lowerName);
- if (attrType == null)
+ if(lowerName.equals("*"))
{
- attrType = DirectoryServer.getDefaultAttributeType(attrName);
+ includeAllUserAttributes = true;
}
+ else if(lowerName.equals("+"))
+ {
+ includeAllOperationalAttributes = true;
+ }
+ else
+ {
+ AttributeType attrType = DirectoryServer.getAttributeType(lowerName);
+ if (attrType == null)
+ {
+ attrType = DirectoryServer.getDefaultAttributeType(attrName);
+ }
- includeAttributes.add(attrType);
+ includeAttributes.add(attrType);
+ }
}
}
@@ -889,6 +915,13 @@
importConfig.setIncludeFilters(includeFilters);
importConfig.setValidateSchema(!skipSchemaValidation.isPresent());
importConfig.setBufferSize(LDIF_BUFFER_SIZE);
+ importConfig.setExcludeAllUserAttributes(
+ excludeAllUserAttributes);
+ importConfig.setExcludeAllOperationalAttributes(
+ excludeAllOperationalAttributes);
+ importConfig.setIncludeAllOpAttributes(
+ includeAllOperationalAttributes);
+ importConfig.setIncludeAllUserAttributes(includeAllUserAttributes);
// FIXME -- Should this be conditional?
importConfig.setInvokeImportPlugins(true);
diff --git a/opends/src/server/org/opends/server/types/LDIFImportConfig.java b/opends/src/server/org/opends/server/types/LDIFImportConfig.java
index 7caf612..d222111 100644
--- a/opends/src/server/org/opends/server/types/LDIFImportConfig.java
+++ b/opends/src/server/org/opends/server/types/LDIFImportConfig.java
@@ -136,6 +136,20 @@
// The set of attribute types that should be included in the import.
private Set<AttributeType> includeAttributes;
+//Indicates whether all the user attributes should be included.
+ private boolean includeAllUserAttrs;
+
+ //Indicates whether all the operational attributes should be
+ // included.
+ private boolean includeAllOpAttrs;
+
+ //Indicates whether all the user attributes should be excluded.
+ private boolean excludeAllUserAttrs;
+
+ //Indicates whether all the operational attributes should be
+ // excluded.
+ private boolean excludeAllOpAttrs;
+
// The MakeLDIF template file that should be used to generate
// entries (instead of having them read from a file).
private TemplateFile templateFile;
@@ -172,6 +186,11 @@
rejectWriter = null;
excludeAttributes = new HashSet<AttributeType>();
includeAttributes = new HashSet<AttributeType>();
+ includeAllUserAttrs = false;
+ includeAllOpAttrs = false;
+ excludeAllUserAttrs = false;
+ excludeAllOpAttrs = false;
+
}
@@ -206,6 +225,11 @@
rejectWriter = null;
excludeAttributes = new HashSet<AttributeType>();
includeAttributes = new HashSet<AttributeType>();
+ includeAllUserAttrs = false;
+ includeAllOpAttrs = false;
+ excludeAllUserAttrs = false;
+ excludeAllOpAttrs = false;
+
}
@@ -238,6 +262,11 @@
rejectWriter = null;
excludeAttributes = new HashSet<AttributeType>();
includeAttributes = new HashSet<AttributeType>();
+ includeAllUserAttrs = false;
+ includeAllOpAttrs = false;
+ excludeAllUserAttrs = false;
+ excludeAllOpAttrs = false;
+
}
/**
@@ -268,6 +297,11 @@
rejectWriter = null;
excludeAttributes = new HashSet<AttributeType>();
includeAttributes = new HashSet<AttributeType>();
+ includeAllUserAttrs = false;
+ includeAllOpAttrs = false;
+ excludeAllUserAttrs = false;
+ excludeAllOpAttrs = false;
+
}
/**
@@ -921,11 +955,30 @@
return false;
}
+ if(excludeAllOpAttrs && attributeType.isOperational() ||
+ excludeAllUserAttrs && !attributeType.isOperational())
+ {
+ return false;
+ }
+
+ if(includeAllUserAttrs && !attributeType.isOperational() ||
+ includeAllOpAttrs && attributeType.isOperational())
+ {
+ return true;
+ }
+
if (! includeAttributes.isEmpty())
{
return includeAttributes.contains(attributeType);
}
-
+ else
+ {
+ if(includeAllUserAttrs && attributeType.isOperational() ||
+ includeAllOpAttrs && !attributeType.isOperational())
+ {
+ return false;
+ }
+ }
return true;
}
@@ -1079,6 +1132,64 @@
+ /**
+ * Specifies whether all the user attributes should be excluded.
+ *
+ * @param excludeAllUserAttrs Specifies all user attributes to
+ * be excluded.
+ */
+ public void setExcludeAllUserAttributes(boolean excludeAllUserAttrs)
+ {
+ this.excludeAllUserAttrs = excludeAllUserAttrs;
+ }
+
+
+
+ /**
+ * Specifies whether all the operational attributes should be
+ * excluded.
+ *
+ * @param excludeAllOpAttrs Specifies whether all the
+ * operational attributes
+ * should be excluded.
+ */
+ public void setExcludeAllOperationalAttributes(
+ boolean excludeAllOpAttrs)
+ {
+ this.excludeAllOpAttrs = excludeAllOpAttrs;
+ }
+
+
+
+ /**
+ * Specifies whether all the operational attributes should be
+ * included.
+ *
+ * @param includeAllOpAttrs Specifies whether all
+ * the operation attributes should be included.
+ *
+ */
+ public void setIncludeAllOpAttributes(boolean includeAllOpAttrs)
+ {
+ this.includeAllOpAttrs = includeAllOpAttrs;
+ }
+
+
+
+ /**
+ * Specifies whether all the user attributes should be included.
+ *
+ * @param includeAllUserAttrs Specifies whether all the
+ * user attributes should be
+ * included.
+ */
+ public void setIncludeAllUserAttributes(boolean includeAllUserAttrs)
+ {
+ this.includeAllUserAttrs = includeAllUserAttrs;
+ }
+
+
+
/**
* Closes any resources that this import config might have open.
*/
diff --git a/opends/tests/unit-tests-testng/src/server/org/opends/server/tools/ImportLDIFTestCase.java b/opends/tests/unit-tests-testng/src/server/org/opends/server/tools/ImportLDIFTestCase.java
new file mode 100644
index 0000000..7779193
--- /dev/null
+++ b/opends/tests/unit-tests-testng/src/server/org/opends/server/tools/ImportLDIFTestCase.java
@@ -0,0 +1,483 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License, Version 1.0 only
+ * (the "License"). You may not use this file except in compliance
+ * with the License.
+ *
+ * You can obtain a copy of the license at
+ * trunk/opends/resource/legal-notices/OpenDS.LICENSE
+ * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at
+ * trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable,
+ * add the following below this CDDL HEADER, with the fields enclosed
+ * by brackets "[]" replaced with your own identifying information:
+ * Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ *
+ *
+ * Portions Copyright 2006-2007 Sun Microsystems, Inc.
+ */
+package org.opends.server.tools;
+
+import static org.opends.server.config.ConfigConstants.DN_BACKEND_BASE;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+
+import org.opends.server.TestCaseUtils;
+import org.opends.server.config.ConfigEntry;
+import org.opends.server.util.Base64;
+import org.opends.server.api.Backend;
+import org.opends.server.backends.jeb.BackendImpl;
+import org.opends.server.core.DirectoryServer;
+import org.opends.server.core.LockFileManager;
+import static org.testng.Assert.*;
+import java.io.*;
+import java.util.*;
+
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+
+import org.opends.server.tools.ToolsTestCase;
+import org.opends.server.tasks.TaskUtils;
+import org.opends.server.tools.ImportLDIF;
+import org.opends.server.types.ResultCode;
+import org.opends.server.types.Entry;
+import org.opends.server.types.DN;
+import org.opends.server.types.Attribute;
+import static org.testng.Assert.*;
+
+
+
+public class ImportLDIFTestCase extends ToolsTestCase {
+
+ private File tempDir;
+ private String ldifFilePath;
+ String configFilePath ;
+ private BackendImpl backend;
+ private String homeDirName;
+ private ConfigEntry backendConfigEntry;
+ private String beID;
+
+ /**
+ * Ensures that the ldif file is created with the entry.
+ *
+ * @throws Exception If an unexpected problem occurs.
+ */
+ @BeforeClass
+ public void setUp() throws Exception
+ {
+ TestCaseUtils.startServer();
+ beID = "userRoot";
+ configFilePath = DirectoryServer.getInstance().getConfigFile();
+ backend = (BackendImpl)DirectoryServer.getBackend(beID);
+ backendConfigEntry = TaskUtils.getConfigEntry(backend);
+ TaskUtils.setBackendEnabled(backendConfigEntry, false);
+
+ String entry =
+ "dn: dc=example,dc=com\n" +
+ "objectclass: domain\n" +
+ "objectclass: top\n" +
+ "dc: example\n\n" +
+ "dn: uid=user.0,dc=example,dc=com\n" +
+ "objectClass: person\n" +
+ "objectClass: inetorgperson\n" +
+ "objectClass: organizationalPerson\n" +
+ "objectClass: top\n" +
+ "givenName: Aaccf\n" +
+ "sn: Amar\n" +
+ "cn: Aaccf Amar\n" +
+ "employeeNumber: 0\n" +
+ "uid: user.0\n" +
+ "mail: user.0@example.com\n" +
+ "userPassword: password\n" +
+ "telephoneNumber: 380-535-2354\n" +
+ "description: This is the description for Aaccf Amar\n" +
+ "creatorsName: Import\n" +
+ "modifiersName: Import\n";
+
+ tempDir = TestCaseUtils.createTemporaryDirectory("importLDIFtest");
+ homeDirName = tempDir.getAbsolutePath();
+ ldifFilePath = homeDirName + File.separator + "entries.ldif";
+ FileOutputStream ldifFile = new FileOutputStream(ldifFilePath);
+ PrintStream writer = new PrintStream(ldifFile);
+ writer.println(entry);
+ writer.close();
+ ldifFile.close();
+ }
+
+
+
+ /**
+ * Tests an import of LDIF with only the operational attributes
+ * included.
+ *
+ * @throws Exception If an unexpected problem occurs.
+ */
+ @Test
+ public void testImportIncludeOnlyOperational() throws Exception
+ {
+ File reject = File.createTempFile("reject", ".ldif");
+ String rejectFilePath = reject.getAbsolutePath();
+
+ String[] args =
+ {
+ "-f",configFilePath,
+ "-l",ldifFilePath,
+ "-R", rejectFilePath,
+ "-n", beID,
+ "-i", "+"
+ };
+ assertEquals(ImportLDIF.mainImportLDIF(args,false), 0);
+ //Expecting a non-empty reject file.
+ assertRejectedFile(reject,false);
+ }
+
+
+
+ /**
+ * Tests an import of LDIF with only thel user attributes
+ * included.
+ *
+ * @throws Exception If an unexpected problem occurs.
+ */
+ @Test
+ public void testImportIncludeOnlyUser() throws Exception
+ {
+ File reject = File.createTempFile("reject", ".ldif");
+ String rejectFilePath = reject.getAbsolutePath();
+
+ String[] args =
+ {
+ "-f",configFilePath,
+ "-l",ldifFilePath,
+ "-R", rejectFilePath,
+ "-n", beID,
+ "-i", "*"
+ };
+ assertEquals(ImportLDIF.mainImportLDIF(args,false), 0);
+ //Expecting an empty reject file.
+ assertRejectedFile(reject,true);
+
+ Attribute[] opAttr =
+ {
+ new Attribute ("creatorsname", "Import") ,
+ new Attribute("modifiersname","Import")
+ } ;
+ //operational attributes shouldn't be present.
+ assertEntry(opAttr,false);
+ }
+
+
+
+ /**
+ * Tests a simple Import LDIF with none of the attributes
+ * excluded or included. It is expected to import the entry(ies)
+ * with all the attributes in the ldif file.
+ *
+ * @throws Exception If an unexpected problem occurs.
+ */
+ @Test
+ public void testImportDefault() throws Exception
+ {
+
+ File reject = File.createTempFile("reject", ".ldif");
+ String rejectFilePath = reject.getAbsolutePath();
+ String[] args =
+ {
+ "-f", DirectoryServer.getInstance().getConfigFile(),
+ "-l",ldifFilePath,
+ "-n", beID,
+ "-R", rejectFilePath
+ };
+ assertEquals(ImportLDIF.mainImportLDIF(args,false),0);
+ //Reject file should be empty.
+ assertRejectedFile(reject,true);
+ //check the presence of some random attributes.
+ Attribute[] attr =
+ {
+ new Attribute ("description",
+ "This is the description for Aaccf Amar"),
+ new Attribute("mail","user.0@example.com"),
+ new Attribute ("creatorsname", "Import") ,
+ new Attribute("modifiersname","Import")
+ } ;
+ assertEntry(attr,true);
+ }
+
+
+
+ /**
+ * Tests an import of LDIF with all the user attributes included
+ * but "description"
+ *
+ * @throws Exception If an unexpected problem occurs.
+ */
+ @Test
+ public void testImportLDIFIncludeUserExcludeDescription()
+ throws Exception
+ {
+ File reject = File.createTempFile("reject", ".ldif");
+ String rejectFilePath = reject.getAbsolutePath();
+
+ String[] args =
+ {
+ "-f", DirectoryServer.getInstance().getConfigFile(),
+ "-l",ldifFilePath,
+ "-n", beID,
+ "-R",rejectFilePath,
+ "-i","*",
+ "-e", "description"
+ };
+
+ assertEquals(ImportLDIF.mainImportLDIF(args,false), 0);
+ assertRejectedFile(reject,true);
+ Attribute[] attr =
+ {
+ new Attribute ("description",
+ "This is the description for Aaccf Amar")
+ };
+ assertEntry(attr,false);
+ }
+
+
+
+ /**
+ * Tests an import of LDIF with all user attributes excluded option.
+ *
+ * @throws Exception If an unexpected problem occurs.
+ */
+ @Test
+ public void testImportLDIFExcludeUser() throws Exception
+ {
+ File reject = File.createTempFile("reject", ".ldif");
+ String rejectFilePath = reject.getAbsolutePath();
+ String[] args =
+ {
+ "-f", DirectoryServer.getInstance().getConfigFile(),
+ "-l",ldifFilePath,
+ "-n", beID,
+ "-R",rejectFilePath,
+ "-e", "*"
+ };
+
+ assertEquals(ImportLDIF.mainImportLDIF(args,false), 0);
+ assertRejectedFile(reject,false);
+ }
+
+
+
+ /**
+ * Tests an import of LDIF with all the operational attributes excluded option.
+ *
+ * @throws Exception If an unexpected problem occurs.
+ */
+ @Test
+ public void testImportLDIFExcludeOperational() throws Exception
+ {
+
+ File reject = File.createTempFile("reject", ".ldif");
+ String rejectFilePath = reject.getAbsolutePath();
+ String[] args =
+ {
+ "-f", DirectoryServer.getInstance().getConfigFile(),
+ "-l",ldifFilePath,
+ "-n", beID,
+ "-R",rejectFilePath,
+ "-e", "+"
+ };
+ assertEquals(ImportLDIF.mainImportLDIF(args,false), 0);
+ assertRejectedFile(reject,true);
+ Attribute[] attrs = {
+ new Attribute ("creatorsname", "Import") ,
+ new Attribute("modifiersname","Import")
+ };
+ assertEntry(attrs,false);
+ }
+
+
+
+ /**
+ * Tests an import of LDIF with all user attributes and
+ * one operational attribute included..
+ *
+ * @throws Exception If an unexpected problem occurs.
+ */
+ @Test
+ public void testImportLDIFUserAndOperational() throws Exception
+ {
+
+ File reject = File.createTempFile("reject", ".ldif");
+ String rejectFilePath = reject.getAbsolutePath();
+ String[] args =
+ {
+ "-f", DirectoryServer.getInstance().getConfigFile(),
+ "-l",ldifFilePath,
+ "-n", beID,
+ "-R",rejectFilePath,
+ "-i", "*",
+ "-i","creatorsname"
+ };
+ assertEquals(ImportLDIF.mainImportLDIF(args,false), 0);
+ assertRejectedFile(reject,true);
+ Attribute[] attrs = {
+ new Attribute ("creatorsname", "Import")
+ };
+ assertEntry(attrs,true);
+ }
+
+
+
+ /**
+ * Tests an import of LDIF with select user and operational
+ * attributes included..
+ *
+ * @throws Exception If an unexpected problem occurs.
+ */
+ @Test
+ public void testImportLDIFSelectiveIncludeAttributes() throws Exception
+ {
+
+ File reject = File.createTempFile("reject", ".ldif");
+ String rejectFilePath = reject.getAbsolutePath();
+ String[] args =
+ {
+ "-f", DirectoryServer.getInstance().getConfigFile(),
+ "-l",ldifFilePath,
+ "-n", beID,
+ "-R",rejectFilePath,
+ "-i", "cn",
+ "-i", "uid",
+ "-i", "dc",
+ "-i", "sn",
+ "-i","creatorsname"
+ };
+ assertEquals(ImportLDIF.mainImportLDIF(args,false), 0);
+ assertRejectedFile(reject,true);
+ Attribute[] attrsPr = {
+ new Attribute ("creatorsname", "Import")
+ };
+ assertEntry(attrsPr,true);
+ Attribute[] attrsAb = {
+ new Attribute ("givenname", "Aaccf"),
+ new Attribute("employeenumber","0")
+ };
+ assertEntry(attrsAb,false);
+ }
+
+
+
+ /**
+ * Tests an import of LDIF with select user and operational
+ * attributes encluded..
+ *
+ * @throws Exception If an unexpected problem occurs.
+ */
+ @Test
+ public void testImportLDIFSelectiveExcludeAttributes() throws Exception
+ {
+
+ File reject = File.createTempFile("reject", ".ldif");
+ String rejectFilePath = reject.getAbsolutePath();
+ String[] args =
+ {
+ "-f", DirectoryServer.getInstance().getConfigFile(),
+ "-l",ldifFilePath,
+ "-n", beID,
+ "-R",rejectFilePath,
+ "-e", "givenName",
+ "-e","creatorsname"
+ };
+ assertEquals(ImportLDIF.mainImportLDIF(args,false), 0);
+ assertRejectedFile(reject,true);
+ Attribute[] attrsPr = {
+ new Attribute ("modifiersname", "Import"),
+ new Attribute("employeenumber","0")
+ };
+ assertEntry(attrsPr,true);
+ Attribute[] attrsAb = {
+ new Attribute ("creatorsname", "Import"),
+ new Attribute("givenname","Aaccf")
+ };
+ assertEntry(attrsAb,false);
+ }
+
+
+
+ /**
+ * Utility method which is called by the testcase for asserting
+ * the rejected file.
+ *
+ * @param reject The file to be asserted
+ * @param shouldBeEmpty whether the file should be empty.
+ */
+ private void assertRejectedFile(File reject, boolean shouldBeEmpty)
+ {
+ if(shouldBeEmpty)
+ {
+ assertEquals(reject.length(),0);
+ }
+ else
+ {
+ assertFalse(reject.length()==0);
+ }
+ reject.delete();
+ }
+
+
+ /**
+ * Utility method which is called by the testcase for asserting
+ * the imported entry.
+ *
+ * @param attrs The array of attributes to be asserted.
+ * @param assertType the boolean flag for assert type.
+ * @throws Exception If an unexpected problem occurs.
+ */
+ private void assertEntry(Attribute[] attrs,boolean assertType) throws Exception
+ {
+ if(attrs != null && attrs.length > 0)
+ {
+ TaskUtils.setBackendEnabled(backendConfigEntry, true);
+ Entry entry = DirectoryServer.getEntry(DN.decode(
+ " uid=user.0,dc=example,dc=com"));
+ TaskUtils.setBackendEnabled(backendConfigEntry,false);
+ assertNotNull(entry);
+ List<Attribute> list = entry.getAttributes();
+ for(int i=0;i<attrs.length;i++)
+ {
+ if(assertType)
+ {
+ assertTrue(list.contains(attrs[i]));
+ }
+ else
+ {
+ assertFalse(list.contains(attrs[i]));
+ }
+ }
+ }
+ }
+
+
+
+ /**
+ * Clean up method.
+ *
+ * @throws Exception If an unexpected problem occurs.
+ */
+ @AfterClass
+ public void cleanUp() throws Exception
+ {
+ //reinstate the backend.
+ TaskUtils.setBackendEnabled(backendConfigEntry, true);
+ TestCaseUtils.deleteDirectory(tempDir);
+ }
+}
--
Gitblit v1.10.0