Search in sources :

Example 11 with FHIRContext

use of io.github.linuxforhealth.fhir.FHIRContext in project hl7v2-fhir-converter by LinuxForHealth.

the class FHIRConverterTest method test_dosage_output.

@Test
void test_dosage_output() throws IOException {
    String hl7message = "MSH|^~\\&|MyEMR|DE-000001| |CAIRLO|20160701123030-0700||VXU^V04^VXU_V04|CA0001|P|2.6|||ER|AL|||||Z22^CDCPHINVS|DE-000001\r" + "PID|1||PA123456^^^MYEMR^MR||JONES^GEORGE^M^JR^^^L|MILLER^MARTHA^G^^^^M|20140227|M||2106-3^WHITE^CDCREC|1234 W FIRST ST^^BEVERLY HILLS^CA^90210^^H||^PRN^PH^^^555^5555555||ENG^English^HL70296|||||||2186-5^ not Hispanic or Latino^CDCREC||Y|2\r" + "ORC|RE||197023^CMC|||||||^Clark^Dave||1234567890^Smith^Janet^^^^^^NPPES^L^^^NPI^^^^^^^^MD\r" + "RXA|0|1|20140730||08^HEPB-PEDIATRIC/ADOLESCENT^CVX|.5|mL^mL^UCUM||00^NEW IMMUNIZATION RECORD^NIP001|1234567890^Smith^Janet^^^^^^NPPES^^^^NPI^^^^^^^^MD |^^^DE-000001||||0039F|20200531|MSD^MERCK^MVX|||CP|A";
    String json = ftv.convert(hl7message, OPTIONS);
    FHIRContext context = new FHIRContext();
    IBaseResource bundleResource = context.getParser().parseResource(json);
    assertThat(bundleResource).isNotNull();
    Bundle b = (Bundle) bundleResource;
    List<BundleEntryComponent> e = b.getEntry();
    List<Resource> immunization = e.stream().filter(v -> ResourceType.Immunization == v.getResource().getResourceType()).map(BundleEntryComponent::getResource).collect(Collectors.toList());
    assertThat(immunization).hasSize(1);
    String s = context.getParser().encodeResourceToString(immunization.get(0));
    Class<? extends IBaseResource> klass = Immunization.class;
    Immunization expectDoseQuantity = (Immunization) context.getParser().parseResource(klass, s);
    assertThat(expectDoseQuantity.hasDoseQuantity()).isTrue();
    Quantity dosage = expectDoseQuantity.getDoseQuantity();
    BigDecimal value = dosage.getValue();
    String unit = dosage.getUnit();
    assertThat(value).isEqualTo(BigDecimal.valueOf(.5));
    assertThat(unit).isEqualTo("mL");
}
Also used : FHIRContext(io.github.linuxforhealth.fhir.FHIRContext) Immunization(org.hl7.fhir.r4.model.Immunization) BundleEntryComponent(org.hl7.fhir.r4.model.Bundle.BundleEntryComponent) Bundle(org.hl7.fhir.r4.model.Bundle) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) Resource(org.hl7.fhir.r4.model.Resource) Quantity(org.hl7.fhir.r4.model.Quantity) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) BigDecimal(java.math.BigDecimal) Test(org.junit.jupiter.api.Test)

Example 12 with FHIRContext

use of io.github.linuxforhealth.fhir.FHIRContext in project hl7v2-fhir-converter by LinuxForHealth.

the class PatientUtils method createPatientFromHl7Segment.

public static Patient createPatientFromHl7Segment(HL7ToFHIRConverter ftv, String inputSegment) {
    String json = ftv.convert(inputSegment, OPTIONS);
    LOGGER.debug(json);
    assertThat(json).isNotBlank();
    FHIRContext context = new FHIRContext();
    IBaseResource bundleResource = context.getParser().parseResource(json);
    assertThat(bundleResource).isNotNull();
    Bundle b = (Bundle) bundleResource;
    List<BundleEntryComponent> e = b.getEntry();
    List<Resource> patients = e.stream().filter(v -> ResourceType.Patient == v.getResource().getResourceType()).map(BundleEntryComponent::getResource).collect(Collectors.toList());
    assertThat(patients).hasSize(1);
    return getPatientFromResource(patients.get(0));
}
Also used : FHIRContext(io.github.linuxforhealth.fhir.FHIRContext) BundleEntryComponent(org.hl7.fhir.r4.model.Bundle.BundleEntryComponent) Bundle(org.hl7.fhir.r4.model.Bundle) Resource(org.hl7.fhir.r4.model.Resource) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource)

Example 13 with FHIRContext

use of io.github.linuxforhealth.fhir.FHIRContext in project hl7v2-fhir-converter by LinuxForHealth.

the class HL7ToFHIRConverter method getMessageEngine.

private HL7MessageEngine getMessageEngine(ConverterOptions options) {
    Preconditions.checkArgument(options != null, "options cannot be null.");
    FHIRContext context = new FHIRContext(options.isPrettyPrint(), options.isValidateResource(), options.getProperties(), options.getZoneIdText());
    return new HL7MessageEngine(context, options.getBundleType());
}
Also used : FHIRContext(io.github.linuxforhealth.fhir.FHIRContext) HL7MessageEngine(io.github.linuxforhealth.hl7.message.HL7MessageEngine)

Example 14 with FHIRContext

use of io.github.linuxforhealth.fhir.FHIRContext in project hl7v2-fhir-converter by LinuxForHealth.

the class Hl7ORUMessageTest method test_oru_multipleOBXofDifferentTypes.

/**
 * ORU messages with an OBR and multiple OBX segments create records only for non TX type OBX
 * The OBX type TX are added to the presentedForm as an attachment for the diagnostic
 * @throws IOException
 */
// Suppress warnings about too many assertions in a test.  Justification: creating a FHIR message is very costly; we need to check many asserts per creation for efficiency.
@java.lang.SuppressWarnings("squid:S5961")
@Test
void test_oru_multipleOBXofDifferentTypes() throws IOException {
    String json = ftv.convert(new File("src/test/resources/ORU-multiline-short.hl7"), OPTIONS_PRETTYPRINT);
    assertThat(json).isNotBlank();
    LOGGER.debug("FHIR json result:\n" + json);
    // Verify conversion
    FHIRContext context = new FHIRContext();
    IBaseResource bundleResource = context.getParser().parseResource(json);
    assertThat(bundleResource).isNotNull();
    Bundle b = (Bundle) bundleResource;
    Assertions.assertSame(BundleType.COLLECTION, b.getType(), "Bundle type not expected");
    List<BundleEntryComponent> e = b.getEntry();
    // Verify that the right resources have been created
    List<Resource> patientResource = ResourceUtils.getResourceList(e, ResourceType.Patient);
    assertThat(patientResource).hasSize(1);
    List<Resource> encounterResource = ResourceUtils.getResourceList(e, ResourceType.Encounter);
    assertThat(encounterResource).hasSize(1);
    List<Resource> organizationResource = ResourceUtils.getResourceList(e, ResourceType.Organization);
    // We expect an organization created from an Encounter.serviceProvider reference
    assertThat(organizationResource).hasSize(1);
    List<Resource> practitionerResource = ResourceUtils.getResourceList(e, ResourceType.Practitioner);
    assertThat(practitionerResource).hasSize(4);
    List<Resource> messageHeader = ResourceUtils.getResourceList(e, ResourceType.MessageHeader);
    assertThat(messageHeader).hasSize(1);
    // Verify Diagnostic Report is created as expected
    List<Resource> reportResource = ResourceUtils.getResourceList(e, ResourceType.DiagnosticReport);
    assertThat(reportResource).hasSize(1);
    List<Resource> servReqResource = ResourceUtils.getResourceList(e, ResourceType.ServiceRequest);
    assertThat(servReqResource).hasSize(1);
    // Verify there are no extra resources created
    assertThat(e).hasSize(10);
    // Verify no observations are created
    List<Resource> obsResource = ResourceUtils.getResourceList(e, ResourceType.Observation);
    // TODO: When NTE is implemented, then update this to one.
    assertThat(obsResource).isEmpty();
    // /////////////////////////////////////////
    // Now confirm content of the diagnosticReport because we don't have separate tests for DiagnosticReport
    // /////////////////////////////////////////
    DiagnosticReport diag = ResourceUtils.getResourceDiagnosticReport(reportResource.get(0), context);
    // Verify status from OBR.25
    assertThat(diag.getStatus().toCode()).isEqualTo("final");
    // Verify category from OBR.24
    assertThat(diag.getCategory()).hasSize(1);
    DatatypeUtils.checkCommonCodeableConceptAssertions(diag.getCategoryFirstRep(), "CT", "CAT Scan", "http://terminology.hl7.org/CodeSystem/v2-0074", "CT");
    // Verify code from OBR.4; This tests scenario of the code not being in the default loinc system.
    assertThat(diag.hasCode()).isTrue();
    List<Coding> codings = diag.getCode().getCoding();
    assertThat(codings).hasSize(1);
    Coding coding = codings.get(0);
    assertThat(coding.hasDisplay()).isTrue();
    assertThat(coding.getDisplay()).hasToString("ECHO CARDIOGRAM COMPLETE");
    assertThat(coding.hasCode()).isTrue();
    assertThat(coding.getCode()).hasToString("1487");
    assertThat(coding.hasSystem()).isFalse();
    // Verify encounter reference
    assertThat(diag.getEncounter().isEmpty()).isFalse();
    // Verify subject reference
    assertThat(diag.getSubject().isEmpty()).isFalse();
    // Verify effectiveDateTime from OBR.7 and OBR.8
    // This also verifies the type, confirming effectiveDateTime was set rather than effectivePeriod
    assertThat(diag.getEffectiveDateTimeType().asStringValue()).isEqualTo("2020-08-02T12:44:55+08:00");
    // Verify issued from OBR.22
    assertThat(diag.getIssued()).isNull();
    // Verify resultsInterpreter from OBR.32
    assertThat(diag.getResultsInterpreter()).isEmpty();
    // Verify basedOn is ref to the ServiceRequest created for ORC or OBR
    assertThat(diag.getBasedOn()).hasSize(1);
    assertThat(diag.getBasedOn().get(0).getReference().substring(0, 15)).isEqualTo("ServiceRequest/");
    // Verify specimen reference
    List<Reference> spmRef = diag.getSpecimen();
    assertThat(spmRef).isEmpty();
    // Verify result reference
    List<Reference> obsRef = diag.getResult();
    assertThat(obsRef).isEmpty();
    // Verify attachment to diagnostic report
    List<Attachment> attachments = diag.getPresentedForm();
    Assertions.assertEquals(1, attachments.size(), "Unexpected number of attachments");
    Attachment a = attachments.get(0);
    Assertions.assertTrue(a.getContentType().equalsIgnoreCase("text/plain"), "Incorrect content type");
    Assertions.assertTrue(a.getLanguage().equalsIgnoreCase("en"), "Incorrect language");
    // Verify data attachment after decoding
    String decoded = new String(Base64.getDecoder().decode(a.getDataElement().getValueAsString()));
    System.out.println("Decoded: '" + decoded + "'");
    Assertions.assertEquals("\n[PII] Emergency Department\nED Encounter Arrival Date: [ADDRESS] [PERSONALNAME]:", decoded, "Incorrect data");
    Assertions.assertTrue(a.getTitle().equalsIgnoreCase("ECHO CARDIOGRAM COMPLETE"), "Incorrect title");
    // Verify creation data is persisted correctly - 2020-08-02T12:44:55+08:00
    Calendar c = Calendar.getInstance();
    // needed to completely clear out calendar object
    c.clear();
    c.set(2020, 7, 2, 12, 44, 55);
    c.setTimeZone(TimeZone.getTimeZone(ZoneId.of("+08:00")));
    Date d = c.getTime();
    Assertions.assertEquals(d, a.getCreation(), "Incorrect creation date");
    // //////////////////////////////////
    for (Resource res : obsResource) {
        // Verify encounter reference exists
        Observation obs = (Observation) res;
        assertThat(obs.getEncounter().isEmpty()).isFalse();
        assertThat(obs.getEncounter().getReference().substring(0, 10)).isEqualTo("Encounter/");
        // Verify subject reference to Patient exists
        assertThat(obs.getSubject().isEmpty()).isFalse();
        assertThat(obs.getSubject().getReference().substring(0, 8)).isEqualTo("Patient/");
    }
}
Also used : FHIRContext(io.github.linuxforhealth.fhir.FHIRContext) Bundle(org.hl7.fhir.r4.model.Bundle) Reference(org.hl7.fhir.r4.model.Reference) Calendar(java.util.Calendar) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) Resource(org.hl7.fhir.r4.model.Resource) DiagnosticReport(org.hl7.fhir.r4.model.DiagnosticReport) Attachment(org.hl7.fhir.r4.model.Attachment) Date(java.util.Date) BundleEntryComponent(org.hl7.fhir.r4.model.Bundle.BundleEntryComponent) Coding(org.hl7.fhir.r4.model.Coding) Observation(org.hl7.fhir.r4.model.Observation) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 15 with FHIRContext

use of io.github.linuxforhealth.fhir.FHIRContext in project hl7v2-fhir-converter by LinuxForHealth.

the class Hl7ORUMessageTest method test_oru_multipleOBXWithMixedType.

/**
 * Verifies ORU messages with mixed OBX types
 *
 * @throws IOException
 */
@Test
// Suppress warnings about too many assertions in a test.  Justification: creating a FHIR message is very costly; we need to check many asserts per creation for efficiency.
@java.lang.SuppressWarnings("squid:S5961")
void test_oru_multipleOBXWithMixedType() throws IOException {
    String json = ftv.convert(new File("src/test/resources/ORU-multiline-short-mixed.hl7"), OPTIONS_PRETTYPRINT);
    assertThat(json).isNotBlank();
    LOGGER.debug("FHIR json result:\n" + json);
    // Verify conversion
    FHIRContext context = new FHIRContext();
    IBaseResource bundleResource = context.getParser().parseResource(json);
    Bundle b = (Bundle) bundleResource;
    assertThat(bundleResource).isNotNull();
    Assertions.assertSame(BundleType.COLLECTION, b.getType(), "Bundle type not expected");
    List<BundleEntryComponent> e = b.getEntry();
    // Verify that the right resources have been created
    List<Resource> patientResource = ResourceUtils.getResourceList(e, ResourceType.Patient);
    assertThat(patientResource).hasSize(1);
    List<Resource> encounterResource = ResourceUtils.getResourceList(e, ResourceType.Encounter);
    assertThat(encounterResource).hasSize(1);
    List<Resource> organizationResource = ResourceUtils.getResourceList(e, ResourceType.Organization);
    // We expect an organization created from an Encounter.serviceProvider reference
    assertThat(organizationResource).hasSize(1);
    List<Resource> practitionerResource = ResourceUtils.getResourceList(e, ResourceType.Practitioner);
    assertThat(practitionerResource).hasSize(4);
    List<Resource> messageHeader = ResourceUtils.getResourceList(e, ResourceType.MessageHeader);
    assertThat(messageHeader).hasSize(1);
    // Verify one Observation is created (from the ST, not the TX)
    List<Resource> obsResource = ResourceUtils.getResourceList(e, ResourceType.Observation);
    // TODO: When NTE is implemented, then update this.
    assertThat(obsResource).hasSize(1);
    // Verify Diagnostic Report is created as expected
    List<Resource> reportResource = ResourceUtils.getResourceList(e, ResourceType.DiagnosticReport);
    assertThat(reportResource).hasSize(1);
    List<Resource> servReqResource = ResourceUtils.getResourceList(e, ResourceType.ServiceRequest);
    assertThat(servReqResource).hasSize(1);
    // Verify there are no extra resources created
    assertThat(e).hasSize(11);
    // /////////////////////////////////////////
    // Now confirm content of the diagnosticReport because we don't have separate tests for DiagnosticReport
    // /////////////////////////////////////////
    DiagnosticReport diag = ResourceUtils.getResourceDiagnosticReport(reportResource.get(0), context);
    // Verify status from OBR.25
    assertThat(diag.getStatus().toCode()).isEqualTo("final");
    // Verify category from OBR.24
    assertThat(diag.getCategory()).hasSize(1);
    assertThat(diag.getCategory().get(0).getCoding()).hasSize(1);
    assertThat(diag.getCategory().get(0).getCoding().get(0).getSystem()).isEqualTo("http://terminology.hl7.org/CodeSystem/v2-0074");
    assertThat(diag.getCategory().get(0).getCoding().get(0).getCode()).isEqualTo("CT");
    assertThat(diag.getCategory().get(0).getCoding().get(0).getDisplay()).isEqualTo("CAT Scan");
    assertThat(diag.getCategory().get(0).getText()).isEqualTo("CT");
    // Verify code from OBR.4; This tests scenario of the code not being in the default loinc system.
    assertThat(diag.hasCode()).isTrue();
    List<Coding> codings = diag.getCode().getCoding();
    assertThat(codings).hasSize(1);
    Coding coding = codings.get(0);
    assertThat(coding.hasDisplay()).isTrue();
    assertThat(coding.getDisplay()).hasToString("ECHO CARDIOGRAM COMPLETE");
    assertThat(coding.hasCode()).isTrue();
    assertThat(coding.getCode()).hasToString("1487");
    assertThat(coding.hasSystem()).isFalse();
    // Verify encounter reference
    assertThat(diag.getEncounter().isEmpty()).isFalse();
    // Verify subject reference
    assertThat(diag.getSubject().isEmpty()).isFalse();
    // Verify effectiveDateTime from OBR.7 and OBR.8
    // This also verifies the type, confirming effectiveDateTime was set rather than effectivePeriod
    assertThat(diag.getEffectiveDateTimeType().asStringValue()).isEqualTo("2020-08-02T12:44:55+08:00");
    // Verify issued from OBR.22
    assertThat(diag.getIssued()).isNull();
    // Verify resultsInterpreter from OBR.32
    assertThat(diag.getResultsInterpreter()).isEmpty();
    // Verify basedOn is ref to the ServiceRequest created for ORC or OBR
    assertThat(diag.getBasedOn()).hasSize(1);
    assertThat(diag.getBasedOn().get(0).getReference().substring(0, 15)).isEqualTo("ServiceRequest/");
    // Verify specimen reference
    List<Reference> spmRef = diag.getSpecimen();
    assertThat(spmRef).isEmpty();
    // Verify result reference
    List<Reference> obsRef = diag.getResult();
    assertThat(obsRef).isNotEmpty().hasSize(1);
    assertThat(obsRef.get(0).isEmpty()).isFalse();
    // No attachment created since OBX with TX and no id is not first
    List<Attachment> attachments = diag.getPresentedForm();
    Assertions.assertEquals(0, attachments.size(), "Unexpected number of attachments");
    // //////////////////////////////////
    for (Resource res : obsResource) {
        // Verify encounter reference exists
        Observation obs = (Observation) res;
        assertThat(obs.getEncounter().isEmpty()).isFalse();
        assertThat(obs.getEncounter().getReference().substring(0, 10)).isEqualTo("Encounter/");
        // Verify subject reference to Patient exists
        assertThat(obs.getSubject().isEmpty()).isFalse();
        assertThat(obs.getSubject().getReference().substring(0, 8)).isEqualTo("Patient/");
    }
}
Also used : FHIRContext(io.github.linuxforhealth.fhir.FHIRContext) Bundle(org.hl7.fhir.r4.model.Bundle) Reference(org.hl7.fhir.r4.model.Reference) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) Resource(org.hl7.fhir.r4.model.Resource) DiagnosticReport(org.hl7.fhir.r4.model.DiagnosticReport) Attachment(org.hl7.fhir.r4.model.Attachment) BundleEntryComponent(org.hl7.fhir.r4.model.Bundle.BundleEntryComponent) Coding(org.hl7.fhir.r4.model.Coding) Observation(org.hl7.fhir.r4.model.Observation) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) File(java.io.File) Test(org.junit.jupiter.api.Test)

Aggregations

FHIRContext (io.github.linuxforhealth.fhir.FHIRContext)15 IBaseResource (org.hl7.fhir.instance.model.api.IBaseResource)14 Bundle (org.hl7.fhir.r4.model.Bundle)14 BundleEntryComponent (org.hl7.fhir.r4.model.Bundle.BundleEntryComponent)13 Resource (org.hl7.fhir.r4.model.Resource)12 Test (org.junit.jupiter.api.Test)10 Coding (org.hl7.fhir.r4.model.Coding)5 CodeableConcept (org.hl7.fhir.r4.model.CodeableConcept)3 DiagnosticReport (org.hl7.fhir.r4.model.DiagnosticReport)3 Encounter (org.hl7.fhir.r4.model.Encounter)3 Observation (org.hl7.fhir.r4.model.Observation)3 Message (ca.uhn.hl7v2.model.Message)2 Hl7InputStreamMessageStringIterator (ca.uhn.hl7v2.util.Hl7InputStreamMessageStringIterator)2 HL7HapiParser (io.github.linuxforhealth.hl7.parsing.HL7HapiParser)2 File (java.io.File)2 InputStream (java.io.InputStream)2 Attachment (org.hl7.fhir.r4.model.Attachment)2 Base (org.hl7.fhir.r4.model.Base)2 Immunization (org.hl7.fhir.r4.model.Immunization)2 Reference (org.hl7.fhir.r4.model.Reference)2