Skip to content

Commit

Permalink
Expand HL7 test coverage
Browse files Browse the repository at this point in the history
* Setting custom charset
* ack expression

Fixes #2520
  • Loading branch information
jamesnetherton committed Apr 29, 2021
1 parent 0ac2afc commit f6480bc
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 7 deletions.
1 change: 1 addition & 0 deletions integration-tests/hl7/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
id_file
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
*/
package org.apache.camel.quarkus.component.hl7.it;

import java.util.HashMap;
import java.util.Map;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.json.Json;
Expand All @@ -25,6 +28,7 @@
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

Expand Down Expand Up @@ -73,15 +77,24 @@ public JsonObject mllp(String message) throws Exception {
@POST
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.TEXT_PLAIN)
public String marshalUnmarshal(String message) throws Exception {
return producerTemplate.requestBody("direct:marshalUnmarshal", message, String.class);
public Response marshalUnmarshal(@QueryParam("charset") String charset, String message) {
Response.ResponseBuilder builder = Response.ok();
Map<String, Object> headers = new HashMap<>();
if (charset != null) {
headers.put(Exchange.CHARSET_NAME, charset);
builder.header("Content-Type", MediaType.TEXT_PLAIN + ";" + charset);
}

String result = producerTemplate.requestBodyAndHeaders("direct:marshalUnmarshal", message, headers, String.class);

return builder.entity(result).build();
}

@Path("/validate")
@POST
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.TEXT_PLAIN)
public Response validate(String message) throws Exception {
public Response validate(String message) {
Exchange exchange = producerTemplate.request("direct:validate", e -> e.getMessage().setBody(message));
if (exchange.isFailed()) {
Exception exception = exchange.getException();
Expand All @@ -94,7 +107,7 @@ public Response validate(String message) throws Exception {
@POST
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.TEXT_PLAIN)
public Response validateCustom(String message) throws Exception {
public Response validateCustom(String message) {
Exchange exchange = producerTemplate.request("direct:validateCustom", e -> e.getMessage().setBody(message));
if (exchange.isFailed()) {
Exception exception = exchange.getException();
Expand All @@ -107,7 +120,7 @@ public Response validateCustom(String message) throws Exception {
@POST
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.TEXT_PLAIN)
public String hl7terser(String message) throws Exception {
public String hl7terser(String message) {
Exchange exchange = producerTemplate.request("direct:hl7terser", e -> e.getMessage().setBody(message));
return exchange.getMessage().getHeader("PATIENT_ID", String.class);
}
Expand All @@ -116,19 +129,27 @@ public String hl7terser(String message) throws Exception {
@POST
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.TEXT_PLAIN)
public String hl7terserBean(String message) throws Exception {
public String hl7terserBean(String message) {
return producerTemplate.requestBody("direct:hl7terserBean", message, String.class);
}

@Path("/xml")
@POST
@Consumes(MediaType.APPLICATION_XML)
@Produces(MediaType.APPLICATION_JSON)
public JsonObject hl7Xml(String messageXml) throws Exception {
public JsonObject hl7Xml(String messageXml) {
ADT_A01 result = producerTemplate.requestBody("direct:unmarshalXml", messageXml, ADT_A01.class);
return adtToJsonObject(result);
}

@Path("/ack")
@POST
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.TEXT_PLAIN)
public String validateWithAck(String message) {
return producerTemplate.requestBody("direct:ack", message, String.class);
}

private JsonObject adtToJsonObject(ADT_A01 result) {
JsonObjectBuilder objectBuilder = Json.createObjectBuilder();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

import ca.uhn.hl7v2.AcknowledgmentCode;
import ca.uhn.hl7v2.model.v22.message.ADT_A01;
import ca.uhn.hl7v2.parser.Parser;
import org.apache.camel.builder.RouteBuilder;

import static org.apache.camel.component.hl7.HL7.ack;
import static org.apache.camel.component.hl7.HL7.hl7terser;

@ApplicationScoped
Expand Down Expand Up @@ -56,5 +58,9 @@ public void configure() throws Exception {

from("direct:unmarshalXml")
.unmarshal("hl7DataFormat");

from("direct:ack")
.unmarshal("hl7DataFormat")
.transform(ack(AcknowledgmentCode.CA));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.Test;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.is;

Expand Down Expand Up @@ -146,6 +147,67 @@ public void hl7Xml() throws HL7Exception {
"phone", is("(818)565-1551"));
}

@Test
public void testGetEncodingFromPid() {
String[] pidParts = PID_MESSAGE.split("\r");

// Set encoding
String header = pidParts[0] + "||||||ISO-8859-1";

// Add some characters to the patient name that the encoding cannot deal with
String pid = pidParts[1].replace("JOHN", "JÖHN").replace("SMITH", "SMÏTH");

// Verify the name field got messed up due to the encoding
String message = header + "\r" + pid;
RestAssured.given()
.body(message)
.post("/hl7/marshalUnmarshal")
.then()
.statusCode(200)
.body(containsString("SMÃ\u008FTH^JÃ\u0096HN^M"));

// Try again with UTF-8
message = pidParts[0] + "||||||UTF-8" + "\r" + pid;
RestAssured.given()
.body(message)
.post("/hl7/marshalUnmarshal")
.then()
.statusCode(200)
.body(containsString("SMÏTH^JÖHN^M"));
}

@Test
public void testGetEncodingFromHeader() {
// Verify the name field got messed up due to the encoding
String message = PID_MESSAGE.replace("JOHN", "JÖHN").replace("SMITH", "SMÏTH");
RestAssured.given()
.queryParam("charset", "US-ASCII")
.body(message)
.post("/hl7/marshalUnmarshal")
.then()
.statusCode(200)
.body(containsString("SM?TH^J?HN^M"));

// Try again with UTF-8
RestAssured.given()
.queryParam("charset", "UTF-8")
.body(message)
.post("/hl7/marshalUnmarshal")
.then()
.statusCode(200)
.body(containsString("SMÏTH^JÖHN^M"));
}

@Test
public void hl7CustomAck() {
RestAssured.given()
.body(PID_MESSAGE)
.post("/hl7/ack")
.then()
.statusCode(200)
.body(containsString("MSA|CA"));
}

private static final String readPidFile() {
try {
String pidContent = IOUtils.toString(Hl7Test.class.getResourceAsStream("/hl7-2.2-pid.txt"), StandardCharsets.UTF_8);
Expand Down

0 comments on commit f6480bc

Please sign in to comment.