Merge "Fixed string literal comparision issue"
[so.git] / bpmn / MSOCommonBPMN / src / test / java / org / openecomp / mso / bpmn / common / BPMNUtil.java
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * ONAP - SO\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  *\r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ============LICENSE_END=========================================================\r
19  */\r
20 \r
21 package org.openecomp.mso.bpmn.common;\r
22 \r
23 import static org.junit.Assert.assertEquals;\r
24 import static org.junit.Assert.fail;\r
25 import static org.mockito.Matchers.any;\r
26 import static org.mockito.Mockito.doAnswer;\r
27 import static org.mockito.Mockito.spy;\r
28 \r
29 import java.util.HashMap;\r
30 import java.util.List;\r
31 import java.util.Map;\r
32 \r
33 import javax.ws.rs.core.Response;\r
34 \r
35 import org.camunda.bpm.engine.ProcessEngineServices;\r
36 import org.camunda.bpm.engine.history.HistoricProcessInstance;\r
37 import org.camunda.bpm.engine.history.HistoricVariableInstance;\r
38 import org.camunda.bpm.engine.variable.impl.VariableMapImpl;\r
39 import org.jboss.resteasy.spi.AsynchronousResponse;\r
40 import org.mockito.invocation.InvocationOnMock;\r
41 import org.mockito.stubbing.Answer;\r
42 import org.openecomp.mso.bpmn.common.workflow.service.WorkflowAsyncCommonResource;\r
43 import org.openecomp.mso.bpmn.common.workflow.service.WorkflowAsyncResource;\r
44 import org.openecomp.mso.bpmn.common.workflow.service.WorkflowResource;\r
45 import org.openecomp.mso.bpmn.common.workflow.service.WorkflowResponse;\r
46 \r
47 /**\r
48  * Set of utility methods used for Unit testing\r
49  *\r
50  */\r
51 public class BPMNUtil {\r
52 \r
53         public static String getVariable(ProcessEngineServices processEngineServices, String processDefinitionID, String name) {\r
54                 String pID = getProcessInstanceId(processEngineServices,\r
55                                 processDefinitionID);\r
56                 assertProcessInstanceFinished(processEngineServices, pID);\r
57                 HistoricVariableInstance responseData = processEngineServices.getHistoryService()\r
58                             .createHistoricVariableInstanceQuery().processInstanceId(pID)\r
59                             .variableName(name)\r
60                             .singleResult();\r
61                 \r
62                 if (responseData != null) {\r
63                         return (responseData.getValue() != null ? responseData.getValue().toString(): null); \r
64                 }\r
65                 return null;\r
66         }\r
67 \r
68         @SuppressWarnings("unchecked")\r
69         public static <T extends Object> T getRawVariable(ProcessEngineServices processEngineServices, String processDefinitionID, String name) {\r
70                 String pID = getProcessInstanceId(processEngineServices,\r
71                                 processDefinitionID);\r
72                 assertProcessInstanceFinished(processEngineServices, pID);\r
73                 Object responseData = processEngineServices.getHistoryService()\r
74                             .createHistoricVariableInstanceQuery().processInstanceId(pID)\r
75                             .variableName(name)\r
76                             .singleResult()\r
77                             .getValue();\r
78                 return (T) responseData;\r
79         }\r
80         \r
81         \r
82         public static void assertProcessInstanceFinished(ProcessEngineServices processEngineServices, String pid) {\r
83             assertEquals(1, processEngineServices.getHistoryService().createHistoricProcessInstanceQuery().processInstanceId(pid).finished().count());\r
84         }\r
85         \r
86         public static void assertProcessInstanceNotFinished(ProcessEngineServices processEngineServices, String processDefinitionID) {\r
87                 String pID = getProcessInstanceId(processEngineServices,\r
88                                 processDefinitionID);           \r
89             assertEquals(0, processEngineServices.getHistoryService().createHistoricProcessInstanceQuery().processInstanceId(pID).finished().count());\r
90         }\r
91 \r
92         private static String getProcessInstanceId(\r
93                         ProcessEngineServices processEngineServices, String processDefinitionID) {\r
94                 List<HistoricProcessInstance> historyList =  processEngineServices.getHistoryService().createHistoricProcessInstanceQuery().list();\r
95                 String pID = null;\r
96                 for (HistoricProcessInstance hInstance: historyList) {\r
97                         if (hInstance.getProcessDefinitionKey().equals(processDefinitionID)) {\r
98                                 pID = hInstance.getId();\r
99                                 break;\r
100                         }\r
101                 }\r
102                 return pID;\r
103         }\r
104 \r
105         public static boolean isProcessInstanceFinished(ProcessEngineServices processEngineServices, String pid) {\r
106             return processEngineServices.getHistoryService().createHistoricProcessInstanceQuery().processInstanceId(pid).finished().count() == 1 ? true: false;\r
107         }\r
108 \r
109         \r
110         private static void buildVariable(String key, String value, Map<String,Object> variableValueType) {\r
111                 Map<String, Object> host = new HashMap<String, Object>();\r
112                 host.put("value", value);\r
113                 host.put("type", "String");\r
114                 variableValueType.put(key, host);\r
115         }\r
116         \r
117         public static WorkflowResponse executeWorkFlow(ProcessEngineServices processEngineServices, String processKey, Map<String,String> variables) {\r
118                 WorkflowResource workflowResource = new WorkflowResource();\r
119                 VariableMapImpl variableMap = new VariableMapImpl();\r
120 \r
121                 Map<String, Object> variableValueType = new HashMap<String, Object>();\r
122                 for (String key : variables.keySet()) {\r
123                         buildVariable(key, variables.get(key), variableValueType);\r
124                 }\r
125                 buildVariable("mso-service-request-timeout","600", variableValueType);\r
126                 variableMap.put("variables", variableValueType);\r
127                 \r
128                 workflowResource.setProcessEngineServices4junit(processEngineServices);\r
129                 Response response = workflowResource.startProcessInstanceByKey(\r
130                                         processKey, variableMap);\r
131                 WorkflowResponse workflowResponse = (WorkflowResponse) response.getEntity();\r
132                 return workflowResponse;\r
133         }\r
134 \r
135         //Check the runtime service to see whether the process is completed\r
136         public static void waitForWorkflowToFinish(ProcessEngineServices processEngineServices, String pid) throws InterruptedException {\r
137                 // Don't wait forever\r
138                 long waitTime = 120000;\r
139                 long endTime = System.currentTimeMillis() + waitTime;\r
140 \r
141                 while (true) {\r
142                         if (processEngineServices.getRuntimeService().createProcessInstanceQuery().processInstanceId(pid).singleResult() == null) {\r
143                                 break;\r
144                         }\r
145 \r
146                         if (System.currentTimeMillis() >= endTime) {\r
147                                 fail("Process " + pid + " did not finish in " + waitTime + "ms");\r
148                         }\r
149 \r
150                         Thread.sleep(200);\r
151                 }\r
152         }\r
153         \r
154         /**\r
155          * Executes the Asynchronous workflow in synchronous fashion and returns the WorkflowResponse object\r
156          * @param processEngineServices\r
157          * @param processKey\r
158          * @param variables\r
159          * @return\r
160          * @throws InterruptedException\r
161          */\r
162         public static WorkflowResponse executeAsyncWorkflow(ProcessEngineServices processEngineServices, String processKey, Map<String,String> variables) throws InterruptedException {\r
163                 ProcessThread pthread = new ProcessThread(processKey, processEngineServices, variables);\r
164                 pthread.start();\r
165                 BPMNUtil.assertProcessInstanceNotFinished(processEngineServices, processKey);\r
166                 String pid = getProcessInstanceId(processEngineServices, processKey);\r
167                 //Caution: If there is a problem with workflow, this may wait for ever\r
168                 while (true) {\r
169                         pid = getProcessInstanceId(processEngineServices, processKey);\r
170                         if (!isProcessInstanceFinished(processEngineServices,pid)) {\r
171                                 Thread.sleep(200);\r
172                         } else{\r
173                                 break;\r
174                         }\r
175                 }\r
176                 //need to retrieve for second time ?\r
177                 pid = getProcessInstanceId(processEngineServices, processKey);\r
178                 waitForWorkflowToFinish(processEngineServices, pid);\r
179                 return pthread.workflowResponse;\r
180         }\r
181 \r
182         /**\r
183          * Execute workflow using async resource\r
184          * @param processEngineServices\r
185          * @param processKey\r
186          * @param asyncResponse\r
187          * @param variables\r
188          */\r
189         private static void executeAsyncFlow(ProcessEngineServices processEngineServices, String processKey, AsynchronousResponse asyncResponse, Map<String,String> variables) {\r
190                 WorkflowAsyncResource workflowResource = new WorkflowAsyncCommonResource();\r
191                 VariableMapImpl variableMap = new VariableMapImpl();\r
192 \r
193                 Map<String, Object> variableValueType = new HashMap<String, Object>();\r
194                 for (String key : variables.keySet()) {\r
195                         buildVariable(key, variables.get(key), variableValueType);\r
196                 }\r
197                 buildVariable("mso-service-request-timeout","600", variableValueType);\r
198                 variableMap.put("variables", variableValueType);\r
199                 \r
200                 workflowResource.setProcessEngineServices4junit(processEngineServices);\r
201                 workflowResource.startProcessInstanceByKey(asyncResponse, processKey, variableMap);\r
202         }\r
203         \r
204         /**\r
205          * Helper class which executes workflow in a thread\r
206          *\r
207          */\r
208         static class ProcessThread extends Thread {\r
209                 \r
210                 public WorkflowResponse workflowResponse = null;\r
211                 public String processKey;\r
212                 public AsynchronousResponse asyncResponse = spy(AsynchronousResponse.class);\r
213                 public boolean started;\r
214                 public ProcessEngineServices processEngineServices;\r
215                 public Map<String,String> variables;\r
216                 \r
217                 public ProcessThread(String processKey, ProcessEngineServices processEngineServices, Map<String,String> variables) {\r
218                         this.processKey = processKey;\r
219                         this.processEngineServices = processEngineServices;\r
220                         this.variables = variables;\r
221                 }\r
222                 \r
223                 public void run() {\r
224                         started = true;\r
225                         doAnswer(new Answer<Void>() {\r
226                             public Void answer(InvocationOnMock invocation) {\r
227                               Response response = (Response) invocation.getArguments()[0];\r
228                               workflowResponse = (WorkflowResponse) response.getEntity();\r
229                               return null;\r
230                             }\r
231                         }).when(asyncResponse).setResponse(any(Response.class));                \r
232                         executeAsyncFlow(processEngineServices, processKey, asyncResponse, variables);\r
233                 }\r
234         }\r
235 }\r