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.tutorials.hl7.route;
17  
18  import ca.uhn.hl7v2.HL7Exception;
19  import ca.uhn.hl7v2.model.Message;
20  import ca.uhn.hl7v2.parser.PipeParser;
21  import org.apache.camel.ProducerTemplate;
22  import org.junit.Test;
23  import org.junit.runner.RunWith;
24  import org.springframework.beans.factory.annotation.Autowired;
25  import org.springframework.core.io.FileSystemResource;
26  import org.springframework.core.io.Resource;
27  import org.springframework.test.context.ContextConfiguration;
28  import org.springframework.test.context.TestExecutionListeners;
29  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
30  import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
31  
32  import java.io.InputStream;
33  import java.util.Scanner;
34  
35  import static org.junit.Assert.assertEquals;
36  
37  /**
38   * @author Martin Krasser
39   */
40  @RunWith(SpringJUnit4ClassRunner.class)
41  @TestExecutionListeners({DependencyInjectionTestExecutionListener.class})
42  @ContextConfiguration(locations = { "/context.xml" })
43  public class SampleRouteTest {
44  
45      @Autowired
46      private ProducerTemplate producerTemplate;
47  
48      @Test
49      public void testRoute() throws Exception {
50          producerTemplate.sendBody("direct:input", getClass().getResourceAsStream("/msg-01.hl7"));
51          Resource result = new FileSystemResource("target/output/HZL.hl7");
52          assertEquals(
53                  load(getClass().getResourceAsStream("/msg-01.hl7.expected")).toString(),
54                  load(result.getInputStream()).toString());
55      }
56  
57      protected static <T extends Message> T load(InputStream is) throws HL7Exception {
58          return (T)new PipeParser().parse(
59                  new Scanner(is).useDelimiter("\\A").next());
60      }
61  }