Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AB2D-6148] increase ab2d-fhir code coverage #396

Merged
merged 11 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 3 additions & 16 deletions ab2d-fhir/src/main/java/gov/cms/ab2d/fhir/ExtensionUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand All @@ -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;
}

Expand Down
11 changes: 10 additions & 1 deletion ab2d-fhir/src/main/java/gov/cms/ab2d/fhir/FhirVersion.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<? extends IBaseBundle> getBundleClass() {
try {
return (Class<? extends IBaseBundle>) Class.forName(this.getClassName("Bundle"));
Expand Down Expand Up @@ -173,4 +182,4 @@ public IBaseResource getErrorOutcome(String msg) {
public Object getPatientEnum() {
return patientEnum;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Extension> 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<Extension> extensions2 = id.getExtension();
Extension ex2 = extensions2.get(0);
assertNotNull(extensions2);
Expand All @@ -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();
Expand All @@ -56,4 +76,4 @@ void testPatientExt() {
referenceYear = ExtensionUtils.getReferenceYear(patient);
assertEquals(Calendar.getInstance().get(Calendar.YEAR), referenceYear);
}
}
}
18 changes: 18 additions & 0 deletions ab2d-fhir/src/test/java/gov/cms/ab2d/fhir/FhirVersionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -97,6 +98,23 @@ 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"));
}
coilysiren marked this conversation as resolved.
Show resolved Hide resolved

@Test
void testGetBundleClass() {
assertEquals(org.hl7.fhir.dstu3.model.Bundle.class, FhirVersion.STU3.getBundleClass());
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Loading