View Javadoc
1   /*
2    * Copyright 2013 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.validation;
17  
18  
19  import ca.uhn.hl7v2.DefaultHapiContext;
20  import ca.uhn.hl7v2.HL7Exception;
21  import ca.uhn.hl7v2.HapiContext;
22  import ca.uhn.hl7v2.model.Message;
23  import org.apache.camel.CamelContext;
24  import org.apache.camel.ProducerTemplate;
25  import org.apache.camel.impl.DefaultCamelContext;
26  import org.apache.camel.impl.DefaultProducerTemplate;
27  import org.apache.commons.io.IOUtils;
28  import org.junit.After;
29  import org.junit.Before;
30  import org.junit.Test;
31  import org.openehealth.ipf.gazelle.validation.profile.store.GazelleProfileStore;
32  
33  import java.io.IOException;
34  import java.nio.charset.Charset;
35  
36  import static org.junit.Assert.fail;
37  
38  /**
39   * @author Boris Stanojevic
40   */
41  public class ConformanceProfileValidatorsTest {
42  
43      CamelContext camelContext = new DefaultCamelContext();
44  
45      ProducerTemplate template;
46  
47      @Before
48      public void onBeforeClass() throws Exception {
49          camelContext.addRoutes(new TestRouteBuilder());
50          camelContext.start();
51          template = new DefaultProducerTemplate(camelContext);
52          template.start();
53      }
54  
55      @After
56      public void onAfterClass() throws Exception {
57          template.stop();
58          camelContext.stop();
59      }
60  
61      @Test
62      public void testIti8() {
63          try {
64              template.sendBody("direct:iti8", getParsedMessage("hl7/iti-8.hl7"));
65              fail();
66          } catch (Exception e){
67              assert e.getCause().getMessage().contains("Message validation failed");
68          }
69      }
70  
71      @Test
72      public void testIti10() throws Exception {
73          template.sendBody("direct:iti10", getParsedMessage("hl7/iti-10.hl7"));
74      }
75  
76      @Test
77      public void testIti10Adapter() throws Exception {
78          template.sendBody("direct:iti10", getParsedMessage("hl7/iti-10.hl7"));
79      }
80  
81      @Test
82      public void testIti21() throws Exception {
83          template.sendBody("direct:iti21", getParsedMessage("hl7/iti-21.hl7"));
84      }
85  
86      @Test
87      public void testIti21WrongIHETransaction() {
88          try {
89              template.sendBody("direct:iti8", getParsedMessage("hl7/iti-21.hl7"));
90              fail();
91          } catch (Exception e){
92              assert e.getCause().getMessage().contains("No matching profile");
93          }
94      }
95  
96      protected String getMessageAsString(String resourcePath){
97          String message = null;
98          try {
99              message = IOUtils.toString(this.getClass().getClassLoader().getResource(resourcePath), Charset.defaultCharset());
100         } catch (IOException ioe){
101             ioe.printStackTrace();
102         }
103         assert message != null;
104         return message.replaceAll("\n", "\r");
105     }
106 
107     protected Message getParsedMessage(String path) throws HL7Exception {
108         HapiContext context = new DefaultHapiContext();
109         context.setProfileStore(new GazelleProfileStore());
110         context.getParserConfiguration().setValidating(false);
111         return context.getPipeParser().parse(getMessageAsString(path));
112     }
113 }