Github user ijokarumawak commented on a diff in the pull request: https://github.com/apache/nifi/pull/2473#discussion_r175000719 --- Diff: nifi-nar-bundles/nifi-standard-services/nifi-record-serialization-services-bundle/nifi-record-serialization-services/src/test/java/org/apache/nifi/csv/TestCSVRecordReader.java --- @@ -106,6 +117,195 @@ public void testDate() throws IOException, MalformedRecordException { } } + @Test + public void testDateNoCoersionSuccess() throws IOException, MalformedRecordException { + final String text = "date\n11/30/1983"; + + final List<RecordField> fields = new ArrayList<>(); + fields.add(new RecordField("date", RecordFieldType.DATE.getDataType())); + final RecordSchema schema = new SimpleRecordSchema(fields); + + try (final InputStream bais = new ByteArrayInputStream(text.getBytes()); + final CSVRecordReader reader = new CSVRecordReader(bais, Mockito.mock(ComponentLog.class), schema, format, true, false, + "MM/dd/yyyy", RecordFieldType.TIME.getDefaultFormat(), RecordFieldType.TIMESTAMP.getDefaultFormat(), "UTF-8")) { + + final Record record = reader.nextRecord(false, false); + final java.sql.Date date = (Date) record.getValue("date"); + final Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("gmt")); + calendar.setTimeInMillis(date.getTime()); + + assertEquals(1983, calendar.get(Calendar.YEAR)); + assertEquals(10, calendar.get(Calendar.MONTH)); + assertEquals(30, calendar.get(Calendar.DAY_OF_MONTH)); + } + } + + @Test + public void testDateNoCoersionFailure() throws IOException, MalformedRecordException { + final String text = "date\n11/30/1983"; + + final List<RecordField> fields = new ArrayList<>(); + fields.add(new RecordField("date", RecordFieldType.DATE.getDataType())); + final RecordSchema schema = new SimpleRecordSchema(fields); + + try (final InputStream bais = new ByteArrayInputStream(text.getBytes()); + final CSVRecordReader reader = new CSVRecordReader(bais, Mockito.mock(ComponentLog.class), schema, format, true, false, + "MM-dd-yyyy", RecordFieldType.TIME.getDefaultFormat(), RecordFieldType.TIMESTAMP.getDefaultFormat(), "UTF-8")) { + + final Record record = reader.nextRecord(false, false); + assertEquals("11/30/1983", (String)record.getValue("date")); --- End diff -- Assertion comment would be helpful. "When values are not in the expected format, return String as it is".
---