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 VnfAdapterRollbackMockTransformer extends ResponseTransformer {
41 private String notifyCallbackResponse;
42 private String ackResponse;
43 private String messageId;
45 public VnfAdapterRollbackMockTransformer() {
46 notifyCallbackResponse = FileUtil.readResourceFile("__files/vnfAdapterMocks/vnfRollbackSimResponse.xml");
49 public VnfAdapterRollbackMockTransformer(String messageId) {
50 this.messageId = messageId;
54 public String name() {
55 return "vnf-adapter-rollback-transformer";
59 public ResponseDefinition transform(Request request, ResponseDefinition responseDefinition,
60 FileSource fileSource) {
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 System.out.println(" ******* Use default response file in '__files/vnfAdapterMocks/vnfRollbackSimResponse.xml'");
78 responseMessageId = notifyCallbackResponse.substring(notifyCallbackResponse.indexOf("<messageId>")+11, notifyCallbackResponse.indexOf("</messageId>"));
79 updatedResponse = notifyCallbackResponse.replace(responseMessageId, messageId);
82 System.out.println("response (mock) messageId : " + responseMessageId);
83 System.out.println("request (replacement) messageId: " + messageId);
85 System.out.println("vnf Response (before):" + notifyCallbackResponse);
86 System.out.println("vnf Response (after):" + updatedResponse);
88 Object vnfDelay = MockResource.getMockProperties().get("vnf_delay");
90 if (vnfDelay != null) {
91 delay = Integer.parseInt(vnfDelay.toString());
94 //Kick off callback thread
95 System.out.println("VnfAdapterRollbackMockTransformer notficationUrl: " + notficationUrl + ":delay: " + delay);
96 CallbackResponseThread callbackResponseThread = new CallbackResponseThread(notficationUrl,updatedResponse, delay);
97 callbackResponseThread.start();
99 return ResponseDefinitionBuilder
100 .like(responseDefinition).but()
101 .withStatus(200).withBody(updatedResponse).withHeader("Content-Type", "text/xml")
107 public boolean applyGlobally() {
111 private class CallbackResponseThread extends Thread {
113 private String callbackUrl;
114 private String payLoad;
117 public CallbackResponseThread(String callbackUrl, String payLoad, int delay) {
118 this.callbackUrl = callbackUrl;
119 this.payLoad = payLoad;
125 //Delay sending callback response
127 } catch (InterruptedException e1) {
128 // TODO Auto-generated catch block
129 e1.printStackTrace();
131 System.out.println("Sending callback response to url: " + callbackUrl);
132 ClientRequest request = new ClientRequest(callbackUrl);
133 request.body("text/xml", payLoad);
134 //System.err.println(payLoad);
136 ClientResponse result = request.post();
137 System.out.println("Successfully posted callback? Status: " + result.getStatus());
138 //System.err.println("Successfully posted callback:" + result.getStatus());
139 } catch (Exception e) {
140 // TODO Auto-generated catch block
141 System.out.println("catch error in - request.post() ");