@Test
public void testMultipleCandidateGroups() {
// Deploy and start process
RuntimeService runtimeService = activitiRule.getRuntimeService();
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("multipleCandidatesGroup");
// Task should not yet be assigned to anyone
TaskService taskService = activitiRule.getTaskService();
List<Task> tasks = taskService
.createTaskQuery()
.taskAssignee(KERMIT)
.list();
assertTrue(tasks.isEmpty());
tasks = taskService
.createTaskQuery()
.taskAssignee(GONZO)
.list();
assertTrue(tasks.isEmpty());
// The task should be visible in the candidate task list of Gonzo and Kermit
// and anyone in the management/accountancy group
assertEquals(1, taskService.createTaskQuery().taskCandidateUser(KERMIT).list().size());
assertEquals(1, taskService.createTaskQuery().taskCandidateUser(GONZO).list().size());
assertEquals(1, taskService.createTaskQuery().taskCandidateGroup("management").count());
assertEquals(1, taskService.createTaskQuery().taskCandidateGroup("accountancy").count());
assertEquals(0, taskService.createTaskQuery().taskCandidateGroup("sales").count());
// Gonzo claims the task
tasks = taskService.createTaskQuery().taskCandidateUser(GONZO).list();
Task task = tasks.get(0);
assertEquals("Approve expenses", task.getName());
taskService.claim(task.getId(), GONZO);
// The task must now be gone from the candidate task lists
assertTrue(taskService.createTaskQuery().taskCandidateUser(KERMIT).list().isEmpty());
assertTrue(taskService.createTaskQuery().taskCandidateUser(GONZO).list().isEmpty());
assertEquals(0, taskService.createTaskQuery().taskCandidateGroup("management").count());
// The task will be visible on the personal task list of Gonzo
assertEquals(1, taskService
.createTaskQuery()
.taskAssignee(GONZO)
.count());
// But not on the personal task list of (for example) Kermit
assertEquals(0, taskService.createTaskQuery().taskAssignee(KERMIT).count());
// Completing the task ends the process
taskService.complete(task.getId());
assertNull("Process ended", activitiRule
.getRuntimeService()
.createProcessInstanceQuery()
.processInstanceId(processInstance.getId())
.singleResult());
}