Skip to content

Commit

Permalink
feat(engine): create user operation log for starting processes
Browse files Browse the repository at this point in the history
Starting a process with a set of variables creates HistoricDetail entries.
These entries also carry a UserOperationId.
However, that UserOperationId can't be resolved to a UserOperationLogEntry.
Therefore StartProcessInstance and StartProcessInstanceAtActivities now
create the necessary UserOperationLogEntry.

related to CAM-9949
Merges camunda#323
Closes camunda#323
  • Loading branch information
matthiasblaesing authored and tmetzke committed Mar 21, 2019
1 parent 680a518 commit 8d060e0
Show file tree
Hide file tree
Showing 14 changed files with 169 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
package org.camunda.bpm.engine.impl.cmd;


import java.util.Collections;
import static org.camunda.bpm.engine.impl.util.EnsureUtil.ensureNotEmpty;
import static org.camunda.bpm.engine.impl.util.EnsureUtil.ensureNotNull;

import java.util.List;

import org.camunda.bpm.engine.exception.NotValidException;
import org.camunda.bpm.engine.history.UserOperationLogEntry;
import org.camunda.bpm.engine.impl.ProcessEngineLogger;
import org.camunda.bpm.engine.impl.ProcessInstanceModificationBuilderImpl;
import org.camunda.bpm.engine.impl.ProcessInstantiationBuilderImpl;
Expand All @@ -33,6 +35,7 @@
import org.camunda.bpm.engine.impl.persistence.entity.ExecutionVariableSnapshotObserver;
import org.camunda.bpm.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.camunda.bpm.engine.impl.persistence.entity.ProcessInstanceWithVariablesImpl;
import org.camunda.bpm.engine.impl.persistence.entity.PropertyChange;
import org.camunda.bpm.engine.impl.pvm.process.ActivityImpl;
import org.camunda.bpm.engine.impl.pvm.process.ProcessDefinitionImpl;
import org.camunda.bpm.engine.impl.pvm.process.TransitionImpl;
Expand Down Expand Up @@ -104,6 +107,13 @@ public ProcessInstanceWithVariables execute(CommandContext commandContext) {
processInstance.propagateEnd();
}

commandContext.getOperationLogManager().logProcessInstanceOperation(
UserOperationLogEntry.OPERATION_TYPE_CREATE,
processInstance.getId(),
processInstance.getProcessDefinitionId(),
processInstance.getProcessDefinition().getKey(),
Collections.singletonList(PropertyChange.EMPTY_CHANGE));

return new ProcessInstanceWithVariablesImpl(processInstance, variablesListener.getVariables());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package org.camunda.bpm.engine.impl.cmd;

import java.io.Serializable;
import java.util.Collections;
import org.camunda.bpm.engine.history.UserOperationLogEntry;

import org.camunda.bpm.engine.impl.ProcessInstantiationBuilderImpl;
import org.camunda.bpm.engine.impl.cfg.CommandChecker;
Expand All @@ -25,6 +27,7 @@
import org.camunda.bpm.engine.impl.persistence.entity.ExecutionVariableSnapshotObserver;
import org.camunda.bpm.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.camunda.bpm.engine.impl.persistence.entity.ProcessInstanceWithVariablesImpl;
import org.camunda.bpm.engine.impl.persistence.entity.PropertyChange;
import org.camunda.bpm.engine.runtime.ProcessInstanceWithVariables;

/**
Expand Down Expand Up @@ -60,6 +63,14 @@ public ProcessInstanceWithVariables execute(CommandContext commandContext) {
final ExecutionVariableSnapshotObserver variablesListener = new ExecutionVariableSnapshotObserver(processInstance);

processInstance.start(instantiationBuilder.getVariables());

commandContext.getOperationLogManager().logProcessInstanceOperation(
UserOperationLogEntry.OPERATION_TYPE_CREATE,
processInstance.getId(),
processInstance.getProcessDefinitionId(),
processInstance.getProcessDefinition().getKey(),
Collections.singletonList(PropertyChange.EMPTY_CHANGE));

return new ProcessInstanceWithVariablesImpl(processInstance, variablesListener.getVariables());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;

import java.util.List;

Expand All @@ -31,6 +32,7 @@
import org.camunda.bpm.engine.TaskService;
import org.camunda.bpm.engine.history.HistoricDetail;
import org.camunda.bpm.engine.history.UserOperationLogEntry;
import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.camunda.bpm.engine.task.Task;
import org.camunda.bpm.engine.test.Deployment;
import org.camunda.bpm.engine.test.ProcessEngineRule;
Expand Down Expand Up @@ -190,6 +192,51 @@ public void testSetTaskVariablesInServiceTask() {
assertNull(historicDetail.getUserOperationId());
}

@Test
@Deployment(resources={"org/camunda/bpm/engine/test/api/oneTaskProcess.bpmn20.xml"})
public void testStartProcessOperationId() {
// given
identityService.setAuthenticatedUserId("demo");

// when
ProcessInstance pi = runtimeService.startProcessInstanceByKey(PROCESS_KEY, getVariables());

//then
List<UserOperationLogEntry> userOperationLogEntries = historyService.createUserOperationLogQuery()
.operationType(UserOperationLogEntry.OPERATION_TYPE_CREATE)
.processInstanceId(pi.getId())
.list();
List<HistoricDetail> historicDetails = historyService.createHistoricDetailQuery().list();

assertFalse(userOperationLogEntries.isEmpty());
assertFalse(historicDetails.isEmpty());
verifySameOperationId(userOperationLogEntries, historicDetails);
}

@Test
@Deployment(resources={"org/camunda/bpm/engine/test/api/oneTaskProcess.bpmn20.xml"})
public void testStartProcessAtActivityOperationId() {
// given
identityService.setAuthenticatedUserId("demo");

// when
ProcessInstance pi = runtimeService.createProcessInstanceByKey(PROCESS_KEY)
.startBeforeActivity("theTask")
.setVariables(getVariables())
.execute();

//then
List<UserOperationLogEntry> userOperationLogEntries = historyService.createUserOperationLogQuery()
.operationType(UserOperationLogEntry.OPERATION_TYPE_CREATE)
.processInstanceId(pi.getId())
.list();
List<HistoricDetail> historicDetails = historyService.createHistoricDetailQuery().list();

assertFalse(userOperationLogEntries.isEmpty());
assertFalse(historicDetails.isEmpty());
verifySameOperationId(userOperationLogEntries, historicDetails);
}

private void verifySameOperationId(List<UserOperationLogEntry> userOperationLogEntries, List<HistoricDetail> historicDetails) {
assertTrue("Operation log entry must exist", userOperationLogEntries.size() > 0);
String operationId = userOperationLogEntries.get(0).getOperationId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public void testQuerySetAssigneeTaskUserOperationLogWithReadHistoryPermissionOnP
UserOperationLogQuery query = historyService.createUserOperationLogQuery();

// then
verifyQueryResults(query, 1);
verifyQueryResults(query, 2);
}

public void testQuerySetAssigneeTaskUserOperationLogWithReadHistoryPermissionOnAnyProcessDefinition() {
Expand All @@ -140,7 +140,7 @@ public void testQuerySetAssigneeTaskUserOperationLogWithReadHistoryPermissionOnA
UserOperationLogQuery query = historyService.createUserOperationLogQuery();

// then
verifyQueryResults(query, 1);
verifyQueryResults(query, 2);
}

public void testQuerySetAssigneeTaskUserOperationLogWithMultiple() {
Expand All @@ -156,7 +156,7 @@ public void testQuerySetAssigneeTaskUserOperationLogWithMultiple() {
UserOperationLogQuery query = historyService.createUserOperationLogQuery();

// then
verifyQueryResults(query, 1);
verifyQueryResults(query, 2);
}

// (case) human task /////////////////////////////
Expand Down Expand Up @@ -233,7 +233,7 @@ public void testQuerySetJobRetriesUserOperationLogWithReadHistoryPermissionOnPro
UserOperationLogQuery query = historyService.createUserOperationLogQuery();

// then
verifyQueryResults(query, 1);
verifyQueryResults(query, 2);
}

public void testQuerySetJobRetriesUserOperationLogWithReadHistoryPermissionOnAnyProcessDefinition() {
Expand All @@ -251,7 +251,7 @@ public void testQuerySetJobRetriesUserOperationLogWithReadHistoryPermissionOnAny
UserOperationLogQuery query = historyService.createUserOperationLogQuery();

// then
verifyQueryResults(query, 1);
verifyQueryResults(query, 2);
}

// process definition ////////////////////////////////////////////
Expand Down Expand Up @@ -324,7 +324,7 @@ public void testQuerySuspendProcessInstanceUserOperationLogWithReadHistoryPermis
UserOperationLogQuery query = historyService.createUserOperationLogQuery();

// then
verifyQueryResults(query, 1);
verifyQueryResults(query, 2);

clearDatabase();
}
Expand All @@ -340,7 +340,7 @@ public void testQuerySuspendProcessInstanceUserOperationLogWithReadHistoryPermis
UserOperationLogQuery query = historyService.createUserOperationLogQuery();

// then
verifyQueryResults(query, 1);
verifyQueryResults(query, 2);

clearDatabase();
}
Expand All @@ -364,7 +364,7 @@ public void testQueryAfterDeletingDeployment() {
UserOperationLogQuery query = historyService.createUserOperationLogQuery();

// then
verifyQueryResults(query, 2);
verifyQueryResults(query, 3);

disableAuthorization();
List<HistoricProcessInstance> instances = historyService.createHistoricProcessInstanceQuery().list();
Expand Down Expand Up @@ -401,7 +401,7 @@ public void testDeleteEntryWithoutAuthorization() {
setAssignee(taskId, "demo");

disableAuthorization();
String entryId = historyService.createUserOperationLogQuery().singleResult().getId();
String entryId = historyService.createUserOperationLogQuery().entityType("Task").singleResult().getId();
enableAuthorization();

try {
Expand All @@ -426,15 +426,15 @@ public void testDeleteEntryWithDeleteHistoryPermissionOnProcessDefinition() {
createGrantAuthorization(PROCESS_DEFINITION, ONE_TASK_PROCESS_KEY, userId, DELETE_HISTORY);

disableAuthorization();
String entryId = historyService.createUserOperationLogQuery().singleResult().getId();
String entryId = historyService.createUserOperationLogQuery().entityType("Task").singleResult().getId();
enableAuthorization();

// when
historyService.deleteUserOperationLogEntry(entryId);

// then
disableAuthorization();
assertNull(historyService.createUserOperationLogQuery().singleResult());
assertNull(historyService.createUserOperationLogQuery().entityType("Task").singleResult());
enableAuthorization();
}

Expand All @@ -446,15 +446,15 @@ public void testDeleteEntryWithDeleteHistoryPermissionOnAnyProcessDefinition() {
createGrantAuthorization(PROCESS_DEFINITION, ANY, userId, DELETE_HISTORY);

disableAuthorization();
String entryId = historyService.createUserOperationLogQuery().singleResult().getId();
String entryId = historyService.createUserOperationLogQuery().entityType("Task").singleResult().getId();
enableAuthorization();

// when
historyService.deleteUserOperationLogEntry(entryId);

// then
disableAuthorization();
assertNull(historyService.createUserOperationLogQuery().singleResult());
assertNull(historyService.createUserOperationLogQuery().entityType("Task").singleResult());
enableAuthorization();
}

Expand All @@ -470,14 +470,14 @@ public void testDeleteEntryAfterDeletingDeployment() {

deleteDeployment(deploymentId, false);

String entryId = historyService.createUserOperationLogQuery().singleResult().getId();
String entryId = historyService.createUserOperationLogQuery().entityType("Task").singleResult().getId();

// when
historyService.deleteUserOperationLogEntry(entryId);

// then
disableAuthorization();
assertNull(historyService.createUserOperationLogQuery().singleResult());
assertNull(historyService.createUserOperationLogQuery().entityType("Task").singleResult());
enableAuthorization();

disableAuthorization();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,10 @@ public void testLogCreation() {
rule.getIdentityService().clearAuthentication();

// then
List<UserOperationLogEntry> opLogEntries = rule.getHistoryService().createUserOperationLogQuery().list();
List<UserOperationLogEntry> opLogEntries = rule.getHistoryService()
.createUserOperationLogQuery()
.operationType(UserOperationLogEntry.OPERATION_TYPE_MODIFY_PROCESS_INSTANCE)
.list();
Assert.assertEquals(2, opLogEntries.size());

Map<String, UserOperationLogEntry> entries = asMap(opLogEntries);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -696,12 +696,12 @@ public void testOpLogSetProcessDefinitionVersionCmd() {
List<UserOperationLogEntry> userOperations = historyService
.createUserOperationLogQuery()
.processInstanceId(processInstance.getId())
.operationType(UserOperationLogEntry.OPERATION_TYPE_MODIFY_PROCESS_INSTANCE)
.list();

assertEquals(1, userOperations.size());

UserOperationLogEntry userOperationLogEntry = userOperations.get(0);
assertEquals(UserOperationLogEntry.OPERATION_TYPE_MODIFY_PROCESS_INSTANCE, userOperationLogEntry.getOperationType());
assertEquals("processDefinitionVersion", userOperationLogEntry.getProperty());
assertEquals("1", userOperationLogEntry.getOrgValue());
assertEquals("2", userOperationLogEntry.getNewValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ public void testLogAllOperationWithAuthentication() {
assertTrue((Boolean) runtimeService.getVariable(processInstanceId, "serviceTaskCalled"));

UserOperationLogQuery query = userOperationLogQuery().userId(USER_ID);
assertEquals(3, query.count());
assertEquals(4, query.count());
assertEquals(1, query.operationType(UserOperationLogEntry.OPERATION_TYPE_CREATE).count());
assertEquals(1, query.operationType(UserOperationLogEntry.OPERATION_TYPE_COMPLETE).count());
assertEquals(2, query.operationType(UserOperationLogEntry.OPERATION_TYPE_SET_VARIABLE).count());

Expand All @@ -132,10 +133,17 @@ public void testLogOperationWithoutAuthentication() {
assertTrue((Boolean) runtimeService.getVariable(processInstanceId, "taskListenerCalled"));
assertTrue((Boolean) runtimeService.getVariable(processInstanceId, "serviceTaskCalled"));

assertEquals(4, userOperationLogQuery().count());
assertEquals(5, userOperationLogQuery().count());
assertEquals(1, userOperationLogQuery().operationType(UserOperationLogEntry.OPERATION_TYPE_COMPLETE).count());
assertEquals(2, userOperationLogQuery().operationType(UserOperationLogEntry.OPERATION_TYPE_SET_VARIABLE).count());
assertEquals(1, userOperationLogQuery().operationType(UserOperationLogEntry.OPERATION_TYPE_CREATE).count());
assertEquals(1, userOperationLogQuery()
.entityType(EntityTypes.DEPLOYMENT)
.operationType(UserOperationLogEntry.OPERATION_TYPE_CREATE)
.count());
assertEquals(1, userOperationLogQuery()
.entityType(EntityTypes.PROCESS_INSTANCE)
.operationType(UserOperationLogEntry.OPERATION_TYPE_CREATE)
.count());
}

@Test
Expand All @@ -148,9 +156,16 @@ public void testLogSetVariableWithoutAuthentication() {
runtimeService.setVariable(processInstanceId, "aVariable", "aValue");

// then
assertEquals(2, userOperationLogQuery().count());
assertEquals(3, userOperationLogQuery().count());
assertEquals(1, userOperationLogQuery().operationType(UserOperationLogEntry.OPERATION_TYPE_SET_VARIABLE).count());
assertEquals(1, userOperationLogQuery().operationType(UserOperationLogEntry.OPERATION_TYPE_CREATE).count());
assertEquals(1, userOperationLogQuery()
.entityType(EntityTypes.DEPLOYMENT)
.operationType(UserOperationLogEntry.OPERATION_TYPE_CREATE)
.count());
assertEquals(1, userOperationLogQuery()
.entityType(EntityTypes.PROCESS_INSTANCE)
.operationType(UserOperationLogEntry.OPERATION_TYPE_CREATE)
.count());
}

@Test
Expand All @@ -169,7 +184,7 @@ public void testDontWriteDuplicateLogOnBatchDeletionJobExecution() {
managementService.executeJob(pending.getId());
}

assertEquals(4, userOperationLogQuery().count());
assertEquals(5, userOperationLogQuery().count());
}

@Test
Expand Down Expand Up @@ -200,11 +215,15 @@ public void testDontWriteDuplicateLogOnBatchMigrationJobExecution() {
managementService.executeJob(migrationJob.getId());

// then
assertEquals(5, userOperationLogQuery().count());
assertEquals(6, userOperationLogQuery().count());
assertEquals(2, userOperationLogQuery()
.operationType(UserOperationLogEntry.OPERATION_TYPE_CREATE)
.entityType(EntityTypes.DEPLOYMENT)
.count());
assertEquals(1, userOperationLogQuery()
.operationType(UserOperationLogEntry.OPERATION_TYPE_CREATE)
.entityType(EntityTypes.PROCESS_INSTANCE)
.count());
assertEquals(3, userOperationLogQuery()
.operationType(UserOperationLogEntry.OPERATION_TYPE_MIGRATE)
.entityType(EntityTypes.PROCESS_INSTANCE)
Expand Down
Loading

0 comments on commit 8d060e0

Please sign in to comment.