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 javax.xml.ws.Endpoint;
23 import org.jboss.resteasy.client.ClientRequest;
24 import org.jboss.resteasy.client.ClientResponse;
25 import org.openecomp.mso.logger.MsoLogger;
27 import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
28 import com.github.tomakehurst.wiremock.common.FileSource;
29 import com.github.tomakehurst.wiremock.extension.ResponseTransformer;
30 import com.github.tomakehurst.wiremock.http.Request;
31 import com.github.tomakehurst.wiremock.http.ResponseDefinition;
33 import org.openecomp.mso.logger.MsoLogger;
35 * Please describe the VnfAdapterCreateMockTransformer.java class
38 public class VnfAdapterCreateMockTransformer extends ResponseTransformer {
40 private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL);
42 private String notifyCallbackResponse;
43 private String ackResponse;
45 public VnfAdapterCreateMockTransformer() {
46 notifyCallbackResponse = FileUtil.readResourceFile("__files/vnfAdapterMocks/vnfCreateSimResponse.xml"); // default response
50 public String name() {
51 return "vnf-adapter-create-transformer";
55 public ResponseDefinition transform(Request request, ResponseDefinition responseDefinition,
56 FileSource fileSource) {
58 String requestBody = request.getBodyAsString();
60 String notficationUrl = requestBody.substring(requestBody.indexOf("<notificationUrl>")+17, requestBody.indexOf("</notificationUrl>"));
61 String messageId = requestBody.substring(requestBody.indexOf("<messageId>")+11, requestBody.indexOf("</messageId>"));
62 String responseMessageId = "";
63 String updatedResponse = "";
66 // try supplied response file (if any)
67 System.out.println(" Supplied fileName: " + responseDefinition.getBodyFileName());
68 ackResponse = FileUtil.readResourceFile("__files/" + responseDefinition.getBodyFileName());
69 notifyCallbackResponse = ackResponse;
70 responseMessageId = ackResponse.substring(ackResponse.indexOf("<messageId>")+11, ackResponse.indexOf("</messageId>"));
71 updatedResponse = ackResponse.replace(responseMessageId, messageId);
72 } catch (Exception ex) {
73 LOGGER.debug("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 LOGGER.debug("Exception :",e1);
129 LOGGER.debug("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 LOGGER.debug("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 LOGGER.debug("catch error in - request.post() ");
141 LOGGER.debug("Exception :",e);