WIP Add tests for Primitive Types + use getValueAsString for conversion

This commit is contained in:
dotasek 2022-03-15 12:35:50 -04:00
parent fb8e2cfa66
commit 4068db73ff
7 changed files with 148 additions and 33 deletions

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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 <K,L> Stream<Arguments> getDateTimeArgs(Class<? extends VersionConvertorFactory> versionConverterFactoryClazz, Class<K> firstTypeClazz, Class<?> firstTypeMethodClazz, Class<L> secondTypeClazz, Class<?> secondTypeMethodClazz, String[] testStrings) {
return Arrays.stream(testStrings).map( it -> Arguments.of(versionConverterFactoryClazz,
firstTypeClazz, firstTypeMethodClazz, it,
secondTypeClazz, secondTypeMethodClazz, it
));
}
private static Stream<Arguments> 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 <K, L> void testValidConversion(Class<VersionConvertorFactory> versionConverterFactoryClazz, Class<K> firstTypeClazz, Class<?> firstTypeMethodClazz, String firstString, Class<L> 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 <K, L> void testValidConversion(Class<VersionConvertorFactory> versionConverterFactoryClazz, Class<K> srcTypeClazz, Class<?> srcTypeMethodClazz, String srcString, Class<L> 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);
}
}

View File

@ -716,9 +716,12 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
}
@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) {

View File

@ -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());
}
}