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 javax.xml.ws.Endpoint;
29 import org.jboss.resteasy.client.ClientRequest;
30 import org.jboss.resteasy.client.ClientResponse;
31 import org.openecomp.mso.logger.MsoLogger;
33 import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
34 import com.github.tomakehurst.wiremock.common.FileSource;
35 import com.github.tomakehurst.wiremock.extension.ResponseTransformer;
36 import com.github.tomakehurst.wiremock.http.Request;
37 import com.github.tomakehurst.wiremock.http.ResponseDefinition;
39 import org.openecomp.mso.logger.MsoLogger;
41 * Please describe the VnfAdapterCreateMockTransformer.java class
44 public class VnfAdapterCreateMockTransformer extends ResponseTransformer {
46 private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL);
48 private String notifyCallbackResponse;
49 private String ackResponse;
51 public VnfAdapterCreateMockTransformer() {
52 notifyCallbackResponse = FileUtil.readResourceFile("__files/vnfAdapterMocks/vnfCreateSimResponse.xml"); // default response
56 public String name() {
57 return "vnf-adapter-create-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 '__files/vnfAdapterMocks/vnfCreateSimResponse.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("VnfAdapterCreateMockTransformer 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;
126 @SuppressWarnings("deprecation")
129 //Delay sending callback response
131 } catch (InterruptedException e1) {
132 // TODO Auto-generated catch block
133 LOGGER.debug("Exception :",e1);
135 System.out.println("Sending callback response to url: " + callbackUrl);
136 ClientRequest request = new ClientRequest(callbackUrl);
137 request.body("text/xml", payLoad);
138 //System.out.println("payLoad: " + 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 // TODO Auto-generated catch block
146 System.out.println("catch error in - request.post() ");
147 LOGGER.debug("Exception :",e);