2 * © 2014 AT&T Intellectual Property. All rights reserved. Used under license from AT&T Intellectual Property.
5 * ============LICENSE_START=======================================================
7 * ================================================================================
8 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
9 * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
10 * ================================================================================
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
22 * ============LICENSE_END=========================================================
25 package org.openecomp.mso.bpmn.mock;
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 VnfAdapterUpdateMockTransformer.java class
42 public class VnfAdapterUpdateMockTransformer extends ResponseTransformer {
44 private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL);
46 private String notifyCallbackResponse;
47 private String requestId;
48 private String ackResponse;
50 public VnfAdapterUpdateMockTransformer() {
51 notifyCallbackResponse = FileUtil.readResourceFile("vnfAdapter/vnfUpdateSimResponse.xml");
54 public VnfAdapterUpdateMockTransformer(String requestId) {
55 this.requestId = requestId;
59 public String name() {
60 return "vnf-adapter-update-transformer";
64 public ResponseDefinition transform(Request request, ResponseDefinition responseDefinition,
65 FileSource fileSource) {
67 String requestBody = request.getBodyAsString();
69 String notficationUrl = requestBody.substring(requestBody.indexOf("<notificationUrl>")+17, requestBody.indexOf("</notificationUrl>"));
70 String messageId = requestBody.substring(requestBody.indexOf("<messageId>")+11, requestBody.indexOf("</messageId>"));
71 String responseMessageId = "";
72 String updatedResponse = "";
75 // try supplied response file (if any)
76 System.out.println(" Supplied fileName: " + responseDefinition.getBodyFileName());
77 ackResponse = FileUtil.readResourceFile("__files/" + responseDefinition.getBodyFileName());
78 notifyCallbackResponse = ackResponse;
79 responseMessageId = ackResponse.substring(ackResponse.indexOf("<messageId>")+11, ackResponse.indexOf("</messageId>"));
80 updatedResponse = ackResponse.replace(responseMessageId, messageId);
81 } catch (Exception ex) {
82 LOGGER.debug("Exception :",ex);
83 System.out.println(" ******* Use default response file in 'vnfAdapter/vnfUpdateSimResponse.xml'");
84 responseMessageId = notifyCallbackResponse.substring(notifyCallbackResponse.indexOf("<messageId>")+11, notifyCallbackResponse.indexOf("</messageId>"));
85 updatedResponse = notifyCallbackResponse.replace(responseMessageId, messageId);
88 System.out.println("response (mock) messageId : " + responseMessageId);
89 System.out.println("request (replacement) messageId: " + messageId);
91 System.out.println("vnf Response (before):" + notifyCallbackResponse);
92 System.out.println("vnf Response (after):" + updatedResponse);
94 Object vnfDelay = MockResource.getMockProperties().get("vnf_delay");
96 if (vnfDelay != null) {
97 delay = Integer.parseInt(vnfDelay.toString());
100 //Kick off callback thread
101 System.out.println("VnfAdapterUpdateMockTransformer notficationUrl: " + notficationUrl + ":delay: " + delay);
102 CallbackResponseThread callbackResponseThread = new CallbackResponseThread(notficationUrl,updatedResponse, delay);
103 callbackResponseThread.start();
105 return ResponseDefinitionBuilder
106 .like(responseDefinition).but()
107 .withStatus(200).withBody(updatedResponse).withHeader("Content-Type", "text/xml")
113 public boolean applyGlobally() {
117 private class CallbackResponseThread extends Thread {
119 private String callbackUrl;
120 private String payLoad;
123 public CallbackResponseThread(String callbackUrl, String payLoad, int delay) {
124 this.callbackUrl = callbackUrl;
125 this.payLoad = payLoad;
131 //Delay sending callback response
133 } catch (InterruptedException e1) {
134 LOGGER.debug("Exception :", e1);
136 System.out.println("Sending callback response to url: " + callbackUrl);
137 ClientRequest request = new ClientRequest(callbackUrl);
138 request.body("text/xml", payLoad);
139 //System.err.println(payLoad);
141 ClientResponse result = request.post();
142 System.out.println("Successfully posted callback? Status: " + result.getStatus());
143 //System.err.println("Successfully posted callback:" + result.getStatus());
144 } catch (Exception e) {
145 System.out.println("catch error in - request.post() ");
146 LOGGER.debug("Exception :",e);