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;
36 * Please describe the VnfAdapterUpdateMockTransformer.java class
39 public class VnfAdapterUpdateMockTransformer extends ResponseTransformer {
41 private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL);
43 private String notifyCallbackResponse;
44 private String requestId;
45 private String ackResponse;
47 public VnfAdapterUpdateMockTransformer() {
48 notifyCallbackResponse = FileUtil.readResourceFile("vnfAdapter/vnfUpdateSimResponse.xml");
51 public VnfAdapterUpdateMockTransformer(String requestId) {
52 this.requestId = requestId;
56 public String name() {
57 return "vnf-adapter-update-transformer";
61 public ResponseDefinition transform(Request request, ResponseDefinition responseDefinition,
62 FileSource fileSource) {
64 String requestBody = request.getBodyAsString();
66 String notficationUrl = requestBody.substring(requestBody.indexOf("<notificationUrl>")+17, requestBody.indexOf("</notificationUrl>"));
67 String messageId = requestBody.substring(requestBody.indexOf("<messageId>")+11, requestBody.indexOf("</messageId>"));
68 String responseMessageId = "";
69 String updatedResponse = "";
72 // try supplied response file (if any)
73 System.out.println(" Supplied fileName: " + responseDefinition.getBodyFileName());
74 ackResponse = FileUtil.readResourceFile("__files/" + responseDefinition.getBodyFileName());
75 notifyCallbackResponse = ackResponse;
76 responseMessageId = ackResponse.substring(ackResponse.indexOf("<messageId>")+11, ackResponse.indexOf("</messageId>"));
77 updatedResponse = ackResponse.replace(responseMessageId, messageId);
78 } catch (Exception ex) {
79 LOGGER.debug("Exception :",ex);
80 System.out.println(" ******* Use default response file in 'vnfAdapter/vnfUpdateSimResponse.xml'");
81 responseMessageId = notifyCallbackResponse.substring(notifyCallbackResponse.indexOf("<messageId>")+11, notifyCallbackResponse.indexOf("</messageId>"));
82 updatedResponse = notifyCallbackResponse.replace(responseMessageId, messageId);
85 System.out.println("response (mock) messageId : " + responseMessageId);
86 System.out.println("request (replacement) messageId: " + messageId);
88 System.out.println("vnf Response (before):" + notifyCallbackResponse);
89 System.out.println("vnf Response (after):" + updatedResponse);
91 Object vnfDelay = MockResource.getMockProperties().get("vnf_delay");
93 if (vnfDelay != null) {
94 delay = Integer.parseInt(vnfDelay.toString());
97 //Kick off callback thread
98 System.out.println("VnfAdapterUpdateMockTransformer notficationUrl: " + notficationUrl + ":delay: " + delay);
99 CallbackResponseThread callbackResponseThread = new CallbackResponseThread(notficationUrl,updatedResponse, delay);
100 callbackResponseThread.start();
102 return ResponseDefinitionBuilder
103 .like(responseDefinition).but()
104 .withStatus(200).withBody(updatedResponse).withHeader("Content-Type", "text/xml")
110 public boolean applyGlobally() {
114 private class CallbackResponseThread extends Thread {
116 private String callbackUrl;
117 private String payLoad;
120 public CallbackResponseThread(String callbackUrl, String payLoad, int delay) {
121 this.callbackUrl = callbackUrl;
122 this.payLoad = payLoad;
128 //Delay sending callback response
130 } catch (InterruptedException e1) {
131 LOGGER.debug("Exception :", e1);
133 System.out.println("Sending callback response to url: " + callbackUrl);
134 ClientRequest request = new ClientRequest(callbackUrl);
135 request.body("text/xml", payLoad);
136 //System.err.println(payLoad);
138 ClientResponse result = request.post();
139 System.out.println("Successfully posted callback? Status: " + result.getStatus());
140 //System.err.println("Successfully posted callback:" + result.getStatus());
141 } catch (Exception e) {
142 System.out.println("catch error in - request.post() ");
143 LOGGER.debug("Exception :",e);