Repository: camel Updated Branches: refs/heads/master 8a11d4714 -> 404fae80b
http://git-wip-us.apache.org/repos/asf/camel/blob/62715117/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpTcpServerConsumerAcknowledgementTest.java ---------------------------------------------------------------------- diff --git a/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpTcpServerConsumerAcknowledgementTest.java b/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpTcpServerConsumerAcknowledgementTest.java index 33ce3c8..e2971dd 100644 --- a/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpTcpServerConsumerAcknowledgementTest.java +++ b/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpTcpServerConsumerAcknowledgementTest.java @@ -20,26 +20,25 @@ import java.util.concurrent.TimeUnit; import org.apache.camel.CamelContext; import org.apache.camel.EndpointInject; +import org.apache.camel.Exchange; 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.junit4.CamelTestSupport; import org.junit.Rule; import org.junit.Test; +import static org.apache.camel.component.mllp.MllpConstants.MLLP_ACKNOWLEDGEMENT; +import static org.apache.camel.component.mllp.MllpConstants.MLLP_ACKNOWLEDGEMENT_EXCEPTION; import static org.apache.camel.component.mllp.MllpConstants.MLLP_EVENT_TYPE; import static org.apache.camel.component.mllp.MllpConstants.MLLP_RECEIVING_APPLICATION; -import static org.apache.camel.component.mllp.MllpConstants.MLLP_RECEIVING_FACILITY; import static org.apache.camel.component.mllp.MllpConstants.MLLP_SENDING_APPLICATION; import static org.apache.camel.component.mllp.MllpConstants.MLLP_SENDING_FACILITY; import static org.apache.camel.component.mllp.MllpConstants.MLLP_TRIGGER_EVENT; import static org.apache.camel.component.mllp.MllpConstants.MLLP_VERSION_ID; -import static org.apache.camel.test.mllp.Hl7MessageGenerator.generateMessage; public class MllpTcpServerConsumerAcknowledgementTest extends CamelTestSupport { @Rule @@ -48,6 +47,12 @@ public class MllpTcpServerConsumerAcknowledgementTest extends CamelTestSupport { @EndpointInject(uri = "mock://result") MockEndpoint result; + @EndpointInject(uri = "mock://on-complete-only") + MockEndpoint complete; + + @EndpointInject(uri = "mock://on-failure-only") + MockEndpoint failure; + @Override protected CamelContext createCamelContext() throws Exception { DefaultCamelContext context = (DefaultCamelContext) super.createCamelContext(); @@ -60,7 +65,6 @@ public class MllpTcpServerConsumerAcknowledgementTest extends CamelTestSupport { @Override protected RouteBuilder createRouteBuilder() { - mllpClient.setMllpHost("localhost"); mllpClient.setMllpPort(AvailablePortFinder.getNextAvailable()); @@ -73,27 +77,33 @@ public class MllpTcpServerConsumerAcknowledgementTest extends CamelTestSupport { String routeId = "mllp-test-receiver-route"; onCompletion() - .toF("log:%s?level=INFO&showAll=true", routeId) - .log(LoggingLevel.INFO, routeId, "Test route complete"); - + .onCompleteOnly() + .log(LoggingLevel.INFO, routeId, "Test route complete") + .to("mock://on-complete-only"); + onCompletion() + .onFailureOnly() + .log(LoggingLevel.INFO, routeId, "Test route complete") + .to("mock://on-failure-only"); fromF("mllp://%s:%d?autoAck=true&connectTimeout=%d&receiveTimeout=%d", mllpClient.getMllpHost(), mllpClient.getMllpPort(), connectTimeout, responseTimeout) .routeId(routeId) .to(result); - } }; } @Test public void testReceiveSingleMessage() throws Exception { - final String testMessage = "MSH|^~\\&|APP_A|FAC_A|^org^sys||||ADT^A04^ADT_A04|||2.6" + '\r' - + "PID|1||1100832^^^^PI||TEST^FIG||98765432|U||R|435 MAIN STREET^^LONGMONT^CO^80503||123-456-7890|||S" + '\r' - + '\r' + '\n'; + final String testMessage = + "MSH|^~\\&|APP_A|FAC_A|^org^sys||||ADT^A04^ADT_A04|||2.6" + '\r' + + "PID|1||1100832^^^^PI||TEST^FIG||98765432|U||R|435 MAIN STREET^^LONGMONT^CO^80503||123-456-7890|||S" + '\r' + + '\r' + '\n'; - final String expectedAcknowledgement = "MSH|^~\\&|^org^sys||APP_A|FAC_A|||ACK^A04^ADT_A04|||2.6" + '\r' + "MSA|AA|" + '\r' + '\n'; + final String expectedAcknowledgement = + "MSH|^~\\&|^org^sys||APP_A|FAC_A|||ACK^A04^ADT_A04|||2.6" + '\r' + + "MSA|AA|" + '\r' + '\n'; - result.expectedMessageCount(1); + result.expectedBodiesReceived(testMessage); result.expectedHeaderReceived(MLLP_SENDING_APPLICATION, "APP_A"); result.expectedHeaderReceived(MLLP_SENDING_FACILITY, "FAC_A"); result.expectedHeaderReceived(MLLP_RECEIVING_APPLICATION, "^org^sys"); @@ -101,14 +111,71 @@ public class MllpTcpServerConsumerAcknowledgementTest extends CamelTestSupport { result.expectedHeaderReceived(MLLP_TRIGGER_EVENT, "A04"); result.expectedHeaderReceived(MLLP_VERSION_ID, "2.6"); + complete.expectedBodiesReceived(testMessage); + complete.expectedHeaderReceived(MLLP_SENDING_APPLICATION, "APP_A"); + complete.expectedHeaderReceived(MLLP_SENDING_FACILITY, "FAC_A"); + complete.expectedHeaderReceived(MLLP_RECEIVING_APPLICATION, "^org^sys"); + complete.expectedHeaderReceived(MLLP_EVENT_TYPE, "ADT"); + complete.expectedHeaderReceived(MLLP_TRIGGER_EVENT, "A04"); + complete.expectedHeaderReceived(MLLP_VERSION_ID, "2.6"); + complete.expectedHeaderReceived(MllpConstants.MLLP_ACKNOWLEDGEMENT_TYPE, "AA"); + complete.expectedHeaderReceived(MLLP_ACKNOWLEDGEMENT, expectedAcknowledgement); + + failure.expectedMessageCount(0); + mllpClient.connect(); String acknowledgement = mllpClient.sendMessageAndWaitForAcknowledgement(testMessage, 10000); + assertMockEndpointsSatisfied(10, TimeUnit.SECONDS); + assertEquals("Unexpected Acknowledgement", expectedAcknowledgement, acknowledgement); + } + + @Test + public void testAcknowledgementDeliveryFailure() throws Exception { + final String testMessage = + "MSH|^~\\&|APP_A|FAC_A|^org^sys||||ADT^A04^ADT_A04|||2.6" + '\r' + + "PID|1||1100832^^^^PI||TEST^FIG||98765432|U||R|435 MAIN STREET^^LONGMONT^CO^80503||123-456-7890|||S" + '\r' + + '\r' + '\n'; + + final String expectedAcknowledgement = + "MSH|^~\\&|^org^sys||APP_A|FAC_A|||ACK^A04^ADT_A04|||2.6" + '\r' + + "MSA|AA|" + + '\r' + '\n'; + + result.expectedBodiesReceived(testMessage); + result.expectedHeaderReceived(MLLP_SENDING_APPLICATION, "APP_A"); + result.expectedHeaderReceived(MLLP_SENDING_FACILITY, "FAC_A"); + result.expectedHeaderReceived(MLLP_RECEIVING_APPLICATION, "^org^sys"); + result.expectedHeaderReceived(MLLP_EVENT_TYPE, "ADT"); + result.expectedHeaderReceived(MLLP_TRIGGER_EVENT, "A04"); + result.expectedHeaderReceived(MLLP_VERSION_ID, "2.6"); + + complete.expectedMessageCount(0); + + failure.expectedBodiesReceived(testMessage); + failure.expectedHeaderReceived(MLLP_SENDING_APPLICATION, "APP_A"); + failure.expectedHeaderReceived(MLLP_SENDING_FACILITY, "FAC_A"); + failure.expectedHeaderReceived(MLLP_RECEIVING_APPLICATION, "^org^sys"); + failure.expectedHeaderReceived(MLLP_EVENT_TYPE, "ADT"); + failure.expectedHeaderReceived(MLLP_TRIGGER_EVENT, "A04"); + failure.expectedHeaderReceived(MLLP_VERSION_ID, "2.6"); + failure.expectedHeaderReceived(MllpConstants.MLLP_ACKNOWLEDGEMENT_TYPE, "AA"); + failure.expectedHeaderReceived(MLLP_ACKNOWLEDGEMENT, expectedAcknowledgement); + + boolean disconnectAfterSend = true; + mllpClient.setDisconnectMethod(MllpClientResource.DisconnectMethod.RESET); + mllpClient.connect(); + + mllpClient.sendFramedData(testMessage, disconnectAfterSend); assertMockEndpointsSatisfied(10, TimeUnit.SECONDS); - } + Exchange failureExchange = failure.getExchanges().get(0); + Object failureException = failureExchange.getProperty(MLLP_ACKNOWLEDGEMENT_EXCEPTION); + assertNotNull("OnFailureOnly exchange should have a " + MLLP_ACKNOWLEDGEMENT_EXCEPTION + " property", failureException); + assertIsInstanceOf(Exception.class, failureException); + } } http://git-wip-us.apache.org/repos/asf/camel/blob/62715117/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpTimeoutExceptionTest.java ---------------------------------------------------------------------- diff --git a/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpTimeoutExceptionTest.java b/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpTimeoutExceptionTest.java new file mode 100644 index 0000000..da4ceb7 --- /dev/null +++ b/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpTimeoutExceptionTest.java @@ -0,0 +1,101 @@ +/** + * 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.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class MllpTimeoutExceptionTest { + static final String EXCEPTION_MESSAGE = "Test Timeoute Exception"; + + static final String HL7_MESSAGE = + "MSH|^~\\&|APP_A|FAC_A|^org^sys||||ADT^A04^ADT_A04|||2.6" + '\r' + + "PID|1||1100832^^^^PI||TEST^FIG||98765432|U||R|435 MAIN STREET^^LONGMONT^CO^80503||123-456-7890|||S" + '\r' + + '\r' + '\n'; + + static final String EXCEPTION_MESSAGE_WITH_LOG_PHI_DISABLED = EXCEPTION_MESSAGE; + static final String EXCEPTION_MESSAGE_WITH_LOG_PHI_ENABLED = + String.format(String.format("%s:\n\tHL7 Message: %s", + EXCEPTION_MESSAGE, + new String(HL7_MESSAGE).replaceAll("\r", "<CR>").replaceAll("\n", "<LF>")) + ); + + Exception exception; + + @Before + public void setUp() throws Exception { + exception = new MllpTimeoutException(EXCEPTION_MESSAGE, HL7_MESSAGE.getBytes()); + } + + @After + public void tearDown() throws Exception { + System.clearProperty(MllpComponent.MLLP_LOG_PHI_PROPERTY); + } + + @Test + public void testLogPhiDefault() throws Exception { + String exceptionMessage = exception.getMessage(); + + assertEquals(EXCEPTION_MESSAGE_WITH_LOG_PHI_ENABLED, exceptionMessage); + } + + @Test + public void testLogPhiDisabled() throws Exception { + System.setProperty(MllpComponent.MLLP_LOG_PHI_PROPERTY, "false"); + + String exceptionMessage = exception.getMessage(); + + assertEquals(EXCEPTION_MESSAGE_WITH_LOG_PHI_DISABLED, exceptionMessage); + } + + @Test + public void testLogPhiEnabled() throws Exception { + System.setProperty(MllpComponent.MLLP_LOG_PHI_PROPERTY, "true"); + + String exceptionMessage = exception.getMessage(); + + assertEquals(EXCEPTION_MESSAGE_WITH_LOG_PHI_ENABLED, exceptionMessage); + } + + @Test + public void testNullPayload() throws Exception { + final String expectedMessage = String.format("%s:\n\tHL7 Message: null", EXCEPTION_MESSAGE); + + exception = new MllpTimeoutException(EXCEPTION_MESSAGE, null); + + System.setProperty(MllpComponent.MLLP_LOG_PHI_PROPERTY, "true"); + String exceptionMessage = exception.getMessage(); + + assertEquals(expectedMessage, exceptionMessage); + } + + @Test + public void testToString() throws Exception { + final String expectedString = + "org.apache.camel.component.mllp.MllpTimeoutException: " + + "{hl7Message=" + + "MSH|^~\\&|APP_A|FAC_A|^org^sys||||ADT^A04^ADT_A04|||2.6<CR>" + + "PID|1||1100832^^^^PI||TEST^FIG||98765432|U||R|435 MAIN STREET^^LONGMONT^CO^80503||123-456-7890|||S<CR><CR><LF>" + + "}"; + + assertEquals(expectedString, exception.toString()); + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/62715117/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpWriteExceptionTest.java ---------------------------------------------------------------------- diff --git a/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpWriteExceptionTest.java b/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpWriteExceptionTest.java new file mode 100644 index 0000000..daf7fe9 --- /dev/null +++ b/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpWriteExceptionTest.java @@ -0,0 +1,101 @@ +/** + * 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.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class MllpWriteExceptionTest { + static final String EXCEPTION_MESSAGE = "Test Write Exception"; + + static final String HL7_MESSAGE = + "MSH|^~\\&|APP_A|FAC_A|^org^sys||||ADT^A04^ADT_A04|||2.6" + '\r' + + "PID|1||1100832^^^^PI||TEST^FIG||98765432|U||R|435 MAIN STREET^^LONGMONT^CO^80503||123-456-7890|||S" + '\r' + + '\r' + '\n'; + + static final String EXCEPTION_MESSAGE_WITH_LOG_PHI_DISABLED = EXCEPTION_MESSAGE; + static final String EXCEPTION_MESSAGE_WITH_LOG_PHI_ENABLED = + String.format(String.format("%s:\n\tMLLP Payload: %s", + EXCEPTION_MESSAGE, + new String(HL7_MESSAGE).replaceAll("\r", "<CR>").replaceAll("\n", "<LF>")) + ); + + Exception exception; + + @Before + public void setUp() throws Exception { + exception = new MllpWriteException(EXCEPTION_MESSAGE, HL7_MESSAGE.getBytes()); + } + + @After + public void tearDown() throws Exception { + System.clearProperty(MllpComponent.MLLP_LOG_PHI_PROPERTY); + } + + @Test + public void testLogPhiDefault() throws Exception { + String exceptionMessage = exception.getMessage(); + + assertEquals(EXCEPTION_MESSAGE_WITH_LOG_PHI_ENABLED, exceptionMessage); + } + + @Test + public void testLogPhiDisabled() throws Exception { + System.setProperty(MllpComponent.MLLP_LOG_PHI_PROPERTY, "false"); + + String exceptionMessage = exception.getMessage(); + + assertEquals(EXCEPTION_MESSAGE_WITH_LOG_PHI_DISABLED, exceptionMessage); + } + + @Test + public void testLogPhiEnabled() throws Exception { + System.setProperty(MllpComponent.MLLP_LOG_PHI_PROPERTY, "true"); + + String exceptionMessage = exception.getMessage(); + + assertEquals(EXCEPTION_MESSAGE_WITH_LOG_PHI_ENABLED, exceptionMessage); + } + + @Test + public void testNullPayload() throws Exception { + final String expectedMessage = String.format("%s:\n\tMLLP Payload: null", EXCEPTION_MESSAGE); + + exception = new MllpWriteException(EXCEPTION_MESSAGE, null); + + System.setProperty(MllpComponent.MLLP_LOG_PHI_PROPERTY, "true"); + String exceptionMessage = exception.getMessage(); + + assertEquals(expectedMessage, exceptionMessage); + } + + @Test + public void testToString() throws Exception { + final String expectedString = + "org.apache.camel.component.mllp.MllpWriteException: " + + "{mllpPayload=" + + "MSH|^~\\&|APP_A|FAC_A|^org^sys||||ADT^A04^ADT_A04|||2.6<CR>" + + "PID|1||1100832^^^^PI||TEST^FIG||98765432|U||R|435 MAIN STREET^^LONGMONT^CO^80503||123-456-7890|||S<CR><CR><LF>" + + "}"; + + assertEquals(expectedString, exception.toString()); + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/62715117/components/camel-mllp/src/test/java/org/apache/camel/test/junit/rule/mllp/MllpClientResource.java ---------------------------------------------------------------------- diff --git a/components/camel-mllp/src/test/java/org/apache/camel/test/junit/rule/mllp/MllpClientResource.java b/components/camel-mllp/src/test/java/org/apache/camel/test/junit/rule/mllp/MllpClientResource.java index 8157e69..56634eb 100644 --- a/components/camel-mllp/src/test/java/org/apache/camel/test/junit/rule/mllp/MllpClientResource.java +++ b/components/camel-mllp/src/test/java/org/apache/camel/test/junit/rule/mllp/MllpClientResource.java @@ -60,6 +60,7 @@ public class MllpClientResource extends ExternalResource { boolean reuseAddress; boolean tcpNoDelay = true; + DisconnectMethod disconnectMethod = DisconnectMethod.CLOSE; /** * Use this constructor to avoid having the connection started by JUnit (since the port is still -1) @@ -89,7 +90,22 @@ public class MllpClientResource extends ExternalResource { @Override protected void after() { super.after(); - this.disconnect(); + this.close(); + } + + public void close() { + try { + if (null != inputStream) { + clientSocket.close(); + } + } catch (IOException e) { + log.warn(String.format("Exception encountered closing connection to {}:{}", mllpHost, mllpPort), e); + } finally { + inputStream = null; + outputStream = null; + clientSocket = null; + } + return; } public void connect() { @@ -116,33 +132,36 @@ public class MllpClientResource extends ExternalResource { } } - public void close() { - this.disconnect(); - return; - } - public void reset() { try { clientSocket.setSoLinger(true, 0); } catch (SocketException socketEx) { log.warn("Exception encountered setting set SO_LINGER to force a TCP reset", socketEx); } - this.disconnect(); - return; - } - - public void disconnect() { try { if (null != inputStream) { clientSocket.close(); } } catch (IOException e) { - log.warn(String.format("Exception encountered closing connection to {}:{}", mllpHost, mllpPort), e); + log.warn(String.format("Exception encountered resetting connection to {}:{}", mllpHost, mllpPort), e); } finally { inputStream = null; outputStream = null; clientSocket = null; } + return; + } + + public void disconnect() { + if (DisconnectMethod.RESET == disconnectMethod) { + reset(); + } else { + close(); + } + } + + public DisconnectMethod getDisconnectMethod() { + return disconnectMethod; } public boolean isConnected() { @@ -458,4 +477,12 @@ public class MllpClientResource extends ExternalResource { this.tcpNoDelay = tcpNoDelay; } + public void setDisconnectMethod(DisconnectMethod disconnectMethod) { + this.disconnectMethod = disconnectMethod; + } + + public enum DisconnectMethod { + CLOSE, + RESET + } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/62715117/components/camel-mllp/src/test/resources/OSGI-INF/blueprint/mllp-tcp-client-producer-test.xml ---------------------------------------------------------------------- diff --git a/components/camel-mllp/src/test/resources/OSGI-INF/blueprint/mllp-tcp-client-producer-test.xml b/components/camel-mllp/src/test/resources/OSGI-INF/blueprint/mllp-tcp-client-producer-test.xml index 03a07a4..e3c10ae 100644 --- a/components/camel-mllp/src/test/resources/OSGI-INF/blueprint/mllp-tcp-client-producer-test.xml +++ b/components/camel-mllp/src/test/resources/OSGI-INF/blueprint/mllp-tcp-client-producer-test.xml @@ -61,7 +61,7 @@ </onException> <onException> - <exception>org.apache.camel.component.mllp.MllpCorruptFrameException</exception> + <exception>org.apache.camel.component.mllp.MllpFrameException</exception> <redeliveryPolicy logHandled="true"/> <handled> <constant>true</constant>