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