Search in sources :

Example 1 with EventService

use of org.kie.internal.task.api.EventService 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");
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) ExecutionError(org.kie.internal.runtime.error.ExecutionError) TaskService(org.kie.api.task.TaskService) AbstractRuntimeManager(org.jbpm.runtime.manager.impl.AbstractRuntimeManager) EventService(org.kie.internal.task.api.EventService) DefaultTaskEventListener(org.jbpm.services.task.events.DefaultTaskEventListener) TaskExecutionException(org.jbpm.services.task.exception.TaskExecutionException) ExecutionErrorManager(org.kie.internal.runtime.error.ExecutionErrorManager) TaskSummary(org.kie.api.task.model.TaskSummary) TaskEvent(org.kie.api.task.TaskEvent) ExecutionErrorStorage(org.kie.internal.runtime.error.ExecutionErrorStorage) KieSession(org.kie.api.runtime.KieSession) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest) Test(org.junit.Test)

Example 2 with EventService

use of org.kie.internal.task.api.EventService in project jbpm by kiegroup.

the class LifeCycleBaseTest method testCompleteWithContentAndVarListener.

protected void testCompleteWithContentAndVarListener(TaskLifeCycleEventListener listener) {
    // 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(listener);
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("input", "simple input");
    Task task = TaskFactory.evalTask(new StringReader(str));
    taskService.addTask(task, params);
    long taskId = task.getId();
    // start task
    taskService.start(taskId, "Darth Vader");
    Task task1 = taskService.getTaskById(taskId);
    assertEquals(Status.InProgress, task1.getTaskData().getStatus());
    assertEquals("Darth Vader", task1.getTaskData().getActualOwner().getId());
    // complete task with output
    params = new HashMap<String, Object>();
    params.put("content", "content");
    taskService.complete(taskId, "Darth Vader", params);
}
Also used : Task(org.kie.api.task.model.Task) InternalTask(org.kie.internal.task.api.model.InternalTask) HashMap(java.util.HashMap) StringReader(java.io.StringReader) EventService(org.kie.internal.task.api.EventService)

Example 3 with EventService

use of org.kie.internal.task.api.EventService 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());
}
Also used : Task(org.kie.api.task.model.Task) InternalTask(org.kie.internal.task.api.model.InternalTask) HashMap(java.util.HashMap) EventService(org.kie.internal.task.api.EventService) DefaultTaskEventListener(org.jbpm.services.task.events.DefaultTaskEventListener) TaskEvent(org.kie.api.task.TaskEvent) StringReader(java.io.StringReader) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 4 with EventService

use of org.kie.internal.task.api.EventService 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"));
}
Also used : Task(org.kie.api.task.model.Task) InternalTask(org.kie.internal.task.api.model.InternalTask) HashMap(java.util.HashMap) TaskEvent(org.kie.api.task.TaskEvent) StringReader(java.io.StringReader) EventService(org.kie.internal.task.api.EventService) DefaultTaskEventListener(org.jbpm.services.task.events.DefaultTaskEventListener) Test(org.junit.Test)

Example 5 with EventService

use of org.kie.internal.task.api.EventService in project jbpm by kiegroup.

the class AdminAPIsWithListenerTest method automaticCleanUpTest.

@SuppressWarnings("unchecked")
@Test
public void automaticCleanUpTest() throws Exception {
    createRuntimeManager("org/jbpm/test/functional/task/patient-appointment.bpmn");
    RuntimeEngine runtimeEngine = getRuntimeEngine();
    KieSession ksession = runtimeEngine.getKieSession();
    TaskService taskService = runtimeEngine.getTaskService();
    KnowledgeRuntimeLoggerFactory.newConsoleLogger(ksession);
    ksession.addEventListener(new TaskCleanUpProcessEventListener(taskService));
    // let check how many listeners we have
    assertEquals(2, ((EventService<TaskLifeCycleEventListener>) taskService).getTaskEventListeners().size());
    assertEquals(2, ((EventService<TaskLifeCycleEventListener>) taskService).getTaskEventListeners().size());
    logger.info("### Starting process ###");
    Map<String, Object> parameters = new HashMap<String, Object>();
    ProcessInstance process = ksession.startProcess("org.jbpm.PatientAppointment", parameters);
    // The process is in the first Human Task waiting for its completion
    assertEquals(ProcessInstance.STATE_ACTIVE, process.getState());
    // gets frontDesk's tasks
    List<TaskSummary> frontDeskTasks = taskService.getTasksAssignedAsPotentialOwner("frontDesk", "en-UK");
    assertEquals(1, frontDeskTasks.size());
    // doctor doesn't have any task
    List<TaskSummary> doctorTasks = taskService.getTasksAssignedAsPotentialOwner("doctor", "en-UK");
    Assert.assertTrue(doctorTasks.isEmpty());
    // manager doesn't have any task
    List<TaskSummary> managerTasks = taskService.getTasksAssignedAsPotentialOwner("manager", "en-UK");
    Assert.assertTrue(managerTasks.isEmpty());
    taskService.start(frontDeskTasks.get(0).getId(), "frontDesk");
    taskService.complete(frontDeskTasks.get(0).getId(), "frontDesk", null);
    // Now doctor has 1 task
    doctorTasks = taskService.getTasksAssignedAsPotentialOwner("doctor", "en-UK");
    assertEquals(1, doctorTasks.size());
    // No tasks for manager yet
    managerTasks = taskService.getTasksAssignedAsPotentialOwner("manager", "en-UK");
    Assert.assertTrue(managerTasks.isEmpty());
    taskService.start(doctorTasks.get(0).getId(), "doctor");
    taskService.complete(doctorTasks.get(0).getId(), "doctor", null);
    // tasks for manager
    managerTasks = taskService.getTasksAssignedAsPotentialOwner("manager", "en-UK");
    assertEquals(1, managerTasks.size());
    taskService.start(managerTasks.get(0).getId(), "manager");
    taskService.complete(managerTasks.get(0).getId(), "manager", null);
    // since persisted process instance is completed it should be null
    process = ksession.getProcessInstance(process.getId());
    Assert.assertNull(process);
    final EntityManager em = emfTasks.createEntityManager();
    assertEquals(0, em.createQuery("select t from TaskImpl t").getResultList().size());
    assertEquals(0, em.createQuery("select i from I18NTextImpl i").getResultList().size());
    assertEquals(0, em.createNativeQuery("select * from PeopleAssignments_BAs").getResultList().size());
    assertEquals(0, em.createNativeQuery("select * from PeopleAssignments_ExclOwners").getResultList().size());
    assertEquals(0, em.createNativeQuery("select * from PeopleAssignments_PotOwners").getResultList().size());
    assertEquals(0, em.createNativeQuery("select * from PeopleAssignments_Recipients").getResultList().size());
    assertEquals(0, em.createNativeQuery("select * from PeopleAssignments_Stakeholders").getResultList().size());
    assertEquals(0, em.createQuery("select c from ContentImpl c").getResultList().size());
    em.close();
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) HashMap(java.util.HashMap) InternalTaskService(org.kie.internal.task.api.InternalTaskService) TaskService(org.kie.api.task.TaskService) TaskCleanUpProcessEventListener(org.jbpm.services.task.admin.listener.TaskCleanUpProcessEventListener) EventService(org.kie.internal.task.api.EventService) EntityManager(javax.persistence.EntityManager) TaskSummary(org.kie.api.task.model.TaskSummary) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) Test(org.junit.Test)

Aggregations

EventService (org.kie.internal.task.api.EventService)16 Test (org.junit.Test)12 HashMap (java.util.HashMap)8 DefaultTaskEventListener (org.jbpm.services.task.events.DefaultTaskEventListener)7 TaskEvent (org.kie.api.task.TaskEvent)7 StringReader (java.io.StringReader)6 TaskLifeCycleEventListener (org.kie.api.task.TaskLifeCycleEventListener)6 Task (org.kie.api.task.model.Task)6 InternalTask (org.kie.internal.task.api.model.InternalTask)6 KieSession (org.kie.api.runtime.KieSession)5 RuntimeEngine (org.kie.api.runtime.manager.RuntimeEngine)5 TaskService (org.kie.api.task.TaskService)5 TaskSummary (org.kie.api.task.model.TaskSummary)5 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)4 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)4 WorkItemImpl (org.drools.core.process.instance.impl.WorkItemImpl)3 ArrayList (java.util.ArrayList)2 EntityManager (javax.persistence.EntityManager)2 AbstractRuntimeManager (org.jbpm.runtime.manager.impl.AbstractRuntimeManager)2 TaskExecutionException (org.jbpm.services.task.exception.TaskExecutionException)2