2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 * ============LICENSE_END=========================================================
19 package org.openecomp.mso.bpmn.mock;
22 import com.github.tomakehurst.wiremock.extension.ResponseTransformer;
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 VnfAdapterQueryMockTransformer.java class
41 public class VnfAdapterQueryMockTransformer extends ResponseTransformer{
43 private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL);
45 private String notifyCallbackResponse;
46 private String ackResponse;
47 private String messageId;
49 public VnfAdapterQueryMockTransformer() {
50 notifyCallbackResponse = FileUtil.readResourceFile("__files/vnfAdapterMocks/vnfQuerySimResponse.xml");
53 public VnfAdapterQueryMockTransformer(String messageId) {
54 this.messageId = messageId;
58 public String name() {
59 return "vnf-adapter-query-transformer";
63 public ResponseDefinition transform(Request request, ResponseDefinition responseDefinition,
64 FileSource fileSource) {
66 String requestBody = request.getBodyAsString();
68 String notficationUrl = requestBody.substring(requestBody.indexOf("<notificationUrl>")+17, requestBody.indexOf("</notificationUrl>"));
69 String messageId = requestBody.substring(requestBody.indexOf("<messageId>")+11, requestBody.indexOf("</messageId>"));
70 // String updatedResponse = notifyCallbackResponse.replace("b1a82ce6-7f5c-45fd-9273-acaf88fc2137", messageId);
72 String responseMessageId = "";
73 String updatedResponse = "";
75 // if (ackResponse == null) {
76 //System.err.println("file:" + responseDefinition.getBodyFileName());
77 // ackResponse = FileUtil.readResourceFile("__files/" + responseDefinition.getBodyFileName());
82 // try supplied response file (if any)
83 System.out.println(" Supplied fileName: " + responseDefinition.getBodyFileName());
84 ackResponse = FileUtil.readResourceFile("__files/" + responseDefinition.getBodyFileName());
85 notifyCallbackResponse = ackResponse;
86 responseMessageId = ackResponse.substring(ackResponse.indexOf("<messageId>")+11, ackResponse.indexOf("</messageId>"));
87 updatedResponse = ackResponse.replace(responseMessageId, messageId);
88 } catch (Exception ex) {
89 LOGGER.debug("Exception :",ex);
90 System.out.println(" ******* Use default response file in '__files/vnfAdapterMocks/vnfQuerySimResponse.xml'");
91 responseMessageId = notifyCallbackResponse.substring(notifyCallbackResponse.indexOf("<messageId>")+11, notifyCallbackResponse.indexOf("</messageId>"));
92 updatedResponse = notifyCallbackResponse.replace(responseMessageId, messageId);
95 System.out.println("response (mock) messageId : " + responseMessageId);
96 System.out.println("request (replacement) messageId: " + messageId);
98 System.out.println("vnf Response (before):" + notifyCallbackResponse);
99 System.out.println("vnf Response (after):" + updatedResponse);
102 Object vnfDelay = MockResource.getMockProperties().get("vnf_delay");
104 if (vnfDelay != null) {
105 delay = Integer.parseInt(vnfDelay.toString());
108 //Kick off callback thread
110 //System.out.println("notficationUrl" + notficationUrl);
111 //System.out.println("updatedResponse" + updatedResponse);
112 System.out.println("VnfAdapterQueryMockTransformer notficationUrl: " + notficationUrl + ":delay: " + delay);
113 CallbackResponseThread callbackResponseThread = new CallbackResponseThread(notficationUrl,updatedResponse, delay);
114 System.out.println("Inside Callback" );
115 callbackResponseThread.start();
117 return ResponseDefinitionBuilder
118 .like(responseDefinition).but()
119 .withStatus(200).withBody(updatedResponse).withHeader("Content-Type", "text/xml")
124 public boolean applyGlobally() {
128 private class CallbackResponseThread extends Thread {
130 private String callbackUrl;
131 private String payLoad;
134 public CallbackResponseThread(String callbackUrl, String payLoad, int delay) {
135 this.callbackUrl = callbackUrl;
136 this.payLoad = payLoad;
142 //Delay sending callback response
144 } catch (InterruptedException e1) {
145 LOGGER.debug("Exception :",e1);
147 ClientRequest request = new ClientRequest(callbackUrl);
148 request.body("text/xml", payLoad);
149 //System.err.println(payLoad);
151 ClientResponse result = request.post();
152 //System.err.println("Successfully posted callback:" + result.getStatus());
153 } catch (Exception e) {
154 LOGGER.debug("Exception :",e);