Unit Testing HL7 Messages with Apache Camel
Apache Camel has an HL7 component that it is very useful when we want to route HL7 messages. In this post I will show, step by step, how to send an HL7 String message, convert it to Hapi Message and the process it.
Step 1:
Add to your pom.xml camel-hl7 dependency:
[xml]
[/xml]
Step 2:
Your test class will need to extend CamelTestSupport, in my case, my test is called HL7CamelCodecTest.
[java]
public class HL7CamelCodecTest extends CamelTestSupport {
…
]
[/java]
Step 3:
– Override createRouteBuilder method and add in the route the hl7 codec. The HL7 component supports type Converter from/to Hapi and String which we will be using in this tutorial.
– (optional) Process the Hl7 message. In this example, we will change the Patient surname.
note: that the “hl7codec” it is just a name, the codec will be register in Step 4.
[java]
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
from(“mina:tcp://localhost:8877?sync=true&codec=#hl7codec”)
.process(new Processor() {
public void process(Exchange exchange) throws Exception {
//The codec has converted the message from string to Hapi Message
Message message = exchange.getIn().getBody(Message.class);
//Now, we can use the terser to make some changes
Terser terser = new Terser(message);
//We will change the Patient Surname from Wood to INTEGRATION_ENGINEER
terser.set(“/PID-5-1”, “INTEGRATION_ENGINEER”);
exchange.getOut().setBody(message);
}
});
}
};
}
[/java]
Step 4:
– Create a JNDI registry, so we can bind the “hl7codec” label to the actual HL7MLLPCodec.
[java]
protected JndiRegistry createRegistry() throws Exception {
JndiRegistry jndi = super.createRegistry();
jndi.bind(“hl7codec”, new HL7MLLPCodec());
return jndi;
}
[/java]
note: In a production environment you could define the HL7 codec in your Spring XML file:
<bean id=”hl7codec”>
<property name=”charset” value=”iso-8859-1″/>
</bean>
Step 5:
– Create a test. In this example, we are just sending an HL7 String Message, using the processor to change the Patient Surname and printing the outbound Message.
[java]
@Test
public void testHl7Codec() throws Exception {
String inMessage = “MSH|^~\\&|hl7Integration|hl7Integration|||||ADT^A01|||2.3|\r” +
“EVN|A01|20130617154644\r” +
“PID|1|465 306 5961||407623|Wood^Patrick^^^MR||19700101|1|||High Street^^Oxford^^Ox1 4DP~George St^^Oxford^^Ox1 5AP|||||||”;
Object outMessage = template.requestBody(“mina:tcp://localhost:8877?sync=true&codec=#hl7codec”, inMessage);
System.out.println(outMessage.toString());
}
[/java]
Now, you can see that the outbound message contains in the PID segment the patient surname equal to INTEGRATION_ENGINEER.
PID|1|465 306 5961||407623|INTEGRATION_ENGINEER^Patrick^^^MR||19700101|1|||High Street^^Oxford^^Ox1 4DP~George St^^Oxford^^Ox1 5AP
You can find the whole project in github.
Do you have any example of how to do the same without extend of CamelTestSupport I mean send messages hl7 but not in a test?
Not at the moment, I wrote this post when I was working in my previous company. Nevertheless, have a look to the post “How to create a camel HL7 listener”. The server side should be the same, you need to create an endpoint and then send the message to that endpoint. Try to do the same and let me know how far do you get.
Btw there is a book called “Camel in action” which was very helpful for me at that moment, is it worth to have a look.
Thanks for posting these articles. Wish I found them sooner.
I’m using Camel, AngularJS and just started with the traditional; HL7 HAPI libs. Found it useful to add Hawt.io UI for support and debugging. We’re mostly using Camel with HL7 FHIR using the hl7 FHIR libs and the HAPI FHIR Server (HAPI server is looking like it could be very useful for small to medium applications)