[MSO-8] Update the maven dependency
[so.git] / bpmn / MSOCommonBPMN / src / test / java / org / openecomp / mso / bpmn / common / SDNCAdapterV1Test.java
1 /*- 
2  * ============LICENSE_START======================================================= 
3  * OPENECOMP - MSO 
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.openecomp.mso.bpmn.common;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27 import static org.openecomp.mso.bpmn.mock.StubResponseDatabase.mockUpdateRequestDB;
28 import static org.openecomp.mso.bpmn.mock.StubResponseSDNCAdapter.mockSDNCAdapter;
29
30 import java.io.IOException;
31 import java.util.HashMap;
32 import java.util.Map;
33
34 import javax.ws.rs.core.Response;
35
36 import org.camunda.bpm.engine.MismatchingMessageCorrelationException;
37 import org.camunda.bpm.engine.impl.cfg.ProcessEngineConfigurationImpl;
38 import org.camunda.bpm.engine.runtime.Job;
39 import org.camunda.bpm.engine.test.Deployment;
40 import org.camunda.bpm.engine.variable.impl.VariableMapImpl;
41 import org.junit.Assert;
42 import org.junit.Test;
43 import org.openecomp.mso.bpmn.common.adapter.sdnc.CallbackHeader;
44 import org.openecomp.mso.bpmn.common.adapter.sdnc.SDNCAdapterCallbackRequest;
45 import org.openecomp.mso.bpmn.common.adapter.sdnc.SDNCAdapterResponse;
46 import org.openecomp.mso.bpmn.common.workflow.service.SDNCAdapterCallbackServiceImpl;
47 import org.openecomp.mso.bpmn.common.workflow.service.SDNCAdapterCallbackServiceImpl.SDNCAdapterExceptionResponse;
48 import org.openecomp.mso.bpmn.common.workflow.service.WorkflowResource;
49 import org.openecomp.mso.bpmn.common.workflow.service.WorkflowResponse;
50 import org.openecomp.mso.bpmn.mock.FileUtil;
51
52 /**
53  * Unit test cases for SDNCAdapterV1.bpmn
54  */
55 public class SDNCAdapterV1Test extends WorkflowTest {
56
57         private String sdncAdapterWorkflowRequest;
58         private String sdncAdapterWorkflowRequestAct;
59         private String sdncAdapterCallbackRequestData;
60         private String sdncAdapterCallbackRequestDataNonfinal;
61
62         public SDNCAdapterV1Test() throws IOException {
63                 sdncAdapterWorkflowRequest = FileUtil.readResourceFile("sdncadapterworkflowrequest.xml");
64                 sdncAdapterWorkflowRequestAct = FileUtil.readResourceFile("sdncadapterworkflowrequest-act.xml");
65                 sdncAdapterCallbackRequestData = FileUtil.readResourceFile("sdncadaptercallbackrequestdata.text");
66                 sdncAdapterCallbackRequestDataNonfinal = FileUtil.readResourceFile("sdncadaptercallbackrequestdata-nonfinal.text");
67         }
68
69         /**
70          * End-to-End flow - Unit test for SDNCAdapterV1.bpmn
71          *  - String input & String response
72          */
73
74         private WorkflowResponse invokeFlow(String workflowRequest) {
75
76                 Map<String, Object>valueMap = new HashMap<String, Object>();
77                 valueMap.put("value", workflowRequest);
78                 Map<String, Object> variables = new HashMap<String, Object>();
79                 variables.put("sdncAdapterWorkflowRequest", valueMap);
80
81                 Map<String, Object> valueMap2 = new HashMap<String, Object>();
82                 valueMap2.put("value", "true");
83                 variables.put("isDebugLogEnabled", valueMap2);
84
85                 VariableMapImpl varMap = new VariableMapImpl();
86                 varMap.put("variables", variables);
87
88                 //System.out.println("Invoking the flow");
89
90                 WorkflowResource workflowResource = new WorkflowResource();
91                 workflowResource.setProcessEngineServices4junit(processEngineRule);
92
93                 Response response = workflowResource.startProcessInstanceByKey("sdncAdapter", varMap);
94                 WorkflowResponse workflowResponse = (WorkflowResponse) response.getEntity();
95
96                 //String pid = workflowResponse.getProcessInstanceID();
97                 //System.out.println("Back from executing process instance with pid=" + pid);
98                 return workflowResponse;
99         }
100
101         @Test
102         @Deployment(resources = {"subprocess/SDNCAdapterV1.bpmn"})
103         public void sunnyDay() throws InterruptedException {
104
105                 mockSDNCAdapter(200);
106
107                 //System.out.println("SDNCAdapter sunny day flow Started!");
108
109                 ProcessExecutionThread thread = new ProcessExecutionThread(sdncAdapterWorkflowRequest);
110                 thread.start();
111                 waitForExecutionToStart("sdncAdapter", 3);
112                 String pid = getPid();
113
114                 assertProcessInstanceNotFinished(pid);
115
116                 System.out.println("Injecting SDNC Adapter asynchronous callback message to continue processing");
117                 String generatedRequestId = (String) processEngineRule.getRuntimeService().getVariable(pid, "SDNCA_requestId");
118                 CallbackHeader callbackHeader = new CallbackHeader();
119                 callbackHeader.setRequestId(generatedRequestId);
120                 callbackHeader.setResponseCode("200");
121                 callbackHeader.setResponseMessage("OK");
122                 SDNCAdapterCallbackRequest sdncAdapterCallbackRequest = new SDNCAdapterCallbackRequest();
123                 sdncAdapterCallbackRequest.setCallbackHeader(callbackHeader);
124                 sdncAdapterCallbackRequest.setRequestData(sdncAdapterCallbackRequestData);
125                 SDNCAdapterCallbackServiceImpl callbackService = new SDNCAdapterCallbackServiceImpl();
126                 callbackService.setProcessEngineServices4junit(processEngineRule);
127                 SDNCAdapterResponse sdncAdapterResponse = callbackService.sdncAdapterCallback(sdncAdapterCallbackRequest);
128                 //System.out.println("Back from executing process again");
129
130                 assertFalse(sdncAdapterResponse instanceof SDNCAdapterExceptionResponse);
131                 assertProcessInstanceFinished(pid);
132
133                 //System.out.println("SDNCAdapter sunny day flow Completed!");
134         }
135
136         @Test
137         @Deployment(resources = {"subprocess/SDNCAdapterV1.bpmn"})
138         public void nonFinalWithTimeout() throws InterruptedException {
139
140                 mockSDNCAdapter(200);
141                 mockUpdateRequestDB(200, "Database/DBAdapter.xml");
142
143                 //System.out.println("SDNCAdapter interim status processing flow Started!");
144
145                 ProcessExecutionThread thread = new ProcessExecutionThread(sdncAdapterWorkflowRequestAct);
146                 thread.start();
147                 waitForExecutionToStart("sdncAdapter", 3);
148                 String pid = getPid();
149
150                 assertProcessInstanceNotFinished(pid);
151
152                 //System.out.println("Injecting SDNC Adapter asynchronous callback message to continue processing");
153                 String generatedRequestId = (String) processEngineRule.getRuntimeService().getVariable(pid, "SDNCA_requestId");
154                 CallbackHeader callbackHeader = new CallbackHeader();
155                 callbackHeader.setRequestId(generatedRequestId);
156                 callbackHeader.setResponseCode("200");
157                 callbackHeader.setResponseMessage("OK");
158                 SDNCAdapterCallbackRequest sdncAdapterCallbackRequest = new SDNCAdapterCallbackRequest();
159                 sdncAdapterCallbackRequest.setCallbackHeader(callbackHeader);
160                 sdncAdapterCallbackRequest.setRequestData(sdncAdapterCallbackRequestDataNonfinal);
161                 SDNCAdapterCallbackServiceImpl callbackService = new SDNCAdapterCallbackServiceImpl();
162                 callbackService.setProcessEngineServices4junit(processEngineRule);
163                 SDNCAdapterResponse sdncAdapterResponse = callbackService.sdncAdapterCallback(sdncAdapterCallbackRequest);
164                 //System.out.println("Back from executing process again");
165
166                 assertFalse(sdncAdapterResponse instanceof SDNCAdapterExceptionResponse);
167                 assertProcessInstanceNotFinished(pid);
168
169                 checkForTimeout(pid);
170
171                 assertEquals(true, (Boolean) (getVariable(pid, "continueListening")));
172
173
174                 //System.out.println("SDNCAdapter interim status processing flow Completed!");
175         }
176
177         @Test
178         @Deployment(resources = {"subprocess/SDNCAdapterV1.bpmn"})
179         public void nonFinalThenFinal() throws InterruptedException {
180
181                 mockSDNCAdapter(200);
182                 mockUpdateRequestDB(200, "Database/DBAdapter.xml");
183
184                 //System.out.println("SDNCAdapter non-final then final processing flow Started!");
185
186                 // Start the flow
187                 ProcessExecutionThread thread = new ProcessExecutionThread(sdncAdapterWorkflowRequestAct);
188                 thread.start();
189                 waitForExecutionToStart("sdncAdapter", 3);
190                 String pid = getPid();
191
192                 assertProcessInstanceNotFinished(pid);
193
194                 // Inject a "non-final" SDNC Adapter asynchronous callback message
195                 //System.out.println("Injecting SDNC Adapter asynchronous callback message to continue processing");
196                 String generatedRequestId = (String) processEngineRule.getRuntimeService().getVariable(pid, "SDNCA_requestId");
197                 CallbackHeader callbackHeader = new CallbackHeader();
198                 callbackHeader.setRequestId(generatedRequestId);
199                 callbackHeader.setResponseCode("200");
200                 callbackHeader.setResponseMessage("OK");
201                 SDNCAdapterCallbackRequest sdncAdapterCallbackRequest = new SDNCAdapterCallbackRequest();
202                 sdncAdapterCallbackRequest.setCallbackHeader(callbackHeader);
203                 sdncAdapterCallbackRequest.setRequestData(sdncAdapterCallbackRequestDataNonfinal);
204                 SDNCAdapterCallbackServiceImpl callbackService = new SDNCAdapterCallbackServiceImpl();
205                 callbackService.setProcessEngineServices4junit(processEngineRule);
206                 SDNCAdapterResponse sdncAdapterResponse = callbackService.sdncAdapterCallback(sdncAdapterCallbackRequest);
207                 //System.out.println("Back from executing process again");
208
209                 assertFalse(sdncAdapterResponse instanceof SDNCAdapterExceptionResponse);
210                 assertProcessInstanceNotFinished(pid);
211                 assertEquals(true, (Boolean) (getVariable(pid, "continueListening")));
212
213                 // Inject a "final" SDNC Adapter asynchronous callback message
214                 sdncAdapterCallbackRequest.setRequestData(sdncAdapterCallbackRequestData);
215                 sdncAdapterResponse = callbackService.sdncAdapterCallback(sdncAdapterCallbackRequest);
216                 //System.out.println("Back from executing process again");
217
218                 assertFalse(sdncAdapterResponse instanceof SDNCAdapterExceptionResponse);
219                 assertProcessInstanceFinished(pid);
220                 assertEquals(false, (Boolean) (getVariable(pid, "continueListening")));
221
222                 //System.out.println("SDNCAdapter non-final then final processing flow Completed!");
223         }
224
225         
226
227         private void waitForExecutionToStart(String processDefintion, int count) throws InterruptedException {
228                 //System.out.println(processEngineRule.getRuntimeService().createExecutionQuery().processDefinitionKey(processDefintion).count());
229                 while (processEngineRule.getRuntimeService().createExecutionQuery().processDefinitionKey(processDefintion).count() != count) {
230                         Thread.sleep(200);
231                 }
232         }
233
234         @Test
235         @Deployment(resources = {"subprocess/SDNCAdapterV1.bpmn"})
236         public void badCorrelationIdTest() throws InterruptedException {
237
238                 mockSDNCAdapter(200);
239
240                 //System.out.println("SDNCAdapter bad RequestId test Started!");
241
242                 ProcessExecutionThread thread = new ProcessExecutionThread(sdncAdapterWorkflowRequest);
243                 thread.start();
244                 waitForExecutionToStart("sdncAdapter", 3);
245                 String pid = getPid();
246                 assertProcessInstanceNotFinished(pid);
247
248                 //System.out.println("Injecting SDNC Adapter asynchronous callback message to continue processing");
249                 String badRequestId = "This is not the RequestId that was used";
250                 CallbackHeader callbackHeader = new CallbackHeader();
251                 callbackHeader.setRequestId(badRequestId);
252                 callbackHeader.setResponseCode("200");
253                 callbackHeader.setResponseMessage("OK");
254                 SDNCAdapterCallbackRequest sdncAdapterCallbackRequest = new SDNCAdapterCallbackRequest();
255                 sdncAdapterCallbackRequest.setCallbackHeader(callbackHeader);
256                 sdncAdapterCallbackRequest.setRequestData(sdncAdapterCallbackRequestData);
257                 SDNCAdapterCallbackServiceImpl callbackService = new SDNCAdapterCallbackServiceImpl();
258                 callbackService.setProcessEngineServices4junit(processEngineRule);
259                 SDNCAdapterResponse sdncAdapterResponse = callbackService.sdncAdapterCallback(sdncAdapterCallbackRequest);
260                 //System.out.println("Back from executing process again");
261
262                 assertTrue(sdncAdapterResponse instanceof SDNCAdapterExceptionResponse);
263                 assertTrue(((SDNCAdapterExceptionResponse) sdncAdapterResponse).getException() instanceof MismatchingMessageCorrelationException);
264                 assertProcessInstanceNotFinished(pid);
265
266                 //System.out.println("SDNCAdapter bad RequestId test Completed!");
267         }
268
269         @Test
270         @Deployment(resources = {"subprocess/SDNCAdapterV1.bpmn"})
271         public void badSynchronousResponse() throws IOException, InterruptedException {
272
273                 mockSDNCAdapter(404);
274
275                 //System.out.println("SDNCAdapter bad synchronous response flow Started!");
276
277                 ProcessExecutionThread thread = new ProcessExecutionThread(sdncAdapterWorkflowRequest);
278                 thread.start();
279                 while (thread.isAlive()) {
280                         Thread.sleep(200);
281                 }
282                 WorkflowResponse response = thread.workflowResponse;
283                 Assert.assertNotNull(response);
284                 Assert.assertEquals("404 error", response.getMessageCode(),7000);
285 //              assertProcessInstanceFinished(response.getProcessInstanceID());
286                 //System.out.println("SDNCAdapter bad synchronous response flow Completed!");
287         }
288
289         @Test
290         @Deployment(resources = {"subprocess/SDNCAdapterV1.bpmn"})
291         public void sdncNotFound() throws IOException, InterruptedException {
292                 mockSDNCAdapter(200);
293                 mockSDNCAdapter("/sdncAdapterMock/404", 400, "sdncCallbackErrorResponse.xml");
294
295                 ProcessExecutionThread thread = new ProcessExecutionThread(sdncAdapterWorkflowRequest);
296                 thread.start();
297                 waitForExecutionToStart("sdncAdapter", 3);
298                 String pid = getPid();
299
300                 //System.out.println("Injecting SDNC Adapter asynchronous callback message to continue processing");
301                 String generatedRequestId = (String) processEngineRule.getRuntimeService().getVariable(pid, "SDNCA_requestId");
302                 CallbackHeader callbackHeader = new CallbackHeader();
303                 callbackHeader.setRequestId(generatedRequestId);
304                 callbackHeader.setResponseCode("404");
305                 callbackHeader.setResponseMessage("Error processing request to SDNC. Not Found. https://sdncodl.it.us.aic.cip.att.com:8443/restconf/config/L3SDN-API:services/layer3-service-list/AS%2FVLXM%2F000199%2F%2FSB_INTERNET. SDNC Returned-[error-type:application, error-tag:data-missing, error-message:Request could not be completed because the relevant data model content does not exist.]");
306                 SDNCAdapterCallbackRequest sdncAdapterCallbackRequest = new SDNCAdapterCallbackRequest();
307                 sdncAdapterCallbackRequest.setCallbackHeader(callbackHeader);
308                 SDNCAdapterCallbackServiceImpl callbackService = new SDNCAdapterCallbackServiceImpl();
309                 callbackService.setProcessEngineServices4junit(processEngineRule);
310                 SDNCAdapterResponse sdncAdapterResponse = callbackService.sdncAdapterCallback(sdncAdapterCallbackRequest);
311                 //System.out.println("Back from executing process again");
312
313                 assertProcessInstanceFinished(pid);
314                 assertNotNull(sdncAdapterResponse);
315                 //TODO query history to see SDNCA_ResponseCode, SDNCA_ErrorResponse
316                 //System.out.println("SDNCAdapter SDNC Notfound test Completed!");
317         }
318
319         @Test
320         @Deployment(resources = {"subprocess/SDNCAdapterV1.bpmn"})
321         public void asynchronousMessageTimeout() throws IOException, InterruptedException {
322                 mockSDNCAdapter(200);
323                 //System.out.println("SDNCAdapter asynchronous message timeout flow Started!");
324                 ProcessExecutionThread thread = new ProcessExecutionThread(sdncAdapterWorkflowRequest);
325                 thread.start();
326                 waitForExecutionToStart("sdncAdapter", 3);
327                 checkForTimeout(getPid());
328         }
329
330         private void checkForTimeout(String pid) throws InterruptedException {
331
332                 assertProcessInstanceNotFinished(pid);
333
334                 ProcessEngineConfigurationImpl processEngineConfiguration =
335                         (ProcessEngineConfigurationImpl) processEngineRule.getProcessEngine().getProcessEngineConfiguration();
336                 assertTrue(processEngineConfiguration.getJobExecutor().isActive());
337
338             Job timerJob = processEngineRule.getManagementService().createJobQuery().processInstanceId(pid).singleResult();
339             assertNotNull(timerJob);
340
341             processEngineRule.getManagementService().executeJob(timerJob.getId());
342
343             assertProcessInstanceFinished(pid);
344
345                 //System.out.println("SDNCAdapter asynchronous message timeout flow Completed!");
346         }
347
348         class ProcessExecutionThread extends Thread {
349
350                 private String workflowRequest;
351                 private WorkflowResponse workflowResponse;
352
353                 public ProcessExecutionThread(String workflowRequest) {
354                         this.workflowRequest = workflowRequest;
355                 }
356
357                 public void run() {
358                         workflowResponse = invokeFlow(workflowRequest);
359                         workflowResponse.getProcessInstanceID();
360                 }
361         }
362
363         private String getPid() {
364                 return processEngineRule.getHistoryService().createHistoricProcessInstanceQuery().list().get(0).getId();
365         }
366
367         private Object getVariable(String pid, String variableName) {
368                 try {
369                         return
370                                 processEngineRule
371                                         .getHistoryService()
372                                         .createHistoricVariableInstanceQuery()
373                                         .processInstanceId(pid).variableName(variableName)
374                                         .singleResult()
375                                         .getValue();
376                 } catch(Exception ex) {
377                         return null;
378                 }
379         }
380
381         private void assertProcessInstanceFinished(String pid) {
382             assertEquals(1, processEngineRule.getHistoryService().createHistoricProcessInstanceQuery().processInstanceId(pid).finished().count());
383         }
384
385         private void assertProcessInstanceNotFinished(String pid) {
386             assertEquals(0, processEngineRule.getHistoryService().createHistoricProcessInstanceQuery().processInstanceId(pid).finished().count());
387         }
388
389 }