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 javax.xml.ws.Endpoint;
28 import org.jboss.resteasy.client.ClientRequest;
29 import org.jboss.resteasy.client.ClientResponse;
31 import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
32 import com.github.tomakehurst.wiremock.common.FileSource;
33 import com.github.tomakehurst.wiremock.extension.ResponseTransformer;
34 import com.github.tomakehurst.wiremock.http.Request;
35 import com.github.tomakehurst.wiremock.http.ResponseDefinition;
38 * Please describe the VnfAdapterCreateMockTransformer.java class
41 public class VnfAdapterCreateMockTransformer extends ResponseTransformer {
43 private String notifyCallbackResponse;
44 private String ackResponse;
46 public VnfAdapterCreateMockTransformer() {
47 notifyCallbackResponse = FileUtil.readResourceFile("__files/vnfAdapterMocks/vnfCreateSimResponse.xml"); // default response
51 public String name() {
52 return "vnf-adapter-create-transformer";
56 public ResponseDefinition transform(Request request, ResponseDefinition responseDefinition,
57 FileSource fileSource) {
59 String requestBody = request.getBodyAsString();
61 String notficationUrl = requestBody.substring(requestBody.indexOf("<notificationUrl>")+17, requestBody.indexOf("</notificationUrl>"));
62 String messageId = requestBody.substring(requestBody.indexOf("<messageId>")+11, requestBody.indexOf("</messageId>"));
63 String responseMessageId = "";
64 String updatedResponse = "";
67 // try supplied response file (if any)
68 System.out.println(" Supplied fileName: " + responseDefinition.getBodyFileName());
69 ackResponse = FileUtil.readResourceFile("__files/" + responseDefinition.getBodyFileName());
70 notifyCallbackResponse = ackResponse;
71 responseMessageId = ackResponse.substring(ackResponse.indexOf("<messageId>")+11, ackResponse.indexOf("</messageId>"));
72 updatedResponse = ackResponse.replace(responseMessageId, messageId);
73 } catch (Exception ex) {
74 System.out.println(" ******* Use default response file in '__files/vnfAdapterMocks/vnfCreateSimResponse.xml'");
75 responseMessageId = notifyCallbackResponse.substring(notifyCallbackResponse.indexOf("<messageId>")+11, notifyCallbackResponse.indexOf("</messageId>"));
76 updatedResponse = notifyCallbackResponse.replace(responseMessageId, messageId);
79 System.out.println("response (mock) messageId : " + responseMessageId);
80 System.out.println("request (replacement) messageId: " + messageId);
82 System.out.println("vnf Response (before):" + notifyCallbackResponse);
83 System.out.println("vnf Response (after):" + updatedResponse);
85 Object vnfDelay = MockResource.getMockProperties().get("vnf_delay");
87 if (vnfDelay != null) {
88 delay = Integer.parseInt(vnfDelay.toString());
91 //Kick off callback thread
92 System.out.println("VnfAdapterCreateMockTransformer notficationUrl: " + notficationUrl + ":delay: " + delay);
93 CallbackResponseThread callbackResponseThread = new CallbackResponseThread(notficationUrl,updatedResponse, delay);
94 callbackResponseThread.start();
96 return ResponseDefinitionBuilder
97 .like(responseDefinition).but()
98 .withStatus(200).withBody(updatedResponse).withHeader("Content-Type", "text/xml")
104 public boolean applyGlobally() {
108 private class CallbackResponseThread extends Thread {
110 private String callbackUrl;
111 private String payLoad;
114 public CallbackResponseThread(String callbackUrl, String payLoad, int delay) {
115 this.callbackUrl = callbackUrl;
116 this.payLoad = payLoad;
120 @SuppressWarnings("deprecation")
123 //Delay sending callback response
125 } catch (InterruptedException e1) {
126 // TODO Auto-generated catch block
127 e1.printStackTrace();
129 System.out.println("Sending callback response to url: " + callbackUrl);
130 ClientRequest request = new ClientRequest(callbackUrl);
131 request.body("text/xml", payLoad);
132 //System.out.println("payLoad: " + 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() ");