2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.mso.bpmn.mock;
23 import org.jboss.resteasy.client.ClientRequest;
24 import org.jboss.resteasy.client.ClientResponse;
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;
35 * Simulates SDNC Adapter Callback response
38 public class SDNCAdapterAsyncTransformer extends ResponseTransformer {
40 private String syncResponse;
41 private String callbackResponseWrapper;
43 public SDNCAdapterAsyncTransformer() {
44 syncResponse = FileUtil.readResourceFile("__files/StandardSDNCSynchResponse.xml");
45 callbackResponseWrapper = FileUtil.readResourceFile("__files/sdncCallbackSoapWrapper.xml");
48 public String name() {
49 return "sdnc-adapter-vf-module-assign";
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
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
60 public ResponseDefinition transform(Request request, ResponseDefinition responseDefinition,
61 FileSource fileSource) {
63 String requestBody = request.getBodyAsString();
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>"));
68 System.out.println("responseDefinition: " + responseDefinition);
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);
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);
87 // Transform the SDNC response to escape < and >
88 sdncResponse = sdncResponse.replaceAll ("<", "<");
89 sdncResponse = sdncResponse.replaceAll (">", ">");
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);
95 Object sdncDelay = MockResource.getMockProperties().get("sdnc_delay");
97 if (sdncDelay != null) {
98 delay = Integer.parseInt(sdncDelay.toString());
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();
106 //return 200 OK with empty body
107 return ResponseDefinitionBuilder
108 .like(responseDefinition).but()
109 .withStatus(200).withBody(syncResponse).withHeader("Content-Type", "text/xml")
114 public boolean applyGlobally() {
120 * Callback response thread which sends the callback response asynchronously
123 private class CallbackResponseThread extends Thread {
125 private String callbackUrl;
126 private String payLoad;
129 public CallbackResponseThread(String callbackUrl, String payLoad, int delay) {
130 this.callbackUrl = callbackUrl;
131 this.payLoad = payLoad;
137 //Delay sending callback response
139 } catch (InterruptedException e1) {
140 // TODO Auto-generated catch block
141 e1.printStackTrace();
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);
148 ClientResponse result = request.post();
149 //System.err.println("Successfully posted callback:" + result.getStatus());
150 } catch (Exception e) {
151 // TODO Auto-generated catch block