Repository: nifi
Updated Branches:
  refs/heads/master 95e543d4c -> 11a7d5c09


NIFI-4955 - Preserve columns ordering with CSV and ValidateRecord

Signed-off-by: Matthew Burgess <mattyb...@apache.org>

This closes #2528


Project: http://git-wip-us.apache.org/repos/asf/nifi/repo
Commit: http://git-wip-us.apache.org/repos/asf/nifi/commit/11a7d5c0
Tree: http://git-wip-us.apache.org/repos/asf/nifi/tree/11a7d5c0
Diff: http://git-wip-us.apache.org/repos/asf/nifi/diff/11a7d5c0

Branch: refs/heads/master
Commit: 11a7d5c09fb4fa965e7b240080f9a8b6b5730c62
Parents: 95e543d
Author: Pierre Villard <pierre.villard...@gmail.com>
Authored: Fri Mar 9 18:49:27 2018 +0100
Committer: Matthew Burgess <mattyb...@apache.org>
Committed: Tue Mar 13 17:09:07 2018 -0400

----------------------------------------------------------------------
 .../processors/standard/TestValidateRecord.java | 62 ++++++++++++++++++++
 .../org/apache/nifi/csv/CSVRecordReader.java    |  4 +-
 2 files changed, 64 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/nifi/blob/11a7d5c0/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestValidateRecord.java
----------------------------------------------------------------------
diff --git 
a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestValidateRecord.java
 
b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestValidateRecord.java
new file mode 100644
index 0000000..54362a0
--- /dev/null
+++ 
b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestValidateRecord.java
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.nifi.processors.standard;
+
+import org.apache.nifi.csv.CSVReader;
+import org.apache.nifi.csv.CSVRecordSetWriter;
+import org.apache.nifi.csv.CSVUtils;
+import org.apache.nifi.reporting.InitializationException;
+import org.apache.nifi.util.TestRunner;
+import org.apache.nifi.util.TestRunners;
+import org.junit.Before;
+import org.junit.Test;
+
+public class TestValidateRecord {
+
+    private TestRunner runner;
+
+    @Before
+    public void setup() throws InitializationException {
+        runner = TestRunners.newTestRunner(ValidateRecord.class);
+    }
+
+    @Test
+    public void testColumnsOrder() throws InitializationException {
+        final CSVReader csvReader = new CSVReader();
+        runner.addControllerService("reader", csvReader);
+        runner.setProperty(csvReader, CSVUtils.FIRST_LINE_IS_HEADER, "true");
+        runner.setProperty(csvReader, CSVUtils.QUOTE_MODE, 
CSVUtils.QUOTE_MINIMAL.getValue());
+        runner.setProperty(csvReader, CSVUtils.TRAILING_DELIMITER, "false");
+        runner.enableControllerService(csvReader);
+
+        final CSVRecordSetWriter csvWriter = new CSVRecordSetWriter();
+        runner.addControllerService("writer", csvWriter);
+        runner.setProperty(csvWriter, "Schema Write Strategy", 
"full-schema-attribute");
+        runner.enableControllerService(csvWriter);
+
+        runner.setProperty(ValidateRecord.RECORD_READER, "reader");
+        runner.setProperty(ValidateRecord.RECORD_WRITER, "writer");
+
+        final String content = 
"fieldA,fieldB,fieldC,fieldD,fieldE,fieldF\nvalueA,valueB,valueC,valueD,valueE,valueF\nvalueA,valueB,valueC,valueD,valueE,valueF\n";
+        runner.enqueue(content);
+        runner.run();
+        runner.assertAllFlowFilesTransferred(ValidateRecord.REL_VALID, 1);
+        
runner.getFlowFilesForRelationship(ValidateRecord.REL_VALID).get(0).assertContentEquals(content);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/nifi/blob/11a7d5c0/nifi-nar-bundles/nifi-standard-services/nifi-record-serialization-services-bundle/nifi-record-serialization-services/src/main/java/org/apache/nifi/csv/CSVRecordReader.java
----------------------------------------------------------------------
diff --git 
a/nifi-nar-bundles/nifi-standard-services/nifi-record-serialization-services-bundle/nifi-record-serialization-services/src/main/java/org/apache/nifi/csv/CSVRecordReader.java
 
b/nifi-nar-bundles/nifi-standard-services/nifi-record-serialization-services-bundle/nifi-record-serialization-services/src/main/java/org/apache/nifi/csv/CSVRecordReader.java
index 20a0788..3e39c9c 100644
--- 
a/nifi-nar-bundles/nifi-standard-services/nifi-record-serialization-services-bundle/nifi-record-serialization-services/src/main/java/org/apache/nifi/csv/CSVRecordReader.java
+++ 
b/nifi-nar-bundles/nifi-standard-services/nifi-record-serialization-services-bundle/nifi-record-serialization-services/src/main/java/org/apache/nifi/csv/CSVRecordReader.java
@@ -23,7 +23,7 @@ import java.io.InputStreamReader;
 import java.io.Reader;
 import java.text.DateFormat;
 import java.util.ArrayList;
-import java.util.HashMap;
+import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
@@ -95,7 +95,7 @@ public class CSVRecordReader implements RecordReader {
         final int numFieldNames = recordFields.size();
 
         for (final CSVRecord csvRecord : csvParser) {
-            final Map<String, Object> values = new 
HashMap<>(recordFields.size() * 2);
+            final Map<String, Object> values = new 
LinkedHashMap<>(recordFields.size() * 2);
             for (int i = 0; i < csvRecord.size(); i++) {
                 final String rawValue = csvRecord.get(i);
 

Reply via email to