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.onap.so.bpmn.mock;
23 import javax.ws.rs.core.UriBuilder;
25 import org.onap.so.client.HttpClient;
26 import org.onap.so.utils.TargetEntity;
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;
38 * Simulates SDNC Adapter Callback response
41 public class SDNCAdapterAsyncTransformer extends ResponseDefinitionTransformer {
43 private String syncResponse;
44 private String callbackResponseWrapper;
46 public SDNCAdapterAsyncTransformer() {
47 syncResponse = FileUtil.readResourceFile("__files/StandardSDNCSynchResponse.xml");
48 callbackResponseWrapper = FileUtil.readResourceFile("__files/sdncCallbackSoapWrapper.xml");
52 public String getName() {
53 return "sdnc-adapter-vf-module-assign";
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
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
64 public ResponseDefinition transform(Request request, ResponseDefinition responseDefinition,
65 FileSource fileSource, Parameters parameters) {
67 String requestBody = request.getBodyAsString();
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>"));
72 System.out.println("responseDefinition: " + responseDefinition);
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);
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);
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();
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