1 package org.openecomp.mso.bpmn.common;
\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
9 import java.util.HashMap;
\r
10 import java.util.List;
\r
11 import java.util.Map;
\r
13 import javax.ws.rs.core.Response;
\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
28 * Set of utility methods used for Unit testing
\r
31 public class BPMNUtil {
\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
42 if (responseData != null) {
\r
43 return (responseData.getValue() != null ? responseData.getValue().toString(): null);
\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
58 return (T) responseData;
\r
62 public static void assertProcessInstanceFinished(ProcessEngineServices processEngineServices, String pid) {
\r
63 assertEquals(1, processEngineServices.getHistoryService().createHistoricProcessInstanceQuery().processInstanceId(pid).finished().count());
\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
72 private static String getProcessInstanceId(
\r
73 ProcessEngineServices processEngineServices, String processDefinitionID) {
\r
74 List<HistoricProcessInstance> historyList = processEngineServices.getHistoryService().createHistoricProcessInstanceQuery().list();
\r
76 for (HistoricProcessInstance hInstance: historyList) {
\r
77 if (hInstance.getProcessDefinitionKey().equals(processDefinitionID)) {
\r
78 pID = hInstance.getId();
\r
85 public static boolean isProcessInstanceFinished(ProcessEngineServices processEngineServices, String pid) {
\r
86 return processEngineServices.getHistoryService().createHistoricProcessInstanceQuery().processInstanceId(pid).finished().count() == 1 ? true: false;
\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
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
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
105 buildVariable("mso-service-request-timeout","600", variableValueType);
\r
106 variableMap.put("variables", variableValueType);
\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
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
122 if (processEngineServices.getRuntimeService().createProcessInstanceQuery().processInstanceId(pid).singleResult() == null) {
\r
126 if (System.currentTimeMillis() >= endTime) {
\r
127 fail("Process " + pid + " did not finish in " + waitTime + "ms");
\r
135 * Executes the Asynchronous workflow in synchronous fashion and returns the WorkflowResponse object
\r
136 * @param processEngineServices
\r
137 * @param processKey
\r
140 * @throws InterruptedException
\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
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
149 pid = getProcessInstanceId(processEngineServices, processKey);
\r
150 if (!isProcessInstanceFinished(processEngineServices,pid)) {
\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
163 * Execute workflow using async resource
\r
164 * @param processEngineServices
\r
165 * @param processKey
\r
166 * @param asyncResponse
\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
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
177 buildVariable("mso-service-request-timeout","600", variableValueType);
\r
178 variableMap.put("variables", variableValueType);
\r
180 workflowResource.setProcessEngineServices4junit(processEngineServices);
\r
181 workflowResource.startProcessInstanceByKey(asyncResponse, processKey, variableMap);
\r
185 * Helper class which executes workflow in a thread
\r
188 static class ProcessThread extends Thread {
\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
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
203 public void run() {
\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
211 }).when(asyncResponse).setResponse(any(Response.class));
\r
212 executeAsyncFlow(processEngineServices, processKey, asyncResponse, variables);
\r