From 3848fc4749fc7528597fb5a0cbebeb90d3bea97d Mon Sep 17 00:00:00 2001
From: Gaetan Boismal <gaetan.boismal@forgerock.com>
Date: Thu, 10 Mar 2016 15:49:16 +0000
Subject: [PATCH] OPENDJ-2505 Makeldif supports line folding

---
 opendj-sdk/opendj-core/src/main/resources/com/forgerock/opendj/ldap/core.properties              |    5 +
 opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldif/TemplateFile.java                 |   18 ++++--
 opendj-sdk/opendj-ldap-toolkit/src/test/java/com/forgerock/opendj/ldap/tools/MakeLDIFITCase.java |   42 +++++++++++++-
 opendj-sdk/opendj-ldap-toolkit/src/test/resources/valid_test_template.ldif                       |   28 +++++++++
 opendj-sdk/opendj-ldap-toolkit/src/test/resources/invalid_test_template.ldif                     |   29 +++++++++
 opendj-sdk/opendj-ldap-toolkit/src/test/resources/expected_output.ldif                           |   11 +++
 6 files changed, 123 insertions(+), 10 deletions(-)

diff --git a/opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldif/TemplateFile.java b/opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldif/TemplateFile.java
index a8c6887..28113c5 100644
--- a/opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldif/TemplateFile.java
+++ b/opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldif/TemplateFile.java
@@ -12,7 +12,7 @@
  * information: "Portions Copyright [year] [name of copyright owner]".
  *
  * Copyright 2006-2009 Sun Microsystems, Inc.
- * Portions Copyright 2013-2015 ForgeRock AS.
+ * Portions Copyright 2013-2016 ForgeRock AS.
  */
 package org.forgerock.opendj.ldif;
 
@@ -1240,12 +1240,18 @@
      */
     private List<String> readLines(final BufferedReader reader) throws IOException {
         final List<String> lines = new ArrayList<>();
-        while (true) {
-            final String line = reader.readLine();
-            if (line == null) {
-                break;
+        String line;
+        for (int lineNumber = 1; (line = reader.readLine()) != null; lineNumber++) {
+            if (line.startsWith(" ")) {
+                final int lastLineIndex = lines.size() - 1;
+                final String previousLine = lines.get(lastLineIndex);
+                if (lines.isEmpty() || previousLine.isEmpty()) {
+                    throw DecodeException.fatalError(ERR_TEMPLATE_FILE_INVALID_LEADING_SPACE.get(lineNumber, line));
+                }
+                lines.set(lastLineIndex, previousLine + line.substring(1));
+            } else {
+                lines.add(line);
             }
-            lines.add(line);
         }
         return lines;
     }
diff --git a/opendj-sdk/opendj-core/src/main/resources/com/forgerock/opendj/ldap/core.properties b/opendj-sdk/opendj-core/src/main/resources/com/forgerock/opendj/ldap/core.properties
index 1315639..77056c7 100644
--- a/opendj-sdk/opendj-core/src/main/resources/com/forgerock/opendj/ldap/core.properties
+++ b/opendj-sdk/opendj-core/src/main/resources/com/forgerock/opendj/ldap/core.properties
@@ -1619,7 +1619,10 @@
 ERR_TRANSACTION_ID_CONTROL_DECODE_NULL=Cannot decode the provided ASN.1 \
  element as a transaction id control because it did not have a value, when \
  a value must always be provided
- 
+ERR_TEMPLATE_FILE_INVALID_LEADING_SPACE=Unable to parse line %d ("%s") from the \
+ template file because the line started with a space but there were no previous \
+ lines in the entry to which this line could be appended
+
 # Labels for generated documentation
 DOC_LOCALE_TAG=Code tag: %s
 DOC_LOCALE_OID=Collation order object identifier: %s
diff --git a/opendj-sdk/opendj-ldap-toolkit/src/test/java/com/forgerock/opendj/ldap/tools/MakeLDIFITCase.java b/opendj-sdk/opendj-ldap-toolkit/src/test/java/com/forgerock/opendj/ldap/tools/MakeLDIFITCase.java
index 53787f8..3fa67de 100644
--- a/opendj-sdk/opendj-ldap-toolkit/src/test/java/com/forgerock/opendj/ldap/tools/MakeLDIFITCase.java
+++ b/opendj-sdk/opendj-ldap-toolkit/src/test/java/com/forgerock/opendj/ldap/tools/MakeLDIFITCase.java
@@ -11,17 +11,22 @@
  * Header, with the fields enclosed by brackets [] replaced by your own identifying
  * information: "Portions Copyright [year] [name of copyright owner]".
  *
- * Copyright 2013-2015 ForgeRock AS.
+ * Copyright 2013-2016 ForgeRock AS.
  */
 package com.forgerock.opendj.ldap.tools;
 
 import static org.fest.assertions.Assertions.*;
 import static org.forgerock.util.Utils.*;
+import static com.forgerock.opendj.ldap.CoreMessages.*;
 
 import static com.forgerock.opendj.ldap.tools.ToolsMessages.*;
 import static com.forgerock.opendj.cli.CliMessages.INFO_GLOBAL_HELP_REFERENCE;
 
+import java.io.IOException;
 import java.io.PrintStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
 
 import org.forgerock.i18n.LocalizableMessage;
 import org.forgerock.opendj.ldap.ByteStringBuilder;
@@ -33,6 +38,13 @@
 @SuppressWarnings("javadoc")
 public class MakeLDIFITCase extends ToolsITCase {
 
+    private static final String TEMP_OUTPUT_FILE = ".temp_test_file.ldif";
+    private static final String TEST_RESOURCE_PATH = "src/test/resources";
+    private static final String VALID_TEMPLATE_FILE_PATH =
+            Paths.get(TEST_RESOURCE_PATH, "valid_test_template.ldif").toString();
+    private static final boolean SUCCESS = true;
+    private static final boolean FAILURE = false;
+
     private ByteStringBuilder out;
     private ByteStringBuilder err;
     private PrintStream outStream;
@@ -93,13 +105,37 @@
     @Test(dataProvider = "validArguments")
     public void testMakeLDIFValidUseCases(final String[] arguments, final LocalizableMessage expectedOut)
             throws Exception {
-        run(arguments, true, expectedOut);
+        run(arguments, SUCCESS, expectedOut);
     }
 
     @Test(dataProvider = "invalidArguments")
     public void testMakeLDIFInvalidUseCases(final String[] arguments, final LocalizableMessage expectedErr)
             throws Exception {
-        run(arguments, false, expectedErr);
+        run(arguments, FAILURE, expectedErr);
+    }
+
+    /** See OPENDJ-2505 */
+    @Test
+    public void testMakeLDIFInvalidLineFolding() throws Exception {
+        final LocalizableMessage expectedOutput = ERR_LDIF_GEN_TOOL_EXCEPTION_DURING_PARSE.get(
+                ERR_TEMPLATE_FILE_INVALID_LEADING_SPACE.get(
+                        27, " \"lineFoldingTest\":\\[\"This line should not be accepted by the parser\"\\],"));
+        run(args("src/test/resources/invalid_test_template.ldif"), FAILURE, expectedOutput);
+    }
+
+    /** See OPENDJ-2505 */
+    @Test
+    public void testMakeLDIFSupportsLineFolding() throws Exception {
+        final Path tempOutputFile = Paths.get(TEST_RESOURCE_PATH, TEMP_OUTPUT_FILE);
+        run(args("-o", tempOutputFile.toString(), VALID_TEMPLATE_FILE_PATH),
+                SUCCESS, INFO_MAKELDIF_PROCESSING_COMPLETE.get(2));
+        assertFilesAreEquals(TEMP_OUTPUT_FILE, "expected_output.ldif");
+        Files.delete(tempOutputFile);
+    }
+
+    /** See OPENDJ-2505 and OPENDJ-2754 */
+        assertThat(Files.readAllBytes(Paths.get(TEST_RESOURCE_PATH, outputFile))).isEqualTo(
+                   Files.readAllBytes(Paths.get(TEST_RESOURCE_PATH, expectedOutputFileName)));
     }
 
     private void run(final String[] arguments, final boolean expectsSuccess, final LocalizableMessage expectedOutput)
diff --git a/opendj-sdk/opendj-ldap-toolkit/src/test/resources/expected_output.ldif b/opendj-sdk/opendj-ldap-toolkit/src/test/resources/expected_output.ldif
new file mode 100644
index 0000000..e5095ca
--- /dev/null
+++ b/opendj-sdk/opendj-ldap-toolkit/src/test/resources/expected_output.ldif
@@ -0,0 +1,11 @@
+dn: dc=example,dc=com
+dc: example
+
+dn: coretokenid=tokenId,dc=example,dc=com
+coretokenid: tokenId
+objectClass: top
+objectClass: frCoreToken
+coretokenstring08: /myrealm
+coretokenstring07: Bearer
+coretokenobject: {"redirectURI":["http://fake.com"],"acr":[],"clientID":["clientOIDC"],"lineFoldingTest":["This line should have been correctly folded"],"tokenName":["refresh_token"],"authModules":["LDAP"],"realm":["/myrealm"],"id":["fakeid"],"userName":["johndoe"],"tokenType":["Bearer"]}
+
diff --git a/opendj-sdk/opendj-ldap-toolkit/src/test/resources/invalid_test_template.ldif b/opendj-sdk/opendj-ldap-toolkit/src/test/resources/invalid_test_template.ldif
new file mode 100644
index 0000000..d629754
--- /dev/null
+++ b/opendj-sdk/opendj-ldap-toolkit/src/test/resources/invalid_test_template.ldif
@@ -0,0 +1,29 @@
+# The contents of this file are subject to the terms of the Common Development and
+# Distribution License (the License). You may not use this file except in compliance with the
+# License.
+#
+# You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
+# specific language governing permission and limitations under the License.
+#
+# When distributing Covered Software, include this CDDL Header Notice in each file and include
+# the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
+# Header, with the fields enclosed by brackets [] replaced by your own identifying
+# information: "Portions Copyright [year] [name of copyright owner]".
+#
+# Copyright 2016 ForgeRock AS.
+
+branch: dc=example,dc=com
+subordinateTemplate: refreshToken:10
+
+template: refreshToken
+rdnAttr: coreTokenId
+coreTokenId: <random:hex:8>-<random:hex:4>-<random:hex:4>-<random:hex:4>-<random:hex:12>
+objectClass: top
+objectClass: frCoreToken
+coreTokenString08: /myrealm
+coreTokenString07: Bearer
+coreTokenObject: \{"redirectURI":\["http://fake.com"\],"acr":\[\],"clientID":\["clientOIDC"\],
+
+ "lineFoldingTest":\["This line should not be accepted by the parser"\],
+ "tokenName":\["refresh_token"\],"authModules":\["LDAP"\],"realm":\["{coreTokenString08}"\],
+ "id":\["fakeid"\],"userName":\["johndoe"\],"tokenType":\["Bearer"\]\}
\ No newline at end of file
diff --git a/opendj-sdk/opendj-ldap-toolkit/src/test/resources/valid_test_template.ldif b/opendj-sdk/opendj-ldap-toolkit/src/test/resources/valid_test_template.ldif
new file mode 100644
index 0000000..fa3f0c3
--- /dev/null
+++ b/opendj-sdk/opendj-ldap-toolkit/src/test/resources/valid_test_template.ldif
@@ -0,0 +1,28 @@
+# The contents of this file are subject to the terms of the Common Development and
+# Distribution License (the License). You may not use this file except in compliance with the
+# License.
+#
+# You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
+# specific language governing permission and limitations under the License.
+#
+# When distributing Covered Software, include this CDDL Header Notice in each file and include
+# the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
+# Header, with the fields enclosed by brackets [] replaced by your own identifying
+# information: "Portions Copyright [year] [name of copyright owner]".
+#
+# Copyright 2016 ForgeRock AS.
+
+branch: dc=example,dc=com
+subordinateTemplate: refreshToken:1
+
+template: refreshToken
+rdnAttr: coreTokenId
+coreTokenId: tokenId
+objectClass: top
+objectClass: frCoreToken
+coreTokenString08: /myrealm
+coreTokenString07: Bearer
+coreTokenObject: \{"redirectURI":\["http://fake.com"\],"acr":\[\],"clientID":\["clientOIDC"\],
+ "lineFoldingTest":\["This line should have been correctly folded"\],
+ "tokenName":\["refresh_token"\],"authModules":\["LDAP"\],"realm":\["{coreTokenString08}"\],
+ "id":\["fakeid"\],"userName":\["johndoe"\],"tokenType":\["Bearer"\]\}
\ No newline at end of file

--
Gitblit v1.10.0