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;
25 import com.github.tomakehurst.wiremock.extension.ResponseTransformer;
27 import org.jboss.resteasy.client.ClientRequest;
28 import org.jboss.resteasy.client.ClientResponse;
29 import org.openecomp.mso.logger.MsoLogger;
31 import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
32 import com.github.tomakehurst.wiremock.common.FileSource;
33 import com.github.tomakehurst.wiremock.extension.ResponseTransformer;
34 import com.github.tomakehurst.wiremock.http.Request;
35 import com.github.tomakehurst.wiremock.http.ResponseDefinition;
37 import org.openecomp.mso.logger.MsoLogger;
39 * Please describe the VnfAdapterQueryMockTransformer.java class
44 public class VnfAdapterQueryMockTransformer extends ResponseTransformer{
46 private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL);
48 private String notifyCallbackResponse;
49 private String ackResponse;
50 private String messageId;
52 public VnfAdapterQueryMockTransformer() {
53 notifyCallbackResponse = FileUtil.readResourceFile("__files/vnfAdapterMocks/vnfQuerySimResponse.xml");
56 public VnfAdapterQueryMockTransformer(String messageId) {
57 this.messageId = messageId;
61 public String name() {
62 return "vnf-adapter-query-transformer";
66 public ResponseDefinition transform(Request request, ResponseDefinition responseDefinition,
67 FileSource fileSource) {
69 String requestBody = request.getBodyAsString();
71 String notficationUrl = requestBody.substring(requestBody.indexOf("<notificationUrl>")+17, requestBody.indexOf("</notificationUrl>"));
72 String messageId = requestBody.substring(requestBody.indexOf("<messageId>")+11, requestBody.indexOf("</messageId>"));
73 // String updatedResponse = notifyCallbackResponse.replace("b1a82ce6-7f5c-45fd-9273-acaf88fc2137", messageId);
75 String responseMessageId = "";
76 String updatedResponse = "";
78 // if (ackResponse == null) {
79 //System.err.println("file:" + responseDefinition.getBodyFileName());
80 // ackResponse = FileUtil.readResourceFile("__files/" + responseDefinition.getBodyFileName());
85 // try supplied response file (if any)
86 System.out.println(" Supplied fileName: " + responseDefinition.getBodyFileName());
87 ackResponse = FileUtil.readResourceFile("__files/" + responseDefinition.getBodyFileName());
88 notifyCallbackResponse = ackResponse;
89 responseMessageId = ackResponse.substring(ackResponse.indexOf("<messageId>")+11, ackResponse.indexOf("</messageId>"));
90 updatedResponse = ackResponse.replace(responseMessageId, messageId);
91 } catch (Exception ex) {
92 LOGGER.debug("Exception :",ex);
93 System.out.println(" ******* Use default response file in '__files/vnfAdapterMocks/vnfQuerySimResponse.xml'");
94 responseMessageId = notifyCallbackResponse.substring(notifyCallbackResponse.indexOf("<messageId>")+11, notifyCallbackResponse.indexOf("</messageId>"));
95 updatedResponse = notifyCallbackResponse.replace(responseMessageId, messageId);
98 System.out.println("response (mock) messageId : " + responseMessageId);
99 System.out.println("request (replacement) messageId: " + messageId);
101 System.out.println("vnf Response (before):" + notifyCallbackResponse);
102 System.out.println("vnf Response (after):" + updatedResponse);
105 Object vnfDelay = MockResource.getMockProperties().get("vnf_delay");
107 if (vnfDelay != null) {
108 delay = Integer.parseInt(vnfDelay.toString());
111 //Kick off callback thread
113 //System.out.println("notficationUrl" + notficationUrl);
114 //System.out.println("updatedResponse" + updatedResponse);
115 System.out.println("VnfAdapterQueryMockTransformer notficationUrl: " + notficationUrl + ":delay: " + delay);
116 CallbackResponseThread callbackResponseThread = new CallbackResponseThread(notficationUrl,updatedResponse, delay);
117 System.out.println("Inside Callback" );
118 callbackResponseThread.start();
120 return ResponseDefinitionBuilder
121 .like(responseDefinition).but()
122 .withStatus(200).withBody(updatedResponse).withHeader("Content-Type", "text/xml")
127 public boolean applyGlobally() {
131 private class CallbackResponseThread extends Thread {
133 private String callbackUrl;
134 private String payLoad;
137 public CallbackResponseThread(String callbackUrl, String payLoad, int delay) {
138 this.callbackUrl = callbackUrl;
139 this.payLoad = payLoad;
145 //Delay sending callback response
147 } catch (InterruptedException e1) {
148 LOGGER.debug("Exception :",e1);
150 ClientRequest request = new ClientRequest(callbackUrl);
151 request.body("text/xml", payLoad);
152 //System.err.println(payLoad);
154 ClientResponse result = request.post();
155 //System.err.println("Successfully posted callback:" + result.getStatus());
156 } catch (Exception e) {
157 LOGGER.debug("Exception :",e);