Skip to content

Commit

Permalink
fix PublishEventsIT
Browse files Browse the repository at this point in the history
  • Loading branch information
elguardian committed Sep 4, 2024
1 parent ce6441b commit 1b8b2a3
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,9 @@ public static UserTaskState initalized() {
return of(null);
}

@Override
public String toString() {
return "UserTaskState [terminate=" + terminate + ", name=" + name + "]";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.kie.kogito.usertask.model;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand All @@ -39,6 +40,12 @@ public class UserTaskModel {
private Map<String, Object> inputs;
private Map<String, Object> outputs;

private Map<String, Object> metadata;

public UserTaskModel() {
this.metadata = new HashMap<>();
}

public Map<String, Object> getInputs() {
return inputs;
}
Expand Down Expand Up @@ -192,5 +199,17 @@ public String getExternalReferenceId() {
public void setExternalReferenceId(String externalReferenceId) {
this.externalReferenceId = externalReferenceId;
}

public void setMetadata(String key, Object value) {
this.metadata.put(key, value);
}

public Map<String, Object> getMetadata() {
return metadata;
}

public void setMetadata(Map<String, Object> metadata) {
this.metadata = metadata;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public DataEvent<?> adapt(Object payload) {

UserTaskInstanceStateEventBody body = builder.build();
UserTaskInstanceStateDataEvent utEvent =
new UserTaskInstanceStateDataEvent(AdapterHelper.buildSource(getConfig().service(), event.getUserTaskModel().getExternalReferenceId()), getConfig().addons().toString(),
new UserTaskInstanceStateDataEvent(AdapterHelper.buildSource(getConfig().service(), (String) event.getUserTaskModel().getMetadata().get("ProcessId")), getConfig().addons().toString(),
event.getEventUser(),
metadata, body);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.kie.kogito.usertask.model.UserTaskModel;

import static java.util.Collections.emptyMap;
import static java.util.Collections.singletonMap;
import static java.util.Optional.ofNullable;

/**
Expand Down Expand Up @@ -130,7 +131,8 @@ static public Optional<WorkItemTransition> userTaskActivateWorkItemHandler(Kogit
model.setTaskDescription((String) workItem.getParameter(DESCRIPTION));
model.setTaskPriority(priorityInteger);
model.setExternalReferenceId(workItem.getStringId());

model.setMetadata("ProcessId", workItem.getProcessInstance().getProcessId());

ofNullable(workItem.getParameters().get(ACTOR_ID)).map(String.class::cast).map(UserTaskKogitoWorkItemHandler::toSet).ifPresent(model::setPotentialUsers);
ofNullable(workItem.getParameters().get(GROUP_ID)).map(String.class::cast).map(UserTaskKogitoWorkItemHandler::toSet).ifPresent(model::setPotentialGroups);
ofNullable(workItem.getParameters().get(BUSINESSADMINISTRATOR_ID)).map(String.class::cast).map(UserTaskKogitoWorkItemHandler::toSet).ifPresent(model::setAdminUsers);
Expand All @@ -140,14 +142,13 @@ static public Optional<WorkItemTransition> userTaskActivateWorkItemHandler(Kogit
UserTaskInstance instance = userTask.createInstance(model);
if (workItem instanceof InternalKogitoWorkItem ikw) {
ikw.setExternalReferenceId(instance.id());
ikw.setActualOwner(instance.getActualOwner());
}

instance.transition(instance.createTransitionToken("activate", emptyMap()));
if (instance.getActualOwner() == null) {
return Optional.empty();
} else {
return Optional.of(handler.newTransition(TRANSITION_ACTIVATED_CLAIM.id(), workItem.getPhaseStatus(), emptyMap()));
return Optional.of(handler.newTransition(TRANSITION_ACTIVATED_CLAIM.id(), workItem.getPhaseStatus(), singletonMap("ACTUAL_OWNER", instance.getActualOwner())));
}
}

Expand All @@ -172,7 +173,6 @@ static public Optional<WorkItemTransition> userTaskClaimWorkItemHandler(KogitoWo
if (workItem instanceof InternalKogitoWorkItem ikw) {
ikw.setActualOwner(ut.getActualOwner());
}
ut.transition(ut.createTransitionToken("claim", data));
});
return Optional.empty();
}
Expand Down Expand Up @@ -202,7 +202,7 @@ static public Optional<WorkItemTransition> userTaskAbortWorkItemHandler(KogitoWo
UserTasks userTasks = handler.getApplication().get(UserTasks.class);
UserTask userTask = userTasks.userTaskById((String) workItem.getParameter("id"));
userTask.instances().findById(workItem.getExternalReferenceId()).ifPresent(ut -> {
ut.transition(ut.createTransitionToken("fail", emptyMap()));
ut.transition(ut.createTransitionToken("skip", emptyMap()));
});
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;

Expand Down Expand Up @@ -126,9 +127,16 @@ public UserTaskTransitionToken createTransitionToken(String transitionId, Map<St

@Override
public void transition(UserTaskTransitionToken token) {
UserTaskTransition transition = token.transition();
this.setUserTaskLifeCycle.transition(this, token);
userTaskEventSupport.fireOneUserTaskStateChange(this, transition.source().getName(), transition.target().getName());
Optional<UserTaskTransitionToken> next = Optional.of(token);
while (next.isPresent()) {
UserTaskTransition transition = next.get().transition();
next = this.setUserTaskLifeCycle.transition(this, token);
this.status = transition.target();
userTaskEventSupport.fireOneUserTaskStateChange(this, transition.source().getName(), transition.target().getName());
if (this.status.isTerminate().isPresent()) {
this.instances.remove(this.id);
}
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ public class DefaultUserTaskLifeCycle implements UserTaskLifeCycle {
private static final UserTaskTransition T_NEW_ACTIVE = new DefaultUserTransition("activate", INACTIVE, ACTIVE, DefaultUserTaskLifeCycle::activate);
private static final UserTaskTransition T_ACTIVE_RESERVED = new DefaultUserTransition("claim", ACTIVE, RESERVED, DefaultUserTaskLifeCycle::claim);
private static final UserTaskTransition T_RESERVED_ACTIVE = new DefaultUserTransition("release", RESERVED, ACTIVE, DefaultUserTaskLifeCycle::release);
private static final UserTaskTransition T_ACTIVE_COMPLETED = new DefaultUserTransition("complete", ACTIVE, COMPLETED, DefaultUserTaskLifeCycle::complete);
private static final UserTaskTransition T_RESERVED_COMPLETED = new DefaultUserTransition("complete", RESERVED, COMPLETED, DefaultUserTaskLifeCycle::complete);
private static final UserTaskTransition T_RESERVED_SKIPPED = new DefaultUserTransition("skip", RESERVED, OBSOLETE, DefaultUserTaskLifeCycle::skip);
private static final UserTaskTransition T_ACTIVE_SKIPPED = new DefaultUserTransition("skip", ACTIVE, OBSOLETE, DefaultUserTaskLifeCycle::complete);
private static final UserTaskTransition T_RESERVED_ERROR = new DefaultUserTransition("fail", RESERVED, ERROR, DefaultUserTaskLifeCycle::fail);

private List<UserTaskTransition> transitions;
Expand All @@ -52,7 +54,9 @@ public DefaultUserTaskLifeCycle() {
T_NEW_ACTIVE,
T_ACTIVE_RESERVED,
T_RESERVED_ACTIVE,
T_ACTIVE_COMPLETED,
T_RESERVED_COMPLETED,
T_ACTIVE_SKIPPED,
T_RESERVED_SKIPPED,
T_RESERVED_ERROR);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,9 @@ public Optional<WorkItemTransition> abortWorkItemHandler(KogitoWorkItemManager m
return Optional.of(this.workItemLifeCycle.newTransition("abort", workItem.getPhaseStatus(), workItem.getResults()));
}


@Override
public String getName() {
return "Log";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ public String getExternalReferenceId() {

@Override
public void setExternalReferenceId(String externalReferenceId) {
this.externalReferenceId = id;
this.externalReferenceId = externalReferenceId;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ public void onUserTaskState(UserTaskStateEvent event) {
left = findNodeInstanceEvents(events, 2);
assertThat(left).hasSize(2).extractingResultOf("getNodeType").containsOnly("HumanTaskNode", "EndNode");

assertUserTaskInstanceEvent(events.get(1), "SecondTask", null, "1", "Completed", "UserTasksProcess", "Second Task");
assertUserTaskInstanceEvent(events.get(0), "SecondTask", null, "1", "Completed", "UserTasksProcess", "Second Task");
}

@Test
Expand Down Expand Up @@ -299,12 +299,12 @@ public void testBasicUserTaskProcessAbort() throws Exception {
uow.end();
assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ABORTED);
events = publisher.extract();
assertThat(events).hasSize(4);
assertThat(events).hasSize(3);

triggered = findNodeInstanceEvents(events, ProcessInstanceNodeEventBody.EVENT_TYPE_ABORTED);
assertThat(triggered).hasSize(1).extractingResultOf("getNodeName").containsOnly("First Task");

assertProcessInstanceEvent(events.get(3), "UserTasksProcess", "UserTasksProcess", ProcessInstance.STATE_ABORTED);
assertProcessInstanceEvent(events.get(2), "UserTasksProcess", "UserTasksProcess", ProcessInstance.STATE_ABORTED);

}

Expand Down

0 comments on commit 1b8b2a3

Please sign in to comment.