Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[kie-roadmap#76] Track user aborting a process instance #2297

Merged
merged 1 commit into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
{ "index" : { "_index" : "processes", "_type" : "process", "_id" : "_99" } }
{"compositeId":"_99","id":99,"processId":"myprocess","processName":"MyProcess","processVersion":"1.0","state":1,"containerId":"test","initiator":null,"date":"2018-10-23","processInstanceDescription":"","correlationKey":"key","parentId":-1,"variables":{"initiator":"john","variable":123}}
{"compositeId":"_99","id":99,"processId":"myprocess","processName":"MyProcess","processVersion":"1.0","state":1,"containerId":"test","initiator":null,"terminator":null,"date":"2018-10-23","processInstanceDescription":"","correlationKey":"key","parentId":-1,"variables":{"initiator":"john","variable":123}}
Original file line number Diff line number Diff line change
Expand Up @@ -34,32 +34,33 @@ public class ProcessInstanceView implements InstanceView<ProcessInstance> {

private String compositeId;
private Long id;
private String processId;
private String processName;
private String processId;
private String processName;
private String processVersion;
private Integer state;
private String containerId;
private String initiator;
private String terminator;
private Date date;
private String processInstanceDescription;
private String correlationKey;
private Long parentId;
private Map<String, Object> variables;

private transient ProcessInstance source;

public ProcessInstanceView() {
public ProcessInstanceView() {
}

public ProcessInstanceView(ProcessInstance source) {
this.source = source;
this.id = source.getId();
}

public String getCompositeId() {
return compositeId;
}

public void setCompositeId(String compositeId) {
this.compositeId = compositeId;
}
Expand Down Expand Up @@ -120,6 +121,14 @@ public void setInitiator(String initiator) {
this.initiator = initiator;
}

public String getTerminator() {
return terminator;
}

public void setTerminator(String terminator) {
this.terminator = terminator;
}

public Date getDate() {
return date;
}
Expand Down Expand Up @@ -176,7 +185,7 @@ public String toString() {
public ProcessInstance getSource() {
return source;
}

@Override
public void copyFromSource() {
this.compositeId = System.getProperty("org.kie.server.id", "") + "_" + source.getId();
Expand All @@ -185,6 +194,7 @@ public void copyFromSource() {
this.date = new Date();
this.id = source.getId();
this.initiator = (String) ((WorkflowProcessInstanceImpl)source).getVariable("initiator");
this.terminator = (String) ((WorkflowProcessInstanceImpl)source).getVariable("terminator");
this.parentId = source.getParentProcessInstanceId();
this.processId = source.getProcessId();
this.processInstanceDescription = ((WorkflowProcessInstanceImpl)source).getDescription();
Expand Down Expand Up @@ -218,4 +228,4 @@ public boolean equals(Object obj) {
return false;
return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.jbpm.workflow.instance.WorkflowProcessInstance;
import org.kie.api.event.process.DefaultProcessEventListener;
import org.kie.api.event.process.ProcessCompletedEvent;
import org.kie.api.event.process.ProcessStartedEvent;
import org.kie.api.runtime.KieSession;
import org.kie.internal.identity.IdentityProvider;
Expand All @@ -27,6 +28,9 @@ public class IdentityProviderAwareProcessListener extends DefaultProcessEventLis
private KieSession kieSession;
private IdentityProvider identityProvider;

public static final String PROCESS_INITIATOR_KEY = "initiator";
public static final String PROCESS_TERMINATOR_KEY = "terminator";

public IdentityProviderAwareProcessListener(final KieSession kieSession) {
this.kieSession = kieSession;
}
Expand All @@ -47,9 +51,17 @@ public void beforeProcessStarted(final ProcessStartedEvent event) {
if (identityProvider != null) {
final WorkflowProcessInstance wpi = (WorkflowProcessInstance) event.getProcessInstance();
final String name = identityProvider.getName();
wpi.setVariable("initiator", name);
wpi.setVariable(PROCESS_INITIATOR_KEY, name);
wpi.getMetaData().put("OwnerId", name);
}
}

public void beforeProcessCompleted(final ProcessCompletedEvent event) {
resolveIdentityProvider();
if (identityProvider != null) {
final WorkflowProcessInstance wpi = (WorkflowProcessInstance) event.getProcessInstance();
final String name = identityProvider.getName();
wpi.setVariable(PROCESS_TERMINATOR_KEY, name);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ public AuditEvent buildEvent(ProcessStartedEvent pse) {
@Override
public AuditEvent buildEvent(ProcessCompletedEvent pce, Object log) {
ProcessInstanceLog instanceLog = (ProcessInstanceLog) super.buildEvent(pce, log);
if (pce.getProcessInstance().getState() == ProcessInstance.STATE_ABORTED) {
instanceLog.setIdentity(getIdentity(pce));
}
instanceLog.setExternalId(deploymentUnitId);
return instanceLog;

Expand Down Expand Up @@ -126,4 +129,17 @@ private String getIdentity(ProcessStartedEvent pse) {
}
return identity;
}

private String getIdentity(ProcessCompletedEvent pce) {
String identity = identityProvider.getName();
if (allowSetInitiator) {
ProcessInstance pi = (ProcessInstance) pce.getProcessInstance();
VariableScopeInstance variableScope = (VariableScopeInstance) pi.getContextInstance(VariableScope.VARIABLE_SCOPE);
Map<String, Object> processVariables = variableScope.getVariables();
String terminator = (String) processVariables.get("terminator");

identity = !StringUtils.isEmpty(terminator) ? terminator : identity;
}
return identity;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,32 @@
*/
package org.jbpm.kie.services.impl.audit;

import static org.jbpm.kie.services.impl.IdentityProviderAwareProcessListener.PROCESS_TERMINATOR_KEY;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.when;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.assertj.core.api.Assertions;
import org.drools.core.event.ProcessCompletedEventImpl;
import org.drools.core.event.ProcessStartedEventImpl;
import org.jbpm.kie.services.test.ProcessServiceImplTest;
import org.jbpm.kie.test.util.AbstractKieServicesBaseTest;
import org.jbpm.process.audit.ProcessInstanceLog;
import org.jbpm.process.core.context.variable.VariableScope;
import org.jbpm.process.instance.ProcessInstance;
import org.jbpm.process.instance.context.variable.VariableScopeInstance;
import org.jbpm.process.instance.impl.ProcessInstanceImpl;
import org.jbpm.services.api.model.VariableDesc;
import org.jbpm.workflow.core.WorkflowProcess;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.kie.api.event.process.ProcessCompletedEvent;
import org.kie.api.event.process.ProcessStartedEvent;
import org.kie.api.runtime.KieSession;
import org.kie.internal.process.CorrelationKey;
Expand Down Expand Up @@ -125,6 +132,26 @@ public void testBuildProcessStartedEventWithInitiatorAndNoUserAuthBypass() {
assertEquals("testUser", log.getIdentity());
}

@Test
public void testBuildProcessCompletedEventWithTerminatorAndUserAuthBypassEnabled() {
// Set up one more specific mock
when(processInstance.getState()).thenReturn(ProcessInstance.STATE_ABORTED);

processMetadata.put("initiator", "john");
processMetadata.put("terminator", "arnold");

enableSetInitiator(builder);

ProcessStartedEvent pse = new ProcessStartedEventImpl(processInstance, kieRuntime);
ProcessCompletedEvent pce = new ProcessCompletedEventImpl(processInstance, kieRuntime);

ProcessInstanceLog startLog = (ProcessInstanceLog) builder.buildEvent(pse);
Assertions.assertThat(startLog.getIdentity()).isEqualTo("john");

ProcessInstanceLog completeLog = (ProcessInstanceLog) builder.buildEvent(pce, startLog);
Assertions.assertThat(completeLog.getIdentity()).isEqualTo("arnold");
}

/**
* Test build the ProcessInstanceLog for a process with initiator metadata
* and user auth bypass enabled
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,42 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import org.drools.compiler.kie.builder.impl.InternalKieModule;
import org.jbpm.kie.services.impl.KModuleDeploymentUnit;
import org.jbpm.kie.test.util.AbstractKieServicesBaseTest;
import org.jbpm.persistence.api.integration.EventManagerProvider;
import org.jbpm.persistence.api.integration.InstanceView;
import org.jbpm.persistence.api.integration.model.ProcessInstanceView;
import org.jbpm.services.api.model.DeployedUnit;
import org.jbpm.services.api.model.ProcessDefinition;
import org.jbpm.services.api.model.VariableDesc;
import org.jbpm.test.persistence.processinstance.objects.TestEventEmitter;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.kie.api.KieServices;
import org.kie.api.builder.ReleaseId;
import org.kie.api.runtime.process.ProcessInstance;
import org.kie.api.runtime.query.QueryContext;
import org.kie.internal.runtime.conf.DeploymentDescriptor;
import org.kie.internal.runtime.conf.NamedObjectModel;
import org.kie.internal.runtime.manager.deploy.DeploymentDescriptorImpl;

import static org.assertj.core.api.Assertions.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.kie.scanner.KieMavenRepository.getKieMavenRepository;
import static org.jbpm.kie.services.impl.IdentityProviderAwareProcessListener.PROCESS_INITIATOR_KEY;
import static org.jbpm.kie.services.impl.IdentityProviderAwareProcessListener.PROCESS_TERMINATOR_KEY;

public class IdentityProviderAwareProcessListenerTest extends AbstractKieServicesBaseTest {

private KModuleDeploymentUnit deploymentUnit;

private static final String PROCESS_ID = "org.jbpm.writedocument";

@Before
public void init() {
configureServices();
Expand Down Expand Up @@ -102,7 +114,7 @@ public void testStartProcessWithIdentityListener() {
final String userId = "userId";
identityProvider.setName(userId);

long processInstanceId = processService.startProcess(deploymentUnit.getIdentifier(), "org.jbpm.writedocument");
long processInstanceId = processService.startProcess(deploymentUnit.getIdentifier(), PROCESS_ID);
assertNotNull(processInstanceId);
try {
final String initiator = (String) processService.getProcessInstanceVariable(processInstanceId, "initiator");
Expand All @@ -112,4 +124,35 @@ public void testStartProcessWithIdentityListener() {
}
}

@Test
public void testAbortProcessWithIdentityListener() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    EventManagerProvider.getInstance().get().setEventEmitter(new TestEventEmitter());

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add that line there

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do, thanks


EventManagerProvider.getInstance().get().setEventEmitter(new TestEventEmitter());
assertNotNull(processService);

final String startUserId = "startUserId";
identityProvider.setName(startUserId);
long processInstanceId = processService.startProcess(deploymentUnit.getIdentifier(), PROCESS_ID);

final String initiator = (String) processService.getProcessInstanceVariable(processInstanceId, PROCESS_INITIATOR_KEY);
assertEquals(startUserId, initiator);

final String abortUserId = "abortUserId";
identityProvider.setName(abortUserId);
processService.abortProcessInstance(processInstanceId);

Collection<VariableDesc> variableHistory = runtimeDataService.getVariableHistory(processInstanceId, PROCESS_TERMINATOR_KEY, null);
assertThat(variableHistory).hasSize(1);
VariableDesc var = variableHistory.iterator().next();
assertThat(var.getVariableId()).isEqualTo(PROCESS_TERMINATOR_KEY);
assertThat(var.getNewValue()).isEqualTo(abortUserId);

List<InstanceView<?>> events = TestEventEmitter.getListEvents();
List<ProcessInstanceView> views = events.stream().filter(ProcessInstanceView.class::isInstance).map(ProcessInstanceView.class::cast).collect(Collectors.toList());
assertThat(views).hasSize(2);
assertThat(views).extracting(ProcessInstanceView::getTerminator).anyMatch(abortUserId::equals);
TestEventEmitter.clear();

cimbalek marked this conversation as resolved.
Show resolved Hide resolved
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@

package org.jbpm.test.persistence.processinstance.objects;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.*;

import org.jbpm.persistence.api.integration.EventCollection;
import org.jbpm.persistence.api.integration.EventEmitter;
Expand All @@ -30,7 +26,7 @@
public class TestEventEmitter implements EventEmitter {


private static Set<InstanceView<?>> events = new LinkedHashSet<>();
private static List<InstanceView<?>> events = new ArrayList<>();

@Override
public void deliver(Collection<InstanceView<?>> data) {
Expand All @@ -55,6 +51,10 @@ public EventCollection newCollection() {
}

public static Set<InstanceView<?>> getEvents() {
return new HashSet<>(events);
}

public static List<InstanceView<?>> getListEvents() {
return events;
}

Expand All @@ -65,5 +65,4 @@ public static void clear() {
@Override
public void close() {
}

}