[MSO-8] Update the maven dependency
[so.git] / bpmn / MSOCommonBPMN / src / test / java / org / openecomp / mso / bpmn / common / BPMNUtil.java
1 package org.openecomp.mso.bpmn.common;
2
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;
8
9 import java.util.HashMap;
10 import java.util.List;
11 import java.util.Map;
12
13 import javax.ws.rs.core.Response;
14
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;
25
26 /**
27  * Set of utility methods used for Unit testing
28  *
29  */
30 public class BPMNUtil {
31
32         public static String getVariable(ProcessEngineServices processEngineServices, String processDefinitionID, String name) {
33                 String pID = getProcessInstanceId(processEngineServices,
34                                 processDefinitionID);
35                 assertProcessInstanceFinished(processEngineServices, pID);
36                 HistoricVariableInstance responseData = processEngineServices.getHistoryService()
37                             .createHistoricVariableInstanceQuery().processInstanceId(pID)
38                             .variableName(name)
39                             .singleResult();
40                 
41                 if (responseData != null) {
42                         return (responseData.getValue() != null ? responseData.getValue().toString(): null); 
43                 }
44                 return null;
45         }
46
47         @SuppressWarnings("unchecked")
48         public static <T extends Object> T getRawVariable(ProcessEngineServices processEngineServices, String processDefinitionID, String name) {
49                 String pID = getProcessInstanceId(processEngineServices,
50                                 processDefinitionID);
51                 assertProcessInstanceFinished(processEngineServices, pID);
52                 Object responseData = processEngineServices.getHistoryService()
53                             .createHistoricVariableInstanceQuery().processInstanceId(pID)
54                             .variableName(name)
55                             .singleResult()
56                             .getValue();
57                 return (T) responseData;
58         }
59         
60         
61         public static void assertProcessInstanceFinished(ProcessEngineServices processEngineServices, String pid) {
62             assertEquals(1, processEngineServices.getHistoryService().createHistoricProcessInstanceQuery().processInstanceId(pid).finished().count());
63         }
64         
65         public static void assertProcessInstanceNotFinished(ProcessEngineServices processEngineServices, String processDefinitionID) {
66                 String pID = getProcessInstanceId(processEngineServices,
67                                 processDefinitionID);           
68             assertEquals(0, processEngineServices.getHistoryService().createHistoricProcessInstanceQuery().processInstanceId(pID).finished().count());
69         }
70
71         private static String getProcessInstanceId(
72                         ProcessEngineServices processEngineServices, String processDefinitionID) {
73                 List<HistoricProcessInstance> historyList =  processEngineServices.getHistoryService().createHistoricProcessInstanceQuery().list();
74                 String pID = null;
75                 for (HistoricProcessInstance hInstance: historyList) {
76                         if (hInstance.getProcessDefinitionKey().equals(processDefinitionID)) {
77                                 pID = hInstance.getId();
78                                 break;
79                         }
80                 }
81                 return pID;
82         }
83
84         public static boolean isProcessInstanceFinished(ProcessEngineServices processEngineServices, String pid) {
85             return processEngineServices.getHistoryService().createHistoricProcessInstanceQuery().processInstanceId(pid).finished().count() == 1 ? true: false;
86         }
87
88         
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);
94         }
95         
96         public static WorkflowResponse executeWorkFlow(ProcessEngineServices processEngineServices, String processKey, Map<String,String> variables) {
97                 WorkflowResource workflowResource = new WorkflowResource();
98                 VariableMapImpl variableMap = new VariableMapImpl();
99
100                 Map<String, Object> variableValueType = new HashMap<String, Object>();
101                 for (String key : variables.keySet()) {
102                         buildVariable(key, variables.get(key), variableValueType);
103                 }
104                 buildVariable("mso-service-request-timeout","600", variableValueType);
105                 variableMap.put("variables", variableValueType);
106                 
107                 workflowResource.setProcessEngineServices4junit(processEngineServices);
108                 Response response = workflowResource.startProcessInstanceByKey(
109                                         processKey, variableMap);
110                 WorkflowResponse workflowResponse = (WorkflowResponse) response.getEntity();
111                 return workflowResponse;
112         }
113
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;
119
120                 while (true) {
121                         if (processEngineServices.getRuntimeService().createProcessInstanceQuery().processInstanceId(pid).singleResult() == null) {
122                                 break;
123                         }
124
125                         if (System.currentTimeMillis() >= endTime) {
126                                 fail("Process " + pid + " did not finish in " + waitTime + "ms");
127                         }
128
129                         Thread.sleep(200);
130                 }
131         }
132         
133         /**
134          * Executes the Asynchronous workflow in synchronous fashion and returns the WorkflowResponse object
135          * @param processEngineServices
136          * @param processKey
137          * @param variables
138          * @return
139          * @throws InterruptedException
140          */
141         public static WorkflowResponse executeAsyncWorkflow(ProcessEngineServices processEngineServices, String processKey, Map<String,String> variables) throws InterruptedException {
142                 ProcessThread pthread = new ProcessThread(processKey, processEngineServices, variables);
143                 pthread.start();
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
147                 while (true) {
148                         pid = getProcessInstanceId(processEngineServices, processKey);
149                         if (!isProcessInstanceFinished(processEngineServices,pid)) {
150                                 Thread.sleep(200);
151                         } else{
152                                 break;
153                         }
154                 }
155                 //need to retrieve for second time ?
156                 pid = getProcessInstanceId(processEngineServices, processKey);
157                 waitForWorkflowToFinish(processEngineServices, pid);
158                 return pthread.workflowResponse;
159         }
160
161         /**
162          * Execute workflow using async resource
163          * @param processEngineServices
164          * @param processKey
165          * @param asyncResponse
166          * @param variables
167          */
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();
171
172                 Map<String, Object> variableValueType = new HashMap<String, Object>();
173                 for (String key : variables.keySet()) {
174                         buildVariable(key, variables.get(key), variableValueType);
175                 }
176                 buildVariable("mso-service-request-timeout","600", variableValueType);
177                 variableMap.put("variables", variableValueType);
178                 
179                 workflowResource.setProcessEngineServices4junit(processEngineServices);
180                 workflowResource.startProcessInstanceByKey(asyncResponse, processKey, variableMap);
181         }
182         
183         /**
184          * Helper class which executes workflow in a thread
185          *
186          */
187         static class ProcessThread extends Thread {
188                 
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;
195                 
196                 public ProcessThread(String processKey, ProcessEngineServices processEngineServices, Map<String,String> variables) {
197                         this.processKey = processKey;
198                         this.processEngineServices = processEngineServices;
199                         this.variables = variables;
200                 }
201                 
202                 public void run() {
203                         started = true;
204                         doAnswer(new Answer<Void>() {
205                             public Void answer(InvocationOnMock invocation) {
206                               Response response = (Response) invocation.getArguments()[0];
207                               workflowResponse = (WorkflowResponse) response.getEntity();
208                               return null;
209                             }
210                         }).when(asyncResponse).setResponse(any(Response.class));                
211                         executeAsyncFlow(processEngineServices, processKey, asyncResponse, variables);
212                 }
213         }
214 }