diff --git a/org.hl7.fhir.dstu2/src/test/java/org/hl7/fhir/dstu2/model/BaseDateTimeTypeTests.java b/org.hl7.fhir.dstu2/src/test/java/org/hl7/fhir/dstu2/model/BaseDateTimeTypeTests.java new file mode 100644 index 000000000..9ca552247 --- /dev/null +++ b/org.hl7.fhir.dstu2/src/test/java/org/hl7/fhir/dstu2/model/BaseDateTimeTypeTests.java @@ -0,0 +1,63 @@ +package org.hl7.fhir.dstu2.model; + +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.InvocationTargetException; +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class BaseDateTimeTypeTests { + + private static Stream getInvalidStringParams() { + return Stream.of( + Arguments.of(DateType.class, "1933-01-02T12:34:56"), + Arguments.of(DateType.class, "1933-01-02T12:34:56.7"), + Arguments.of(DateType.class, "1933-01-02T12:34:56.78"), + Arguments.of(DateType.class, "1933-01-02T12:34:56.789"), + Arguments.of(InstantType.class, "1933"), + Arguments.of(InstantType.class, "1933-01"), + Arguments.of(InstantType.class, "1933-01-02") + ); + } + + @ParameterizedTest + @MethodSource("getInvalidStringParams") + public void testInvalidString(Class clazz, String param) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { + InvocationTargetException exceptionWrapper = Assertions.assertThrows(InvocationTargetException.class, () -> clazz.getConstructor(String.class).newInstance(param)); + assertEquals(IllegalArgumentException.class, exceptionWrapper.getTargetException().getClass()); + K srcInstance = clazz.getDeclaredConstructor().newInstance(); + Assertions.assertThrows(IllegalArgumentException.class, () -> srcInstance.setValueAsString(param)); + } + + private static Stream getValidStringParams() { + return Stream.of( + Arguments.of(DateType.class, "1933"), + Arguments.of(DateType.class, "1933-01"), + Arguments.of(DateType.class, "1933-01-02"), + Arguments.of(DateTimeType.class, "1933"), + Arguments.of(DateTimeType.class, "1933-01"), + Arguments.of(DateTimeType.class, "1933-01-02"), + Arguments.of(DateTimeType.class, "1933-01-02T12:34:56"), + Arguments.of(DateTimeType.class, "1933-01-02T12:34:56.7"), + Arguments.of(DateTimeType.class, "1933-01-02T12:34:56.78"), + Arguments.of(DateTimeType.class, "1933-01-02T12:34:56.789"), + Arguments.of(InstantType.class, "1933-01-02T12:34:56"), + Arguments.of(InstantType.class, "1933-01-02T12:34:56.7"), + Arguments.of(InstantType.class, "1933-01-02T12:34:56.78"), + Arguments.of(InstantType.class, "1933-01-02T12:34:56.789") + ); + } + + @ParameterizedTest + @MethodSource("getValidStringParams") + public void testValidString(Class clazz, String param) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { + clazz.getConstructor(String.class).newInstance(param); + K srcInstance = clazz.getDeclaredConstructor().newInstance(); + srcInstance.setValueAsString(param); + assertEquals(param, srcInstance.getValueAsString()); + } +} diff --git a/org.hl7.fhir.dstu2016may/src/test/java/org/hl7/fhir/dstu2016may/model/BaseDateTimeTypeTests.java b/org.hl7.fhir.dstu2016may/src/test/java/org/hl7/fhir/dstu2016may/model/BaseDateTimeTypeTests.java new file mode 100644 index 000000000..a51326362 --- /dev/null +++ b/org.hl7.fhir.dstu2016may/src/test/java/org/hl7/fhir/dstu2016may/model/BaseDateTimeTypeTests.java @@ -0,0 +1,64 @@ +package org.hl7.fhir.dstu2016may.model; + +import ca.uhn.fhir.parser.DataFormatException; +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.InvocationTargetException; +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class BaseDateTimeTypeTests { + + private static Stream getInvalidStringParams() { + return Stream.of( + Arguments.of(DateType.class, "1933-01-02T12:34:56"), + Arguments.of(DateType.class, "1933-01-02T12:34:56.7"), + Arguments.of(DateType.class, "1933-01-02T12:34:56.78"), + Arguments.of(DateType.class, "1933-01-02T12:34:56.789"), + Arguments.of(InstantType.class, "1933"), + Arguments.of(InstantType.class, "1933-01"), + Arguments.of(InstantType.class, "1933-01-02") + ); + } + + @ParameterizedTest + @MethodSource("getInvalidStringParams") + public void testInvalidString(Class clazz, String param) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { + InvocationTargetException exceptionWrapper = Assertions.assertThrows(InvocationTargetException.class, () -> clazz.getConstructor(String.class).newInstance(param)); + assertEquals(DataFormatException.class, exceptionWrapper.getTargetException().getClass()); + K srcInstance = clazz.getDeclaredConstructor().newInstance(); + Assertions.assertThrows(DataFormatException.class, () -> srcInstance.setValueAsString(param)); + } + + private static Stream getValidStringParams() { + return Stream.of( + Arguments.of(DateType.class, "1933"), + Arguments.of(DateType.class, "1933-01"), + Arguments.of(DateType.class, "1933-01-02"), + Arguments.of(DateTimeType.class, "1933"), + Arguments.of(DateTimeType.class, "1933-01"), + Arguments.of(DateTimeType.class, "1933-01-02"), + Arguments.of(DateTimeType.class, "1933-01-02T12:34:56"), + Arguments.of(DateTimeType.class, "1933-01-02T12:34:56.7"), + Arguments.of(DateTimeType.class, "1933-01-02T12:34:56.78"), + Arguments.of(DateTimeType.class, "1933-01-02T12:34:56.789"), + Arguments.of(InstantType.class, "1933-01-02T12:34:56"), + Arguments.of(InstantType.class, "1933-01-02T12:34:56.7"), + Arguments.of(InstantType.class, "1933-01-02T12:34:56.78"), + Arguments.of(InstantType.class, "1933-01-02T12:34:56.789") + ); + } + + @ParameterizedTest + @MethodSource("getValidStringParams") + public void testValidString(Class clazz, String param) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { + clazz.getConstructor(String.class).newInstance(param); + K srcInstance = clazz.getDeclaredConstructor().newInstance(); + srcInstance.setValueAsString(param); + assertEquals(param, srcInstance.getValueAsString()); + } +} diff --git a/org.hl7.fhir.dstu3/src/test/java/org/hl7/fhir/dstu3/model/BaseDateTimeTypeTests.java b/org.hl7.fhir.dstu3/src/test/java/org/hl7/fhir/dstu3/model/BaseDateTimeTypeTests.java new file mode 100644 index 000000000..3d10288d8 --- /dev/null +++ b/org.hl7.fhir.dstu3/src/test/java/org/hl7/fhir/dstu3/model/BaseDateTimeTypeTests.java @@ -0,0 +1,64 @@ +package org.hl7.fhir.dstu3.model; + +import ca.uhn.fhir.parser.DataFormatException; +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.InvocationTargetException; +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class BaseDateTimeTypeTests { + + private static Stream getInvalidStringParams() { + return Stream.of( + Arguments.of(DateType.class, "1933-01-02T12:34:56"), + Arguments.of(DateType.class, "1933-01-02T12:34:56.7"), + Arguments.of(DateType.class, "1933-01-02T12:34:56.78"), + Arguments.of(DateType.class, "1933-01-02T12:34:56.789"), + Arguments.of(InstantType.class, "1933"), + Arguments.of(InstantType.class, "1933-01"), + Arguments.of(InstantType.class, "1933-01-02") + ); + } + + @ParameterizedTest + @MethodSource("getInvalidStringParams") + public void testInvalidString(Class clazz, String param) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { + InvocationTargetException exceptionWrapper = Assertions.assertThrows(InvocationTargetException.class, () -> clazz.getConstructor(String.class).newInstance(param)); + assertEquals(DataFormatException.class, exceptionWrapper.getTargetException().getClass()); + K srcInstance = clazz.getDeclaredConstructor().newInstance(); + Assertions.assertThrows(DataFormatException.class, () -> srcInstance.setValueAsString(param)); + } + + private static Stream getValidStringParams() { + return Stream.of( + Arguments.of(DateType.class, "1933"), + Arguments.of(DateType.class, "1933-01"), + Arguments.of(DateType.class, "1933-01-02"), + Arguments.of(DateTimeType.class, "1933"), + Arguments.of(DateTimeType.class, "1933-01"), + Arguments.of(DateTimeType.class, "1933-01-02"), + Arguments.of(DateTimeType.class, "1933-01-02T12:34:56"), + Arguments.of(DateTimeType.class, "1933-01-02T12:34:56.7"), + Arguments.of(DateTimeType.class, "1933-01-02T12:34:56.78"), + Arguments.of(DateTimeType.class, "1933-01-02T12:34:56.789"), + Arguments.of(InstantType.class, "1933-01-02T12:34:56"), + Arguments.of(InstantType.class, "1933-01-02T12:34:56.7"), + Arguments.of(InstantType.class, "1933-01-02T12:34:56.78"), + Arguments.of(InstantType.class, "1933-01-02T12:34:56.789") + ); + } + + @ParameterizedTest + @MethodSource("getValidStringParams") + public void testValidString(Class clazz, String param) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { + clazz.getConstructor(String.class).newInstance(param); + K srcInstance = clazz.getDeclaredConstructor().newInstance(); + srcInstance.setValueAsString(param); + assertEquals(param, srcInstance.getValueAsString()); + } +} diff --git a/org.hl7.fhir.r4/src/test/java/org/hl7/fhir/r4/model/BaseDateTimeTypeTest.java b/org.hl7.fhir.r4/src/test/java/org/hl7/fhir/r4/model/BaseDateTimeTypeTest.java index af866441c..c4141d77c 100644 --- a/org.hl7.fhir.r4/src/test/java/org/hl7/fhir/r4/model/BaseDateTimeTypeTest.java +++ b/org.hl7.fhir.r4/src/test/java/org/hl7/fhir/r4/model/BaseDateTimeTypeTest.java @@ -1,8 +1,17 @@ package org.hl7.fhir.r4.model; +import ca.uhn.fhir.parser.DataFormatException; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import java.lang.reflect.InvocationTargetException; +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.assertEquals; public class BaseDateTimeTypeTest { @@ -70,4 +79,53 @@ public class BaseDateTimeTypeTest { return leftDt.equalsUsingFhirPathRules(rightDt); } + private static Stream getInvalidStringParams() { + return Stream.of( + Arguments.of(DateType.class, "1933-01-02T12:34:56"), + Arguments.of(DateType.class, "1933-01-02T12:34:56.7"), + Arguments.of(DateType.class, "1933-01-02T12:34:56.78"), + Arguments.of(DateType.class, "1933-01-02T12:34:56.789"), + Arguments.of(InstantType.class, "1933"), + Arguments.of(InstantType.class, "1933-01"), + Arguments.of(InstantType.class, "1933-01-02") + ); + } + + @ParameterizedTest + @MethodSource("getInvalidStringParams") + public void testInvalidString(Class clazz, String param) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { + InvocationTargetException exceptionWrapper = Assertions.assertThrows(InvocationTargetException.class, () -> clazz.getConstructor(String.class).newInstance(param)); + assertEquals(IllegalArgumentException.class, exceptionWrapper.getTargetException().getClass()); + K srcInstance = clazz.getDeclaredConstructor().newInstance(); + Assertions.assertThrows(IllegalArgumentException.class, () -> srcInstance.setValueAsString(param)); + } + + private static Stream getValidStringParams() { + return Stream.of( + Arguments.of(DateType.class, "1933"), + Arguments.of(DateType.class, "1933-01"), + Arguments.of(DateType.class, "1933-01-02"), + Arguments.of(DateTimeType.class, "1933"), + Arguments.of(DateTimeType.class, "1933-01"), + Arguments.of(DateTimeType.class, "1933-01-02"), + Arguments.of(DateTimeType.class, "1933-01-02T12:34:56"), + Arguments.of(DateTimeType.class, "1933-01-02T12:34:56.7"), + Arguments.of(DateTimeType.class, "1933-01-02T12:34:56.78"), + Arguments.of(DateTimeType.class, "1933-01-02T12:34:56.789"), + Arguments.of(InstantType.class, "1933-01-02T12:34:56"), + Arguments.of(InstantType.class, "1933-01-02T12:34:56.7"), + Arguments.of(InstantType.class, "1933-01-02T12:34:56.78"), + Arguments.of(InstantType.class, "1933-01-02T12:34:56.789") + ); + } + + @ParameterizedTest + @MethodSource("getValidStringParams") + public void testValidString(Class clazz, String param) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { + clazz.getConstructor(String.class).newInstance(param); + K srcInstance = clazz.getDeclaredConstructor().newInstance(); + srcInstance.setValueAsString(param); + assertEquals(param, srcInstance.getValueAsString()); + } + } \ No newline at end of file diff --git a/org.hl7.fhir.r4b/src/test/java/org/hl7/fhir/r4b/model/BaseDateTimeTypeTest.java b/org.hl7.fhir.r4b/src/test/java/org/hl7/fhir/r4b/model/BaseDateTimeTypeTest.java index 2fa22b0b2..62b5fd34c 100644 --- a/org.hl7.fhir.r4b/src/test/java/org/hl7/fhir/r4b/model/BaseDateTimeTypeTest.java +++ b/org.hl7.fhir.r4b/src/test/java/org/hl7/fhir/r4b/model/BaseDateTimeTypeTest.java @@ -1,9 +1,16 @@ package org.hl7.fhir.r4b.model; +import java.lang.reflect.InvocationTargetException; import java.util.TimeZone; +import java.util.stream.Stream; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import static org.junit.jupiter.api.Assertions.assertEquals; public class BaseDateTimeTypeTest { @@ -104,4 +111,53 @@ public class BaseDateTimeTypeTest { return leftDt.equalsUsingFhirPathRules(rightDt); } + private static Stream getInvalidStringParams() { + return Stream.of( + Arguments.of(DateType.class, "1933-01-02T12:34:56"), + Arguments.of(DateType.class, "1933-01-02T12:34:56.7"), + Arguments.of(DateType.class, "1933-01-02T12:34:56.78"), + Arguments.of(DateType.class, "1933-01-02T12:34:56.789"), + Arguments.of(InstantType.class, "1933"), + Arguments.of(InstantType.class, "1933-01"), + Arguments.of(InstantType.class, "1933-01-02") + ); + } + + @ParameterizedTest + @MethodSource("getInvalidStringParams") + public void testInvalidString(Class clazz, String param) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { + InvocationTargetException exceptionWrapper = Assertions.assertThrows(InvocationTargetException.class, () -> clazz.getConstructor(String.class).newInstance(param)); + assertEquals(IllegalArgumentException.class, exceptionWrapper.getTargetException().getClass()); + K srcInstance = clazz.getDeclaredConstructor().newInstance(); + Assertions.assertThrows(IllegalArgumentException.class, () -> srcInstance.setValueAsString(param)); + } + + private static Stream getValidStringParams() { + return Stream.of( + Arguments.of(DateType.class, "1933"), + Arguments.of(DateType.class, "1933-01"), + Arguments.of(DateType.class, "1933-01-02"), + Arguments.of(DateTimeType.class, "1933"), + Arguments.of(DateTimeType.class, "1933-01"), + Arguments.of(DateTimeType.class, "1933-01-02"), + Arguments.of(DateTimeType.class, "1933-01-02T12:34:56"), + Arguments.of(DateTimeType.class, "1933-01-02T12:34:56.7"), + Arguments.of(DateTimeType.class, "1933-01-02T12:34:56.78"), + Arguments.of(DateTimeType.class, "1933-01-02T12:34:56.789"), + Arguments.of(InstantType.class, "1933-01-02T12:34:56"), + Arguments.of(InstantType.class, "1933-01-02T12:34:56.7"), + Arguments.of(InstantType.class, "1933-01-02T12:34:56.78"), + Arguments.of(InstantType.class, "1933-01-02T12:34:56.789") + ); + } + + @ParameterizedTest + @MethodSource("getValidStringParams") + public void testValidString(Class clazz, String param) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { + clazz.getConstructor(String.class).newInstance(param); + K srcInstance = clazz.getDeclaredConstructor().newInstance(); + srcInstance.setValueAsString(param); + assertEquals(param, srcInstance.getValueAsString()); + } + } \ No newline at end of file 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 5c41bca31..b35f933fe 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 @@ -100,9 +100,6 @@ public abstract class BaseDateTimeType extends PrimitiveType { */ public BaseDateTimeType(String theString) { setValueAsString(theString); - if (isPrecisionAllowed(getPrecision()) == false) { - throw new IllegalArgumentException("Invalid date/time string (datatype " + getClass().getSimpleName() + " does not support " + getPrecision() + " precision): " + theString); - } } /** diff --git a/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/model/BaseDateTimeTypeTest.java b/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/model/BaseDateTimeTypeTest.java index 929a86cbd..6f2464a5e 100644 --- a/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/model/BaseDateTimeTypeTest.java +++ b/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/model/BaseDateTimeTypeTest.java @@ -1,9 +1,16 @@ package org.hl7.fhir.r5.model; +import java.lang.reflect.InvocationTargetException; import java.util.TimeZone; +import java.util.stream.Stream; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import static org.junit.jupiter.api.Assertions.assertEquals; public class BaseDateTimeTypeTest { @@ -104,4 +111,53 @@ public class BaseDateTimeTypeTest { return leftDt.equalsUsingFhirPathRules(rightDt); } + private static Stream getInvalidStringParams() { + return Stream.of( + Arguments.of(DateType.class, "1933-01-02T12:34:56"), + Arguments.of(DateType.class, "1933-01-02T12:34:56.7"), + Arguments.of(DateType.class, "1933-01-02T12:34:56.78"), + Arguments.of(DateType.class, "1933-01-02T12:34:56.789"), + Arguments.of(InstantType.class, "1933"), + Arguments.of(InstantType.class, "1933-01"), + Arguments.of(InstantType.class, "1933-01-02") + ); + } + + @ParameterizedTest + @MethodSource("getInvalidStringParams") + public void testInvalidString(Class clazz, String param) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { + InvocationTargetException exceptionWrapper = Assertions.assertThrows(InvocationTargetException.class, () -> clazz.getConstructor(String.class).newInstance(param)); + assertEquals(IllegalArgumentException.class, exceptionWrapper.getTargetException().getClass()); + K srcInstance = clazz.getDeclaredConstructor().newInstance(); + Assertions.assertThrows(IllegalArgumentException.class, () -> srcInstance.setValueAsString(param)); + } + + private static Stream getValidStringParams() { + return Stream.of( + Arguments.of(DateType.class, "1933"), + Arguments.of(DateType.class, "1933-01"), + Arguments.of(DateType.class, "1933-01-02"), + Arguments.of(DateTimeType.class, "1933"), + Arguments.of(DateTimeType.class, "1933-01"), + Arguments.of(DateTimeType.class, "1933-01-02"), + Arguments.of(DateTimeType.class, "1933-01-02T12:34:56"), + Arguments.of(DateTimeType.class, "1933-01-02T12:34:56.7"), + Arguments.of(DateTimeType.class, "1933-01-02T12:34:56.78"), + Arguments.of(DateTimeType.class, "1933-01-02T12:34:56.789"), + Arguments.of(InstantType.class, "1933-01-02T12:34:56"), + Arguments.of(InstantType.class, "1933-01-02T12:34:56.7"), + Arguments.of(InstantType.class, "1933-01-02T12:34:56.78"), + Arguments.of(InstantType.class, "1933-01-02T12:34:56.789") + ); + } + + @ParameterizedTest + @MethodSource("getValidStringParams") + public void testValidString(Class clazz, String param) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { + clazz.getConstructor(String.class).newInstance(param); + K srcInstance = clazz.getDeclaredConstructor().newInstance(); + srcInstance.setValueAsString(param); + assertEquals(param, srcInstance.getValueAsString()); + } + } \ No newline at end of file