1 package org.openecomp.mso.bpmn.common;
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.fail;
5 import static org.mockito.Matchers.any;
6 import static org.mockito.Mockito.doAnswer;
7 import static org.mockito.Mockito.spy;
9 import java.util.HashMap;
10 import java.util.List;
13 import javax.ws.rs.core.Response;
15 import org.camunda.bpm.engine.ProcessEngineServices;
16 import org.camunda.bpm.engine.history.HistoricProcessInstance;
17 import org.camunda.bpm.engine.history.HistoricVariableInstance;
18 import org.camunda.bpm.engine.variable.impl.VariableMapImpl;
19 import org.jboss.resteasy.spi.AsynchronousResponse;
20 import org.mockito.invocation.InvocationOnMock;
21 import org.mockito.stubbing.Answer;
22 import org.openecomp.mso.bpmn.common.workflow.service.WorkflowAsyncCommonResource;
23 import org.openecomp.mso.bpmn.common.workflow.service.WorkflowResource;
24 import org.openecomp.mso.bpmn.common.workflow.service.WorkflowResponse;
27 * Set of utility methods used for Unit testing
30 public class BPMNUtil {
32 public static String getVariable(ProcessEngineServices processEngineServices, String processDefinitionID, String name) {
33 String pID = getProcessInstanceId(processEngineServices,
35 assertProcessInstanceFinished(processEngineServices, pID);
36 HistoricVariableInstance responseData = processEngineServices.getHistoryService()
37 .createHistoricVariableInstanceQuery().processInstanceId(pID)
41 if (responseData != null) {
42 return (responseData.getValue() != null ? responseData.getValue().toString(): null);
47 @SuppressWarnings("unchecked")
48 public static <T extends Object> T getRawVariable(ProcessEngineServices processEngineServices, String processDefinitionID, String name) {
49 String pID = getProcessInstanceId(processEngineServices,
51 assertProcessInstanceFinished(processEngineServices, pID);
52 Object responseData = processEngineServices.getHistoryService()
53 .createHistoricVariableInstanceQuery().processInstanceId(pID)
57 return (T) responseData;
61 public static void assertProcessInstanceFinished(ProcessEngineServices processEngineServices, String pid) {
62 assertEquals(1, processEngineServices.getHistoryService().createHistoricProcessInstanceQuery().processInstanceId(pid).finished().count());
65 public static void assertProcessInstanceNotFinished(ProcessEngineServices processEngineServices, String processDefinitionID) {
66 String pID = getProcessInstanceId(processEngineServices,
68 assertEquals(0, processEngineServices.getHistoryService().createHistoricProcessInstanceQuery().processInstanceId(pID).finished().count());
71 private static String getProcessInstanceId(
72 ProcessEngineServices processEngineServices, String processDefinitionID) {
73 List<HistoricProcessInstance> historyList = processEngineServices.getHistoryService().createHistoricProcessInstanceQuery().list();
75 for (HistoricProcessInstance hInstance: historyList) {
76 if (hInstance.getProcessDefinitionKey().equals(processDefinitionID)) {
77 pID = hInstance.getId();
84 public static boolean isProcessInstanceFinished(ProcessEngineServices processEngineServices, String pid) {
85 return processEngineServices.getHistoryService().createHistoricProcessInstanceQuery().processInstanceId(pid).finished().count() == 1 ? true: false;
89 private static void buildVariable(String key, String value, Map<String,Object> variableValueType) {
90 Map<String, Object> host = new HashMap<String, Object>();
91 host.put("value", value);
92 host.put("type", "String");
93 variableValueType.put(key, host);
96 public static WorkflowResponse executeWorkFlow(ProcessEngineServices processEngineServices, String processKey, Map<String,String> variables) {
97 WorkflowResource workflowResource = new WorkflowResource();
98 VariableMapImpl variableMap = new VariableMapImpl();
100 Map<String, Object> variableValueType = new HashMap<String, Object>();
101 for (String key : variables.keySet()) {
102 buildVariable(key, variables.get(key), variableValueType);
104 buildVariable("mso-service-request-timeout","600", variableValueType);
105 variableMap.put("variables", variableValueType);
107 workflowResource.setProcessEngineServices4junit(processEngineServices);
108 Response response = workflowResource.startProcessInstanceByKey(
109 processKey, variableMap);
110 WorkflowResponse workflowResponse = (WorkflowResponse) response.getEntity();
111 return workflowResponse;
114 //Check the runtime service to see whether the process is completed
115 public static void waitForWorkflowToFinish(ProcessEngineServices processEngineServices, String pid) throws InterruptedException {
116 // Don't wait forever
117 long waitTime = 120000;
118 long endTime = System.currentTimeMillis() + waitTime;
121 if (processEngineServices.getRuntimeService().createProcessInstanceQuery().processInstanceId(pid).singleResult() == null) {
125 if (System.currentTimeMillis() >= endTime) {
126 fail("Process " + pid + " did not finish in " + waitTime + "ms");
134 * Executes the Asynchronous workflow in synchronous fashion and returns the WorkflowResponse object
135 * @param processEngineServices
139 * @throws InterruptedException
141 public static WorkflowResponse executeAsyncWorkflow(ProcessEngineServices processEngineServices, String processKey, Map<String,String> variables) throws InterruptedException {
142 ProcessThread pthread = new ProcessThread(processKey, processEngineServices, variables);
144 BPMNUtil.assertProcessInstanceNotFinished(processEngineServices, processKey);
145 String pid = getProcessInstanceId(processEngineServices, processKey);
146 //Caution: If there is a problem with workflow, this may wait for ever
148 pid = getProcessInstanceId(processEngineServices, processKey);
149 if (!isProcessInstanceFinished(processEngineServices,pid)) {
155 //need to retrieve for second time ?
156 pid = getProcessInstanceId(processEngineServices, processKey);
157 waitForWorkflowToFinish(processEngineServices, pid);
158 return pthread.workflowResponse;
162 * Execute workflow using async resource
163 * @param processEngineServices
165 * @param asyncResponse
168 private static void executeAsyncFlow(ProcessEngineServices processEngineServices, String processKey, AsynchronousResponse asyncResponse, Map<String,String> variables) {
169 WorkflowAsyncCommonResource workflowResource = new WorkflowAsyncCommonResource();
170 VariableMapImpl variableMap = new VariableMapImpl();
172 Map<String, Object> variableValueType = new HashMap<String, Object>();
173 for (String key : variables.keySet()) {
174 buildVariable(key, variables.get(key), variableValueType);
176 buildVariable("mso-service-request-timeout","600", variableValueType);
177 variableMap.put("variables", variableValueType);
179 workflowResource.setProcessEngineServices4junit(processEngineServices);
180 workflowResource.startProcessInstanceByKey(asyncResponse, processKey, variableMap);
184 * Helper class which executes workflow in a thread
187 static class ProcessThread extends Thread {
189 public WorkflowResponse workflowResponse = null;
190 public String processKey;
191 public AsynchronousResponse asyncResponse = spy(AsynchronousResponse.class);
192 public boolean started;
193 public ProcessEngineServices processEngineServices;
194 public Map<String,String> variables;
196 public ProcessThread(String processKey, ProcessEngineServices processEngineServices, Map<String,String> variables) {
197 this.processKey = processKey;
198 this.processEngineServices = processEngineServices;
199 this.variables = variables;
204 doAnswer(new Answer<Void>() {
205 public Void answer(InvocationOnMock invocation) {
206 Response response = (Response) invocation.getArguments()[0];
207 workflowResponse = (WorkflowResponse) response.getEntity();
210 }).when(asyncResponse).setResponse(any(Response.class));
211 executeAsyncFlow(processEngineServices, processKey, asyncResponse, variables);