Search in sources :

Example 26 with TaskService

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

the class RuntimeManagerWithDescriptorTest method testDeployWithFullCustomDeploymentDescriptor.

@Test
public void testDeployWithFullCustomDeploymentDescriptor() throws Exception {
    Map<String, String> resources = new HashMap<String, String>();
    String scriptString = IOUtils.toString(this.getClass().getResourceAsStream("/BPMN2-ScriptTask.bpmn2"), "UTF-8");
    resources.put("src/main/resources/BPMN2-ScriptTask.bpmn2", scriptString);
    String manualString = IOUtils.toString(this.getClass().getResourceAsStream("/BPMN2-ManualTask.bpmn2"), "UTF-8");
    resources.put("src/main/resources/BPMN2-ManualTask.bpmn2", manualString);
    String userString = IOUtils.toString(this.getClass().getResourceAsStream("/BPMN2-UserTask.bpmn2"), "UTF-8");
    resources.put("src/main/resources/BPMN2-UserTask.bpmn2", userString);
    String callString = IOUtils.toString(this.getClass().getResourceAsStream("/BPMN2-CallActivity.bpmn2"), "UTF-8");
    resources.put("src/main/resources/BPMN2-CallActivity.bpmn2", callString);
    String subProcessString = IOUtils.toString(this.getClass().getResourceAsStream("/BPMN2-CallActivitySubProcess.bpmn2"), "UTF-8");
    resources.put("src/main/resources/BPMN2-CallActivitySubProcess.bpmn2", subProcessString);
    DeploymentDescriptor customDescriptor = new DeploymentDescriptorImpl("org.jbpm.persistence.jpa");
    customDescriptor.getBuilder().runtimeStrategy(RuntimeStrategy.PER_PROCESS_INSTANCE).addMarshalingStrategy(new ObjectModel("org.jbpm.runtime.manager.impl.deploy" + ".RuntimeManagerWithDescriptorTest$TestMarshallingStrategy")).addConfiguration(new NamedObjectModel("drools.processSignalManagerFactory", "java.lang.String", new Object[] { DefaultSignalManagerFactory.class.getName() })).addEnvironmentEntry(new NamedObjectModel("jbpm.business.calendar", "org.jbpm.runtime.manager.impl.deploy.RuntimeManagerWithDescriptorTest$TestBusinessCalendar")).addEventListener(new ObjectModel("org.jbpm.runtime.manager.impl.deploy" + ".RuntimeManagerWithDescriptorTest$TestProcessEventListener")).addGlobal(new NamedObjectModel("service", "java.util.ArrayList")).addTaskEventListener(new ObjectModel("org.jbpm.runtime.manager.impl.deploy" + ".RuntimeManagerWithDescriptorTest$TestTaskEventListener")).addWorkItemHandler(new NamedObjectModel("Manual Task", "org.jbpm.runtime.manager.impl.deploy" + ".RuntimeManagerWithDescriptorTest$TestWorkItemHandler"));
    resources.put("src/main/resources/" + DeploymentDescriptor.META_INF_LOCATION, customDescriptor.toXml());
    String drl = "package org.jbpm; global java.util.List service; " + "	rule \"Start Hello1\"" + "	  when" + "	  then" + "	    System.out.println(\"Hello\");" + "	end";
    resources.put("src/main/resources/simple.drl", drl);
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId = ks.newReleaseId("org.jbpm.test.dd", "-kjar-with-dd", "1.0.0");
    InternalKieModule kJar1 = createKieJar(ks, releaseId, resources);
    installKjar(releaseId, kJar1);
    RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder(releaseId).userGroupCallback(userGroupCallback).get();
    manager = RuntimeManagerFactory.Factory.get().newPerProcessInstanceRuntimeManager(environment);
    assertNotNull(manager);
    InternalRuntimeManager internalManager = (InternalRuntimeManager) manager;
    RegisterableItemsFactory factory = internalManager.getEnvironment().getRegisterableItemsFactory();
    assertNotNull(factory);
    assertTrue(factory instanceof InternalRegisterableItemsFactory);
    assertNotNull(((InternalRegisterableItemsFactory) factory).getRuntimeManager());
    DeploymentDescriptor descriptor = internalManager.getDeploymentDescriptor();
    assertNotNull(descriptor);
    assertEquals("org.jbpm.persistence.jpa", descriptor.getPersistenceUnit());
    assertEquals("org.jbpm.persistence.jpa", descriptor.getAuditPersistenceUnit());
    assertEquals(AuditMode.JPA, descriptor.getAuditMode());
    assertEquals(PersistenceMode.JPA, descriptor.getPersistenceMode());
    assertEquals(RuntimeStrategy.PER_PROCESS_INSTANCE, descriptor.getRuntimeStrategy());
    assertEquals(1, descriptor.getMarshallingStrategies().size());
    // assertEquals(1, descriptor.getConfiguration().size());
    assertEquals(1, descriptor.getEnvironmentEntries().size());
    assertEquals(1, descriptor.getEventListeners().size());
    assertEquals(1, descriptor.getGlobals().size());
    assertEquals(1, descriptor.getTaskEventListeners().size());
    assertEquals(1, descriptor.getWorkItemHandlers().size());
    assertEquals(0, descriptor.getRequiredRoles().size());
    RuntimeEngine engine = manager.getRuntimeEngine(ProcessInstanceIdContext.get());
    assertNotNull(engine);
    KieSession kieSession = engine.getKieSession();
    // configuration
    assertEquals(((SessionConfiguration) kieSession.getSessionConfiguration()).getSignalManagerFactory(), DefaultSignalManagerFactory.class.getName());
    BusinessCalendar bc = (BusinessCalendar) kieSession.getEnvironment().get("jbpm.business.calendar");
    assertNotNull(bc);
    assertTrue(bc instanceof TestBusinessCalendar);
    // globals
    Object service = kieSession.getGlobal("service");
    assertNotNull(service);
    assertTrue(service instanceof ArrayList);
    // work item handler
    ProcessInstance processInstance = kieSession.startProcess("ManualTask");
    long processInstanceId = processInstance.getId();
    assertNotNull(kieSession.getProcessInstance(processInstanceId));
    kieSession.getWorkItemManager().completeWorkItem(TestWorkItemHandler.getWorkItem().getId(), null);
    assertNull(kieSession.getProcessInstance(processInstanceId));
    manager.disposeRuntimeEngine(engine);
    engine = manager.getRuntimeEngine(ProcessInstanceIdContext.get());
    kieSession = engine.getKieSession();
    // process event listener
    assertArrayEquals(new String[] { "beforeProcessStarted", "afterProcessStarted", "beforeProcessCompleted", "afterProcessCompleted" }, processEvents.toArray());
    processEvents.clear();
    manager.disposeRuntimeEngine(engine);
    engine = manager.getRuntimeEngine(ProcessInstanceIdContext.get());
    kieSession = engine.getKieSession();
    // task event listener
    processInstance = kieSession.startProcess("UserTask");
    processInstanceId = processInstance.getId();
    assertNotNull(kieSession.getProcessInstance(processInstanceId));
    manager.disposeRuntimeEngine(engine);
    engine = manager.getRuntimeEngine(ProcessInstanceIdContext.get(processInstanceId));
    TaskService taskService = engine.getTaskService();
    List<TaskSummary> tasks = taskService.getTasksAssignedAsPotentialOwner("john", "en-UK");
    long taskId = tasks.get(0).getId();
    taskService.start(taskId, "john");
    taskService.complete(taskId, "john", null);
    assertArrayEquals(new String[] { "beforeTaskAddedEvent", "afterTaskAddedEvent", "beforeTaskStartedEvent", "afterTaskStartedEvent", "beforeTaskCompletedEvent", "afterTaskCompletedEvent" }, taskEvents.toArray());
    manager.disposeRuntimeEngine(engine);
    engine = manager.getRuntimeEngine(ProcessInstanceIdContext.get());
    kieSession = engine.getKieSession();
    // marshalling strategy
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("x", "marshal");
    processInstance = kieSession.startProcess("ParentProcess", params);
    processInstanceId = processInstance.getId();
    ProcessInstance pi = kieSession.getProcessInstance(processInstanceId);
    assertNotNull(pi);
    String varX = (String) ((WorkflowProcessInstance) pi).getVariable("x");
    assertEquals(TestMarshallingStrategy.ALWAYS_RESPOND_WITH, varX);
    manager.disposeRuntimeEngine(engine);
}
Also used : ObjectModel(org.kie.internal.runtime.conf.ObjectModel) NamedObjectModel(org.kie.internal.runtime.conf.NamedObjectModel) InternalRuntimeManager(org.kie.internal.runtime.manager.InternalRuntimeManager) HashMap(java.util.HashMap) DeploymentDescriptor(org.kie.internal.runtime.conf.DeploymentDescriptor) DefaultSignalManagerFactory(org.jbpm.process.instance.event.DefaultSignalManagerFactory) ArrayList(java.util.ArrayList) InternalRegisterableItemsFactory(org.kie.internal.runtime.manager.InternalRegisterableItemsFactory) RegisterableItemsFactory(org.kie.api.runtime.manager.RegisterableItemsFactory) KieServices(org.kie.api.KieServices) InternalRegisterableItemsFactory(org.kie.internal.runtime.manager.InternalRegisterableItemsFactory) KieSession(org.kie.api.runtime.KieSession) RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) RuntimeEnvironment(org.kie.api.runtime.manager.RuntimeEnvironment) TaskService(org.kie.api.task.TaskService) ReleaseId(org.kie.api.builder.ReleaseId) NamedObjectModel(org.kie.internal.runtime.conf.NamedObjectModel) BusinessCalendar(org.jbpm.process.core.timer.BusinessCalendar) TaskSummary(org.kie.api.task.model.TaskSummary) WorkflowProcessInstance(org.jbpm.workflow.instance.WorkflowProcessInstance) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) Test(org.junit.Test)

Example 27 with TaskService

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

the class ExecutionErrorHandlingRuntimeManagerTest method testScriptFailureAfterUserTask.

@Test
public void testScriptFailureAfterUserTask() {
    RuntimeEngine runtime1 = manager.getRuntimeEngine(ProcessInstanceIdContext.get());
    KieSession ksession1 = runtime1.getKieSession();
    assertNotNull(ksession1);
    ProcessInstance pi = ksession1.startProcess("UserTaskWithRollback");
    manager.disposeRuntimeEngine(runtime1);
    runtime1 = manager.getRuntimeEngine(ProcessInstanceIdContext.get(pi.getId()));
    ksession1 = runtime1.getKieSession();
    TaskService taskService = runtime1.getTaskService();
    List<TaskSummary> tasks = taskService.getTasksAssignedAsPotentialOwner("john", "en-UK");
    assertEquals(1, tasks.size());
    long taskId = tasks.get(0).getId();
    taskService.start(taskId, "john");
    Map<String, Object> results = new HashMap<>();
    results.put("output1", "rollback");
    try {
        taskService.complete(taskId, "john", results);
        fail("Complete 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), "Process", "UserTaskWithRollback", "Script Task 1");
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) ExecutionError(org.kie.internal.runtime.error.ExecutionError) HashMap(java.util.HashMap) TaskService(org.kie.api.task.TaskService) AbstractRuntimeManager(org.jbpm.runtime.manager.impl.AbstractRuntimeManager) ExecutionErrorManager(org.kie.internal.runtime.error.ExecutionErrorManager) TaskSummary(org.kie.api.task.model.TaskSummary) ExecutionErrorStorage(org.kie.internal.runtime.error.ExecutionErrorStorage) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest) Test(org.junit.Test)

Example 28 with TaskService

use of org.kie.api.task.TaskService 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 29 with TaskService

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

the class MigrationManagerTest method testRemoveNonActiveBeforeActiveTask.

@Test
public void testRemoveNonActiveBeforeActiveTask() {
    createRuntimeManagers("migration/v1/RemoveNonActiveBeforeTask-v1.bpmn2", "migration/v2/RemoveNonActiveBeforeTask-v2.bpmn2");
    assertNotNull(managerV1);
    assertNotNull(managerV2);
    RuntimeEngine runtime = managerV1.getRuntimeEngine(EmptyContext.get());
    KieSession ksession = runtime.getKieSession();
    assertNotNull(ksession);
    ProcessInstance pi1 = ksession.startProcess(REMOVENONACTIVEBEFORETASK_ID_V1);
    assertNotNull(pi1);
    assertEquals(ProcessInstance.STATE_ACTIVE, pi1.getState());
    assertTaskAndComplete(runtime.getTaskService(), REMOVENONACTIVEBEFORETASK_ID_V1, pi1.getId(), "Active Task");
    managerV1.disposeRuntimeEngine(runtime);
    MigrationSpec migrationSpec = new MigrationSpec(DEPLOYMENT_ID_V1, pi1.getId(), DEPLOYMENT_ID_V2, REMOVENONACTIVEBEFORETASK_ID_V2);
    MigrationManager migrationManager = new MigrationManager(migrationSpec);
    MigrationReport report = migrationManager.migrate();
    assertNotNull(report);
    assertTrue(report.isSuccessful());
    assertMigratedProcessInstance(REMOVENONACTIVEBEFORETASK_ID_V2, pi1.getId(), ProcessInstance.STATE_ACTIVE);
    runtime = managerV2.getRuntimeEngine(EmptyContext.get());
    TaskService taskService = runtime.getTaskService();
    assertMigratedTaskAndComplete(taskService, REMOVENONACTIVEBEFORETASK_ID_V2, pi1.getId(), "Non-active Task");
    assertMigratedProcessInstance(REMOVENONACTIVEBEFORETASK_ID_V2, pi1.getId(), ProcessInstance.STATE_COMPLETED);
    managerV2.disposeRuntimeEngine(runtime);
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) TaskService(org.kie.api.task.TaskService) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 30 with TaskService

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

the class MigrationManagerTest method testMigrateUserTaskProcessInstance.

@Test
public void testMigrateUserTaskProcessInstance() {
    createRuntimeManagers("migration/v1/BPMN2-UserTask-v1.bpmn2", "migration/v2/BPMN2-UserTask-v2.bpmn2");
    assertNotNull(managerV1);
    assertNotNull(managerV2);
    RuntimeEngine runtime = managerV1.getRuntimeEngine(EmptyContext.get());
    KieSession ksession = runtime.getKieSession();
    assertNotNull(ksession);
    ProcessInstance pi1 = ksession.startProcess(PROCESS_ID_V1);
    assertNotNull(pi1);
    assertEquals(ProcessInstance.STATE_ACTIVE, pi1.getState());
    JPAAuditLogService auditService = new JPAAuditLogService(emf);
    ProcessInstanceLog log = auditService.findProcessInstance(pi1.getId());
    assertNotNull(log);
    assertEquals(PROCESS_ID_V1, log.getProcessId());
    assertEquals(PROCESS_NAME_V1, log.getProcessName());
    assertEquals(DEPLOYMENT_ID_V1, log.getExternalId());
    TaskService taskService = runtime.getTaskService();
    List<TaskSummary> tasks = taskService.getTasksAssignedAsPotentialOwner(USER_JOHN, "en-UK");
    assertNotNull(tasks);
    assertEquals(1, tasks.size());
    TaskSummary task = tasks.get(0);
    assertNotNull(task);
    assertEquals(PROCESS_ID_V1, task.getProcessId());
    assertEquals(DEPLOYMENT_ID_V1, task.getDeploymentId());
    assertEquals(TASK_NAME_V1, task.getName());
    managerV1.disposeRuntimeEngine(runtime);
    MigrationSpec migrationSpec = new MigrationSpec(DEPLOYMENT_ID_V1, pi1.getId(), DEPLOYMENT_ID_V2, PROCESS_ID_V2);
    MigrationManager migrationManager = new MigrationManager(migrationSpec);
    MigrationReport report = migrationManager.migrate();
    assertNotNull(report);
    assertTrue(report.isSuccessful());
    log = auditService.findProcessInstance(pi1.getId());
    assertNotNull(log);
    assertEquals(PROCESS_ID_V2, log.getProcessId());
    assertEquals(PROCESS_NAME_V2, log.getProcessName());
    assertEquals(DEPLOYMENT_ID_V2, log.getExternalId());
    auditService.dispose();
    runtime = managerV2.getRuntimeEngine(EmptyContext.get());
    taskService = runtime.getTaskService();
    tasks = taskService.getTasksAssignedAsPotentialOwner(USER_JOHN, "en-UK");
    assertNotNull(tasks);
    assertEquals(1, tasks.size());
    task = tasks.get(0);
    assertNotNull(task);
    assertEquals(PROCESS_ID_V2, task.getProcessId());
    assertEquals(DEPLOYMENT_ID_V2, task.getDeploymentId());
    // same name as the node mapping was not given
    assertEquals(TASK_NAME_V2, task.getName());
    managerV2.disposeRuntimeEngine(runtime);
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) TaskService(org.kie.api.task.TaskService) TaskSummary(org.kie.api.task.model.TaskSummary) JPAAuditLogService(org.jbpm.process.audit.JPAAuditLogService) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) ProcessInstanceLog(org.kie.api.runtime.manager.audit.ProcessInstanceLog) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

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