use of org.kie.kogito.process.ProcessInstance in project kogito-runtimes by kiegroup.
the class AdHocFragmentsIT method testStartUserTask.
@Test
void testStartUserTask() throws Exception {
String taskName = "AdHoc User Task";
Application app = generateCodeProcessesOnly("cases/AdHocFragments.bpmn");
assertThat(app).isNotNull();
Process<? extends Model> p = app.get(Processes.class).processById("TestCase.AdHocFragments");
ProcessInstance<? extends Model> processInstance = p.createInstance(p.createModel());
processInstance.start();
Optional<WorkItem> workItem = processInstance.workItems().stream().filter(wi -> wi.getParameters().get("NodeName").equals(taskName)).findFirst();
assertThat(workItem).isNotPresent();
processInstance.send(Sig.of(taskName, p.createModel()));
assertEquals(ProcessInstance.STATE_ACTIVE, processInstance.status());
workItem = processInstance.workItems().stream().filter(wi -> wi.getParameters().get("NodeName").equals(taskName)).findFirst();
assertThat(workItem).isPresent();
assertThat(workItem.get().getId()).isNotBlank();
assertThat(workItem.get().getName()).isNotBlank();
}
use of org.kie.kogito.process.ProcessInstance in project kogito-runtimes by kiegroup.
the class PublishEventIT method testServiceTaskProcessWithError.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testServiceTaskProcessWithError() throws Exception {
Application app = generateCodeProcessesOnly("servicetask/ServiceProcessDifferentOperations.bpmn2");
assertThat(app).isNotNull();
TestEventPublisher publisher = new TestEventPublisher();
app.unitOfWorkManager().eventManager().setService("http://myhost");
app.unitOfWorkManager().eventManager().addPublisher(publisher);
UnitOfWork uow = app.unitOfWorkManager().newUnitOfWork();
uow.start();
Process<? extends Model> p = app.get(Processes.class).processById("ServiceProcessDifferentOperations");
Model m = p.createModel();
Map<String, Object> parameters = new HashMap<>();
m.fromMap(parameters);
ProcessInstance processInstance = p.createInstance(m);
processInstance.start();
uow.end();
assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ERROR);
List<DataEvent<?>> events = publisher.extract();
assertThat(events).isNotNull().hasSize(1);
ProcessInstanceEventBody body = assertProcessInstanceEvent(events.get(0), "ServiceProcessDifferentOperations", "Service Process", 5);
assertThat(body.getNodeInstances()).hasSize(2).extractingResultOf("getNodeType").contains("StartNode", "WorkItemNode");
assertThat(body.getNodeInstances()).extractingResultOf("getTriggerTime").allMatch(v -> v != null);
// human task is active thus null for leave time
assertThat(body.getNodeInstances()).extractingResultOf("getLeaveTime").containsNull();
assertThat(body.getError()).isNotNull();
assertThat(body.getError().getErrorMessage()).contains("java.lang.NullPointerException");
assertThat(body.getError().getNodeDefinitionId()).isEqualTo("_38E04E27-3CCA-47F9-927B-E37DC4B8CE25");
parameters.put("s", "john");
m.fromMap(parameters);
uow = app.unitOfWorkManager().newUnitOfWork();
uow.start();
processInstance.updateVariables(m);
uow.end();
events = publisher.extract();
assertThat(events).isNotNull().hasSize(1);
body = assertProcessInstanceEvent(events.get(0), "ServiceProcessDifferentOperations", "Service Process", 5);
assertThat(body.getError()).isNotNull();
assertThat(body.getError().getErrorMessage()).contains("java.lang.NullPointerException");
assertThat(body.getError().getNodeDefinitionId()).isEqualTo("_38E04E27-3CCA-47F9-927B-E37DC4B8CE25");
uow = app.unitOfWorkManager().newUnitOfWork();
uow.start();
if (processInstance.error().isPresent()) {
((ProcessError) processInstance.error().get()).retrigger();
}
uow.end();
events = publisher.extract();
assertThat(events).isNotNull().hasSize(1);
body = assertProcessInstanceEvent(events.get(0), "ServiceProcessDifferentOperations", "Service Process", 2);
assertThat(body.getError()).isNull();
assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED);
Model result = (Model) processInstance.variables();
assertThat(result.toMap()).hasSize(1).containsKeys("s");
assertThat(result.toMap().get("s")).isNotNull().isEqualTo("Goodbye Hello john!!");
}
use of org.kie.kogito.process.ProcessInstance in project kogito-runtimes by kiegroup.
the class CacheProcessInstancesIT 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();
}
use of org.kie.kogito.process.ProcessInstance 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);
}
use of org.kie.kogito.process.ProcessInstance in project kogito-runtimes by kiegroup.
the class ActivityGenerationModelTest method testAsyncExecution.
@Test
public void testAsyncExecution() throws Exception {
BpmnProcess process = BpmnProcess.from(new ClassPathResource("async/AsyncProcess.bpmn")).get(0);
ProcessMetaData metaData = ProcessToExecModelGenerator.INSTANCE.generate((WorkflowProcess) process.get());
String content = metaData.getGeneratedClassModel().toString();
assertThat(content).isNotNull();
log(content);
Map<String, String> classData = Collections.singletonMap("com.example.AsyncProcessProcess", content);
CountDownLatch latch = new CountDownLatch(1);
String mainThread = Thread.currentThread().getName();
AtomicReference<String> workItemThread = new AtomicReference<>();
KogitoWorkItemHandler workItemHandler = new KogitoWorkItemHandler() {
@Override
public void executeWorkItem(KogitoWorkItem workItem, KogitoWorkItemManager manager) {
workItemThread.set(Thread.currentThread().getName());
manager.completeWorkItem(workItem.getStringId(), Collections.singletonMap("response", "hello " + workItem.getParameter("name")));
latch.countDown();
}
@Override
public void abortWorkItem(KogitoWorkItem workItem, KogitoWorkItemManager manager) {
latch.countDown();
}
};
Map<String, BpmnProcess> processes = createProcesses(classData, Collections.singletonMap("org.jbpm.bpmn2.objects.HelloService_hello_3_Handler", workItemHandler));
ProcessInstance i = UnitOfWorkExecutor.executeInUnitOfWork(process.getApplication().unitOfWorkManager(), () -> {
ProcessInstance<BpmnVariables> processInstance = processes.get("AsyncProcess").createInstance(BpmnVariables.create(Collections.singletonMap("name", "Tiago")));
processInstance.start();
assertEquals(STATE_ACTIVE, processInstance.status());
return processInstance;
});
// since the tasks as async, possibly executed in different threads.
latch.await(5, TimeUnit.SECONDS);
assertEquals(STATE_COMPLETED, i.status());
BpmnVariables variables = (BpmnVariables) i.variables();
assertEquals(variables.get("greeting"), "hello Tiago");
assertNotEquals(mainThread, workItemThread.get());
}
Aggregations