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;
23 import javax.ws.rs.core.UriBuilder;
25 import org.onap.so.client.HttpClient;
26 import org.onap.so.logger.MsoLogger;
27 import org.onap.so.utils.TargetEntity;
29 import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
30 import com.github.tomakehurst.wiremock.common.FileSource;
31 import com.github.tomakehurst.wiremock.extension.Parameters;
32 import com.github.tomakehurst.wiremock.extension.ResponseDefinitionTransformer;
33 import com.github.tomakehurst.wiremock.http.Request;
34 import com.github.tomakehurst.wiremock.http.ResponseDefinition;
37 * Please describe the VnfAdapterCreateMockTransformer.java class
40 public class VnfAdapterDeleteMockTransformer extends ResponseDefinitionTransformer {
42 private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, VnfAdapterDeleteMockTransformer.class);
44 private String notifyCallbackResponse;
45 private String ackResponse;
47 public VnfAdapterDeleteMockTransformer() {
48 notifyCallbackResponse = FileUtil.readResourceFile("__files/vnfAdapterMocks/vnfDeleteSimResponse.xml");
52 public String getName() {
53 return "vnf-adapter-delete-transformer";
57 public ResponseDefinition transform(Request request, ResponseDefinition responseDefinition,
58 FileSource fileSource, Parameters parameters) {
60 // System.err.println("notifyCallbackResponse:" + notifyCallbackResponse);
62 String requestBody = request.getBodyAsString();
64 String notficationUrl = requestBody.substring(requestBody.indexOf("<notificationUrl>")+17, requestBody.indexOf("</notificationUrl>"));
65 String messageId = requestBody.substring(requestBody.indexOf("<messageId>")+11, requestBody.indexOf("</messageId>"));
66 String responseMessageId = "";
67 String updatedResponse = "";
70 // try supplied response file (if any)
71 System.out.println(" Supplied fileName: " + responseDefinition.getBodyFileName());
72 ackResponse = FileUtil.readResourceFile("__files/" + responseDefinition.getBodyFileName());
73 notifyCallbackResponse = ackResponse;
74 responseMessageId = ackResponse.substring(ackResponse.indexOf("<messageId>")+11, ackResponse.indexOf("</messageId>"));
75 updatedResponse = ackResponse.replace(responseMessageId, messageId);
76 } catch (Exception ex) {
77 LOGGER.debug("Exception :",ex);
78 System.out.println(" ******* Use default response file in '__files/vnfAdapterMocks/vnfDeleteSimResponse.xml'");
79 responseMessageId = notifyCallbackResponse.substring(notifyCallbackResponse.indexOf("<messageId>")+11, notifyCallbackResponse.indexOf("</messageId>"));
80 updatedResponse = notifyCallbackResponse.replace(responseMessageId, messageId);
83 System.out.println("response (mock) messageId : " + responseMessageId);
84 System.out.println("request (replacement) messageId: " + messageId);
86 System.out.println("vnf Response (before):" + notifyCallbackResponse);
87 System.out.println("vnf Response (after):" + updatedResponse);
89 Object vnfDelay = MockResource.getMockProperties().get("vnf_delay");
91 if (vnfDelay != null) {
92 delay = Integer.parseInt(vnfDelay.toString());
95 //Kick off callback thread
96 System.out.println("VnfAdapterDeleteMockTransformer notficationUrl: " + notficationUrl + ":delay: " + delay);
97 CallbackResponseThread callbackResponseThread = new CallbackResponseThread(notficationUrl,updatedResponse, delay);
98 callbackResponseThread.start();
100 return ResponseDefinitionBuilder
101 .like(responseDefinition).but()
102 .withStatus(200).withBody(updatedResponse).withHeader("Content-Type", "text/xml")
108 public boolean applyGlobally() {
112 private class CallbackResponseThread extends Thread {
114 private String callbackUrl;
115 private String payLoad;
118 public CallbackResponseThread(String callbackUrl, String payLoad, int delay) {
119 this.callbackUrl = callbackUrl;
120 this.payLoad = payLoad;
124 @SuppressWarnings("deprecation")
127 //Delay sending callback response
129 } catch (InterruptedException e1) {
130 // TODO Auto-generated catch block
131 LOGGER.debug("Exception :",e1);
135 HttpClient client = new HttpClient(UriBuilder.fromUri(callbackUrl).build().toURL(), "text/xml", TargetEntity.VNF_ADAPTER);
136 client.post(payLoad);
137 } catch (Exception e) {
138 // TODO Auto-generated catch block
139 System.out.println("catch error in - request.post() ");
140 LOGGER.debug("Exception :",e);