2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.openecomp.mso.bpmn.mock;
24 import org.jboss.resteasy.client.ClientRequest;
25 import org.jboss.resteasy.client.ClientResponse;
26 import org.openecomp.mso.logger.MsoLogger;
28 import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
29 import com.github.tomakehurst.wiremock.common.FileSource;
30 import com.github.tomakehurst.wiremock.extension.ResponseTransformer;
31 import com.github.tomakehurst.wiremock.http.Request;
32 import com.github.tomakehurst.wiremock.http.ResponseDefinition;
34 import org.openecomp.mso.logger.MsoLogger;
37 * Simulates SDNC Adapter Callback response
40 public class SDNCAdapterMockTransformer extends ResponseTransformer {
42 private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL);
43 private String callbackResponse;
44 private String requestId;
46 public SDNCAdapterMockTransformer() {
47 callbackResponse = FileUtil.readResourceFile("__files/sdncSimResponse.xml");
50 public SDNCAdapterMockTransformer(String requestId) {
51 this.requestId = requestId;
54 public String name() {
55 return "sdnc-adapter-transformer";
59 * Grab the incoming request xml,extract the request id and replace the stub response with the incoming request id
60 * so that callback response can be correlated
62 * Mock Resource can be used to add dynamic properties. If sdnc_delay is not in the list by default waits for 300ms before
63 * the callback response is sent
66 public ResponseDefinition transform(Request request, ResponseDefinition responseDefinition,
67 FileSource fileSource) {
68 String requestBody = request.getBodyAsString();
70 String callbackUrl = requestBody.substring(requestBody.indexOf("<sdncadapter:CallbackUrl>")+25, requestBody.indexOf("</sdncadapter:CallbackUrl>"));
71 String requestId = requestBody.substring(requestBody.indexOf("<sdncadapter:RequestId>")+23, requestBody.indexOf("</sdncadapter:RequestId>"));
73 callbackResponse = FileUtil.readResourceFile("__files/" + responseDefinition.getBodyFileName());
74 System.out.println("callbackResponse:" + callbackResponse);
76 if (this.requestId != null) {
77 callbackResponse = callbackResponse.replace(this.requestId, requestId);
79 callbackResponse = callbackResponse.replace("testRequestId", requestId);
83 Object sdncDelay = MockResource.getMockProperties().get("sdnc_delay");
85 if (sdncDelay != null) {
86 delay = Integer.parseInt(sdncDelay.toString());
89 //Kick off callback thread
90 System.out.println("callback Url:" + callbackUrl + ":delay:" + delay);
91 CallbackResponseThread calbackResponseThread = new CallbackResponseThread(callbackUrl,callbackResponse, delay);
92 calbackResponseThread.start();
94 //return 200 OK with empty body
95 return ResponseDefinitionBuilder
96 .like(responseDefinition).but()
97 .withStatus(200).withBody("").withHeader("Content-Type", "text/xml")
102 public boolean applyGlobally() {
108 * Callback response thread which sends the callback response asynchronously
111 private class CallbackResponseThread extends Thread {
113 private String callbackUrl;
114 private String payLoad;
117 public CallbackResponseThread(String callbackUrl, String payLoad, int delay) {
118 this.callbackUrl = callbackUrl;
119 this.payLoad = payLoad;
125 //Delay sending callback response
127 } catch (InterruptedException e1) {
128 LOGGER.debug("Exception :",e1);
130 System.out.println("Sending callback response:" + callbackUrl);
131 ClientRequest request = new ClientRequest(callbackUrl);
132 request.body("text/xml", payLoad);
133 System.err.println(payLoad);
135 ClientResponse result = request.post();
136 //System.err.println("Successfully posted callback:" + result.getStatus());
137 } catch (Exception e) {
138 LOGGER.debug("Exception :",e);