use of org.jbpm.services.task.events.DefaultTaskEventListener in project jbpm by kiegroup.
the class PerProcessInstanceRuntimeManagerTest method testCreationOfSessionWithCustomTaskListener.
@Test
public void testCreationOfSessionWithCustomTaskListener() {
final List<Long> addedTasks = new ArrayList<Long>();
RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultInMemoryBuilder().userGroupCallback(userGroupCallback).entityManagerFactory(emf).addAsset(ResourceFactory.newClassPathResource("BPMN2-ScriptTask.bpmn2"), ResourceType.BPMN2).addAsset(ResourceFactory.newClassPathResource("BPMN2-UserTask.bpmn2"), ResourceType.BPMN2).registerableItemsFactory(new DefaultRegisterableItemsFactory() {
@Override
public List<TaskLifeCycleEventListener> getTaskListeners() {
List<TaskLifeCycleEventListener> listeners = super.getTaskListeners();
listeners.add(new DefaultTaskEventListener() {
@Override
public void afterTaskAddedEvent(TaskEvent event) {
addedTasks.add(event.getTask().getId());
}
});
return listeners;
}
}).get();
manager = RuntimeManagerFactory.Factory.get().newPerProcessInstanceRuntimeManager(environment);
assertNotNull(manager);
// ksession for process instance #1
// since there is no process instance yet we need to get new session
RuntimeEngine runtime = manager.getRuntimeEngine(ProcessInstanceIdContext.get());
KieSession ksession = runtime.getKieSession();
assertNotNull(ksession);
long ksession1Id = ksession.getIdentifier();
assertTrue(ksession1Id == 1);
// FIXME quick hack to overcome problems with same pi ids when not using persistence
ksession.startProcess("ScriptTask");
// ksession for process instance #2
// since there is no process instance yet we need to get new session
RuntimeEngine runtime2 = manager.getRuntimeEngine(ProcessInstanceIdContext.get());
KieSession ksession2 = runtime2.getKieSession();
assertNotNull(ksession2);
long ksession2Id = ksession2.getIdentifier();
assertTrue(ksession2Id == 2);
ProcessInstance pi1 = ksession.startProcess("UserTask");
ProcessInstance pi2 = ksession2.startProcess("UserTask");
// both processes started
assertEquals(ProcessInstance.STATE_ACTIVE, pi1.getState());
assertEquals(ProcessInstance.STATE_ACTIVE, pi2.getState());
runtime = manager.getRuntimeEngine(ProcessInstanceIdContext.get(pi1.getId()));
ksession = runtime.getKieSession();
assertEquals(ksession1Id, ksession.getIdentifier());
runtime2 = manager.getRuntimeEngine(ProcessInstanceIdContext.get(pi2.getId()));
ksession2 = runtime2.getKieSession();
assertEquals(ksession2Id, ksession2.getIdentifier());
assertEquals(2, addedTasks.size());
manager.close();
}
use of org.jbpm.services.task.events.DefaultTaskEventListener in project jbpm by kiegroup.
the class ExecutionErrorHandlingRuntimeManagerTest method testUserTaskFailure.
@SuppressWarnings("unchecked")
@Test
public void testUserTaskFailure() {
RuntimeEngine runtime1 = manager.getRuntimeEngine(ProcessInstanceIdContext.get());
KieSession ksession1 = runtime1.getKieSession();
assertNotNull(ksession1);
ksession1.startProcess("UserTaskWithRollback");
TaskService taskService = runtime1.getTaskService();
List<TaskSummary> tasks = taskService.getTasksAssignedAsPotentialOwner("john", "en-UK");
assertEquals(1, tasks.size());
long taskId = tasks.get(0).getId();
try {
((EventService<TaskLifeCycleEventListener>) taskService).registerTaskEventListener(new DefaultTaskEventListener() {
@Override
public void afterTaskStartedEvent(TaskEvent event) {
throw new TaskExecutionException("On purpose");
}
});
taskService.start(taskId, "john");
fail("Start task should fail due to broken script");
} catch (Throwable e) {
// expected
}
manager.disposeRuntimeEngine(runtime1);
ExecutionErrorManager errorManager = ((AbstractRuntimeManager) manager).getExecutionErrorManager();
ExecutionErrorStorage storage = errorManager.getStorage();
List<ExecutionError> errors = storage.list(0, 10);
assertNotNull(errors);
assertEquals(1, errors.size());
assertExecutionError(errors.get(0), "Task", "UserTaskWithRollback", "Hello");
}
use of org.jbpm.services.task.events.DefaultTaskEventListener in project jbpm by kiegroup.
the class LifeCycleBaseTest method testCompleteWithMergeOfResults.
@Test
public void testCompleteWithMergeOfResults() {
final Map<String, Object> outputsAfterCompletion = new HashMap<String, Object>();
// One potential owner, should go straight to state Reserved
String str = "(with (new Task()) { priority = 55, taskData = (with( new TaskData()) { } ), ";
str += "peopleAssignments = (with ( new PeopleAssignments() ) { potentialOwners = [new User('Bobba Fet'), new User('Darth Vader') ], businessAdministrators = [ new User('Administrator') ],}),";
str += "name = 'This is my task name' })";
((EventService<TaskLifeCycleEventListener>) taskService).registerTaskEventListener(new DefaultTaskEventListener() {
@Override
public void afterTaskCompletedEvent(TaskEvent event) {
Map<String, Object> outs = event.getTask().getTaskData().getTaskOutputVariables();
if (outs != null) {
outputsAfterCompletion.putAll(outs);
}
}
});
Task task = TaskFactory.evalTask(new StringReader(str));
taskService.addTask(task, new HashMap<String, Object>());
long taskId = task.getId();
taskService.start(taskId, "Darth Vader");
Task task1 = taskService.getTaskById(taskId);
assertEquals(Status.InProgress, task1.getTaskData().getStatus());
assertEquals("Darth Vader", task1.getTaskData().getActualOwner().getId());
task1 = taskService.getTaskById(taskId);
Map<String, Object> outputs = getTaskOutput(task1);
assertEquals(0, outputs.size());
taskService.complete(taskId, "Darth Vader", null);
task1 = taskService.getTaskById(taskId);
assertEquals(Status.Completed, task1.getTaskData().getStatus());
assertEquals("Darth Vader", task1.getTaskData().getActualOwner().getId());
outputs = getTaskOutput(task1);
assertEquals(0, outputs.size());
// now let's check what was actually given to listeners
assertEquals(0, outputsAfterCompletion.size());
}
use of org.jbpm.services.task.events.DefaultTaskEventListener in project jbpm by kiegroup.
the class LifeCycleBaseTest method testCompleteWithMergeOfResultsNoDataBeforeCompletion.
@Test
public void testCompleteWithMergeOfResultsNoDataBeforeCompletion() {
final Map<String, Object> outputsAfterCompletion = new HashMap<String, Object>();
// One potential owner, should go straight to state Reserved
String str = "(with (new Task()) { priority = 55, taskData = (with( new TaskData()) { } ), ";
str += "peopleAssignments = (with ( new PeopleAssignments() ) { potentialOwners = [new User('Bobba Fet'), new User('Darth Vader') ], businessAdministrators = [ new User('Administrator') ],}),";
str += "name = 'This is my task name' })";
((EventService<TaskLifeCycleEventListener>) taskService).registerTaskEventListener(new DefaultTaskEventListener() {
@Override
public void afterTaskCompletedEvent(TaskEvent event) {
outputsAfterCompletion.putAll(event.getTask().getTaskData().getTaskOutputVariables());
}
});
Task task = TaskFactory.evalTask(new StringReader(str));
taskService.addTask(task, new HashMap<String, Object>());
long taskId = task.getId();
taskService.start(taskId, "Darth Vader");
Task task1 = taskService.getTaskById(taskId);
assertEquals(Status.InProgress, task1.getTaskData().getStatus());
assertEquals("Darth Vader", task1.getTaskData().getActualOwner().getId());
task1 = taskService.getTaskById(taskId);
Map<String, Object> outputs = getTaskOutput(task1);
assertEquals(0, outputs.size());
Map<String, Object> params = new HashMap<String, Object>();
params.put("response", "let's do it");
params.put("feedback", "ok");
taskService.complete(taskId, "Darth Vader", params);
task1 = taskService.getTaskById(taskId);
assertEquals(Status.Completed, task1.getTaskData().getStatus());
assertEquals("Darth Vader", task1.getTaskData().getActualOwner().getId());
outputs = getTaskOutput(task1);
assertEquals(2, outputs.size());
assertEquals("let's do it", outputs.get("response"));
assertEquals("ok", outputs.get("feedback"));
// now let's check what was actually given to listeners
assertEquals(2, outputsAfterCompletion.size());
assertEquals("let's do it", outputsAfterCompletion.get("response"));
assertEquals("ok", outputsAfterCompletion.get("feedback"));
}
use of org.jbpm.services.task.events.DefaultTaskEventListener in project jbpm by kiegroup.
the class LifeCycleBaseTest method testCompleteWithMergeOfResultsOverrideAndAddAtCompletion.
@Test
public void testCompleteWithMergeOfResultsOverrideAndAddAtCompletion() {
final Map<String, Object> outputsAfterCompletion = new HashMap<String, Object>();
// One potential owner, should go straight to state Reserved
String str = "(with (new Task()) { priority = 55, taskData = (with( new TaskData()) { } ), ";
str += "peopleAssignments = (with ( new PeopleAssignments() ) { potentialOwners = [new User('Bobba Fet'), new User('Darth Vader') ], businessAdministrators = [ new User('Administrator') ],}),";
str += "name = 'This is my task name' })";
((EventService<TaskLifeCycleEventListener>) taskService).registerTaskEventListener(new DefaultTaskEventListener() {
@Override
public void afterTaskCompletedEvent(TaskEvent event) {
outputsAfterCompletion.putAll(event.getTask().getTaskData().getTaskOutputVariables());
}
});
Task task = TaskFactory.evalTask(new StringReader(str));
taskService.addTask(task, new HashMap<String, Object>());
long taskId = task.getId();
taskService.start(taskId, "Darth Vader");
Task task1 = taskService.getTaskById(taskId);
assertEquals(Status.InProgress, task1.getTaskData().getStatus());
assertEquals("Darth Vader", task1.getTaskData().getActualOwner().getId());
Map<String, Object> params = new HashMap<String, Object>();
params.put("response", "not sure");
taskService.addContent(taskId, params);
task1 = taskService.getTaskById(taskId);
Map<String, Object> outputs = getTaskOutput(task1);
assertEquals(1, outputs.size());
assertEquals("not sure", outputs.get("response"));
params.clear();
params.put("response", "let's do it");
params.put("feedback", "ok");
taskService.complete(taskId, "Darth Vader", params);
task1 = taskService.getTaskById(taskId);
assertEquals(Status.Completed, task1.getTaskData().getStatus());
assertEquals("Darth Vader", task1.getTaskData().getActualOwner().getId());
outputs = getTaskOutput(task1);
assertEquals(2, outputs.size());
assertEquals("let's do it", outputs.get("response"));
assertEquals("ok", outputs.get("feedback"));
// now let's check what was actually given to listeners
assertEquals(2, outputsAfterCompletion.size());
assertEquals("let's do it", outputsAfterCompletion.get("response"));
assertEquals("ok", outputsAfterCompletion.get("feedback"));
}
Aggregations