Containerization feature of SO
[so.git] / bpmn / MSOCommonBPMN / src / test / java / org / onap / so / 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.onap.so.bpmn.mock;
22
23 import javax.ws.rs.core.UriBuilder;
24
25 import org.onap.so.client.HttpClient;
26 import org.onap.so.utils.TargetEntity;
27
28 import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
29 import com.github.tomakehurst.wiremock.common.BinaryFile;
30 import com.github.tomakehurst.wiremock.common.FileSource;
31 import com.github.tomakehurst.wiremock.extension.Parameters;
32 import com.github.tomakehurst.wiremock.extension.ResponseDefinitionTransformer;
33 import com.github.tomakehurst.wiremock.http.Request;
34 import com.github.tomakehurst.wiremock.http.ResponseDefinition;
35
36 /**
37  * 
38  * Simulates SDNC Adapter Callback response
39  *
40  */
41 public class SDNCAdapterAsyncTransformer extends ResponseDefinitionTransformer {
42
43         private String syncResponse;
44         private String callbackResponseWrapper;
45         
46         public SDNCAdapterAsyncTransformer() {
47                 syncResponse = FileUtil.readResourceFile("__files/StandardSDNCSynchResponse.xml");
48                 callbackResponseWrapper = FileUtil.readResourceFile("__files/sdncCallbackSoapWrapper.xml");
49         }
50         
51         @Override
52         public String getName() {
53                 return "sdnc-adapter-vf-module-assign";
54         }
55
56         /**
57          * Grab the incoming request xml,extract the request id and replace the stub response with the incoming request id
58          * so that callback response can be correlated
59          * 
60          * Mock Resource can be used to add dynamic properties. If sdnc_delay is not in the list by default waits for 300ms before
61          * the callback response is sent
62          */
63         @Override
64         public ResponseDefinition transform(Request request, ResponseDefinition responseDefinition,
65                         FileSource fileSource, Parameters parameters) {
66                 
67                 String requestBody = request.getBodyAsString();
68                 
69                 String callbackUrl = requestBody.substring(requestBody.indexOf("<sdncadapter:CallbackUrl>")+25, requestBody.indexOf("</sdncadapter:CallbackUrl>"));
70                 String requestId = requestBody.substring(requestBody.indexOf("<sdncadapter:RequestId>")+23, requestBody.indexOf("</sdncadapter:RequestId>"));
71
72                 System.out.println("responseDefinition: " + responseDefinition);
73
74                 // For this mock, the mapped response body is the Async callback (since the sync response is generic for all requests)
75                 String sdncResponse = responseDefinition.getBody();
76                 System.out.println("sdncResponse:" + sdncResponse);
77
78                 if (sdncResponse == null) {
79                         // Body wasn't specified.  Check for a body file
80                         String bodyFileName = responseDefinition.getBodyFileName();
81                         System.out.println("bodyFileName" + bodyFileName);
82                         if (bodyFileName != null) {
83                                 System.out.println("fileSource Class: " + fileSource.getClass().getName());
84                                 BinaryFile bodyFile = fileSource.getBinaryFileNamed(bodyFileName);
85                                 byte[] responseFile = bodyFile.readContents();
86                                 sdncResponse = new String(responseFile);
87                                 System.out.println("sdncResponse(2):" + sdncResponse);
88                         }
89                 }
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                         try {
144                                 HttpClient client = new HttpClient(UriBuilder.fromUri(callbackUrl).build().toURL(), "text/xml", TargetEntity.SDNC_ADAPTER);
145                                 client.post(payLoad);
146                         } catch (Exception e) {
147                                 // TODO Auto-generated catch block
148                                 e.printStackTrace();
149                         }
150                 }
151                 
152         }
153 }