Search in sources :

Example 36 with QueryContext

use of org.kie.api.runtime.query.QueryContext in project jbpm by kiegroup.

the class CaseSLAComplianceTest method testStartCaseWithSLANotification.

@Test
public void testStartCaseWithSLANotification() {
    Map<String, OrganizationalEntity> roleAssignments = new HashMap<>();
    roleAssignments.put("owner", new UserImpl("john"));
    roleAssignments.put("admin", new UserImpl("mary"));
    Map<String, Object> data = new HashMap<>();
    data.put("s", "Case with SLA");
    CaseFileInstance caseFile = caseService.newCaseFileInstance(deploymentUnit.getIdentifier(), USER_TASK_SLA_CASE_P_ID, data, roleAssignments);
    String caseId = caseService.startCase(deploymentUnit.getIdentifier(), USER_TASK_SLA_CASE_P_ID, caseFile);
    assertNotNull(caseId);
    assertEquals(HR_CASE_ID, caseId);
    try {
        CaseInstance cInstance = caseService.getCaseInstance(caseId);
        assertNotNull(cInstance);
        assertEquals(deploymentUnit.getIdentifier(), cInstance.getDeploymentId());
        assertEquals(ProcessInstance.SLA_PENDING, cInstance.getSlaCompliance().intValue());
        Collection<NodeInstanceDesc> activeNodes = caseRuntimeDataService.getActiveNodesForCase(caseId, new QueryContext());
        assertThat(activeNodes).hasSize(1);
        Iterator<NodeInstanceDesc> it = activeNodes.iterator();
        NodeInstanceDesc active = it.next();
        assertThat(active.getName()).isEqualTo("Hello1");
        CountDownListenerFactory.getExisting("slaCompliance").waitTillCompleted();
        cInstance = caseService.getCaseInstance(caseId);
        assertNotNull(cInstance);
        assertEquals(deploymentUnit.getIdentifier(), cInstance.getDeploymentId());
        assertEquals(ProcessInstance.SLA_VIOLATED, cInstance.getSlaCompliance().intValue());
        activeNodes = caseRuntimeDataService.getActiveNodesForCase(caseId, new QueryContext());
        assertThat(activeNodes).hasSize(2);
        it = activeNodes.iterator();
        active = it.next();
        assertThat(active.getName()).isEqualTo("Hello1");
        active = it.next();
        assertThat(active.getName()).isEqualTo("[Dynamic] SLA Violation for case " + caseId);
        caseService.cancelCase(caseId);
        CaseInstance instance = caseService.getCaseInstance(caseId);
        Assertions.assertThat(instance.getStatus()).isEqualTo(CaseStatus.CANCELLED.getId());
        caseId = null;
    } catch (Exception e) {
        logger.error("Unexpected error {}", e.getMessage(), e);
        fail("Unexpected exception " + e.getMessage());
    } finally {
        if (caseId != null) {
            caseService.cancelCase(caseId);
        }
    }
}
Also used : OrganizationalEntity(org.kie.api.task.model.OrganizationalEntity) HashMap(java.util.HashMap) QueryContext(org.kie.api.runtime.query.QueryContext) CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) CaseInstance(org.jbpm.casemgmt.api.model.instance.CaseInstance) UserImpl(org.jbpm.services.task.impl.model.UserImpl) NodeInstanceDesc(org.jbpm.services.api.model.NodeInstanceDesc) AbstractCaseServicesBaseTest(org.jbpm.casemgmt.impl.util.AbstractCaseServicesBaseTest) Test(org.junit.Test)

Example 37 with QueryContext

use of org.kie.api.runtime.query.QueryContext in project jbpm by kiegroup.

the class CaseServiceImplTest method testAddUserTaskToEmptyCase.

@Test
public void testAddUserTaskToEmptyCase() {
    Map<String, Object> data = new HashMap<>();
    data.put("name", "my first case");
    CaseFileInstance caseFile = caseService.newCaseFileInstance(deploymentUnit.getIdentifier(), EMPTY_CASE_P_ID, data);
    String caseId = caseService.startCase(deploymentUnit.getIdentifier(), EMPTY_CASE_P_ID, caseFile);
    assertNotNull(caseId);
    assertEquals(FIRST_CASE_ID, caseId);
    try {
        CaseInstance cInstance = caseService.getCaseInstance(caseId);
        assertNotNull(cInstance);
        assertEquals(FIRST_CASE_ID, cInstance.getCaseId());
        assertEquals(deploymentUnit.getIdentifier(), cInstance.getDeploymentId());
        // add dynamic user task to empty case instance - first by case id
        Map<String, Object> parameters = new HashMap<>();
        parameters.put("variable", "#{name}");
        caseService.addDynamicTask(FIRST_CASE_ID, caseService.newHumanTaskSpec("First task", "test", "john", null, parameters));
        List<TaskSummary> tasks = runtimeDataService.getTasksAssignedAsPotentialOwner("john", new QueryFilter());
        assertNotNull(tasks);
        assertEquals(1, tasks.size());
        TaskSummary task = tasks.get(0);
        assertTask(task, "john", "First task", Status.Reserved);
        assertEquals("test", task.getDescription());
        Map<String, Object> inputs = userTaskService.getTaskInputContentByTaskId(task.getId());
        assertNotNull(inputs);
        assertEquals("my first case", inputs.get("variable"));
        String nameVar = (String) processService.getProcessInstanceVariable(task.getProcessInstanceId(), "name");
        assertNotNull(nameVar);
        assertEquals("my first case", nameVar);
        userTaskService.start(task.getId(), "john");
        Map<String, Object> outcome = new HashMap<>();
        outcome.put("name", "updated by dynamic task");
        userTaskService.complete(task.getId(), "john", outcome);
        nameVar = (String) processService.getProcessInstanceVariable(task.getProcessInstanceId(), "name");
        assertNotNull(nameVar);
        assertEquals("updated by dynamic task", nameVar);
        // second task add by process instance id
        Collection<ProcessInstanceDesc> caseProcessInstances = caseRuntimeDataService.getProcessInstancesForCase(caseId, new QueryContext());
        assertNotNull(caseProcessInstances);
        assertEquals(1, caseProcessInstances.size());
        ProcessInstanceDesc casePI = caseProcessInstances.iterator().next();
        assertNotNull(casePI);
        assertEquals(FIRST_CASE_ID, casePI.getCorrelationKey());
        caseService.addDynamicTask(casePI.getId(), caseService.newHumanTaskSpec("Second task", "another test", "mary", null, parameters));
        tasks = runtimeDataService.getTasksAssignedAsPotentialOwner("mary", new QueryFilter());
        assertNotNull(tasks);
        assertEquals(1, tasks.size());
        task = tasks.get(0);
        assertTask(task, "mary", "Second task", Status.Reserved);
        assertEquals("another test", task.getDescription());
        // User john cannot work with task assigned to mary
        try {
            userTaskService.start(task.getId(), "john");
        } catch (TaskNotFoundException e) {
        // expected
        }
        userTaskService.start(task.getId(), "mary");
        userTaskService.complete(task.getId(), "mary", null);
        Collection<NodeInstanceDesc> nodes = runtimeDataService.getProcessInstanceHistoryCompleted(casePI.getId(), new QueryContext());
        assertNotNull(nodes);
        assertEquals(4, nodes.size());
        Map<String, String> nodesByName = nodes.stream().collect(toMap(NodeInstanceDesc::getName, NodeInstanceDesc::getNodeType));
        assertTrue(nodesByName.containsKey("StartProcess"));
        assertTrue(nodesByName.containsKey("EndProcess"));
        assertTrue(nodesByName.containsKey("[Dynamic] First task"));
        assertTrue(nodesByName.containsKey("[Dynamic] Second task"));
        assertEquals("StartNode", nodesByName.get("StartProcess"));
        assertEquals("EndNode", nodesByName.get("EndProcess"));
        assertEquals("Human Task", nodesByName.get("[Dynamic] First task"));
        assertEquals("Human Task", nodesByName.get("[Dynamic] Second task"));
    } catch (Exception e) {
        logger.error("Unexpected error {}", e.getMessage(), e);
        fail("Unexpected exception " + e.getMessage());
    } finally {
        if (caseId != null) {
            caseService.cancelCase(caseId);
        }
    }
}
Also used : HashMap(java.util.HashMap) ProcessInstanceDesc(org.jbpm.services.api.model.ProcessInstanceDesc) QueryContext(org.kie.api.runtime.query.QueryContext) CaseCommentNotFoundException(org.jbpm.casemgmt.api.CaseCommentNotFoundException) AdHocFragmentNotFoundException(org.jbpm.casemgmt.api.AdHocFragmentNotFoundException) CaseNotFoundException(org.jbpm.casemgmt.api.CaseNotFoundException) TaskNotFoundException(org.jbpm.services.api.TaskNotFoundException) CaseActiveException(org.jbpm.casemgmt.api.CaseActiveException) CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) CaseInstance(org.jbpm.casemgmt.api.model.instance.CaseInstance) QueryFilter(org.kie.internal.query.QueryFilter) TaskNotFoundException(org.jbpm.services.api.TaskNotFoundException) TaskSummary(org.kie.api.task.model.TaskSummary) NodeInstanceDesc(org.jbpm.services.api.model.NodeInstanceDesc) AbstractCaseServicesBaseTest(org.jbpm.casemgmt.impl.util.AbstractCaseServicesBaseTest) Test(org.junit.Test)

Example 38 with QueryContext

use of org.kie.api.runtime.query.QueryContext in project jbpm by kiegroup.

the class CaseServiceImplTest method testAddServiceTaskToEmptyCase.

@Test
public void testAddServiceTaskToEmptyCase() {
    Map<String, Object> data = new HashMap<>();
    data.put("name", "my first case");
    CaseFileInstance caseFile = caseService.newCaseFileInstance(deploymentUnit.getIdentifier(), EMPTY_CASE_P_ID, data);
    String caseId = caseService.startCase(deploymentUnit.getIdentifier(), EMPTY_CASE_P_ID, caseFile);
    assertNotNull(caseId);
    assertEquals(FIRST_CASE_ID, caseId);
    try {
        CaseInstance cInstance = caseService.getCaseInstance(caseId);
        assertNotNull(cInstance);
        assertEquals(FIRST_CASE_ID, cInstance.getCaseId());
        assertEquals(deploymentUnit.getIdentifier(), cInstance.getDeploymentId());
        Collection<ProcessInstanceDesc> caseProcessInstances = caseRuntimeDataService.getProcessInstancesForCase(caseId, new QueryContext());
        assertNotNull(caseProcessInstances);
        assertEquals(1, caseProcessInstances.size());
        ProcessInstanceDesc casePI = caseProcessInstances.iterator().next();
        assertNotNull(casePI);
        assertEquals(FIRST_CASE_ID, casePI.getCorrelationKey());
        String nameVar = (String) processService.getProcessInstanceVariable(casePI.getId(), "name");
        assertNotNull(nameVar);
        assertEquals("my first case", nameVar);
        // add dynamic service task to empty case instance - first by case id
        Map<String, Object> parameters = new HashMap<>();
        parameters.put("Interface", EchoService.class.getName());
        parameters.put("Operation", "echo");
        parameters.put("ParameterType", String.class.getName());
        parameters.put("Parameter", "testing dynamic service task");
        caseService.addDynamicTask(FIRST_CASE_ID, caseService.newTaskSpec("Service Task", "task 1", parameters));
        nameVar = (String) processService.getProcessInstanceVariable(casePI.getId(), "name");
        assertNotNull(nameVar);
        assertEquals("testing dynamic service task echoed by service", nameVar);
        // second dynamic service task add by process instance id
        parameters.put("Parameter", "testing dynamic service task 2");
        caseService.addDynamicTask(casePI.getId(), caseService.newTaskSpec("Service Task", "task 2", parameters));
        nameVar = (String) processService.getProcessInstanceVariable(casePI.getId(), "name");
        assertNotNull(nameVar);
        assertEquals("testing dynamic service task 2 echoed by service", nameVar);
        Collection<NodeInstanceDesc> nodes = runtimeDataService.getProcessInstanceHistoryCompleted(casePI.getId(), new QueryContext());
        assertNotNull(nodes);
        assertEquals(4, nodes.size());
        Map<String, String> nodesByName = nodes.stream().collect(toMap(NodeInstanceDesc::getName, NodeInstanceDesc::getNodeType));
        assertTrue(nodesByName.containsKey("StartProcess"));
        assertTrue(nodesByName.containsKey("EndProcess"));
        assertTrue(nodesByName.containsKey("[Dynamic] task 1"));
        assertTrue(nodesByName.containsKey("[Dynamic] task 2"));
        assertEquals("StartNode", nodesByName.get("StartProcess"));
        assertEquals("EndNode", nodesByName.get("EndProcess"));
        assertEquals("Service Task", nodesByName.get("[Dynamic] task 1"));
        assertEquals("Service Task", nodesByName.get("[Dynamic] task 2"));
    } catch (Exception e) {
        logger.error("Unexpected error {}", e.getMessage(), e);
        fail("Unexpected exception " + e.getMessage());
    } finally {
        if (caseId != null) {
            caseService.cancelCase(caseId);
        }
    }
}
Also used : HashMap(java.util.HashMap) EchoService(org.jbpm.casemgmt.impl.objects.EchoService) ProcessInstanceDesc(org.jbpm.services.api.model.ProcessInstanceDesc) QueryContext(org.kie.api.runtime.query.QueryContext) CaseCommentNotFoundException(org.jbpm.casemgmt.api.CaseCommentNotFoundException) AdHocFragmentNotFoundException(org.jbpm.casemgmt.api.AdHocFragmentNotFoundException) CaseNotFoundException(org.jbpm.casemgmt.api.CaseNotFoundException) TaskNotFoundException(org.jbpm.services.api.TaskNotFoundException) CaseActiveException(org.jbpm.casemgmt.api.CaseActiveException) CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) CaseInstance(org.jbpm.casemgmt.api.model.instance.CaseInstance) NodeInstanceDesc(org.jbpm.services.api.model.NodeInstanceDesc) AbstractCaseServicesBaseTest(org.jbpm.casemgmt.impl.util.AbstractCaseServicesBaseTest) Test(org.junit.Test)

Example 39 with QueryContext

use of org.kie.api.runtime.query.QueryContext in project jbpm by kiegroup.

the class CaseServiceImplTest method testUserTaskToCaseWithStageCompleteCaseDataItem.

@Test
public void testUserTaskToCaseWithStageCompleteCaseDataItem() {
    Map<String, OrganizationalEntity> roleAssignments = new HashMap<>();
    roleAssignments.put("owner", new UserImpl("john"));
    Map<String, Object> data = new HashMap<>();
    CaseFileInstance caseFile = caseService.newCaseFileInstance(deploymentUnit.getIdentifier(), USER_TASK_CASE_P_ID, data, roleAssignments);
    String caseId = caseService.startCase(deploymentUnit.getIdentifier(), USER_TASK_STAGE_CASE_P_ID, caseFile);
    assertNotNull(caseId);
    assertEquals(FIRST_CASE_ID, caseId);
    try {
        CaseInstance cInstance = caseService.getCaseInstance(caseId);
        assertNotNull(cInstance);
        assertEquals(FIRST_CASE_ID, cInstance.getCaseId());
        assertEquals(deploymentUnit.getIdentifier(), cInstance.getDeploymentId());
        Collection<CaseFileItem> caseFileItems = caseRuntimeDataService.getCaseInstanceDataItems(caseId, new QueryContext());
        assertNotNull(caseFileItems);
        assertEquals(0, caseFileItems.size());
        Collection<CaseStageInstance> activeStages = caseRuntimeDataService.getCaseInstanceStages(caseId, true, new QueryContext());
        assertNotNull(activeStages);
        assertEquals(1, activeStages.size());
        caseService.addDataToCaseFile(caseId, "dataComplete", true);
        activeStages = caseRuntimeDataService.getCaseInstanceStages(caseId, true, new QueryContext());
        assertNotNull(activeStages);
        assertEquals(0, activeStages.size());
        caseFileItems = caseRuntimeDataService.getCaseInstanceDataItems(caseId, new QueryContext());
        assertNotNull(caseFileItems);
        assertEquals(1, caseFileItems.size());
        CaseFileItem dataItem = caseFileItems.iterator().next();
        assertNotNull(dataItem);
        assertEquals(caseId, dataItem.getCaseId());
        assertEquals("dataComplete", dataItem.getName());
        assertEquals("true", dataItem.getValue());
        assertEquals(Boolean.class.getName(), dataItem.getType());
        assertEquals(identityProvider.getName(), dataItem.getLastModifiedBy());
        assertNotNull(dataItem.getLastModified());
        caseService.addDataToCaseFile(caseId, "anotherDataItem", "first version");
        caseFileItems = caseRuntimeDataService.getCaseInstanceDataItems(caseId, new QueryContext());
        assertNotNull(caseFileItems);
        assertEquals(2, caseFileItems.size());
        dataItem = caseFileItems.iterator().next();
        assertNotNull(dataItem);
        assertEquals(caseId, dataItem.getCaseId());
        assertEquals("anotherDataItem", dataItem.getName());
        assertEquals("first version", dataItem.getValue());
        assertEquals(String.class.getName(), dataItem.getType());
        assertEquals(identityProvider.getName(), dataItem.getLastModifiedBy());
        assertNotNull(dataItem.getLastModified());
        caseService.addDataToCaseFile(caseId, "anotherDataItem", "second version");
        caseFileItems = caseRuntimeDataService.getCaseInstanceDataItems(caseId, new QueryContext());
        assertNotNull(caseFileItems);
        assertEquals(2, caseFileItems.size());
        dataItem = caseFileItems.iterator().next();
        assertNotNull(dataItem);
        assertEquals(caseId, dataItem.getCaseId());
        assertEquals("anotherDataItem", dataItem.getName());
        assertEquals("second version", dataItem.getValue());
        assertEquals(String.class.getName(), dataItem.getType());
        assertEquals(identityProvider.getName(), dataItem.getLastModifiedBy());
        assertNotNull(dataItem.getLastModified());
        caseService.removeDataFromCaseFile(caseId, "anotherDataItem");
        caseFileItems = caseRuntimeDataService.getCaseInstanceDataItems(caseId, new QueryContext());
        assertNotNull(caseFileItems);
        assertEquals(1, caseFileItems.size());
        identityProvider.setName("mary");
        caseFileItems = caseRuntimeDataService.getCaseInstanceDataItems(caseId, new QueryContext());
        assertNotNull(caseFileItems);
        // mary is not involved in case isntance
        assertEquals(0, caseFileItems.size());
    } catch (Exception e) {
        logger.error("Unexpected error {}", e.getMessage(), e);
        fail("Unexpected exception " + e.getMessage());
    } finally {
        identityProvider.setName("john");
        if (caseId != null) {
            caseService.cancelCase(caseId);
        }
    }
}
Also used : OrganizationalEntity(org.kie.api.task.model.OrganizationalEntity) HashMap(java.util.HashMap) CaseStageInstance(org.jbpm.casemgmt.api.model.instance.CaseStageInstance) QueryContext(org.kie.api.runtime.query.QueryContext) CaseCommentNotFoundException(org.jbpm.casemgmt.api.CaseCommentNotFoundException) AdHocFragmentNotFoundException(org.jbpm.casemgmt.api.AdHocFragmentNotFoundException) CaseNotFoundException(org.jbpm.casemgmt.api.CaseNotFoundException) TaskNotFoundException(org.jbpm.services.api.TaskNotFoundException) CaseActiveException(org.jbpm.casemgmt.api.CaseActiveException) CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) CaseInstance(org.jbpm.casemgmt.api.model.instance.CaseInstance) UserImpl(org.jbpm.services.task.impl.model.UserImpl) CaseFileItem(org.jbpm.casemgmt.api.model.CaseFileItem) AbstractCaseServicesBaseTest(org.jbpm.casemgmt.impl.util.AbstractCaseServicesBaseTest) Test(org.junit.Test)

Example 40 with QueryContext

use of org.kie.api.runtime.query.QueryContext in project jbpm by kiegroup.

the class CaseServiceImplTest method testTriggerMultipleAdHocTasks.

@Test
public void testTriggerMultipleAdHocTasks() {
    identityProvider.setName("john");
    Map<String, OrganizationalEntity> roleAssignments = new HashMap<>();
    roleAssignments.put("owner", new UserImpl("john"));
    Map<String, Object> data = new HashMap<>();
    data.put("customData", "none");
    CaseFileInstance caseFile = caseService.newCaseFileInstance(deploymentUnit.getIdentifier(), TWO_STAGES_CONDITIONS_CASE_P_ID, data, roleAssignments);
    String caseId = caseService.startCase(deploymentUnit.getIdentifier(), TWO_STAGES_CONDITIONS_CASE_P_ID, caseFile);
    assertThat(caseId).isNotNull();
    try {
        List<TaskSummary> tasks = caseRuntimeDataService.getCaseTasksAssignedAsPotentialOwner(caseId, "john", null, new QueryContext());
        assertThat(tasks).isNotNull().isEmpty();
        caseService.triggerAdHocFragment(caseId, "Task 1", null);
        tasks = caseRuntimeDataService.getCaseTasksAssignedAsPotentialOwner(caseId, "john", null, new QueryContext());
        assertThat(tasks).isNotNull().hasSize(1);
        assertThat(tasks).extracting(TaskSummary::getName).containsOnly("Task 1");
        caseService.triggerAdHocFragment(caseId, "Task 1", null);
        tasks = caseRuntimeDataService.getCaseTasksAssignedAsPotentialOwner(caseId, "john", null, new QueryContext());
        assertThat(tasks).isNotNull().hasSize(2);
        assertThat(tasks).extracting(TaskSummary::getName).containsOnly("Task 1");
    } catch (Exception e) {
        logger.error("Unexpected error {}", e.getMessage(), e);
        fail("Unexpected exception " + e.getMessage());
    } finally {
        caseService.cancelCase(caseId);
    }
}
Also used : CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) OrganizationalEntity(org.kie.api.task.model.OrganizationalEntity) HashMap(java.util.HashMap) UserImpl(org.jbpm.services.task.impl.model.UserImpl) TaskSummary(org.kie.api.task.model.TaskSummary) QueryContext(org.kie.api.runtime.query.QueryContext) CaseCommentNotFoundException(org.jbpm.casemgmt.api.CaseCommentNotFoundException) AdHocFragmentNotFoundException(org.jbpm.casemgmt.api.AdHocFragmentNotFoundException) CaseNotFoundException(org.jbpm.casemgmt.api.CaseNotFoundException) TaskNotFoundException(org.jbpm.services.api.TaskNotFoundException) CaseActiveException(org.jbpm.casemgmt.api.CaseActiveException) AbstractCaseServicesBaseTest(org.jbpm.casemgmt.impl.util.AbstractCaseServicesBaseTest) Test(org.junit.Test)

Aggregations

QueryContext (org.kie.api.runtime.query.QueryContext)315 Test (org.junit.Test)299 ProcessInstanceDesc (org.jbpm.services.api.model.ProcessInstanceDesc)131 HashMap (java.util.HashMap)115 AbstractKieServicesBaseTest (org.jbpm.kie.test.util.AbstractKieServicesBaseTest)109 ArrayList (java.util.ArrayList)99 AbstractCaseServicesBaseTest (org.jbpm.casemgmt.impl.util.AbstractCaseServicesBaseTest)67 RequestInfo (org.kie.api.executor.RequestInfo)53 TaskSummary (org.kie.api.task.model.TaskSummary)53 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)51 CaseInstance (org.jbpm.casemgmt.api.model.instance.CaseInstance)49 CaseFileInstance (org.jbpm.casemgmt.api.model.instance.CaseFileInstance)45 CommandContext (org.kie.api.executor.CommandContext)39 SqlQueryDefinition (org.jbpm.kie.services.impl.query.SqlQueryDefinition)37 UserImpl (org.jbpm.services.task.impl.model.UserImpl)37 OrganizationalEntity (org.kie.api.task.model.OrganizationalEntity)36 KModuleDeploymentUnit (org.jbpm.kie.services.impl.KModuleDeploymentUnit)34 QueryFilter (org.kie.internal.query.QueryFilter)30 NodeInstanceDesc (org.jbpm.services.api.model.NodeInstanceDesc)29 ProcessDefinition (org.jbpm.services.api.model.ProcessDefinition)29