Add tests for setValueAsString and contructor precision checking

This commit is contained in:
dotasek 2022-03-15 17:47:23 -04:00
parent 1a7e076479
commit 9903d15ecc
7 changed files with 361 additions and 3 deletions

View File

@ -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<Arguments> 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 <K extends BaseDateTimeType> void testInvalidString(Class<K> 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<Arguments> 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 <K extends BaseDateTimeType> void testValidString(Class<K> 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());
}
}

View File

@ -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<Arguments> 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 <K extends BaseDateTimeType> void testInvalidString(Class<K> 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<Arguments> 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 <K extends BaseDateTimeType> void testValidString(Class<K> 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());
}
}

View File

@ -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<Arguments> 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 <K extends BaseDateTimeType> void testInvalidString(Class<K> 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<Arguments> 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 <K extends BaseDateTimeType> void testValidString(Class<K> 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());
}
}

View File

@ -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<Arguments> 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 <K extends BaseDateTimeType> void testInvalidString(Class<K> 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<Arguments> 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 <K extends BaseDateTimeType> void testValidString(Class<K> 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());
}
}

View File

@ -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<Arguments> 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 <K extends BaseDateTimeType> void testInvalidString(Class<K> 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<Arguments> 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 <K extends BaseDateTimeType> void testValidString(Class<K> 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());
}
}

View File

@ -100,9 +100,6 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
*/
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);
}
}
/**

View File

@ -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<Arguments> 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 <K extends BaseDateTimeType> void testInvalidString(Class<K> 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<Arguments> 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 <K extends BaseDateTimeType> void testValidString(Class<K> 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());
}
}