Containerization feature of SO
[so.git] / bpmn / MSOCommonBPMN / src / test / java / org / onap / so / bpmn / mock / VnfAdapterDeleteMockTransformer.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 import javax.ws.rs.core.UriBuilder;
24
25 import org.onap.so.client.HttpClient;
26 import org.onap.so.logger.MsoLogger;
27 import org.onap.so.utils.TargetEntity;
28
29 import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
30 import com.github.tomakehurst.wiremock.common.FileSource;
31 import com.github.tomakehurst.wiremock.extension.Parameters;
32 import com.github.tomakehurst.wiremock.extension.ResponseDefinitionTransformer;
33 import com.github.tomakehurst.wiremock.http.Request;
34 import com.github.tomakehurst.wiremock.http.ResponseDefinition;
35
36 /**
37  * Please describe the VnfAdapterCreateMockTransformer.java class
38  *
39  */
40 public class VnfAdapterDeleteMockTransformer extends ResponseDefinitionTransformer {
41
42         private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, VnfAdapterDeleteMockTransformer.class);
43
44         private String notifyCallbackResponse;
45         private String ackResponse;
46
47         public VnfAdapterDeleteMockTransformer() {
48                 notifyCallbackResponse = FileUtil.readResourceFile("__files/vnfAdapterMocks/vnfDeleteSimResponse.xml");
49         }
50
51         @Override
52         public String getName() {
53                 return "vnf-adapter-delete-transformer";
54         }
55
56         @Override
57         public ResponseDefinition transform(Request request, ResponseDefinition responseDefinition,
58                         FileSource fileSource, Parameters parameters) {
59
60                 // System.err.println("notifyCallbackResponse:" + notifyCallbackResponse);
61
62                 String requestBody = request.getBodyAsString();
63
64                 String notficationUrl = requestBody.substring(requestBody.indexOf("<notificationUrl>")+17, requestBody.indexOf("</notificationUrl>"));
65                 String messageId = requestBody.substring(requestBody.indexOf("<messageId>")+11, requestBody.indexOf("</messageId>"));
66                 String responseMessageId = "";
67                 String updatedResponse = "";
68
69                 try {
70                         // try supplied response file (if any)
71                         System.out.println(" Supplied fileName: " + responseDefinition.getBodyFileName());
72                     ackResponse = FileUtil.readResourceFile("__files/" + responseDefinition.getBodyFileName());
73                         notifyCallbackResponse = ackResponse;
74                         responseMessageId = ackResponse.substring(ackResponse.indexOf("<messageId>")+11, ackResponse.indexOf("</messageId>"));
75                     updatedResponse = ackResponse.replace(responseMessageId, messageId);
76                 } catch (Exception ex) {
77                         LOGGER.debug("Exception :",ex);
78                         System.out.println(" ******* Use default response file in '__files/vnfAdapterMocks/vnfDeleteSimResponse.xml'");
79                     responseMessageId = notifyCallbackResponse.substring(notifyCallbackResponse.indexOf("<messageId>")+11, notifyCallbackResponse.indexOf("</messageId>"));
80                         updatedResponse = notifyCallbackResponse.replace(responseMessageId, messageId);
81                 }
82
83                 System.out.println("response (mock) messageId       : " + responseMessageId);
84                 System.out.println("request  (replacement) messageId: " + messageId);
85
86                 System.out.println("vnf Response (before):" + notifyCallbackResponse);
87                 System.out.println("vnf Response (after):" + updatedResponse);
88
89                 Object vnfDelay = MockResource.getMockProperties().get("vnf_delay");
90                 int delay = 300;
91                 if (vnfDelay != null) {
92                         delay = Integer.parseInt(vnfDelay.toString());
93                 }
94
95                 //Kick off callback thread
96                 System.out.println("VnfAdapterDeleteMockTransformer notficationUrl: " + notficationUrl + ":delay: " + delay);
97                 CallbackResponseThread callbackResponseThread = new CallbackResponseThread(notficationUrl,updatedResponse, delay);
98                 callbackResponseThread.start();
99
100                 return ResponseDefinitionBuilder
101                            .like(responseDefinition).but()
102                            .withStatus(200).withBody(updatedResponse).withHeader("Content-Type", "text/xml")
103                            .build();
104
105         }
106
107         @Override
108         public boolean applyGlobally() {
109             return false;
110         }
111
112         private class CallbackResponseThread extends Thread {
113
114                 private String callbackUrl;
115                 private String payLoad;
116                 private int delay;
117
118                 public CallbackResponseThread(String callbackUrl, String payLoad, int delay) {
119                         this.callbackUrl = callbackUrl;
120                         this.payLoad = payLoad;
121                         this.delay = delay;
122                 }
123
124                 @SuppressWarnings("deprecation")
125                 public void run () {
126                         try {
127                                 //Delay sending callback response
128                                 sleep(delay);
129                         } catch (InterruptedException e1) {
130                                 // TODO Auto-generated catch block
131                                 LOGGER.debug("Exception :",e1);
132                         }
133
134                         try {
135                                 HttpClient client = new HttpClient(UriBuilder.fromUri(callbackUrl).build().toURL(), "text/xml", TargetEntity.VNF_ADAPTER);
136                                 client.post(payLoad);
137                         } catch (Exception e) {
138                                 // TODO Auto-generated catch block
139                                 System.out.println("catch error in - request.post() ");
140                                 LOGGER.debug("Exception :",e);
141                         }
142                 }
143
144         }
145 }