2 * ============LICENSE_START=======================================================
\r
4 * ================================================================================
\r
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * ================================================================================
\r
7 * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * you may not use this file except in compliance with the License.
\r
9 * You may obtain a copy of the License at
\r
11 * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * Unless required by applicable law or agreed to in writing, software
\r
14 * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * See the License for the specific language governing permissions and
\r
17 * limitations under the License.
\r
18 * ============LICENSE_END=========================================================
\r
21 package org.openecomp.mso.bpmn.common;
\r
23 import static org.junit.Assert.assertEquals;
\r
24 import static org.junit.Assert.fail;
\r
25 import static org.mockito.Matchers.any;
\r
26 import static org.mockito.Mockito.doAnswer;
\r
27 import static org.mockito.Mockito.spy;
\r
29 import java.util.HashMap;
\r
30 import java.util.List;
\r
31 import java.util.Map;
\r
33 import javax.ws.rs.core.Response;
\r
35 import org.camunda.bpm.engine.ProcessEngineServices;
\r
36 import org.camunda.bpm.engine.history.HistoricProcessInstance;
\r
37 import org.camunda.bpm.engine.history.HistoricVariableInstance;
\r
38 import org.camunda.bpm.engine.variable.impl.VariableMapImpl;
\r
39 import org.jboss.resteasy.spi.AsynchronousResponse;
\r
40 import org.mockito.invocation.InvocationOnMock;
\r
41 import org.mockito.stubbing.Answer;
\r
42 import org.openecomp.mso.bpmn.common.workflow.service.WorkflowAsyncCommonResource;
\r
43 import org.openecomp.mso.bpmn.common.workflow.service.WorkflowAsyncResource;
\r
44 import org.openecomp.mso.bpmn.common.workflow.service.WorkflowResource;
\r
45 import org.openecomp.mso.bpmn.common.workflow.service.WorkflowResponse;
\r
48 * Set of utility methods used for Unit testing
\r
51 public class BPMNUtil {
\r
53 public static String getVariable(ProcessEngineServices processEngineServices, String processDefinitionID, String name) {
\r
54 String pID = getProcessInstanceId(processEngineServices,
\r
55 processDefinitionID);
\r
56 assertProcessInstanceFinished(processEngineServices, pID);
\r
57 HistoricVariableInstance responseData = processEngineServices.getHistoryService()
\r
58 .createHistoricVariableInstanceQuery().processInstanceId(pID)
\r
62 if (responseData != null) {
\r
63 return (responseData.getValue() != null ? responseData.getValue().toString(): null);
\r
68 @SuppressWarnings("unchecked")
\r
69 public static <T extends Object> T getRawVariable(ProcessEngineServices processEngineServices, String processDefinitionID, String name) {
\r
70 String pID = getProcessInstanceId(processEngineServices,
\r
71 processDefinitionID);
\r
72 assertProcessInstanceFinished(processEngineServices, pID);
\r
73 Object responseData = processEngineServices.getHistoryService()
\r
74 .createHistoricVariableInstanceQuery().processInstanceId(pID)
\r
78 return (T) responseData;
\r
82 public static void assertProcessInstanceFinished(ProcessEngineServices processEngineServices, String pid) {
\r
83 assertEquals(1, processEngineServices.getHistoryService().createHistoricProcessInstanceQuery().processInstanceId(pid).finished().count());
\r
86 public static void assertProcessInstanceNotFinished(ProcessEngineServices processEngineServices, String processDefinitionID) {
\r
87 String pID = getProcessInstanceId(processEngineServices,
\r
88 processDefinitionID);
\r
89 assertEquals(0, processEngineServices.getHistoryService().createHistoricProcessInstanceQuery().processInstanceId(pID).finished().count());
\r
92 private static String getProcessInstanceId(
\r
93 ProcessEngineServices processEngineServices, String processDefinitionID) {
\r
94 List<HistoricProcessInstance> historyList = processEngineServices.getHistoryService().createHistoricProcessInstanceQuery().list();
\r
96 for (HistoricProcessInstance hInstance: historyList) {
\r
97 if (hInstance.getProcessDefinitionKey().equals(processDefinitionID)) {
\r
98 pID = hInstance.getId();
\r
105 public static boolean isProcessInstanceFinished(ProcessEngineServices processEngineServices, String pid) {
\r
106 return processEngineServices.getHistoryService().createHistoricProcessInstanceQuery().processInstanceId(pid).finished().count() == 1 ? true: false;
\r
110 private static void buildVariable(String key, String value, Map<String,Object> variableValueType) {
\r
111 Map<String, Object> host = new HashMap<String, Object>();
\r
112 host.put("value", value);
\r
113 host.put("type", "String");
\r
114 variableValueType.put(key, host);
\r
117 public static WorkflowResponse executeWorkFlow(ProcessEngineServices processEngineServices, String processKey, Map<String,String> variables) {
\r
118 WorkflowResource workflowResource = new WorkflowResource();
\r
119 VariableMapImpl variableMap = new VariableMapImpl();
\r
121 Map<String, Object> variableValueType = new HashMap<String, Object>();
\r
122 for (String key : variables.keySet()) {
\r
123 buildVariable(key, variables.get(key), variableValueType);
\r
125 buildVariable("mso-service-request-timeout","600", variableValueType);
\r
126 variableMap.put("variables", variableValueType);
\r
128 workflowResource.setProcessEngineServices4junit(processEngineServices);
\r
129 Response response = workflowResource.startProcessInstanceByKey(
\r
130 processKey, variableMap);
\r
131 WorkflowResponse workflowResponse = (WorkflowResponse) response.getEntity();
\r
132 return workflowResponse;
\r
135 //Check the runtime service to see whether the process is completed
\r
136 public static void waitForWorkflowToFinish(ProcessEngineServices processEngineServices, String pid) throws InterruptedException {
\r
137 // Don't wait forever
\r
138 long waitTime = 120000;
\r
139 long endTime = System.currentTimeMillis() + waitTime;
\r
142 if (processEngineServices.getRuntimeService().createProcessInstanceQuery().processInstanceId(pid).singleResult() == null) {
\r
146 if (System.currentTimeMillis() >= endTime) {
\r
147 fail("Process " + pid + " did not finish in " + waitTime + "ms");
\r
155 * Executes the Asynchronous workflow in synchronous fashion and returns the WorkflowResponse object
\r
156 * @param processEngineServices
\r
157 * @param processKey
\r
160 * @throws InterruptedException
\r
162 public static WorkflowResponse executeAsyncWorkflow(ProcessEngineServices processEngineServices, String processKey, Map<String,String> variables) throws InterruptedException {
\r
163 ProcessThread pthread = new ProcessThread(processKey, processEngineServices, variables);
\r
165 BPMNUtil.assertProcessInstanceNotFinished(processEngineServices, processKey);
\r
166 String pid = getProcessInstanceId(processEngineServices, processKey);
\r
167 //Caution: If there is a problem with workflow, this may wait for ever
\r
169 pid = getProcessInstanceId(processEngineServices, processKey);
\r
170 if (!isProcessInstanceFinished(processEngineServices,pid)) {
\r
176 //need to retrieve for second time ?
\r
177 pid = getProcessInstanceId(processEngineServices, processKey);
\r
178 waitForWorkflowToFinish(processEngineServices, pid);
\r
179 return pthread.workflowResponse;
\r
183 * Execute workflow using async resource
\r
184 * @param processEngineServices
\r
185 * @param processKey
\r
186 * @param asyncResponse
\r
189 private static void executeAsyncFlow(ProcessEngineServices processEngineServices, String processKey, AsynchronousResponse asyncResponse, Map<String,String> variables) {
\r
190 WorkflowAsyncResource workflowResource = new WorkflowAsyncCommonResource();
\r
191 VariableMapImpl variableMap = new VariableMapImpl();
\r
193 Map<String, Object> variableValueType = new HashMap<String, Object>();
\r
194 for (String key : variables.keySet()) {
\r
195 buildVariable(key, variables.get(key), variableValueType);
\r
197 buildVariable("mso-service-request-timeout","600", variableValueType);
\r
198 variableMap.put("variables", variableValueType);
\r
200 workflowResource.setProcessEngineServices4junit(processEngineServices);
\r
201 workflowResource.startProcessInstanceByKey(asyncResponse, processKey, variableMap);
\r
205 * Helper class which executes workflow in a thread
\r
208 static class ProcessThread extends Thread {
\r
210 public WorkflowResponse workflowResponse = null;
\r
211 public String processKey;
\r
212 public AsynchronousResponse asyncResponse = spy(AsynchronousResponse.class);
\r
213 public boolean started;
\r
214 public ProcessEngineServices processEngineServices;
\r
215 public Map<String,String> variables;
\r
217 public ProcessThread(String processKey, ProcessEngineServices processEngineServices, Map<String,String> variables) {
\r
218 this.processKey = processKey;
\r
219 this.processEngineServices = processEngineServices;
\r
220 this.variables = variables;
\r
223 public void run() {
\r
225 doAnswer(new Answer<Void>() {
\r
226 public Void answer(InvocationOnMock invocation) {
\r
227 Response response = (Response) invocation.getArguments()[0];
\r
228 workflowResponse = (WorkflowResponse) response.getEntity();
\r
231 }).when(asyncResponse).setResponse(any(Response.class));
\r
232 executeAsyncFlow(processEngineServices, processKey, asyncResponse, variables);
\r