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