Skip to content

Commit

Permalink
Refactor getObr16ExtensionPractitioner into HapiHelper, add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jbiskie authored and jorg3lopez committed Dec 18, 2024
1 parent 6be3152 commit e626b0e
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class CopyOrcOrderProviderToObrOrderProviderTest extends Specification{
evaluateOrc12Values(bundle, EXPECTED_NPI, EXPECTED_FIRST_NAME, EXPECTED_LAST_NAME, EXPECTED_NAME_TYPE_CODE, EXPECTED_IDENTIFIER_TYPE_CODE)

// OBR16 should not exist initially
def obr16Practitioner = getObr16ExtensionPractitioner(serviceRequest)
def obr16Practitioner = HapiHelper.getObr16ExtensionPractitioner(serviceRequest)
obr16Practitioner == null

when:
Expand Down Expand Up @@ -216,7 +216,7 @@ class CopyOrcOrderProviderToObrOrderProviderTest extends Specification{

void evaluateObr16IsNull(ServiceRequest serviceRequest) {
assert HapiHelper.getObr16Extension(serviceRequest) == null
assert getObr16ExtensionPractitioner(serviceRequest) == null
assert HapiHelper.getObr16ExtensionPractitioner(serviceRequest) == null
}

void evaluateObr16Values(
Expand All @@ -226,7 +226,7 @@ class CopyOrcOrderProviderToObrOrderProviderTest extends Specification{
String expectedLastName,
String expectedNameTypeCode,
String expectedIdentifierTypeCode) {
def practitioner = getObr16ExtensionPractitioner(serviceRequest)
def practitioner = HapiHelper.getObr16ExtensionPractitioner(serviceRequest)
def xcnExtension = practitioner.getExtensionByUrl(PRACTITIONER_EXTENSION_URL)

assert practitioner.identifier[0]?.value == expectedNpi
Expand All @@ -237,14 +237,6 @@ class CopyOrcOrderProviderToObrOrderProviderTest extends Specification{
assert codingSystem == null || codingSystem[0]?.code == expectedIdentifierTypeCode
}

Practitioner getObr16ExtensionPractitioner (serviceRequest) {
def obr16Extension = HapiHelper.getObr16Extension(serviceRequest)
if (obr16Extension == null) {
return null
}
return obr16Extension.value.getResource()
}

Practitioner getOrc12ExtensionPractitioner(Bundle bundle) {
def diagnosticReport = HapiHelper.getDiagnosticReport(bundle)
def serviceRequest = HapiHelper.getServiceRequest(diagnosticReport)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.hl7.fhir.r4.model.Patient;
import org.hl7.fhir.r4.model.Practitioner;
import org.hl7.fhir.r4.model.PractitionerRole;
import org.hl7.fhir.r4.model.Reference;
import org.hl7.fhir.r4.model.Resource;
import org.hl7.fhir.r4.model.ServiceRequest;
import org.hl7.fhir.r4.model.StringType;
Expand Down Expand Up @@ -301,26 +302,25 @@ public static void setPID3_5Value(Bundle bundle, String value) {
}

public static void removePID3_5Value(Bundle bundle) {
Identifier patientIdentifier = HapiHelper.getPID3Identifier(bundle);
Identifier patientIdentifier = getPID3Identifier(bundle);
if (patientIdentifier == null) {
return;
}

patientIdentifier.setType(null);

Extension cxExtension =
patientIdentifier.getExtensionByUrl(HapiHelper.EXTENSION_CX_IDENTIFIER_URL);
Extension cxExtension = patientIdentifier.getExtensionByUrl(EXTENSION_CX_IDENTIFIER_URL);
if (cxExtension == null) {
return;
}

if (cxExtension.hasExtension(HapiHelper.EXTENSION_CX5_URL)) {
cxExtension.removeExtension(HapiHelper.EXTENSION_CX5_URL);
if (cxExtension.hasExtension(EXTENSION_CX5_URL)) {
cxExtension.removeExtension(EXTENSION_CX5_URL);
}

// The cx-identifier extension can be removed if it has no more sub-extensions
if (cxExtension.getExtension().isEmpty()) {
patientIdentifier.removeExtension(HapiHelper.EXTENSION_CX_IDENTIFIER_URL);
patientIdentifier.removeExtension(EXTENSION_CX_IDENTIFIER_URL);
}
}

Expand All @@ -331,33 +331,31 @@ public static Extension getPID5Extension(Bundle bundle) {
return null;
}
HumanName name = patient.getNameFirstRep();
return name.getExtensionByUrl(HapiHelper.EXTENSION_XPN_HUMAN_NAME_URL);
return name.getExtensionByUrl(EXTENSION_XPN_HUMAN_NAME_URL);
}

public static void setPID5_7ExtensionValue(Bundle bundle, String value) {
Extension extension = getPID5Extension(bundle);
if (extension != null && extension.hasExtension(HapiHelper.EXTENSION_XPN7_URL)) {
extension
.getExtensionByUrl(HapiHelper.EXTENSION_XPN7_URL)
.setValue(new StringType(value));
if (extension != null && extension.hasExtension(EXTENSION_XPN7_URL)) {
extension.getExtensionByUrl(EXTENSION_XPN7_URL).setValue(new StringType(value));
}
}

public static void removePID5_7Value(Bundle bundle) {
Patient patient = HapiHelper.getPIDPatient(bundle);
Patient patient = getPIDPatient(bundle);
if (patient == null) {
return;
}
HumanName patientName = patient.getNameFirstRep();
patientName.setUse(null);

Extension pid5Extension = HapiHelper.getPID5Extension(bundle);
Extension pid5Extension = getPID5Extension(bundle);
if (pid5Extension == null) {
return;
}
Extension xpn7Extension = pid5Extension.getExtensionByUrl(HapiHelper.EXTENSION_XPN7_URL);
Extension xpn7Extension = pid5Extension.getExtensionByUrl(EXTENSION_XPN7_URL);
if (xpn7Extension != null) {
pid5Extension.removeExtension(HapiHelper.EXTENSION_XPN7_URL);
pid5Extension.removeExtension(EXTENSION_XPN7_URL);
}
}

Expand Down Expand Up @@ -561,18 +559,31 @@ public static void setOBR16WithPractitioner(
}

Extension obr16Extension =
HapiHelper.ensureSubExtensionExists(
obrExtension, HapiHelper.EXTENSION_OBR16_DATA_TYPE.toString());
ensureSubExtensionExists(obrExtension, EXTENSION_OBR16_DATA_TYPE.toString());

obr16Extension.setValue(practitionerRole.getPractitioner());
}

public static Extension getObr16Extension(ServiceRequest serviceRequest) {
Extension obrExtension = serviceRequest.getExtensionByUrl(HapiHelper.EXTENSION_OBR_URL);
Extension obrExtension = serviceRequest.getExtensionByUrl(EXTENSION_OBR_URL);
if (obrExtension == null) {
return null;
}
return obrExtension.getExtensionByUrl(HapiHelper.EXTENSION_OBR16_DATA_TYPE.toString());
return obrExtension.getExtensionByUrl(EXTENSION_OBR16_DATA_TYPE.toString());
}

public static Practitioner getObr16ExtensionPractitioner(ServiceRequest serviceRequest) {
Extension obr16Extension = getObr16Extension(serviceRequest);
if (obr16Extension == null || obr16Extension.getValue() == null) {
return null;
}

if (obr16Extension.getValue() instanceof Reference reference) {
if (reference.getResource() instanceof Practitioner practitioner) {
return practitioner;
}
}
return null;
}

/**
Expand Down Expand Up @@ -743,9 +754,9 @@ public static void removeHl7FieldIdentifier(List<Identifier> identifiers, String

public static String urlForCodeType(String code) {
return switch (code) {
case HapiHelper.LOINC_CODE -> HapiHelper.LOINC_URL;
case HapiHelper.PLT_CODE -> null;
default -> HapiHelper.LOCAL_CODE_URL;
case LOINC_CODE -> LOINC_URL;
case PLT_CODE -> null;
default -> LOCAL_CODE_URL;
};
}

Expand All @@ -760,22 +771,19 @@ public static String urlForCodeType(String code) {
*/
public static boolean hasDefinedCoding(
Coding coding, String codingExt, String codingSystemExt) {
var codingExtMatch =
hasMatchingCodingExtension(coding, HapiHelper.EXTENSION_CWE_CODING, codingExt);
var codingExtMatch = hasMatchingCodingExtension(coding, EXTENSION_CWE_CODING, codingExt);
var codingSystemExtMatch =
hasMatchingCodingExtension(
coding, HapiHelper.EXTENSION_CODING_SYSTEM, codingSystemExt);
hasMatchingCodingExtension(coding, EXTENSION_CODING_SYSTEM, codingSystemExt);
return codingExtMatch && codingSystemExtMatch;
}

private static boolean hasMatchingCodingExtension(
Coding coding, String extensionUrl, String valueToMatch) {
if (!HapiHelper.hasCodingExtensionWithUrl(coding, extensionUrl)) {
if (!hasCodingExtensionWithUrl(coding, extensionUrl)) {
return false;
}

var extensionValue =
HapiHelper.getCodingExtensionByUrl(coding, extensionUrl).getValue().toString();
var extensionValue = getCodingExtensionByUrl(coding, extensionUrl).getValue().toString();
return Objects.equals(valueToMatch, extensionValue);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,86 @@ class HapiHelperTest extends Specification {
result == null
}

def "getObr16ExtensionPractitioner should return null when ServiceRequest has no OBR-16 extension"() {
given:
def serviceRequest = new ServiceRequest()

when:
def result = HapiHelper.getObr16ExtensionPractitioner(serviceRequest)

then:
result == null
}

def "getObr16ExtensionPractitioner should return null when OBR-16 extension value is null"() {
given:
def serviceRequest = new ServiceRequest()
def obrExtension = new Extension(HapiHelper.EXTENSION_OBR_URL)
def obr16Extension = new Extension(HapiHelper.EXTENSION_OBR16_DATA_TYPE.toString())
obrExtension.addExtension(obr16Extension)
serviceRequest.addExtension(obrExtension)

when:
def result = HapiHelper.getObr16ExtensionPractitioner(serviceRequest)

then:
result == null
}

def "getObr16ExtensionPractitioner should return null when OBR16 extension value is not a Reference"() {
given:
def serviceRequest = new ServiceRequest()
def obrExtension = new Extension(HapiHelper.EXTENSION_OBR_URL)
def obr16Extension = new Extension(HapiHelper.EXTENSION_OBR16_DATA_TYPE.toString())
obr16Extension.setValue(new StringType("Not a Reference"))
obrExtension.addExtension(obr16Extension)
serviceRequest.addExtension(obrExtension)

when:
def result = HapiHelper.getObr16ExtensionPractitioner(serviceRequest)

then:
result == null
}

def "getObr16ExtensionPractitioner should return null when Reference does not contain Practitioner"() {
given:
def serviceRequest = new ServiceRequest()
def reference = new Reference()
def obrExtension = new Extension(HapiHelper.EXTENSION_OBR_URL)
def obr16Extension = new Extension(HapiHelper.EXTENSION_OBR16_DATA_TYPE.toString())
obr16Extension.setValue(reference)
obrExtension.addExtension(obr16Extension)
serviceRequest.addExtension(obrExtension)

when:
def result = HapiHelper.getObr16ExtensionPractitioner(serviceRequest)

then:
result == null
}

def "getObr16ExtensionPractitioner should return Practitioner when Reference contains Practitioner"() {
given:
def serviceRequest = new ServiceRequest()
def obrExtension = new Extension(HapiHelper.EXTENSION_OBR_URL)
def obr16Extension = new Extension(HapiHelper.EXTENSION_OBR16_DATA_TYPE.toString())
obrExtension.addExtension(obr16Extension)
serviceRequest.addExtension(obrExtension)

def practitioner = new Practitioner()
def practitionerRole = new PractitionerRole()
practitionerRole.setPractitioner(new Reference("Practitioner/123").setResource(practitioner) as Reference)

HapiHelper.setOBR16WithPractitioner(obrExtension, practitionerRole)

when:
def result = HapiHelper.getObr16ExtensionPractitioner(serviceRequest)

then:
result == practitioner
}

def "ensureExtensionExists returns extension if it exists"() {
given:
def serviceRequest = new ServiceRequest()
Expand Down

0 comments on commit e626b0e

Please sign in to comment.