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.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;
34 * Simulates SDNC Adapter Callback response
37 public class SDNCAdapterMockTransformer extends ResponseTransformer {
39 private String callbackResponse;
40 private String requestId;
42 public SDNCAdapterMockTransformer() {
43 callbackResponse = FileUtil.readResourceFile("__files/sdncSimResponse.xml");
46 public SDNCAdapterMockTransformer(String requestId) {
47 this.requestId = requestId;
50 public String name() {
51 return "sdnc-adapter-transformer";
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
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
62 public ResponseDefinition transform(Request request, ResponseDefinition responseDefinition,
63 FileSource fileSource) {
64 String requestBody = request.getBodyAsString();
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>"));
69 callbackResponse = FileUtil.readResourceFile("__files/" + responseDefinition.getBodyFileName());
70 System.out.println("callbackResponse:" + callbackResponse);
72 if (this.requestId != null) {
73 callbackResponse = callbackResponse.replace(this.requestId, requestId);
75 callbackResponse = callbackResponse.replace("testRequestId", requestId);
79 Object sdncDelay = MockResource.getMockProperties().get("sdnc_delay");
81 if (sdncDelay != null) {
82 delay = Integer.parseInt(sdncDelay.toString());
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();
90 //return 200 OK with empty body
91 return ResponseDefinitionBuilder
92 .like(responseDefinition).but()
93 .withStatus(200).withBody("").withHeader("Content-Type", "text/xml")
98 public boolean applyGlobally() {
104 * Callback response thread which sends the callback response asynchronously
107 private class CallbackResponseThread extends Thread {
109 private String callbackUrl;
110 private String payLoad;
113 public CallbackResponseThread(String callbackUrl, String payLoad, int delay) {
114 this.callbackUrl = callbackUrl;
115 this.payLoad = payLoad;
121 //Delay sending callback response
123 } catch (InterruptedException e1) {
124 // TODO Auto-generated catch block
125 e1.printStackTrace();
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);
132 ClientResponse result = request.post();
133 //System.err.println("Successfully posted callback:" + result.getStatus());
134 } catch (Exception e) {
135 // TODO Auto-generated catch block