Skip to content

Commit

Permalink
Refactor getOrc12ExtensionPractitioner to 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 e626b0e commit cc2b9d6
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ class CopyOrcOrderProviderToObrOrderProviderTest extends Specification{
}

void evaluateOrc12IsNull(Bundle bundle) {
assert getOrc12ExtensionPractitioner(bundle) == null
assert HapiHelper.getOrc12ExtensionPractitioner(bundle) == null
}

void evaluateOrc12Values(
Expand All @@ -202,7 +202,7 @@ class CopyOrcOrderProviderToObrOrderProviderTest extends Specification{
String expectedLastName,
String expectedNameTypeCode,
String expectedIdentifierTypeCode) {
def practitioner = getOrc12ExtensionPractitioner(bundle)
def practitioner = HapiHelper.getOrc12ExtensionPractitioner(bundle)
def xcnExtension = practitioner.getExtensionByUrl(PRACTITIONER_EXTENSION_URL)

assert practitioner.identifier[0]?.value == expectedNpi
Expand Down Expand Up @@ -236,26 +236,4 @@ class CopyOrcOrderProviderToObrOrderProviderTest extends Specification{
def codingSystem = practitioner.identifier[0]?.type?.coding
assert codingSystem == null || codingSystem[0]?.code == expectedIdentifierTypeCode
}

Practitioner getOrc12ExtensionPractitioner(Bundle bundle) {
def diagnosticReport = HapiHelper.getDiagnosticReport(bundle)
def serviceRequest = HapiHelper.getServiceRequest(diagnosticReport)

def orcExtension = serviceRequest.getExtensionByUrl(HapiHelper.EXTENSION_ORC_URL)
def orc12Extension = orcExtension.getExtensionByUrl(HapiHelper.EXTENSION_ORC12_URL)

if (orc12Extension == null) {
return null
}

def practitionerReference = (Reference) orc12Extension.getValue()
def practitionerUrl = practitionerReference.getReference()

for (Bundle.BundleEntryComponent entry : bundle.getEntry()) {
if (Objects.equals(entry.getFullUrl(), practitionerUrl) && entry.getResource() instanceof Practitioner)
return (Practitioner) entry.getResource()
}

return null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ public static String getOBR4_1Value(ServiceRequest serviceRequest) {
return getCWE1Value(cc.getCoding().get(0));
}

// OBR16 - Ordering Provider
// OBR-16 - Ordering Provider
public static void setOBR16WithPractitioner(
Extension obrExtension, PractitionerRole practitionerRole) {
if (practitionerRole == null || !practitionerRole.getPractitioner().hasReference()) {
Expand All @@ -578,11 +578,39 @@ public static Practitioner getObr16ExtensionPractitioner(ServiceRequest serviceR
return null;
}

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

// ORC-12 - Ordering Provider
public static Practitioner getOrc12ExtensionPractitioner(Bundle bundle) {
DiagnosticReport diagnosticReport = getDiagnosticReport(bundle);
if (diagnosticReport == null) return null;

ServiceRequest serviceRequest = getServiceRequest(diagnosticReport);
if (serviceRequest == null) return null;

Extension orcExtension = serviceRequest.getExtensionByUrl(EXTENSION_ORC_URL);
if (orcExtension == null) return null;

Extension orc12Extension = orcExtension.getExtensionByUrl(EXTENSION_ORC12_URL);
if (orc12Extension == null) return null;

Reference practitionerReference = (Reference) orc12Extension.getValue();
if (practitionerReference == null) return null;

String practitionerUrl = practitionerReference.getReference();
if (practitionerUrl == null) return null;

for (Bundle.BundleEntryComponent entry : bundle.getEntry()) {
if (Objects.equals(entry.getFullUrl(), practitionerUrl)
&& entry.getResource() instanceof Practitioner practitioner)
return practitioner;
}
}

return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gov.hhs.cdc.trustedintermediary.external.hapi

import org.hl7.fhir.r4.model.Bundle
import org.hl7.fhir.r4.model.Coding
import org.hl7.fhir.r4.model.DiagnosticReport
import org.hl7.fhir.r4.model.Extension
import org.hl7.fhir.r4.model.Identifier
import org.hl7.fhir.r4.model.MessageHeader
Expand Down Expand Up @@ -959,6 +960,100 @@ class HapiHelperTest extends Specification {
result == practitioner
}

// ORC-12 - Ordering Provider
def "getOrc12ExtensionPractitioner should return null when bundle has no DiagnosticReport"() {
given:
def bundle = new Bundle()

when:
def result = HapiHelper.getOrc12ExtensionPractitioner(bundle)

then:
result == null
}

def "getOrc12ExtensionPractitioner should return null when DiagnosticReport has no ServiceRequest"() {
given:
def bundle = new Bundle()
HapiFhirHelper.createDiagnosticReport(bundle)

when:
def result = HapiHelper.getOrc12ExtensionPractitioner(bundle)

then:
result == null
}

def "getOrc12ExtensionPractitioner should return null when ServiceRequest has no ORC extension"() {
given:
def bundle = new Bundle()
def diagnosticReport = HapiFhirHelper.createDiagnosticReport(bundle)
HapiFhirHelper.createBasedOnServiceRequest(diagnosticReport)

when:
def result = HapiHelper.getOrc12ExtensionPractitioner(bundle)

then:
result == null
}

def "getOrc12ExtensionPractitioner should return null when ORC extension has no ORC-12 extension"() {
given:
def bundle = new Bundle()
def diagnosticReport = HapiFhirHelper.createDiagnosticReport(bundle)
def serviceRequest = HapiFhirHelper.createBasedOnServiceRequest(diagnosticReport)
serviceRequest.addExtension(new Extension().setUrl(HapiHelper.EXTENSION_ORC_URL))

when:
def result = HapiHelper.getOrc12ExtensionPractitioner(bundle)

then:
result == null
}

def "should return null when ORC12 extension has no Practitioner reference"() {
given:
def bundle = new Bundle()
def diagnosticReport = HapiFhirHelper.createDiagnosticReport(bundle)
def serviceRequest = HapiFhirHelper.createBasedOnServiceRequest(diagnosticReport)

def orcExtension = new Extension().setUrl(HapiHelper.EXTENSION_ORC_URL)
def orc12Extension = new Extension().setUrl(HapiHelper.EXTENSION_ORC12_URL)
orcExtension.addExtension(orc12Extension)
serviceRequest.addExtension(orcExtension)

when:
def result = HapiHelper.getOrc12ExtensionPractitioner(bundle)

then:
result == null
}

def "should return Practitioner when all conditions are met"() {
given:
def bundle = new Bundle()
def diagnosticReport = HapiFhirHelper.createDiagnosticReport(bundle)
def serviceRequest = HapiFhirHelper.createBasedOnServiceRequest(diagnosticReport)

def orcExtension = new Extension().setUrl(HapiHelper.EXTENSION_ORC_URL)
def orc12Extension = new Extension().setUrl(HapiHelper.EXTENSION_ORC12_URL)

def practitioner = new Practitioner()
def practitionerId = "Practitioner/123"
def practitionerReference = new Reference().setReference(practitionerId)

orc12Extension.setValue(practitionerReference)
orcExtension.addExtension(orc12Extension)
serviceRequest.addExtension(orcExtension)
bundle.addEntry(new Bundle.BundleEntryComponent().setFullUrl(practitionerId).setResource(practitioner))

when:
def result = HapiHelper.getOrc12ExtensionPractitioner(bundle)

then:
result == practitioner
}

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

0 comments on commit cc2b9d6

Please sign in to comment.