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);
}
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();
}
Aggregations