Merge "Reorder modifiers"
[so.git] / bpmn / MSOCommonBPMN / src / test / java / org / openecomp / mso / bpmn / mock / VnfAdapterRollbackMockTransformer.java
1 /*
2  * ============LICENSE_START======================================================= 
3  * ONAP - SO 
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 
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0 
10  * 
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========================================================= 
17  */ 
18
19 package org.openecomp.mso.bpmn.mock;
20
21 import org.jboss.resteasy.client.ClientRequest;
22 import org.jboss.resteasy.client.ClientResponse;
23
24 import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
25 import com.github.tomakehurst.wiremock.common.FileSource;
26 import com.github.tomakehurst.wiremock.extension.ResponseTransformer;
27 import com.github.tomakehurst.wiremock.http.Request;
28 import com.github.tomakehurst.wiremock.http.ResponseDefinition;
29
30 import org.openecomp.mso.logger.MsoLogger;
31 /**
32  * Please describe the VnfAdapterCreateMockTransformer.java class
33  *
34  */
35 public class VnfAdapterRollbackMockTransformer extends ResponseTransformer {
36
37         private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL);
38
39         private String notifyCallbackResponse;
40         private String ackResponse;
41         private String messageId;
42
43         public VnfAdapterRollbackMockTransformer() {
44                 notifyCallbackResponse = FileUtil.readResourceFile("__files/vnfAdapterMocks/vnfRollbackSimResponse.xml");
45         }
46         
47         public VnfAdapterRollbackMockTransformer(String messageId) {
48                 this.messageId = messageId;
49         }
50
51         @Override
52         public String name() {
53                 return "vnf-adapter-rollback-transformer";
54         }
55
56         @Override
57         public ResponseDefinition transform(Request request, ResponseDefinition responseDefinition,
58                         FileSource fileSource) {
59
60                 String requestBody = request.getBodyAsString();
61
62                 String notficationUrl = requestBody.substring(requestBody.indexOf("<notificationUrl>")+17, requestBody.indexOf("</notificationUrl>"));
63                 String messageId = requestBody.substring(requestBody.indexOf("<messageId>")+11, requestBody.indexOf("</messageId>"));
64                 String responseMessageId = "";
65                 String updatedResponse = "";
66                 
67                 try {
68                         // try supplied response file (if any)
69                         System.out.println(" Supplied fileName: " + responseDefinition.getBodyFileName());
70                     ackResponse = FileUtil.readResourceFile("__files/" + responseDefinition.getBodyFileName());
71                         notifyCallbackResponse = ackResponse;
72                         responseMessageId = ackResponse.substring(ackResponse.indexOf("<messageId>")+11, ackResponse.indexOf("</messageId>"));
73                     updatedResponse = ackResponse.replace(responseMessageId, messageId); 
74                 } catch (Exception ex) {
75                         LOGGER.debug("Exception :",ex);
76                         System.out.println(" ******* Use default response file in '__files/vnfAdapterMocks/vnfRollbackSimResponse.xml'");
77                     responseMessageId = notifyCallbackResponse.substring(notifyCallbackResponse.indexOf("<messageId>")+11, notifyCallbackResponse.indexOf("</messageId>"));
78                         updatedResponse = notifyCallbackResponse.replace(responseMessageId, messageId);
79                 }
80                 
81                 System.out.println("response (mock) messageId       : " + responseMessageId);           
82                 System.out.println("request  (replacement) messageId: " + messageId);
83                 
84                 System.out.println("vnf Response (before):" + notifyCallbackResponse);
85                 System.out.println("vnf Response (after):" + updatedResponse);
86
87                 Object vnfDelay = MockResource.getMockProperties().get("vnf_delay");
88                 int delay = 300;
89                 if (vnfDelay != null) {
90                         delay = Integer.parseInt(vnfDelay.toString());
91                 }
92
93                 //Kick off callback thread
94                 System.out.println("VnfAdapterRollbackMockTransformer notficationUrl: " + notficationUrl + ":delay: " + delay);         
95                 CallbackResponseThread callbackResponseThread = new CallbackResponseThread(notficationUrl,updatedResponse, delay);
96                 callbackResponseThread.start();
97
98                 return ResponseDefinitionBuilder
99                            .like(responseDefinition).but()
100                            .withStatus(200).withBody(updatedResponse).withHeader("Content-Type", "text/xml")
101                            .build();
102                 
103         }
104
105         @Override
106         public boolean applyGlobally() {
107             return false;
108         }
109
110         private class CallbackResponseThread extends Thread {
111
112                 private String callbackUrl;
113                 private String payLoad;
114                 private int delay;
115
116                 public CallbackResponseThread(String callbackUrl, String payLoad, int delay) {
117                         this.callbackUrl = callbackUrl;
118                         this.payLoad = payLoad;
119                         this.delay = delay;
120                 }
121
122                 public void run () {
123                         try {
124                                 //Delay sending callback response
125                                 sleep(delay);
126                         } catch (InterruptedException e1) {
127                                 LOGGER.debug("Exception :",e1);
128                         }
129                         System.out.println("Sending callback response to url: " + callbackUrl);
130                         ClientRequest request = new ClientRequest(callbackUrl);
131                         request.body("text/xml", payLoad);
132                         //System.err.println(payLoad);
133                         try {
134                                 ClientResponse result = request.post();
135                                 System.out.println("Successfully posted callback? Status: " + result.getStatus());
136                                 //System.err.println("Successfully posted callback:" + result.getStatus());
137                         } catch (Exception e) {
138                                 System.out.println("catch error in - request.post() ");                         
139                                 LOGGER.debug("Exception :",e);
140                         }
141                 }
142
143         }
144 }