From c996bb632d0bf71f98317a748b977891496e4195 Mon Sep 17 00:00:00 2001 From: Joel Biskie Date: Wed, 18 Dec 2024 10:25:28 -0600 Subject: [PATCH] Refactor getOrc12ExtensionPractitioner to HapiHelper, add unit tests --- ...OrderProviderToObrOrderProviderTest.groovy | 26 +---- .../external/hapi/HapiHelper.java | 36 ++++++- .../external/hapi/HapiHelperTest.groovy | 95 +++++++++++++++++++ 3 files changed, 129 insertions(+), 28 deletions(-) diff --git a/etor/src/test/groovy/gov/hhs/cdc/trustedintermediary/etor/ruleengine/transformation/custom/CopyOrcOrderProviderToObrOrderProviderTest.groovy b/etor/src/test/groovy/gov/hhs/cdc/trustedintermediary/etor/ruleengine/transformation/custom/CopyOrcOrderProviderToObrOrderProviderTest.groovy index cc983762f..6263274b2 100644 --- a/etor/src/test/groovy/gov/hhs/cdc/trustedintermediary/etor/ruleengine/transformation/custom/CopyOrcOrderProviderToObrOrderProviderTest.groovy +++ b/etor/src/test/groovy/gov/hhs/cdc/trustedintermediary/etor/ruleengine/transformation/custom/CopyOrcOrderProviderToObrOrderProviderTest.groovy @@ -192,7 +192,7 @@ class CopyOrcOrderProviderToObrOrderProviderTest extends Specification{ } void evaluateOrc12IsNull(Bundle bundle) { - assert getOrc12ExtensionPractitioner(bundle) == null + assert HapiHelper.getOrc12ExtensionPractitioner(bundle) == null } void evaluateOrc12Values( @@ -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 @@ -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 - } } diff --git a/shared/src/main/java/gov/hhs/cdc/trustedintermediary/external/hapi/HapiHelper.java b/shared/src/main/java/gov/hhs/cdc/trustedintermediary/external/hapi/HapiHelper.java index bef4b214a..a35324c0a 100644 --- a/shared/src/main/java/gov/hhs/cdc/trustedintermediary/external/hapi/HapiHelper.java +++ b/shared/src/main/java/gov/hhs/cdc/trustedintermediary/external/hapi/HapiHelper.java @@ -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()) { @@ -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; } diff --git a/shared/src/test/groovy/gov/hhs/cdc/trustedintermediary/external/hapi/HapiHelperTest.groovy b/shared/src/test/groovy/gov/hhs/cdc/trustedintermediary/external/hapi/HapiHelperTest.groovy index 5842b15ea..54283fc92 100644 --- a/shared/src/test/groovy/gov/hhs/cdc/trustedintermediary/external/hapi/HapiHelperTest.groovy +++ b/shared/src/test/groovy/gov/hhs/cdc/trustedintermediary/external/hapi/HapiHelperTest.groovy @@ -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 @@ -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()