Search in sources :

Example 21 with UnitOfWork

use of org.kie.kogito.uow.UnitOfWork in project kogito-runtimes by kiegroup.

the class BusinessRuleUnitIT method testBasicBusinessRuleUnitControlledByUnitOfWork.

@ParameterizedTest
@MethodSource("processes")
public void testBasicBusinessRuleUnitControlledByUnitOfWork(String bpmnPath) throws Exception {
    Map<AbstractCodegenIT.TYPE, List<String>> resourcesTypeMap = new HashMap<>();
    resourcesTypeMap.put(TYPE.PROCESS, Collections.singletonList(bpmnPath));
    resourcesTypeMap.put(TYPE.RULES, Collections.singletonList("org/kie/kogito/codegen/tests/BusinessRuleUnit.drl"));
    Application app = generateCode(resourcesTypeMap);
    assertThat(app).isNotNull();
    final List<String> startedProcesses = new ArrayList<>();
    // add custom event listener that collects data
    app.config().get(ProcessConfig.class).processEventListeners().listeners().add(new DefaultKogitoProcessEventListener() {

        @Override
        public void beforeProcessStarted(ProcessStartedEvent event) {
            startedProcesses.add(((KogitoProcessInstance) event.getProcessInstance()).getStringId());
        }
    });
    UnitOfWork uow = app.unitOfWorkManager().newUnitOfWork();
    uow.start();
    Process<? extends Model> p = app.get(Processes.class).processById("BusinessRuleUnit");
    Model m = p.createModel();
    m.fromMap(Collections.singletonMap("person", new Person("john", 25)));
    ProcessInstance<?> processInstance = p.createInstance(m);
    processInstance.start();
    assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED);
    Model result = (Model) processInstance.variables();
    assertThat(result.toMap()).hasSize(1).containsKey("person");
    assertThat(result.toMap().get("person")).isNotNull().hasFieldOrPropertyWithValue("adult", true);
    // since the unit of work has not been finished yet not listeners where invoked
    assertThat(startedProcesses).hasSize(0);
    uow.end();
    // after unit of work has been ended listeners are invoked
    assertThat(startedProcesses).hasSize(1);
}
Also used : UnitOfWork(org.kie.kogito.uow.UnitOfWork) ProcessConfig(org.kie.kogito.process.ProcessConfig) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ProcessStartedEvent(org.kie.api.event.process.ProcessStartedEvent) DefaultKogitoProcessEventListener(org.kie.kogito.internal.process.event.DefaultKogitoProcessEventListener) Processes(org.kie.kogito.process.Processes) KogitoProcessInstance(org.kie.kogito.internal.process.runtime.KogitoProcessInstance) Model(org.kie.kogito.Model) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) Application(org.kie.kogito.Application) Person(org.kie.kogito.codegen.data.Person) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 22 with UnitOfWork

use of org.kie.kogito.uow.UnitOfWork in project kogito-runtimes by kiegroup.

the class FileSystemProcessInstancesTest method testBasicFlowControlledByUnitOfWork.

@Test
void testBasicFlowControlledByUnitOfWork() {
    BpmnProcess process = createProcess("BPMN2-UserTask.bpmn2");
    process.setProcessInstancesFactory(new FileSystemProcessInstancesFactory());
    process.configure();
    ProcessInstance<BpmnVariables> processInstance = process.createInstance(BpmnVariables.create(Collections.singletonMap("test", "test")));
    UnitOfWorkManager uowManager = process.getApplication().unitOfWorkManager();
    UnitOfWork uow = uowManager.newUnitOfWork();
    uow.start();
    processInstance.start();
    uow.end();
    assertThat(processInstance.status()).isEqualTo(STATE_ACTIVE);
    assertThat(processInstance.description()).isEqualTo("User Task");
    FileSystemProcessInstances fileSystemBasedStorage = (FileSystemProcessInstances) process.instances();
    assertThat(fileSystemBasedStorage.exists(processInstance.id())).isTrue();
    verify(fileSystemBasedStorage).create(processInstance.id(), processInstance);
    verify(fileSystemBasedStorage, times(2)).setMetadata(any(), eq(FileSystemProcessInstances.PI_DESCRIPTION), eq("User Task"));
    verify(fileSystemBasedStorage, times(2)).setMetadata(any(), eq(FileSystemProcessInstances.PI_STATUS), eq("1"));
    String testVar = (String) processInstance.variables().get("test");
    assertThat(testVar).isEqualTo("test");
    assertThat(processInstance.description()).isEqualTo("User Task");
    WorkItem workItem = processInstance.workItems(securityPolicy).get(0);
    assertThat(workItem).isNotNull();
    assertThat(workItem.getParameters().get("ActorId")).isEqualTo("john");
    uow = uowManager.newUnitOfWork();
    uow.start();
    processInstance.completeWorkItem(workItem.getId(), null, securityPolicy);
    uow.end();
    assertThat(processInstance.status()).isEqualTo(STATE_COMPLETED);
    fileSystemBasedStorage = (FileSystemProcessInstances) process.instances();
    verify(fileSystemBasedStorage).remove(processInstance.id());
    assertThat(fileSystemBasedStorage.size()).isZero();
}
Also used : BpmnProcess(org.kie.kogito.process.bpmn2.BpmnProcess) UnitOfWork(org.kie.kogito.uow.UnitOfWork) UnitOfWorkManager(org.kie.kogito.uow.UnitOfWorkManager) WorkItem(org.kie.kogito.process.WorkItem) BpmnVariables(org.kie.kogito.process.bpmn2.BpmnVariables) FileSystemProcessInstances(org.kie.kogito.persistence.filesystem.FileSystemProcessInstances) Test(org.junit.jupiter.api.Test)

Aggregations

UnitOfWork (org.kie.kogito.uow.UnitOfWork)22 Test (org.junit.jupiter.api.Test)20 HashMap (java.util.HashMap)14 Application (org.kie.kogito.Application)14 Model (org.kie.kogito.Model)14 Processes (org.kie.kogito.process.Processes)14 ProcessInstanceDataEvent (org.kie.kogito.event.process.ProcessInstanceDataEvent)11 ProcessInstanceEventBody (org.kie.kogito.event.process.ProcessInstanceEventBody)10 DataEvent (org.kie.kogito.event.DataEvent)9 UserTaskInstanceDataEvent (org.kie.kogito.event.process.UserTaskInstanceDataEvent)9 VariableInstanceDataEvent (org.kie.kogito.event.process.VariableInstanceDataEvent)9 List (java.util.List)8 ArrayList (java.util.ArrayList)7 Person (org.kie.kogito.codegen.data.Person)6 WorkItem (org.kie.kogito.process.WorkItem)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 StaticIdentityProvider (org.kie.kogito.services.identity.StaticIdentityProvider)3 ProcessStartedEvent (org.kie.api.event.process.ProcessStartedEvent)2 VariableInstanceEventBody (org.kie.kogito.event.process.VariableInstanceEventBody)2 DefaultKogitoProcessEventListener (org.kie.kogito.internal.process.event.DefaultKogitoProcessEventListener)2