From fce80bb9b10f14ee5ffba4da709cf1b24cd7f295 Mon Sep 17 00:00:00 2001 From: dotasek Date: Mon, 28 Nov 2022 18:28:34 -0500 Subject: [PATCH] Fix unnecessary switch. Refactor tests. --- .../resources30_40/Immunization30_40.java | 19 ++++--- .../conv30_40/Immunization30_40Test.java | 50 +++++++++++++------ ...> immunization_30_completed_notGiven.json} | 0 .../immunization_40-completed-notGiven.json | 29 +++++++++++ ...=> immunization_40-not-done-notGiven.json} | 0 ..._40.json => immunization_40-not-done.json} | 0 6 files changed, 77 insertions(+), 21 deletions(-) rename org.hl7.fhir.convertors/src/test/resources/{immunization_30.json => immunization_30_completed_notGiven.json} (100%) create mode 100644 org.hl7.fhir.convertors/src/test/resources/immunization_40-completed-notGiven.json rename org.hl7.fhir.convertors/src/test/resources/{immunization_40-converted.json => immunization_40-not-done-notGiven.json} (100%) rename org.hl7.fhir.convertors/src/test/resources/{immunization_40.json => immunization_40-not-done.json} (100%) diff --git a/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/conv30_40/resources30_40/Immunization30_40.java b/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/conv30_40/resources30_40/Immunization30_40.java index e04fee0a4..57b49a902 100644 --- a/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/conv30_40/resources30_40/Immunization30_40.java +++ b/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/conv30_40/resources30_40/Immunization30_40.java @@ -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; diff --git a/org.hl7.fhir.convertors/src/test/java/org/hl7/fhir/convertors/conv30_40/Immunization30_40Test.java b/org.hl7.fhir.convertors/src/test/java/org/hl7/fhir/convertors/conv30_40/Immunization30_40Test.java index 6eff9d1c6..3fe71fff9 100644 --- a/org.hl7.fhir.convertors/src/test/java/org/hl7/fhir/convertors/conv30_40/Immunization30_40Test.java +++ b/org.hl7.fhir.convertors/src/test/java/org/hl7/fhir/convertors/conv30_40/Immunization30_40Test.java @@ -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 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 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 { + + } } diff --git a/org.hl7.fhir.convertors/src/test/resources/immunization_30.json b/org.hl7.fhir.convertors/src/test/resources/immunization_30_completed_notGiven.json similarity index 100% rename from org.hl7.fhir.convertors/src/test/resources/immunization_30.json rename to org.hl7.fhir.convertors/src/test/resources/immunization_30_completed_notGiven.json diff --git a/org.hl7.fhir.convertors/src/test/resources/immunization_40-completed-notGiven.json b/org.hl7.fhir.convertors/src/test/resources/immunization_40-completed-notGiven.json new file mode 100644 index 000000000..c8bc0ef2f --- /dev/null +++ b/org.hl7.fhir.convertors/src/test/resources/immunization_40-completed-notGiven.json @@ -0,0 +1,29 @@ +{ + "resourceType": "Immunization", + "id": "notGiven", + "text": { + "status": "generated", + "div": "
dummyDiv
" + }, + "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 +} diff --git a/org.hl7.fhir.convertors/src/test/resources/immunization_40-converted.json b/org.hl7.fhir.convertors/src/test/resources/immunization_40-not-done-notGiven.json similarity index 100% rename from org.hl7.fhir.convertors/src/test/resources/immunization_40-converted.json rename to org.hl7.fhir.convertors/src/test/resources/immunization_40-not-done-notGiven.json diff --git a/org.hl7.fhir.convertors/src/test/resources/immunization_40.json b/org.hl7.fhir.convertors/src/test/resources/immunization_40-not-done.json similarity index 100% rename from org.hl7.fhir.convertors/src/test/resources/immunization_40.json rename to org.hl7.fhir.convertors/src/test/resources/immunization_40-not-done.json