2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 * ============LICENSE_END=========================================================
19 package org.openecomp.mso.bpmn.mock;
21 import org.jboss.resteasy.client.ClientRequest;
22 import org.jboss.resteasy.client.ClientResponse;
23 import org.openecomp.mso.logger.MsoLogger;
25 import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
26 import com.github.tomakehurst.wiremock.common.FileSource;
27 import com.github.tomakehurst.wiremock.extension.ResponseTransformer;
28 import com.github.tomakehurst.wiremock.http.Request;
29 import com.github.tomakehurst.wiremock.http.ResponseDefinition;
31 import org.openecomp.mso.logger.MsoLogger;
34 * Simulates SDNC Adapter Callback response
37 public class SDNCAdapterMockTransformer extends ResponseTransformer {
39 private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL);
40 private String callbackResponse;
41 private String requestId;
43 public SDNCAdapterMockTransformer() {
44 callbackResponse = FileUtil.readResourceFile("__files/sdncSimResponse.xml");
47 public SDNCAdapterMockTransformer(String requestId) {
48 this.requestId = requestId;
51 public String name() {
52 return "sdnc-adapter-transformer";
56 * Grab the incoming request xml,extract the request id and replace the stub response with the incoming request id
57 * so that callback response can be correlated
59 * Mock Resource can be used to add dynamic properties. If sdnc_delay is not in the list by default waits for 300ms before
60 * the callback response is sent
63 public ResponseDefinition transform(Request request, ResponseDefinition responseDefinition,
64 FileSource fileSource) {
65 String requestBody = request.getBodyAsString();
67 String callbackUrl = requestBody.substring(requestBody.indexOf("<sdncadapter:CallbackUrl>")+25, requestBody.indexOf("</sdncadapter:CallbackUrl>"));
68 String requestId = requestBody.substring(requestBody.indexOf("<sdncadapter:RequestId>")+23, requestBody.indexOf("</sdncadapter:RequestId>"));
70 callbackResponse = FileUtil.readResourceFile("__files/" + responseDefinition.getBodyFileName());
71 System.out.println("callbackResponse:" + callbackResponse);
73 if (this.requestId != null) {
74 callbackResponse = callbackResponse.replace(this.requestId, requestId);
76 callbackResponse = callbackResponse.replace("testRequestId", requestId);
80 Object sdncDelay = MockResource.getMockProperties().get("sdnc_delay");
82 if (sdncDelay != null) {
83 delay = Integer.parseInt(sdncDelay.toString());
86 //Kick off callback thread
87 System.out.println("callback Url:" + callbackUrl + ":delay:" + delay);
88 CallbackResponseThread calbackResponseThread = new CallbackResponseThread(callbackUrl,callbackResponse, delay);
89 calbackResponseThread.start();
91 //return 200 OK with empty body
92 return ResponseDefinitionBuilder
93 .like(responseDefinition).but()
94 .withStatus(200).withBody("").withHeader("Content-Type", "text/xml")
99 public boolean applyGlobally() {
105 * Callback response thread which sends the callback response asynchronously
108 private class CallbackResponseThread extends Thread {
110 private String callbackUrl;
111 private String payLoad;
114 public CallbackResponseThread(String callbackUrl, String payLoad, int delay) {
115 this.callbackUrl = callbackUrl;
116 this.payLoad = payLoad;
122 //Delay sending callback response
124 } catch (InterruptedException e1) {
125 LOGGER.debug("Exception :",e1);
127 LOGGER.debug("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 LOGGER.debug("Exception :",e);