Containerization feature of SO
[so.git] / bpmn / MSOCommonBPMN / src / test / java / org / onap / so / bpmn / mock / SDNCAdapterMockTransformer.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.logger.MsoLogger;
27 import org.onap.so.utils.TargetEntity;
28
29 import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
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 SDNCAdapterMockTransformer extends ResponseDefinitionTransformer {
42
43         private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, SDNCAdapterMockTransformer.class);
44         private String callbackResponse;
45         private String requestId;
46         
47         public SDNCAdapterMockTransformer() {
48                 callbackResponse = FileUtil.readResourceFile("__files/sdncSimResponse.xml");
49         }
50
51         public SDNCAdapterMockTransformer(String requestId) {
52                 this.requestId = requestId;
53         }
54         
55         @Override
56         public String getName() {
57                 return "sdnc-adapter-transformer";
58         }
59
60         /**
61          * Grab the incoming request xml,extract the request id and replace the stub response with the incoming request id
62          * so that callback response can be correlated
63          * 
64          * Mock Resource can be used to add dynamic properties. If sdnc_delay is not in the list by default waits for 300ms before
65          * the callback response is sent
66          */
67         @Override
68         public ResponseDefinition transform(Request request, ResponseDefinition responseDefinition,
69                         FileSource fileSource, Parameters parameters) {
70                 String requestBody = request.getBodyAsString();
71                 
72                 String callbackUrl = requestBody.substring(requestBody.indexOf("<sdncadapter:CallbackUrl>")+25, requestBody.indexOf("</sdncadapter:CallbackUrl>"));
73                 String requestId = requestBody.substring(requestBody.indexOf("<sdncadapter:RequestId>")+23, requestBody.indexOf("</sdncadapter:RequestId>"));
74
75                 callbackResponse = FileUtil.readResourceFile("__files/" + responseDefinition.getBodyFileName());
76                 System.out.println("callbackResponse:" + callbackResponse);
77                 
78                 if (this.requestId != null) {
79                         callbackResponse = callbackResponse.replace(this.requestId, requestId);
80                 } else {
81                         callbackResponse = callbackResponse.replace("testRequestId", requestId);
82                 }
83                 
84
85                 Object sdncDelay = MockResource.getMockProperties().get("sdnc_delay");
86                 int delay = 300;
87                 if (sdncDelay != null) {
88                         delay = Integer.parseInt(sdncDelay.toString());
89                 }
90                 
91                 //Kick off callback thread
92                 System.out.println("callback Url:" + callbackUrl + ":delay:" + delay);
93                 CallbackResponseThread calbackResponseThread = new CallbackResponseThread(callbackUrl,callbackResponse, delay);
94                 calbackResponseThread.start();
95                 
96                 //return 200 OK with empty body
97                 return ResponseDefinitionBuilder
98                 .like(responseDefinition).but()
99                 .withStatus(200).withBody("").withHeader("Content-Type", "text/xml")
100                 .build();
101         }
102
103         @Override
104         public boolean applyGlobally() {
105             return false;
106         }
107         
108         /**
109          * 
110          * Callback response thread which sends the callback response asynchronously
111          *
112          */
113         private class CallbackResponseThread extends Thread {
114                 
115                 private String callbackUrl;
116                 private String payLoad;
117                 private int delay;
118                 
119                 public CallbackResponseThread(String callbackUrl, String payLoad, int delay) {
120                         this.callbackUrl = callbackUrl;
121                         this.payLoad = payLoad;
122                         this.delay = delay;
123                 }
124                 
125                 public void run () {
126                         try {
127                                 //Delay sending callback response
128                                 sleep(delay);
129                         } catch (InterruptedException e1) {
130                                 LOGGER.debug("Exception :",e1);
131                         }
132                         LOGGER.debug("Sending callback response:" + callbackUrl);
133                         try {
134                                 HttpClient client = new HttpClient(UriBuilder.fromUri(callbackUrl).build().toURL(), "text/xml", TargetEntity.SDNC_ADAPTER);
135                                 client.post(payLoad);
136                         } catch (Exception e) {
137                                 LOGGER.debug("Exception :",e);
138                         }
139                 }
140                 
141         }
142 }