Skip to content

Commit

Permalink
fix examples
Browse files Browse the repository at this point in the history
  • Loading branch information
elguardian committed Sep 30, 2024
1 parent 0d2a8b9 commit 13415b0
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,12 @@
import org.kie.kogito.usertask.lifecycle.UserTaskLifeCycle;
import org.kie.kogito.usertask.lifecycle.UserTaskState;
import org.kie.kogito.usertask.lifecycle.UserTaskState.TerminationType;

import jakarta.inject.Singleton;

import org.kie.kogito.usertask.lifecycle.UserTaskTransition;
import org.kie.kogito.usertask.lifecycle.UserTaskTransitionException;
import org.kie.kogito.usertask.lifecycle.UserTaskTransitionToken;

import jakarta.inject.Singleton;

@Singleton
public class CustomUserTaskLifeCycle implements UserTaskLifeCycle {
public static final String WORKFLOW_ENGINE_USER = "WORKFLOW_ENGINE_USER";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@

import io.quarkus.test.junit.QuarkusTest;

import jakarta.inject.Inject;
import jakarta.inject.Named;

import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import jakarta.inject.Inject;
import jakarta.inject.Named;

@QuarkusTest
public class ApprovalsProcessTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,20 @@
import org.acme.travels.Traveller;
import org.junit.jupiter.api.Test;
import org.kie.kogito.Model;
import org.kie.kogito.auth.IdentityProvider;
import org.kie.kogito.auth.IdentityProviders;
import org.kie.kogito.auth.SecurityPolicy;
import org.kie.kogito.internal.process.workitem.KogitoWorkItemHandler;
import org.kie.kogito.process.Process;
import org.kie.kogito.process.ProcessInstance;
import org.kie.kogito.process.WorkItem;
import org.kie.kogito.usertask.UserTaskInstance;
import org.kie.kogito.usertask.UserTasks;
import org.kie.kogito.usertask.impl.lifecycle.DefaultUserTaskLifeCycle;

import io.quarkus.test.junit.QuarkusTest;

import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

Expand All @@ -49,6 +54,9 @@ public class ApprovalsProcessTest {
@Inject
Process<? extends Model> approvalsProcess;

@Inject
UserTasks userTasks;

@Test
public void testApprovalProcess() {

Expand All @@ -65,8 +73,6 @@ public void testApprovalProcess() {

SecurityPolicy policy = SecurityPolicy.of(IdentityProviders.of("admin", Collections.singletonList("managers")));

processInstance.workItems(policy);

List<WorkItem> workItems = processInstance.workItems(policy);
assertEquals(1, workItems.size());
Map<String, Object> results = new HashMap<>();
Expand All @@ -77,9 +83,7 @@ public void testApprovalProcess() {
workItems = processInstance.workItems(policy);
assertEquals(0, workItems.size());

policy = SecurityPolicy.of(IdentityProviders.of("john", Collections.singletonList("managers")));

processInstance.workItems(policy);
policy = SecurityPolicy.of(IdentityProviders.of("manager", Collections.singletonList("managers")));

workItems = processInstance.workItems(policy);
assertEquals(1, workItems.size());
Expand All @@ -90,7 +94,7 @@ public void testApprovalProcess() {

Model result = (Model) processInstance.variables();
assertEquals(4, result.toMap().size());
assertEquals(result.toMap().get("approver"), "admin");
assertEquals(result.toMap().get("approver"), "manager");
assertEquals(result.toMap().get("firstLineApproval"), true);
assertEquals(result.toMap().get("secondLineApproval"), false);
}
Expand All @@ -109,38 +113,47 @@ public void testApprovalProcessViaPhases() {
processInstance.start();
assertEquals(org.kie.api.runtime.process.ProcessInstance.STATE_ACTIVE, processInstance.status());

SecurityPolicy policy = SecurityPolicy.of(IdentityProviders.of("admin", Collections.singletonList("managers")));
IdentityProvider identity = IdentityProviders.of("admin", singletonList("managers"));
SecurityPolicy policy = SecurityPolicy.of(identity);

processInstance.workItems(policy);

List<WorkItem> workItems = processInstance.workItems(policy);
assertEquals(1, workItems.size());

KogitoWorkItemHandler handler = approvalsProcess.getKogitoWorkItemHandler(workItems.get(0).getWorkItemHandlerName());
processInstance.transitionWorkItem(workItems.get(0).getId(), handler.newTransition("claim", workItems.get(0).getPhaseStatus(), Collections.emptyMap(), policy));
workItems = processInstance.workItems(policy);
processInstance.transitionWorkItem(workItems.get(0).getId(), handler.newTransition("complete", workItems.get(0).getPhaseStatus(), Collections.singletonMap("approved", true), policy));
List<UserTaskInstance> userTaskInstances = userTasks.instances().findByIdentity(identity);
assertThat(userTaskInstances).isNotEmpty();
userTaskInstances.forEach(ut -> {
IdentityProvider userIdentity = IdentityProviders.of("manager", singletonList("managers"));
assertThat(ut.getStatus()).isEqualTo(DefaultUserTaskLifeCycle.RESERVED);
ut.setOutput("approved", true);
ut.transition(DefaultUserTaskLifeCycle.COMPLETE, Collections.emptyMap(), userIdentity);
});

policy = SecurityPolicy.of(IdentityProviders.of("admin", Collections.singletonList("mgmt")));
policy = SecurityPolicy.of(IdentityProviders.of("admin", singletonList("mgmt")));
workItems = processInstance.workItems(policy);
assertEquals(0, workItems.size());

policy = SecurityPolicy.of(IdentityProviders.of("john", Collections.singletonList("managers")));

processInstance.workItems(policy);
identity = IdentityProviders.of("john", singletonList("managers"));
policy = SecurityPolicy.of(identity);

workItems = processInstance.workItems(policy);
assertEquals(1, workItems.size());

processInstance.transitionWorkItem(workItems.get(0).getId(), handler.newTransition("claim", workItems.get(0).getPhaseStatus(), Collections.emptyMap(), policy));
workItems = processInstance.workItems(policy);
processInstance.transitionWorkItem(workItems.get(0).getId(), handler.newTransition("complete", workItems.get(0).getPhaseStatus(), Collections.singletonMap("approved", false), policy));

userTaskInstances = userTasks.instances().findByIdentity(identity);
assertThat(userTaskInstances).isNotEmpty();
userTaskInstances.forEach(ut -> {
IdentityProvider userIdentity = IdentityProviders.of("john", singletonList("managers"));
ut.transition(DefaultUserTaskLifeCycle.CLAIM, Collections.emptyMap(), userIdentity);
assertThat(ut.getStatus()).isEqualTo(DefaultUserTaskLifeCycle.RESERVED);
ut.setOutput("approved", false);
ut.transition(DefaultUserTaskLifeCycle.COMPLETE, Collections.emptyMap(), userIdentity);
});
assertEquals(org.kie.api.runtime.process.ProcessInstance.STATE_COMPLETED, processInstance.status());

Model result = (Model) processInstance.variables();
assertEquals(4, result.toMap().size());
assertEquals(result.toMap().get("approver"), "admin");
assertEquals(result.toMap().get("approver"), "manager");
assertEquals(result.toMap().get("firstLineApproval"), true);
assertEquals(result.toMap().get("secondLineApproval"), false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,36 @@
import org.acme.travels.Traveller;
import org.junit.jupiter.api.Test;
import org.kie.kogito.Model;
import org.kie.kogito.auth.IdentityProvider;
import org.kie.kogito.auth.IdentityProviders;
import org.kie.kogito.auth.SecurityPolicy;
import org.kie.kogito.internal.process.workitem.KogitoWorkItemHandler;
import org.kie.kogito.process.Process;
import org.kie.kogito.process.ProcessInstance;
import org.kie.kogito.process.WorkItem;
import org.kie.kogito.usertask.UserTaskInstance;
import org.kie.kogito.usertask.UserTasks;
import org.kie.kogito.usertask.impl.lifecycle.DefaultUserTaskLifeCycle;

import io.quarkus.test.junit.QuarkusTest;

import jakarta.inject.Inject;
import jakarta.inject.Named;

import static java.util.Collections.emptyMap;
import static java.util.Collections.singletonMap;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import jakarta.inject.Inject;
import jakarta.inject.Named;

@QuarkusTest
public class ApprovalsProcessTest {

@Named("approvals")
@Inject
Process<? extends Model> approvalsProcess;

@Inject
UserTasks userTasks;

@Test
public void testApprovalProcess() {

Expand Down Expand Up @@ -92,7 +98,7 @@ public void testApprovalProcess() {

Model result = (Model) processInstance.variables();
assertEquals(4, result.toMap().size());
assertEquals(result.toMap().get("approver"), "admin");
assertEquals(result.toMap().get("approver"), "manager");
assertEquals(result.toMap().get("firstLineApproval"), true);
assertEquals(result.toMap().get("secondLineApproval"), false);
}
Expand All @@ -111,38 +117,47 @@ public void testApprovalProcessViaPhases() {
processInstance.start();
assertEquals(org.kie.api.runtime.process.ProcessInstance.STATE_ACTIVE, processInstance.status());

SecurityPolicy policy = SecurityPolicy.of(IdentityProviders.of("admin", Collections.singletonList("managers")));
IdentityProvider identity = IdentityProviders.of("admin", singletonList("managers"));
SecurityPolicy policy = SecurityPolicy.of(identity);

processInstance.workItems(policy);

List<WorkItem> workItems = processInstance.workItems(policy);
assertEquals(1, workItems.size());

KogitoWorkItemHandler handler = approvalsProcess.getKogitoWorkItemHandler(workItems.get(0).getWorkItemHandlerName());
processInstance.transitionWorkItem(workItems.get(0).getId(), handler.newTransition("claim", workItems.get(0).getPhaseStatus(), emptyMap(), policy));
workItems = processInstance.workItems(policy);
processInstance.transitionWorkItem(workItems.get(0).getId(), handler.newTransition("complete", workItems.get(0).getPhaseStatus(), singletonMap("approved", true), policy));
List<UserTaskInstance> userTaskInstances = userTasks.instances().findByIdentity(identity);
assertThat(userTaskInstances).isNotEmpty();
userTaskInstances.forEach(ut -> {
IdentityProvider userIdentity = IdentityProviders.of("manager", singletonList("managers"));
assertThat(ut.getStatus()).isEqualTo(DefaultUserTaskLifeCycle.RESERVED);
ut.setOutput("approved", true);
ut.transition(DefaultUserTaskLifeCycle.COMPLETE, Collections.emptyMap(), userIdentity);
});

policy = SecurityPolicy.of(IdentityProviders.of("admin", Collections.singletonList("mgmt")));
policy = SecurityPolicy.of(IdentityProviders.of("admin", singletonList("mgmt")));
workItems = processInstance.workItems(policy);
assertEquals(0, workItems.size());

policy = SecurityPolicy.of(IdentityProviders.of("john", Collections.singletonList("managers")));

processInstance.workItems(policy);
identity = IdentityProviders.of("john", singletonList("managers"));
policy = SecurityPolicy.of(identity);

workItems = processInstance.workItems(policy);
assertEquals(1, workItems.size());

processInstance.transitionWorkItem(workItems.get(0).getId(), handler.newTransition("claim", workItems.get(0).getPhaseStatus(), emptyMap(), policy));
workItems = processInstance.workItems(policy);
processInstance.transitionWorkItem(workItems.get(0).getId(), handler.newTransition("complete", workItems.get(0).getPhaseStatus(), singletonMap("approved", false), policy));

userTaskInstances = userTasks.instances().findByIdentity(identity);
assertThat(userTaskInstances).isNotEmpty();
userTaskInstances.forEach(ut -> {
IdentityProvider userIdentity = IdentityProviders.of("john", singletonList("managers"));
ut.transition(DefaultUserTaskLifeCycle.CLAIM, Collections.emptyMap(), userIdentity);
assertThat(ut.getStatus()).isEqualTo(DefaultUserTaskLifeCycle.RESERVED);
ut.setOutput("approved", false);
ut.transition(DefaultUserTaskLifeCycle.COMPLETE, Collections.emptyMap(), userIdentity);
});
assertEquals(org.kie.api.runtime.process.ProcessInstance.STATE_COMPLETED, processInstance.status());

Model result = (Model) processInstance.variables();
assertEquals(4, result.toMap().size());
assertEquals(result.toMap().get("approver"), "admin");
assertEquals(result.toMap().get("approver"), "manager");
assertEquals(result.toMap().get("firstLineApproval"), true);
assertEquals(result.toMap().get("secondLineApproval"), false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,25 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.kie.kogito.Model;
import org.kie.kogito.auth.IdentityProvider;
import org.kie.kogito.auth.IdentityProviders;
import org.kie.kogito.auth.SecurityPolicy;
import org.kie.kogito.internal.process.workitem.KogitoWorkItemHandler;
import org.kie.kogito.process.Process;
import org.kie.kogito.process.ProcessInstance;
import org.kie.kogito.process.WorkItem;
import org.kie.kogito.tests.KogitoInfinispanSpringbootApplication;
import org.kie.kogito.usertask.UserTaskInstance;
import org.kie.kogito.usertask.UserTasks;
import org.kie.kogito.usertask.impl.lifecycle.DefaultUserTaskLifeCycle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.annotation.DirtiesContext.ClassMode;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

Expand All @@ -54,6 +59,9 @@ public class ApprovalsProcessTest {
@Qualifier("approvals")
Process<? extends Model> approvalsProcess;

@Autowired
UserTasks userTasks;

@Test
public void testApprovalProcess() {

Expand Down Expand Up @@ -91,7 +99,7 @@ public void testApprovalProcess() {

Model result = (Model) processInstance.variables();
assertEquals(4, result.toMap().size());
assertEquals(result.toMap().get("approver"), "admin");
assertEquals(result.toMap().get("approver"), "manager");
assertEquals(result.toMap().get("firstLineApproval"), true);
assertEquals(result.toMap().get("secondLineApproval"), false);
}
Expand All @@ -110,38 +118,47 @@ public void testApprovalProcessViaPhases() {
processInstance.start();
assertEquals(org.kie.api.runtime.process.ProcessInstance.STATE_ACTIVE, processInstance.status());

SecurityPolicy policy = SecurityPolicy.of(IdentityProviders.of("admin", Collections.singletonList("managers")));
IdentityProvider identity = IdentityProviders.of("admin", singletonList("managers"));
SecurityPolicy policy = SecurityPolicy.of(identity);

processInstance.workItems(policy);

List<WorkItem> workItems = processInstance.workItems(policy);
assertEquals(1, workItems.size());

KogitoWorkItemHandler handler = approvalsProcess.getKogitoWorkItemHandler(workItems.get(0).getWorkItemHandlerName());
processInstance.transitionWorkItem(workItems.get(0).getId(), handler.newTransition("claim", workItems.get(0).getPhaseStatus(), Collections.emptyMap(), policy));
workItems = processInstance.workItems(policy);
processInstance.transitionWorkItem(workItems.get(0).getId(), handler.newTransition("complete", workItems.get(0).getPhaseStatus(), Collections.singletonMap("approved", true), policy));
List<UserTaskInstance> userTaskInstances = userTasks.instances().findByIdentity(identity);
assertThat(userTaskInstances).isNotEmpty();
userTaskInstances.forEach(ut -> {
IdentityProvider userIdentity = IdentityProviders.of("manager", singletonList("managers"));
assertThat(ut.getStatus()).isEqualTo(DefaultUserTaskLifeCycle.RESERVED);
ut.setOutput("approved", true);
ut.transition(DefaultUserTaskLifeCycle.COMPLETE, Collections.emptyMap(), userIdentity);
});

policy = SecurityPolicy.of(IdentityProviders.of("admin", Collections.singletonList("mgmt")));
policy = SecurityPolicy.of(IdentityProviders.of("admin", singletonList("mgmt")));
workItems = processInstance.workItems(policy);
assertEquals(0, workItems.size());

policy = SecurityPolicy.of(IdentityProviders.of("john", Collections.singletonList("managers")));

processInstance.workItems(policy);
identity = IdentityProviders.of("john", singletonList("managers"));
policy = SecurityPolicy.of(identity);

workItems = processInstance.workItems(policy);
assertEquals(1, workItems.size());

processInstance.transitionWorkItem(workItems.get(0).getId(), handler.newTransition("claim", workItems.get(0).getPhaseStatus(), Collections.emptyMap(), policy));
workItems = processInstance.workItems(policy);
processInstance.transitionWorkItem(workItems.get(0).getId(), handler.newTransition("complete", workItems.get(0).getPhaseStatus(), Collections.singletonMap("approved", false), policy));

userTaskInstances = userTasks.instances().findByIdentity(identity);
assertThat(userTaskInstances).isNotEmpty();
userTaskInstances.forEach(ut -> {
IdentityProvider userIdentity = IdentityProviders.of("john", singletonList("managers"));
ut.transition(DefaultUserTaskLifeCycle.CLAIM, Collections.emptyMap(), userIdentity);
assertThat(ut.getStatus()).isEqualTo(DefaultUserTaskLifeCycle.RESERVED);
ut.setOutput("approved", false);
ut.transition(DefaultUserTaskLifeCycle.COMPLETE, Collections.emptyMap(), userIdentity);
});
assertEquals(org.kie.api.runtime.process.ProcessInstance.STATE_COMPLETED, processInstance.status());

Model result = (Model) processInstance.variables();
assertEquals(4, result.toMap().size());
assertEquals(result.toMap().get("approver"), "admin");
assertEquals(result.toMap().get("approver"), "manager");
assertEquals(result.toMap().get("firstLineApproval"), true);
assertEquals(result.toMap().get("secondLineApproval"), false);
}
Expand Down
Loading

0 comments on commit 13415b0

Please sign in to comment.