Merge "Reorder modifiers"
[so.git] / bpmn / MSOCommonBPMN / src / test / java / org / openecomp / mso / bpmn / mock / SDNCAdapterNetworkTopologyMockTransformer.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
24 import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
25 import com.github.tomakehurst.wiremock.common.FileSource;
26 import com.github.tomakehurst.wiremock.extension.ResponseTransformer;
27 import com.github.tomakehurst.wiremock.http.Request;
28 import com.github.tomakehurst.wiremock.http.ResponseDefinition;
29
30 import org.openecomp.mso.logger.MsoLogger;
31
32 public class SDNCAdapterNetworkTopologyMockTransformer extends ResponseTransformer {
33
34         private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL);
35         
36         private String callbackResponse;
37         private String requestId;
38         
39         public SDNCAdapterNetworkTopologyMockTransformer() {
40                 callbackResponse = ""; // FileUtil.readResourceFile("__files/sdncDeleteNetworkTopologySimResponse.xml");
41         }
42
43         public SDNCAdapterNetworkTopologyMockTransformer(String requestId) {
44                 this.requestId = requestId;
45         }
46         
47         public String name() {
48                 return "network-topology-operation-transformer";
49         }
50
51         @Override
52         public ResponseDefinition transform(Request request, ResponseDefinition responseDefinition, FileSource fileSource) {
53                 String requestBody = request.getBodyAsString();
54                 
55                 String callbackUrl = requestBody.substring(requestBody.indexOf("<sdncadapter:CallbackUrl>")+25, requestBody.indexOf("</sdncadapter:CallbackUrl>"));
56                 String requestId = requestBody.substring(requestBody.indexOf("<sdncadapter:RequestId>")+23, requestBody.indexOf("</sdncadapter:RequestId>"));
57                 System.out.println("request callbackUrl : " + callbackUrl);
58                 System.out.println("request requestId : " + requestId);
59                 
60                 System.out.println("file path/name : " + responseDefinition.getBodyFileName());
61                 callbackResponse = FileUtil.readResourceFile("__files/" + responseDefinition.getBodyFileName());                
62                 // extract Response responseRequestId
63                 String responseRequestId = callbackResponse.substring(callbackResponse.indexOf("<RequestId>")+11, callbackResponse.indexOf("</RequestId>"));
64                 System.out.println("response requestId: " + responseRequestId);         
65                 System.out.println("callbackResponse (before): " + callbackResponse);
66                 callbackResponse = callbackResponse.replace(responseRequestId, requestId);                              
67                 if (this.requestId != null) {
68                         callbackResponse = callbackResponse.replace(this.requestId, requestId);
69                 } else {
70                         callbackResponse = callbackResponse.replace(responseRequestId, requestId);
71                 }       
72                 System.out.println("callbackResponse (after):" + callbackResponse);             
73
74                 Object sdncDelay = MockResource.getMockProperties().get("sdnc_delay");
75                 int delay = 300;
76                 if (sdncDelay != null) {
77                         delay = Integer.parseInt(sdncDelay.toString());
78                 }
79                 
80                 //Kick off callback thread
81                 System.out.println("(NetworkTopologyMockTransformer) callback Url:" + callbackUrl + ":delay:" + delay);
82                 CallbackResponseThread calbackResponseThread = new CallbackResponseThread(callbackUrl,callbackResponse, delay);
83                 calbackResponseThread.start();
84                 
85                 //return 200 OK with body
86                 return ResponseDefinitionBuilder
87                 .like(responseDefinition).but()
88                 .withStatus(200).withBody(callbackResponse).withHeader("Content-Type", "text/xml")
89                 .build();
90         }
91
92         @Override
93         public boolean applyGlobally() {
94             return false;
95         }
96         
97         private class CallbackResponseThread extends Thread {
98                 
99                 private String callbackUrl;
100                 private String payLoad;
101                 private int delay;
102                 
103                 public CallbackResponseThread(String callbackUrl, String payLoad, int delay) {
104                         this.callbackUrl = callbackUrl;
105                         this.payLoad = payLoad;
106                         this.delay = delay;
107                 }
108                 
109                 public void run () {
110                         try {
111                                 //Delay sending callback response
112                                 sleep(delay);
113                         } catch (InterruptedException e1) {
114                                 // TODO Auto-generated catch block
115                                 LOGGER.debug("Exception :",e1);
116                         }
117                         LOGGER.debug("Sending callback response to url: " + callbackUrl);
118                         ClientRequest request = new ClientRequest(callbackUrl);
119                         request.body("text/xml", payLoad);
120                         //System.err.println(payLoad);
121                         try {
122                                 ClientResponse result = request.post();
123                                 LOGGER.debug("Successfully posted callback? Status: " + result.getStatus());
124                         } catch (Exception e) {
125                                 // TODO Auto-generated catch block
126                             LOGGER.debug("catch error in - request.post() ");
127                                 LOGGER.debug("Exception :",e);
128                         }
129                 }
130                 
131         }
132 }