-
Notifications
You must be signed in to change notification settings - Fork 12
/
HumanTaskTest.java
318 lines (263 loc) · 11 KB
/
HumanTaskTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package com.test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import junit.framework.Assert;
import org.drools.KnowledgeBase;
import org.drools.logger.KnowledgeRuntimeLoggerFactory;
import org.drools.persistence.jpa.JPAKnowledgeService;
import org.drools.runtime.StatefulKnowledgeSession;
import org.drools.runtime.process.ProcessInstance;
import org.jbpm.process.audit.JPAProcessInstanceDbLog;
import org.jbpm.process.audit.JPAWorkingMemoryDbLogger;
import org.jbpm.process.audit.ProcessInstanceLog;
import org.jbpm.task.Status;
import org.jbpm.task.query.TaskSummary;
import org.jbpm.task.service.TaskClient;
import org.jbpm.task.service.hornetq.CommandBasedHornetQWSHumanTaskHandler;
import org.junit.Test;
/**
* Tests to show particular situations.
*
* @author calcacuervo
*
*/
public class HumanTaskTest extends BaseHumanTaskTest {
@Override
protected String[] getTestUsers() {
return new String[] { "testUser1", "testUser2", "testUser3",
"Administrator" };
}
@Override
protected String[] getTestGroups() {
return new String[] { "testGroup1", "testGroup2" };
}
@Override
protected String[] getProcessPaths() {
return new String[] { "two-tasks-human-task-test.bpmn",
"dynamic-user-human-task-test.bpmn",
"simple-human-task-assigned-to-user-test.bpmn",
"simple-human-task-test.bpmn"};
}
@Override
protected Map<String, List<String>> getTestUserGroupsAssignments() {
Map<String, List<String>> assign = new HashMap<String, List<String>>();
List<String> user1Groups = new ArrayList<String>();
List<String> user2Groups = new ArrayList<String>();
user1Groups.add("testGroup1");
user2Groups.add("testGroup2");
assign.put("testUser1", user1Groups);
assign.put("testUser2", user2Groups);
return assign;
}
private StatefulKnowledgeSession session;
/**
* Show a case when there are two human tasks, and these two are started and
* completed, and the process is finished.
*
* @throws InterruptedException
*/
@Test
public void twoHumanTasksCompleted() throws InterruptedException {
KnowledgeBase kbase = this.createKnowledgeBase();
session = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null,
env);
// this will log in audit tables
new JPAWorkingMemoryDbLogger(session);
KnowledgeRuntimeLoggerFactory.newConsoleLogger(session);
// Logger that will give information about the process state, variables,
// etc
JPAProcessInstanceDbLog processLog = new JPAProcessInstanceDbLog(
session.getEnvironment());
CommandBasedHornetQWSHumanTaskHandler wsHumanTaskHandler = new CommandBasedHornetQWSHumanTaskHandler(
session);
wsHumanTaskHandler.setClient(client.getTaskClient());
session.getWorkItemManager().registerWorkItemHandler("Human Task",
wsHumanTaskHandler);
ProcessInstance process = session.createProcessInstance("TwoTasksTest",
null);
session.insert(process);
long processInstanceId = process.getId();
session.startProcessInstance(processInstanceId);
Thread.sleep(2000);
// A new task for testUser1 will be created.
List<TaskSummary> tasks = client.getTasksAssignedAsPotentialOwner(
"testUser1", "en-UK",
this.getTestUserGroupsAssignments().get("testUser1"));
Assert.assertEquals(1, tasks.size());
// complete task.
this.fullCycleCompleteTask(tasks.get(0).getId(), "testUser1", this
.getTestUserGroupsAssignments().get("testUser1"));
tasks = client.getTasksOwned("testUser1", "en-UK");
Assert.assertEquals(1, tasks.size());
Assert.assertEquals(Status.Completed, tasks.get(0).getStatus());
// ended first task, let's take the second.. this will be testUser2
List<TaskSummary> tasksUser2 = client.getTasksAssignedAsPotentialOwner(
"testUser2", "en-UK",
this.getTestUserGroupsAssignments().get("testUser2"));
this.fullCycleCompleteTask(tasksUser2.get(0).getId(), "testUser2", this
.getTestUserGroupsAssignments().get("testUser2"));
// Reload the tasks to see new status.
tasksUser2 = client.getTasksOwned("testUser2", "en-UK");
Assert.assertEquals(1, tasksUser2.size());
Assert.assertEquals(Status.Completed, tasksUser2.get(0).getStatus());
// now check in the logs the process finished.
ProcessInstanceLog processInstanceLog = processLog
.findProcessInstance(processInstanceId);
Assert.assertNotNull(processInstanceLog.getEnd());
}
/**
* Test to show how the a user or group can be dinamically selected in the
* bpmn file. Please refer to this process:
* dynamic-user-human-task-test.bpmn to see how to declare the groupId or
* actorId to be picked up from a process variable.
*
* @throws InterruptedException
*/
@Test
public void dynamicUser() throws InterruptedException {
KnowledgeBase kbase = this.createKnowledgeBase();
session = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null,
env);
// this will log in audit tables
new JPAWorkingMemoryDbLogger(session);
KnowledgeRuntimeLoggerFactory.newConsoleLogger(session);
// Logger that will give information about the process state, variables,
// etc
JPAProcessInstanceDbLog processLog = new JPAProcessInstanceDbLog(
session.getEnvironment());
CommandBasedHornetQWSHumanTaskHandler wsHumanTaskHandler = new CommandBasedHornetQWSHumanTaskHandler(
session);
wsHumanTaskHandler.setClient(client.getTaskClient());
session.getWorkItemManager().registerWorkItemHandler("Human Task",
wsHumanTaskHandler);
Map<String, Object> params = new HashMap<String, Object>();
// this variable "group" will be used to assign the task.
params.put("group", "testGroup2");
ProcessInstance process = session.createProcessInstance(
"DynamicSimpleTest", params);
session.insert(process);
long processInstanceId = process.getId();
session.startProcessInstance(processInstanceId);
Thread.sleep(2000);
// Check a task was created according to the variable group
List<TaskSummary> tasks = client.getTasksAssignedAsPotentialOwner(
"testUser2", "en-UK",
this.getTestUserGroupsAssignments().get("testUser2"));
Assert.assertEquals(1, tasks.size());
}
/**
* Test for skipping a human task.
*
* @throws InterruptedException
*/
@Test
public void skipHumanTask() throws InterruptedException {
KnowledgeBase kbase = this.createKnowledgeBase();
session = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null,
env);
// this will log in audit tables
new JPAWorkingMemoryDbLogger(session);
KnowledgeRuntimeLoggerFactory.newConsoleLogger(session);
// Logger that will give information about the process state, variables,
// etc
JPAProcessInstanceDbLog processLog = new JPAProcessInstanceDbLog(
session.getEnvironment());
CommandBasedHornetQWSHumanTaskHandler wsHumanTaskHandler = new CommandBasedHornetQWSHumanTaskHandler(
session);
wsHumanTaskHandler.setClient(client.getTaskClient());
session.getWorkItemManager().registerWorkItemHandler("Human Task",
wsHumanTaskHandler);
ProcessInstance process = session.createProcessInstance("TwoTasksTest",
null);
session.insert(process);
long processInstanceId = process.getId();
session.startProcessInstance(processInstanceId);
Thread.sleep(2000);
List<TaskSummary> tasks = client.getTasksAssignedAsPotentialOwner(
"testUser1", "en-UK",
this.getTestUserGroupsAssignments().get("testUser1"));
Assert.assertEquals(1, tasks.size());
long taskId = tasks.get(0).getId();
client.claim(taskId, "testUser1", this.getTestUserGroupsAssignments()
.get("testUser1"));
client.start(taskId, "testUser1");
// Here we skip the In Progress task-
client.skip(taskId, "testUser1");
tasks = client.getTasksOwned("testUser1", "en-UK");
Assert.assertEquals(1, tasks.size());
Assert.assertEquals(Status.Obsolete, tasks.get(0).getStatus());
// And then the process continues.
List<TaskSummary> tasksUser2 = client.getTasksAssignedAsPotentialOwner(
"testUser2", "en-UK",
this.getTestUserGroupsAssignments().get("testUser2"));
this.fullCycleCompleteTask(tasksUser2.get(0).getId(), "testUser2", this
.getTestUserGroupsAssignments().get("testUser2"));
// Reload the tasks to see new status.
tasksUser2 = client.getTasksOwned("testUser2", "en-UK");
Assert.assertEquals(1, tasksUser2.size());
Assert.assertEquals(Status.Completed, tasksUser2.get(0).getStatus());
// now check in the logs the process finished.
ProcessInstanceLog processInstanceLog = processLog
.findProcessInstance(processInstanceId);
Assert.assertNotNull(processInstanceLog.getEnd());
}
/**
* I have almost all tests with tasks potentially owned by groups. This test
* uses the
* {@link TaskClient#getTasksAssignedAsPotentialOwner(String, String, org.jbpm.task.service.TaskClientHandler.TaskSummaryResponseHandler)}
* method.
*
* @throws InterruptedException
*/
@Test
public void humanTaskAssignedToUser() throws InterruptedException {
KnowledgeBase kbase = this.createKnowledgeBase();
session = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null,
env);
// this will log in audit tables
new JPAWorkingMemoryDbLogger(session);
KnowledgeRuntimeLoggerFactory.newConsoleLogger(session);
// Logger that will give information about the process state, variables,
// etc
JPAProcessInstanceDbLog processLog = new JPAProcessInstanceDbLog(
session.getEnvironment());
CommandBasedHornetQWSHumanTaskHandler wsHumanTaskHandler = new CommandBasedHornetQWSHumanTaskHandler(
session);
wsHumanTaskHandler.setClient(client.getTaskClient());
session.getWorkItemManager().registerWorkItemHandler("Human Task",
wsHumanTaskHandler);
Map<String, Object> vars = new HashMap<String, Object>();
vars.put("testVar", "A value");
ProcessInstance process = session.createProcessInstance(
"HumanTaskAssignedToUser", vars);
session.insert(process);
long processInstanceId = process.getId();
session.startProcessInstance(processInstanceId);
Thread.sleep(2000);
// A new task for testUser1 will be created.
List<TaskSummary> tasks = client.getTasksAssignedAsPotentialOwner(
"testUser1", "en-UK", null);
Assert.assertEquals(1, tasks.size());
// complete task. As it is automatically put in reserved, we don't have to claim it.
Map<String, Object> params = (Map<String, Object>) client.getTaskContent(tasks.get(0).getId());
Assert.assertEquals("A value", params.get("testVar"));
Assert.assertEquals(Status.Reserved, tasks.get(0).getStatus());
client.start(tasks.get(0).getId(), "testUser1");
client.complete(tasks.get(0).getId(), "testUser1", null);
tasks = client.getTasksOwned("testUser1", "en-UK");
Assert.assertEquals(1, tasks.size());
Assert.assertEquals(Status.Completed, tasks.get(0).getStatus());
// now check in the logs the process finished.
ProcessInstanceLog processInstanceLog = processLog
.findProcessInstance(processInstanceId);
Assert.assertNotNull(processInstanceLog.getEnd());
}
private void fullCycleCompleteTask(long taskId, String userId,
List<String> groups) {
client.claim(taskId, userId, groups);
client.start(taskId, userId);
client.complete(taskId, userId, null);
}
}