From fb8e2cfa66f6a598e3bb8e7e349ecbd75cd5d10d Mon Sep 17 00:00:00 2001 From: dotasek Date: Fri, 11 Mar 2022 17:32:09 -0500 Subject: [PATCH] WIP Start building tests --- .../primitive40_50/Date40_50.java | 6 +- .../hl7/fhir/utilities/DateTimeUtilTests.java | 61 +++++++++++++++++++ .../hl7/fhir/conversion/tests/DateTest.java | 15 +++++ 3 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/DateTimeUtilTests.java create mode 100644 org.hl7.fhir.validation/src/test/java/org/hl7/fhir/conversion/tests/DateTest.java diff --git a/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/conv40_50/datatypes40_50/primitive40_50/Date40_50.java b/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/conv40_50/datatypes40_50/primitive40_50/Date40_50.java index 90066879b..a317f1d3b 100644 --- a/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/conv40_50/datatypes40_50/primitive40_50/Date40_50.java +++ b/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/conv40_50/datatypes40_50/primitive40_50/Date40_50.java @@ -5,13 +5,15 @@ import org.hl7.fhir.exceptions.FHIRException; public class Date40_50 { public static org.hl7.fhir.r5.model.DateType convertDate(org.hl7.fhir.r4.model.DateType src) throws FHIRException { - org.hl7.fhir.r5.model.DateType tgt = src.hasValue() ? new org.hl7.fhir.r5.model.DateType(src.getValue()) : new org.hl7.fhir.r5.model.DateType(); + org.hl7.fhir.r5.model.DateType tgt = new org.hl7.fhir.r5.model.DateType(); + tgt.setValueAsString(src.getValueAsString()); ConversionContext40_50.INSTANCE.getVersionConvertor_40_50().copyElement(src, tgt); return tgt; } public static org.hl7.fhir.r4.model.DateType convertDate(org.hl7.fhir.r5.model.DateType src) throws FHIRException { - org.hl7.fhir.r4.model.DateType tgt = src.hasValue() ? new org.hl7.fhir.r4.model.DateType(src.getValue()) : new org.hl7.fhir.r4.model.DateType(); + org.hl7.fhir.r4.model.DateType tgt = new org.hl7.fhir.r4.model.DateType(); + tgt.setValueAsString(src.getValueAsString()); ConversionContext40_50.INSTANCE.getVersionConvertor_40_50().copyElement(src, tgt); return tgt; } diff --git a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/DateTimeUtilTests.java b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/DateTimeUtilTests.java new file mode 100644 index 000000000..1415fa883 --- /dev/null +++ b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/DateTimeUtilTests.java @@ -0,0 +1,61 @@ +package org.hl7.fhir.utilities; + +import ca.uhn.fhir.model.api.TemporalPrecisionEnum; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import java.util.Date; +import java.util.TimeZone; +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class DateTimeUtilTests { + + private static Stream getToHumanDisplayParams() { + return Stream.of( + Arguments.of(TimeZone.getTimeZone("EST"), TemporalPrecisionEnum.YEAR, new Date("2002/02/04"), "dummyValueAsString"), + Arguments.of(TimeZone.getTimeZone("EST"), TemporalPrecisionEnum.MONTH, new Date("2002/02/04"), "dummyValueAsString"), + Arguments.of(TimeZone.getTimeZone("EST"), TemporalPrecisionEnum.DAY, new Date("2002/02/04"), "dummyValueAsString"), + Arguments.of(TimeZone.getTimeZone("EST"), TemporalPrecisionEnum.MILLI, new Date("2002/02/04"), "4-Feb-2002 12:00:00 AM"), + Arguments.of(TimeZone.getTimeZone("EST"), TemporalPrecisionEnum.SECOND, new Date("2002/02/04"), "4-Feb-2002 12:00:00 AM"), + Arguments.of(TimeZone.getTimeZone("EST"), TemporalPrecisionEnum.MINUTE, new Date("2002/02/04"), "4-Feb-2002 12:00:00 AM"), + + Arguments.of(TimeZone.getTimeZone("UTC"), TemporalPrecisionEnum.YEAR, new Date("2002/02/04"), "dummyValueAsString"), + Arguments.of(TimeZone.getTimeZone("UTC"), TemporalPrecisionEnum.MONTH, new Date("2002/02/04"), "dummyValueAsString"), + Arguments.of(TimeZone.getTimeZone("UTC"), TemporalPrecisionEnum.DAY, new Date("2002/02/04"), "dummyValueAsString"), + Arguments.of(TimeZone.getTimeZone("UTC"), TemporalPrecisionEnum.MILLI, new Date("2002/02/04"), "4-Feb-2002 12:00:00 AM"), + Arguments.of(TimeZone.getTimeZone("UTC"), TemporalPrecisionEnum.SECOND, new Date("2002/02/04"), "4-Feb-2002 12:00:00 AM"), + Arguments.of(TimeZone.getTimeZone("UTC"), TemporalPrecisionEnum.MINUTE, new Date("2002/02/04"), "4-Feb-2002 12:00:00 AM") + + ); + } + + @ParameterizedTest + @MethodSource("getToHumanDisplayParams") + public void testToHumanDisplay(TimeZone theTimeZone, TemporalPrecisionEnum thePrecision, Date theValue, String expected){ + final String humanDisplay = DateTimeUtil.toHumanDisplay(theTimeZone, thePrecision, theValue, "dummyValueAsString"); + assertEquals(expected, humanDisplay); + } + + private static Stream getToHumanDisplayLocalTimezoneParams() { + return Stream.of( + Arguments.of(TemporalPrecisionEnum.YEAR, new Date("2002/02/04"), "dummyValueAsString"), + Arguments.of(TemporalPrecisionEnum.MONTH, new Date("2002/02/04"), "dummyValueAsString"), + Arguments.of(TemporalPrecisionEnum.DAY, new Date("2002/02/04"), "dummyValueAsString"), + Arguments.of(TemporalPrecisionEnum.MILLI, new Date("2002/02/04"), "4-Feb-2002 12:00:00 AM"), + Arguments.of(TemporalPrecisionEnum.SECOND, new Date("2002/02/04"), "4-Feb-2002 12:00:00 AM"), + Arguments.of(TemporalPrecisionEnum.MINUTE, new Date("2002/02/04"), "4-Feb-2002 12:00:00 AM") + + ); + } + + @ParameterizedTest + @MethodSource("getToHumanDisplayLocalTimezoneParams") + public void testToHumanDisplayLocalTimezone(TemporalPrecisionEnum thePrecision, Date theValue, String expected){ + final String humanDisplay = DateTimeUtil.toHumanDisplayLocalTimezone(thePrecision, theValue, "dummyValueAsString"); + assertEquals(expected, humanDisplay); + } + +} diff --git a/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/conversion/tests/DateTest.java b/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/conversion/tests/DateTest.java new file mode 100644 index 000000000..2b5e88431 --- /dev/null +++ b/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/conversion/tests/DateTest.java @@ -0,0 +1,15 @@ +package org.hl7.fhir.conversion.tests; + +import org.hl7.fhir.convertors.factory.VersionConvertorFactory_40_50; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class DateTest { + @Test + public void testDateConversion() { + org.hl7.fhir.r4.model.DateType date4 = new org.hl7.fhir.r4.model.DateType(); + date4.setValueAsString("1933"); + org.hl7.fhir.r5.model.DateType date5 = ( org.hl7.fhir.r5.model.DateType) VersionConvertorFactory_40_50.convertType(date4); + Assertions.assertEquals(date4.getValueAsString(), date5.getValueAsString()); + } +}