View Javadoc
1   /*
2    * Copyright 2011 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.ihe.continua.hrn;
17  
18  import java.io.InputStream;
19  import javax.activation.DataHandler;
20  import javax.xml.bind.JAXBContext;
21  import javax.xml.bind.JAXBElement;
22  import javax.xml.bind.JAXBException;
23  import javax.xml.bind.Unmarshaller;
24  import javax.xml.parsers.DocumentBuilder;
25  import javax.xml.parsers.DocumentBuilderFactory;
26  
27  import org.apache.cxf.transport.servlet.CXFServlet;
28  import org.junit.Before;
29  import org.junit.BeforeClass;
30  import org.junit.Test;
31  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.ProvideAndRegisterDocumentSetRequestType;
32  import org.openehealth.ipf.commons.ihe.xds.core.metadata.Document;
33  import org.openehealth.ipf.commons.ihe.xds.core.requests.ProvideAndRegisterDocumentSet;
34  import org.openehealth.ipf.commons.ihe.xds.core.responses.Response;
35  import org.openehealth.ipf.commons.ihe.xds.core.responses.Status;
36  import org.openehealth.ipf.modules.cda.CDAR2Parser;
37  import org.openehealth.ipf.platform.camel.ihe.ws.StandardTestContainer;
38  import org.openehealth.ipf.platform.camel.ihe.xds.core.converters.EbXML30Converters;
39  import org.openhealthtools.mdht.uml.cda.ClinicalDocument;
40  
41  import static org.junit.Assert.assertEquals;
42  
43  /**
44   * Tests the Continua HRN transaction.
45   * @author Stefan Ivanov
46   */
47  public class ContinuaHrnTransactionTest extends StandardTestContainer {
48      
49      public static final String CONTEXT_DESCRIPTOR = "continua-hrn-context.xml";
50  
51      private static JAXBContext jaxbContext;
52      private ProvideAndRegisterDocumentSetRequestType hrnRequest;
53      
54      public static void main(String... args) {
55          startServer(new CXFServlet(), CONTEXT_DESCRIPTOR, false, DEMO_APP_PORT);
56      }
57  
58      @BeforeClass
59      public static void setUpClass() throws JAXBException {
60          startServer(new CXFServlet(), CONTEXT_DESCRIPTOR);
61          jaxbContext = JAXBContext.newInstance("org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30");
62      }
63      
64      @Before
65      public void setUp() throws Exception {
66          initRequest();
67      }
68  
69      @Test
70      public void happyCase() throws Exception {
71          String uri = "xds-iti41://localhost:" + getPort() + "/continuaHRNService";
72          Response response = (Response) send(uri, hrnRequest, Response.class);
73          assertEquals(Status.SUCCESS, response.getStatus());
74      }
75  
76  
77      /**
78       * Test whether DOM tree can be used as input for Continua HRN.
79       */
80      @Test
81      public void testDomInputDatatype() throws Exception {
82          String uri = "xds-iti41://localhost:" + getPort() + "/continuaHRNService";
83  
84          // prepare request, delete original document contents
85          ProvideAndRegisterDocumentSet request = EbXML30Converters.convert(hrnRequest);
86          assertEquals(1, request.getDocuments().size());
87          Document xdsDocument = request.getDocuments().get(0);
88          xdsDocument.removeContent(DataHandler.class);
89          assertEquals(0, xdsDocument.getContentsCount());
90  
91          // read in CCD file as DOM tree and make it the new document contents
92          InputStream stream = getClass().getClassLoader().getResourceAsStream("continua-hrn/SampleCCDDocument.xml");
93          DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
94          factory.setNamespaceAware(true);
95          DocumentBuilder builder = factory.newDocumentBuilder();
96          org.w3c.dom.Document domDocument = builder.parse(stream);
97          xdsDocument.setContent(org.w3c.dom.Document.class, domDocument);
98  
99          // create data handler from DOM tree
100         xdsDocument.getContent(byte[].class);
101         xdsDocument.getContent(DataHandler.class);
102         assertEquals(3, xdsDocument.getContentsCount());
103 
104         // send the resulting request
105         Response response = (Response) send(uri, hrnRequest, Response.class);
106         assertEquals(Status.SUCCESS, response.getStatus());
107     }
108 
109 
110     /**
111      * Test whether MDHT POJO can be used as input for Continua HRN.
112      */
113     @Test
114     public void testMdhtInputDatatype() throws Exception {
115         String uri = "xds-iti41://localhost:" + getPort() + "/continuaHRNService";
116 
117         // prepare request, delete original document contents
118         ProvideAndRegisterDocumentSet request = EbXML30Converters.convert(hrnRequest);
119         assertEquals(1, request.getDocuments().size());
120         Document xdsDocument = request.getDocuments().get(0);
121         xdsDocument.removeContent(DataHandler.class);
122         assertEquals(0, xdsDocument.getContentsCount());
123 
124         // read in CCD file as MDHT and make it the new document contents
125         InputStream stream = getClass().getClassLoader().getResourceAsStream("continua-hrn/SampleCCDDocument.xml");
126         ClinicalDocument mdhtDocument = new CDAR2Parser().parse(stream);
127         xdsDocument.setContent(ClinicalDocument.class, mdhtDocument);
128 
129         // create data handler from MDHT document
130         xdsDocument.getContent(byte[].class);
131         xdsDocument.getContent(DataHandler.class);
132         assertEquals(3, xdsDocument.getContentsCount());
133 
134         // send the resulting request
135         Response response = (Response) send(uri, hrnRequest, Response.class);
136         assertEquals(Status.SUCCESS, response.getStatus());
137     }
138     
139     
140     @SuppressWarnings("unchecked")
141     private void initRequest() throws Exception {
142         Unmarshaller u = jaxbContext.createUnmarshaller();
143         InputStream is = getClass().getClassLoader().getResourceAsStream(
144             "continua-hrn/ProvideAndRegisterDocumentSet.xml");
145         hrnRequest = ((JAXBElement<ProvideAndRegisterDocumentSetRequestType>) u.unmarshal(is)).getValue();
146     }
147 }