Search in sources :

Example 1 with StaticIdentityProvider

use of org.kie.kogito.services.identity.StaticIdentityProvider in project kogito-runtimes by kiegroup.

the class PublishEventIT method testBasicUserTaskProcessAbort.

@Test
public void testBasicUserTaskProcessAbort() throws Exception {
    Application app = generateCodeProcessesOnly("usertask/UserTasksProcess.bpmn2");
    assertThat(app).isNotNull();
    Process<? extends Model> p = app.get(Processes.class).processById("UserTasksProcess");
    Model m = p.createModel();
    Map<String, Object> parameters = new HashMap<>();
    m.fromMap(parameters);
    TestEventPublisher publisher = new TestEventPublisher();
    app.unitOfWorkManager().eventManager().setService("http://myhost");
    app.unitOfWorkManager().eventManager().addPublisher(publisher);
    UnitOfWork uow = app.unitOfWorkManager().newUnitOfWork();
    uow.start();
    ProcessInstance<?> processInstance = p.createInstance(m);
    processInstance.start();
    uow.end();
    assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE);
    List<DataEvent<?>> events = publisher.extract();
    assertThat(events).isNotNull().hasSize(2);
    ProcessInstanceEventBody body = assertProcessInstanceEvent(events.get(0), "UserTasksProcess", "UserTasksProcess", 1);
    assertThat(body.getNodeInstances()).hasSize(2).extractingResultOf("getNodeType").contains("StartNode", "HumanTaskNode");
    assertThat(body.getNodeInstances()).extractingResultOf("getTriggerTime").allMatch(v -> v != null);
    // human task is active thus null for leave time
    assertThat(body.getNodeInstances()).extractingResultOf("getLeaveTime").containsNull();
    assertUserTaskInstanceEvent(events.get(1), "FirstTask", null, "1", "Ready", "UserTasksProcess", "First Task");
    List<WorkItem> workItems = processInstance.workItems(SecurityPolicy.of(new StaticIdentityProvider("john")));
    assertEquals(1, workItems.size());
    assertEquals("FirstTask", workItems.get(0).getName());
    uow = app.unitOfWorkManager().newUnitOfWork();
    uow.start();
    processInstance.abort();
    uow.end();
    assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ABORTED);
    events = publisher.extract();
    assertThat(events).isNotNull().hasSize(2);
    body = assertProcessInstanceEvent(events.get(0), "UserTasksProcess", "UserTasksProcess", ProcessInstance.STATE_ABORTED);
    assertThat(body.getNodeInstances()).hasSize(1).extractingResultOf("getNodeType").contains("HumanTaskNode");
    assertThat(body.getNodeInstances()).extractingResultOf("getTriggerTime").allMatch(v -> v != null);
    assertThat(body.getNodeInstances()).extractingResultOf("getLeaveTime").allMatch(v -> v != null);
    assertUserTaskInstanceEvent(events.get(1), "FirstTask", null, "1", "Aborted", "UserTasksProcess", "First Task");
}
Also used : StaticIdentityProvider(org.kie.kogito.services.identity.StaticIdentityProvider) UnitOfWork(org.kie.kogito.uow.UnitOfWork) HashMap(java.util.HashMap) ProcessInstanceEventBody(org.kie.kogito.services.event.impl.ProcessInstanceEventBody) UserTaskInstanceDataEvent(org.kie.kogito.services.event.UserTaskInstanceDataEvent) ProcessInstanceDataEvent(org.kie.kogito.services.event.ProcessInstanceDataEvent) DataEvent(org.kie.kogito.event.DataEvent) VariableInstanceDataEvent(org.kie.kogito.services.event.VariableInstanceDataEvent) Processes(org.kie.kogito.process.Processes) WorkItem(org.kie.kogito.process.WorkItem) Model(org.kie.kogito.Model) Application(org.kie.kogito.Application) Test(org.junit.jupiter.api.Test)

Example 2 with StaticIdentityProvider

use of org.kie.kogito.services.identity.StaticIdentityProvider in project kogito-runtimes by kiegroup.

the class UserTaskIT method testBasicUserTaskProcessClaimAndCompleteWrongUser.

@Test
public void testBasicUserTaskProcessClaimAndCompleteWrongUser() throws Exception {
    Application app = generateCodeProcessesOnly("usertask/UserTasksProcess.bpmn2");
    assertThat(app).isNotNull();
    Process<? extends Model> p = app.get(Processes.class).processById("UserTasksProcess");
    Model m = p.createModel();
    Map<String, Object> parameters = new HashMap<>();
    m.fromMap(parameters);
    ProcessInstance<?> processInstance = p.createInstance(m);
    processInstance.start();
    assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE);
    List<WorkItem> workItems = processInstance.workItems(securityPolicy);
    assertEquals(1, workItems.size());
    WorkItem wi = workItems.get(0);
    assertEquals("FirstTask", wi.getName());
    assertEquals(Active.ID, wi.getPhase());
    assertEquals(Active.STATUS, wi.getPhaseStatus());
    assertEquals(0, wi.getResults().size());
    final String wiId = wi.getId();
    IdentityProvider identity = new StaticIdentityProvider("kelly");
    // if user that is not authorized to work on work item both listing and getting by id should apply it
    List<WorkItem> securedWorkItems = processInstance.workItems(SecurityPolicy.of(identity));
    assertEquals(0, securedWorkItems.size());
    assertThrows(WorkItemNotFoundException.class, () -> processInstance.workItem(wiId, SecurityPolicy.of(identity)));
    assertThrows(NotAuthorizedException.class, () -> processInstance.transitionWorkItem(wiId, new HumanTaskTransition(Claim.ID, null, identity)));
    assertThrows(NotAuthorizedException.class, () -> processInstance.completeWorkItem(wiId, null, SecurityPolicy.of(identity)));
    assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE);
    workItems = processInstance.workItems(securityPolicy);
    assertEquals(1, workItems.size());
    wi = workItems.get(0);
    assertEquals("FirstTask", wi.getName());
    assertEquals(Active.ID, wi.getPhase());
    assertEquals(Active.STATUS, wi.getPhaseStatus());
    assertEquals(0, wi.getResults().size());
    IdentityProvider identityCorrect = new StaticIdentityProvider("john");
    processInstance.transitionWorkItem(workItems.get(0).getId(), new HumanTaskTransition(Complete.ID, null, identityCorrect));
    assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE);
    workItems = processInstance.workItems(securityPolicy);
    assertEquals(1, workItems.size());
    wi = workItems.get(0);
    assertEquals("SecondTask", wi.getName());
    assertEquals(Active.ID, wi.getPhase());
    assertEquals(Active.STATUS, wi.getPhaseStatus());
    assertEquals(0, wi.getResults().size());
    processInstance.abort();
    assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ABORTED);
}
Also used : StaticIdentityProvider(org.kie.kogito.services.identity.StaticIdentityProvider) HashMap(java.util.HashMap) IdentityProvider(org.kie.kogito.auth.IdentityProvider) StaticIdentityProvider(org.kie.kogito.services.identity.StaticIdentityProvider) Processes(org.kie.kogito.process.Processes) WorkItem(org.kie.kogito.process.WorkItem) HumanTaskTransition(org.jbpm.process.instance.impl.humantask.HumanTaskTransition) Model(org.kie.kogito.Model) Application(org.kie.kogito.Application) Test(org.junit.jupiter.api.Test)

Example 3 with StaticIdentityProvider

use of org.kie.kogito.services.identity.StaticIdentityProvider in project kogito-runtimes by kiegroup.

the class MockCacheProcessInstancesTest method testBasicFlowWithError.

private void testBasicFlowWithError(Consumer<ProcessInstance<BpmnVariables>> op) {
    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> processInstance = process.createInstance(BpmnVariables.create());
    processInstance.start();
    assertThat(processInstance.status()).isEqualTo(STATE_ERROR);
    Optional<ProcessError> errorOp = processInstance.error();
    assertThat(errorOp).isPresent();
    assertThat(errorOp.get().failedNodeId()).isEqualTo("ScriptTask_1");
    assertThat(errorOp.get().errorMessage()).isNotNull().contains("java.lang.NullPointerException");
    op.accept(processInstance);
    assertThat(processInstance.error()).isNotPresent();
    WorkItem workItem = processInstance.workItems(SecurityPolicy.of(new StaticIdentityProvider("john"))).get(0);
    assertThat(workItem).isNotNull();
    assertThat(workItem.getParameters().get("ActorId")).isEqualTo("john");
    processInstance.completeWorkItem(workItem.getId(), null, SecurityPolicy.of(new StaticIdentityProvider("john")));
    assertThat(processInstance.status()).isEqualTo(STATE_COMPLETED);
}
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) StaticIdentityProvider(org.kie.kogito.services.identity.StaticIdentityProvider) 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) WorkItem(org.kie.kogito.process.WorkItem) ClassPathResource(org.drools.core.io.impl.ClassPathResource) ProcessError(org.kie.kogito.process.ProcessError) WorkflowProcess(org.jbpm.workflow.core.WorkflowProcess) BpmnVariables(org.kie.kogito.process.bpmn2.BpmnVariables)

Example 4 with StaticIdentityProvider

use of org.kie.kogito.services.identity.StaticIdentityProvider in project kogito-runtimes by kiegroup.

the class MockCacheProcessInstancesTest method testBasicFlow.

@Test
public void testBasicFlow() {
    BpmnProcess process = BpmnProcess.from(new ClassPathResource("BPMN2-UserTask.bpmn2")).get(0);
    process.setProcessInstancesFactory(new CacheProcessInstancesFactory(cacheManager));
    process.configure();
    ProcessInstance<BpmnVariables> processInstance = process.createInstance(BpmnVariables.create(Collections.singletonMap("test", "test")));
    processInstance.start();
    assertThat(processInstance.status()).isEqualTo(STATE_ACTIVE);
    WorkItem workItem = processInstance.workItems(SecurityPolicy.of(new StaticIdentityProvider("john"))).get(0);
    assertThat(workItem).isNotNull();
    assertThat(workItem.getParameters().get("ActorId")).isEqualTo("john");
    processInstance.completeWorkItem(workItem.getId(), null, SecurityPolicy.of(new StaticIdentityProvider("john")));
    assertThat(processInstance.status()).isEqualTo(STATE_COMPLETED);
}
Also used : StaticIdentityProvider(org.kie.kogito.services.identity.StaticIdentityProvider) BpmnProcess(org.kie.kogito.process.bpmn2.BpmnProcess) WorkItem(org.kie.kogito.process.WorkItem) ClassPathResource(org.drools.core.io.impl.ClassPathResource) BpmnVariables(org.kie.kogito.process.bpmn2.BpmnVariables) Test(org.junit.jupiter.api.Test)

Example 5 with StaticIdentityProvider

use of org.kie.kogito.services.identity.StaticIdentityProvider in project kogito-runtimes by kiegroup.

the class ActivityGenerationModelTest method testUserTaskProcess.

@Test
public void testUserTaskProcess() throws Exception {
    BpmnProcess process = BpmnProcess.from(new ClassPathResource("BPMN2-UserTask.bpmn2")).get(0);
    ProcessMetaData metaData = ProcessToExecModelGenerator.INSTANCE.generate((WorkflowProcess) process.get());
    String content = metaData.getGeneratedClassModel().toString();
    assertThat(content).isNotNull();
    log(content);
    Map<String, String> classData = new HashMap<>();
    classData.put("org.drools.bpmn2.UserTaskProcess", content);
    TestWorkItemHandler workItemHandler = new TestWorkItemHandler();
    Map<String, BpmnProcess> processes = createProcesses(classData, Collections.singletonMap("Human Task", workItemHandler));
    ProcessInstance<BpmnVariables> processInstance = processes.get("UserTask").createInstance();
    processInstance.start();
    assertEquals(STATE_ACTIVE, processInstance.status());
    KogitoWorkItem workItem = workItemHandler.getWorkItem();
    assertNotNull(workItem);
    assertEquals("john", workItem.getParameter("ActorId"));
    processInstance.completeWorkItem(workItem.getStringId(), null, SecurityPolicy.of(new StaticIdentityProvider("john")));
    assertEquals(STATE_COMPLETED, processInstance.status());
}
Also used : TestWorkItemHandler(org.jbpm.bpmn2.objects.TestWorkItemHandler) StaticIdentityProvider(org.kie.kogito.services.identity.StaticIdentityProvider) BpmnProcess(org.kie.kogito.process.bpmn2.BpmnProcess) HashMap(java.util.HashMap) KogitoWorkItem(org.kie.kogito.internal.process.runtime.KogitoWorkItem) ProcessMetaData(org.jbpm.compiler.canonical.ProcessMetaData) ClassPathResource(org.drools.core.io.impl.ClassPathResource) BpmnVariables(org.kie.kogito.process.bpmn2.BpmnVariables) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)11 StaticIdentityProvider (org.kie.kogito.services.identity.StaticIdentityProvider)11 WorkItem (org.kie.kogito.process.WorkItem)9 HashMap (java.util.HashMap)7 ClassPathResource (org.drools.core.io.impl.ClassPathResource)6 BpmnProcess (org.kie.kogito.process.bpmn2.BpmnProcess)6 BpmnVariables (org.kie.kogito.process.bpmn2.BpmnVariables)6 Application (org.kie.kogito.Application)5 Model (org.kie.kogito.Model)5 Processes (org.kie.kogito.process.Processes)5 SecurityPolicy (org.kie.kogito.auth.SecurityPolicy)4 TestWorkItemHandler (org.jbpm.bpmn2.objects.TestWorkItemHandler)2 ProcessMetaData (org.jbpm.compiler.canonical.ProcessMetaData)2 HumanTaskTransition (org.jbpm.process.instance.impl.humantask.HumanTaskTransition)2 DataEvent (org.kie.kogito.event.DataEvent)2 KogitoWorkItem (org.kie.kogito.internal.process.runtime.KogitoWorkItem)2 Collections (java.util.Collections)1 List (java.util.List)1 Optional (java.util.Optional)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1