View Javadoc
1   /*
2    * Copyright 2008 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.util.Scanner;
19  
20  import static org.junit.Assert.assertTrue;
21  
22  import org.apache.camel.EndpointInject;
23  import org.apache.camel.Message;
24  import org.apache.camel.component.mock.MockEndpoint;
25  import org.junit.After;
26  import org.junit.Test;
27  import org.springframework.test.context.ContextConfiguration;
28  
29  /**
30   * @author Martin Krasser
31   */
32  @ContextConfiguration(locations = { "/config/context-sample.xml" })
33  public class SampleExtensionTest extends AbstractExtensionTest {
34  
35      private static String MSH_EXPECTED_1 = "MSH|^~\\&|SAP-ISH|HZL|blah||20040805152637||ADT^A01|123456|T|2.2|||ER";
36      private static String MSH_EXPECTED_2 = "MSH|^~\\&|SAP-ISH|HZL|blub||20040805152637||ADT^A01|123456|T|2.2|||ER";
37      
38      @EndpointInject(uri="mock:output1")
39      private MockEndpoint mockOutput1;
40      
41      @EndpointInject(uri="mock:output2")
42      private MockEndpoint mockOutput2;
43      
44      private String resource = "message/msg-01.hl7";
45      
46      @After
47      public void myTearDown() throws Exception {
48          mockOutput1.reset();
49          mockOutput2.reset();
50      }
51  
52      @Test
53      public void testRoute1() throws Exception {
54          String message = inputMessage(resource);
55          mockOutput1.expectedMessageCount(1);
56          producerTemplate.sendBodyAndHeader("direct:input1", message, "foo", "blah");
57          mockOutput1.assertIsSatisfied();
58          assertTrue(resultString(mockOutput1).contains(MSH_EXPECTED_1));
59      }
60  
61      @Test
62      public void testRoute2() throws Exception {
63          String message = inputMessage(resource);
64          mockOutput2.expectedMessageCount(1);
65          producerTemplate.sendBodyAndHeader("direct:input1", message, "foo", "blub");
66          mockOutput2.assertIsSatisfied();
67          assertTrue(resultString(mockOutput2).contains(MSH_EXPECTED_2));
68      }
69  
70      private static String resultString(MockEndpoint mock) {
71          return resultMessage(mock).getBody(String.class);
72      }
73  
74      private static Message resultMessage(MockEndpoint mock) {
75          return mock.getExchanges().get(0).getIn();
76      }
77  
78      private static String inputMessage(String resource) {
79          return new Scanner(SampleExtensionTest.class.getResourceAsStream("/" + resource)).useDelimiter("\\A").next();
80      }
81      
82  }