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.logger.MsoLogger;
27 import org.onap.so.utils.TargetEntity;
29 import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
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 SDNCAdapterMockTransformer extends ResponseDefinitionTransformer {
43 private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, SDNCAdapterMockTransformer.class);
44 private String callbackResponse;
45 private String requestId;
47 public SDNCAdapterMockTransformer() {
48 callbackResponse = FileUtil.readResourceFile("__files/sdncSimResponse.xml");
51 public SDNCAdapterMockTransformer(String requestId) {
52 this.requestId = requestId;
56 public String getName() {
57 return "sdnc-adapter-transformer";
61 * Grab the incoming request xml,extract the request id and replace the stub response with the incoming request id
62 * so that callback response can be correlated
64 * Mock Resource can be used to add dynamic properties. If sdnc_delay is not in the list by default waits for 300ms before
65 * the callback response is sent
68 public ResponseDefinition transform(Request request, ResponseDefinition responseDefinition,
69 FileSource fileSource, Parameters parameters) {
70 String requestBody = request.getBodyAsString();
72 String callbackUrl = requestBody.substring(requestBody.indexOf("<sdncadapter:CallbackUrl>")+25, requestBody.indexOf("</sdncadapter:CallbackUrl>"));
73 String requestId = requestBody.substring(requestBody.indexOf("<sdncadapter:RequestId>")+23, requestBody.indexOf("</sdncadapter:RequestId>"));
75 callbackResponse = FileUtil.readResourceFile("__files/" + responseDefinition.getBodyFileName());
76 System.out.println("callbackResponse:" + callbackResponse);
78 if (this.requestId != null) {
79 callbackResponse = callbackResponse.replace(this.requestId, requestId);
81 callbackResponse = callbackResponse.replace("testRequestId", requestId);
85 Object sdncDelay = MockResource.getMockProperties().get("sdnc_delay");
87 if (sdncDelay != null) {
88 delay = Integer.parseInt(sdncDelay.toString());
91 //Kick off callback thread
92 System.out.println("callback Url:" + callbackUrl + ":delay:" + delay);
93 CallbackResponseThread calbackResponseThread = new CallbackResponseThread(callbackUrl,callbackResponse, delay);
94 calbackResponseThread.start();
96 //return 200 OK with empty body
97 return ResponseDefinitionBuilder
98 .like(responseDefinition).but()
99 .withStatus(200).withBody("").withHeader("Content-Type", "text/xml")
104 public boolean applyGlobally() {
110 * Callback response thread which sends the callback response asynchronously
113 private class CallbackResponseThread extends Thread {
115 private String callbackUrl;
116 private String payLoad;
119 public CallbackResponseThread(String callbackUrl, String payLoad, int delay) {
120 this.callbackUrl = callbackUrl;
121 this.payLoad = payLoad;
127 //Delay sending callback response
129 } catch (InterruptedException e1) {
130 LOGGER.debug("Exception :",e1);
132 LOGGER.debug("Sending callback response:" + callbackUrl);
134 HttpClient client = new HttpClient(UriBuilder.fromUri(callbackUrl).build().toURL(), "text/xml", TargetEntity.SDNC_ADAPTER);
135 client.post(payLoad);
136 } catch (Exception e) {
137 LOGGER.debug("Exception :",e);