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