From 4068db73ffc4fba3ba07ac308027964953cdd5c0 Mon Sep 17 00:00:00 2001 From: dotasek Date: Tue, 15 Mar 2022 12:35:50 -0400 Subject: [PATCH] WIP Add tests for Primitive Types + use getValueAsString for conversion --- .../primitivetypes14_30/Date14_30.java | 6 +- .../primitivetypes14_40/Date14_40.java | 6 +- .../primitivetypes14_50/Date14_50.java | 6 +- .../primitive40_50/Date40_50.java | 6 +- .../VersionConvertorPrimitiveTypeTests.java | 135 ++++++++++++++++++ .../hl7/fhir/r5/model/BaseDateTimeType.java | 7 +- .../hl7/fhir/conversion/tests/DateTest.java | 15 -- 7 files changed, 148 insertions(+), 33 deletions(-) create mode 100644 org.hl7.fhir.convertors/src/test/java/org/hl7/fhir/convertors/VersionConvertorPrimitiveTypeTests.java delete 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/conv14_30/datatypes14_30/primitivetypes14_30/Date14_30.java b/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/conv14_30/datatypes14_30/primitivetypes14_30/Date14_30.java index dd47f74e6..6ebf99dea 100644 --- a/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/conv14_30/datatypes14_30/primitivetypes14_30/Date14_30.java +++ b/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/conv14_30/datatypes14_30/primitivetypes14_30/Date14_30.java @@ -5,15 +5,13 @@ import org.hl7.fhir.exceptions.FHIRException; public class Date14_30 { public static org.hl7.fhir.dstu3.model.DateType convertDate(org.hl7.fhir.dstu2016may.model.DateType src) throws FHIRException { - org.hl7.fhir.dstu3.model.DateType tgt = new org.hl7.fhir.dstu3.model.DateType(); - if (src.hasValue()) tgt.setValue(src.getValue()); + org.hl7.fhir.dstu3.model.DateType tgt = src.hasValue() ? new org.hl7.fhir.dstu3.model.DateType(src.getValueAsString()) : new org.hl7.fhir.dstu3.model.DateType(); ConversionContext14_30.INSTANCE.getVersionConvertor_14_30().copyElement(src, tgt); return tgt; } public static org.hl7.fhir.dstu2016may.model.DateType convertDate(org.hl7.fhir.dstu3.model.DateType src) throws FHIRException { - org.hl7.fhir.dstu2016may.model.DateType tgt = new org.hl7.fhir.dstu2016may.model.DateType(); - if (src.hasValue()) tgt.setValue(src.getValue()); + org.hl7.fhir.dstu2016may.model.DateType tgt = src.hasValue() ? new org.hl7.fhir.dstu2016may.model.DateType(src.getValueAsString()) : new org.hl7.fhir.dstu2016may.model.DateType(); ConversionContext14_30.INSTANCE.getVersionConvertor_14_30().copyElement(src, tgt); return tgt; } diff --git a/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/conv14_40/datatypes14_40/primitivetypes14_40/Date14_40.java b/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/conv14_40/datatypes14_40/primitivetypes14_40/Date14_40.java index 8eaa16ba0..2e4cb4fa8 100644 --- a/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/conv14_40/datatypes14_40/primitivetypes14_40/Date14_40.java +++ b/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/conv14_40/datatypes14_40/primitivetypes14_40/Date14_40.java @@ -5,15 +5,13 @@ import org.hl7.fhir.exceptions.FHIRException; public class Date14_40 { public static org.hl7.fhir.r4.model.DateType convertDate(org.hl7.fhir.dstu2016may.model.DateType src) throws FHIRException { - org.hl7.fhir.r4.model.DateType tgt = new org.hl7.fhir.r4.model.DateType(); - if (src.hasValue()) tgt.setValue(src.getValue()); + org.hl7.fhir.r4.model.DateType tgt = src.hasValue() ? new org.hl7.fhir.r4.model.DateType(src.getValueAsString()) : new org.hl7.fhir.r4.model.DateType(); ConversionContext14_40.INSTANCE.getVersionConvertor_14_40().copyElement(src, tgt); return tgt; } public static org.hl7.fhir.dstu2016may.model.DateType convertDate(org.hl7.fhir.r4.model.DateType src) throws FHIRException { - org.hl7.fhir.dstu2016may.model.DateType tgt = new org.hl7.fhir.dstu2016may.model.DateType(); - if (src.hasValue()) tgt.setValue(src.getValue()); + org.hl7.fhir.dstu2016may.model.DateType tgt = src.hasValue() ? new org.hl7.fhir.dstu2016may.model.DateType(src.getValueAsString()) : new org.hl7.fhir.dstu2016may.model.DateType(); ConversionContext14_40.INSTANCE.getVersionConvertor_14_40().copyElement(src, tgt); return tgt; } diff --git a/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/conv14_50/datatypes14_50/primitivetypes14_50/Date14_50.java b/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/conv14_50/datatypes14_50/primitivetypes14_50/Date14_50.java index 64e6bf56c..1c050420b 100644 --- a/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/conv14_50/datatypes14_50/primitivetypes14_50/Date14_50.java +++ b/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/conv14_50/datatypes14_50/primitivetypes14_50/Date14_50.java @@ -5,15 +5,13 @@ import org.hl7.fhir.exceptions.FHIRException; public class Date14_50 { public static org.hl7.fhir.r5.model.DateType convertDate(org.hl7.fhir.dstu2016may.model.DateType src) throws FHIRException { - org.hl7.fhir.r5.model.DateType tgt = new org.hl7.fhir.r5.model.DateType(); - if (src.hasValue()) tgt.setValue(src.getValue()); + org.hl7.fhir.r5.model.DateType tgt = src.hasValue() ? new org.hl7.fhir.r5.model.DateType(src.getValueAsString()) : new org.hl7.fhir.r5.model.DateType(); ConversionContext14_50.INSTANCE.getVersionConvertor_14_50().copyElement(src, tgt); return tgt; } public static org.hl7.fhir.dstu2016may.model.DateType convertDate(org.hl7.fhir.r5.model.DateType src) throws FHIRException { - org.hl7.fhir.dstu2016may.model.DateType tgt = new org.hl7.fhir.dstu2016may.model.DateType(); - if (src.hasValue()) tgt.setValue(src.getValue()); + org.hl7.fhir.dstu2016may.model.DateType tgt = src.hasValue() ? new org.hl7.fhir.dstu2016may.model.DateType(src.getValueAsString()) : new org.hl7.fhir.dstu2016may.model.DateType(); ConversionContext14_50.INSTANCE.getVersionConvertor_14_50().copyElement(src, tgt); return tgt; } 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 a317f1d3b..204aa486a 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,15 +5,13 @@ 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 = new org.hl7.fhir.r5.model.DateType(); - tgt.setValueAsString(src.getValueAsString()); + org.hl7.fhir.r5.model.DateType tgt = src.hasValue() ? new org.hl7.fhir.r5.model.DateType(src.getValueAsString()) : new org.hl7.fhir.r5.model.DateType(); 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 = new org.hl7.fhir.r4.model.DateType(); - tgt.setValueAsString(src.getValueAsString()); + org.hl7.fhir.r4.model.DateType tgt = src.hasValue() ? new org.hl7.fhir.r4.model.DateType(src.getValueAsString()) : new org.hl7.fhir.r4.model.DateType(); ConversionContext40_50.INSTANCE.getVersionConvertor_40_50().copyElement(src, tgt); return tgt; } diff --git a/org.hl7.fhir.convertors/src/test/java/org/hl7/fhir/convertors/VersionConvertorPrimitiveTypeTests.java b/org.hl7.fhir.convertors/src/test/java/org/hl7/fhir/convertors/VersionConvertorPrimitiveTypeTests.java new file mode 100644 index 000000000..14925becf --- /dev/null +++ b/org.hl7.fhir.convertors/src/test/java/org/hl7/fhir/convertors/VersionConvertorPrimitiveTypeTests.java @@ -0,0 +1,135 @@ +package org.hl7.fhir.convertors; + +import org.hl7.fhir.convertors.factory.*; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import java.lang.reflect.Array; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +import java.util.Arrays; +import java.util.stream.Stream; + +public class VersionConvertorPrimitiveTypeTests { + + private static String[] DEFAULT_DATE_PRECISION_STRINGS = { + "1933", "1933-01", "1933-01-02" + }; + + private static String[] SECOND_DATE_PRECISION_STRINGS = + Stream.concat(Arrays.stream(DEFAULT_DATE_PRECISION_STRINGS),Stream.of("1933-01-02T13:45:12")) + .toArray(size -> (String[]) Array.newInstance(String.class, size)); + + private static String[] MILLISECOND_DATE_PRECISION_STRINGS = + Stream.concat(Arrays.stream(SECOND_DATE_PRECISION_STRINGS),Stream.of("1933-01-02T13:45:12.3", "1933-01-02T13:45:12.3456")) + .toArray(size -> (String[]) Array.newInstance(String.class, size)); + + private static Stream getDateTimeArgs(Class versionConverterFactoryClazz, Class firstTypeClazz, Class firstTypeMethodClazz, Class secondTypeClazz, Class secondTypeMethodClazz, String[] testStrings) { + + return Arrays.stream(testStrings).map( it -> Arguments.of(versionConverterFactoryClazz, + firstTypeClazz, firstTypeMethodClazz, it, + secondTypeClazz, secondTypeMethodClazz, it + )); + } + + private static Stream getParams() { + return Stream.of( + //Date + getDateTimeArgs(VersionConvertorFactory_10_30.class, + org.hl7.fhir.dstu2.model.DateType.class, org.hl7.fhir.dstu2.model.Type.class, + org.hl7.fhir.dstu3.model.DateType.class, org.hl7.fhir.dstu3.model.Type.class, + DEFAULT_DATE_PRECISION_STRINGS), + getDateTimeArgs(VersionConvertorFactory_10_40.class, + org.hl7.fhir.dstu2.model.DateType.class, org.hl7.fhir.dstu2.model.Type.class, + org.hl7.fhir.r4.model.DateType.class, org.hl7.fhir.r4.model.Type.class, + DEFAULT_DATE_PRECISION_STRINGS), + getDateTimeArgs(VersionConvertorFactory_10_50.class, + org.hl7.fhir.dstu2.model.DateType.class, org.hl7.fhir.dstu2.model.Type.class, + org.hl7.fhir.r5.model.DateType.class, org.hl7.fhir.r5.model.DataType.class, + DEFAULT_DATE_PRECISION_STRINGS), + getDateTimeArgs(VersionConvertorFactory_14_30.class, + org.hl7.fhir.dstu2016may.model.DateType.class, org.hl7.fhir.dstu2016may.model.Type.class, + org.hl7.fhir.dstu3.model.DateType.class, org.hl7.fhir.dstu3.model.Type.class, + DEFAULT_DATE_PRECISION_STRINGS), + getDateTimeArgs(VersionConvertorFactory_14_40.class, + org.hl7.fhir.dstu2016may.model.DateType.class, org.hl7.fhir.dstu2016may.model.Type.class, + org.hl7.fhir.r4.model.DateType.class, org.hl7.fhir.r4.model.Type.class, + DEFAULT_DATE_PRECISION_STRINGS), + getDateTimeArgs(VersionConvertorFactory_14_50.class, + org.hl7.fhir.dstu2016may.model.DateType.class, org.hl7.fhir.dstu2016may.model.Type.class, + org.hl7.fhir.r5.model.DateType.class, org.hl7.fhir.r5.model.DataType.class, + DEFAULT_DATE_PRECISION_STRINGS), + getDateTimeArgs(VersionConvertorFactory_30_40.class, + org.hl7.fhir.dstu3.model.DateType.class, org.hl7.fhir.dstu3.model.Type.class, + org.hl7.fhir.r4.model.DateType.class, org.hl7.fhir.r4.model.Type.class, + DEFAULT_DATE_PRECISION_STRINGS), + getDateTimeArgs(VersionConvertorFactory_30_50.class, + org.hl7.fhir.dstu3.model.DateType.class, org.hl7.fhir.dstu3.model.Type.class, + org.hl7.fhir.r5.model.DateType.class, org.hl7.fhir.r5.model.DataType.class, + DEFAULT_DATE_PRECISION_STRINGS), + getDateTimeArgs(VersionConvertorFactory_40_50.class, + org.hl7.fhir.r4.model.DateType.class, org.hl7.fhir.r4.model.Type.class, + org.hl7.fhir.r5.model.DateType.class, org.hl7.fhir.r5.model.DataType.class, + DEFAULT_DATE_PRECISION_STRINGS), + + //Date DateTime + getDateTimeArgs(VersionConvertorFactory_10_30.class, + org.hl7.fhir.dstu2.model.DateType.class, org.hl7.fhir.dstu2.model.Type.class, + org.hl7.fhir.dstu3.model.DateTimeType.class, org.hl7.fhir.dstu3.model.Type.class, + DEFAULT_DATE_PRECISION_STRINGS), + getDateTimeArgs(VersionConvertorFactory_10_40.class, + org.hl7.fhir.dstu2.model.DateType.class, org.hl7.fhir.dstu2.model.Type.class, + org.hl7.fhir.r4.model.DateTimeType.class, org.hl7.fhir.r4.model.Type.class, + DEFAULT_DATE_PRECISION_STRINGS), + getDateTimeArgs(VersionConvertorFactory_10_50.class, + org.hl7.fhir.dstu2.model.DateType.class, org.hl7.fhir.dstu2.model.Type.class, + org.hl7.fhir.r5.model.DateTimeType.class, org.hl7.fhir.r5.model.DataType.class, + DEFAULT_DATE_PRECISION_STRINGS), + getDateTimeArgs(VersionConvertorFactory_10_30.class, + org.hl7.fhir.dstu2.model.DateTimeType.class, org.hl7.fhir.dstu2.model.Type.class, + org.hl7.fhir.dstu3.model.DateType.class, org.hl7.fhir.dstu3.model.Type.class, + DEFAULT_DATE_PRECISION_STRINGS), + getDateTimeArgs(VersionConvertorFactory_10_40.class, + org.hl7.fhir.dstu2.model.DateTimeType.class, org.hl7.fhir.dstu2.model.Type.class, + org.hl7.fhir.r4.model.DateType.class, org.hl7.fhir.r4.model.Type.class, + DEFAULT_DATE_PRECISION_STRINGS), + getDateTimeArgs(VersionConvertorFactory_10_50.class, + org.hl7.fhir.dstu2.model.DateTimeType.class, org.hl7.fhir.dstu2.model.Type.class, + org.hl7.fhir.r5.model.DateType.class, org.hl7.fhir.r5.model.DataType.class, + DEFAULT_DATE_PRECISION_STRINGS) + + ).flatMap(i -> i); + + } + + @ParameterizedTest(name = "Test index: {index} ConvertorFactory={0} First Class={1} First Value={3} Second Class={4} Second Value={6}") + @MethodSource("getParams") + public void testValidConversion(Class versionConverterFactoryClazz, Class firstTypeClazz, Class firstTypeMethodClazz, String firstString, Class secondTypeClazz, Class secondTypeMethodClazz, String secondString) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException { + testValidConversion(versionConverterFactoryClazz, firstTypeClazz, firstTypeMethodClazz, firstString, secondTypeClazz, secondString); + testValidConversion(versionConverterFactoryClazz, secondTypeClazz, secondTypeMethodClazz, secondString, firstTypeClazz, firstString); + } + + public void testValidConversion(Class versionConverterFactoryClazz, Class srcTypeClazz, Class srcTypeMethodClazz, String srcString, Class tgtTypeClazz, String tgtString) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { + K srcInstance = srcTypeClazz.getDeclaredConstructor().newInstance(); + + Method srcSetValueAsStringMethod = srcTypeClazz.getMethod("setValueAsString", String.class); + Method srcGetValueAsString = srcTypeClazz.getMethod("getValueAsString"); + + Method convertTypeMethod = versionConverterFactoryClazz.getMethod("convertType", srcTypeMethodClazz); + + Method tgtGetValueAsString = tgtTypeClazz.getMethod("getValueAsString"); + srcSetValueAsStringMethod.invoke(srcInstance, srcString); + + String srcValueAsString = (String) srcGetValueAsString.invoke(srcInstance); + Assertions.assertEquals(srcString, srcValueAsString); + + L tgtInstance = (L) convertTypeMethod.invoke(null, srcInstance); + + String tgtValueAsString = (String) tgtGetValueAsString.invoke(tgtInstance); + + Assertions.assertEquals( tgtString, tgtValueAsString); + } +} diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/model/BaseDateTimeType.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/model/BaseDateTimeType.java index defd051a3..5c41bca31 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/model/BaseDateTimeType.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/model/BaseDateTimeType.java @@ -716,9 +716,12 @@ public abstract class BaseDateTimeType extends PrimitiveType { } @Override - public void setValueAsString(String theValue) throws DataFormatException { + public void setValueAsString(String theString) throws DataFormatException { clearTimeZone(); - super.setValueAsString(theValue); + super.setValueAsString(theString); + if (isPrecisionAllowed(getPrecision()) == false) { + throw new IllegalArgumentException("Invalid date/time string (datatype " + getClass().getSimpleName() + " does not support " + getPrecision() + " precision): " + theString); + } } protected void setValueAsV3String(String theV3String) { 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 deleted file mode 100644 index 2b5e88431..000000000 --- a/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/conversion/tests/DateTest.java +++ /dev/null @@ -1,15 +0,0 @@ -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()); - } -}