Search in sources :

Example 56 with TaskService

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

the class ProcessPersistenceHumanTaskTest method testTransactions.

@Test
public void testTransactions() throws Exception {
    createRuntimeManager("org/jbpm/test/functional/task/humantask.bpmn");
    RuntimeEngine runtimeEngine = getRuntimeEngine();
    KieSession ksession = runtimeEngine.getKieSession();
    TaskService taskService = runtimeEngine.getTaskService();
    long processId;
    UserTransaction ut = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
    try {
        ut.begin();
        ProcessInstance processInstance = ksession.startProcess("com.sample.bpmn.hello");
        processId = processInstance.getId();
    } finally {
        ut.rollback();
    }
    assertNull(ksession.getProcessInstance(processId));
    List<TaskSummary> list = taskService.getTasksAssignedAsPotentialOwner("john", "en-UK");
    assertEquals(0, list.size());
}
Also used : UserTransaction(javax.transaction.UserTransaction) RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) TaskService(org.kie.api.task.TaskService) TaskSummary(org.kie.api.task.model.TaskSummary) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) InitialContext(javax.naming.InitialContext) Test(org.junit.Test)

Example 57 with TaskService

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

the class SerializedTimerRollbackTest method testSerizliableTestsWithExternalRollback.

@Test
public void testSerizliableTestsWithExternalRollback() {
    try {
        createRuntimeManager("org/jbpm/test/functional/timer/HumanTaskWithBoundaryTimer.bpmn");
        RuntimeEngine runtimeEngine = getRuntimeEngine();
        KieSession ksession = runtimeEngine.getKieSession();
        TaskService taskService = runtimeEngine.getTaskService();
        logger.debug("Created knowledge session");
        TransactionManager tm = com.arjuna.ats.jta.TransactionManager.transactionManager();
        List<Long> committedProcessInstanceIds = new ArrayList<Long>();
        for (int i = 0; i < 10; i++) {
            tm.begin();
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("test", "john");
            logger.debug("Creating process instance: {}", i);
            ProcessInstance pi = ksession.startProcess("PROCESS_1", params);
            if (i % 2 == 0) {
                committedProcessInstanceIds.add(pi.getId());
                tm.commit();
            } else {
                tm.rollback();
            }
        }
        Connection c = getDs().getConnection();
        Statement st = c.createStatement();
        ResultSet rs = st.executeQuery("select rulesbytearray from sessioninfo");
        rs.next();
        Blob b = rs.getBlob("rulesbytearray");
        assertNotNull(b);
        KnowledgeBuilder builder = KnowledgeBuilderFactory.newKnowledgeBuilder();
        ProtobufMarshaller marshaller = new ProtobufMarshaller(builder.newKieBase(), new MarshallingConfigurationImpl());
        StatefulKnowledgeSession session = marshaller.unmarshall(b.getBinaryStream());
        assertNotNull(session);
        TimerManager timerManager = ((InternalProcessRuntime) ((InternalKnowledgeRuntime) session).getProcessRuntime()).getTimerManager();
        assertNotNull(timerManager);
        Collection<TimerInstance> timers = timerManager.getTimers();
        assertNotNull(timers);
        assertEquals(5, timers.size());
        for (TimerInstance timerInstance : timers) {
            assertTrue(committedProcessInstanceIds.contains(timerInstance.getProcessInstanceId()));
            ksession.abortProcessInstance(timerInstance.getProcessInstanceId());
        }
        List<TaskSummary> tasks = taskService.getTasksAssignedAsPotentialOwner("john", "en-UK");
        assertEquals(0, tasks.size());
    } catch (Exception e) {
        e.printStackTrace();
        fail("Exception thrown");
    }
}
Also used : ProtobufMarshaller(org.drools.core.marshalling.impl.ProtobufMarshaller) HashMap(java.util.HashMap) TimerInstance(org.jbpm.process.instance.timer.TimerInstance) ArrayList(java.util.ArrayList) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ResultSet(java.sql.ResultSet) KieSession(org.kie.api.runtime.KieSession) MarshallingConfigurationImpl(org.drools.core.marshalling.impl.MarshallingConfigurationImpl) RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) Blob(java.sql.Blob) TaskService(org.kie.api.task.TaskService) Statement(java.sql.Statement) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) Connection(java.sql.Connection) TimerManager(org.jbpm.process.instance.timer.TimerManager) TransactionManager(javax.transaction.TransactionManager) TaskSummary(org.kie.api.task.model.TaskSummary) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) InternalProcessRuntime(org.jbpm.process.instance.InternalProcessRuntime) Test(org.junit.Test)

Example 58 with TaskService

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

the class WorkitemAssignmentTest method testWorkitemAssignment.

@Test
public void testWorkitemAssignment() {
    manager = createRuntimeManager("org/jbpm/test/functional/workitem/workitemAssignmentTest.bpmn2");
    RuntimeEngine runtimeEngine = getRuntimeEngine(EmptyContext.get());
    KieSession kieSession = runtimeEngine.getKieSession();
    WorkItemManager workItemManager = (org.drools.core.process.instance.WorkItemManager) kieSession.getWorkItemManager();
    workItemManager.registerWorkItemHandler("SampleUserWorkitem", new UserAssignmentWorkitemHandler());
    Map<String, Object> initEmptyVars = new HashMap<String, Object>();
    initEmptyVars.put("firstName", "initValue");
    initEmptyVars.put("lastName", "initValue");
    ProcessInstance pi = kieSession.startProcess("workitemassignmenttest", initEmptyVars);
    // values should be from initial state when process instance started -- "initValue", "initValue"
    String varFirstNameOrig = (String) ((WorkflowProcessInstance) pi).getVariable("firstName");
    String varLasttNameOrig = (String) ((WorkflowProcessInstance) pi).getVariable("lastName");
    assertEquals("initValue", varFirstNameOrig);
    assertEquals("initValue", varLasttNameOrig);
    // advance process execution past first user task
    // it will then go through the custom workitem and
    // get into the second user task
    TaskService taskService = runtimeEngine.getTaskService();
    assertProcessInstanceActive(pi.getId());
    assertNodeTriggered(pi.getId(), "myFirstUserTask");
    assertNodeActive(pi.getId(), kieSession, "myFirstUserTask");
    List<TaskSummary> firstTasks = taskService.getTasksAssignedAsPotentialOwner("john", "en-UK");
    assertEquals(1, firstTasks.size());
    TaskSummary firstTaskSummary = firstTasks.get(0);
    taskService.start(firstTaskSummary.getId(), "john");
    taskService.complete(firstTaskSummary.getId(), "john", null);
    // get process instance again to get updated process var values which were set
    // by the assignments in custom workitem handler
    ProcessInstance processInstance = kieSession.getProcessInstance(pi.getId());
    String varFirstName = (String) ((WorkflowProcessInstance) processInstance).getVariable("firstName");
    String varLasttName = (String) ((WorkflowProcessInstance) processInstance).getVariable("lastName");
    assertNotNull(varFirstName);
    assertNotNull(varLasttName);
    assertEquals(UserAssignmentWorkitemHandler.fnameStr, varFirstName);
    assertEquals(UserAssignmentWorkitemHandler.lnameStr, varLasttName);
    // make sure we passed the custom workitem
    assertNodeTriggered(pi.getId(), "SampleUserWorkitem");
    assertNodeActive(pi.getId(), kieSession, "mySecondUserTask");
    List<TaskSummary> secondTasks = taskService.getTasksAssignedAsPotentialOwner("john", "en-UK");
    assertEquals(1, secondTasks.size());
    // advance paste the second user task to complete the process instance
    TaskSummary secondTaskSummary = secondTasks.get(0);
    taskService.start(secondTaskSummary.getId(), "john");
    taskService.complete(secondTaskSummary.getId(), "john", null);
    assertProcessInstanceCompleted(processInstance.getId());
    // now we can check audit service to see process vars
    // and how they chaged throughout process execution
    AuditService auditService = runtimeEngine.getAuditService();
    List<? extends VariableInstanceLog> firstNameVars = auditService.findVariableInstances(processInstance.getId(), "firstName");
    List<? extends VariableInstanceLog> lastNameVars = auditService.findVariableInstances(processInstance.getId(), "lastName");
    assertNotNull(firstNameVars);
    assertNotNull(lastNameVars);
    assertEquals(2, firstNameVars.size());
    assertEquals("initValue", firstNameVars.get(0).getValue());
    assertEquals(UserAssignmentWorkitemHandler.fnameStr, firstNameVars.get(1).getValue());
    assertEquals(2, lastNameVars.size());
    assertEquals("initValue", lastNameVars.get(0).getValue());
    assertEquals(UserAssignmentWorkitemHandler.lnameStr, lastNameVars.get(1).getValue());
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) HashMap(java.util.HashMap) TaskService(org.kie.api.task.TaskService) TaskSummary(org.kie.api.task.model.TaskSummary) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) WorkflowProcessInstance(org.kie.api.runtime.process.WorkflowProcessInstance) WorkItemManager(org.drools.core.process.instance.WorkItemManager) AuditService(org.kie.api.runtime.manager.audit.AuditService) Test(org.junit.Test)

Example 59 with TaskService

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

the class TimerEventTest method testCancelledTimerNotScheduled.

@Test
@BZ("1148304")
public void testCancelledTimerNotScheduled() {
    for (int i = 0; i < 5; i++) {
        createRuntimeManager(Strategy.PROCESS_INSTANCE, (String) null, CANCELLED_TIMER);
        KieSession ksession = getRuntimeEngine().getKieSession();
        TaskService taskService = getRuntimeEngine().getTaskService();
        Map<String, Object> params = new HashMap<String, Object>();
        ProcessInstance pi = ksession.startProcess(CANCELLED_TIMER_ID, params);
        System.out.println("A process instance started : pid = " + pi.getId());
        List<Long> list = taskService.getTasksByProcessInstanceId(pi.getId());
        for (long taskId : list) {
            Task task = taskService.getTaskById(taskId);
            System.out.println("taskId = " + task.getId() + ", status = " + task.getTaskData().getStatus());
        }
        Date before = new Date();
        disposeRuntimeManager();
        // Check if engine did not waited for timer
        Date after = new Date();
        long seconds = (after.getTime() - before.getTime()) / 1000;
        Assertions.assertThat(seconds).as("Cancelled timer has been scheduled").isLessThan(5);
    }
}
Also used : Task(org.kie.api.task.model.Task) HashMap(java.util.HashMap) TaskService(org.kie.api.task.TaskService) Date(java.util.Date) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) Test(org.junit.Test) BZ(qa.tools.ikeeper.annotation.BZ)

Example 60 with TaskService

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

the class HumanTaskTest method testOnExitScriptException.

@Test
@BZ("1120122")
public void testOnExitScriptException() {
    createRuntimeManager(ON_EXIT_SCRIPT_EXCEPTION);
    KieSession ksession = getRuntimeEngine().getKieSession();
    TaskService taskService = getRuntimeEngine().getTaskService();
    long pid = ksession.startProcess(ON_EXIT_SCRIPT_EXCEPTION_ID).getId();
    List<Long> tasks = taskService.getTasksByProcessInstanceId(pid);
    Assertions.assertThat(tasks).hasSize(1);
    long taskId = tasks.get(0);
    taskService.start(taskId, "john");
    taskService.complete(taskId, "john", null);
    tasks = taskService.getTasksByProcessInstanceId(pid);
    Assertions.assertThat(tasks).hasSize(2);
    Task task1 = taskService.getTaskById(Math.min(tasks.get(0), tasks.get(1)));
    Assertions.assertThat(task1.getNames().get(0).getText()).isEqualTo("Human task 1");
    Assertions.assertThat(task1.getTaskData().getStatus()).isEqualTo(Status.Completed);
    Task task2 = taskService.getTaskById(Math.max(tasks.get(0), tasks.get(1)));
    Assertions.assertThat(task2.getNames().get(0).getText()).isEqualTo("Human task 2");
    Assertions.assertThat(task2.getTaskData().getStatus()).isEqualTo(Status.Reserved);
}
Also used : Task(org.kie.api.task.model.Task) TaskService(org.kie.api.task.TaskService) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test) BZ(qa.tools.ikeeper.annotation.BZ)

Aggregations

TaskService (org.kie.api.task.TaskService)129 RuntimeEngine (org.kie.api.runtime.manager.RuntimeEngine)111 KieSession (org.kie.api.runtime.KieSession)79 Test (org.junit.Test)73 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)70 TaskSummary (org.kie.api.task.model.TaskSummary)66 RuntimeManager (org.kie.api.runtime.manager.RuntimeManager)53 InternalTaskService (org.kie.internal.task.api.InternalTaskService)52 HashMap (java.util.HashMap)41 InternalRuntimeManager (org.kie.internal.runtime.manager.InternalRuntimeManager)39 UserTaskService (org.jbpm.services.api.UserTaskService)38 UserTaskInstanceDesc (org.jbpm.services.api.model.UserTaskInstanceDesc)34 ArrayList (java.util.ArrayList)17 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)17 TaskNotFoundException (org.jbpm.services.api.TaskNotFoundException)15 PermissionDeniedException (org.jbpm.services.task.exception.PermissionDeniedException)15 Task (org.kie.api.task.model.Task)14 RuntimeEnvironment (org.kie.api.runtime.manager.RuntimeEnvironment)12 BZ (qa.tools.ikeeper.annotation.BZ)10 TaskCleanUpProcessEventListener (org.jbpm.services.task.admin.listener.TaskCleanUpProcessEventListener)9