View Javadoc
1   /*
2    * Copyright 2014 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.openehealth.ipf.platform.camel.hl7.extend;
17  
18  import java.io.InputStream;
19  import java.util.Scanner;
20  
21  import org.apache.camel.EndpointInject;
22  import org.apache.camel.Message;
23  import org.apache.camel.component.mock.MockEndpoint;
24  import org.junit.After;
25  import org.junit.Test;
26  import org.springframework.test.context.ContextConfiguration;
27  
28  import static org.junit.Assert.assertTrue;
29  
30  /**
31   * @author Christian Ohr
32   */
33  @ContextConfiguration(locations = {"/config/context-ack.xml"})
34  public class AcknowledgementExtensionTest extends AbstractExtensionTest {
35  
36      private static String MSH_EXPECTED = "||ACK^A01";
37      private static String ERR1_EXPECTED = "ERR|^^^203&Unsupported version id&HL70357&&Don't like it";
38      private static String ERR2_EXPECTED = "ERR|^^^207&Application internal error&HL70357&&Exception";
39  
40  
41      @EndpointInject(uri = "mock:output")
42      private MockEndpoint mockOutput;
43  
44      private String resource = "/message/msg-01.hl7";
45  
46      @After
47      public void myTearDown() throws Exception {
48          mockOutput.reset();
49      }
50  
51      @Test
52      public void testRoute2() throws Exception {
53          String message = inputMessage(resource);
54          mockOutput.expectedMessageCount(1);
55          producerTemplate.sendBody("direct:input2", message);
56          mockOutput.assertIsSatisfied();
57          assertTrue(resultString(mockOutput).contains(MSH_EXPECTED));
58      }
59  
60      @Test
61      public void testRoute3() throws Exception {
62          String message = inputMessage(resource);
63          mockOutput.expectedMessageCount(1);
64          producerTemplate.sendBody("direct:input3", message);
65          mockOutput.assertIsSatisfied();
66          assertTrue(resultString(mockOutput).contains(ERR1_EXPECTED));
67      }
68  
69      @Test
70      public void testRoute4() throws Exception {
71          String message = inputMessage(resource);
72          mockOutput.expectedMessageCount(1);
73          producerTemplate.sendBody("direct:input4", message);
74          mockOutput.assertIsSatisfied();
75          String result = resultString(mockOutput);
76          assertTrue(result.contains(ERR2_EXPECTED));
77      }
78  
79      private String resultString(MockEndpoint mock) {
80          return resultMessage(mock).getBody(String.class);
81      }
82  
83      private Message resultMessage(MockEndpoint mock) {
84          return mock.getExchanges().get(0).getIn();
85      }
86  
87      private String inputMessage(String resource) {
88          InputStream is = getClass().getResourceAsStream(resource);
89          return new Scanner(is, "UTF-8").useDelimiter("\\A").next();
90      }
91  
92  }