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.onap.so.bpmn.mock;
24 import javax.ws.rs.core.UriBuilder;
26 import org.onap.so.client.HttpClient;
27 import org.onap.so.logger.MsoLogger;
28 import org.onap.so.utils.TargetEntity;
30 import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
31 import com.github.tomakehurst.wiremock.common.FileSource;
32 import com.github.tomakehurst.wiremock.extension.Parameters;
33 import com.github.tomakehurst.wiremock.extension.ResponseDefinitionTransformer;
34 import com.github.tomakehurst.wiremock.http.Request;
35 import com.github.tomakehurst.wiremock.http.ResponseDefinition;
38 * Please describe the VnfAdapterQueryMockTransformer.java class
43 public class VnfAdapterQueryMockTransformer extends ResponseDefinitionTransformer{
45 private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, VnfAdapterQueryMockTransformer.class);
47 private String notifyCallbackResponse;
48 private String ackResponse;
49 private String messageId;
51 public VnfAdapterQueryMockTransformer() {
52 notifyCallbackResponse = FileUtil.readResourceFile("__files/vnfAdapterMocks/vnfQuerySimResponse.xml");
55 public VnfAdapterQueryMockTransformer(String messageId) {
56 this.messageId = messageId;
60 public String getName() {
61 return "vnf-adapter-query-transformer";
65 public ResponseDefinition transform(Request request, ResponseDefinition responseDefinition,
66 FileSource fileSource, Parameters parameters) {
68 String requestBody = request.getBodyAsString();
70 String notficationUrl = requestBody.substring(requestBody.indexOf("<notificationUrl>")+17, requestBody.indexOf("</notificationUrl>"));
71 String messageId = requestBody.substring(requestBody.indexOf("<messageId>")+11, requestBody.indexOf("</messageId>"));
72 // String updatedResponse = notifyCallbackResponse.replace("b1a82ce6-7f5c-45fd-9273-acaf88fc2137", messageId);
74 String responseMessageId = "";
75 String updatedResponse = "";
77 // if (ackResponse == null) {
78 //System.err.println("file:" + responseDefinition.getBodyFileName());
79 // ackResponse = FileUtil.readResourceFile("__files/" + responseDefinition.getBodyFileName());
84 // try supplied response file (if any)
85 System.out.println(" Supplied fileName: " + responseDefinition.getBodyFileName());
86 ackResponse = FileUtil.readResourceFile("__files/" + responseDefinition.getBodyFileName());
87 notifyCallbackResponse = ackResponse;
88 responseMessageId = ackResponse.substring(ackResponse.indexOf("<messageId>")+11, ackResponse.indexOf("</messageId>"));
89 updatedResponse = ackResponse.replace(responseMessageId, messageId);
90 } catch (Exception ex) {
91 LOGGER.debug("Exception :",ex);
92 System.out.println(" ******* Use default response file in '__files/vnfAdapterMocks/vnfQuerySimResponse.xml'");
93 responseMessageId = notifyCallbackResponse.substring(notifyCallbackResponse.indexOf("<messageId>")+11, notifyCallbackResponse.indexOf("</messageId>"));
94 updatedResponse = notifyCallbackResponse.replace(responseMessageId, messageId);
97 System.out.println("response (mock) messageId : " + responseMessageId);
98 System.out.println("request (replacement) messageId: " + messageId);
100 System.out.println("vnf Response (before):" + notifyCallbackResponse);
101 System.out.println("vnf Response (after):" + updatedResponse);
104 Object vnfDelay = MockResource.getMockProperties().get("vnf_delay");
106 if (vnfDelay != null) {
107 delay = Integer.parseInt(vnfDelay.toString());
110 //Kick off callback thread
112 //System.out.println("notficationUrl" + notficationUrl);
113 //System.out.println("updatedResponse" + updatedResponse);
114 System.out.println("VnfAdapterQueryMockTransformer notficationUrl: " + notficationUrl + ":delay: " + delay);
115 CallbackResponseThread callbackResponseThread = new CallbackResponseThread(notficationUrl,updatedResponse, delay);
116 System.out.println("Inside Callback" );
117 callbackResponseThread.start();
119 return ResponseDefinitionBuilder
120 .like(responseDefinition).but()
121 .withStatus(200).withBody(updatedResponse).withHeader("Content-Type", "text/xml")
126 public boolean applyGlobally() {
130 private class CallbackResponseThread extends Thread {
132 private String callbackUrl;
133 private String payLoad;
136 public CallbackResponseThread(String callbackUrl, String payLoad, int delay) {
137 this.callbackUrl = callbackUrl;
138 this.payLoad = payLoad;
144 //Delay sending callback response
146 } catch (InterruptedException e1) {
147 LOGGER.debug("Exception :",e1);
151 HttpClient client = new HttpClient(UriBuilder.fromUri(callbackUrl).build().toURL(), "text/xml", TargetEntity.VNF_ADAPTER);
152 client.post(payLoad);
153 } catch (Exception e) {
154 LOGGER.debug("Exception :",e);