Skip to content

Commit

Permalink
Fix usage of clock in dmn engine start and end time for audit
Browse files Browse the repository at this point in the history
  • Loading branch information
tijsrademakers committed Aug 2, 2023
1 parent c89e38c commit 773b964
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ public class DecisionExecutionAuditContainer {
public DecisionExecutionAuditContainer() {
}

public DecisionExecutionAuditContainer(String decisionKey, String decisionName, int decisionVersion, Boolean strictMode, Map<String, Object> inputVariables) {
this.startTime = new Date();
public DecisionExecutionAuditContainer(String decisionKey, String decisionName, int decisionVersion,
Boolean strictMode, Map<String, Object> inputVariables, Date startTime) {

this.startTime = startTime;
this.decisionKey = decisionKey;
this.decisionName = decisionName;
this.decisionVersion = decisionVersion;
Expand All @@ -66,9 +68,9 @@ public DecisionExecutionAuditContainer(String decisionKey, String decisionName,
}

public DecisionExecutionAuditContainer(String decisionKey, String decisionName, int decisionVersion, HitPolicy hitPolicy,
Boolean strictMode, Map<String, Object> inputVariables) {
Boolean strictMode, Map<String, Object> inputVariables, Date startTime) {

this.startTime = new Date();
this.startTime = startTime;
this.decisionKey = decisionKey;
this.decisionName = decisionName;
this.decisionVersion = decisionVersion;
Expand Down Expand Up @@ -109,8 +111,8 @@ protected Map<String, String> getVariablesTypeMap(Map<String, Object> variableVa
return variablesTypesMap;
}

public void stopAudit() {
endTime = new Date();
public void stopAudit(Date endTime) {
this.endTime = endTime;
}

public void addRuleEntry(DecisionRule rule) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package org.flowable.dmn.api;

import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
Expand All @@ -25,8 +26,10 @@ public class DecisionServiceExecutionAuditContainer extends DecisionExecutionAud
public DecisionServiceExecutionAuditContainer() {
}

public DecisionServiceExecutionAuditContainer(String id, String name, int decisionVersion, boolean strictMode, Map<String, Object> variables) {
super(id, name, decisionVersion, strictMode, variables);
public DecisionServiceExecutionAuditContainer(String id, String name, int decisionVersion, boolean strictMode,
Map<String, Object> variables, Date startTime) {

super(id, name, decisionVersion, strictMode, variables, startTime);
}

public Map<String, List<Map<String, Object>>> getDecisionServiceResult() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ public Map<String, AbstractHitPolicy> getDefaultHitPolicyBehaviors() {
/////////////////////////////////////////////////////////////
public void initRuleEngineExecutor() {
if (ruleEngineExecutor == null) {
ruleEngineExecutor = new RuleEngineExecutorImpl(hitPolicyBehaviors, expressionManager, objectMapper);
ruleEngineExecutor = new RuleEngineExecutorImpl(hitPolicyBehaviors, expressionManager, objectMapper, this);

} else {
if (ruleEngineExecutor.getExpressionManager() == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
* @author Yvo Swillens
*/
public interface RuleEngineExecutor {

DecisionExecutionAuditContainer execute(Decision decision, ExecuteDecisionContext executeDecisionInfo);

Map<String, AbstractHitPolicy> getHitPolicyBehaviors();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ protected Map<String, List<Map<String, Object>>> composeDecisionServiceResult(Ex
protected DecisionExecutionAuditContainer persistDecisionAudit(ExecuteDecisionContext executeDecisionContext) {
DecisionExecutionAuditContainer decisionExecution = executeDecisionContext.getDecisionExecution();

decisionExecution.stopAudit();
decisionExecution.stopAudit(configuration.getClock().getCurrentTime());

commandExecutor.execute(new PersistHistoricDecisionExecutionCmd(executeDecisionContext));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.flowable.common.engine.impl.el.ExpressionManager;
import org.flowable.dmn.api.DecisionExecutionAuditContainer;
import org.flowable.dmn.api.ExecuteDecisionContext;
import org.flowable.dmn.engine.DmnEngineConfiguration;
import org.flowable.dmn.engine.RuleEngineExecutor;
import org.flowable.dmn.engine.impl.el.ELExecutionContext;
import org.flowable.dmn.engine.impl.el.ELExecutionContextBuilder;
Expand Down Expand Up @@ -53,11 +54,15 @@ public class RuleEngineExecutorImpl implements RuleEngineExecutor {
protected Map<String, AbstractHitPolicy> hitPolicyBehaviors;
protected ExpressionManager expressionManager;
protected ObjectMapper objectMapper;
protected DmnEngineConfiguration dmnEngineConfiguration;

public RuleEngineExecutorImpl(Map<String, AbstractHitPolicy> hitPolicyBehaviors, ExpressionManager expressionManager, ObjectMapper objectMapper) {
public RuleEngineExecutorImpl(Map<String, AbstractHitPolicy> hitPolicyBehaviors, ExpressionManager expressionManager,
ObjectMapper objectMapper, DmnEngineConfiguration dmnEngineConfiguration) {

this.hitPolicyBehaviors = hitPolicyBehaviors;
this.expressionManager = expressionManager;
this.objectMapper = objectMapper;
this.dmnEngineConfiguration = dmnEngineConfiguration;
}

/**
Expand Down Expand Up @@ -95,7 +100,7 @@ public DecisionExecutionAuditContainer execute(Decision decision, ExecuteDecisio

} finally {
// end audit trail
executionContext.getAuditContainer().stopAudit();
executionContext.getAuditContainer().stopAudit(dmnEngineConfiguration.getClock().getCurrentTime());
}

return executionContext.getAuditContainer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
*/
package org.flowable.dmn.engine.impl.audit;

import org.flowable.common.engine.impl.runtime.Clock;
import org.flowable.dmn.api.DecisionExecutionAuditContainer;
import org.flowable.dmn.api.DecisionServiceExecutionAuditContainer;
import org.flowable.dmn.api.ExecuteDecisionContext;
import org.flowable.dmn.engine.DmnEngineConfiguration;
import org.flowable.dmn.engine.impl.util.CommandContextUtil;
import org.flowable.dmn.model.Decision;
import org.flowable.dmn.model.DecisionService;
Expand All @@ -35,9 +37,11 @@ public static DecisionServiceExecutionAuditContainer initializeDecisionServiceEx
LOGGER.error("decision service does not contain key");
throw new IllegalArgumentException("decision does not contain decision key");
}

DmnEngineConfiguration dmnEngineConfiguration = CommandContextUtil.getDmnEngineConfiguration();

return new DecisionServiceExecutionAuditContainer(decisionService.getId(), decisionService.getName(), executeDecisionInfo.getDecisionVersion(),
CommandContextUtil.getDmnEngineConfiguration().isStrictMode(), executeDecisionInfo.getVariables());
dmnEngineConfiguration.isStrictMode(), executeDecisionInfo.getVariables(), dmnEngineConfiguration.getClock().getCurrentTime());
}

public static DecisionExecutionAuditContainer initializeDecisionExecutionAudit(Decision decision, ExecuteDecisionContext executeDecisionInfo) {
Expand All @@ -53,8 +57,11 @@ public static DecisionExecutionAuditContainer initializeDecisionExecutionAudit(D
LOGGER.error("decision table does not contain a hit policy");
throw new IllegalArgumentException("decision table does not contain a hit policy");
}

Clock clock = CommandContextUtil.getDmnEngineConfiguration().getClock();

return new DecisionExecutionAuditContainer(decision.getId(), decision.getName(), executeDecisionInfo.getDecisionVersion(),
decisionTable.getHitPolicy(), CommandContextUtil.getDmnEngineConfiguration().isStrictMode(), executeDecisionInfo.getVariables());
decisionTable.getHitPolicy(), CommandContextUtil.getDmnEngineConfiguration().isStrictMode(),
executeDecisionInfo.getVariables(), clock.getCurrentTime());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import static org.assertj.core.api.Assertions.assertThat;

import java.io.InputStream;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

import org.flowable.dmn.api.DecisionExecutionAuditContainer;
Expand Down Expand Up @@ -271,55 +273,68 @@ public void multipleSameDeployments() throws Exception {

@Test
public void multipleDeployments() throws Exception {
repositoryService.createDeployment()
.name("deploymentA")
.addClasspathResource("org/flowable/dmn/engine/test/deployment/simple.dmn")
.enableDuplicateFiltering()
.deploy();

org.flowable.dmn.api.DmnDeployment deployment = repositoryService.createDeploymentQuery()
.deploymentName("deploymentA")
.singleResult();
assertThat(deployment).isNotNull();

List<DmnDecision> decisions = repositoryService.createDecisionQuery()
.decisionKey("decision")
.list();
assertThat(decisions).hasSize(1);

repositoryService.createDeployment()
.name("deploymentA")
.addClasspathResource("org/flowable/dmn/engine/test/deployment/simple2.dmn")
.enableDuplicateFiltering()
.deploy();

List<org.flowable.dmn.api.DmnDeployment> deployments = repositoryService.createDeploymentQuery()
.deploymentName("deploymentA")
.list();
assertThat(deployments).hasSize(2);

decisions = repositoryService.createDecisionQuery()
.decisionKey("anotherDecision")
.list();
assertThat(decisions).hasSize(1);

repositoryService.createDeployment()
.name("deploymentA")
.addClasspathResource("org/flowable/dmn/engine/test/deployment/simple2.dmn")
.enableDuplicateFiltering()
.deploy();

deployments = repositoryService.createDeploymentQuery()
.deploymentName("deploymentA")
.list();
assertThat(deployments).hasSize(2);

decisions = repositoryService.createDecisionQuery()
.decisionKey("anotherDecision")
.list();
assertThat(decisions).hasSize(1);

deleteDeployments();
try {
repositoryService.createDeployment()
.name("deploymentA")
.addClasspathResource("org/flowable/dmn/engine/test/deployment/simple.dmn")
.enableDuplicateFiltering()
.deploy();

org.flowable.dmn.api.DmnDeployment deployment = repositoryService.createDeploymentQuery()
.deploymentName("deploymentA")
.singleResult();
assertThat(deployment).isNotNull();

List<DmnDecision> decisions = repositoryService.createDecisionQuery()
.decisionKey("decision")
.list();
assertThat(decisions).hasSize(1);

Calendar deployCal = new GregorianCalendar();
deployCal.add(Calendar.DATE, 2);
dmnEngineConfiguration.getClock().setCurrentTime(deployCal.getTime());

repositoryService.createDeployment()
.name("deploymentA")
.addClasspathResource("org/flowable/dmn/engine/test/deployment/simple2.dmn")
.enableDuplicateFiltering()
.deploy();

List<org.flowable.dmn.api.DmnDeployment> deployments = repositoryService.createDeploymentQuery()
.deploymentName("deploymentA")
.list();
assertThat(deployments).hasSize(2);

decisions = repositoryService.createDecisionQuery()
.decisionKey("anotherDecision")
.list();
assertThat(decisions).hasSize(1);

deployCal = new GregorianCalendar();
deployCal.add(Calendar.DATE, 4);
dmnEngineConfiguration.getClock().setCurrentTime(deployCal.getTime());

repositoryService.createDeployment()
.name("deploymentA")
.addClasspathResource("org/flowable/dmn/engine/test/deployment/simple2.dmn")
.enableDuplicateFiltering()
.deploy();

deployments = repositoryService.createDeploymentQuery()
.deploymentName("deploymentA")
.list();
assertThat(deployments).hasSize(2);

decisions = repositoryService.createDecisionQuery()
.decisionKey("anotherDecision")
.list();
assertThat(decisions).hasSize(1);

deleteDeployments();

} finally {
dmnEngineConfiguration.getClock().reset();
}
}

@Test
Expand Down

0 comments on commit 773b964

Please sign in to comment.