Merge "Reorder modifiers"
[so.git] / bpmn / MSOCommonBPMN / src / test / java / org / openecomp / mso / bpmn / mock / VnfAdapterUpdateMockTransformer.java
1 /*
2  * ============LICENSE_START======================================================= 
3  * ONAP - SO 
4  * ================================================================================ 
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
7  * ================================================================================ 
8  * Licensed under the Apache License, Version 2.0 (the "License"); 
9  * you may not use this file except in compliance with the License. 
10  * You may obtain a copy of the License at 
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0 
13  * 
14  * Unless required by applicable law or agreed to in writing, software 
15  * distributed under the License is distributed on an "AS IS" BASIS, 
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
17  * See the License for the specific language governing permissions and 
18  * limitations under the License. 
19  * ============LICENSE_END========================================================= 
20  */ 
21
22 package org.openecomp.mso.bpmn.mock;
23
24 import org.jboss.resteasy.client.ClientRequest;
25 import org.jboss.resteasy.client.ClientResponse;
26 import org.openecomp.mso.logger.MsoLogger;
27
28 import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
29 import com.github.tomakehurst.wiremock.common.FileSource;
30 import com.github.tomakehurst.wiremock.extension.ResponseTransformer;
31 import com.github.tomakehurst.wiremock.http.Request;
32 import com.github.tomakehurst.wiremock.http.ResponseDefinition;
33
34 import org.openecomp.mso.logger.MsoLogger;
35 /**
36  * Please describe the VnfAdapterUpdateMockTransformer.java class
37  *
38  */
39 public class VnfAdapterUpdateMockTransformer extends ResponseTransformer {
40
41         private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL);
42         
43         private String notifyCallbackResponse;
44         private String requestId;
45         private String ackResponse;
46
47         public VnfAdapterUpdateMockTransformer() {
48                 notifyCallbackResponse = FileUtil.readResourceFile("vnfAdapter/vnfUpdateSimResponse.xml");
49         }
50
51         public VnfAdapterUpdateMockTransformer(String requestId) {
52                 this.requestId = requestId;
53         }
54
55
56         public String name() {
57                 return "vnf-adapter-update-transformer";
58         }
59
60         @Override
61         public ResponseDefinition transform(Request request, ResponseDefinition responseDefinition,
62                         FileSource fileSource) {
63
64                 String requestBody = request.getBodyAsString();
65
66                 String notficationUrl = requestBody.substring(requestBody.indexOf("<notificationUrl>")+17, requestBody.indexOf("</notificationUrl>"));
67                 String messageId = requestBody.substring(requestBody.indexOf("<messageId>")+11, requestBody.indexOf("</messageId>"));
68                 String responseMessageId = "";
69                 String updatedResponse = "";
70                 
71                 try {
72                         // try supplied response file (if any)
73                         System.out.println(" Supplied fileName: " + responseDefinition.getBodyFileName());
74                     ackResponse = FileUtil.readResourceFile("__files/" + responseDefinition.getBodyFileName());
75                         notifyCallbackResponse = ackResponse;
76                         responseMessageId = ackResponse.substring(ackResponse.indexOf("<messageId>")+11, ackResponse.indexOf("</messageId>"));
77                     updatedResponse = ackResponse.replace(responseMessageId, messageId); 
78                 } catch (Exception ex) {
79                         LOGGER.debug("Exception :",ex);
80                         System.out.println(" ******* Use default response file in 'vnfAdapter/vnfUpdateSimResponse.xml'");
81                     responseMessageId = notifyCallbackResponse.substring(notifyCallbackResponse.indexOf("<messageId>")+11, notifyCallbackResponse.indexOf("</messageId>"));
82                         updatedResponse = notifyCallbackResponse.replace(responseMessageId, messageId);
83                 }
84                 
85                 System.out.println("response (mock) messageId       : " + responseMessageId);           
86                 System.out.println("request  (replacement) messageId: " + messageId);
87                 
88                 System.out.println("vnf Response (before):" + notifyCallbackResponse);
89                 System.out.println("vnf Response (after):" + updatedResponse);
90                 
91                 Object vnfDelay = MockResource.getMockProperties().get("vnf_delay");
92                 int delay = 300;
93                 if (vnfDelay != null) {
94                         delay = Integer.parseInt(vnfDelay.toString());
95                 }
96
97                 //Kick off callback thread
98                 System.out.println("VnfAdapterUpdateMockTransformer notficationUrl: " + notficationUrl + ":delay: " + delay);           
99                 CallbackResponseThread callbackResponseThread = new CallbackResponseThread(notficationUrl,updatedResponse, delay);
100                 callbackResponseThread.start();
101
102                 return ResponseDefinitionBuilder
103                            .like(responseDefinition).but()
104                            .withStatus(200).withBody(updatedResponse).withHeader("Content-Type", "text/xml")
105                            .build();
106
107         }
108
109         @Override
110         public boolean applyGlobally() {
111             return false;
112         }
113
114         private class CallbackResponseThread extends Thread {
115
116                 private String callbackUrl;
117                 private String payLoad;
118                 private int delay;
119
120                 public CallbackResponseThread(String callbackUrl, String payLoad, int delay) {
121                         this.callbackUrl = callbackUrl;
122                         this.payLoad = payLoad;
123                         this.delay = delay;
124                 }
125
126                 public void run () {
127                         try {
128                                 //Delay sending callback response
129                                 sleep(delay);
130                         } catch (InterruptedException e1) {
131                                 LOGGER.debug("Exception :", e1);
132                         }
133                         System.out.println("Sending callback response to url: " + callbackUrl);                 
134                         ClientRequest request = new ClientRequest(callbackUrl);
135                         request.body("text/xml", payLoad);
136                         //System.err.println(payLoad);
137                         try {
138                                 ClientResponse result = request.post();
139                                 System.out.println("Successfully posted callback? Status: " + result.getStatus());                              
140                                 //System.err.println("Successfully posted callback:" + result.getStatus());
141                         } catch (Exception e) {
142                                 System.out.println("catch error in - request.post() ");
143                                 LOGGER.debug("Exception :",e);
144                         }
145                 }
146
147         }
148 }
149