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.cda.extend;
17  
18  import java.io.IOException;
19  import java.io.InputStream;
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.Test;
25  import org.openehealth.ipf.modules.cda.CDAR2Parser;
26  import org.openehealth.ipf.modules.cda.CDAR2Renderer;
27  import org.openhealthtools.mdht.uml.cda.ClinicalDocument;
28  import org.springframework.test.context.ContextConfiguration;
29  import static org.junit.Assert.assertTrue;
30  
31  /**
32   * @author Christian Ohr
33   */
34  @ContextConfiguration(locations = { "/config/context-extend.xml" })
35  public class MdhtModelExtensionTest extends AbstractExtensionTest {
36  
37      private String cdaExample = "/message/SampleCDADocument.xml";
38      private String ccdExample = "/message/SampleCCDDocument.xml";
39  
40      @EndpointInject(uri="mock:error")
41      protected MockEndpoint mockError;
42      
43      
44      @Test
45      public void testMarshalDefault() throws Exception {
46          testMarshalCDA("direct:input1", cdaExample);
47          testMarshalCDA("direct:input1", ccdExample);
48      }
49      
50      @Test
51      public void testUnmarshalDefault() throws Exception {
52          testUnmarshalCDA("direct:input2", cdaExample);
53          testUnmarshalCDA("direct:input2", ccdExample);
54      }
55  
56      @Test
57      public void testValidateDefault() throws Exception {
58          testValidateCDA("direct:input3", cdaExample);
59          testValidateCDA("direct:input3", ccdExample);
60      }
61  
62      
63  
64      private void testMarshalCDA(String endpoint, String file) throws Exception {
65          mockOutput.reset();
66          mockError.reset();
67          ClinicalDocument message = inputMessage(file);
68          mockOutput.expectedMessageCount(1);
69          producerTemplate.sendBody(endpoint, message);
70          mockOutput.assertIsSatisfied();
71          assertTrue(resultString().contains("<ClinicalDocument"));
72      }
73      
74      private void testUnmarshalCDA(String endpoint, String file) throws Exception {
75          mockOutput.reset();
76          mockError.reset();
77          InputStream stream = inputStream(file);
78          mockOutput.expectedMessageCount(1);
79          producerTemplate.sendBody(endpoint, stream);
80          mockOutput.assertIsSatisfied();
81          assertTrue(messageAsString(resultAdapter()).contains("<ClinicalDocument"));
82      }
83      
84      private void testValidateCDA(String endpoint, String file) throws Exception {
85          mockOutput.reset();
86          mockError.reset();
87          InputStream stream = inputStream(file);
88          mockOutput.expectedMessageCount(1);
89          producerTemplate.sendBody(endpoint, stream);
90          mockOutput.assertIsSatisfied();
91      }
92  
93      private String resultString() {
94          return resultMessage().getBody(String.class);
95      }
96  
97      private ClinicalDocument resultAdapter() {
98          return resultMessage().getBody(ClinicalDocument.class);
99      }
100 
101     private Message resultMessage() {
102         return mockOutput.getExchanges().get(0).getIn();
103     }   
104     
105     private static InputStream inputStream(String resource) {
106         return MdhtModelExtensionTest.class.getResourceAsStream(resource);
107     }
108 
109     private ClinicalDocument inputMessage(String resource) throws IOException {
110         return new CDAR2Parser().parse(inputStream(resource), (Object[])null);
111     }
112     
113     private String messageAsString(ClinicalDocument message) {
114         return new CDAR2Renderer().render(message, (Object[])null);
115     }
116     
117 }