Merge "Reorder modifiers"
[so.git] / bpmn / MSOCommonBPMN / src / test / java / org / openecomp / mso / bpmn / mock / SDNCAdapterAsyncTransformer.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.openecomp.mso.bpmn.mock;
22
23 import org.jboss.resteasy.client.ClientRequest;
24 import org.jboss.resteasy.client.ClientResponse;
25
26 import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
27 import com.github.tomakehurst.wiremock.common.BinaryFile;
28 import com.github.tomakehurst.wiremock.common.FileSource;
29 import com.github.tomakehurst.wiremock.extension.ResponseTransformer;
30 import com.github.tomakehurst.wiremock.http.Request;
31 import com.github.tomakehurst.wiremock.http.ResponseDefinition;
32
33 /**
34  * 
35  * Simulates SDNC Adapter Callback response
36  *
37  */
38 public class SDNCAdapterAsyncTransformer extends ResponseTransformer {
39
40         private String syncResponse;
41         private String callbackResponseWrapper;
42         
43         public SDNCAdapterAsyncTransformer() {
44                 syncResponse = FileUtil.readResourceFile("__files/StandardSDNCSynchResponse.xml");
45                 callbackResponseWrapper = FileUtil.readResourceFile("__files/sdncCallbackSoapWrapper.xml");
46         }
47
48         public String name() {
49                 return "sdnc-adapter-vf-module-assign";
50         }
51
52         /**
53          * Grab the incoming request xml,extract the request id and replace the stub response with the incoming request id
54          * so that callback response can be correlated
55          * 
56          * Mock Resource can be used to add dynamic properties. If sdnc_delay is not in the list by default waits for 300ms before
57          * the callback response is sent
58          */
59         @Override
60         public ResponseDefinition transform(Request request, ResponseDefinition responseDefinition,
61                         FileSource fileSource) {
62                 
63                 String requestBody = request.getBodyAsString();
64                 
65                 String callbackUrl = requestBody.substring(requestBody.indexOf("<sdncadapter:CallbackUrl>")+25, requestBody.indexOf("</sdncadapter:CallbackUrl>"));
66                 String requestId = requestBody.substring(requestBody.indexOf("<sdncadapter:RequestId>")+23, requestBody.indexOf("</sdncadapter:RequestId>"));
67
68                 System.out.println("responseDefinition: " + responseDefinition);
69
70                 // For this mock, the mapped response body is the Async callback (since the sync response is generic for all requests)
71                 String sdncResponse = responseDefinition.getBody();
72                 System.out.println("sdncResponse:" + sdncResponse);
73
74                 if (sdncResponse == null) {
75                         // Body wasn't specified.  Check for a body file
76                         String bodyFileName = responseDefinition.getBodyFileName();
77                         System.out.println("bodyFileName" + bodyFileName);
78                         if (bodyFileName != null) {
79                                 System.out.println("fileSource Class: " + fileSource.getClass().getName());
80                                 BinaryFile bodyFile = fileSource.getBinaryFileNamed(bodyFileName);
81                                 byte[] responseFile = bodyFile.readContents();
82                                 sdncResponse = new String(responseFile);
83                                 System.out.println("sdncResponse(2):" + sdncResponse);
84                         }
85                 }
86                 
87                 // Transform the SDNC response to escape < and >
88                 sdncResponse = sdncResponse.replaceAll ("<", "&lt;");
89                 sdncResponse = sdncResponse.replaceAll (">", "&gt;");
90                 
91                 // Next substitute the SDNC response into the callbackResponse (SOAP wrapper).
92                 // Also, replace the request ID wherever it appears
93                 String callbackResponse = callbackResponseWrapper.replace("SDNC_RESPONSE_DATA", sdncResponse).replaceAll("SDNC_REQUEST_ID", requestId);
94                 
95                 Object sdncDelay = MockResource.getMockProperties().get("sdnc_delay");
96                 int delay = 2000;
97                 if (sdncDelay != null) {
98                         delay = Integer.parseInt(sdncDelay.toString());
99                 }
100                 
101                 //Kick off callback thread
102                 System.out.println("callback Url:" + callbackUrl + ":delay:" + delay);
103                 CallbackResponseThread calbackResponseThread = new CallbackResponseThread(callbackUrl,callbackResponse, delay);
104                 calbackResponseThread.start();
105                 
106                 //return 200 OK with empty body
107                 return ResponseDefinitionBuilder
108                 .like(responseDefinition).but()
109                 .withStatus(200).withBody(syncResponse).withHeader("Content-Type", "text/xml")
110                 .build();
111         }
112
113         @Override
114         public boolean applyGlobally() {
115             return false;
116         }
117         
118         /**
119          * 
120          * Callback response thread which sends the callback response asynchronously
121          *
122          */
123         private class CallbackResponseThread extends Thread {
124                 
125                 private String callbackUrl;
126                 private String payLoad;
127                 private int delay;
128                 
129                 public CallbackResponseThread(String callbackUrl, String payLoad, int delay) {
130                         this.callbackUrl = callbackUrl;
131                         this.payLoad = payLoad;
132                         this.delay = delay;
133                 }
134                 
135                 public void run () {
136                         try {
137                                 //Delay sending callback response
138                                 sleep(delay);
139                         } catch (InterruptedException e1) {
140                                 // TODO Auto-generated catch block
141                                 e1.printStackTrace();
142                         }
143                         System.out.println("Sending callback response:" + callbackUrl);
144                         ClientRequest request = new ClientRequest(callbackUrl);
145                         request.body("text/xml", payLoad);
146                         System.err.println(payLoad);
147                         try {
148                                 ClientResponse result = request.post();
149                                 //System.err.println("Successfully posted callback:" + result.getStatus());
150                         } catch (Exception e) {
151                                 // TODO Auto-generated catch block
152                                 e.printStackTrace();
153                         }
154                 }
155                 
156         }
157 }