diff --git a/ab2d-fhir/src/main/java/gov/cms/ab2d/fhir/ExtensionUtils.java b/ab2d-fhir/src/main/java/gov/cms/ab2d/fhir/ExtensionUtils.java index 2bca6151..0dc99d67 100644 --- a/ab2d-fhir/src/main/java/gov/cms/ab2d/fhir/ExtensionUtils.java +++ b/ab2d-fhir/src/main/java/gov/cms/ab2d/fhir/ExtensionUtils.java @@ -39,11 +39,7 @@ public static void addExtension(IBaseResource resource, IBase extension, FhirVer if (resource == null || extension == null) { return; } - try { - Versions.invokeSetMethod(resource, "addExtension", extension, Class.forName(version.getClassName(EXTENSION_CLASSNAME))); - } catch (Exception ex) { - log.error("Unable to add Extension"); - } + Versions.invokeSetMethod(resource, "addExtension", extension, version.getClassFromName(EXTENSION_CLASSNAME)); } /** @@ -64,21 +60,12 @@ public static IBase createMbiExtension(String mbi, boolean current, FhirVersion Object currencyExtension = Versions.getObject(version, EXTENSION_CLASSNAME); Versions.invokeSetMethod(currencyExtension, "setUrl", CURRENCY_IDENTIFIER, String.class); - try { - Versions.invokeSetMethod(currencyExtension, SET_VALUE_METHOD_NAME, coding, Class.forName(version.getClassName("Type"))); - } catch (Exception ex) { - log.error("Unable to setValue"); - } - + Versions.invokeSetMethod(currencyExtension, SET_VALUE_METHOD_NAME, coding, version.getClassFromName("Type")); Versions.invokeSetMethod(identifier, "setExtension", List.of(currencyExtension), List.class); Object ext = Versions.getObject(version, EXTENSION_CLASSNAME); Versions.invokeSetMethod(ext, "setUrl", ID_EXT, String.class); - try { - Versions.invokeSetMethod(ext, SET_VALUE_METHOD_NAME, identifier, Class.forName(version.getClassName("Type"))); - } catch (Exception ex) { - log.error("Unable to setValue"); - } + Versions.invokeSetMethod(ext, SET_VALUE_METHOD_NAME, identifier, version.getClassFromName("Type")); return (IBase) ext; } diff --git a/ab2d-fhir/src/main/java/gov/cms/ab2d/fhir/FhirVersion.java b/ab2d-fhir/src/main/java/gov/cms/ab2d/fhir/FhirVersion.java index eec37f6c..1ad3cc95 100644 --- a/ab2d-fhir/src/main/java/gov/cms/ab2d/fhir/FhirVersion.java +++ b/ab2d-fhir/src/main/java/gov/cms/ab2d/fhir/FhirVersion.java @@ -75,6 +75,15 @@ public String getClassName(String name) { return this.classLocation + "." + name; } + public Class getClassFromName(String name) { + try { + return Class.forName(getClassName(name)); + } catch (Exception e) { + log.error(String.format("Unable to create a class from name: %s", name), e); + return null; + } + } + public Class getBundleClass() { try { return (Class) Class.forName(this.getClassName("Bundle")); @@ -173,4 +182,4 @@ public IBaseResource getErrorOutcome(String msg) { public Object getPatientEnum() { return patientEnum; } -} \ No newline at end of file +} diff --git a/ab2d-fhir/src/test/java/gov/cms/ab2d/fhir/ExtensionUtilsTest.java b/ab2d-fhir/src/test/java/gov/cms/ab2d/fhir/ExtensionUtilsTest.java index 9471fa45..5f8baa5d 100644 --- a/ab2d-fhir/src/test/java/gov/cms/ab2d/fhir/ExtensionUtilsTest.java +++ b/ab2d-fhir/src/test/java/gov/cms/ab2d/fhir/ExtensionUtilsTest.java @@ -19,23 +19,25 @@ import static org.junit.jupiter.api.Assertions.*; class ExtensionUtilsTest { + @Test - void testExtensions() { - String mbiIdCurrent = "MBI1"; - String mbiIdPrevious = "MBI1"; + void testAddExtension() { + String mbiId = "MBI1"; org.hl7.fhir.dstu3.model.ExplanationOfBenefit eob = new org.hl7.fhir.dstu3.model.ExplanationOfBenefit(); - IBase extension = ExtensionUtils.createMbiExtension(mbiIdCurrent, true, STU3); - ExtensionUtils.addExtension(eob, extension, STU3); + IBase extension1 = ExtensionUtils.createMbiExtension(mbiId, true, STU3); + IBase extension2 = ExtensionUtils.createMbiExtension(mbiId, false, STU3); + ExtensionUtils.addExtension(eob, extension1, STU3); + ExtensionUtils.addExtension(eob, extension2, STU3); List extensions = eob.getExtension(); assertNotNull(extensions); - assertEquals(1, extensions.size()); + assertEquals(2, extensions.size()); Extension ex = extensions.get(0); assertEquals(ID_EXT, ex.getUrl()); Identifier id = (Identifier) ex.getValue(); assertNotNull(id); assertEquals(MBI_ID, id.getSystem()); - assertEquals(mbiIdCurrent, id.getValue()); + assertEquals(mbiId, id.getValue()); List extensions2 = id.getExtension(); Extension ex2 = extensions2.get(0); assertNotNull(extensions2); @@ -44,6 +46,24 @@ void testExtensions() { assertEquals(CURRENT_MBI, c.getCode()); } + @Test + void testAddExtensionInvalidCases() { + String mbiId = "MBI1"; + org.hl7.fhir.dstu3.model.ExplanationOfBenefit eob = new org.hl7.fhir.dstu3.model.ExplanationOfBenefit(); + IBase extension = ExtensionUtils.createMbiExtension(mbiId, true, STU3); + + assertTrue(eob.getExtension().isEmpty()); + ExtensionUtils.addExtension(null, extension, STU3); + assertTrue(eob.getExtension().isEmpty()); + ExtensionUtils.addExtension(eob, null, STU3); + assertTrue(eob.getExtension().isEmpty()); + ExtensionUtils.addExtension(null, null, STU3); + assertTrue(eob.getExtension().isEmpty()); + + ExtensionUtils.addExtension(eob, extension, STU3); + assertFalse(eob.getExtension().isEmpty()); + } + @Test void testPatientExt() { org.hl7.fhir.dstu3.model.Patient patient = new org.hl7.fhir.dstu3.model.Patient(); @@ -56,4 +76,4 @@ void testPatientExt() { referenceYear = ExtensionUtils.getReferenceYear(patient); assertEquals(Calendar.getInstance().get(Calendar.YEAR), referenceYear); } -} \ No newline at end of file +} diff --git a/ab2d-fhir/src/test/java/gov/cms/ab2d/fhir/FhirVersionTest.java b/ab2d-fhir/src/test/java/gov/cms/ab2d/fhir/FhirVersionTest.java index e5e0ee57..fe7682a7 100644 --- a/ab2d-fhir/src/test/java/gov/cms/ab2d/fhir/FhirVersionTest.java +++ b/ab2d-fhir/src/test/java/gov/cms/ab2d/fhir/FhirVersionTest.java @@ -3,6 +3,7 @@ import java.time.OffsetDateTime; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotEquals; @@ -97,6 +98,27 @@ void testGetClassName() { ReflectionTestUtils.setField(FhirVersion.R4, "classLocation", classLocation); } + @Test + void testGetClassFromName() { + assertEquals( + org.hl7.fhir.dstu3.model.Patient.class, + FhirVersion.STU3.getClassFromName("Patient") + ); + assertEquals( + org.hl7.fhir.r4.model.Patient.class, + FhirVersion.R4.getClassFromName("Patient") + ); + + assertDoesNotThrow(() -> { + FhirVersion.R4.getClassFromName("doesNotExist"); + }); + assertNull(FhirVersion.R4.getClassFromName("doesNotExist")); + assertDoesNotThrow(() -> { + FhirVersion.STU3.getClassFromName("doesNotExist"); + }); + assertNull(FhirVersion.STU3.getClassFromName("doesNotExist")); + } + @Test void testGetBundleClass() { assertEquals(org.hl7.fhir.dstu3.model.Bundle.class, FhirVersion.STU3.getBundleClass()); diff --git a/build.gradle b/build.gradle index 3baaca90..7783d271 100644 --- a/build.gradle +++ b/build.gradle @@ -22,7 +22,7 @@ ext { : System.getenv()['ARTIFACTORY_PASSWORD'] // AB2D libraries - fhirVersion='1.2.5' + fhirVersion='1.2.6' bfdVersion='2.2.0' aggregatorVersion='1.3.4' filtersVersion='1.9.4'