Skip to content

Commit

Permalink
refactor(engine): Fix Tests for null & Empty createTimeConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
psavidis committed Nov 8, 2023
1 parent ccf8675 commit 5d7a359
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,7 @@ public void setIncludeExtensionProperties(boolean includeExtensionProperties) {
}

public ExternalTaskQueryBuilder buildQuery(ProcessEngine processEngine) {

var configDto = getCreateTimeConfig();
var config = getConfigFromDto(configDto);
var config = getConfigFromDto();

ExternalTaskQueryBuilder fetchBuilder = processEngine
.getExternalTaskService()
Expand Down Expand Up @@ -297,7 +295,9 @@ public ExternalTaskQueryBuilder buildQuery(ProcessEngine processEngine) {
return fetchBuilder;
}

protected CreateTimeConfig getConfigFromDto(CreateTimeConfigDto dto) {
protected CreateTimeConfig getConfigFromDto() {
var dto = getCreateTimeConfig();

if (dto == null || !dto.isUseCreateTime()) {
return EMPTY;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,9 @@
* EMPTY configuration means ignoring the field at all.
*/
public enum CreateTimeConfig {
ASC, DESC, EMPTY
ASC, DESC, EMPTY;

public boolean isEmpty() {
return this == EMPTY;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ public ExternalTaskQueryBuilder fetchAndLock(int maxTasks, String workerId) {

@Override
public ExternalTaskQueryBuilder fetchAndLock(int maxTasks, String workerId, boolean usePriority, CreateTimeConfig createTimeConfig) {
var direction = getDirection(createTimeConfig);
var useCreateTime = useCreateTime(createTimeConfig);
var direction = useCreateTime ? getDirection(createTimeConfig) : null;

return new ExternalTaskQueryTopicBuilderImpl(commandExecutor, workerId, maxTasks, usePriority, true, direction);
return new ExternalTaskQueryTopicBuilderImpl(commandExecutor, workerId, maxTasks, usePriority, useCreateTime, direction);
}

@Override
Expand Down Expand Up @@ -171,13 +172,17 @@ public void extendLock(String externalTaskId, String workerId, long lockDuration
}

protected Direction getDirection(CreateTimeConfig config) {
if (config == null || config == EMPTY) {
if (config == null || config.isEmpty()) {
return Direction.DESCENDING; // default order in case of null or empty config
}

return Direction.findByNameIgnoreCase(config.name());
}

protected boolean useCreateTime(CreateTimeConfig config) {
return config != null && !config.isEmpty();
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ public void shouldFetchWithCreateTimeDESCWithoutPriority() {
"org/camunda/bpm/engine/test/api/externaltask/twoExternalTaskWithPriorityProcess.bpmn20.xml"
})
@Test
public void shouldFetchWithDefaultDESCOrderWhenCreateTimeOrderIsEmpty() {
public void shouldIgnoreCreateTimeConfigWhenOrderIsEmpty() {
// given
runtimeService.startProcessInstanceByKey("twoExternalTaskWithPriorityProcess"); // priority 7 & null
ClockTestUtil.incrementClock(60_000);
Expand All @@ -334,26 +334,26 @@ public void shouldFetchWithDefaultDESCOrderWhenCreateTimeOrderIsEmpty() {
runtimeService.startProcessInstanceByKey("oneExternalTaskProcess"); // null priority

// when
var result = externalTaskService.fetchAndLock(6, WORKER_ID, false, EMPTY)
var result = externalTaskService.fetchAndLock(6, WORKER_ID, true, EMPTY)
.topic(TOPIC_NAME, LOCK_TIME)
.execute();

assertThat(result.size()).isEqualTo(5);

// then
// DESC order will be used as a 'sensible' default order
assertThat(result.get(0).getCreateTime()).isAfterOrEqualsTo(result.get(1).getCreateTime());
assertThat(result.get(1).getCreateTime()).isAfterOrEqualsTo(result.get(2).getCreateTime());
assertThat(result.get(2).getCreateTime()).isAfterOrEqualsTo(result.get(3).getCreateTime());
assertThat(result.get(3).getCreateTime()).isAfterOrEqualsTo(result.get(4).getCreateTime());
// create time ordering will be ignored, only priority will be used
assertThat(result.get(0).getPriority()).isGreaterThanOrEqualTo(result.get(1).getPriority());
assertThat(result.get(1).getPriority()).isGreaterThanOrEqualTo(result.get(2).getPriority());
assertThat(result.get(2).getPriority()).isGreaterThanOrEqualTo(result.get(3).getPriority());
assertThat(result.get(3).getPriority()).isGreaterThanOrEqualTo(result.get(4).getPriority());
}

@Deployment(resources = {
"org/camunda/bpm/engine/test/api/externaltask/oneExternalTaskProcess.bpmn20.xml",
"org/camunda/bpm/engine/test/api/externaltask/twoExternalTaskWithPriorityProcess.bpmn20.xml"
})
@Test
public void shouldFetchWithDefaultDESCOrderWhenCreateTimeOrderIsNull() {
public void shouldIgnoreCreateTimeConfigWhenOrderIsNull() {
// given
runtimeService.startProcessInstanceByKey("twoExternalTaskWithPriorityProcess"); // priority 7 & null
ClockTestUtil.incrementClock(60_000);
Expand All @@ -362,18 +362,18 @@ public void shouldFetchWithDefaultDESCOrderWhenCreateTimeOrderIsNull() {
runtimeService.startProcessInstanceByKey("oneExternalTaskProcess"); // null priority

// when
var result = externalTaskService.fetchAndLock(6, WORKER_ID, false, null)
var result = externalTaskService.fetchAndLock(6, WORKER_ID, true, null)
.topic(TOPIC_NAME, LOCK_TIME)
.execute();

assertThat(result.size()).isEqualTo(5);

// then
// DESC order will be used as a 'sensible' default order
assertThat(result.get(0).getCreateTime()).isAfterOrEqualsTo(result.get(1).getCreateTime());
assertThat(result.get(1).getCreateTime()).isAfterOrEqualsTo(result.get(2).getCreateTime());
assertThat(result.get(2).getCreateTime()).isAfterOrEqualsTo(result.get(3).getCreateTime());
assertThat(result.get(3).getCreateTime()).isAfterOrEqualsTo(result.get(4).getCreateTime());
// create time ordering will be ignored, only priority will be used
assertThat(result.get(0).getPriority()).isGreaterThanOrEqualTo(result.get(1).getPriority());
assertThat(result.get(1).getPriority()).isGreaterThanOrEqualTo(result.get(2).getPriority());
assertThat(result.get(2).getPriority()).isGreaterThanOrEqualTo(result.get(3).getPriority());
assertThat(result.get(3).getPriority()).isGreaterThanOrEqualTo(result.get(4).getPriority());
}

@Deployment(resources = "org/camunda/bpm/engine/test/api/externaltask/externalTaskPriorityProcess.bpmn20.xml")
Expand Down

0 comments on commit 5d7a359

Please sign in to comment.