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 0000000000..54362a0e80 --- /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); + } + +} 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 20a07881d0..3e39c9c64f 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 values = new HashMap<>(recordFields.size() * 2); + final Map values = new LinkedHashMap<>(recordFields.size() * 2); for (int i = 0; i < csvRecord.size(); i++) { final String rawValue = csvRecord.get(i);