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-6101] Address Critical Code Smell - Reduce cognitive method compleixty #403

Merged
merged 13 commits into from
Jul 22, 2024
66 changes: 42 additions & 24 deletions ab2d-fhir/src/main/java/gov/cms/ab2d/fhir/IdentifierUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.hl7.fhir.instance.model.api.IBaseExtension;
import org.hl7.fhir.instance.model.api.ICompositeType;
import org.hl7.fhir.instance.model.api.IDomainResource;
import org.hl7.fhir.dstu3.model.Coding;

import java.util.ArrayList;
import java.util.Date;
Expand Down Expand Up @@ -206,41 +207,58 @@ private static PatientIdentifier.Currency getCurrencyMbiStandard(ICompositeType
return getCurrencyFromTypeCodingExtension(identifier);
}

private static PatientIdentifier.Currency getCurrencyFromTypeCodingExtension(ICompositeType identifier) {
public static PatientIdentifier.Currency getCurrencyFromTypeCodingExtension(ICompositeType identifier) {
Object type = Versions.invokeGetMethod(identifier, "getType");
if (type == null) {
return PatientIdentifier.Currency.UNKNOWN;
}
List vals = (List) Versions.invokeGetMethod(type, "getCoding");
if (vals == null || vals.isEmpty()) {
List<Coding> vals = (List) Versions.invokeGetMethod(type, "getCoding");

if (!checkTypeAndCodingExists(type, vals)) {
return PatientIdentifier.Currency.UNKNOWN;
}

Object val = vals.get(0);
String codeSystem = (String) Versions.invokeGetMethod(val, GET_SYSTEM);
String codeValue = (String) Versions.invokeGetMethod(val, GET_CODE);
if (codeSystem != null && codeSystem.equalsIgnoreCase(MBI_ID_R4) && ("MB".equalsIgnoreCase(codeValue) || "MC".equalsIgnoreCase(codeValue))) {
List extensions = (List) Versions.invokeGetMethod(val, "getExtension");
if (extensions != null && extensions.size() > 0) {
Object extension = extensions.get(0);
String url = (String) Versions.invokeGetMethod(extension, "getUrl");
if (url != null && url.equalsIgnoreCase(CURRENCY_IDENTIFIER)) {
Object currValue = Versions.invokeGetMethod(extension, GET_VALUE);
String extValueSystem = (String) Versions.invokeGetMethod(currValue, GET_SYSTEM);
if (CURRENCY_IDENTIFIER.equalsIgnoreCase(extValueSystem)) {
String currValueCode = (String) Versions.invokeGetMethod(currValue, GET_CODE);
if (CURRENT_MBI.equalsIgnoreCase(currValueCode)) {
return PatientIdentifier.Currency.CURRENT;
}
if (HISTORIC_MBI.equalsIgnoreCase(currValueCode)) {
return PatientIdentifier.Currency.HISTORIC;
}
}
}
if (!checkCodingIsValid(codeSystem, codeValue)) {
return PatientIdentifier.Currency.UNKNOWN;
}

List extensions = (List) Versions.invokeGetMethod(val, "getExtension");
Rwolfe-Nava marked this conversation as resolved.
Show resolved Hide resolved
if (extensions == null || extensions.isEmpty()) {
Rwolfe-Nava marked this conversation as resolved.
Show resolved Hide resolved
return PatientIdentifier.Currency.UNKNOWN;
}

Object extension = extensions.get(0);
String url = (String) Versions.invokeGetMethod(extension, "getUrl");
if (!checkCurrencyUrlIsValid(url)) {
return PatientIdentifier.Currency.UNKNOWN;
}

Object currValue = Versions.invokeGetMethod(extension, GET_VALUE);
String extValueSystem = (String) Versions.invokeGetMethod(currValue, GET_SYSTEM);
if (CURRENCY_IDENTIFIER.equalsIgnoreCase(extValueSystem)) {
String currValueCode = (String) Versions.invokeGetMethod(currValue, GET_CODE);
if (CURRENT_MBI.equalsIgnoreCase(currValueCode)) {
return PatientIdentifier.Currency.CURRENT;
}
if (HISTORIC_MBI.equalsIgnoreCase(currValueCode)) {
return PatientIdentifier.Currency.HISTORIC;
}
}
return PatientIdentifier.Currency.UNKNOWN;
}

public static boolean checkTypeAndCodingExists(Object type, List<Coding> vals) {
return (type != null && vals != null && !vals.isEmpty());
Rwolfe-Nava marked this conversation as resolved.
Show resolved Hide resolved
}

public static boolean checkCodingIsValid(String codeSystem, String codeValue) {
return ((codeSystem != null && codeSystem.equalsIgnoreCase(MBI_ID_R4)) && ("MB".equalsIgnoreCase(codeValue) || "MC".equalsIgnoreCase(codeValue)));
}

public static boolean checkCurrencyUrlIsValid(String url) {
return (url != null && url.equalsIgnoreCase(CURRENCY_IDENTIFIER));
}

/**
* If the period has a value for the identifier, return the information from that, otherwise UNKNOWN
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static gov.cms.ab2d.fhir.PatientIdentifier.Currency.HISTORIC;
import static gov.cms.ab2d.fhir.PatientIdentifier.HISTORIC_MBI;
import static gov.cms.ab2d.fhir.PatientIdentifier.MBI_ID;
import static gov.cms.ab2d.fhir.PatientIdentifier.MBI_ID_R4;
import static gov.cms.ab2d.fhir.IdentifierUtils.CURRENCY_IDENTIFIER;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -177,6 +178,225 @@ void testGetCurrentMbiWrongType() {
assertNull(IdentifierUtils.getCurrentMbi(List.of(patientIdentifier)));
}

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

@Test
void testGetCurrencyFromTypeCodingExtensionReturnsCurrent() {
Patient patient = new Patient();
Rwolfe-Nava marked this conversation as resolved.
Show resolved Hide resolved
Identifier identifier = new Identifier();
identifier.setSystem(MBI_ID);

Coding coding = new Coding();
coding.setSystem(MBI_ID_R4);
coding.setCode("MB");
identifier.getType().addCoding(coding);

identifier.setValue("mbi-1");

Extension extension = new Extension();
extension.setUrl(CURRENCY_IDENTIFIER);
Coding extCoding = new Coding();
extCoding.setSystem(CURRENCY_IDENTIFIER);
extCoding.setCode(CURRENT_MBI);
extension.setValue(extCoding);
identifier.getType().getCoding().get(0).addExtension(extension);

patient.setIdentifier(List.of(identifier));

assertEquals(PatientIdentifier.Currency.CURRENT, IdentifierUtils.getCurrencyFromTypeCodingExtension(patient.getIdentifier().get(0)));
}

@Test
void testGetCurrencyFromTypeCodingExtensionReturnsHistoric() {
Patient patient = new Patient();
Identifier identifier = new Identifier();
identifier.setSystem(MBI_ID);

Coding coding = new Coding();
coding.setSystem(MBI_ID_R4);
coding.setCode("MB");
identifier.getType().addCoding(coding);

identifier.setValue("mbi-1");

Extension extension = new Extension();
extension.setUrl(CURRENCY_IDENTIFIER);
Coding extCoding = new Coding();
extCoding.setSystem(CURRENCY_IDENTIFIER);
extCoding.setCode(HISTORIC_MBI);
extension.setValue(extCoding);
identifier.getType().getCoding().get(0).addExtension(extension);

patient.setIdentifier(List.of(identifier));

assertEquals(PatientIdentifier.Currency.HISTORIC, IdentifierUtils.getCurrencyFromTypeCodingExtension(patient.getIdentifier().get(0)));
}

@Test
void testGetCurrencyFromTypeCodingExtensionReturnsUnknown() {
Patient patient = new Patient();
Identifier identifier = new Identifier();
identifier.setSystem(MBI_ID);

Coding coding = new Coding();
coding.setSystem(MBI_ID_R4);
coding.setCode("MB");
identifier.getType().addCoding(coding);

identifier.setValue("mbi-1");

Extension extension = new Extension();
extension.setUrl(CURRENCY_IDENTIFIER);
Coding extCoding = new Coding();
extCoding.setSystem(CURRENCY_IDENTIFIER);
extCoding.setCode("unknown");
extension.setValue(extCoding);
identifier.getType().getCoding().get(0).addExtension(extension);

patient.setIdentifier(List.of(identifier));

assertEquals(PatientIdentifier.Currency.UNKNOWN, IdentifierUtils.getCurrencyFromTypeCodingExtension(patient.getIdentifier().get(0)));
}

@Test
void testGetCurrencyFromTypeCodingExtensionReturnsUnknownWhenCodingEmpty() {
Patient patient = new Patient();
Identifier identifier = new Identifier();
identifier.setSystem(MBI_ID);

patient.setIdentifier(List.of(identifier));

assertEquals(PatientIdentifier.Currency.UNKNOWN, IdentifierUtils.getCurrencyFromTypeCodingExtension(patient.getIdentifier().get(0)));
}

@Test
void testGetCurrencyFromTypeCodingExtensionReturnsUnknownWhenCodingInvalid() {
Patient patient = new Patient();
Identifier identifier = new Identifier();
identifier.setSystem(MBI_ID);

Coding coding = new Coding();
coding.setSystem("invalid_system");
coding.setCode("MB");
identifier.getType().addCoding(coding);

identifier.setValue("mbi-1");

Extension extension = new Extension();
extension.setUrl(CURRENCY_IDENTIFIER);
Coding extCoding = new Coding();
extCoding.setSystem(CURRENCY_IDENTIFIER);
extCoding.setCode(CURRENT_MBI);
extension.setValue(extCoding);
identifier.getType().getCoding().get(0).addExtension(extension);

patient.setIdentifier(List.of(identifier));

assertEquals(PatientIdentifier.Currency.UNKNOWN, IdentifierUtils.getCurrencyFromTypeCodingExtension(patient.getIdentifier().get(0)));

coding.setSystem(MBI_ID_R4);
coding.setCode("invalid_code");
assertEquals(PatientIdentifier.Currency.UNKNOWN, IdentifierUtils.getCurrencyFromTypeCodingExtension(patient.getIdentifier().get(0)));
}

@Test
void testGetCurrencyFromTypeCodingExtensionReturnsUnknownWhenUrlNull() {
Patient patient = new Patient();
Identifier identifier = new Identifier();
identifier.setSystem(MBI_ID);

Coding coding = new Coding();
coding.setSystem(MBI_ID_R4);
coding.setCode("MB");
identifier.getType().addCoding(coding);

identifier.setValue("mbi-1");

Extension extension = new Extension();
extension.setUrl(null);
Coding extCoding = new Coding();
extCoding.setSystem(CURRENCY_IDENTIFIER);
extCoding.setCode(CURRENT_MBI);
extension.setValue(extCoding);
identifier.getType().getCoding().get(0).addExtension(extension);

patient.setIdentifier(List.of(identifier));

assertEquals(PatientIdentifier.Currency.UNKNOWN, IdentifierUtils.getCurrencyFromTypeCodingExtension(patient.getIdentifier().get(0)));
}

@Test
void testGetCurrencyFromTypeCodingExtensionReturnsUnknownWhenUrlInvalid() {
Patient patient = new Patient();
Identifier identifier = new Identifier();
identifier.setSystem(MBI_ID);

Coding coding = new Coding();
coding.setSystem(MBI_ID_R4);
coding.setCode("MB");
identifier.getType().addCoding(coding);

identifier.setValue("mbi-1");

Extension extension = new Extension();
extension.setUrl("invalid_url");
Coding extCoding = new Coding();
extCoding.setSystem(CURRENCY_IDENTIFIER);
extCoding.setCode(CURRENT_MBI);
extension.setValue(extCoding);
identifier.getType().getCoding().get(0).addExtension(extension);

patient.setIdentifier(List.of(identifier));

assertEquals(PatientIdentifier.Currency.UNKNOWN, IdentifierUtils.getCurrencyFromTypeCodingExtension(patient.getIdentifier().get(0)));
}

@Test
void testReturnsFalseIfCodingNotExist() {
PatientIdentifier patientIdentifier = new PatientIdentifier();
patientIdentifier.setType(PatientIdentifier.Type.MBI);
patientIdentifier.setValue("test-1");

Object type = Versions.invokeGetMethod(patientIdentifier, "getType");
List vals = (List) Versions.invokeGetMethod(type, "getCode");

assertFalse(IdentifierUtils.checkTypeAndCodingExists(type, vals));
}

@Test
void testReturnsFalseIfCodingInvalid() {
String codeSystem = "invalid_system";
Rwolfe-Nava marked this conversation as resolved.
Show resolved Hide resolved
String codeValue = "invalid_value";
assertFalse(IdentifierUtils.checkCodingIsValid(codeSystem, codeValue));
}

@Test
void testReturnsTrueIfCodingValid() {
String codeSystem = MBI_ID_R4;
String mbCodeValue = "MB";
assertTrue(IdentifierUtils.checkCodingIsValid(codeSystem, mbCodeValue));
String mcCodeValue = "MC";
assertTrue(IdentifierUtils.checkCodingIsValid(codeSystem, mcCodeValue));
}

@Test
void testReturnsTrueIfURLValid() {
String url = IdentifierUtils.CURRENCY_IDENTIFIER;
Rwolfe-Nava marked this conversation as resolved.
Show resolved Hide resolved
assertTrue(IdentifierUtils.checkCurrencyUrlIsValid(url));
}

@Test
void testReturnsFalseIfURLInvalid() {
String url = "invalid_url";
Rwolfe-Nava marked this conversation as resolved.
Show resolved Hide resolved
assertFalse(IdentifierUtils.checkCurrencyUrlIsValid(url));
}

@Test
void testR4ExtractIds() throws IOException {
List<String> beneIds = List.of("-19990000001101", "-19990000001102", "-19990000001103");
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.6'
fhirVersion='1.2.7'
bfdVersion='2.2.2'
aggregatorVersion='1.3.4'
filtersVersion='1.9.4'
Expand Down
Loading