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