View Javadoc
1   /*
2    * Copyright 2009 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.xds.iti42;
17  
18  import org.apache.cxf.endpoint.Server;
19  import org.apache.cxf.frontend.ServerFactoryBean;
20  import org.apache.cxf.transport.servlet.CXFServlet;
21  import org.junit.After;
22  import org.junit.Assert;
23  import org.junit.Before;
24  import org.junit.Test;
25  import org.openehealth.ipf.commons.ihe.ws.JaxWsClientFactory;
26  import org.openehealth.ipf.commons.ihe.ws.JaxWsRequestClientFactory;
27  import org.openehealth.ipf.commons.ihe.ws.JaxWsRequestServiceFactory;
28  import org.openehealth.ipf.commons.ihe.ws.JaxWsServiceFactory;
29  import org.openehealth.ipf.commons.ihe.ws.server.JettyServer;
30  import org.openehealth.ipf.commons.ihe.ws.server.ServletServer;
31  import org.openehealth.ipf.commons.ihe.xds.core.SampleData;
32  import org.openehealth.ipf.commons.ihe.xds.core.audit.XdsAuditDataset;
33  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLFactory;
34  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLRegistryResponse;
35  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLSubmitObjectsRequest;
36  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.EbXMLFactory30;
37  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.EbXMLRegistryResponse30;
38  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.EbXMLSubmitObjectsRequest30;
39  import org.openehealth.ipf.commons.ihe.xds.core.requests.RegisterDocumentSet;
40  import org.openehealth.ipf.commons.ihe.xds.core.responses.*;
41  import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.lcm.SubmitObjectsRequest;
42  import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rs.RegistryResponseType;
43  import org.openehealth.ipf.commons.ihe.xds.core.transform.requests.RegisterDocumentSetTransformer;
44  import org.openehealth.ipf.commons.ihe.xds.core.transform.responses.ResponseTransformer;
45  import org.openehealth.ipf.commons.ihe.xds.core.validate.requests.SubmitObjectsRequestValidator;
46  import org.openehealth.ipf.commons.ihe.xds.core.validate.responses.RegistryResponseValidator;
47  import org.openehealth.ipf.commons.ihe.xds.iti42.Iti42PortType;
48  
49  import java.io.IOException;
50  import java.net.URISyntaxException;
51  import java.util.Arrays;
52  
53  import static org.openehealth.ipf.commons.ihe.xds.XDS.Interactions.ITI_42;
54  
55  public class CxfEndpointTest {
56      private final EbXMLFactory factory = new EbXMLFactory30();
57  
58      private final RegisterDocumentSetTransformer reqTransformer =
59          new RegisterDocumentSetTransformer(factory);
60  
61      private final ResponseTransformer respTransformer =
62          new ResponseTransformer(factory);
63  
64      private int port;
65      private JettyServer server;
66  
67      @Before
68      public void setUp() throws IOException, URISyntaxException {
69          port = ServletServer.getFreePort();
70          server = new JettyServer();
71          server.setContextResource(getClass().getResource("/cxf-context.xml").toURI().toString());
72          server.setPort(port);
73          server.setContextPath("/");
74          server.setServletPath("/*");
75          server.setServlet(new CXFServlet());
76  
77          server.start();
78      }
79  
80      @After
81      public void tearDown() {
82          server.stop();
83      }
84  
85      @Test
86      public void test() throws Exception {
87          runRequestAndExpectFailure();
88  
89          JaxWsServiceFactory<? extends XdsAuditDataset> serviceFactory = new JaxWsRequestServiceFactory<>(
90                  ITI_42.getWsTransactionConfiguration(), "/iti-42", null, null, null, null);
91          ServerFactoryBean factory = serviceFactory.createServerFactory(MyIti42.class);
92          Server serviceServer = factory.create();
93  
94          runRequestAndExpectFailure();
95  
96          serviceServer.start();
97          Assert.assertEquals(Status.SUCCESS, runRequest().getStatus());
98  
99          serviceServer.stop();
100         runRequestAndExpectFailure();
101 
102         //serviceServer = factory.create();
103         serviceServer.start();
104         Assert.assertEquals(Status.SUCCESS, runRequest().getStatus());
105 
106         serviceServer.stop();
107     }
108 
109     private void runRequestAndExpectFailure() {
110         try {
111             runRequest();
112             Assert.fail("Expected Exception: " + RuntimeException.class);
113         }
114         catch (RuntimeException e) {
115             // ok
116         }
117     }
118 
119     private Response runRequest() {
120         JaxWsClientFactory<? extends XdsAuditDataset> clientFactory = new JaxWsRequestClientFactory<>(
121                 ITI_42.getWsTransactionConfiguration(),
122                 "http://localhost:" + port + "/iti-42",
123                 null, null, null, null, null, null);
124 
125         Iti42PortType client = (Iti42PortType) clientFactory.getClient();
126         RegisterDocumentSet request = SampleData.createRegisterDocumentSet();
127         EbXMLSubmitObjectsRequest ebXMLReq = reqTransformer.toEbXML(request);
128         SubmitObjectsRequest rawReq = (SubmitObjectsRequest) ebXMLReq.getInternal();
129         RegistryResponseType rawResp = client.documentRegistryRegisterDocumentSetB(rawReq);
130         EbXMLRegistryResponse30 ebXMLResp = new EbXMLRegistryResponse30(rawResp);
131         return respTransformer.fromEbXML(ebXMLResp);
132     }
133 
134     public static class MyIti42 implements Iti42PortType {
135         private final EbXMLFactory factory = new EbXMLFactory30();
136 
137         private final RegisterDocumentSetTransformer reqTransformer =
138             new RegisterDocumentSetTransformer(factory);
139 
140         private final ResponseTransformer respTransformer =
141             new ResponseTransformer(factory);
142 
143         private final SubmitObjectsRequestValidator reqValidator =
144             new SubmitObjectsRequestValidator();
145 
146         private final RegistryResponseValidator respValidator =
147             new RegistryResponseValidator();
148 
149         @Override
150         public RegistryResponseType documentRegistryRegisterDocumentSetB(SubmitObjectsRequest rawReq) {
151 
152             EbXMLSubmitObjectsRequest30 ebXMLReq = new EbXMLSubmitObjectsRequest30(rawReq);
153             reqValidator.validate(ebXMLReq, ITI_42);
154             RegisterDocumentSet request = reqTransformer.fromEbXML(ebXMLReq);
155 
156             Response response = new Response(Status.SUCCESS);
157             if (!request.getSubmissionSet().getEntryUuid().equals("submissionSet01")) {
158                 response.setStatus(Status.FAILURE);
159                 response.setErrors(Arrays.asList(new ErrorInfo(ErrorCode.REGISTRY_ERROR, "unexpected value", Severity.ERROR, null, null)));
160             }
161 
162             EbXMLRegistryResponse ebXMLResp = respTransformer.toEbXML(response);
163             respValidator.validate(ebXMLResp, ITI_42);
164             return (RegistryResponseType) ebXMLResp.getInternal();
165         }
166     }
167 }