Skip to content

Commit

Permalink
[AB2D-6148] increase ab2d-fhir code coverage (#394)
Browse files Browse the repository at this point in the history
* checkpoint-kai-1718661872

* PatientIdentifierUtilsTest

* testNullInput

* VersionsTest

* VersionsTest

* PatientIdentifierUtilsTest

* PatientIdentifierUtilsTest

* PatientIdentifierUtilsTest

* PatientIdentifierUtilsTest

* PatientIdentifierUtilsTest

* PatientIdentifierUtilsTest

* PatientIdentifierUtilsTest

* version
  • Loading branch information
coilysiren authored Jun 20, 2024
1 parent 2be55f9 commit ef5f588
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 14 deletions.
7 changes: 1 addition & 6 deletions ab2d-fhir/src/main/java/gov/cms/ab2d/fhir/Versions.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,8 @@ static Object getObject(FhirVersion version, String className) {
Class clazz;
try {
clazz = Class.forName(fullName);
} catch (ClassNotFoundException e) {
log.error("Unable to create class " + fullName);
return null;
}
try {
object = clazz.getDeclaredConstructor(null).newInstance();
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
} catch (Exception e) {
log.error("Unable to create class " + fullName);
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.hl7.fhir.dstu3.model.Identifier;
import org.hl7.fhir.dstu3.model.Patient;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.instance.model.api.ICompositeType;
import org.junit.jupiter.api.Test;

import java.io.IOException;
Expand All @@ -20,10 +21,27 @@
import static gov.cms.ab2d.fhir.PatientIdentifier.HISTORIC_MBI;
import static gov.cms.ab2d.fhir.PatientIdentifier.MBI_ID;
import static gov.cms.ab2d.fhir.IdentifierUtils.CURRENCY_IDENTIFIER;
import static gov.cms.ab2d.fhir.IdentifierUtils.getIdentifiers;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;

class PatientIdentifierUtilsTest {

@Test
void testGetTypes() {
assertEquals(PatientIdentifier.MBI_ID, PatientIdentifier.Type.MBI.getSystem());
assertEquals(PatientIdentifier.Type.MBI, PatientIdentifier.Type.fromSystem(PatientIdentifier.MBI_ID));
assertEquals(null, PatientIdentifier.Type.fromSystem("does-not-exist"));
}

@Test
void testGetValueAsLong() {
PatientIdentifier patientIdentifier = new PatientIdentifier();
assertNull(patientIdentifier.getValueAsLong());

patientIdentifier.setValue("1234");
assertEquals(1234, patientIdentifier.getValueAsLong());
}

@Test
void testGetMbis() {
Patient patient = new Patient();
Expand All @@ -47,7 +65,7 @@ void testGetMbis() {
identifier3.addExtension(extension2);
patient.setIdentifier(List.of(identifier, identifier2, identifier3));

List<PatientIdentifier> identifiers = gov.cms.ab2d.fhir.IdentifierUtils.getIdentifiers(patient);
List<PatientIdentifier> identifiers = IdentifierUtils.getIdentifiers(patient);
assertEquals("mbi-1", IdentifierUtils.getCurrentMbi(identifiers).getValue());
Set<PatientIdentifier> historical = IdentifierUtils.getHistoricMbi(identifiers);
PatientIdentifier h = (PatientIdentifier) historical.toArray()[0];
Expand All @@ -59,7 +77,7 @@ void testGetMbis() {
void testStu3ExtractIds() throws IOException {
Bundle resource = (Bundle) extractBundle(FhirVersion.STU3, "data/stu3patients.json");
for (Bundle.BundleEntryComponent component : resource.getEntry()) {
List<PatientIdentifier> ids = getIdentifiers((Patient) component.getResource());
List<PatientIdentifier> ids = IdentifierUtils.getIdentifiers((Patient) component.getResource());
assertEquals(5, ids.size());
PatientIdentifier benId = IdentifierUtils.getBeneId(ids);
assertEquals("-19990000001101", benId.getValue());
Expand All @@ -75,15 +93,98 @@ void testStu3ExtractIds() throws IOException {
}
}

@Test
void testNullInputs() {
assertNull(IdentifierUtils.getIdentifiers(null));
}

@Test
void testEmptyPatients() {
Patient patient = new Patient();
assertTrue(IdentifierUtils.getIdentifiers(patient).isEmpty());
}

@Test
void testEmptyPatientIds() {
Patient patient = new Patient();
patient.setIdentifier(List.of());
assertTrue(IdentifierUtils.getIdentifiers(patient).isEmpty());
}

@Test
void testMockInput() {
assertNull(IdentifierUtils.getIdentifier(mock(ICompositeType.class)));
}

@Test
void testRealInput() {
Patient patient = new Patient();
Identifier identifier = new Identifier();
identifier.setSystem("https://bluebutton.cms.gov/resources/variables/bene_id");
identifier.setValue("test-1");
patient.setIdentifier(List.of(identifier));
assertNotNull(IdentifierUtils.getIdentifier(patient.getIdentifier().get(0)));
}

@Test
void testGetBeneId() {
PatientIdentifier patientIdentifier = new PatientIdentifier();
patientIdentifier.setType(PatientIdentifier.Type.BENE_ID);
patientIdentifier.setValue("test-1");
assertEquals("test-1", IdentifierUtils.getBeneId(List.of(patientIdentifier)).getValue());
}

@Test
void testGetBeneIdWrongType() {
PatientIdentifier patientIdentifier = new PatientIdentifier();
patientIdentifier.setType(PatientIdentifier.Type.MBI);
patientIdentifier.setValue("test-1");
assertNull(IdentifierUtils.getBeneId(List.of(patientIdentifier)));
}

@Test
void testGetCurrentMbiCurrent() {
PatientIdentifier patientIdentifier = new PatientIdentifier();
patientIdentifier.setType(PatientIdentifier.Type.MBI);
patientIdentifier.setValue("test-1");
patientIdentifier.setCurrency(PatientIdentifier.Currency.CURRENT);
assertEquals("test-1", IdentifierUtils.getCurrentMbi(List.of(patientIdentifier)).getValue());
}

@Test
void testGetCurrentMbiHistoric() {
PatientIdentifier patientIdentifier = new PatientIdentifier();
patientIdentifier.setType(PatientIdentifier.Type.MBI);
patientIdentifier.setValue("test-1");
patientIdentifier.setCurrency(PatientIdentifier.Currency.HISTORIC);
assertNull(IdentifierUtils.getCurrentMbi(List.of(patientIdentifier)));
}

@Test
void testGetCurrentMbiUnknown() {
PatientIdentifier patientIdentifier = new PatientIdentifier();
patientIdentifier.setType(PatientIdentifier.Type.MBI);
patientIdentifier.setValue("test-1");
patientIdentifier.setCurrency(PatientIdentifier.Currency.UNKNOWN);
assertEquals("test-1", IdentifierUtils.getCurrentMbi(List.of(patientIdentifier)).getValue());
}

@Test
void testGetCurrentMbiWrongType() {
PatientIdentifier patientIdentifier = new PatientIdentifier();
patientIdentifier.setType(PatientIdentifier.Type.BENE_ID);
patientIdentifier.setValue("test-1");
assertNull(IdentifierUtils.getCurrentMbi(List.of(patientIdentifier)));
}

@Test
void testR4ExtractIds() throws IOException {
List<String> beneIds = List.of("-19990000001101", "-19990000001102", "-19990000001103");
List<String> currentMbis = List.of("3S24A00AA00", "4S24A00AA00", "5S24A00AA00");
List<String> historicMbis = List.of();
org.hl7.fhir.r4.model.Bundle resource = (org.hl7.fhir.r4.model.Bundle) extractBundle(FhirVersion.R4, "data/r4patients.json");
for (org.hl7.fhir.r4.model.Bundle.BundleEntryComponent component : resource.getEntry()) {
org.hl7.fhir.r4.model.Patient patient = (org.hl7.fhir.r4.model.Patient) component.getResource();
List<PatientIdentifier> ids = getIdentifiers(patient);
List<PatientIdentifier> ids = IdentifierUtils.getIdentifiers(patient);
assertEquals(3, ids.size());

PatientIdentifier benId = IdentifierUtils.getBeneId(ids);
Expand Down Expand Up @@ -125,4 +226,4 @@ String getRawJson(String path) throws IOException {

return new String(sampleData.readAllBytes(), StandardCharsets.UTF_8);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package gov.cms.ab2d.fhir;

import static org.junit.jupiter.api.Assertions.assertThrows;

import org.junit.jupiter.api.Test;

class VersionNotSupportedTest {

@Test
void testConstructor() {
assertThrows(RuntimeException.class, () -> {
throw new VersionNotSupported("test");
});
}

}
18 changes: 17 additions & 1 deletion ab2d-fhir/src/test/java/gov/cms/ab2d/fhir/VersionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ void executeGetMethod() {
assertEquals("Patient/123", ((Reference) obj).getReference());
}

@Test
void executeGetMethodWithBadSetArgs() {
ExplanationOfBenefit eob = new ExplanationOfBenefit();
Versions.invokeSetMethod(eob, "setPatient", "broken", Reference.class);
Object obj = Versions.invokeGetMethod(eob, "getPatient");
assertEquals(null, ((Reference) obj).getReference());
}

@Test
void methodNotAvailableWhenInvokeGet() {
ExplanationOfBenefit eob = new ExplanationOfBenefit();
Expand Down Expand Up @@ -61,6 +69,11 @@ void executeSubObject() {
assertEquals(org.hl7.fhir.r4.model.OperationOutcome.OperationOutcomeIssueComponent.class, obj.getClass());
}

@Test
void testInvalidClass() {
assertNull(Versions.instantiateClass(STU3, "does-not-exist", "does-not-exist"));
}

@Test
void testInvalidVersion() {
assertNull(Versions.getObject(STU3, "Bogus"));
Expand All @@ -72,6 +85,9 @@ void testInvalidVersion() {
void executeInstantiateSimpleEnum() {
Object obj = Versions.instantiateEnum(R4, "Enumerations", "ResourceType", "PATIENT");
assertEquals(Enumerations.ResourceType.PATIENT, obj);

obj = Versions.instantiateEnum(R4, "Enumerations", "FAKE", "PATIENT");
assertNull(obj);
}

@Test
Expand Down Expand Up @@ -164,4 +180,4 @@ void testOutcomeToJSON() {
final String payload = STU3.outcomePrettyToJSON(oo);
assertNotNull(payload);
}
}
}
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.4'
fhirVersion='1.2.5'
bfdVersion='2.2.0'
aggregatorVersion='1.3.4'
filtersVersion='1.9.4'
Expand Down

0 comments on commit ef5f588

Please sign in to comment.