This is an automated email from the ASF dual-hosted git repository.

jamesnetherton pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-quarkus.git


The following commit(s) were added to refs/heads/main by this push:
     new f6480bc  Expand HL7 test coverage
f6480bc is described below

commit f6480bcc879606011178577684bff4ea31e5a1cf
Author: James Netherton <[email protected]>
AuthorDate: Thu Apr 29 14:36:10 2021 +0100

    Expand HL7 test coverage
    
    * Setting custom charset
    * ack expression
    
    Fixes #2520
---
 integration-tests/hl7/.gitignore                   |  1 +
 .../quarkus/component/hl7/it/Hl7Resource.java      | 35 +++++++++---
 .../camel/quarkus/component/hl7/it/Hl7Routes.java  |  6 +++
 .../camel/quarkus/component/hl7/it/Hl7Test.java    | 62 ++++++++++++++++++++++
 4 files changed, 97 insertions(+), 7 deletions(-)

diff --git a/integration-tests/hl7/.gitignore b/integration-tests/hl7/.gitignore
new file mode 100644
index 0000000..05248b6
--- /dev/null
+++ b/integration-tests/hl7/.gitignore
@@ -0,0 +1 @@
+id_file
diff --git 
a/integration-tests/hl7/src/main/java/org/apache/camel/quarkus/component/hl7/it/Hl7Resource.java
 
b/integration-tests/hl7/src/main/java/org/apache/camel/quarkus/component/hl7/it/Hl7Resource.java
index f9c33d5..6672e27 100644
--- 
a/integration-tests/hl7/src/main/java/org/apache/camel/quarkus/component/hl7/it/Hl7Resource.java
+++ 
b/integration-tests/hl7/src/main/java/org/apache/camel/quarkus/component/hl7/it/Hl7Resource.java
@@ -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;
@@ -25,6 +28,7 @@ import javax.ws.rs.Consumes;
 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;
 
@@ -73,15 +77,24 @@ public class Hl7Resource {
     @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();
@@ -94,7 +107,7 @@ public class Hl7Resource {
     @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();
@@ -107,7 +120,7 @@ public class Hl7Resource {
     @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);
     }
@@ -116,7 +129,7 @@ public class Hl7Resource {
     @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);
     }
 
@@ -124,11 +137,19 @@ public class Hl7Resource {
     @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();
 
diff --git 
a/integration-tests/hl7/src/main/java/org/apache/camel/quarkus/component/hl7/it/Hl7Routes.java
 
b/integration-tests/hl7/src/main/java/org/apache/camel/quarkus/component/hl7/it/Hl7Routes.java
index 6daf8c2..6e57f83 100644
--- 
a/integration-tests/hl7/src/main/java/org/apache/camel/quarkus/component/hl7/it/Hl7Routes.java
+++ 
b/integration-tests/hl7/src/main/java/org/apache/camel/quarkus/component/hl7/it/Hl7Routes.java
@@ -19,10 +19,12 @@ package org.apache.camel.quarkus.component.hl7.it;
 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
@@ -56,5 +58,9 @@ public class Hl7Routes extends RouteBuilder {
 
         from("direct:unmarshalXml")
                 .unmarshal("hl7DataFormat");
+
+        from("direct:ack")
+                .unmarshal("hl7DataFormat")
+                .transform(ack(AcknowledgmentCode.CA));
     }
 }
diff --git 
a/integration-tests/hl7/src/test/java/org/apache/camel/quarkus/component/hl7/it/Hl7Test.java
 
b/integration-tests/hl7/src/test/java/org/apache/camel/quarkus/component/hl7/it/Hl7Test.java
index af55bbd..60ec127 100644
--- 
a/integration-tests/hl7/src/test/java/org/apache/camel/quarkus/component/hl7/it/Hl7Test.java
+++ 
b/integration-tests/hl7/src/test/java/org/apache/camel/quarkus/component/hl7/it/Hl7Test.java
@@ -35,6 +35,7 @@ import io.restassured.http.ContentType;
 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;
 
@@ -146,6 +147,67 @@ class Hl7Test {
                         "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);

Reply via email to