Merge "Reorder modifiers"
[so.git] / bpmn / MSOCommonBPMN / src / test / java / org / openecomp / mso / bpmn / mock / VnfAdapterQueryMockTransformer.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
22 import com.github.tomakehurst.wiremock.extension.ResponseTransformer;
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 VnfAdapterQueryMockTransformer.java class
37  *
38  */
39
40
41 public class VnfAdapterQueryMockTransformer extends ResponseTransformer{
42         
43         private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL);
44         
45         private String notifyCallbackResponse;
46         private String ackResponse;
47         private String messageId;
48
49         public VnfAdapterQueryMockTransformer() {
50                 notifyCallbackResponse = FileUtil.readResourceFile("__files/vnfAdapterMocks/vnfQuerySimResponse.xml");
51         }
52         
53         public VnfAdapterQueryMockTransformer(String messageId) {
54                 this.messageId = messageId;
55         }
56
57         @Override
58         public String name() {
59                 return "vnf-adapter-query-transformer";
60         }
61         
62         @Override
63         public ResponseDefinition transform(Request request, ResponseDefinition responseDefinition,
64                         FileSource fileSource) {
65
66                 String requestBody = request.getBodyAsString();
67
68                 String notficationUrl = requestBody.substring(requestBody.indexOf("<notificationUrl>")+17, requestBody.indexOf("</notificationUrl>"));
69                 String messageId = requestBody.substring(requestBody.indexOf("<messageId>")+11, requestBody.indexOf("</messageId>"));
70         //      String updatedResponse = notifyCallbackResponse.replace("b1a82ce6-7f5c-45fd-9273-acaf88fc2137", messageId);
71         
72                 String responseMessageId = "";
73                 String updatedResponse = "";
74                 
75         //      if (ackResponse == null) {
76                         //System.err.println("file:" + responseDefinition.getBodyFileName());
77                 //      ackResponse = FileUtil.readResourceFile("__files/" + responseDefinition.getBodyFileName());
78                 //}
79
80                 
81                 try {
82                         // try supplied response file (if any)
83                         System.out.println(" Supplied fileName: " + responseDefinition.getBodyFileName());
84                     ackResponse = FileUtil.readResourceFile("__files/" + responseDefinition.getBodyFileName());
85                         notifyCallbackResponse = ackResponse;
86                         responseMessageId = ackResponse.substring(ackResponse.indexOf("<messageId>")+11, ackResponse.indexOf("</messageId>"));
87                     updatedResponse = ackResponse.replace(responseMessageId, messageId); 
88                 } catch (Exception ex) {
89                         LOGGER.debug("Exception :",ex);
90                         System.out.println(" ******* Use default response file in '__files/vnfAdapterMocks/vnfQuerySimResponse.xml'");
91                     responseMessageId = notifyCallbackResponse.substring(notifyCallbackResponse.indexOf("<messageId>")+11, notifyCallbackResponse.indexOf("</messageId>"));
92                         updatedResponse = notifyCallbackResponse.replace(responseMessageId, messageId);
93                 }
94                 
95                 System.out.println("response (mock) messageId       : " + responseMessageId);           
96                 System.out.println("request  (replacement) messageId: " + messageId);
97                 
98                 System.out.println("vnf Response (before):" + notifyCallbackResponse);
99                 System.out.println("vnf Response (after):" + updatedResponse);
100                 
101                 
102                 Object vnfDelay = MockResource.getMockProperties().get("vnf_delay");
103                 int delay = 300;
104                 if (vnfDelay != null) {
105                         delay = Integer.parseInt(vnfDelay.toString());
106                 }
107
108                 //Kick off callback thread
109                 
110                 //System.out.println("notficationUrl" + notficationUrl);
111                 //System.out.println("updatedResponse" + updatedResponse);
112                 System.out.println("VnfAdapterQueryMockTransformer notficationUrl: " + notficationUrl + ":delay: " + delay);
113                 CallbackResponseThread callbackResponseThread = new CallbackResponseThread(notficationUrl,updatedResponse, delay);
114                 System.out.println("Inside Callback" );
115                 callbackResponseThread.start();
116
117                                 return ResponseDefinitionBuilder
118                              .like(responseDefinition).but()
119                              .withStatus(200).withBody(updatedResponse).withHeader("Content-Type", "text/xml")
120                              .build();
121         }
122
123         @Override
124         public boolean applyGlobally() {
125             return false;
126         }
127
128         private class CallbackResponseThread extends Thread {
129
130                 private String callbackUrl;
131                 private String payLoad;
132                 private int delay;
133
134                 public CallbackResponseThread(String callbackUrl, String payLoad, int delay) {
135                         this.callbackUrl = callbackUrl;
136                         this.payLoad = payLoad;
137                         this.delay = delay;
138                 }
139
140                 public void run () {
141                         try {
142                                 //Delay sending callback response
143                                 sleep(delay);
144                         } catch (InterruptedException e1) {
145                                 LOGGER.debug("Exception :",e1);
146                         }
147                         ClientRequest request = new ClientRequest(callbackUrl);
148                         request.body("text/xml", payLoad);
149                         //System.err.println(payLoad);
150                         try {
151                                 ClientResponse result = request.post();
152                                 //System.err.println("Successfully posted callback:" + result.getStatus());
153                         } catch (Exception e) {
154                                 LOGGER.debug("Exception :",e);
155                         }
156                 }
157
158         }
159
160
161 }