Merge "Reorder modifiers"
[so.git] / bpmn / MSOCommonBPMN / src / test / java / org / openecomp / mso / bpmn / mock / VnfAdapterDeleteMockTransformer.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 import org.openecomp.mso.logger.MsoLogger;
24
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;
30 /**
31  * Please describe the VnfAdapterCreateMockTransformer.java class
32  *
33  */
34 public class VnfAdapterDeleteMockTransformer extends ResponseTransformer {
35
36         private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL);
37
38         private String notifyCallbackResponse;
39         private String ackResponse;
40
41         public VnfAdapterDeleteMockTransformer() {
42                 notifyCallbackResponse = FileUtil.readResourceFile("__files/vnfAdapterMocks/vnfDeleteSimResponse.xml");
43         }
44
45         @Override
46         public String name() {
47                 return "vnf-adapter-delete-transformer";
48         }
49
50         @Override
51         public ResponseDefinition transform(Request request, ResponseDefinition responseDefinition,
52                         FileSource fileSource) {
53
54                 // System.err.println("notifyCallbackResponse:" + notifyCallbackResponse);
55
56                 String requestBody = request.getBodyAsString();
57
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 = "";
62
63                 try {
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);
75                 }
76
77                 System.out.println("response (mock) messageId       : " + responseMessageId);
78                 System.out.println("request  (replacement) messageId: " + messageId);
79
80                 System.out.println("vnf Response (before):" + notifyCallbackResponse);
81                 System.out.println("vnf Response (after):" + updatedResponse);
82
83                 Object vnfDelay = MockResource.getMockProperties().get("vnf_delay");
84                 int delay = 300;
85                 if (vnfDelay != null) {
86                         delay = Integer.parseInt(vnfDelay.toString());
87                 }
88
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();
93
94                 return ResponseDefinitionBuilder
95                            .like(responseDefinition).but()
96                            .withStatus(200).withBody(updatedResponse).withHeader("Content-Type", "text/xml")
97                            .build();
98
99         }
100
101         @Override
102         public boolean applyGlobally() {
103             return false;
104         }
105
106         private class CallbackResponseThread extends Thread {
107
108                 private String callbackUrl;
109                 private String payLoad;
110                 private int delay;
111
112                 public CallbackResponseThread(String callbackUrl, String payLoad, int delay) {
113                         this.callbackUrl = callbackUrl;
114                         this.payLoad = payLoad;
115                         this.delay = delay;
116                 }
117
118                 @SuppressWarnings("deprecation")
119                 public void run () {
120                         try {
121                                 //Delay sending callback response
122                                 sleep(delay);
123                         } catch (InterruptedException e1) {
124                                 // TODO Auto-generated catch block
125                                 LOGGER.debug("Exception :",e1);
126                         }
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);
131                         try {
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);
139                         }
140                 }
141
142         }
143 }