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