From c70a86eac00c149f8d9036db02525d9483bfd809 Mon Sep 17 00:00:00 2001 From: Mike Thomsen Date: Mon, 6 Nov 2017 15:36:55 -0500 Subject: [PATCH] NIFI-4575 Added ability to convert from a date to a timestamp within the record API. Signed-off-by: Matthew Burgess This closes #2255 --- .../record/util/DataTypeUtils.java | 4 ++ .../record/TestDataTypeUtils.java | 44 +++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 nifi-commons/nifi-record/src/test/java/org/apache/nifi/serialization/record/TestDataTypeUtils.java diff --git a/nifi-commons/nifi-record/src/main/java/org/apache/nifi/serialization/record/util/DataTypeUtils.java b/nifi-commons/nifi-record/src/main/java/org/apache/nifi/serialization/record/util/DataTypeUtils.java index 4feec2487a..55a4d6909c 100644 --- a/nifi-commons/nifi-record/src/main/java/org/apache/nifi/serialization/record/util/DataTypeUtils.java +++ b/nifi-commons/nifi-record/src/main/java/org/apache/nifi/serialization/record/util/DataTypeUtils.java @@ -534,6 +534,10 @@ public class DataTypeUtils { return null; } + if (value instanceof java.util.Date) { + return new Timestamp(((java.util.Date)value).getTime()); + } + if (value instanceof Timestamp) { return (Timestamp) value; } diff --git a/nifi-commons/nifi-record/src/test/java/org/apache/nifi/serialization/record/TestDataTypeUtils.java b/nifi-commons/nifi-record/src/test/java/org/apache/nifi/serialization/record/TestDataTypeUtils.java new file mode 100644 index 0000000000..6666651af8 --- /dev/null +++ b/nifi-commons/nifi-record/src/test/java/org/apache/nifi/serialization/record/TestDataTypeUtils.java @@ -0,0 +1,44 @@ +/* + * 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.serialization.record; + +import org.apache.nifi.serialization.record.util.DataTypeUtils; +import org.junit.Assert; +import org.junit.Test; + +import java.sql.Timestamp; + +public class TestDataTypeUtils { + /** + * This is a unit test to verify conversion java Date objects to Timestamps. Support for this was + * required in order to help the MongoDB packages handle date/time logical types in the Record API. + */ + @Test + public void testDateToTimestamp() { + java.util.Date date = new java.util.Date(); + Timestamp ts = DataTypeUtils.toTimestamp(date, null, null); + + Assert.assertNotNull(ts); + Assert.assertEquals("Times didn't match", ts.getTime(), date.getTime()); + + java.sql.Date sDate = new java.sql.Date(date.getTime()); + ts = DataTypeUtils.toTimestamp(date, null, null); + Assert.assertNotNull(ts); + Assert.assertEquals("Times didn't match", ts.getTime(), sDate.getTime()); + } +}