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.WorkflowAsyncResource;
\r
23 import org.openecomp.mso.bpmn.common.workflow.service.WorkflowResource;
\r
24 import org.openecomp.mso.bpmn.common.workflow.service.WorkflowResponse;
\r
27 * Set of utility methods used for Unit testing
\r
30 public class BPMNUtil {
\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
41 if (responseData != null) {
\r
42 return (responseData.getValue() != null ? responseData.getValue().toString(): null);
\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
57 return (T) responseData;
\r
61 public static void assertProcessInstanceFinished(ProcessEngineServices processEngineServices, String pid) {
\r
62 assertEquals(1, processEngineServices.getHistoryService().createHistoricProcessInstanceQuery().processInstanceId(pid).finished().count());
\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
71 private static String getProcessInstanceId(
\r
72 ProcessEngineServices processEngineServices, String processDefinitionID) {
\r
73 List<HistoricProcessInstance> historyList = processEngineServices.getHistoryService().createHistoricProcessInstanceQuery().list();
\r
75 for (HistoricProcessInstance hInstance: historyList) {
\r
76 if (hInstance.getProcessDefinitionKey().equals(processDefinitionID)) {
\r
77 pID = hInstance.getId();
\r
84 public static boolean isProcessInstanceFinished(ProcessEngineServices processEngineServices, String pid) {
\r
85 return processEngineServices.getHistoryService().createHistoricProcessInstanceQuery().processInstanceId(pid).finished().count() == 1 ? true: false;
\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
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
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
104 buildVariable("mso-service-request-timeout","600", variableValueType);
\r
105 variableMap.put("variables", variableValueType);
\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
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
121 if (processEngineServices.getRuntimeService().createProcessInstanceQuery().processInstanceId(pid).singleResult() == null) {
\r
125 if (System.currentTimeMillis() >= endTime) {
\r
126 fail("Process " + pid + " did not finish in " + waitTime + "ms");
\r
134 * Executes the Asynchronous workflow in synchronous fashion and returns the WorkflowResponse object
\r
135 * @param processEngineServices
\r
136 * @param processKey
\r
139 * @throws InterruptedException
\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
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
148 pid = getProcessInstanceId(processEngineServices, processKey);
\r
149 if (!isProcessInstanceFinished(processEngineServices,pid)) {
\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
162 * Execute workflow using async resource
\r
163 * @param processEngineServices
\r
164 * @param processKey
\r
165 * @param asyncResponse
\r
168 private static void executeAsyncFlow(ProcessEngineServices processEngineServices, String processKey, AsynchronousResponse asyncResponse, Map<String,String> variables) {
\r
169 WorkflowAsyncResource workflowResource = new WorkflowAsyncResource();
\r
170 VariableMapImpl variableMap = new VariableMapImpl();
\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
176 buildVariable("mso-service-request-timeout","600", variableValueType);
\r
177 variableMap.put("variables", variableValueType);
\r
179 workflowResource.setProcessEngineServices4junit(processEngineServices);
\r
180 workflowResource.startProcessInstanceByKey(asyncResponse, processKey, variableMap);
\r
184 * Helper class which executes workflow in a thread
\r
187 static class ProcessThread extends Thread {
\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
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
202 public void run() {
\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
210 }).when(asyncResponse).setResponse(any(Response.class));
\r
211 executeAsyncFlow(processEngineServices, processKey, asyncResponse, variables);
\r