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 * ================================================================================
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 * ============LICENSE_END=========================================================
24 package org.openecomp.mso.bpmn.mock;
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 VnfAdapterCreateMockTransformer.java class
39 public class VnfAdapterDeleteMockTransformer extends ResponseTransformer {
42 private String notifyCallbackResponse;
43 private String ackResponse;
45 public VnfAdapterDeleteMockTransformer() {
46 notifyCallbackResponse = FileUtil.readResourceFile("__files/vnfAdapterMocks/vnfDeleteSimResponse.xml");
50 public String name() {
51 return "vnf-adapter-delete-transformer";
55 public ResponseDefinition transform(Request request, ResponseDefinition responseDefinition,
56 FileSource fileSource) {
58 // System.err.println("notifyCallbackResponse:" + notifyCallbackResponse);
60 String requestBody = request.getBodyAsString();
62 String notficationUrl = requestBody.substring(requestBody.indexOf("<notificationUrl>")+17, requestBody.indexOf("</notificationUrl>"));
63 String messageId = requestBody.substring(requestBody.indexOf("<messageId>")+11, requestBody.indexOf("</messageId>"));
64 String responseMessageId = "";
65 String updatedResponse = "";
68 // try supplied response file (if any)
69 System.out.println(" Supplied fileName: " + responseDefinition.getBodyFileName());
70 ackResponse = FileUtil.readResourceFile("__files/" + responseDefinition.getBodyFileName());
71 notifyCallbackResponse = ackResponse;
72 responseMessageId = ackResponse.substring(ackResponse.indexOf("<messageId>")+11, ackResponse.indexOf("</messageId>"));
73 updatedResponse = ackResponse.replace(responseMessageId, messageId);
74 } catch (Exception ex) {
75 System.out.println(" ******* Use default response file in '__files/vnfAdapterMocks/vnfDeleteSimResponse.xml'");
76 responseMessageId = notifyCallbackResponse.substring(notifyCallbackResponse.indexOf("<messageId>")+11, notifyCallbackResponse.indexOf("</messageId>"));
77 updatedResponse = notifyCallbackResponse.replace(responseMessageId, messageId);
80 System.out.println("response (mock) messageId : " + responseMessageId);
81 System.out.println("request (replacement) messageId: " + messageId);
83 System.out.println("vnf Response (before):" + notifyCallbackResponse);
84 System.out.println("vnf Response (after):" + updatedResponse);
86 Object vnfDelay = MockResource.getMockProperties().get("vnf_delay");
88 if (vnfDelay != null) {
89 delay = Integer.parseInt(vnfDelay.toString());
92 //Kick off callback thread
93 System.out.println("VnfAdapterDeleteMockTransformer notficationUrl: " + notficationUrl + ":delay: " + delay);
94 CallbackResponseThread callbackResponseThread = new CallbackResponseThread(notficationUrl,updatedResponse, delay);
95 callbackResponseThread.start();
97 return ResponseDefinitionBuilder
98 .like(responseDefinition).but()
99 .withStatus(200).withBody(updatedResponse).withHeader("Content-Type", "text/xml")
105 public boolean applyGlobally() {
109 private class CallbackResponseThread extends Thread {
111 private String callbackUrl;
112 private String payLoad;
115 public CallbackResponseThread(String callbackUrl, String payLoad, int delay) {
116 this.callbackUrl = callbackUrl;
117 this.payLoad = payLoad;
121 @SuppressWarnings("deprecation")
124 //Delay sending callback response
126 } catch (InterruptedException e1) {
127 // TODO Auto-generated catch block
128 e1.printStackTrace();
130 System.out.println("Sending callback response to url: " + callbackUrl);
131 ClientRequest request = new ClientRequest(callbackUrl);
132 request.body("text/xml", payLoad);
133 //System.err.println(payLoad);
135 ClientResponse result = request.post();
136 System.out.println("Successfully posted callback? Status: " + result.getStatus());
137 //System.err.println("Successfully posted callback:" + result.getStatus());
138 } catch (Exception e) {
139 // TODO Auto-generated catch block
140 System.out.println("catch error in - request.post() ");