use of ca.uhn.hl7v2.util.Hl7InputStreamMessageStringIterator in project hl7v2-fhir-converter by LinuxForHealth.
the class FHIRConverterTest method test_adt_40_message.
@Test
// Test an example of a message with no message structure specifed
void test_adt_40_message() throws Exception {
Message hl7message = null;
// Test that an ADT A40 message with no MSH-9.3 is successfully parsed and converted.
String hl7messageString = "MSH|^~\\&|REGADT|MCM|RSP1P8|MCM|200301051530|SEC|ADT^A40|00000003|P|2.6\n" + "PID|||MR1^^^XYZ||MAIDENNAME^EVE\n" + "MRG|MR2^^^XYZ\n";
InputStream ins = IOUtils.toInputStream(hl7messageString, StandardCharsets.UTF_8);
Hl7InputStreamMessageStringIterator iterator = new Hl7InputStreamMessageStringIterator(ins);
if (iterator.hasNext()) {
HL7HapiParser hparser = new HL7HapiParser();
hl7message = hparser.getParser().parse(iterator.next());
}
String messageType = HL7DataExtractor.getMessageType(hl7message);
assertThat(messageType).isEqualTo("ADT_A40");
// Convert and check for a patient resource
String json = ftv.convert(hl7messageString, ConverterOptions.SIMPLE_OPTIONS);
FHIRContext context = new FHIRContext();
IBaseResource bundleResource = context.getParser().parseResource(json);
assertThat(bundleResource).isNotNull();
Bundle b = (Bundle) bundleResource;
assertThat(b.getType()).isEqualTo(BundleType.COLLECTION);
assertThat(b.getId()).isNotNull();
assertThat(b.getMeta().getLastUpdated()).isNotNull();
List<BundleEntryComponent> e = b.getEntry();
List<Resource> patientResource = e.stream().filter(v -> ResourceType.Patient == v.getResource().getResourceType()).map(BundleEntryComponent::getResource).collect(Collectors.toList());
assertThat(patientResource).hasSize(2);
}
use of ca.uhn.hl7v2.util.Hl7InputStreamMessageStringIterator in project hl7v2-fhir-converter by LinuxForHealth.
the class HL7ToFHIRConverter method getHl7Message.
private static Message getHl7Message(String data) {
Message hl7message = null;
try (InputStream ins = IOUtils.toInputStream(data, StandardCharsets.UTF_8)) {
Hl7InputStreamMessageStringIterator iterator = new Hl7InputStreamMessageStringIterator(ins);
// only supports single message conversion.
if (iterator.hasNext()) {
hl7message = hparser.getParser().parse(iterator.next());
}
} catch (HL7Exception e) {
throw new IllegalArgumentException("Cannot parse the message.", e);
} catch (IOException ioe) {
throw new IllegalArgumentException("IOException encountered.", ioe);
}
try {
if (hl7message != null) {
String messageStructureInfo = hl7message.printStructure();
StringBuilder output = new StringBuilder();
String[] messageStructureInfoLines = messageStructureInfo.split(System.getProperty("line.separator"));
for (String line : messageStructureInfoLines) {
if (!line.contains("|")) {
output.append(line);
} else {
int firstDash = line.indexOf("-");
output.append(line.substring(0, firstDash + 5));
}
output.append("\n");
}
if (output.length() > 0) {
LOGGER.info("HL7_MESSAGE_STRUCTURE=\n{}", output);
}
}
} catch (HL7Exception e) {
throw new IllegalArgumentException("Error printing message structure.", e);
}
return hl7message;
}
use of ca.uhn.hl7v2.util.Hl7InputStreamMessageStringIterator in project hl7v2-fhir-converter by LinuxForHealth.
the class FHIRConverterTest method test_adt_40_message_with_adt_a39_structure_specified.
@Test
// Test an example of a message with message structure specified
void test_adt_40_message_with_adt_a39_structure_specified() throws Exception {
Message hl7message = null;
// Test that an ADT A40 message with MSH-9.3 of 'ADT_A39' is successfully parsed and converted as an ADT A40 message.
// Note that ADT_A39 is the expected structure of an ADT_A40 message.
String hl7messageString = "MSH|^~\\&|REGADT|MCM|RSP1P8|MCM|200301051530|SEC|ADT^A40^ADT_A39|00000003|P|2.6\n" + "PID|||MR1^^^XYZ||MAIDENNAME^EVE\n" + "MRG|MR2^^^XYZ\n";
InputStream ins = IOUtils.toInputStream(hl7messageString, StandardCharsets.UTF_8);
Hl7InputStreamMessageStringIterator iterator = new Hl7InputStreamMessageStringIterator(ins);
if (iterator.hasNext()) {
HL7HapiParser hparser = new HL7HapiParser();
hl7message = hparser.getParser().parse(iterator.next());
}
String messageType = HL7DataExtractor.getMessageType(hl7message);
assertThat(messageType).isEqualTo("ADT_A40");
// Convert and check for a patient resource
String json = ftv.convert(hl7messageString, ConverterOptions.SIMPLE_OPTIONS);
FHIRContext context = new FHIRContext();
IBaseResource bundleResource = context.getParser().parseResource(json);
assertThat(bundleResource).isNotNull();
Bundle b = (Bundle) bundleResource;
assertThat(b.getType()).isEqualTo(BundleType.COLLECTION);
assertThat(b.getId()).isNotNull();
assertThat(b.getMeta().getLastUpdated()).isNotNull();
List<BundleEntryComponent> e = b.getEntry();
List<Resource> patientResource = e.stream().filter(v -> ResourceType.Patient == v.getResource().getResourceType()).map(BundleEntryComponent::getResource).collect(Collectors.toList());
assertThat(patientResource).hasSize(2);
}
Aggregations