Merge "Reorder modifiers"
[so.git] / bpmn / MSOCommonBPMN / src / test / java / org / openecomp / mso / bpmn / mock / SDNCAdapterMockTransformer.java
1 /* 
2  * ============LICENSE_START======================================================= 
3  * ONAP - SO 
4  * ================================================================================ 
5  * Licensed under the Apache License, Version 2.0 (the "License"); 
6  * you may not use this file except in compliance with the License. 
7  * You may obtain a copy of the License at 
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0 
10  * 
11  * Unless required by applicable law or agreed to in writing, software 
12  * distributed under the License is distributed on an "AS IS" BASIS, 
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
14  * See the License for the specific language governing permissions and 
15  * limitations under the License. 
16  * ============LICENSE_END========================================================= 
17  */ 
18
19 package org.openecomp.mso.bpmn.mock;
20
21 import org.jboss.resteasy.client.ClientRequest;
22 import org.jboss.resteasy.client.ClientResponse;
23 import org.openecomp.mso.logger.MsoLogger;
24
25 import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
26 import com.github.tomakehurst.wiremock.common.FileSource;
27 import com.github.tomakehurst.wiremock.extension.ResponseTransformer;
28 import com.github.tomakehurst.wiremock.http.Request;
29 import com.github.tomakehurst.wiremock.http.ResponseDefinition;
30
31 import org.openecomp.mso.logger.MsoLogger;
32 /**
33  * 
34  * Simulates SDNC Adapter Callback response
35  *
36  */
37 public class SDNCAdapterMockTransformer extends ResponseTransformer {
38
39         private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL);
40         private String callbackResponse;
41         private String requestId;
42         
43         public SDNCAdapterMockTransformer() {
44                 callbackResponse = FileUtil.readResourceFile("__files/sdncSimResponse.xml");
45         }
46
47         public SDNCAdapterMockTransformer(String requestId) {
48                 this.requestId = requestId;
49         }
50         
51         public String name() {
52                 return "sdnc-adapter-transformer";
53         }
54
55         /**
56          * Grab the incoming request xml,extract the request id and replace the stub response with the incoming request id
57          * so that callback response can be correlated
58          * 
59          * Mock Resource can be used to add dynamic properties. If sdnc_delay is not in the list by default waits for 300ms before
60          * the callback response is sent
61          */
62         @Override
63         public ResponseDefinition transform(Request request, ResponseDefinition responseDefinition,
64                         FileSource fileSource) {
65                 String requestBody = request.getBodyAsString();
66                 
67                 String callbackUrl = requestBody.substring(requestBody.indexOf("<sdncadapter:CallbackUrl>")+25, requestBody.indexOf("</sdncadapter:CallbackUrl>"));
68                 String requestId = requestBody.substring(requestBody.indexOf("<sdncadapter:RequestId>")+23, requestBody.indexOf("</sdncadapter:RequestId>"));
69
70                 callbackResponse = FileUtil.readResourceFile("__files/" + responseDefinition.getBodyFileName());
71                 System.out.println("callbackResponse:" + callbackResponse);
72                 
73                 if (this.requestId != null) {
74                         callbackResponse = callbackResponse.replace(this.requestId, requestId);
75                 } else {
76                         callbackResponse = callbackResponse.replace("testRequestId", requestId);
77                 }
78                 
79
80                 Object sdncDelay = MockResource.getMockProperties().get("sdnc_delay");
81                 int delay = 300;
82                 if (sdncDelay != null) {
83                         delay = Integer.parseInt(sdncDelay.toString());
84                 }
85                 
86                 //Kick off callback thread
87                 System.out.println("callback Url:" + callbackUrl + ":delay:" + delay);
88                 CallbackResponseThread calbackResponseThread = new CallbackResponseThread(callbackUrl,callbackResponse, delay);
89                 calbackResponseThread.start();
90                 
91                 //return 200 OK with empty body
92                 return ResponseDefinitionBuilder
93                 .like(responseDefinition).but()
94                 .withStatus(200).withBody("").withHeader("Content-Type", "text/xml")
95                 .build();
96         }
97
98         @Override
99         public boolean applyGlobally() {
100             return false;
101         }
102         
103         /**
104          * 
105          * Callback response thread which sends the callback response asynchronously
106          *
107          */
108         private class CallbackResponseThread extends Thread {
109                 
110                 private String callbackUrl;
111                 private String payLoad;
112                 private int delay;
113                 
114                 public CallbackResponseThread(String callbackUrl, String payLoad, int delay) {
115                         this.callbackUrl = callbackUrl;
116                         this.payLoad = payLoad;
117                         this.delay = delay;
118                 }
119                 
120                 public void run () {
121                         try {
122                                 //Delay sending callback response
123                                 sleep(delay);
124                         } catch (InterruptedException e1) {
125                                 LOGGER.debug("Exception :",e1);
126                         }
127                         LOGGER.debug("Sending callback response:" + callbackUrl);
128                         ClientRequest request = new ClientRequest(callbackUrl);
129                         request.body("text/xml", payLoad);
130                         System.err.println(payLoad);
131                         try {
132                                 ClientResponse result = request.post();
133                                 //System.err.println("Successfully posted callback:" + result.getStatus());
134                         } catch (Exception e) {
135                                 LOGGER.debug("Exception :",e);
136                         }
137                 }
138                 
139         }
140 }