Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
jbarrez committed Jan 25, 2024
1 parent 32f63d4 commit 9ae1694
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.flowable.common.engine.api.FlowableException;
import org.flowable.common.engine.api.FlowableIllegalArgumentException;
import org.flowable.common.engine.api.FlowableObjectNotFoundException;
import org.flowable.engine.impl.test.PluggableFlowableTestCase;
import org.flowable.engine.runtime.ActivityInstance;
import org.flowable.engine.runtime.Execution;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.engine.test.Deployment;
Expand Down Expand Up @@ -990,4 +991,115 @@ public void testQueryByParentScopeId() {
);
}

@Test
@Deployment(resources = {
"org/flowable/engine/test/api/oneTaskProcess.bpmn20.xml"
})
public void testQueryByVariousTimeConstraints() {

// Test for https://github.com/flowable/flowable-engine/issues/3827

Date testStartTime = new Date();
processEngineConfiguration.getClock().setCurrentTime(testStartTime);

Set<String> processInstanceIds = new HashSet<>();
for (int i = 0; i < 4; i++){
processInstanceIds.add(runtimeService.startProcessInstanceByKey("oneTaskProcess").getId());
}

Date afterInstancesStartedTime = new Date(testStartTime.getTime() + 10_000);
processEngineConfiguration.getClock().setCurrentTime(afterInstancesStartedTime);

for (String processInstanceId : processInstanceIds) {

assertThat(historyService.createHistoricTaskInstanceQuery().processInstanceId(processInstanceId).count()).isOne();
assertThat(historyService.createHistoricTaskInstanceQuery().processInstanceIdIn(Collections.singletonList(processInstanceId)).count()).isOne();

// Start time
assertThat(historyService.createHistoricTaskInstanceQuery().processInstanceIdIn(Collections.singletonList(processInstanceId))
.taskCreatedAfter(new Date(testStartTime.getTime() - 10_000)).count()).isOne();
assertThat(historyService.createHistoricTaskInstanceQuery().processInstanceIdIn(Collections.singletonList(processInstanceId))
.taskCreatedBefore(new Date(afterInstancesStartedTime.getTime())).count()).isOne();

// Claim --> change state
taskService.claim(taskService.createTaskQuery().processInstanceId(processInstanceId).singleResult().getId(), "johnDoe");
}

Date afterClaimTime = new Date(afterInstancesStartedTime.getTime() + 10_000);
processEngineConfiguration.getClock().setCurrentTime(afterClaimTime);

for (String processInstanceId : processInstanceIds) {
// State
assertThat(historyService.createHistoricTaskInstanceQuery().processInstanceIdIn(Collections.singletonList(processInstanceId))
.taskState(Task.CLAIMED).count()).isOne();
assertThat(historyService.createHistoricTaskInstanceQuery().processInstanceIdIn(Collections.singletonList(processInstanceId))
.taskClaimedBy("johnDoe").count()).isOne();

// Claim time
assertThat(historyService.createHistoricTaskInstanceQuery().processInstanceIdIn(Collections.singletonList(processInstanceId))
.taskClaimedAfter(new Date(afterInstancesStartedTime.getTime() - 10_000)).count()).isOne();
assertThat(historyService.createHistoricTaskInstanceQuery().processInstanceIdIn(Collections.singletonList(processInstanceId))
.taskClaimedBefore(new Date(afterClaimTime.getTime())).count()).isOne();

// Make tasks in progress
taskService.startProgress(taskService.createTaskQuery().processInstanceId(processInstanceId).singleResult().getId(), "johnDoe");
}

Date afterInProgressTime = new Date(afterClaimTime.getTime() + 10_000);
processEngineConfiguration.getClock().setCurrentTime(afterInProgressTime);

for (String processInstanceId : processInstanceIds) {
assertThat(historyService.createHistoricTaskInstanceQuery().processInstanceIdIn(Collections.singletonList(processInstanceId))
.taskState(Task.IN_PROGRESS).count()).isOne();
assertThat(historyService.createHistoricTaskInstanceQuery().processInstanceIdIn(Collections.singletonList(processInstanceId))
.taskInProgressStartedBy("johnDoe").count()).isOne();

// In progress time
assertThat(historyService.createHistoricTaskInstanceQuery().processInstanceIdIn(Collections.singletonList(processInstanceId))
.taskInProgressStartTimeAfter(new Date(afterClaimTime.getTime() - 10_000)).count()).isOne();
assertThat(historyService.createHistoricTaskInstanceQuery().processInstanceIdIn(Collections.singletonList(processInstanceId))
.taskInProgressStartTimeBefore(new Date(afterInProgressTime.getTime())).count()).isOne();

// Suspend the tasks
taskService.suspendTask(taskService.createTaskQuery().processInstanceId(processInstanceId).singleResult().getId(), "johnDoe");
}

Date afterSuspendTime = new Date(afterInProgressTime.getTime() + 10_000);
processEngineConfiguration.getClock().setCurrentTime(afterSuspendTime);

for (String processInstanceId : processInstanceIds) {
assertThat(historyService.createHistoricTaskInstanceQuery().processInstanceIdIn(Collections.singletonList(processInstanceId))
.taskState(Task.SUSPENDED).count()).isOne();
assertThat(historyService.createHistoricTaskInstanceQuery().processInstanceIdIn(Collections.singletonList(processInstanceId))
.taskSuspendedBy("johnDoe").count()).isOne();

// Suspend time
assertThat(historyService.createHistoricTaskInstanceQuery().processInstanceIdIn(Collections.singletonList(processInstanceId))
.taskSuspendedAfter(new Date(afterInProgressTime.getTime() - 10_000)).count()).isOne();
assertThat(historyService.createHistoricTaskInstanceQuery().processInstanceIdIn(Collections.singletonList(processInstanceId))
.taskSuspendedBefore(new Date(afterSuspendTime.getTime())).count()).isOne();

// Complete the tasks
taskService.activateTask(taskService.createTaskQuery().processInstanceId(processInstanceId).singleResult().getId(), "johnDoe");
taskService.complete(taskService.createTaskQuery().processInstanceId(processInstanceId).singleResult().getId(), "johnDoe");
}

Date afterCompleteTime = new Date(afterSuspendTime.getTime() + 10_000);
processEngineConfiguration.getClock().setCurrentTime(afterSuspendTime);

for (String processInstanceId : processInstanceIds) {
assertThat(historyService.createHistoricTaskInstanceQuery().processInstanceIdIn(Collections.singletonList(processInstanceId))
.taskState(Task.COMPLETED).count()).isOne();
assertThat(historyService.createHistoricTaskInstanceQuery().processInstanceIdIn(Collections.singletonList(processInstanceId))
.taskCompletedBy("johnDoe").count()).isOne();

// Completion time
assertThat(historyService.createHistoricTaskInstanceQuery().processInstanceIdIn(Collections.singletonList(processInstanceId))
.taskCompletedAfter(new Date(afterSuspendTime.getTime() - 10_000)).count()).isOne();
assertThat(historyService.createHistoricTaskInstanceQuery().processInstanceIdIn(Collections.singletonList(processInstanceId))
.taskCompletedBefore(new Date(afterCompleteTime.getTime())).count()).isOne();
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,25 @@ public interface HistoricTaskInstanceQuery extends TaskInfoQuery<HistoricTaskIns
HistoricTaskInstanceQuery taskParentTaskId(String parentTaskId);

/**
* Only select select historic task instances which are completed on the given date
* Only select historic task instances which are completed on the given date
*/
HistoricTaskInstanceQuery taskCompletedOn(Date endDate);

/**
* Only select select historic task instances which are completed before the given date
* Only select historic task instances which are completed before the given date
*/
HistoricTaskInstanceQuery taskCompletedBefore(Date endDate);

/**
* Only select select historic task instances which are completed after the given date
* Only select historic task instances which are completed after the given date
*/
HistoricTaskInstanceQuery taskCompletedAfter(Date endDate);

/**
* Only select historic task instances which are completed by the given user
*/
HistoricTaskInstanceQuery taskCompletedBy(String userId);

/**
* Only select historic tasks without a delete reason (completed normally)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1699,6 +1699,16 @@ public HistoricTaskInstanceQuery taskCompletedAfter(Date completedAfterDate) {
}
return this;
}

@Override
public HistoricTaskInstanceQuery taskCompletedBy(String completedBy) {
if (inOrStatement) {
this.currentOrQueryObject.completedBy = completedBy;
} else {
this.completedBy = completedBy;
}
return this;
}

@Override
public HistoricTaskInstanceQuery taskInProgressStartDueDate(Date dueDate) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1336,73 +1336,73 @@
and ${queryTablePrefix}END_TIME_ is not null
</if>
<if test="state != null">
or ${queryTablePrefix}STATE_ = #{state}
and ${queryTablePrefix}STATE_ = #{state}
</if>
<if test="createTime != null">
or ${queryTablePrefix}START_TIME_ = #{createTime}
and ${queryTablePrefix}START_TIME_ = #{createTime}
</if>
<if test="createTimeBefore != null">
or ${queryTablePrefix}START_TIME_ &lt; #{createTimeBefore}
and ${queryTablePrefix}START_TIME_ &lt; #{createTimeBefore}
</if>
<if test="createTimeAfter != null">
or ${queryTablePrefix}START_TIME_ &gt; #{createTimeAfter}
and ${queryTablePrefix}START_TIME_ &gt; #{createTimeAfter}
</if>
<if test="inProgressStartTime != null">
or ${queryTablePrefix}IN_PROGRESS_TIME_ = #{inProgressStartTime}
and ${queryTablePrefix}IN_PROGRESS_TIME_ = #{inProgressStartTime}
</if>
<if test="inProgressStartTimeBefore != null">
or ${queryTablePrefix}IN_PROGRESS_TIME_ &lt; #{inProgressStartTimeBefore}
and ${queryTablePrefix}IN_PROGRESS_TIME_ &lt; #{inProgressStartTimeBefore}
</if>
<if test="inProgressStartTimeAfter != null">
or ${queryTablePrefix}IN_PROGRESS_TIME_ &gt; #{inProgressStartTimeAfter}
and ${queryTablePrefix}IN_PROGRESS_TIME_ &gt; #{inProgressStartTimeAfter}
</if>
<if test="inProgressStartedBy != null">
or ${queryTablePrefix}IN_PROGRESS_STARTED_BY = #{inProgressStartedBy}
and ${queryTablePrefix}IN_PROGRESS_STARTED_BY_ = #{inProgressStartedBy}
</if>
<if test="claimTime != null">
or ${queryTablePrefix}CLAIM_TIME_ = #{claimTime}
and ${queryTablePrefix}CLAIM_TIME_ = #{claimTime}
</if>
<if test="claimTimeBefore != null">
or ${queryTablePrefix}CLAIM_TIME_ &lt; #{claimTimeBefore}
and ${queryTablePrefix}CLAIM_TIME_ &lt; #{claimTimeBefore}
</if>
<if test="claimTimeAfter != null">
or ${queryTablePrefix}CLAIM_TIME_ &gt; #{claimTimeAfter}
and ${queryTablePrefix}CLAIM_TIME_ &gt; #{claimTimeAfter}
</if>
<if test="claimedBy != null">
or ${queryTablePrefix}CLAIMED_BY = #{claimedBy}
and ${queryTablePrefix}CLAIMED_BY_ = #{claimedBy}
</if>
<if test="suspendedTime != null">
or ${queryTablePrefix}SUSPENDED_TIME_ = #{suspendedTime}
and ${queryTablePrefix}SUSPENDED_TIME_ = #{suspendedTime}
</if>
<if test="suspendedTimeBefore != null">
or ${queryTablePrefix}SUSPENDED_TIME_ &lt; #{suspendedTimeBefore}
and ${queryTablePrefix}SUSPENDED_TIME_ &lt; #{suspendedTimeBefore}
</if>
<if test="suspendedTimeAfter != null">
or ${queryTablePrefix}SUSPENDED_TIME_ &gt; #{suspendedTimeAfter}
and ${queryTablePrefix}SUSPENDED_TIME_ &gt; #{suspendedTimeAfter}
</if>
<if test="suspendedBy != null">
or ${queryTablePrefix}SUSPENDED_BY = #{suspendedBy}
and ${queryTablePrefix}SUSPENDED_BY_ = #{suspendedBy}
</if>
<if test="completedTime != null">
or ${queryTablePrefix}END_TIME_ = #{completedTime}
and ${queryTablePrefix}END_TIME_ = #{completedTime}
</if>
<if test="completedTimeBefore != null">
or ${queryTablePrefix}END_TIME_ &lt; #{completedTimeBefore}
and ${queryTablePrefix}END_TIME_ &lt; #{completedTimeBefore}
</if>
<if test="completedTimeAfter != null">
or ${queryTablePrefix}END_TIME_ &gt; #{completedTimeAfter}
and ${queryTablePrefix}END_TIME_ &gt; #{completedTimeAfter}
</if>
<if test="completedBy != null">
or ${queryTablePrefix}COMPLETED_BY = #{completedBy}
and ${queryTablePrefix}COMPLETED_BY_ = #{completedBy}
</if>
<if test="inProgressStartDueDate != null">
or ${queryTablePrefix}IN_PROGRESS_DUE_DATE_ = #{inProgressStartDueDate}
and ${queryTablePrefix}IN_PROGRESS_DUE_DATE_ = #{inProgressStartDueDate}
</if>
<if test="inProgressStartDueBefore != null">
or ${queryTablePrefix}IN_PROGRESS_DUE_DATE_ &lt; #{inProgressStartDueBefore}
and ${queryTablePrefix}IN_PROGRESS_DUE_DATE_ &lt; #{inProgressStartDueBefore}
</if>
<if test="inProgressStartDueAfter != null">
or ${queryTablePrefix}IN_PROGRESS_DUE_DATE_ &gt; #{inProgressStartDueAfter}
and ${queryTablePrefix}IN_PROGRESS_DUE_DATE_ &gt; #{inProgressStartDueAfter}
</if>
<if test="dueDate != null">
and ${queryTablePrefix}DUE_DATE_ = #{dueDate}
Expand Down Expand Up @@ -1705,7 +1705,7 @@
or ${queryTablePrefix}IN_PROGRESS_TIME_ &gt; #{orQueryObject.inProgressStartTimeAfter}
</if>
<if test="orQueryObject.inProgressStartedBy != null">
or ${queryTablePrefix}IN_PROGRESS_STARTED_BY = #{orQueryObject.inProgressStartedBy}
or ${queryTablePrefix}IN_PROGRESS_STARTED_BY_ = #{orQueryObject.inProgressStartedBy}
</if>
<if test="orQueryObject.claimTime != null">
or ${queryTablePrefix}CLAIM_TIME_ = #{orQueryObject.claimTime}
Expand All @@ -1717,7 +1717,7 @@
or ${queryTablePrefix}CLAIM_TIME_ &gt; #{orQueryObject.claimTimeAfter}
</if>
<if test="orQueryObject.claimedBy != null">
or ${queryTablePrefix}CLAIMED_BY = #{orQueryObject.claimedBy}
or ${queryTablePrefix}CLAIMED_BY_ = #{orQueryObject.claimedBy}
</if>
<if test="orQueryObject.suspendedTime != null">
or ${queryTablePrefix}SUSPENDED_TIME_ = #{orQueryObject.suspendedTime}
Expand All @@ -1729,7 +1729,7 @@
or ${queryTablePrefix}SUSPENDED_TIME_ &gt; #{orQueryObject.suspendedTimeAfter}
</if>
<if test="orQueryObject.suspendedBy != null">
or ${queryTablePrefix}SUSPENDED_BY = #{orQueryObject.suspendedBy}
or ${queryTablePrefix}SUSPENDED_BY_ = #{orQueryObject.suspendedBy}
</if>
<if test="orQueryObject.completedTime != null">
or ${queryTablePrefix}END_TIME_ = #{orQueryObject.completedTime}
Expand All @@ -1741,7 +1741,7 @@
or ${queryTablePrefix}END_TIME_ &gt; #{orQueryObject.completedTimeAfter}
</if>
<if test="orQueryObject.completedBy != null">
or ${queryTablePrefix}COMPLETED_BY = #{orQueryObject.completedBy}
or ${queryTablePrefix}COMPLETED_BY_ = #{orQueryObject.completedBy}
</if>
<if test="orQueryObject.inProgressStartDueDate != null">
or ${queryTablePrefix}IN_PROGRESS_DUE_DATE_ = #{orQueryObject.inProgressStartDueDate}
Expand Down

0 comments on commit 9ae1694

Please sign in to comment.