dc5c36f5a720e4e2c9b643eba3d9edb5bf6af825
[so.git] / bpmn / mso-infrastructure-bpmn / src / test / java / org / onap / so / bpmn / common / SDNCAdapterCallbackRule.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.bpmn.common;
22
23 import javax.xml.ws.Endpoint;
24
25 import org.camunda.bpm.engine.ProcessEngineServices;
26 import org.junit.rules.TestRule;
27 import org.junit.runner.Description;
28 import org.junit.runners.model.Statement;
29 import org.onap.so.bpmn.common.workflow.service.SDNCAdapterCallbackServiceImpl;
30
31 /**
32  * A JUnit rule that starts the SDNC Adapter Callback Service before
33  * every test, and tears it down after every test.  Example:
34  * <pre>
35  *     @Rule
36  *     public final SDNCAdapterCallbackRule sdncAdapterCallbackRule =
37  *         new SDNCAdapterCallbackRule(processEngineRule);
38  * </pre>
39  */
40 public class SDNCAdapterCallbackRule implements TestRule {
41         public static final String DEFAULT_ENDPOINT_URL =
42                 "http://localhost:28080/mso/SDNCAdapterCallbackService";
43
44         private final ProcessEngineServices processEngineServices;
45         private final String endpointUrl;
46
47         public SDNCAdapterCallbackRule(ProcessEngineServices processEngineServices) {
48                 this(processEngineServices, DEFAULT_ENDPOINT_URL);
49         }
50
51         public SDNCAdapterCallbackRule(ProcessEngineServices processEngineServices,
52                         String endpointUrl) {
53                 this.processEngineServices = processEngineServices;
54                 this.endpointUrl = endpointUrl;
55         }
56
57         @Override
58         public Statement apply(final Statement baseStmt, Description description) {
59                 return new Statement() {
60                         @Override
61                         public void evaluate() throws Throwable {
62                                 Endpoint endpoint = null;
63
64                                 try {
65                                         SDNCAdapterCallbackServiceImpl sdncCallbackService = new SDNCAdapterCallbackServiceImpl();
66                                         sdncCallbackService.setProcessEngineServices4junit(processEngineServices);
67
68                                         System.setProperty("com.sun.xml.ws.transport.http.HttpAdapter.dump", "true");
69                                         System.setProperty("com.sun.xml.internal.ws.transport.http.HttpAdapter.dump", "true");
70
71                                         System.out.println("Publishing Endpoint - " + endpointUrl);
72                                         endpoint = Endpoint.publish(endpointUrl, sdncCallbackService);
73
74                                         baseStmt.evaluate();
75                                 } finally {
76                                         if (endpoint != null) {
77                                                 System.out.println("Stopping Endpoint - " + endpointUrl);
78                                                 endpoint.stop();
79                                         }
80                                 }
81                         }
82                 };
83         }
84 }