You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2016/12/16 21:59:12 UTC

[3/6] camel git commit: CAMEL-10511: Updated MllpTcpClientProducer and MllpTcpServerConsumer to consume all available data on socket - backport to 2.17

http://git-wip-us.apache.org/repos/asf/camel/blob/a53540da/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpTcpServerConsumerConnectionTest.java
----------------------------------------------------------------------
diff --git a/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpTcpServerConsumerConnectionTest.java b/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpTcpServerConsumerConnectionTest.java
index 99667b2..f4d46cb 100644
--- a/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpTcpServerConsumerConnectionTest.java
+++ b/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpTcpServerConsumerConnectionTest.java
@@ -16,9 +16,7 @@
  */
 package org.apache.camel.component.mllp;
 
-import java.net.InetSocketAddress;
-import java.net.Socket;
-import java.net.SocketAddress;
+import java.net.SocketException;
 import java.util.concurrent.TimeUnit;
 
 import org.apache.camel.EndpointInject;
@@ -26,25 +24,45 @@ import org.apache.camel.LoggingLevel;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
 import org.apache.camel.test.AvailablePortFinder;
+import org.apache.camel.test.junit.rule.mllp.MllpClientResource;
+import org.apache.camel.test.junit.rule.mllp.MllpJUnitResourceException;
 import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Rule;
 import org.junit.Test;
 
+import static org.apache.camel.component.mllp.MllpTcpServerConsumer.SOCKET_STARTUP_TEST_READ_TIMEOUT;
+import static org.apache.camel.component.mllp.MllpTcpServerConsumer.SOCKET_STARTUP_TEST_WAIT;
+
 public class MllpTcpServerConsumerConnectionTest extends CamelTestSupport {
-    String mllpHost = "localhost";
-    int mllpPort = AvailablePortFinder.getNextAvailable();
+    static final int RECEIVE_TIMEOUT = 500;
+
+    @Rule
+    public MllpClientResource mllpClient = new MllpClientResource();
+
 
     @EndpointInject(uri = "mock://result")
     MockEndpoint result;
 
     @Override
-    protected RouteBuilder createRouteBuilder() throws Exception {
-        mllpPort = AvailablePortFinder.getNextAvailable();
+    protected void doPreSetup() throws Exception {
+        mllpClient.setMllpHost("localhost");
+        mllpClient.setMllpPort(AvailablePortFinder.getNextAvailable());
 
+        super.doPreSetup();
+    }
+
+    @Override
+    public boolean isUseRouteBuilder() {
+        return false;
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {
             String routeId = "mllp-receiver";
 
             public void configure() {
-                fromF("mllp://%s:%d?autoAck=false", mllpHost, mllpPort)
+                fromF("mllp://%s:%d?autoAck=false", mllpClient.getMllpHost(), mllpClient.getMllpPort())
                         .log(LoggingLevel.INFO, routeId, "Receiving: ${body}")
                         .to(result);
             }
@@ -68,34 +86,96 @@ public class MllpTcpServerConsumerConnectionTest extends CamelTestSupport {
      * @throws Exception
      */
     @Test
-    public void testConnectWithoutData() throws Exception {
+    public void testConnectThenCloseWithoutData() throws Exception {
+        int connectionCount = 10;
+        long connectionMillis = 200;
+
         result.setExpectedCount(0);
+        result.setAssertPeriod(SOCKET_STARTUP_TEST_WAIT + SOCKET_STARTUP_TEST_READ_TIMEOUT);
+
+        addTestRoute(-1);
+
+        for (int i = 1; i <= connectionCount; ++i) {
+            mllpClient.connect();
+            Thread.sleep(connectionMillis);
+            mllpClient.close();
+        }
+
+        // Connect one more time and allow a client thread to start
+        mllpClient.connect();
+        Thread.sleep(SOCKET_STARTUP_TEST_WAIT + SOCKET_STARTUP_TEST_READ_TIMEOUT + 1000);
+        mllpClient.close();
+
+        assertMockEndpointsSatisfied(15, TimeUnit.SECONDS);
+    }
+
+    @Test
+    public void testConnectThenResetWithoutData() throws Exception {
         int connectionCount = 10;
+        long connectionMillis = 200;
+
+        result.setExpectedCount(0);
+        result.setAssertPeriod(SOCKET_STARTUP_TEST_WAIT + SOCKET_STARTUP_TEST_READ_TIMEOUT);
+
+        addTestRoute(-1);
+
+        for (int i = 1; i <= connectionCount; ++i) {
+            mllpClient.connect();
+            Thread.sleep(connectionMillis);
+            mllpClient.reset();
+        }
+
+        // Connect one more time and allow a client thread to start
+        mllpClient.connect();
+        Thread.sleep(SOCKET_STARTUP_TEST_WAIT + SOCKET_STARTUP_TEST_READ_TIMEOUT + 1000);
+        mllpClient.reset();
+
+        assertMockEndpointsSatisfied(15, TimeUnit.SECONDS);
+    }
+
+    /**
+     * Simulate an Idle Client
+     *
+     * @throws Exception
+     */
+    @Test
+    public void testIdleConnection() throws Exception {
+        final int maxReceiveTimeouts = 3;
+        String testMessage = "MSH|^~\\&|ADT|EPIC|JCAPS|CC|20160902123950|RISTECH|ADT^A08|00001|D|2.3|||||||" + '\r' + '\n';
+
+        result.setExpectedCount(1);
+        result.setAssertPeriod(1000);
+
+        addTestRoute(maxReceiveTimeouts);
+
+        mllpClient.connect();
+        mllpClient.sendMessageAndWaitForAcknowledgement(testMessage);
+        Thread.sleep(RECEIVE_TIMEOUT * (maxReceiveTimeouts + 1));
 
-        Socket dummyLoadBalancerSocket = null;
-        SocketAddress address = new InetSocketAddress(mllpHost, mllpPort);
-        int connectTimeout = 5000;
         try {
-            for (int i = 1; i <= connectionCount; ++i) {
-                log.debug("Creating connection #{}", i);
-                dummyLoadBalancerSocket = new Socket();
-                dummyLoadBalancerSocket.connect(address, connectTimeout);
-                log.debug("Closing connection #{}", i);
-                dummyLoadBalancerSocket.close();
-                Thread.sleep(1000);
-            }
-        } finally {
-            if (null != dummyLoadBalancerSocket) {
-                try {
-                    dummyLoadBalancerSocket.close();
-                } catch (Exception ex) {
-                    log.warn("Exception encountered closing dummy load balancer socket", ex);
-                }
-            }
+            mllpClient.sendMessageAndWaitForAcknowledgement(testMessage);
+            fail("The MllpClientResource should have thrown an exception when writing to the reset socket");
+        } catch (MllpJUnitResourceException ex) {
+            Throwable cause = ex.getCause();
+            assertIsInstanceOf(SocketException.class, cause);
         }
 
         assertMockEndpointsSatisfied(15, TimeUnit.SECONDS);
     }
 
+    void addTestRoute(final int maxReceiveTimeouts) throws Exception {
+        RouteBuilder builder = new RouteBuilder() {
+            String routeId = "mllp-receiver";
+
+            public void configure() {
+                fromF("mllp://%s:%d?receiveTimeout=%d&maxReceiveTimeouts=%d", mllpClient.getMllpHost(), mllpClient.getMllpPort(), RECEIVE_TIMEOUT, maxReceiveTimeouts)
+                        .log(LoggingLevel.INFO, routeId, "Receiving: ${body}")
+                        .to(result);
+            }
+        };
+
+        context.addRoutes(builder);
+        context.start();
+    }
 
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/a53540da/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpTcpServerConsumerMessageHeadersTest.java
----------------------------------------------------------------------
diff --git a/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpTcpServerConsumerMessageHeadersTest.java b/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpTcpServerConsumerMessageHeadersTest.java
new file mode 100644
index 0000000..caf4d70
--- /dev/null
+++ b/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpTcpServerConsumerMessageHeadersTest.java
@@ -0,0 +1,158 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.mllp;
+
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.EndpointInject;
+import org.apache.camel.LoggingLevel;
+import org.apache.camel.Message;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.test.AvailablePortFinder;
+import org.apache.camel.test.junit.rule.mllp.MllpClientResource;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Rule;
+import org.junit.Test;
+
+public class MllpTcpServerConsumerMessageHeadersTest extends CamelTestSupport {
+    @Rule
+    public MllpClientResource mllpClient = new MllpClientResource();
+
+    @EndpointInject(uri = "mock://result")
+    MockEndpoint result;
+
+    @EndpointInject(uri = "mock://on-completion-result")
+    MockEndpoint onCompletionResult;
+
+    @Override
+    protected CamelContext createCamelContext() throws Exception {
+        DefaultCamelContext context = (DefaultCamelContext) super.createCamelContext();
+
+        context.setUseMDCLogging(true);
+        context.setName(this.getClass().getSimpleName());
+
+        return context;
+    }
+
+    @Override
+    public boolean isUseRouteBuilder() {
+        return false;
+    }
+
+    @Override
+    protected void doPreSetup() throws Exception {
+        mllpClient.setMllpHost("localhost");
+        mllpClient.setMllpPort(AvailablePortFinder.getNextAvailable());
+
+        super.doPreSetup();
+    }
+
+    @Test
+    public void testHl7HeadersEnabled() throws Exception {
+        String testMessage = "MSH|^~\\&|ADT|EPIC|JCAPS|CC|20160902123950|RISTECH|ADT^A08|00001|D|2.3|||||||" + '\r' + '\n';
+
+        addTestRoute(true);
+
+        result.expectedMessageCount(1);
+
+        result.expectedHeaderReceived(MllpConstants.MLLP_SENDING_APPLICATION, "ADT");
+        result.expectedHeaderReceived(MllpConstants.MLLP_SENDING_FACILITY, "EPIC");
+        result.expectedHeaderReceived(MllpConstants.MLLP_RECEIVING_APPLICATION, "JCAPS");
+        result.expectedHeaderReceived(MllpConstants.MLLP_TIMESTAMP, "20160902123950");
+        result.expectedHeaderReceived(MllpConstants.MLLP_SECURITY, "RISTECH");
+        result.expectedHeaderReceived(MllpConstants.MLLP_MESSAGE_TYPE, "ADT^A08");
+        result.expectedHeaderReceived(MllpConstants.MLLP_EVENT_TYPE, "ADT");
+        result.expectedHeaderReceived(MllpConstants.MLLP_TRIGGER_EVENT, "A08");
+        result.expectedHeaderReceived(MllpConstants.MLLP_MESSAGE_CONTROL, "00001");
+        result.expectedHeaderReceived(MllpConstants.MLLP_PROCESSING_ID, "D");
+        result.expectedHeaderReceived(MllpConstants.MLLP_VERSION_ID, "2.3");
+
+        mllpClient.connect();
+
+        mllpClient.sendMessageAndWaitForAcknowledgement(testMessage, 10000);
+
+        assertMockEndpointsSatisfied(10, TimeUnit.SECONDS);
+
+        Message message = result.getExchanges().get(0).getIn();
+
+        assertNotNull("Should have header" + MllpConstants.MLLP_LOCAL_ADDRESS, message.getHeader(MllpConstants.MLLP_LOCAL_ADDRESS));
+        assertNotNull("Should have header" + MllpConstants.MLLP_REMOTE_ADDRESS, message.getHeader(MllpConstants.MLLP_REMOTE_ADDRESS));
+    }
+
+
+    @Test
+    public void testHl7HeadersDisabled() throws Exception {
+        String testMessage = "MSH|^~\\&|ADT|EPIC|JCAPS|CC|20160902123950|RISTECH|ADT^A08|00001|D|2.3|||||||" + '\r' + '\n';
+
+        addTestRoute(false);
+
+        result.expectedMessageCount(1);
+
+        mllpClient.connect();
+
+        mllpClient.sendMessageAndWaitForAcknowledgement(testMessage, 10000);
+
+        assertMockEndpointsSatisfied(10, TimeUnit.SECONDS);
+
+        Message message = result.getExchanges().get(0).getIn();
+
+        assertNotNull("Should have header" + MllpConstants.MLLP_LOCAL_ADDRESS, message.getHeader(MllpConstants.MLLP_LOCAL_ADDRESS));
+        assertNotNull("Should have header" + MllpConstants.MLLP_REMOTE_ADDRESS, message.getHeader(MllpConstants.MLLP_REMOTE_ADDRESS));
+
+        assertNull("Should NOT have header" + MllpConstants.MLLP_SENDING_APPLICATION, message.getHeader(MllpConstants.MLLP_SENDING_APPLICATION));
+        assertNull("Should NOT have header" + MllpConstants.MLLP_SENDING_FACILITY, message.getHeader(MllpConstants.MLLP_SENDING_FACILITY));
+        assertNull("Should NOT have header" + MllpConstants.MLLP_RECEIVING_APPLICATION, message.getHeader(MllpConstants.MLLP_RECEIVING_APPLICATION));
+        assertNull("Should NOT have header" + MllpConstants.MLLP_TIMESTAMP, message.getHeader(MllpConstants.MLLP_TIMESTAMP));
+        assertNull("Should NOT have header" + MllpConstants.MLLP_SECURITY, message.getHeader(MllpConstants.MLLP_SECURITY));
+        assertNull("Should NOT have header" + MllpConstants.MLLP_MESSAGE_TYPE, message.getHeader(MllpConstants.MLLP_MESSAGE_TYPE));
+        assertNull("Should NOT have header" + MllpConstants.MLLP_EVENT_TYPE, message.getHeader(MllpConstants.MLLP_EVENT_TYPE));
+        assertNull("Should NOT have header" + MllpConstants.MLLP_MESSAGE_CONTROL, message.getHeader(MllpConstants.MLLP_MESSAGE_CONTROL));
+        assertNull("Should NOT have header" + MllpConstants.MLLP_PROCESSING_ID, message.getHeader(MllpConstants.MLLP_PROCESSING_ID));
+        assertNull("Should NOT have header" + MllpConstants.MLLP_VERSION_ID, message.getHeader(MllpConstants.MLLP_VERSION_ID));
+    }
+
+    void addTestRoute(final boolean hl7Headers) throws Exception {
+        RouteBuilder builder = new RouteBuilder() {
+            int connectTimeout = 500;
+            int responseTimeout = 5000;
+
+            @Override
+            public void configure() throws Exception {
+                String routeId = "mllp-test-receiver-route";
+
+                onCompletion()
+                        .to("mock://on-completion-result")
+                        .toF("log:%s?level=INFO&showAll=true", routeId)
+                        .log(LoggingLevel.INFO, routeId, "Test route complete");
+
+                fromF("mllp://%s:%d?autoAck=true&connectTimeout=%d&receiveTimeout=%d&hl7Headers=%b",
+                        mllpClient.getMllpHost(), mllpClient.getMllpPort(), connectTimeout, responseTimeout, hl7Headers)
+                        .routeId(routeId)
+                        .log(LoggingLevel.INFO, routeId, "Test route received message")
+                        .to(result);
+
+            }
+        };
+        context.addRoutes(builder);
+        context.start();
+    }
+
+}
+

http://git-wip-us.apache.org/repos/asf/camel/blob/a53540da/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpTcpServerConsumerMessageValidationTest.java
----------------------------------------------------------------------
diff --git a/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpTcpServerConsumerMessageValidationTest.java b/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpTcpServerConsumerMessageValidationTest.java
new file mode 100644
index 0000000..5c431e6
--- /dev/null
+++ b/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpTcpServerConsumerMessageValidationTest.java
@@ -0,0 +1,317 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.mllp;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.EndpointInject;
+import org.apache.camel.LoggingLevel;
+import org.apache.camel.builder.NotifyBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.test.AvailablePortFinder;
+import org.apache.camel.test.junit.rule.mllp.MllpClientResource;
+import org.apache.camel.test.junit.rule.mllp.MllpJUnitResourceException;
+import org.apache.camel.test.junit.rule.mllp.MllpJUnitResourceTimeoutException;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Ignore;
+import org.junit.Rule;
+import org.junit.Test;
+
+import java.util.concurrent.TimeUnit;
+
+import static org.apache.camel.component.mllp.MllpEndpoint.END_OF_BLOCK;
+import static org.apache.camel.component.mllp.MllpEndpoint.START_OF_BLOCK;
+import static org.apache.camel.test.mllp.Hl7MessageGenerator.generateMessage;
+
+public class MllpTcpServerConsumerMessageValidationTest extends CamelTestSupport {
+    @Rule
+    public MllpClientResource mllpClient = new MllpClientResource();
+
+    @EndpointInject(uri = "mock://result")
+    MockEndpoint result;
+
+    @EndpointInject(uri = "mock://timeout-ex")
+    MockEndpoint timeout;
+
+    @EndpointInject(uri = "mock://invalid-ex")
+    MockEndpoint invalid;
+
+    @Override
+    protected CamelContext createCamelContext() throws Exception {
+        DefaultCamelContext context = (DefaultCamelContext) super.createCamelContext();
+
+        context.setUseMDCLogging(true);
+        context.setName(this.getClass().getSimpleName());
+
+        return context;
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+
+        mllpClient.setMllpHost("localhost");
+        mllpClient.setMllpPort(AvailablePortFinder.getNextAvailable());
+
+        return new RouteBuilder() {
+            int connectTimeout = 500;
+            int responseTimeout = 5000;
+
+            @Override
+            public void configure() throws Exception {
+                String routeId = "mllp-test-receiver-route";
+
+                onException(MllpTimeoutException.class)
+                        .to(timeout);
+
+                onException(MllpInvalidMessageException.class)
+                        .to(invalid);
+
+                fromF("mllp://%s:%d?validatePayload=true&autoAck=true&connectTimeout=%d&receiveTimeout=%d",
+                        mllpClient.getMllpHost(), mllpClient.getMllpPort(), connectTimeout, responseTimeout)
+                        .routeId(routeId)
+                        .log(LoggingLevel.INFO, routeId, "Test route received message")
+                        .to(result);
+
+            }
+        };
+    }
+
+    @Test
+    public void testReceiveSingleMessage() throws Exception {
+        result.expectedMessageCount(1);
+        result.setAssertPeriod(1000);
+        timeout.expectedMessageCount(0);
+        invalid.expectedMessageCount(0);
+
+        mllpClient.connect();
+
+        mllpClient.sendMessageAndWaitForAcknowledgement(generateMessage(), 10000);
+
+        assertMockEndpointsSatisfied(10, TimeUnit.SECONDS);
+    }
+
+    @Test
+    public void testReceiveSingleMessageWithDelayAfterConnection() throws Exception {
+        result.expectedMinimumMessageCount(1);
+        result.setAssertPeriod(1000);
+        timeout.expectedMessageCount(0);
+        invalid.expectedMessageCount(0);
+
+        mllpClient.connect();
+
+        Thread.sleep(5000);
+        mllpClient.sendMessageAndWaitForAcknowledgement(generateMessage(), 10000);
+
+        assertMockEndpointsSatisfied(10, TimeUnit.SECONDS);
+    }
+
+    @Test
+    public void testReceiveMultipleMessages() throws Exception {
+        int sendMessageCount = 5;
+        result.expectedMinimumMessageCount(5);
+        result.setAssertPeriod(1000);
+        timeout.expectedMessageCount(0);
+        invalid.expectedMessageCount(0);
+
+        mllpClient.connect();
+
+        for (int i = 1; i <= sendMessageCount; ++i) {
+            mllpClient.sendMessageAndWaitForAcknowledgement(generateMessage(i));
+        }
+
+        assertMockEndpointsSatisfied(10, TimeUnit.SECONDS);
+    }
+
+    @Test
+    public void testOpenMllpEnvelopeWithReset() throws Exception {
+        result.expectedMessageCount(4);
+        result.setAssertPeriod(1000);
+        timeout.expectedMessageCount(1);
+        invalid.expectedMessageCount(0);
+
+        NotifyBuilder notify1 = new NotifyBuilder(context).whenDone(2).create();
+        NotifyBuilder notify2 = new NotifyBuilder(context).whenDone(5).create();
+
+        mllpClient.connect();
+        mllpClient.setSoTimeout(10000);
+
+        log.info("Sending TEST_MESSAGE_1");
+        String acknowledgement1 = mllpClient.sendMessageAndWaitForAcknowledgement(generateMessage(1));
+
+        log.info("Sending TEST_MESSAGE_2");
+        String acknowledgement2 = mllpClient.sendMessageAndWaitForAcknowledgement(generateMessage(2));
+
+        assertTrue("First two normal exchanges did not complete", notify1.matches(10, TimeUnit.SECONDS));
+
+        log.info("Sending TEST_MESSAGE_3");
+        mllpClient.setSendEndOfBlock(false);
+        mllpClient.setSendEndOfData(false);
+        // Acknowledgement won't come here
+        try {
+            mllpClient.sendMessageAndWaitForAcknowledgement(generateMessage(3));
+        } catch (MllpJUnitResourceException resourceEx) {
+            log.info("Expected exception reading response");
+        }
+        mllpClient.disconnect();
+        Thread.sleep(1000);
+        mllpClient.connect();
+
+        log.info("Sending TEST_MESSAGE_4");
+        mllpClient.setSendEndOfBlock(true);
+        mllpClient.setSendEndOfData(true);
+        String acknowledgement4 = mllpClient.sendMessageAndWaitForAcknowledgement(generateMessage(4));
+
+        log.info("Sending TEST_MESSAGE_5");
+        String acknowledgement5 = mllpClient.sendMessageAndWaitForAcknowledgement(generateMessage(5));
+
+        assertTrue("Remaining exchanges did not complete", notify2.matches(10, TimeUnit.SECONDS));
+
+        assertMockEndpointsSatisfied(10, TimeUnit.SECONDS);
+
+        assertTrue("Should be acknowledgment for message 1", acknowledgement1.contains("MSA|AA|00001"));
+        assertTrue("Should be acknowledgment for message 2", acknowledgement2.contains("MSA|AA|00002"));
+        // assertTrue("Should be acknowledgment for message 3", acknowledgement3.contains("MSA|AA|00003"));
+        assertTrue("Should be acknowledgment for message 4", acknowledgement4.contains("MSA|AA|00004"));
+        assertTrue("Should be acknowledgment for message 5", acknowledgement5.contains("MSA|AA|00005"));
+    }
+
+    @Test
+    public void testMessageReadTimeout() throws Exception {
+        result.expectedMessageCount(0);
+        result.setAssertPeriod(1000);
+        timeout.expectedMessageCount(1);
+        invalid.expectedMessageCount(0);
+
+        NotifyBuilder notify = new NotifyBuilder(context).whenDone(1).create();
+
+        mllpClient.setSendEndOfBlock(false);
+        mllpClient.setSendEndOfData(false);
+
+        mllpClient.sendFramedData(generateMessage());
+
+        assertTrue("One exchange should have completed", notify.matches(15, TimeUnit.SECONDS));
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Test
+    public void testInvalidMessage() throws Exception {
+        result.expectedMessageCount(0);
+        result.setAssertPeriod(1000);
+        timeout.expectedMessageCount(0);
+        invalid.expectedMessageCount(1);
+
+        mllpClient.sendFramedData("INVALID PAYLOAD");
+
+        assertMockEndpointsSatisfied(15, TimeUnit.SECONDS);
+    }
+
+    @Test
+    public void testNthInvalidMessage() throws Exception {
+        int messageCount = 10;
+
+        result.expectedMessageCount(messageCount - 1);
+        result.setAssertPeriod(1000);
+        timeout.expectedMessageCount(0);
+        invalid.expectedMessageCount(1);
+
+        for (int i = 0; i < messageCount; ++i) {
+            if (i == messageCount / 2) {
+                try {
+                    mllpClient.sendMessageAndWaitForAcknowledgement("INVALID PAYLOAD");
+                    fail("An acknowledgement should not be received for an invalid HL7 message");
+                } catch (MllpJUnitResourceTimeoutException timeoutEx) {
+                    // expected - eat this
+                }
+            } else {
+                mllpClient.sendMessageAndWaitForAcknowledgement(generateMessage(i + 1));
+            }
+        }
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Test
+    public void testMessageContainingEmbeddedStartOfBlock() throws Exception {
+        result.expectedMessageCount(0);
+        result.setAssertPeriod(1000);
+        timeout.expectedMessageCount(0);
+        invalid.expectedMessageCount(1);
+
+        mllpClient.sendMessageAndWaitForAcknowledgement(generateMessage().replaceFirst("EVN", "EVN" + START_OF_BLOCK));
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Test
+    public void testNthMessageContainingEmbeddedStartOfBlock() throws Exception {
+        int messageCount = 10;
+
+        result.expectedMessageCount(messageCount - 1);
+        result.setAssertPeriod(1000);
+        timeout.expectedMessageCount(0);
+        invalid.expectedMessageCount(1);
+
+        for (int i = 0; i < messageCount; ++i) {
+            String message = (i == (messageCount/2))
+                    ? generateMessage(i + 1).replaceFirst("EVN", "EVN" + START_OF_BLOCK)
+                    : generateMessage(i + 1);
+
+            log.debug("Sending message {}", MllpComponent.covertToPrintFriendlyString(message));
+
+            mllpClient.sendMessageAndWaitForAcknowledgement(message);
+        }
+
+        assertMockEndpointsSatisfied(15, TimeUnit.SECONDS);
+    }
+
+    @Test
+    public void testMessageContainingEmbeddedEndOfBlock() throws Exception {
+        result.expectedMessageCount(0);
+        result.setAssertPeriod(1000);
+        timeout.expectedMessageCount(0);
+        invalid.expectedMessageCount(1);
+
+        mllpClient.sendMessageAndWaitForAcknowledgement(generateMessage().replaceFirst("EVN", "EVN" + END_OF_BLOCK));
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Test
+    public void testInvalidMessageContainingEmbeddedEndOfBlock() throws Exception {
+        int messageCount = 10;
+
+        result.expectedMessageCount(messageCount - 1);
+        result.setAssertPeriod(1000);
+        timeout.expectedMessageCount(0);
+        invalid.expectedMessageCount(1);
+
+        for (int i = 0; i < messageCount; ++i) {
+            String message = (i == (messageCount/2))
+                    ? generateMessage(i + 1).replaceFirst("EVN", "EVN" + END_OF_BLOCK)
+                    : generateMessage(i + 1);
+
+            log.debug("Sending message {}", MllpComponent.covertToPrintFriendlyString(message));
+
+            mllpClient.sendMessageAndWaitForAcknowledgement(message);
+        }
+
+        assertMockEndpointsSatisfied(15, TimeUnit.SECONDS);
+    }
+}
+

http://git-wip-us.apache.org/repos/asf/camel/blob/a53540da/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpTcpServerConsumerTest.java
----------------------------------------------------------------------
diff --git a/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpTcpServerConsumerTest.java b/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpTcpServerConsumerTest.java
index eb6a463..4060626 100644
--- a/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpTcpServerConsumerTest.java
+++ b/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpTcpServerConsumerTest.java
@@ -16,10 +16,6 @@
  */
 package org.apache.camel.component.mllp;
 
-import java.net.BindException;
-import java.net.ServerSocket;
-import java.util.concurrent.TimeUnit;
-
 import org.apache.camel.CamelContext;
 import org.apache.camel.EndpointInject;
 import org.apache.camel.LoggingLevel;
@@ -30,10 +26,15 @@ import org.apache.camel.impl.DefaultCamelContext;
 import org.apache.camel.test.AvailablePortFinder;
 import org.apache.camel.test.junit.rule.mllp.MllpClientResource;
 import org.apache.camel.test.junit.rule.mllp.MllpJUnitResourceException;
+import org.apache.camel.test.junit.rule.mllp.MllpJUnitResourceTimeoutException;
 import org.apache.camel.test.junit4.CamelTestSupport;
 import org.junit.Rule;
 import org.junit.Test;
 
+import java.util.concurrent.TimeUnit;
+
+import static org.apache.camel.component.mllp.MllpEndpoint.END_OF_BLOCK;
+import static org.apache.camel.component.mllp.MllpEndpoint.START_OF_BLOCK;
 import static org.apache.camel.test.mllp.Hl7MessageGenerator.generateMessage;
 
 public class MllpTcpServerConsumerTest extends CamelTestSupport {
@@ -43,6 +44,9 @@ public class MllpTcpServerConsumerTest extends CamelTestSupport {
     @EndpointInject(uri = "mock://result")
     MockEndpoint result;
 
+    @EndpointInject(uri = "mock://timeout-ex")
+    MockEndpoint timeout;
+
     @Override
     protected CamelContext createCamelContext() throws Exception {
         DefaultCamelContext context = (DefaultCamelContext) super.createCamelContext();
@@ -67,9 +71,8 @@ public class MllpTcpServerConsumerTest extends CamelTestSupport {
             public void configure() throws Exception {
                 String routeId = "mllp-test-receiver-route";
 
-                onCompletion()
-                        .toF("log:%s?level=INFO&showAll=true", routeId)
-                        .log(LoggingLevel.INFO, routeId, "Test route complete");
+                onException(MllpTimeoutException.class)
+                        .to(timeout);
 
                 fromF("mllp://%s:%d?autoAck=true&connectTimeout=%d&receiveTimeout=%d",
                         mllpClient.getMllpHost(), mllpClient.getMllpPort(), connectTimeout, responseTimeout)
@@ -84,6 +87,7 @@ public class MllpTcpServerConsumerTest extends CamelTestSupport {
     @Test
     public void testReceiveSingleMessage() throws Exception {
         result.expectedMessageCount(1);
+        timeout.expectedMessageCount(0);
 
         mllpClient.connect();
 
@@ -95,6 +99,7 @@ public class MllpTcpServerConsumerTest extends CamelTestSupport {
     @Test
     public void testReceiveSingleMessageWithDelayAfterConnection() throws Exception {
         result.expectedMinimumMessageCount(1);
+        timeout.expectedMessageCount(0);
 
         mllpClient.connect();
 
@@ -108,6 +113,7 @@ public class MllpTcpServerConsumerTest extends CamelTestSupport {
     public void testReceiveMultipleMessages() throws Exception {
         int sendMessageCount = 5;
         result.expectedMinimumMessageCount(5);
+        timeout.expectedMessageCount(0);
 
         mllpClient.connect();
 
@@ -121,6 +127,7 @@ public class MllpTcpServerConsumerTest extends CamelTestSupport {
     @Test
     public void testOpenMllpEnvelopeWithReset() throws Exception {
         result.expectedMessageCount(4);
+        timeout.expectedMessageCount(1);
         NotifyBuilder notify1 = new NotifyBuilder(context).whenDone(2).create();
         NotifyBuilder notify2 = new NotifyBuilder(context).whenDone(5).create();
 
@@ -167,5 +174,114 @@ public class MllpTcpServerConsumerTest extends CamelTestSupport {
         assertTrue("Should be acknowledgment for message 5", acknowledgement5.contains("MSA|AA|00005"));
     }
 
+    @Test
+    public void testMessageReadTimeout() throws Exception {
+        result.expectedMessageCount(0);
+        timeout.expectedMessageCount(1);
+
+        NotifyBuilder notify = new NotifyBuilder(context).whenDone(1).create();
+
+        mllpClient.setSendEndOfBlock(false);
+        mllpClient.setSendEndOfData(false);
+
+        mllpClient.sendFramedData(generateMessage());
+
+        assertTrue("One exchange should have completed", notify.matches(15, TimeUnit.SECONDS));
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Test
+    public void testInvalidMessage() throws Exception {
+        result.expectedMessageCount(1);
+        timeout.expectedMessageCount(0);
+
+        mllpClient.sendFramedData("INVALID PAYLOAD");
+
+        assertMockEndpointsSatisfied(15, TimeUnit.SECONDS);
+    }
+
+    @Test
+    public void testNthInvalidMessage() throws Exception {
+        int messageCount = 10;
+
+        result.expectedMessageCount(messageCount);
+        timeout.expectedMessageCount(0);
+
+        for (int i = 0; i < messageCount; ++i) {
+            if (i == messageCount / 2) {
+                try {
+                    mllpClient.sendMessageAndWaitForAcknowledgement("INVALID PAYLOAD");
+                    fail("An acknowledgement should not be received for an invalid HL7 message");
+                } catch (MllpJUnitResourceTimeoutException timeoutEx) {
+                    // expected - eat this
+                }
+            } else {
+                mllpClient.sendMessageAndWaitForAcknowledgement(generateMessage(i + 1));
+            }
+        }
+
+        assertMockEndpointsSatisfied(15, TimeUnit.SECONDS);
+    }
+
+    @Test
+    public void testMessageContainingEmbeddedStartOfBlock() throws Exception {
+        result.expectedMessageCount(1);
+        timeout.expectedMessageCount(0);
+
+        mllpClient.sendMessageAndWaitForAcknowledgement(generateMessage().replaceFirst("EVN", "EVN" + START_OF_BLOCK));
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Test
+    public void testNthMessageContainingEmbeddedStartOfBlock() throws Exception {
+        int messageCount = 10;
+
+        result.expectedMessageCount(messageCount);
+        timeout.expectedMessageCount(0);
+
+        for (int i = 0; i < messageCount; ++i) {
+            String message = (i == (messageCount/2))
+                    ? generateMessage(i + 1).replaceFirst("EVN", "EVN" + START_OF_BLOCK)
+                    : generateMessage(i + 1);
+
+            log.debug("Sending message {}", MllpComponent.covertToPrintFriendlyString(message));
+
+            mllpClient.sendMessageAndWaitForAcknowledgement(message);
+        }
+
+        assertMockEndpointsSatisfied(15, TimeUnit.SECONDS);
+    }
+
+    @Test
+    public void testMessageContainingEmbeddedEndOfBlock() throws Exception {
+        result.expectedMessageCount(1);
+        timeout.expectedMessageCount(0);
+
+        mllpClient.sendMessageAndWaitForAcknowledgement(generateMessage().replaceFirst("EVN", "EVN" + END_OF_BLOCK));
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Test
+    public void testInvalidMessageContainingEmbeddedEndOfBlock() throws Exception {
+        int messageCount = 10;
+
+        result.expectedMessageCount(messageCount);
+        timeout.expectedMessageCount(0);
+
+        for (int i = 0; i < messageCount; ++i) {
+            String message = (i == (messageCount/2))
+                    ? generateMessage(i + 1).replaceFirst("EVN", "EVN" + END_OF_BLOCK)
+                    : generateMessage(i + 1);
+
+            log.debug("Sending message {}", MllpComponent.covertToPrintFriendlyString(message));
+
+            mllpClient.sendMessageAndWaitForAcknowledgement(message);
+        }
+
+        assertMockEndpointsSatisfied(15, TimeUnit.SECONDS);
+    }
 }
 

http://git-wip-us.apache.org/repos/asf/camel/blob/a53540da/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpTcpServerConsumerTransactionTest.java
----------------------------------------------------------------------
diff --git a/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpTcpServerConsumerTransactionTest.java b/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpTcpServerConsumerTransactionTest.java
new file mode 100644
index 0000000..91e31ff
--- /dev/null
+++ b/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpTcpServerConsumerTransactionTest.java
@@ -0,0 +1,142 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.mllp;
+
+import java.util.concurrent.TimeUnit;
+
+import org.apache.activemq.junit.EmbeddedActiveMQBroker;
+import org.apache.camel.CamelContext;
+import org.apache.camel.EndpointInject;
+import org.apache.camel.LoggingLevel;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.component.sjms.SjmsComponent;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.impl.JndiRegistry;
+import org.apache.camel.test.AvailablePortFinder;
+import org.apache.camel.test.junit.rule.mllp.MllpClientResource;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Rule;
+import org.junit.Test;
+
+import static org.apache.camel.test.mllp.Hl7MessageGenerator.generateMessage;
+
+public class MllpTcpServerConsumerTransactionTest extends CamelTestSupport {
+    @Rule
+    public EmbeddedActiveMQBroker broker = new EmbeddedActiveMQBroker();
+
+    @Rule
+    public MllpClientResource mllpClient = new MllpClientResource();
+
+    @EndpointInject(uri = "mock://result")
+    MockEndpoint result;
+
+    @EndpointInject(uri = "mock://on-complete-only")
+    MockEndpoint complete;
+
+    @EndpointInject(uri = "mock://on-failure-only")
+    MockEndpoint failure;
+
+    @Override
+    protected JndiRegistry createRegistry() throws Exception {
+        JndiRegistry registry = super.createRegistry();
+
+        SjmsComponent target = new SjmsComponent();
+        target.setConnectionFactory(broker.createConnectionFactory());
+        registry.bind("target", target);
+
+        return registry;
+    }
+
+    @Override
+    protected CamelContext createCamelContext() throws Exception {
+        DefaultCamelContext context = (DefaultCamelContext) super.createCamelContext();
+
+        context.setUseMDCLogging(true);
+        context.setName(this.getClass().getSimpleName());
+
+        return context;
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+
+        mllpClient.setMllpHost("localhost");
+        mllpClient.setMllpPort(AvailablePortFinder.getNextAvailable());
+
+        return new RouteBuilder() {
+            int connectTimeout = 500;
+            int responseTimeout = 5000;
+
+            @Override
+            public void configure() throws Exception {
+                String routeId = "mllp-test-receiver-route";
+
+                onCompletion()
+                        .onCompleteOnly()
+                        .log(LoggingLevel.INFO, routeId, "Test route complete")
+                        .to(complete);
+
+                onCompletion()
+                        .onFailureOnly()
+                        .log(LoggingLevel.INFO, routeId, "Test route failed")
+                        .to(failure);
+
+                fromF("mllp://%s:%d?autoAck=true&connectTimeout=%d&receiveTimeout=%d",
+                        mllpClient.getMllpHost(), mllpClient.getMllpPort(), connectTimeout, responseTimeout)
+                        .routeId(routeId)
+                        .log(LoggingLevel.INFO, routeId, "Test route received message")
+                        .to("target://test-queue?transacted=true");
+
+                from("target://test-queue")
+                        .routeId("jms-consumer")
+                        .log(LoggingLevel.INFO, routeId, "Test JMS Consumer received message")
+                        .to(result);
+
+            }
+        };
+    }
+
+    @Test
+    public void testReceiveSingleMessage() throws Exception {
+        result.expectedMessageCount(1);
+        complete.expectedMessageCount(1);
+        failure.expectedMessageCount(0);
+
+        mllpClient.connect();
+
+        mllpClient.sendMessageAndWaitForAcknowledgement(generateMessage(), 10000);
+
+        assertMockEndpointsSatisfied(10, TimeUnit.SECONDS);
+    }
+
+    @Test
+    public void testAcknowledgementWriteFailure() throws Exception {
+        result.expectedMessageCount(0);
+        result.setAssertPeriod(1000);
+        complete.expectedMessageCount(0);
+        failure.expectedMessageCount(1);
+
+        mllpClient.connect();
+        mllpClient.setDisconnectMethod(MllpClientResource.DisconnectMethod.RESET);
+
+        mllpClient.sendFramedData(generateMessage(), true);
+
+        assertMockEndpointsSatisfied(10, TimeUnit.SECONDS);
+    }
+}
+

http://git-wip-us.apache.org/repos/asf/camel/blob/a53540da/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpTcpServerProducerBlueprintTest.java
----------------------------------------------------------------------
diff --git a/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpTcpServerProducerBlueprintTest.java b/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpTcpServerProducerBlueprintTest.java
deleted file mode 100644
index dc796a5..0000000
--- a/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpTcpServerProducerBlueprintTest.java
+++ /dev/null
@@ -1,35 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.camel.component.mllp;
-
-import org.apache.camel.EndpointInject;
-import org.apache.camel.component.mock.MockEndpoint;
-import org.apache.camel.test.blueprint.CamelBlueprintTestSupport;
-import org.junit.Ignore;
-
-@Ignore(value = "Not Yet Implemented")
-// TODO: Implement this
-public class MllpTcpServerProducerBlueprintTest extends CamelBlueprintTestSupport {
-    @EndpointInject(uri = "mock://target")
-    MockEndpoint target;
-
-    @Override
-    protected String getBlueprintDescriptor() {
-        return "OSGI-INF/blueprint/mllp-tcp-server-producer.xml";
-    }
-
-}

http://git-wip-us.apache.org/repos/asf/camel/blob/a53540da/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/impl/Hl7UtilTest.java
----------------------------------------------------------------------
diff --git a/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/impl/Hl7UtilTest.java b/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/impl/Hl7UtilTest.java
new file mode 100644
index 0000000..e7d443c
--- /dev/null
+++ b/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/impl/Hl7UtilTest.java
@@ -0,0 +1,126 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.mllp.impl;
+
+import java.io.ByteArrayOutputStream;
+
+import org.junit.Test;
+
+import static org.apache.camel.component.mllp.MllpEndpoint.END_OF_BLOCK;
+import static org.apache.camel.component.mllp.MllpEndpoint.START_OF_BLOCK;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+public class Hl7UtilTest {
+    static final String TEST_MESSAGE =
+        "MSH|^~\\&|ADT|EPIC|JCAPS|CC|20161206193919|RISTECH|ADT^A08|00001|D|2.3^^|||||||" + '\r'
+            + "EVN|A08|20150107161440||REG_UPDATE_SEND_VISIT_MESSAGES_ON_PATIENT_CHANGES|RISTECH^RADIOLOGY^TECHNOLOGIST^^^^^^UCLA^^^^^RRMC||" + '\r'
+            + "PID|1|2100355^^^MRN^MRN|2100355^^^MRN^MRN||MDCLS9^MC9||19700109|F||U|111 HOVER STREET^^LOS ANGELES^CA^90032^USA^P^^LOS ANGELE|"
+                + "LOS ANGELE|(310)725-6952^P^PH^^^310^7256952||ENGLISH|U||60000013647|565-33-2222|||U||||||||N||" + '\r'
+            + "PD1|||UCLA HEALTH SYSTEM^^10|10002116^ADAMS^JOHN^D^^^^^EPIC^^^^PROVID||||||||||||||" + '\r'
+            + "NK1|1|DOE^MC9^^|OTH|^^^^^USA|(310)888-9999^^^^^310^8889999|(310)999-2222^^^^^310^9992222|Emergency Contact 1|||||||||||||||||||||||||||" + '\r'
+            + "PV1|1|OUTPATIENT|RR CT^^^1000^^^^^^^DEPID|EL|||017511^TOBIAS^JONATHAN^^^^^^EPIC^^^^PROVID|017511^TOBIAS^JONATHAN^^^^^^EPIC^^^^PROVID||||||"
+                + "CLR|||||60000013647|SELF|||||||||||||||||||||HOV_CONF|^^^1000^^^^^^^||20150107161438||||||||||" + '\r'
+            + "PV2||||||||20150107161438||||CT BRAIN W WO CONTRAST||||||||||N|||||||||||||||||||||||||||" + '\r'
+            + "ZPV||||||||||||20150107161438|||||||||" + '\r'
+            + "AL1|1||33361^NO KNOWN ALLERGIES^^NOTCOMPUTRITION^NO KNOWN ALLERGIES^EXTELG||||||" + '\r'
+            + "DG1|1|DX|784.0^Headache^DX|Headache||VISIT" + '\r'
+            + "GT1|1|1000235129|MDCLS9^MC9^^||111 HOVER STREET^^LOS ANGELES^CA^90032^USA^^^LOS ANGELE|(310)725-6952^^^^^310^7256952||19700109|F|P/F|SLF|"
+                + "565-33-2222|||||^^^^^USA|||UNKNOWN|||||||||||||||||||||||||||||" + '\r'
+            + "UB2||||||||" + '\r'
+            + '\n';
+
+    @Test
+    public void testGenerateInvalidPayloadExceptionMessage() throws Exception {
+        String message = Hl7Util.generateInvalidPayloadExceptionMessage(TEST_MESSAGE.getBytes());
+
+        assertNull("Valid payload should result in a null message", message);
+    }
+
+    @Test
+    public void testGenerateInvalidPayloadExceptionMessageWithLengthLargerThanArraySize() throws Exception {
+        byte[] payload = TEST_MESSAGE.getBytes();
+        String message = Hl7Util.generateInvalidPayloadExceptionMessage(payload, payload.length * 2);
+
+        assertNull("Valid payload should result in a null message", message);
+    }
+
+    @Test
+    public void testGenerateInvalidPayloadExceptionMessageWithLengthSmallerThanArraySize() throws Exception {
+        byte[] payload = TEST_MESSAGE.getBytes();
+        String message = Hl7Util.generateInvalidPayloadExceptionMessage(payload, 10);
+
+        assertEquals("The HL7 payload terminating bytes [0x7c, 0x41] are incorrect - expected [0xd, 0xa]  {ASCII [<CR>, <LF>]}", message);
+    }
+
+    @Test
+    public void testGenerateInvalidPayloadExceptionMessageWithNullPayload() throws Exception {
+        assertEquals("HL7 payload is null", Hl7Util.generateInvalidPayloadExceptionMessage(null));
+    }
+
+    @Test
+    public void testGenerateInvalidPayloadExceptionMessageWithInvalidStartingSegment() throws Exception {
+        byte[] invalidStartingSegment = "MSA|AA|00001|\r".getBytes();
+        byte[] basePayload = TEST_MESSAGE.getBytes();
+
+        ByteArrayOutputStream payloadStream = new ByteArrayOutputStream(invalidStartingSegment.length + basePayload.length);
+        payloadStream.write(invalidStartingSegment);
+        payloadStream.write(basePayload.length);
+
+        assertEquals("The first segment of the HL7 payload {MSA} is not an MSH segment", Hl7Util.generateInvalidPayloadExceptionMessage(payloadStream.toByteArray()));
+    }
+
+    @Test
+    public void testGenerateInvalidPayloadExceptionMessageWithEmptyPayload() throws Exception {
+        byte[] payload = new byte[0];
+
+        assertEquals("HL7 payload is empty", Hl7Util.generateInvalidPayloadExceptionMessage(payload));
+        assertEquals("HL7 payload is empty", Hl7Util.generateInvalidPayloadExceptionMessage(payload, payload.length));
+    }
+
+    @Test
+    public void testGenerateInvalidPayloadExceptionMessageWithEmbeddedStartOfBlock() throws Exception {
+        byte[] basePayload = TEST_MESSAGE.getBytes();
+
+        ByteArrayOutputStream payloadStream = new ByteArrayOutputStream(basePayload.length + 1);
+
+        int embeddedStartOfBlockIndex = basePayload.length / 2;
+        payloadStream.write(basePayload, 0, embeddedStartOfBlockIndex);
+        payloadStream.write(START_OF_BLOCK);
+        payloadStream.write(basePayload, embeddedStartOfBlockIndex, basePayload.length - embeddedStartOfBlockIndex);
+
+        String expected = "HL7 payload contains an embedded START_OF_BLOCK {0xb, ASCII <VT>} at index " + embeddedStartOfBlockIndex;
+
+        assertEquals(expected, Hl7Util.generateInvalidPayloadExceptionMessage(payloadStream.toByteArray()));
+    }
+
+    @Test
+    public void testGenerateInvalidPayloadExceptionMessageWithEmbeddedEndOfBlock() throws Exception {
+        byte[] basePayload = TEST_MESSAGE.getBytes();
+
+        ByteArrayOutputStream payloadStream = new ByteArrayOutputStream(basePayload.length + 1);
+
+        int embeddedEndOfBlockIndex = basePayload.length / 2;
+        payloadStream.write(basePayload, 0, embeddedEndOfBlockIndex);
+        payloadStream.write(END_OF_BLOCK);
+        payloadStream.write(basePayload, embeddedEndOfBlockIndex, basePayload.length - embeddedEndOfBlockIndex);
+
+        String expected = "HL7 payload contains an embedded END_OF_BLOCK {0x1c, ASCII <FS>} at index " + embeddedEndOfBlockIndex;
+
+        assertEquals(expected, Hl7Util.generateInvalidPayloadExceptionMessage(payloadStream.toByteArray()));
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/a53540da/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/impl/MllpBufferedSocketAcknowledgementWriterTest.java
----------------------------------------------------------------------
diff --git a/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/impl/MllpBufferedSocketAcknowledgementWriterTest.java b/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/impl/MllpBufferedSocketAcknowledgementWriterTest.java
new file mode 100644
index 0000000..41fa6e5
--- /dev/null
+++ b/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/impl/MllpBufferedSocketAcknowledgementWriterTest.java
@@ -0,0 +1,125 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.mllp.impl;
+
+import org.apache.camel.component.mllp.MllpAcknowledgementDeliveryException;
+import org.apache.camel.test.util.PayloadBuilder;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.apache.camel.component.mllp.MllpEndpoint.END_OF_BLOCK;
+import static org.apache.camel.component.mllp.MllpEndpoint.END_OF_DATA;
+import static org.apache.camel.component.mllp.MllpEndpoint.START_OF_BLOCK;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertNotNull;
+
+public class MllpBufferedSocketAcknowledgementWriterTest extends MllpSocketWriterTestSupport {
+    MllpSocketWriter mllpSocketWriter;
+
+    @Before
+    public void setUp() throws Exception {
+        mllpSocketWriter = new MllpBufferedSocketWriter(fakeSocket, true);
+    }
+
+    @Test
+    public void testWriteAcknowledgement() throws Exception {
+        byte[] expected = PayloadBuilder.build(START_OF_BLOCK, TEST_ACKNOWLEDGEMENT, END_OF_BLOCK, END_OF_DATA);
+
+        mllpSocketWriter.writeEnvelopedPayload(TEST_MESSAGE.getBytes(), TEST_ACKNOWLEDGEMENT.getBytes());
+
+        assertArrayEquals(expected, fakeSocket.payload());
+    }
+
+    @Test
+    public void testWriteNullAcknowledgement() throws Exception {
+        byte[] acknowledgement = null;
+
+        byte[] expected = PayloadBuilder.build(START_OF_BLOCK, END_OF_BLOCK, END_OF_DATA);
+
+        mllpSocketWriter.writeEnvelopedPayload(TEST_MESSAGE.getBytes(), acknowledgement);
+
+        assertArrayEquals(expected, fakeSocket.payload());
+    }
+
+    @Test
+    public void testWriteEmptyAcknowledgement() throws Exception {
+        byte[] acknowledgement = new byte[0];
+
+        byte[] expected = PayloadBuilder.build(START_OF_BLOCK, END_OF_BLOCK, END_OF_DATA);
+
+        mllpSocketWriter.writeEnvelopedPayload(TEST_MESSAGE.getBytes(), acknowledgement);
+
+        assertArrayEquals(expected, fakeSocket.payload());
+    }
+
+    @Test(expected = MllpAcknowledgementDeliveryException.class)
+    public void testGetOutputStreamFailure() throws Exception {
+        fakeSocket.fakeSocketOutputStream = null;
+
+        try {
+            mllpSocketWriter.writeEnvelopedPayload(TEST_MESSAGE.getBytes(), TEST_ACKNOWLEDGEMENT.getBytes());
+        } catch (MllpAcknowledgementDeliveryException expectedEx) {
+            verifyException(expectedEx);
+            throw expectedEx;
+        }
+    }
+
+    @Test(expected = MllpAcknowledgementDeliveryException.class)
+    public void testWriteToUnconnectedSocket() throws Exception {
+        fakeSocket.connected = false;
+
+        try {
+            mllpSocketWriter.writeEnvelopedPayload(TEST_MESSAGE.getBytes(), TEST_ACKNOWLEDGEMENT.getBytes());
+        } catch (MllpAcknowledgementDeliveryException expectedEx) {
+            verifyException(expectedEx);
+            throw expectedEx;
+        }
+    }
+
+    @Test(expected = MllpAcknowledgementDeliveryException.class)
+    public void testWriteToClosedSocket() throws Exception {
+        fakeSocket.closed = true;
+
+        try {
+            mllpSocketWriter.writeEnvelopedPayload(TEST_MESSAGE.getBytes(), TEST_ACKNOWLEDGEMENT.getBytes());
+        } catch (MllpAcknowledgementDeliveryException expectedEx) {
+            verifyException(expectedEx);
+            throw expectedEx;
+        }
+    }
+
+    @Test(expected = MllpAcknowledgementDeliveryException.class)
+    public void testWriteAcknowledgementFailure() throws Exception {
+        fakeSocket.fakeSocketOutputStream.failOnWriteArray = true;
+
+        try {
+            mllpSocketWriter.writeEnvelopedPayload(TEST_MESSAGE.getBytes(), TEST_ACKNOWLEDGEMENT.getBytes());
+        } catch (MllpAcknowledgementDeliveryException expectedEx) {
+            verifyException(expectedEx);
+            throw expectedEx;
+        }
+    }
+
+    private void verifyException(MllpAcknowledgementDeliveryException expectedEx) throws Exception {
+        assertNotNull(expectedEx.getMessage());
+        assertArrayEquals(TEST_MESSAGE.getBytes(), expectedEx.getHl7Message());
+        assertArrayEquals(TEST_ACKNOWLEDGEMENT.getBytes(), expectedEx.getHl7Acknowledgement());
+        assertArrayEquals(TEST_ACKNOWLEDGEMENT.getBytes(), expectedEx.getMllpPayload());
+    }
+
+
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/a53540da/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/impl/MllpBufferedSocketMessageWriterTest.java
----------------------------------------------------------------------
diff --git a/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/impl/MllpBufferedSocketMessageWriterTest.java b/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/impl/MllpBufferedSocketMessageWriterTest.java
new file mode 100644
index 0000000..1f2c7bf
--- /dev/null
+++ b/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/impl/MllpBufferedSocketMessageWriterTest.java
@@ -0,0 +1,126 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.mllp.impl;
+
+import org.apache.camel.component.mllp.MllpWriteException;
+import org.apache.camel.test.util.PayloadBuilder;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.apache.camel.component.mllp.MllpEndpoint.END_OF_BLOCK;
+import static org.apache.camel.component.mllp.MllpEndpoint.END_OF_DATA;
+import static org.apache.camel.component.mllp.MllpEndpoint.START_OF_BLOCK;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+
+public class MllpBufferedSocketMessageWriterTest extends MllpSocketWriterTestSupport {
+    MllpSocketWriter mllpSocketWriter;
+
+    @Before
+    public void setUp() throws Exception {
+        mllpSocketWriter = new MllpBufferedSocketWriter(fakeSocket, false);
+    }
+
+    @Test
+    public void testWriteMessage() throws Exception {
+        byte[] expected = PayloadBuilder.build(START_OF_BLOCK, TEST_MESSAGE, END_OF_BLOCK, END_OF_DATA);
+
+        mllpSocketWriter.writeEnvelopedPayload(TEST_MESSAGE.getBytes(), null);
+
+        assertArrayEquals(expected, fakeSocket.payload());
+    }
+
+    @Test
+    public void testWriteNullMessage() throws Exception {
+        byte[] message = null;
+
+        byte[] expected = PayloadBuilder.build(START_OF_BLOCK, END_OF_BLOCK, END_OF_DATA);
+
+        mllpSocketWriter.writeEnvelopedPayload(message, null);
+
+        assertArrayEquals(expected, fakeSocket.payload());
+    }
+
+    @Test
+    public void testWriteEmptyMessage() throws Exception {
+        byte[] message = new byte[0];
+
+        byte[] expected = PayloadBuilder.build(START_OF_BLOCK, END_OF_BLOCK, END_OF_DATA);
+
+        mllpSocketWriter.writeEnvelopedPayload(message, null);
+
+        assertArrayEquals(expected, fakeSocket.payload());
+    }
+
+    @Test(expected = MllpWriteException.class)
+    public void testGetOutputStreamFailure() throws Exception {
+        fakeSocket.fakeSocketOutputStream = null;
+
+        try {
+            mllpSocketWriter.writeEnvelopedPayload(TEST_MESSAGE.getBytes(), null);
+        } catch (MllpWriteException expectedEx) {
+            verifyException(expectedEx);
+            throw expectedEx;
+        }
+    }
+
+    @Test(expected = MllpWriteException.class)
+    public void testWriteToUnconnectedSocket() throws Exception {
+        fakeSocket.connected = false;
+
+        try {
+            mllpSocketWriter.writeEnvelopedPayload(TEST_MESSAGE.getBytes(), null);
+        } catch (MllpWriteException expectedEx) {
+            verifyException(expectedEx);
+            throw expectedEx;
+        }
+    }
+
+    @Test(expected = MllpWriteException.class)
+    public void testWriteToClosedSocket() throws Exception {
+        fakeSocket.closed = true;
+
+        try {
+            mllpSocketWriter.writeEnvelopedPayload(TEST_MESSAGE.getBytes(), null);
+        } catch (MllpWriteException expectedEx) {
+            verifyException(expectedEx);
+            throw expectedEx;
+        }
+    }
+
+    @Test(expected = MllpWriteException.class)
+    public void testWriteFailure() throws Exception {
+        fakeSocket.fakeSocketOutputStream.failOnWriteArray = true;
+
+        try {
+            mllpSocketWriter.writeEnvelopedPayload(TEST_MESSAGE.getBytes(), null);
+        } catch (MllpWriteException expectedEx) {
+            verifyException(expectedEx);
+            throw expectedEx;
+        }
+    }
+
+
+    private void verifyException(MllpWriteException expectedEx) throws Exception {
+        assertNotNull(expectedEx.getMessage());
+        assertArrayEquals(TEST_MESSAGE.getBytes(), expectedEx.getHl7Message());
+        assertNull(expectedEx.getHl7Acknowledgement());
+        assertArrayEquals(TEST_MESSAGE.getBytes(), expectedEx.getMllpPayload());
+    }
+
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/a53540da/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/impl/MllpSocketAcknowledgementReaderTest.java
----------------------------------------------------------------------
diff --git a/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/impl/MllpSocketAcknowledgementReaderTest.java b/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/impl/MllpSocketAcknowledgementReaderTest.java
new file mode 100644
index 0000000..bda6d63
--- /dev/null
+++ b/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/impl/MllpSocketAcknowledgementReaderTest.java
@@ -0,0 +1,533 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.mllp.impl;
+
+
+import java.io.IOException;
+import java.net.SocketException;
+import java.net.SocketTimeoutException;
+
+import org.apache.camel.component.mllp.MllpAcknowledgementException;
+import org.apache.camel.component.mllp.MllpAcknowledgementTimeoutException;
+import org.apache.camel.component.mllp.MllpReceiveAcknowledgementException;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.apache.camel.component.mllp.MllpEndpoint.SEGMENT_DELIMITER;
+import static org.apache.camel.component.mllp.MllpEndpoint.START_OF_BLOCK;
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.CoreMatchers.sameInstance;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThat;
+
+public class MllpSocketAcknowledgementReaderTest extends MllpSocketReaderTestSupport {
+    MllpSocketReader mllpSocketReader;
+
+    @Before
+    public void setUp() throws Exception {
+        mllpSocketReader = new MllpSocketReader(fakeSocket, 5000, 1000, true);
+    }
+
+    @Test
+    public void testReadMessage() throws Exception {
+        byte[] expected = TEST_ACKNOWLEDGEMENT.getBytes();
+
+        fakeSocket.fakeSocketInputStream.addPacket(START_PACKET, expected, END_PACKET);
+
+        byte[] actual = mllpSocketReader.readEnvelopedPayload(TEST_MESSAGE.getBytes());
+
+        assertArrayEquals(expected, actual);
+        assertSocketOpen();
+    }
+
+    @Test
+    public void testReadMessageWithSeparateEnvelopingAndMessagePackets() throws Exception {
+        byte[] expected = TEST_MESSAGE.getBytes();
+
+        fakeSocket.fakeSocketInputStream.addPackets(START_PACKET, TEST_MESSAGE.getBytes(), END_PACKET);
+
+        byte[] actual = mllpSocketReader.readEnvelopedPayload();
+
+        assertArrayEquals(expected, actual);
+        assertSocketOpen();
+    }
+
+    @Test
+    public void testReadMessageWithMultipleMessagePackets() throws Exception {
+        byte[] expected = TEST_MESSAGE.getBytes();
+
+        fakeSocket.fakeSocketInputStream
+                .addPacket(START_PACKET)
+                .addPackets(TEST_MESSAGE, SEGMENT_DELIMITER)
+                .addPacket(END_PACKET);
+
+        byte[] actual = mllpSocketReader.readEnvelopedPayload();
+
+        assertArrayEquals(expected, actual);
+        assertSocketOpen();
+    }
+
+    @Test
+    public void testReadEmptyMessage() throws Exception {
+        byte[] expected = new byte[0];
+        fakeSocket.fakeSocketInputStream.addPacket(START_PACKET, END_PACKET);
+
+        byte[] actual = mllpSocketReader.readEnvelopedPayload(TEST_MESSAGE.getBytes());
+
+        assertArrayEquals(expected, actual);
+        assertSocketOpen();
+    }
+
+    @Test
+    public void testReadEmptyMessageWithSeparateEnvelopingPackets() throws Exception {
+        byte[] expected = new byte[0];
+        fakeSocket.fakeSocketInputStream.addPackets(START_PACKET, END_PACKET);
+
+        byte[] actual = mllpSocketReader.readEnvelopedPayload();
+
+        assertArrayEquals(expected, actual);
+        assertSocketOpen();
+    }
+
+    @Test()
+    public void testGetInputStreamFailure() throws Exception {
+        fakeSocket.fakeSocketInputStream = null;
+
+        try {
+            mllpSocketReader.readEnvelopedPayload(TEST_MESSAGE.getBytes());
+
+            expectedExceptionFailure(MllpReceiveAcknowledgementException.class);
+        } catch (MllpReceiveAcknowledgementException expectedEx) {
+            assertEmptyExpectedException(expectedEx);
+            assertNotNull(expectedEx.getCause());
+            assertThat(expectedEx.getCause(), instanceOf(IOException.class));
+            assertSocketReset();
+        }
+    }
+
+    @Test
+    public void testEndOfStreamOnInitialRead() throws Exception {
+        fakeSocket.fakeSocketInputStream.addPackets(EMPTY_PACKET, TEST_MESSAGE);
+
+        try {
+            mllpSocketReader.readEnvelopedPayload(TEST_MESSAGE.getBytes());
+
+            expectedExceptionFailure(MllpReceiveAcknowledgementException.class);
+        } catch (MllpReceiveAcknowledgementException expectedEx) {
+            assertEmptyExpectedException(expectedEx);
+            assertNull(expectedEx.getCause());
+            assertSocketReset();
+        }
+    }
+
+    @Test
+    public void testTimeoutOnInitialRead() throws Exception {
+        try {
+            mllpSocketReader.readEnvelopedPayload(TEST_MESSAGE.getBytes());
+
+            expectedExceptionFailure(MllpAcknowledgementTimeoutException.class);
+        } catch (MllpAcknowledgementTimeoutException expectedEx) {
+            assertEmptyExpectedException(expectedEx);
+            assertNotNull(expectedEx.getCause());
+            assertThat(expectedEx.getCause().getClass(), sameInstance((Class)SocketTimeoutException.class));
+            assertSocketOpen();
+        }
+    }
+
+    @Test
+    public void testTimeoutOnInitialReadWithStartOfBlock() throws Exception {
+        fakeSocket.fakeSocketInputStream.addPacket(START_OF_BLOCK);
+
+        try {
+            mllpSocketReader.readEnvelopedPayload(TEST_MESSAGE.getBytes());
+
+            expectedExceptionFailure(MllpAcknowledgementTimeoutException.class);
+        } catch (MllpAcknowledgementTimeoutException expectedEx) {
+            assertEmptyExpectedException(expectedEx);
+            assertNotNull(expectedEx.getCause());
+            assertThat(expectedEx.getCause().getClass(), sameInstance((Class)SocketTimeoutException.class));
+            assertSocketReset();
+        }
+    }
+
+    @Test
+    public void testSocketExceptionOnInitialRead() throws Exception {
+        fakeSocket.fakeSocketInputStream.addPacket(EXCEPTION_PACKET);
+
+        try {
+            mllpSocketReader.readEnvelopedPayload(TEST_MESSAGE.getBytes());
+
+            expectedExceptionFailure(MllpReceiveAcknowledgementException.class);
+        } catch (MllpReceiveAcknowledgementException expectedEx) {
+            assertEmptyExpectedException(expectedEx);
+            assertNotNull(expectedEx.getCause());
+            assertThat(expectedEx.getCause().getClass(), sameInstance((Class)SocketException.class));
+            assertSocketReset();
+        }
+    }
+
+    @Test
+    public void testIOExceptionOnInitialRead() throws Exception {
+        fakeSocket.fakeSocketInputStream.useSocketExceptionOnNullPacket = false;
+        fakeSocket.fakeSocketInputStream.addPacket(EXCEPTION_PACKET);
+
+        try {
+            mllpSocketReader.readEnvelopedPayload(TEST_MESSAGE.getBytes());
+
+            expectedExceptionFailure(MllpReceiveAcknowledgementException.class);
+        } catch (MllpReceiveAcknowledgementException expectedEx) {
+            assertEmptyExpectedException(expectedEx);
+            assertNotNull(expectedEx.getCause());
+            assertThat(expectedEx.getCause().getClass(), sameInstance((Class)IOException.class));
+            assertSocketReset();
+        }
+    }
+
+    @Test
+    public void testEndOfStreamOnFirstAdditionalRead() throws Exception {
+        fakeSocket.fakeSocketInputStream.addPackets(START_PACKET, EMPTY_PACKET);
+
+        try {
+            mllpSocketReader.readEnvelopedPayload(TEST_MESSAGE.getBytes());
+
+            expectedExceptionFailure(MllpReceiveAcknowledgementException.class);
+        } catch (MllpReceiveAcknowledgementException expectedEx) {
+            assertEmptyExpectedException(expectedEx);
+            assertNull(expectedEx.getCause());
+            assertSocketReset();
+        }
+    }
+
+    @Test
+    public void testEndOfStreamOnFirstAdditionalReadWithPartialPayload() throws Exception {
+        fakeSocket.fakeSocketInputStream.addPacket(START_PACKET, TEST_ACKNOWLEDGEMENT.getBytes()).addPacket(EMPTY_PACKET);
+
+        try {
+            mllpSocketReader.readEnvelopedPayload(TEST_MESSAGE.getBytes());
+
+            expectedExceptionFailure(MllpReceiveAcknowledgementException.class);
+        } catch (MllpReceiveAcknowledgementException expectedEx) {
+            assertExpectedException(expectedEx);
+            assertNull(expectedEx.getCause());
+            assertSocketReset();
+        }
+    }
+
+    @Test
+    public void testTimeoutOnFirstAdditionalRead() throws Exception {
+        fakeSocket.fakeSocketInputStream.addPackets(START_PACKET);
+
+        try {
+            mllpSocketReader.readEnvelopedPayload(TEST_MESSAGE.getBytes());
+
+            expectedExceptionFailure(MllpAcknowledgementTimeoutException.class);
+        } catch (MllpAcknowledgementTimeoutException expectedEx) {
+            assertEmptyExpectedException(expectedEx);
+            assertNotNull(expectedEx.getCause());
+            assertThat(expectedEx.getCause().getClass(), sameInstance((Class)SocketTimeoutException.class));
+            assertSocketReset();
+        }
+    }
+
+    @Test
+    public void testTimeoutOnFirstAdditionalReadWithPartialPayload() throws Exception {
+        fakeSocket.fakeSocketInputStream.addPacket(START_PACKET, TEST_ACKNOWLEDGEMENT.getBytes());
+
+        try {
+            mllpSocketReader.readEnvelopedPayload(TEST_MESSAGE.getBytes());
+
+            expectedExceptionFailure(MllpAcknowledgementTimeoutException.class);
+        } catch (MllpAcknowledgementTimeoutException expectedEx) {
+            assertExpectedException(expectedEx);
+            assertNotNull(expectedEx.getCause());
+            assertThat(expectedEx.getCause().getClass(), sameInstance((Class)SocketTimeoutException.class));
+            assertSocketReset();
+        }
+    }
+
+    @Test
+    public void testSocketExceptionOnFirstAdditionalRead() throws Exception {
+        fakeSocket.fakeSocketInputStream.addPackets(START_PACKET, EXCEPTION_PACKET);
+
+        try {
+            mllpSocketReader.readEnvelopedPayload(TEST_MESSAGE.getBytes());
+
+            expectedExceptionFailure(MllpReceiveAcknowledgementException.class);
+        } catch (MllpReceiveAcknowledgementException expectedEx) {
+            assertEmptyExpectedException(expectedEx);
+            assertNotNull(expectedEx.getCause());
+            assertThat(expectedEx.getCause().getClass(), sameInstance((Class)SocketException.class));
+            assertSocketReset();
+        }
+    }
+
+    @Test
+    public void testSocketExceptionOnFirstAdditionalReadWithPartialPayload() throws Exception {
+        fakeSocket.fakeSocketInputStream.addPacket(START_PACKET, TEST_ACKNOWLEDGEMENT.getBytes()).addPacket(EXCEPTION_PACKET);
+
+        try {
+            mllpSocketReader.readEnvelopedPayload(TEST_MESSAGE.getBytes());
+
+            expectedExceptionFailure(MllpReceiveAcknowledgementException.class);
+        } catch (MllpReceiveAcknowledgementException expectedEx) {
+            assertExpectedException(expectedEx);
+            assertNotNull(expectedEx.getCause());
+            assertThat(expectedEx.getCause().getClass(), sameInstance((Class)SocketException.class));
+            assertSocketReset();
+        }
+    }
+
+    @Test
+    public void testIOExceptionOnFirstAdditionalRead() throws Exception {
+        fakeSocket.fakeSocketInputStream.useSocketExceptionOnNullPacket = false;
+        fakeSocket.fakeSocketInputStream.addPackets(START_PACKET, EXCEPTION_PACKET);
+
+        try {
+            mllpSocketReader.readEnvelopedPayload(TEST_MESSAGE.getBytes());
+
+            expectedExceptionFailure(MllpReceiveAcknowledgementException.class);
+        } catch (MllpReceiveAcknowledgementException expectedEx) {
+            assertEmptyExpectedException(expectedEx);
+            assertNotNull(expectedEx.getCause());
+            assertThat(expectedEx.getCause().getClass(), sameInstance((Class)IOException.class));
+            assertSocketReset();
+        }
+    }
+
+    @Test
+    public void testIOExceptionOnFirstAdditionalReadWithPartialPayload() throws Exception {
+        fakeSocket.fakeSocketInputStream.useSocketExceptionOnNullPacket = false;
+        fakeSocket.fakeSocketInputStream.addPacket(START_PACKET, TEST_ACKNOWLEDGEMENT.getBytes()).addPacket(EXCEPTION_PACKET);
+
+        try {
+            mllpSocketReader.readEnvelopedPayload(TEST_MESSAGE.getBytes());
+
+            expectedExceptionFailure(MllpReceiveAcknowledgementException.class);
+        } catch (MllpReceiveAcknowledgementException expectedEx) {
+            assertExpectedException(expectedEx);
+            assertNotNull(expectedEx.getCause());
+            assertThat(expectedEx.getCause().getClass(), sameInstance((Class)IOException.class));
+            assertSocketReset();
+        }
+    }
+
+    @Test
+    public void testEndOfStreamOnSecondAdditionalReadWithPartialPayload() throws Exception {
+        fakeSocket.fakeSocketInputStream.addPackets(START_PACKET, TEST_ACKNOWLEDGEMENT.getBytes(), EMPTY_PACKET);
+
+        try {
+            mllpSocketReader.readEnvelopedPayload(TEST_MESSAGE.getBytes());
+
+            expectedExceptionFailure(MllpReceiveAcknowledgementException.class);
+        } catch (MllpReceiveAcknowledgementException expectedEx) {
+            assertExpectedException(expectedEx);
+            assertNull(expectedEx.getCause());
+            assertSocketReset();
+        }
+    }
+
+    @Test
+    public void testTimeoutOnSecondAdditionalReadWithPartialPayload() throws Exception {
+        fakeSocket.fakeSocketInputStream.addPackets(START_PACKET, TEST_ACKNOWLEDGEMENT.getBytes());
+
+        try {
+            mllpSocketReader.readEnvelopedPayload(TEST_MESSAGE.getBytes());
+
+            expectedExceptionFailure(MllpAcknowledgementTimeoutException.class);
+        } catch (MllpAcknowledgementTimeoutException expectedEx) {
+            assertExpectedException(expectedEx);
+            assertNotNull(expectedEx.getCause());
+            assertThat(expectedEx.getCause().getClass(), sameInstance((Class)SocketTimeoutException.class));
+            assertSocketReset();
+        }
+    }
+
+    @Test
+    public void testSocketExceptionOnSecondAdditionalReadWithPartialPayload() throws Exception {
+        fakeSocket.fakeSocketInputStream.addPackets(START_PACKET, TEST_ACKNOWLEDGEMENT.getBytes(), EXCEPTION_PACKET);
+
+        try {
+            mllpSocketReader.readEnvelopedPayload(TEST_MESSAGE.getBytes());
+
+            expectedExceptionFailure(MllpReceiveAcknowledgementException.class);
+        } catch (MllpReceiveAcknowledgementException expectedEx) {
+            assertExpectedException(expectedEx);
+            assertNotNull(expectedEx.getCause());
+            assertThat(expectedEx.getCause().getClass(), sameInstance((Class)SocketException.class));
+            assertSocketReset();
+        }
+    }
+
+    @Test
+    public void testIOExceptionOnSecondAdditionalReadWithPartialPayload() throws Exception {
+        fakeSocket.fakeSocketInputStream.useSocketExceptionOnNullPacket = false;
+        fakeSocket.fakeSocketInputStream.addPackets(START_PACKET, TEST_ACKNOWLEDGEMENT.getBytes(), EXCEPTION_PACKET);
+
+        try {
+            mllpSocketReader.readEnvelopedPayload(TEST_MESSAGE.getBytes());
+
+            expectedExceptionFailure(MllpReceiveAcknowledgementException.class);
+        } catch (MllpReceiveAcknowledgementException expectedEx) {
+            assertExpectedException(expectedEx);
+            assertNotNull(expectedEx.getCause());
+            assertThat(expectedEx.getCause().getClass(), sameInstance((Class)IOException.class));
+            assertSocketReset();
+        }
+    }
+
+    @Test
+    public void testLeadingOutOfBandBytes() throws Exception {
+        byte[] expected = TEST_ACKNOWLEDGEMENT.getBytes();
+
+        fakeSocket.fakeSocketInputStream.addPacket("Junk".getBytes(), START_PACKET, expected, END_PACKET);
+
+        byte[] actual = mllpSocketReader.readEnvelopedPayload(TEST_MESSAGE.getBytes());
+
+        assertArrayEquals(expected, actual);
+    }
+
+    @Test
+    public void testLeadingOutOfBandBytesWithEmptyMessage() throws Exception {
+        byte[] expected = new byte[0];
+        fakeSocket.fakeSocketInputStream.addPacket("Junk".getBytes(), START_PACKET, END_PACKET);
+
+        byte[] actual = mllpSocketReader.readEnvelopedPayload(TEST_MESSAGE.getBytes());
+
+        assertArrayEquals(expected, actual);
+        assertSocketOpen();
+    }
+
+    @Test
+    public void testLeadingOutOfBandBytesWithEmptyMessageWithSeparateEnvelopingPackets() throws Exception {
+        byte[] expected = new byte[0];
+        fakeSocket.fakeSocketInputStream.addPackets("Junk".getBytes(), START_PACKET, END_PACKET);
+
+        byte[] actual = mllpSocketReader.readEnvelopedPayload(TEST_MESSAGE.getBytes());
+
+        assertArrayEquals(expected, actual);
+        assertSocketOpen();
+    }
+
+    @Test
+    public void testLeadingOutOfBandBytesSeparateEnvelopingAndMessagePackets() throws Exception {
+        byte[] expected = TEST_ACKNOWLEDGEMENT.getBytes();
+
+        fakeSocket.fakeSocketInputStream.addPackets("Junk".getBytes(), START_PACKET, expected, END_PACKET);
+
+        byte[] actual = mllpSocketReader.readEnvelopedPayload(TEST_MESSAGE.getBytes());
+
+        assertArrayEquals(expected, actual);
+        assertSocketOpen();
+    }
+
+    @Test
+    public void testLeadingOutOfBandBytesWithMultipleMessagePackets() throws Exception {
+        byte[] expected = TEST_ACKNOWLEDGEMENT.getBytes();
+
+        fakeSocket.fakeSocketInputStream
+                .addPacket("Junk")
+                .addPacket(START_PACKET)
+                .addPackets(TEST_ACKNOWLEDGEMENT, SEGMENT_DELIMITER)
+                .addPacket(END_PACKET);
+
+        byte[] actual = mllpSocketReader.readEnvelopedPayload(TEST_MESSAGE.getBytes());
+
+        assertArrayEquals(expected, actual);
+        assertSocketOpen();
+    }
+
+    @Test
+    public void testTrailingOutOfBandBytes() throws Exception {
+        byte[] expected = TEST_ACKNOWLEDGEMENT.getBytes();
+
+        fakeSocket.fakeSocketInputStream.addPacket(START_PACKET, expected, END_PACKET, "Junk".getBytes());
+
+        byte[] actual = mllpSocketReader.readEnvelopedPayload(TEST_MESSAGE.getBytes());
+
+        assertArrayEquals(expected, actual);
+    }
+
+    @Test
+    public void testTrailingOutOfBandBytesWithEmptyMessage() throws Exception {
+        byte[] expected = new byte[0];
+        fakeSocket.fakeSocketInputStream.addPacket(START_PACKET, END_PACKET, "Junk".getBytes());
+
+        byte[] actual = mllpSocketReader.readEnvelopedPayload(TEST_MESSAGE.getBytes());
+
+        assertArrayEquals(expected, actual);
+        assertSocketOpen();
+    }
+
+    @Test
+    public void testTrailingOutOfBandBytesWithEmptyMessageWithSeparateEnvelopingPackets() throws Exception {
+        byte[] expected = new byte[0];
+        fakeSocket.fakeSocketInputStream.addPackets(START_PACKET, END_PACKET, "Junk".getBytes());
+
+        byte[] actual = mllpSocketReader.readEnvelopedPayload(TEST_MESSAGE.getBytes());
+
+        assertArrayEquals(expected, actual);
+        assertSocketOpen();
+    }
+
+    @Test
+    public void testTrailingOutOfBandBytesSeparateEnvelopingAndMessagePackets() throws Exception {
+        byte[] expected = TEST_ACKNOWLEDGEMENT.getBytes();
+
+        fakeSocket.fakeSocketInputStream.addPackets(START_PACKET, expected, END_PACKET, "Junk".getBytes());
+
+        byte[] actual = mllpSocketReader.readEnvelopedPayload(TEST_MESSAGE.getBytes());
+
+        assertArrayEquals(expected, actual);
+        assertSocketOpen();
+    }
+
+    @Test
+    public void testTrailingOutOfBandBytesWithMultipleMessagePackets() throws Exception {
+        byte[] expected = TEST_ACKNOWLEDGEMENT.getBytes();
+
+        fakeSocket.fakeSocketInputStream
+                .addPacket(START_PACKET)
+                .addPackets(TEST_ACKNOWLEDGEMENT, SEGMENT_DELIMITER)
+                .addPacket(END_PACKET)
+                .addPacket("Junk");
+
+        byte[] actual = mllpSocketReader.readEnvelopedPayload(TEST_MESSAGE.getBytes());
+
+        assertArrayEquals(expected, actual);
+        assertSocketOpen();
+    }
+
+    private void assertEmptyExpectedException(MllpAcknowledgementException expectedEx) {
+        assertNotNull(expectedEx);
+        assertNotNull(expectedEx.getMessage());
+        assertArrayEquals(TEST_MESSAGE.getBytes(), expectedEx.getHl7Message());
+        assertNull(expectedEx.getHl7Acknowledgement());
+        assertArrayEquals(TEST_MESSAGE.getBytes(), expectedEx.getMllpPayload());
+    }
+
+    private void assertExpectedException(MllpAcknowledgementException expectedEx) {
+        assertNotNull(expectedEx);
+        assertNotNull(expectedEx.getMessage());
+        assertArrayEquals(TEST_MESSAGE.getBytes(), expectedEx.getHl7Message());
+        assertArrayEquals(TEST_ACKNOWLEDGEMENT.getBytes(), expectedEx.getHl7Acknowledgement());
+        assertArrayEquals(TEST_ACKNOWLEDGEMENT.getBytes(), expectedEx.getMllpPayload());
+    }
+
+}
\ No newline at end of file