Fix unnecessary switch. Refactor tests.

This commit is contained in:
dotasek 2022-11-28 18:28:34 -05:00
parent 9e6700d9a9
commit fce80bb9b1
6 changed files with 77 additions and 21 deletions

View File

@ -15,8 +15,12 @@ import org.hl7.fhir.exceptions.FHIRException;
import org.hl7.fhir.r4.model.Extension;
import org.hl7.fhir.r4.model.Immunization;
public class Immunization30_40 {
public static final String NOT_GIVEN_EXTENSION_URL = "http://hl7.org/fhir/3.0/StructureDefinition/extension-Immunization.notGiven";
public static org.hl7.fhir.r4.model.Immunization convertImmunization(org.hl7.fhir.dstu3.model.Immunization src) throws FHIRException {
if (src == null)
return null;
@ -68,7 +72,7 @@ public class Immunization30_40 {
public static org.hl7.fhir.r4.model.Extension getExtensionForNotGiven(boolean notGiven) {
org.hl7.fhir.r4.model.Extension extension = new org.hl7.fhir.r4.model.Extension();
extension.setUrl("http://hl7.org/fhir/3.0/StructureDefinition/extension-Immunization.notGiven");
extension.setUrl(NOT_GIVEN_EXTENSION_URL);
extension.setValue(new org.hl7.fhir.r4.model.BooleanType(notGiven));
return extension;
}
@ -82,10 +86,13 @@ public class Immunization30_40 {
tgt.addIdentifier(Identifier30_40.convertIdentifier(t));
if (src.hasStatus()) {
tgt.setStatusElement(convertImmunizationStatus(src.getStatusElement()));
switch (src.getStatusElement().getValue()) {
case NOTDONE: tgt.setNotGivenElement(new BooleanType(true));
break;
}
if (src.getStatusElement().getValue() == Immunization.ImmunizationStatus.NOTDONE)
tgt.setNotGivenElement(new BooleanType(true));
}
if (src.hasExtension(NOT_GIVEN_EXTENSION_URL)) {
Extension notGivenExtension = src.getExtensionByUrl(NOT_GIVEN_EXTENSION_URL);
if (notGivenExtension.hasValue() && notGivenExtension.getValueAsPrimitive() instanceof org.hl7.fhir.r4.model.BooleanType)
tgt.setNotGivenElement(new org.hl7.fhir.dstu3.model.BooleanType());
}
if (src.hasVaccineCode())
tgt.setVaccineCode(CodeableConcept30_40.convertCodeableConcept(src.getVaccineCode()));
@ -154,8 +161,6 @@ public class Immunization30_40 {
ConversionContext30_40.INSTANCE.getVersionConvertor_30_40().copyElement(src, tgt);
switch (src.getValue()) {
case COMPLETED:
tgt.setValue(org.hl7.fhir.dstu3.model.Immunization.ImmunizationStatus.COMPLETED);
break;
case NOTDONE:
tgt.setValue(org.hl7.fhir.dstu3.model.Immunization.ImmunizationStatus.COMPLETED);
break;

View File

@ -2,45 +2,67 @@ package org.hl7.fhir.convertors.conv30_40;
import org.hl7.fhir.convertors.advisors.impl.BaseAdvisor_30_40;
import org.hl7.fhir.convertors.factory.VersionConvertorFactory_30_40;
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.io.IOException;
import java.io.InputStream;
import java.util.stream.Stream;
public class Immunization30_40Test {
@Test
@DisplayName("Test r4 -> dstu3 immunization conversion.")
public void test1() throws IOException {
InputStream r4_input = this.getClass().getResourceAsStream("/immunization_40.json");
InputStream dstu3_expected_output = this.getClass().getResourceAsStream("/immunization_30.json");
private org.hl7.fhir.r4.formats.JsonParser r4_parser = new org.hl7.fhir.r4.formats.JsonParser();
private org.hl7.fhir.dstu3.formats.JsonParser dstu3_parser = new org.hl7.fhir.dstu3.formats.JsonParser();
org.hl7.fhir.r4.model.Immunization r4_actual = (org.hl7.fhir.r4.model.Immunization) new org.hl7.fhir.r4.formats.JsonParser().parse(r4_input);
public static Stream<Arguments> getR4toDSTU3Arguments() {
return Stream.of(
Arguments.of("test1", "/immunization_40-not-done.json", "/immunization_30_completed_notGiven.json")
);
}
@ParameterizedTest(name = "Test r4 -> dstu3 immunization conversions {0}")
@MethodSource("getR4toDSTU3Arguments")
public void test1(String testName, String r4_input_resource, String dstu3_expected_output_resource) throws IOException {
InputStream r4_input = this.getClass().getResourceAsStream(r4_input_resource);
InputStream dstu3_expected_output = this.getClass().getResourceAsStream(dstu3_expected_output_resource);
org.hl7.fhir.r4.model.Immunization r4_actual = (org.hl7.fhir.r4.model.Immunization) r4_parser.parse(r4_input);
org.hl7.fhir.dstu3.model.Resource dstu3_conv = VersionConvertorFactory_30_40.convertResource(r4_actual, new BaseAdvisor_30_40(false));
org.hl7.fhir.dstu3.formats.JsonParser dstu3_parser = new org.hl7.fhir.dstu3.formats.JsonParser();
org.hl7.fhir.dstu3.model.Resource dstu3_expected = dstu3_parser.parse(dstu3_expected_output);
Assertions.assertTrue(dstu3_expected.equalsDeep(dstu3_conv),
"Failed comparing\n" + dstu3_parser.composeString(dstu3_expected) + "\nand\n" + dstu3_parser.composeString(dstu3_conv));
}
@Test
@DisplayName("Test dstu3 -> r4 immunization conversion.")
public void test2() throws IOException {
InputStream dstu3_input = this.getClass().getResourceAsStream("/immunization_30.json");
InputStream r4_expected_output = this.getClass().getResourceAsStream("/immunization_40-converted.json");
public static Stream<Arguments> getDSTU3toR4Arguments() {
return Stream.of(
Arguments.of("test1", "/immunization_30_completed_notGiven.json", "/immunization_40-not-done-notGiven.json")
);
}
org.hl7.fhir.dstu3.model.Immunization dstu3_actual = (org.hl7.fhir.dstu3.model.Immunization) new org.hl7.fhir.dstu3.formats.JsonParser().parse(dstu3_input);
@ParameterizedTest(name = "Test dstu3 -> r4 immunization conversions {0}")
@MethodSource("getDSTU3toR4Arguments")
public void test2(String testName, String dstu3_input_resource, String r4_expected_output_resource) throws IOException {
InputStream dstu3_input = this.getClass().getResourceAsStream(dstu3_input_resource);
org.hl7.fhir.dstu3.model.Immunization dstu3_actual = (org.hl7.fhir.dstu3.model.Immunization) dstu3_parser.parse(dstu3_input);
org.hl7.fhir.r4.model.Resource r4_conv = VersionConvertorFactory_30_40.convertResource(dstu3_actual, new BaseAdvisor_30_40(false));
org.hl7.fhir.r4.formats.JsonParser r4_parser = new org.hl7.fhir.r4.formats.JsonParser();
InputStream r4_expected_output = this.getClass().getResourceAsStream(r4_expected_output_resource);
org.hl7.fhir.r4.model.Resource r4_expected = r4_parser.parse(r4_expected_output);
Assertions.assertTrue(r4_expected.equalsDeep(r4_conv),
"Failed comparing\n" + r4_parser.composeString(r4_expected) + "\nand\n" + r4_parser.composeString(r4_conv));
}
@Test
@DisplayName("")
public void test3() throws IOException {
}
}

View File

@ -0,0 +1,29 @@
{
"resourceType": "Immunization",
"id": "notGiven",
"text": {
"status": "generated",
"div": "<div xmlns=\"http://www.w3.org/1999/xhtml\">dummyDiv</div>"
},
"extension":[
{
"url":"http://hl7.org/fhir/3.0/StructureDefinition/extension-Immunization.notGiven",
"valueBoolean":true
}
],
"status": "completed",
"vaccineCode": {
"coding": [
{
"system": "http://hl7.org/fhir/sid/cvx",
"code": "01",
"display": "DTP"
}
]
},
"patient": {
"reference": "Patient/example"
},
"occurrenceDateTime": "2013-01-10",
"primarySource": true
}