Skip to content

Commit

Permalink
[incubator-kie-issues-1131] v7 migration to code generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhitocode committed Aug 28, 2024
1 parent de31c08 commit 09dfea4
Showing 1 changed file with 85 additions and 154 deletions.
239 changes: 85 additions & 154 deletions jbpm/jbpm-tests/src/test/java/org/jbpm/bpmn2/SLAComplianceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,34 @@
package org.jbpm.bpmn2;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Collections;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import org.jbpm.bpmn2.objects.TestWorkItemHandler;
import org.jbpm.bpmn2.sla.IntermediateCatchEventSignalWithSLAOnEventModel;
import org.jbpm.bpmn2.sla.IntermediateCatchEventSignalWithSLAOnEventProcess;
import org.jbpm.bpmn2.sla.UserTaskWithSLAExprModel;
import org.jbpm.bpmn2.sla.UserTaskWithSLAExprProcess;
import org.jbpm.bpmn2.sla.UserTaskWithSLAModel;
import org.jbpm.bpmn2.sla.UserTaskWithSLAOnTaskModel;
import org.jbpm.bpmn2.sla.UserTaskWithSLAOnTaskProcess;
import org.jbpm.bpmn2.sla.UserTaskWithSLAProcess;
import org.jbpm.process.instance.impl.demo.SystemOutWorkItemHandler;
import org.jbpm.test.utils.ProcessTestHelper;
import org.junit.jupiter.api.Test;
import org.kie.api.event.process.SLAViolatedEvent;
import org.kie.api.runtime.process.NodeInstance;
import org.kie.kogito.Application;
import org.kie.kogito.internal.process.event.DefaultKogitoProcessEventListener;
import org.kie.kogito.internal.process.event.KogitoProcessEventListener;
import org.kie.kogito.internal.process.runtime.KogitoNodeInstance;
import org.kie.kogito.internal.process.runtime.KogitoProcessInstance;
import org.kie.kogito.internal.process.runtime.KogitoWorkItem;
import org.kie.kogito.internal.process.runtime.KogitoWorkflowProcessInstance;
import org.kie.kogito.process.ProcessInstance;
import org.kie.kogito.process.impl.AbstractProcessInstance;
import org.kie.kogito.process.impl.Sig;

import static org.assertj.core.api.Assertions.assertThat;

Expand Down Expand Up @@ -81,25 +94,21 @@ public void afterSLAViolated(SLAViolatedEvent event) {

@Test
public void testSLAonProcessMet() throws Exception {
kruntime = createKogitoProcessRuntime("org/jbpm/bpmn2/sla/BPMN2-UserTaskWithSLA.bpmn2");

Application app = ProcessTestHelper.newApplication();
TestWorkItemHandler workItemHandler = new TestWorkItemHandler();
kruntime.getKogitoWorkItemManager().registerWorkItemHandler("Human Task", workItemHandler);
KogitoProcessInstance processInstance = kruntime.startProcess("UserTaskWithSLA");
assertThat(processInstance.getState()).isEqualTo(KogitoProcessInstance.STATE_ACTIVE);

ProcessTestHelper.registerHandler(app, "Human Task", workItemHandler);
org.kie.kogito.process.Process<UserTaskWithSLAModel> processDefinition = UserTaskWithSLAProcess.newProcess(app);
ProcessInstance<UserTaskWithSLAModel> processInstance = processDefinition.createInstance(processDefinition.createModel());
processInstance.start();
assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE);
KogitoWorkItem workItem = workItemHandler.getWorkItem();
assertThat(workItem).isNotNull();
assertThat(workItem.getParameter("ActorId")).isEqualTo("john");

processInstance = kruntime.getProcessInstance(processInstance.getStringId());
assertThat(processInstance.getState()).isEqualTo(KogitoProcessInstance.STATE_ACTIVE);

kruntime.getKogitoWorkItemManager().completeWorkItem(workItem.getStringId(), null);
assertProcessInstanceFinished(processInstance, kruntime);

int slaCompliance = getSLAComplianceForProcessInstance(processInstance);
assertThat(slaCompliance).isEqualTo(KogitoProcessInstance.SLA_MET);
ProcessTestHelper.completeWorkItem(processInstance, Collections.emptyMap(), "john");
assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED);
int slaCompliance = ((org.kie.kogito.process.impl.AbstractProcessInstance<?>) processInstance)
.internalGetProcessInstance().getSlaCompliance();
assertThat(slaCompliance).isEqualTo(org.kie.api.runtime.process.ProcessInstance.SLA_MET);
}

@Test
Expand Down Expand Up @@ -155,38 +164,32 @@ public void afterSLAViolated(SLAViolatedEvent event) {

@Test
public void testSLAonUserTaskMet() throws Exception {
kruntime = createKogitoProcessRuntime("org/jbpm/bpmn2/sla/BPMN2-UserTaskWithSLAOnTask.bpmn2");

Application app = ProcessTestHelper.newApplication();
TestWorkItemHandler workItemHandler = new TestWorkItemHandler();
kruntime.getKogitoWorkItemManager().registerWorkItemHandler("Human Task", workItemHandler);
KogitoProcessInstance processInstance = kruntime.startProcess("UserTaskWithSLAOnTask");
assertThat(processInstance.getState()).isEqualTo(KogitoProcessInstance.STATE_ACTIVE);

ProcessTestHelper.registerHandler(app, "Human Task", workItemHandler);
org.kie.kogito.process.Process<UserTaskWithSLAOnTaskModel> processDefinition = UserTaskWithSLAOnTaskProcess.newProcess(app);
ProcessInstance<UserTaskWithSLAOnTaskModel> processInstance = processDefinition.createInstance(processDefinition.createModel());
processInstance.start();
assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE);
KogitoWorkItem workItem = workItemHandler.getWorkItem();
assertThat(workItem).isNotNull();
assertThat(workItem.getParameter("ActorId")).isEqualTo("john");

processInstance = kruntime.getProcessInstance(processInstance.getStringId());
assertThat(processInstance.getState()).isEqualTo(KogitoProcessInstance.STATE_ACTIVE);

Collection<KogitoNodeInstance> active = ((KogitoWorkflowProcessInstance) processInstance).getKogitoNodeInstances();
assertThat(active).hasSize(1);

KogitoNodeInstance userTaskNode = active.iterator().next();

kruntime.getKogitoWorkItemManager().completeWorkItem(workItem.getStringId(), null);
assertProcessInstanceFinished(processInstance, kruntime);

int slaCompliance = getSLAComplianceForProcessInstance(processInstance);
assertThat(slaCompliance).isEqualTo(KogitoProcessInstance.SLA_NA);

slaCompliance = getSLAComplianceForNodeInstance(processInstance.getStringId(), (org.jbpm.workflow.instance.NodeInstance) userTaskNode, 0);
// Whereas in memory it is already met
assertThat(slaCompliance).isEqualTo(KogitoProcessInstance.SLA_MET);

slaCompliance = getSLAComplianceForNodeInstance(processInstance.getStringId(), (org.jbpm.workflow.instance.NodeInstance) userTaskNode, 1);
assertThat(slaCompliance).isEqualTo(KogitoProcessInstance.SLA_MET);

processInstance = processDefinition.instances().findById(processInstance.id()).orElse(null);
assertThat(processInstance).isNotNull();
assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE);
Collection<NodeInstance> activeNodes = ((AbstractProcessInstance<?>) processInstance)
.internalGetProcessInstance().getNodeInstances();
assertThat(activeNodes).hasSize(1);
org.kie.kogito.internal.process.runtime.KogitoNodeInstance userTaskNode = (KogitoNodeInstance) activeNodes.iterator().next();
ProcessTestHelper.completeWorkItem(processInstance, Collections.emptyMap(), "john");
assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED);
int slaCompliance = ((org.kie.kogito.process.impl.AbstractProcessInstance<?>) processInstance)
.internalGetProcessInstance().getSlaCompliance();
assertThat(slaCompliance).isEqualTo(org.kie.api.runtime.process.ProcessInstance.SLA_NA);
slaCompliance = ((org.jbpm.workflow.instance.NodeInstance) userTaskNode).getSlaCompliance();
assertThat(slaCompliance).isEqualTo(org.kie.api.runtime.process.ProcessInstance.SLA_MET);
slaCompliance = ((org.jbpm.workflow.instance.NodeInstance) userTaskNode).getSlaCompliance();
assertThat(slaCompliance).isEqualTo(org.kie.api.runtime.process.ProcessInstance.SLA_MET);
}

@Test
Expand Down Expand Up @@ -237,104 +240,39 @@ public void afterSLAViolated(SLAViolatedEvent event) {

}

@Test
public void testSLAonUserTaskViolatedExternalTracking() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
final KogitoProcessEventListener listener = new DefaultKogitoProcessEventListener() {

@Override
public void afterSLAViolated(SLAViolatedEvent event) {
latch.countDown();
}

};
kruntime = createKogitoProcessRuntime("org/jbpm/bpmn2/sla/BPMN2-UserTaskWithSLAOnTask.bpmn2");

TestWorkItemHandler workItemHandler = new TestWorkItemHandler();
kruntime.getKogitoWorkItemManager().registerWorkItemHandler("Human Task", workItemHandler);
kruntime.getProcessEventManager().addEventListener(listener);
kruntime.getKieRuntime().getEnvironment().set("SLATimerMode", "false");

KogitoProcessInstance processInstance = kruntime.startProcess("UserTaskWithSLAOnTask");
assertThat(processInstance.getState()).isEqualTo(KogitoProcessInstance.STATE_ACTIVE);

KogitoWorkItem workItem = workItemHandler.getWorkItem();
assertThat(workItem).isNotNull();
assertThat(workItem.getParameter("ActorId")).isEqualTo("john");

boolean slaViolated = latch.await(5, TimeUnit.SECONDS);
assertThat(slaViolated).as("SLA should not violated by timer").isFalse();

processInstance = kruntime.getProcessInstance(processInstance.getStringId());
assertThat(processInstance.getState()).isEqualTo(KogitoProcessInstance.STATE_ACTIVE);

Collection<KogitoNodeInstance> active = ((KogitoWorkflowProcessInstance) processInstance).getKogitoNodeInstances();
assertThat(active).hasSize(1);

KogitoNodeInstance userTaskNode = active.iterator().next();

// simulate external tracking of sla
kruntime.signalEvent("slaViolation:" + userTaskNode.getStringId(), null, processInstance.getStringId());

slaViolated = latch.await(10, TimeUnit.SECONDS);
assertThat(slaViolated).as("SLA was not violated while it is expected").isTrue();

kruntime.getKogitoWorkItemManager().completeWorkItem(workItem.getStringId(), null);
assertProcessInstanceFinished(processInstance, kruntime);

int slaCompliance = getSLAComplianceForProcessInstance(processInstance);
assertThat(slaCompliance).isEqualTo(KogitoProcessInstance.SLA_NA);

slaCompliance = getSLAComplianceForNodeInstance(processInstance.getStringId(), (org.jbpm.workflow.instance.NodeInstance) userTaskNode, 0);
assertThat(slaCompliance).isEqualTo(KogitoProcessInstance.SLA_VIOLATED);

slaCompliance = getSLAComplianceForNodeInstance(processInstance.getStringId(), (org.jbpm.workflow.instance.NodeInstance) userTaskNode, 1);
assertThat(slaCompliance).isEqualTo(KogitoProcessInstance.SLA_VIOLATED);

}

@Test
public void testSLAonProcessViolatedWithExpression() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
final KogitoProcessEventListener listener = new DefaultKogitoProcessEventListener() {

@Override
public void afterSLAViolated(SLAViolatedEvent event) {
latch.countDown();
}

};
kruntime = createKogitoProcessRuntime("org/jbpm/bpmn2/sla/BPMN2-UserTaskWithSLAExpr.bpmn2");

Application app = ProcessTestHelper.newApplication();
TestWorkItemHandler workItemHandler = new TestWorkItemHandler();
kruntime.getKogitoWorkItemManager().registerWorkItemHandler("Human Task", workItemHandler);
kruntime.getProcessEventManager().addEventListener(listener);

Map<String, Object> parameters = new HashMap<>();
parameters.put("s", "3s");

KogitoProcessInstance processInstance = kruntime.startProcess("UserTaskWithSLAExpr", parameters);
assertThat(processInstance.getState()).isEqualTo(KogitoProcessInstance.STATE_ACTIVE);

ProcessTestHelper.registerHandler(app, "Human Task", workItemHandler);
ProcessTestHelper.registerProcessEventListener(app, listener);
org.kie.kogito.process.Process<UserTaskWithSLAExprModel> processDefinition = UserTaskWithSLAExprProcess.newProcess(app);
UserTaskWithSLAExprModel model = processDefinition.createModel();
model.setS("3s");
ProcessInstance<UserTaskWithSLAExprModel> processInstance = processDefinition.createInstance(model);
processInstance.start();
assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE);
KogitoWorkItem workItem = workItemHandler.getWorkItem();
assertThat(workItem).isNotNull();
assertThat(workItem.getParameter("ActorId")).isEqualTo("john");

boolean slaViolated = latch.await(10, TimeUnit.SECONDS);
assertThat(slaViolated).as("SLA was not violated while it is expected").isTrue();

processInstance = kruntime.getProcessInstance(processInstance.getStringId());
assertThat(processInstance.getState()).isEqualTo(KogitoProcessInstance.STATE_ACTIVE);

int slaCompliance = getSLAComplianceForProcessInstance(processInstance);
assertThat(slaCompliance).isEqualTo(KogitoProcessInstance.SLA_VIOLATED);

kruntime.getKogitoWorkItemManager().completeWorkItem(workItem.getStringId(), null);
assertProcessInstanceFinished(processInstance, kruntime);

slaCompliance = getSLAComplianceForProcessInstance(processInstance);
assertThat(slaCompliance).isEqualTo(KogitoProcessInstance.SLA_VIOLATED);

assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE);
int slaCompliance = ((org.kie.kogito.process.impl.AbstractProcessInstance<?>) processInstance)
.internalGetProcessInstance().getSlaCompliance();
assertThat(slaCompliance).isEqualTo(org.kie.api.runtime.process.ProcessInstance.SLA_VIOLATED);
ProcessTestHelper.completeWorkItem(processInstance, Collections.emptyMap(), "john");
assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED);
slaCompliance = ((org.kie.kogito.process.impl.AbstractProcessInstance<?>) processInstance)
.internalGetProcessInstance().getSlaCompliance();
assertThat(slaCompliance).isEqualTo(org.kie.api.runtime.process.ProcessInstance.SLA_VIOLATED);
}

@Test
Expand Down Expand Up @@ -427,38 +365,31 @@ public void afterSLAViolated(SLAViolatedEvent event) {
public void testSLAonCatchEventNotViolated() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
final KogitoProcessEventListener listener = new DefaultKogitoProcessEventListener() {

@Override
public void afterSLAViolated(SLAViolatedEvent event) {
latch.countDown();
}

};
kruntime = createKogitoProcessRuntime("org/jbpm/bpmn2/sla/BPMN2-IntermediateCatchEventSignalWithSLAOnEvent.bpmn2");

kruntime.getProcessEventManager().addEventListener(listener);
kruntime.getKogitoWorkItemManager().registerWorkItemHandler("Human Task", new SystemOutWorkItemHandler());
KogitoProcessInstance processInstance = kruntime.startProcess("IntermediateCatchEventSignalWithSLAOnEvent");
assertThat(processInstance.getState()).isEqualTo(KogitoProcessInstance.STATE_ACTIVE);

Collection<KogitoNodeInstance> active = ((KogitoWorkflowProcessInstance) processInstance).getKogitoNodeInstances();
assertThat(active).hasSize(1);

KogitoNodeInstance eventNode = active.iterator().next();

kruntime.signalEvent("MyMessage", null, processInstance.getStringId());

assertProcessInstanceFinished(processInstance, kruntime);

int slaCompliance = getSLAComplianceForProcessInstance(processInstance);
assertThat(slaCompliance).isEqualTo(KogitoProcessInstance.SLA_NA);

slaCompliance = getSLAComplianceForNodeInstance(processInstance.getStringId(), (org.jbpm.workflow.instance.NodeInstance) eventNode, 1);
assertThat(slaCompliance).isEqualTo(KogitoProcessInstance.SLA_MET);

Application app = ProcessTestHelper.newApplication();
ProcessTestHelper.registerProcessEventListener(app, listener);
ProcessTestHelper.registerHandler(app, "Human Task", new SystemOutWorkItemHandler());
org.kie.kogito.process.Process<IntermediateCatchEventSignalWithSLAOnEventModel> processDefinition = IntermediateCatchEventSignalWithSLAOnEventProcess.newProcess(app);
ProcessInstance<IntermediateCatchEventSignalWithSLAOnEventModel> processInstance = processDefinition.createInstance(processDefinition.createModel());
processInstance.start();
assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE);
Collection<NodeInstance> activeNodes = ((AbstractProcessInstance<?>) processInstance)
.internalGetProcessInstance().getNodeInstances();
assertThat(activeNodes).hasSize(1);
org.kie.kogito.internal.process.runtime.KogitoNodeInstance eventNode = (KogitoNodeInstance) activeNodes.iterator().next();
processInstance.send(Sig.of("MyMessage", null));
assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED);
int slaCompliance = ((org.kie.kogito.process.impl.AbstractProcessInstance<?>) processInstance)
.internalGetProcessInstance().getSlaCompliance();
assertThat(slaCompliance).isEqualTo(org.kie.api.runtime.process.ProcessInstance.SLA_NA);
slaCompliance = ((org.jbpm.workflow.instance.NodeInstance) eventNode).getSlaCompliance();
assertThat(slaCompliance).isEqualTo(org.kie.api.runtime.process.ProcessInstance.SLA_MET);
boolean slaViolated = latch.await(3, TimeUnit.SECONDS);
assertThat(slaViolated).as("SLA should not violated by timer").isFalse();

assertThat(slaViolated).as("SLA should not be violated by timer").isFalse();
}

/*
Expand Down

0 comments on commit 09dfea4

Please sign in to comment.