\r
/**\r
* Set of utility methods used for Unit testing\r
+ *\r
*/\r
public class BPMNUtil {\r
\r
- public static String getVariable(ProcessEngineServices processEngineServices, String processDefinitionID, String name) {\r
- String pID = getProcessInstanceId(processEngineServices,\r
- processDefinitionID);\r
- assertProcessInstanceFinished(processEngineServices, pID);\r
- HistoricVariableInstance responseData = processEngineServices.getHistoryService()\r
- .createHistoricVariableInstanceQuery().processInstanceId(pID)\r
- .variableName(name)\r
- .singleResult();\r
-\r
- if (responseData != null) {\r
- return (responseData.getValue() != null ? responseData.getValue().toString() : null);\r
- }\r
- return null;\r
- }\r
-\r
- @SuppressWarnings("unchecked")\r
- public static <T> T getRawVariable(ProcessEngineServices processEngineServices, String processDefinitionID, String name) {\r
- String pID = getProcessInstanceId(processEngineServices,\r
- processDefinitionID);\r
- assertProcessInstanceFinished(processEngineServices, pID);\r
- Object responseData = processEngineServices.getHistoryService()\r
- .createHistoricVariableInstanceQuery().processInstanceId(pID)\r
- .variableName(name)\r
- .singleResult()\r
- .getValue();\r
- return (T) responseData;\r
- }\r
-\r
-\r
- public static void assertAnyProcessInstanceFinished(ProcessEngineServices processEngineServices, String processDefinitionID) {\r
- String pID = getProcessInstanceId(processEngineServices,\r
- processDefinitionID);\r
- assertNotNull(pID);\r
- assertTrue(processEngineServices.getHistoryService().createHistoricProcessInstanceQuery().processInstanceId(pID).finished().count() > 0);\r
- }\r
-\r
- public static void assertNoProcessInstance(ProcessEngineServices processEngineServices, String processDefinitionID) {\r
- assertNull(getProcessInstanceId(processEngineServices, processDefinitionID));\r
- }\r
-\r
- public static void assertProcessInstanceFinished(ProcessEngineServices processEngineServices, String pid) {\r
- assertEquals(1, processEngineServices.getHistoryService().createHistoricProcessInstanceQuery().processInstanceId(pid).finished().count());\r
- }\r
-\r
- public static void assertProcessInstanceNotFinished(ProcessEngineServices processEngineServices, String processDefinitionID) {\r
- String pID = getProcessInstanceId(processEngineServices,\r
- processDefinitionID);\r
- assertEquals(0, processEngineServices.getHistoryService().createHistoricProcessInstanceQuery().processInstanceId(pID).finished().count());\r
- }\r
-\r
- private static String getProcessInstanceId(\r
- ProcessEngineServices processEngineServices, String processDefinitionID) {\r
- List<HistoricProcessInstance> historyList = processEngineServices.getHistoryService().createHistoricProcessInstanceQuery().list();\r
- String pID = null;\r
- for (HistoricProcessInstance hInstance : historyList) {\r
- if (hInstance.getProcessDefinitionKey().equals(processDefinitionID)) {\r
- pID = hInstance.getId();\r
- break;\r
- }\r
- }\r
- return pID;\r
- }\r
-\r
- public static boolean isProcessInstanceFinished(ProcessEngineServices processEngineServices, String pid) {\r
- return processEngineServices.getHistoryService().createHistoricProcessInstanceQuery().processInstanceId(pid).finished().count() == 1 ? true : false;\r
- }\r
-\r
-\r
- private static void buildVariable(String key, String value, Map<String, Object> variableValueType) {\r
- Map<String, Object> host = new HashMap<>();\r
- host.put("value", value);\r
- host.put("type", "String");\r
- variableValueType.put(key, host);\r
- }\r
-\r
- public static WorkflowResponse executeWorkFlow(ProcessEngineServices processEngineServices, String processKey, Map<String, String> variables) {\r
- WorkflowResource workflowResource = new WorkflowResource();\r
- VariableMapImpl variableMap = new VariableMapImpl();\r
-\r
- Map<String, Object> variableValueType = new HashMap<>();\r
- for (String key : variables.keySet()) {\r
- buildVariable(key, variables.get(key), variableValueType);\r
- }\r
- buildVariable("mso-service-request-timeout", "600", variableValueType);\r
- variableMap.put("variables", variableValueType);\r
-\r
- workflowResource.setProcessEngineServices4junit(processEngineServices);\r
- Response response = workflowResource.startProcessInstanceByKey(\r
- processKey, variableMap);\r
- WorkflowResponse workflowResponse = (WorkflowResponse) response.getEntity();\r
- return workflowResponse;\r
- }\r
-\r
- //Check the runtime service to see whether the process is completed\r
- public static void waitForWorkflowToFinish(ProcessEngineServices processEngineServices, String pid) throws InterruptedException {\r
- // Don't wait forever\r
- long waitTime = 120000;\r
- long endTime = System.currentTimeMillis() + waitTime;\r
-\r
- while (true) {\r
- if (processEngineServices.getRuntimeService().createProcessInstanceQuery().processInstanceId(pid).singleResult() == null) {\r
- break;\r
- }\r
-\r
- if (System.currentTimeMillis() >= endTime) {\r
- fail("Process " + pid + " did not finish in " + waitTime + "ms");\r
- }\r
-\r
- Thread.sleep(200);\r
- }\r
- }\r
-\r
- /**\r
- * Executes the Asynchronous workflow in synchronous fashion and returns the WorkflowResponse object\r
- *\r
- * @param processEngineServices\r
- * @param processKey\r
- * @param variables\r
- * @return\r
- * @throws InterruptedException\r
- */\r
- public static WorkflowResponse executeAsyncWorkflow(ProcessEngineServices processEngineServices, String processKey, Map<String, String> variables) throws InterruptedException {\r
- ProcessThread pthread = new ProcessThread(processKey, processEngineServices, variables);\r
- pthread.start();\r
- BPMNUtil.assertProcessInstanceNotFinished(processEngineServices, processKey);\r
- String pid = getProcessInstanceId(processEngineServices, processKey);\r
- //Caution: If there is a problem with workflow, this may wait for ever\r
- while (true) {\r
- pid = getProcessInstanceId(processEngineServices, processKey);\r
- if (!isProcessInstanceFinished(processEngineServices, pid)) {\r
- Thread.sleep(200);\r
- } else {\r
- break;\r
- }\r
- }\r
- //need to retrieve for second time ?\r
- pid = getProcessInstanceId(processEngineServices, processKey);\r
- waitForWorkflowToFinish(processEngineServices, pid);\r
- return pthread.workflowResponse;\r
- }\r
-\r
- /**\r
- * Execute workflow using async resource\r
- *\r
- * @param processEngineServices\r
- * @param processKey\r
- * @param asyncResponse\r
- * @param variables\r
- */\r
- private static void executeAsyncFlow(ProcessEngineServices processEngineServices, String processKey, AsynchronousResponse asyncResponse, Map<String, String> variables) {\r
- WorkflowAsyncResource workflowResource = new WorkflowAsyncResource();\r
- VariableMapImpl variableMap = new VariableMapImpl();\r
-\r
- Map<String, Object> variableValueType = new HashMap<>();\r
- for (String key : variables.keySet()) {\r
- buildVariable(key, variables.get(key), variableValueType);\r
- }\r
- buildVariable("mso-service-request-timeout", "600", variableValueType);\r
- variableMap.put("variables", variableValueType);\r
-\r
- workflowResource.setProcessEngineServices4junit(processEngineServices);\r
- workflowResource.startProcessInstanceByKey(asyncResponse, processKey, variableMap);\r
- }\r
-\r
- /**\r
- * Helper class which executes workflow in a thread\r
- */\r
- static class ProcessThread extends Thread {\r
-\r
- public WorkflowResponse workflowResponse = null;\r
- public String processKey;\r
- public AsynchronousResponse asyncResponse = spy(AsynchronousResponse.class);\r
- public boolean started;\r
- public ProcessEngineServices processEngineServices;\r
- public Map<String, String> variables;\r
-\r
- public ProcessThread(String processKey, ProcessEngineServices processEngineServices, Map<String, String> variables) {\r
- this.processKey = processKey;\r
- this.processEngineServices = processEngineServices;\r
- this.variables = variables;\r
- }\r
-\r
- public void run() {\r
- started = true;\r
- doAnswer(new Answer<Void>() {\r
- public Void answer(InvocationOnMock invocation) {\r
- Response response = (Response) invocation.getArguments()[0];\r
- workflowResponse = (WorkflowResponse) response.getEntity();\r
- return null;\r
- }\r
- }).when(asyncResponse).setResponse(any(Response.class));\r
- executeAsyncFlow(processEngineServices, processKey, asyncResponse, variables);\r
- }\r
- }\r
+ public static String getVariable(ProcessEngineServices processEngineServices, String processDefinitionID, String name) {\r
+ String pID = getProcessInstanceId(processEngineServices,\r
+ processDefinitionID);\r
+ assertProcessInstanceFinished(processEngineServices, pID);\r
+ HistoricVariableInstance responseData = processEngineServices.getHistoryService()\r
+ .createHistoricVariableInstanceQuery().processInstanceId(pID)\r
+ .variableName(name)\r
+ .singleResult();\r
+ \r
+ if (responseData != null) {\r
+ return (responseData.getValue() != null ? responseData.getValue().toString(): null); \r
+ }\r
+ return null;\r
+ }\r
+\r
+ @SuppressWarnings("unchecked")\r
+ public static <T> T getRawVariable(ProcessEngineServices processEngineServices, String processDefinitionID, String name) {\r
+ String pID = getProcessInstanceId(processEngineServices,\r
+ processDefinitionID);\r
+ assertProcessInstanceFinished(processEngineServices, pID);\r
+ Object responseData = processEngineServices.getHistoryService()\r
+ .createHistoricVariableInstanceQuery().processInstanceId(pID)\r
+ .variableName(name)\r
+ .singleResult()\r
+ .getValue();\r
+ return (T) responseData;\r
+ }\r
+\r
+ \r
+ public static void assertAnyProcessInstanceFinished(ProcessEngineServices processEngineServices, String processDefinitionID) {\r
+ String pID = getProcessInstanceId(processEngineServices,\r
+ processDefinitionID);\r
+ assertNotNull(pID);\r
+ assertTrue(processEngineServices.getHistoryService().createHistoricProcessInstanceQuery().processInstanceId(pID).finished().count() > 0);\r
+ }\r
+ \r
+ public static void assertNoProcessInstance(ProcessEngineServices processEngineServices, String processDefinitionID) {\r
+ assertNull(getProcessInstanceId(processEngineServices, processDefinitionID));\r
+ }\r
+ \r
+ public static void assertProcessInstanceFinished(ProcessEngineServices processEngineServices, String pid) {\r
+ assertEquals(1, processEngineServices.getHistoryService().createHistoricProcessInstanceQuery().processInstanceId(pid).finished().count());\r
+ }\r
+ \r
+ public static void assertProcessInstanceNotFinished(ProcessEngineServices processEngineServices, String processDefinitionID) {\r
+ String pID = getProcessInstanceId(processEngineServices,\r
+ processDefinitionID); \r
+ assertEquals(0, processEngineServices.getHistoryService().createHistoricProcessInstanceQuery().processInstanceId(pID).finished().count());\r
+ }\r
+\r
+ private static String getProcessInstanceId(\r
+ ProcessEngineServices processEngineServices, String processDefinitionID) {\r
+ List<HistoricProcessInstance> historyList = processEngineServices.getHistoryService().createHistoricProcessInstanceQuery().list();\r
+ String pID = null;\r
+ for (HistoricProcessInstance hInstance: historyList) {\r
+ if (hInstance.getProcessDefinitionKey().equals(processDefinitionID)) {\r
+ pID = hInstance.getId();\r
+ break;\r
+ }\r
+ }\r
+ return pID;\r
+ }\r
+\r
+ public static boolean isProcessInstanceFinished(ProcessEngineServices processEngineServices, String pid) {\r
+ return processEngineServices.getHistoryService().createHistoricProcessInstanceQuery().processInstanceId(pid).finished().count() == 1 ? true: false;\r
+ }\r
+\r
+ \r
+ private static void buildVariable(String key, String value, Map<String,Object> variableValueType) {\r
+ Map<String, Object> host = new HashMap<>();\r
+ host.put("value", value);\r
+ host.put("type", "String");\r
+ variableValueType.put(key, host);\r
+ }\r
+ \r
+ public static WorkflowResponse executeWorkFlow(ProcessEngineServices processEngineServices, String processKey, Map<String,String> variables) {\r
+ WorkflowResource workflowResource = new WorkflowResource();\r
+ VariableMapImpl variableMap = new VariableMapImpl();\r
+\r
+ Map<String, Object> variableValueType = new HashMap<>();\r
+ for (String key : variables.keySet()) {\r
+ buildVariable(key, variables.get(key), variableValueType);\r
+ }\r
+ buildVariable("mso-service-request-timeout","600", variableValueType);\r
+ variableMap.put("variables", variableValueType);\r
+ \r
+ workflowResource.setProcessEngineServices4junit(processEngineServices);\r
+ Response response = workflowResource.startProcessInstanceByKey(\r
+ processKey, variableMap);\r
+ WorkflowResponse workflowResponse = (WorkflowResponse) response.getEntity();\r
+ return workflowResponse;\r
+ }\r
+\r
+ //Check the runtime service to see whether the process is completed\r
+ public static void waitForWorkflowToFinish(ProcessEngineServices processEngineServices, String pid) throws InterruptedException {\r
+ // Don't wait forever\r
+ long waitTime = 120000;\r
+ long endTime = System.currentTimeMillis() + waitTime;\r
+\r
+ while (true) {\r
+ if (processEngineServices.getRuntimeService().createProcessInstanceQuery().processInstanceId(pid).singleResult() == null) {\r
+ break;\r
+ }\r
+\r
+ if (System.currentTimeMillis() >= endTime) {\r
+ fail("Process " + pid + " did not finish in " + waitTime + "ms");\r
+ }\r
+\r
+ Thread.sleep(200);\r
+ }\r
+ }\r
+ \r
+ /**\r
+ * Executes the Asynchronous workflow in synchronous fashion and returns the WorkflowResponse object\r
+ * @param processEngineServices\r
+ * @param processKey\r
+ * @param variables\r
+ * @return\r
+ * @throws InterruptedException\r
+ */\r
+ public static WorkflowResponse executeAsyncWorkflow(ProcessEngineServices processEngineServices, String processKey, Map<String,String> variables) throws InterruptedException {\r
+ ProcessThread pthread = new ProcessThread(processKey, processEngineServices, variables);\r
+ pthread.start();\r
+ BPMNUtil.assertProcessInstanceNotFinished(processEngineServices, processKey);\r
+ String pid = getProcessInstanceId(processEngineServices, processKey);\r
+ //Caution: If there is a problem with workflow, this may wait for ever\r
+ while (true) {\r
+ pid = getProcessInstanceId(processEngineServices, processKey);\r
+ if (!isProcessInstanceFinished(processEngineServices,pid)) {\r
+ Thread.sleep(200);\r
+ } else{\r
+ break;\r
+ }\r
+ }\r
+ //need to retrieve for second time ?\r
+ pid = getProcessInstanceId(processEngineServices, processKey);\r
+ waitForWorkflowToFinish(processEngineServices, pid);\r
+ return pthread.workflowResponse;\r
+ }\r
+\r
+ /**\r
+ * Execute workflow using async resource\r
+ * @param processEngineServices\r
+ * @param processKey\r
+ * @param asyncResponse\r
+ * @param variables\r
+ */\r
+ private static void executeAsyncFlow(ProcessEngineServices processEngineServices, String processKey, AsynchronousResponse asyncResponse, Map<String,String> variables) {\r
+ WorkflowAsyncResource workflowResource = new WorkflowAsyncResource();\r
+ VariableMapImpl variableMap = new VariableMapImpl();\r
+\r
+ Map<String, Object> variableValueType = new HashMap<>();\r
+ for (String key : variables.keySet()) {\r
+ buildVariable(key, variables.get(key), variableValueType);\r
+ }\r
+ buildVariable("mso-service-request-timeout","600", variableValueType);\r
+ variableMap.put("variables", variableValueType);\r
+ \r
+ workflowResource.setProcessEngineServices4junit(processEngineServices);\r
+ workflowResource.startProcessInstanceByKey(asyncResponse, processKey, variableMap);\r
+ }\r
+ \r
+ /**\r
+ * Helper class which executes workflow in a thread\r
+ *\r
+ */\r
+ static class ProcessThread extends Thread {\r
+ \r
+ public WorkflowResponse workflowResponse = null;\r
+ public String processKey;\r
+ public AsynchronousResponse asyncResponse = spy(AsynchronousResponse.class);\r
+ public boolean started;\r
+ public ProcessEngineServices processEngineServices;\r
+ public Map<String,String> variables;\r
+ \r
+ public ProcessThread(String processKey, ProcessEngineServices processEngineServices, Map<String,String> variables) {\r
+ this.processKey = processKey;\r
+ this.processEngineServices = processEngineServices;\r
+ this.variables = variables;\r
+ }\r
+ \r
+ public void run() {\r
+ started = true;\r
+ doAnswer(new Answer<Void>() {\r
+ public Void answer(InvocationOnMock invocation) {\r
+ Response response = (Response) invocation.getArguments()[0];\r
+ workflowResponse = (WorkflowResponse) response.getEntity();\r
+ return null;\r
+ }\r
+ }).when(asyncResponse).setResponse(any(Response.class)); \r
+ executeAsyncFlow(processEngineServices, processKey, asyncResponse, variables);\r
+ }\r
+ }\r
}\r