Search in sources :

Example 11 with ProcessInstance

use of org.kie.kogito.process.ProcessInstance in project kogito-runtimes by kiegroup.

the class MockCacheProcessInstancesTest method testFindByIdReadMode.

@Test
void testFindByIdReadMode() {
    BpmnProcess process = BpmnProcess.from(new ClassPathResource("BPMN2-UserTask-Script.bpmn2")).get(0);
    // workaround as BpmnProcess does not compile the scripts but just reads the xml
    for (Node node : ((WorkflowProcess) process.get()).getNodes()) {
        if (node instanceof ActionNode) {
            DroolsAction a = ((ActionNode) node).getAction();
            a.setMetaData("Action", (Action) kcontext -> {
                System.out.println("The variable value is " + kcontext.getVariable("s") + " about to call toString on it");
                kcontext.getVariable("s").toString();
            });
        }
    }
    process.setProcessInstancesFactory(new CacheProcessInstancesFactory(cacheManager));
    process.configure();
    ProcessInstance<BpmnVariables> mutablePi = process.createInstance(BpmnVariables.create(Collections.singletonMap("var", "value")));
    mutablePi.start();
    assertThat(mutablePi.status()).isEqualTo(STATE_ERROR);
    assertThat(mutablePi.error()).hasValueSatisfying(error -> {
        assertThat(error.errorMessage()).contains("java.lang.NullPointerException");
        assertThat(error.failedNodeId()).isEqualTo("ScriptTask_1");
    });
    assertThat(mutablePi.variables().toMap()).containsExactly(entry("var", "value"));
    ProcessInstances<BpmnVariables> instances = process.instances();
    assertThat(instances.size()).isOne();
    ProcessInstance<BpmnVariables> pi = instances.findById(mutablePi.id(), ProcessInstanceReadMode.READ_ONLY).get();
    assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> pi.abort());
    ProcessInstance<BpmnVariables> readOnlyPi = instances.findById(mutablePi.id(), ProcessInstanceReadMode.READ_ONLY).get();
    assertThat(readOnlyPi.status()).isEqualTo(STATE_ERROR);
    assertThat(readOnlyPi.error()).hasValueSatisfying(error -> {
        assertThat(error.errorMessage()).contains("java.lang.NullPointerException");
        assertThat(error.failedNodeId()).isEqualTo("ScriptTask_1");
    });
    assertThat(readOnlyPi.variables().toMap()).containsExactly(entry("var", "value"));
    assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> readOnlyPi.abort());
    instances.findById(mutablePi.id()).get().abort();
    assertThat(instances.size()).isZero();
}
Also used : DroolsAction(org.jbpm.workflow.core.DroolsAction) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) MetadataValueImpl(org.infinispan.client.hotrod.impl.MetadataValueImpl) BpmnVariables(org.kie.kogito.process.bpmn2.BpmnVariables) WorkflowProcess(org.jbpm.workflow.core.WorkflowProcess) STATE_ACTIVE(org.kie.kogito.internal.process.runtime.KogitoProcessInstance.STATE_ACTIVE) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RemoteCache(org.infinispan.client.hotrod.RemoteCache) ProcessInstances(org.kie.kogito.process.ProcessInstances) RemoteCacheManager(org.infinispan.client.hotrod.RemoteCacheManager) STATE_COMPLETED(org.kie.kogito.internal.process.runtime.KogitoProcessInstance.STATE_COMPLETED) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) DroolsAction(org.jbpm.workflow.core.DroolsAction) RemoteCacheManagerAdmin(org.infinispan.client.hotrod.RemoteCacheManagerAdmin) ActionNode(org.jbpm.workflow.core.node.ActionNode) BpmnProcess(org.kie.kogito.process.bpmn2.BpmnProcess) StaticIdentityProvider(org.kie.kogito.services.identity.StaticIdentityProvider) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SecurityPolicy(org.kie.kogito.auth.SecurityPolicy) Mockito.when(org.mockito.Mockito.when) Assertions.entry(org.assertj.core.api.Assertions.entry) KogitoProcessInstancesFactory(org.kie.kogito.persistence.KogitoProcessInstancesFactory) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) List(java.util.List) ProcessInstance(org.kie.kogito.process.ProcessInstance) ClassPathResource(org.drools.core.io.impl.ClassPathResource) Node(org.kie.api.definition.process.Node) ProcessError(org.kie.kogito.process.ProcessError) ProcessInstanceReadMode(org.kie.kogito.process.ProcessInstanceReadMode) STATE_ERROR(org.kie.kogito.internal.process.runtime.KogitoProcessInstance.STATE_ERROR) Optional(java.util.Optional) Action(org.jbpm.process.instance.impl.Action) Collections(java.util.Collections) WorkItem(org.kie.kogito.process.WorkItem) ProcessInstanceNotFoundException(org.kie.kogito.process.ProcessInstanceNotFoundException) Mockito.mock(org.mockito.Mockito.mock) BpmnProcess(org.kie.kogito.process.bpmn2.BpmnProcess) ActionNode(org.jbpm.workflow.core.node.ActionNode) Node(org.kie.api.definition.process.Node) ActionNode(org.jbpm.workflow.core.node.ActionNode) WorkflowProcess(org.jbpm.workflow.core.WorkflowProcess) ClassPathResource(org.drools.core.io.impl.ClassPathResource) BpmnVariables(org.kie.kogito.process.bpmn2.BpmnVariables) Test(org.junit.jupiter.api.Test)

Example 12 with ProcessInstance

use of org.kie.kogito.process.ProcessInstance in project kogito-runtimes by kiegroup.

the class FileSystemProcessInstancesTest method testFindByIdReadMode.

@Test
void testFindByIdReadMode() {
    BpmnProcess process = createProcess("BPMN2-UserTask-Script.bpmn2");
    // workaround as BpmnProcess does not compile the scripts but just reads the xml
    for (Node node : ((WorkflowProcess) process.get()).getNodes()) {
        if (node instanceof ActionNode) {
            DroolsAction a = ((ActionNode) node).getAction();
            a.setMetaData("Action", (Action) kcontext -> {
                System.out.println("The variable value is " + kcontext.getVariable("s") + " about to call toString on it");
                kcontext.getVariable("s").toString();
            });
        }
    }
    ProcessInstance<BpmnVariables> mutablePi = process.createInstance(BpmnVariables.create(Collections.singletonMap("var", "value")));
    mutablePi.start();
    assertThat(mutablePi.status()).isEqualTo(STATE_ERROR);
    assertThat(mutablePi.error()).hasValueSatisfying(error -> {
        assertThat(error.errorMessage()).contains("java.lang.NullPointerException");
        assertThat(error.failedNodeId()).isEqualTo("ScriptTask_1");
    });
    assertThat(mutablePi.variables().toMap()).containsExactly(entry("var", "value"));
    ProcessInstances<BpmnVariables> instances = process.instances();
    assertThat(instances.size()).isOne();
    ProcessInstance<BpmnVariables> pi = instances.findById(mutablePi.id(), ProcessInstanceReadMode.READ_ONLY).get();
    assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> pi.abort());
    ProcessInstance<BpmnVariables> readOnlyPi = instances.findById(mutablePi.id(), ProcessInstanceReadMode.READ_ONLY).get();
    assertThat(readOnlyPi.status()).isEqualTo(STATE_ERROR);
    assertThat(readOnlyPi.error()).hasValueSatisfying(error -> {
        assertThat(error.errorMessage()).contains("java.lang.NullPointerException");
        assertThat(error.failedNodeId()).isEqualTo("ScriptTask_1");
    });
    assertThat(readOnlyPi.variables().toMap()).containsExactly(entry("var", "value"));
    assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> readOnlyPi.abort());
    instances.findById(mutablePi.id()).get().abort();
    assertThat(instances.size()).isZero();
}
Also used : DroolsAction(org.jbpm.workflow.core.DroolsAction) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) UnitOfWork(org.kie.kogito.uow.UnitOfWork) BpmnVariables(org.kie.kogito.process.bpmn2.BpmnVariables) WorkflowProcess(org.jbpm.workflow.core.WorkflowProcess) STATE_ACTIVE(org.kie.kogito.internal.process.runtime.KogitoProcessInstance.STATE_ACTIVE) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Mockito.spy(org.mockito.Mockito.spy) ProcessInstances(org.kie.kogito.process.ProcessInstances) STATE_COMPLETED(org.kie.kogito.internal.process.runtime.KogitoProcessInstance.STATE_COMPLETED) UnitOfWorkManager(org.kie.kogito.uow.UnitOfWorkManager) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) DroolsAction(org.jbpm.workflow.core.DroolsAction) Process(org.kie.kogito.process.Process) ActionNode(org.jbpm.workflow.core.node.ActionNode) BpmnProcess(org.kie.kogito.process.bpmn2.BpmnProcess) StaticIdentityProvider(org.kie.kogito.services.identity.StaticIdentityProvider) SecurityPolicy(org.kie.kogito.auth.SecurityPolicy) Mockito.times(org.mockito.Mockito.times) Assertions.entry(org.assertj.core.api.Assertions.entry) KogitoProcessInstancesFactory(org.kie.kogito.persistence.KogitoProcessInstancesFactory) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) FileSystemProcessInstances(org.kie.kogito.persistence.filesystem.FileSystemProcessInstances) ProcessInstance(org.kie.kogito.process.ProcessInstance) ClassPathResource(org.drools.core.io.impl.ClassPathResource) Node(org.kie.api.definition.process.Node) ProcessInstanceReadMode(org.kie.kogito.process.ProcessInstanceReadMode) STATE_ERROR(org.kie.kogito.internal.process.runtime.KogitoProcessInstance.STATE_ERROR) Action(org.jbpm.process.instance.impl.Action) Collections(java.util.Collections) WorkItem(org.kie.kogito.process.WorkItem) BpmnProcess(org.kie.kogito.process.bpmn2.BpmnProcess) ActionNode(org.jbpm.workflow.core.node.ActionNode) Node(org.kie.api.definition.process.Node) ActionNode(org.jbpm.workflow.core.node.ActionNode) WorkflowProcess(org.jbpm.workflow.core.WorkflowProcess) BpmnVariables(org.kie.kogito.process.bpmn2.BpmnVariables) Test(org.junit.jupiter.api.Test)

Example 13 with ProcessInstance

use of org.kie.kogito.process.ProcessInstance in project kogito-runtimes by kiegroup.

the class MilestoneIT method testSimpleMilestone.

@Test
void testSimpleMilestone() throws Exception {
    Application app = generateCodeProcessesOnly("cases/milestones/SimpleMilestone.bpmn");
    assertThat(app).isNotNull();
    Process<? extends Model> p = app.get(Processes.class).processById("TestCase.SimpleMilestone");
    ProcessInstance<?> processInstance = p.createInstance(p.createModel());
    assertState(processInstance, ProcessInstance.STATE_PENDING);
    Collection<Milestone> expected = new ArrayList<>();
    expected.add(Milestone.builder().withName("AutoStartMilestone").withStatus(AVAILABLE).build());
    expected.add(Milestone.builder().withName("SimpleMilestone").withStatus(AVAILABLE).build());
    assertMilestones(expected, processInstance.milestones());
    processInstance.start();
    assertState(processInstance, ProcessInstance.STATE_COMPLETED);
    expected = expected.stream().map(m -> Milestone.builder().withId(m.getId()).withName(m.getName()).withStatus(COMPLETED).build()).collect(Collectors.toList());
    assertMilestones(expected, processInstance.milestones());
    RuleFlowProcessInstance legacyProcessInstance = (RuleFlowProcessInstance) ((AbstractProcessInstance<?>) processInstance).processInstance;
    assertThat(legacyProcessInstance.getNodeInstances()).isEmpty();
    assertThat(legacyProcessInstance.getNodeIdInError()).isNullOrEmpty();
    Optional<String> milestoneId = Stream.of(legacyProcessInstance.getNodeContainer().getNodes()).filter(node -> node.getName().equals("SimpleMilestone")).map(n -> (String) n.getMetaData().get(Metadata.UNIQUE_ID)).findFirst();
    assertTrue(milestoneId.isPresent());
    assertThat(legacyProcessInstance.getCompletedNodeIds()).contains(milestoneId.get());
}
Also used : Milestone(org.kie.kogito.process.flexible.Milestone) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) HashMap(java.util.HashMap) RuleFlowProcessInstance(org.jbpm.ruleflow.instance.RuleFlowProcessInstance) ProcessTestUtils.assertState(org.kie.kogito.process.impl.ProcessTestUtils.assertState) ArrayList(java.util.ArrayList) AbstractCodegenIT(org.kie.kogito.codegen.AbstractCodegenIT) Map(java.util.Map) Process(org.kie.kogito.process.Process) AVAILABLE(org.kie.kogito.process.flexible.ItemDescription.Status.AVAILABLE) Metadata(org.jbpm.ruleflow.core.Metadata) Collection(java.util.Collection) Application(org.kie.kogito.Application) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) Test(org.junit.jupiter.api.Test) List(java.util.List) Stream(java.util.stream.Stream) ProcessInstance(org.kie.kogito.process.ProcessInstance) Model(org.kie.kogito.Model) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Optional(java.util.Optional) COMPLETED(org.kie.kogito.process.flexible.ItemDescription.Status.COMPLETED) Processes(org.kie.kogito.process.Processes) WorkItem(org.kie.kogito.process.WorkItem) RuleFlowProcessInstance(org.jbpm.ruleflow.instance.RuleFlowProcessInstance) Milestone(org.kie.kogito.process.flexible.Milestone) ArrayList(java.util.ArrayList) Processes(org.kie.kogito.process.Processes) Application(org.kie.kogito.Application) Test(org.junit.jupiter.api.Test)

Example 14 with ProcessInstance

use of org.kie.kogito.process.ProcessInstance in project kogito-runtimes by kiegroup.

the class UserTaskIT method testApprovalWithReadonlyVariableTags.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testApprovalWithReadonlyVariableTags() throws Exception {
    Application app = generateCodeProcessesOnly("usertask/approval-with-readonly-variable-tags.bpmn2");
    assertThat(app).isNotNull();
    Class<?> resourceClazz = Class.forName("org.acme.travels.ApprovalsModel", true, testClassLoader());
    assertNotNull(resourceClazz);
    Field approverField = resourceClazz.getDeclaredField("approver");
    assertThat(approverField).isNotNull();
    assertThat(approverField.getType().getCanonicalName()).isEqualTo(String.class.getCanonicalName());
    Process<? extends Model> p = app.get(Processes.class).processById("approvals");
    Model m = p.createModel();
    Map<String, Object> parameters = new HashMap<>();
    parameters.put("approver", "john");
    m.fromMap(parameters);
    ProcessInstance processInstance = p.createInstance(m);
    processInstance.start();
    assertEquals(KogitoProcessInstance.STATE_ACTIVE, processInstance.status());
    final Model updates = p.createModel();
    parameters = new HashMap<>();
    parameters.put("approver", "mary");
    updates.fromMap(parameters);
    // updating readonly variable should fail
    assertThrows(VariableViolationException.class, () -> processInstance.updateVariables(updates));
    processInstance.abort();
    assertEquals(KogitoProcessInstance.STATE_ABORTED, processInstance.status());
}
Also used : Field(java.lang.reflect.Field) HashMap(java.util.HashMap) Model(org.kie.kogito.Model) KogitoProcessInstance(org.kie.kogito.internal.process.runtime.KogitoProcessInstance) ProcessInstance(org.kie.kogito.process.ProcessInstance) Processes(org.kie.kogito.process.Processes) Application(org.kie.kogito.Application) Test(org.junit.jupiter.api.Test)

Example 15 with ProcessInstance

use of org.kie.kogito.process.ProcessInstance in project kogito-runtimes by kiegroup.

the class ProcessInstanceManagementResourceTest method setup.

@SuppressWarnings({ "rawtypes", "unchecked" })
@BeforeEach
public void setup() {
    responseBuilder = mock(ResponseBuilder.class);
    Response response = mock(Response.class);
    when((runtimeDelegate).createResponseBuilder()).thenReturn(responseBuilder);
    lenient().when((responseBuilder).status(any(StatusType.class))).thenReturn(responseBuilder);
    lenient().when((responseBuilder).entity(any())).thenReturn(responseBuilder);
    lenient().when((responseBuilder).build()).thenReturn(response);
    application = mock(Application.class);
    processes = mock(Processes.class);
    AbstractProcess process = mock(AbstractProcess.class);
    ProcessInstances instances = mock(ProcessInstances.class);
    processInstance = mock(ProcessInstance.class);
    error = mock(ProcessError.class);
    lenient().when(processes.processById(anyString())).thenReturn(process);
    lenient().when(process.instances()).thenReturn(instances);
    lenient().when(instances.findById(anyString())).thenReturn(Optional.of(processInstance));
    lenient().when(processInstance.error()).thenReturn(Optional.of(error));
    lenient().when(processInstance.id()).thenReturn("abc-def");
    lenient().when(processInstance.status()).thenReturn(KogitoProcessInstance.STATE_ACTIVE);
    lenient().when(error.failedNodeId()).thenReturn("xxxxx");
    lenient().when(error.errorMessage()).thenReturn("Test error message");
    lenient().when(process.get()).thenReturn(mock(KogitoWorkflowProcess.class));
    lenient().when(application.unitOfWorkManager()).thenReturn(new DefaultUnitOfWorkManager(new CollectingUnitOfWorkFactory()));
    resource = spy(new ProcessInstanceManagementResource(processes, application));
}
Also used : AbstractProcess(org.kie.kogito.process.impl.AbstractProcess) Processes(org.kie.kogito.process.Processes) KogitoWorkflowProcess(org.kie.kogito.internal.process.runtime.KogitoWorkflowProcess) Response(javax.ws.rs.core.Response) ProcessInstances(org.kie.kogito.process.ProcessInstances) ProcessError(org.kie.kogito.process.ProcessError) StatusType(javax.ws.rs.core.Response.StatusType) CollectingUnitOfWorkFactory(org.kie.kogito.services.uow.CollectingUnitOfWorkFactory) KogitoProcessInstance(org.kie.kogito.internal.process.runtime.KogitoProcessInstance) ProcessInstance(org.kie.kogito.process.ProcessInstance) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) Application(org.kie.kogito.Application) DefaultUnitOfWorkManager(org.kie.kogito.services.uow.DefaultUnitOfWorkManager) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

ProcessInstance (org.kie.kogito.process.ProcessInstance)19 Test (org.junit.jupiter.api.Test)15 Application (org.kie.kogito.Application)10 Processes (org.kie.kogito.process.Processes)10 List (java.util.List)8 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)8 WorkItem (org.kie.kogito.process.WorkItem)8 HashMap (java.util.HashMap)7 ProcessInstances (org.kie.kogito.process.ProcessInstances)7 BpmnVariables (org.kie.kogito.process.bpmn2.BpmnVariables)7 Collections (java.util.Collections)6 ClassPathResource (org.drools.core.io.impl.ClassPathResource)6 ActionNode (org.jbpm.workflow.core.node.ActionNode)6 Model (org.kie.kogito.Model)6 SecurityPolicy (org.kie.kogito.auth.SecurityPolicy)6 KogitoProcessInstance (org.kie.kogito.internal.process.runtime.KogitoProcessInstance)6 Process (org.kie.kogito.process.Process)6 BpmnProcess (org.kie.kogito.process.bpmn2.BpmnProcess)6 StaticIdentityProvider (org.kie.kogito.services.identity.StaticIdentityProvider)6 ArrayList (java.util.ArrayList)5