2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 * ============LICENSE_END=========================================================
19 package org.openecomp.mso.bpmn.mock;
21 import org.jboss.resteasy.client.ClientRequest;
22 import org.jboss.resteasy.client.ClientResponse;
23 import org.openecomp.mso.logger.MsoLogger;
25 import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
26 import com.github.tomakehurst.wiremock.common.FileSource;
27 import com.github.tomakehurst.wiremock.extension.ResponseTransformer;
28 import com.github.tomakehurst.wiremock.http.Request;
29 import com.github.tomakehurst.wiremock.http.ResponseDefinition;
31 * Please describe the VnfAdapterCreateMockTransformer.java class
34 public class VnfAdapterDeleteMockTransformer extends ResponseTransformer {
36 private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL);
38 private String notifyCallbackResponse;
39 private String ackResponse;
41 public VnfAdapterDeleteMockTransformer() {
42 notifyCallbackResponse = FileUtil.readResourceFile("__files/vnfAdapterMocks/vnfDeleteSimResponse.xml");
46 public String name() {
47 return "vnf-adapter-delete-transformer";
51 public ResponseDefinition transform(Request request, ResponseDefinition responseDefinition,
52 FileSource fileSource) {
54 // System.err.println("notifyCallbackResponse:" + notifyCallbackResponse);
56 String requestBody = request.getBodyAsString();
58 String notficationUrl = requestBody.substring(requestBody.indexOf("<notificationUrl>")+17, requestBody.indexOf("</notificationUrl>"));
59 String messageId = requestBody.substring(requestBody.indexOf("<messageId>")+11, requestBody.indexOf("</messageId>"));
60 String responseMessageId = "";
61 String updatedResponse = "";
64 // try supplied response file (if any)
65 System.out.println(" Supplied fileName: " + responseDefinition.getBodyFileName());
66 ackResponse = FileUtil.readResourceFile("__files/" + responseDefinition.getBodyFileName());
67 notifyCallbackResponse = ackResponse;
68 responseMessageId = ackResponse.substring(ackResponse.indexOf("<messageId>")+11, ackResponse.indexOf("</messageId>"));
69 updatedResponse = ackResponse.replace(responseMessageId, messageId);
70 } catch (Exception ex) {
71 LOGGER.debug("Exception :",ex);
72 System.out.println(" ******* Use default response file in '__files/vnfAdapterMocks/vnfDeleteSimResponse.xml'");
73 responseMessageId = notifyCallbackResponse.substring(notifyCallbackResponse.indexOf("<messageId>")+11, notifyCallbackResponse.indexOf("</messageId>"));
74 updatedResponse = notifyCallbackResponse.replace(responseMessageId, messageId);
77 System.out.println("response (mock) messageId : " + responseMessageId);
78 System.out.println("request (replacement) messageId: " + messageId);
80 System.out.println("vnf Response (before):" + notifyCallbackResponse);
81 System.out.println("vnf Response (after):" + updatedResponse);
83 Object vnfDelay = MockResource.getMockProperties().get("vnf_delay");
85 if (vnfDelay != null) {
86 delay = Integer.parseInt(vnfDelay.toString());
89 //Kick off callback thread
90 System.out.println("VnfAdapterDeleteMockTransformer notficationUrl: " + notficationUrl + ":delay: " + delay);
91 CallbackResponseThread callbackResponseThread = new CallbackResponseThread(notficationUrl,updatedResponse, delay);
92 callbackResponseThread.start();
94 return ResponseDefinitionBuilder
95 .like(responseDefinition).but()
96 .withStatus(200).withBody(updatedResponse).withHeader("Content-Type", "text/xml")
102 public boolean applyGlobally() {
106 private class CallbackResponseThread extends Thread {
108 private String callbackUrl;
109 private String payLoad;
112 public CallbackResponseThread(String callbackUrl, String payLoad, int delay) {
113 this.callbackUrl = callbackUrl;
114 this.payLoad = payLoad;
118 @SuppressWarnings("deprecation")
121 //Delay sending callback response
123 } catch (InterruptedException e1) {
124 // TODO Auto-generated catch block
125 LOGGER.debug("Exception :",e1);
127 System.out.println("Sending callback response to url: " + callbackUrl);
128 ClientRequest request = new ClientRequest(callbackUrl);
129 request.body("text/xml", payLoad);
130 //System.err.println(payLoad);
132 ClientResponse result = request.post();
133 System.out.println("Successfully posted callback? Status: " + result.getStatus());
134 //System.err.println("Successfully posted callback:" + result.getStatus());
135 } catch (Exception e) {
136 // TODO Auto-generated catch block
137 System.out.println("catch error in - request.post() ");
138 LOGGER.debug("Exception :",e);