2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.mso.bpmn.mock;
24 import com.github.tomakehurst.wiremock.extension.ResponseTransformer;
26 import org.jboss.resteasy.client.ClientRequest;
27 import org.jboss.resteasy.client.ClientResponse;
29 import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
30 import com.github.tomakehurst.wiremock.common.FileSource;
31 import com.github.tomakehurst.wiremock.extension.ResponseTransformer;
32 import com.github.tomakehurst.wiremock.http.Request;
33 import com.github.tomakehurst.wiremock.http.ResponseDefinition;
36 * Please describe the VnfAdapterQueryMockTransformer.java class
41 public class VnfAdapterQueryMockTransformer extends ResponseTransformer{
43 private String notifyCallbackResponse;
44 private String ackResponse;
45 private String messageId;
47 public VnfAdapterQueryMockTransformer() {
48 notifyCallbackResponse = FileUtil.readResourceFile("__files/vnfAdapterMocks/vnfQuerySimResponse.xml");
51 public VnfAdapterQueryMockTransformer(String messageId) {
52 this.messageId = messageId;
56 public String name() {
57 return "vnf-adapter-query-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 updatedResponse = notifyCallbackResponse.replace("b1a82ce6-7f5c-45fd-9273-acaf88fc2137", messageId);
70 String responseMessageId = "";
71 String updatedResponse = "";
73 // if (ackResponse == null) {
74 //System.err.println("file:" + responseDefinition.getBodyFileName());
75 // ackResponse = FileUtil.readResourceFile("__files/" + responseDefinition.getBodyFileName());
80 // try supplied response file (if any)
81 System.out.println(" Supplied fileName: " + responseDefinition.getBodyFileName());
82 ackResponse = FileUtil.readResourceFile("__files/" + responseDefinition.getBodyFileName());
83 notifyCallbackResponse = ackResponse;
84 responseMessageId = ackResponse.substring(ackResponse.indexOf("<messageId>")+11, ackResponse.indexOf("</messageId>"));
85 updatedResponse = ackResponse.replace(responseMessageId, messageId);
86 } catch (Exception ex) {
87 System.out.println(" ******* Use default response file in '__files/vnfAdapterMocks/vnfQuerySimResponse.xml'");
88 responseMessageId = notifyCallbackResponse.substring(notifyCallbackResponse.indexOf("<messageId>")+11, notifyCallbackResponse.indexOf("</messageId>"));
89 updatedResponse = notifyCallbackResponse.replace(responseMessageId, messageId);
92 System.out.println("response (mock) messageId : " + responseMessageId);
93 System.out.println("request (replacement) messageId: " + messageId);
95 System.out.println("vnf Response (before):" + notifyCallbackResponse);
96 System.out.println("vnf Response (after):" + updatedResponse);
99 Object vnfDelay = MockResource.getMockProperties().get("vnf_delay");
101 if (vnfDelay != null) {
102 delay = Integer.parseInt(vnfDelay.toString());
105 //Kick off callback thread
107 //System.out.println("notficationUrl" + notficationUrl);
108 //System.out.println("updatedResponse" + updatedResponse);
109 System.out.println("VnfAdapterQueryMockTransformer notficationUrl: " + notficationUrl + ":delay: " + delay);
110 CallbackResponseThread callbackResponseThread = new CallbackResponseThread(notficationUrl,updatedResponse, delay);
111 System.out.println("Inside Callback" );
112 callbackResponseThread.start();
114 return ResponseDefinitionBuilder
115 .like(responseDefinition).but()
116 .withStatus(200).withBody(updatedResponse).withHeader("Content-Type", "text/xml")
121 public boolean applyGlobally() {
125 private class CallbackResponseThread extends Thread {
127 private String callbackUrl;
128 private String payLoad;
131 public CallbackResponseThread(String callbackUrl, String payLoad, int delay) {
132 this.callbackUrl = callbackUrl;
133 this.payLoad = payLoad;
139 //Delay sending callback response
141 } catch (InterruptedException e1) {
142 // TODO Auto-generated catch block
143 e1.printStackTrace();
145 ClientRequest request = new ClientRequest(callbackUrl);
146 request.body("text/xml", payLoad);
147 //System.err.println(payLoad);
149 ClientResponse result = request.post();
150 //System.err.println("Successfully posted callback:" + result.getStatus());
151 } catch (Exception e) {
152 // TODO Auto-generated catch block