Add tests and fixes for remaining FHIR versions

This commit is contained in:
dotasek 2024-12-04 16:50:38 -05:00
parent f210ef36a5
commit d0fa45959e
11 changed files with 269 additions and 44 deletions

View File

@ -34,6 +34,7 @@ import java.math.MathContext;
import java.math.RoundingMode;
import ca.uhn.fhir.model.api.annotation.DatatypeDef;
import org.apache.commons.lang3.StringUtils;
import org.hl7.fhir.instance.model.api.IBaseDecimalDatatype;
/**
@ -78,7 +79,7 @@ public class DecimalType extends PrimitiveType<BigDecimal> implements Comparable
* Constructor
*/
public DecimalType(String theValue) {
setValue(new BigDecimal(theValue));
setValueAsString(theValue);
}
@Override
@ -148,6 +149,14 @@ public class DecimalType extends PrimitiveType<BigDecimal> implements Comparable
setValue(new BigDecimal(theValue));
}
public void setValueAsString(String theString) {
if (StringUtils.isBlank(theString)) {
setValue((BigDecimal) null);
} else {
setValue(new BigDecimal(theString));
}
}
@Override
public DecimalType copy() {
return new DecimalType(getValue());

View File

@ -3,6 +3,11 @@ package org.hl7.fhir.dstu2.model;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
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.util.stream.Stream;
class DecimalTypeNullTest {
@ -44,4 +49,27 @@ class DecimalTypeNullTest {
DecimalType copyDecimal = (DecimalType) nullDecimal.typedCopy();
Assertions.assertNull(copyDecimal.getValue());
}
public static Stream<Arguments> provideEmptyOrNullStringsConstructor() {
return Stream.of(
Arguments.of((String)null),
Arguments.of(""),
Arguments.of(" ")
);
}
@ParameterizedTest
@MethodSource("provideEmptyOrNullStringsConstructor")
void testNullValue(String theStringValue) {
DecimalType nullDecimal = new DecimalType();
Assertions.assertNull(nullDecimal.getValue());
Assertions.assertNull(nullDecimal.asStringValue());
DecimalType anotherNullDecimal = new DecimalType(theStringValue);
Assertions.assertNull(anotherNullDecimal.getValue());
Assertions.assertNull(anotherNullDecimal.asStringValue());
Assertions.assertTrue(nullDecimal.equalsDeep(anotherNullDecimal));
Assertions.assertTrue(nullDecimal.equalsShallow(anotherNullDecimal));
}
}

View File

@ -33,6 +33,7 @@ import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import org.apache.commons.lang3.StringUtils;
import org.hl7.fhir.instance.model.api.IBaseDecimalDatatype;
import ca.uhn.fhir.model.api.annotation.DatatypeDef;
@ -79,7 +80,7 @@ public class DecimalType extends PrimitiveType<BigDecimal> implements Comparable
* Constructor
*/
public DecimalType(String theValue) {
setValue(new BigDecimal(theValue));
setValueAsString(theValue);
}
@Override
@ -149,6 +150,14 @@ public class DecimalType extends PrimitiveType<BigDecimal> implements Comparable
setValue(new BigDecimal(theValue));
}
public void setValueAsString(String theString) {
if (StringUtils.isBlank(theString)) {
setValue((BigDecimal) null);
} else {
setValue(new BigDecimal(theString));
}
}
/**
* Sets a new value using a long
*/

View File

@ -0,0 +1,75 @@
package org.hl7.fhir.dstu2016may.model;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
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.util.stream.Stream;
class DecimalTypeNullTest {
@Test
@DisplayName("Test null value toString()")
void testToString() {
DecimalType nullDecimal = new DecimalType();
System.out.println("Value -> " + nullDecimal);
}
@Test
@DisplayName("Test null value equalsDeep()")
void equalsDeep() {
DecimalType nullDecimal = new DecimalType();
DecimalType validDecimal = new DecimalType("3.14");
Assertions.assertFalse(nullDecimal.equalsDeep(validDecimal));
}
@Test
@DisplayName("Test null value equalsShallow()")
void equalsShallow() {
DecimalType nullDecimal = new DecimalType();
DecimalType validDecimal = new DecimalType("3.14");
Assertions.assertFalse(nullDecimal.equalsShallow(validDecimal));
}
@Test
@DisplayName("Test null value copy()")
void copy() {
DecimalType nullDecimal = new DecimalType();
DecimalType copyDecimal = nullDecimal.copy();
Assertions.assertNull(copyDecimal.getValue());
}
@Test
@DisplayName("Test null value typedCopy()")
void typedCopy() {
DecimalType nullDecimal = new DecimalType();
DecimalType copyDecimal = (DecimalType) nullDecimal.typedCopy();
Assertions.assertNull(copyDecimal.getValue());
}
public static Stream<Arguments> provideEmptyOrNullStringsConstructor() {
return Stream.of(
Arguments.of((String)null),
Arguments.of(""),
Arguments.of(" ")
);
}
@ParameterizedTest
@MethodSource("provideEmptyOrNullStringsConstructor")
void testNullValue(String theStringValue) {
DecimalType nullDecimal = new DecimalType();
Assertions.assertNull(nullDecimal.getValue());
Assertions.assertNull(nullDecimal.asStringValue());
DecimalType anotherNullDecimal = new DecimalType(theStringValue);
Assertions.assertNull(anotherNullDecimal.getValue());
Assertions.assertNull(anotherNullDecimal.asStringValue());
Assertions.assertTrue(nullDecimal.equalsDeep(anotherNullDecimal));
Assertions.assertTrue(nullDecimal.equalsShallow(anotherNullDecimal));
}
}

View File

@ -1,33 +1,33 @@
package org.hl7.fhir.dstu3.model;
/*
Copyright (c) 2011+, HL7, Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of HL7 nor the names of its contributors may be used to
endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
/*
Copyright (c) 2011+, HL7, Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of HL7 nor the names of its contributors may be used to
endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
@ -35,6 +35,7 @@ import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import org.apache.commons.lang3.StringUtils;
import org.hl7.fhir.instance.model.api.IBaseDecimalDatatype;
import ca.uhn.fhir.model.api.annotation.DatatypeDef;
@ -81,7 +82,7 @@ public class DecimalType extends PrimitiveType<BigDecimal> implements Comparable
* Constructor
*/
public DecimalType(String theValue) {
setValue(new BigDecimal(theValue));
setValueAsString(theValue);
}
@Override
@ -151,6 +152,14 @@ public class DecimalType extends PrimitiveType<BigDecimal> implements Comparable
setValue(BigDecimal.valueOf(theValue));
}
public void setValueAsString(String theString) {
if (StringUtils.isBlank(theString)) {
setValue((BigDecimal) null);
} else {
setValue(new BigDecimal(theString));
}
}
/**
* Sets a new value using a long
*/

View File

@ -3,6 +3,11 @@ package org.hl7.fhir.dstu3.model;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
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.util.stream.Stream;
class DecimalTypeNullTest {
@ -44,4 +49,27 @@ class DecimalTypeNullTest {
DecimalType copyDecimal = (DecimalType) nullDecimal.typedCopy();
Assertions.assertNull(copyDecimal.getValue());
}
public static Stream<Arguments> provideEmptyOrNullStringsConstructor() {
return Stream.of(
Arguments.of((String)null),
Arguments.of(""),
Arguments.of(" ")
);
}
@ParameterizedTest
@MethodSource("provideEmptyOrNullStringsConstructor")
void testNullValue(String theStringValue) {
DecimalType nullDecimal = new DecimalType();
Assertions.assertNull(nullDecimal.getValue());
Assertions.assertNull(nullDecimal.asStringValue());
DecimalType anotherNullDecimal = new DecimalType(theStringValue);
Assertions.assertNull(anotherNullDecimal.getValue());
Assertions.assertNull(anotherNullDecimal.asStringValue());
Assertions.assertTrue(nullDecimal.equalsDeep(anotherNullDecimal));
Assertions.assertTrue(nullDecimal.equalsShallow(anotherNullDecimal));
}
}

View File

@ -33,6 +33,7 @@ import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import org.apache.commons.lang3.StringUtils;
import org.hl7.fhir.instance.model.api.IBaseDecimalDatatype;
import ca.uhn.fhir.model.api.annotation.DatatypeDef;
@ -150,8 +151,13 @@ public class DecimalType extends PrimitiveType<BigDecimal> implements Comparable
}
public void setValueAsString(String theString) {
setValue(new BigDecimal(theString));
setRepresentation(theString);
if (StringUtils.isBlank(theString)) {
setValue((BigDecimal) null);
setRepresentation(null);
} else {
setValue(new BigDecimal(theString));
setRepresentation(theString);
}
}
/**

View File

@ -3,6 +3,11 @@ package org.hl7.fhir.r4.model;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
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.util.stream.Stream;
class DecimalTypeNullTest {
@ -44,4 +49,27 @@ class DecimalTypeNullTest {
DecimalType copyDecimal = (DecimalType) nullDecimal.typedCopy();
Assertions.assertNull(copyDecimal.getValue());
}
public static Stream<Arguments> provideEmptyOrNullStringsConstructor() {
return Stream.of(
Arguments.of((String)null),
Arguments.of(""),
Arguments.of(" ")
);
}
@ParameterizedTest
@MethodSource("provideEmptyOrNullStringsConstructor")
void testNullValue(String theStringValue) {
DecimalType nullDecimal = new DecimalType();
Assertions.assertNull(nullDecimal.getValue());
Assertions.assertNull(nullDecimal.asStringValue());
DecimalType anotherNullDecimal = new DecimalType(theStringValue);
Assertions.assertNull(anotherNullDecimal.getValue());
Assertions.assertNull(anotherNullDecimal.asStringValue());
Assertions.assertTrue(nullDecimal.equalsDeep(anotherNullDecimal));
Assertions.assertTrue(nullDecimal.equalsShallow(anotherNullDecimal));
}
}

View File

@ -33,6 +33,7 @@ import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import org.apache.commons.lang3.StringUtils;
import org.hl7.fhir.instance.model.api.IBaseDecimalDatatype;
import ca.uhn.fhir.model.api.annotation.DatatypeDef;
@ -150,8 +151,13 @@ public class DecimalType extends PrimitiveType<BigDecimal> implements Comparable
}
public void setValueAsString(String theString) {
setValue(new BigDecimal(theString));
setRepresentation(theString);
if (StringUtils.isBlank(theString)) {
setValue((BigDecimal) null);
setRepresentation(null);
} else {
setValue(new BigDecimal(theString));
setRepresentation(theString);
}
}
/**

View File

@ -1,9 +1,13 @@
package org.hl7.fhir.r4b.model;
import org.hl7.fhir.r4b.model.DecimalType;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
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.util.stream.Stream;
class DecimalTypeNullTest {
@ -45,4 +49,27 @@ class DecimalTypeNullTest {
DecimalType copyDecimal = (DecimalType) nullDecimal.typedCopy();
Assertions.assertNull(copyDecimal.getValue());
}
public static Stream<Arguments> provideEmptyOrNullStringsConstructor() {
return Stream.of(
Arguments.of((String)null),
Arguments.of(""),
Arguments.of(" ")
);
}
@ParameterizedTest
@MethodSource("provideEmptyOrNullStringsConstructor")
void testNullValue(String theStringValue) {
DecimalType nullDecimal = new DecimalType();
Assertions.assertNull(nullDecimal.getValue());
Assertions.assertNull(nullDecimal.asStringValue());
DecimalType anotherNullDecimal = new DecimalType(theStringValue);
Assertions.assertNull(anotherNullDecimal.getValue());
Assertions.assertNull(anotherNullDecimal.asStringValue());
Assertions.assertTrue(nullDecimal.equalsDeep(anotherNullDecimal));
Assertions.assertTrue(nullDecimal.equalsShallow(anotherNullDecimal));
}
}

View File

@ -12,14 +12,6 @@ import java.util.stream.Stream;
class DecimalTypeNullTest {
public static Stream<Arguments> provideEmptyOrNullStringsConstructor() {
return Stream.of(
Arguments.of((String)null),
Arguments.of(""),
Arguments.of(" ")
);
}
@Test
@DisplayName("Test null value toString()")
void testToString() {
@ -59,6 +51,14 @@ class DecimalTypeNullTest {
Assertions.assertNull(copyDecimal.getValue());
}
public static Stream<Arguments> provideEmptyOrNullStringsConstructor() {
return Stream.of(
Arguments.of((String)null),
Arguments.of(""),
Arguments.of(" ")
);
}
@ParameterizedTest
@MethodSource("provideEmptyOrNullStringsConstructor")
void testNullValue(String theStringValue) {