f30c223449b6b188b6005460abeceb7c90d70e5d
[so.git] / bpmn / MSOMockServer / src / main / java / org / openecomp / mso / bpmn / mock / SDNCAdapterMockTransformer.java
1 /*- 
2  * ============LICENSE_START======================================================= 
3  * OPENECOMP - MSO 
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.FileSource;
28 import com.github.tomakehurst.wiremock.extension.ResponseTransformer;
29 import com.github.tomakehurst.wiremock.http.Request;
30 import com.github.tomakehurst.wiremock.http.ResponseDefinition;
31
32 /**
33  * 
34  * Simulates SDNC Adapter Callback response
35  *
36  */
37 public class SDNCAdapterMockTransformer extends ResponseTransformer {
38
39         private String callbackResponse;
40         private String requestId;
41         
42         public SDNCAdapterMockTransformer() {
43                 callbackResponse = FileUtil.readResourceFile("__files/sdncSimResponse.xml");
44         }
45
46         public SDNCAdapterMockTransformer(String requestId) {
47                 this.requestId = requestId;
48         }
49         
50         public String name() {
51                 return "sdnc-adapter-transformer";
52         }
53
54         /**
55          * Grab the incoming request xml,extract the request id and replace the stub response with the incoming request id
56          * so that callback response can be correlated
57          * 
58          * Mock Resource can be used to add dynamic properties. If sdnc_delay is not in the list by default waits for 300ms before
59          * the callback response is sent
60          */
61         @Override
62         public ResponseDefinition transform(Request request, ResponseDefinition responseDefinition,
63                         FileSource fileSource) {
64                 String requestBody = request.getBodyAsString();
65                 
66                 String callbackUrl = requestBody.substring(requestBody.indexOf("<sdncadapter:CallbackUrl>")+25, requestBody.indexOf("</sdncadapter:CallbackUrl>"));
67                 String requestId = requestBody.substring(requestBody.indexOf("<sdncadapter:RequestId>")+23, requestBody.indexOf("</sdncadapter:RequestId>"));
68
69                 callbackResponse = FileUtil.readResourceFile("__files/" + responseDefinition.getBodyFileName());
70                 System.out.println("callbackResponse:" + callbackResponse);
71                 
72                 if (this.requestId != null) {
73                         callbackResponse = callbackResponse.replace(this.requestId, requestId);
74                 } else {
75                         callbackResponse = callbackResponse.replace("testRequestId", requestId);
76                 }
77                 
78
79                 Object sdncDelay = MockResource.getMockProperties().get("sdnc_delay");
80                 int delay = 300;
81                 if (sdncDelay != null) {
82                         delay = Integer.parseInt(sdncDelay.toString());
83                 }
84                 
85                 //Kick off callback thread
86                 System.out.println("callback Url:" + callbackUrl + ":delay:" + delay);
87                 CallbackResponseThread calbackResponseThread = new CallbackResponseThread(callbackUrl,callbackResponse, delay);
88                 calbackResponseThread.start();
89                 
90                 //return 200 OK with empty body
91                 return ResponseDefinitionBuilder
92                 .like(responseDefinition).but()
93                 .withStatus(200).withBody("").withHeader("Content-Type", "text/xml")
94                 .build();
95         }
96
97         @Override
98         public boolean applyGlobally() {
99             return false;
100         }
101         
102         /**
103          * 
104          * Callback response thread which sends the callback response asynchronously
105          *
106          */
107         private class CallbackResponseThread extends Thread {
108                 
109                 private String callbackUrl;
110                 private String payLoad;
111                 private int delay;
112                 
113                 public CallbackResponseThread(String callbackUrl, String payLoad, int delay) {
114                         this.callbackUrl = callbackUrl;
115                         this.payLoad = payLoad;
116                         this.delay = delay;
117                 }
118                 
119                 public void run () {
120                         try {
121                                 //Delay sending callback response
122                                 sleep(delay);
123                         } catch (InterruptedException e1) {
124                                 // TODO Auto-generated catch block
125                                 e1.printStackTrace();
126                         }
127                         System.out.println("Sending callback response:" + callbackUrl);
128                         ClientRequest request = new ClientRequest(callbackUrl);
129                         request.body("text/xml", payLoad);
130                         System.err.println(payLoad);
131                         try {
132                                 ClientResponse result = request.post();
133                                 //System.err.println("Successfully posted callback:" + result.getStatus());
134                         } catch (Exception e) {
135                                 // TODO Auto-generated catch block
136                                 e.printStackTrace();
137                         }
138                 }
139                 
140         }
141 }