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