Skip to content

Commit

Permalink
chore(engine, engine-rest): Add CreateTime Tests to REST API
Browse files Browse the repository at this point in the history
- Add tests that verify the parameter propagation from the DTO to the builder and the correct invocation
- Add validations to the FetchAndLockBuilderImpl similar to the QueryAPI for ensuring the correct method invocation on the sorting field and the order
  • Loading branch information
psavidis committed Nov 17, 2023
1 parent 417c251 commit 8f1555e
Show file tree
Hide file tree
Showing 3 changed files with 242 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.Optional;
import java.util.function.Consumer;
import org.camunda.bpm.engine.ExternalTaskService;
import org.camunda.bpm.engine.ProcessEngine;
import org.camunda.bpm.engine.externaltask.ExternalTaskQueryTopicBuilder;
Expand All @@ -36,13 +37,6 @@
*/
public class FetchExternalTasksDto {

protected Map<String, BiConsumer<SortingDto, FetchAndLockBuilder>> SORT_BY_FIELD = Map.of(
"createTime", this::configureCreateTime
);

public static final String SORT_ORDER_ASC_VALUE = "asc";
public static final String SORT_ORDER_DESC_VALUE = "desc";

protected int maxTasks;
protected String workerId;
protected boolean usePriority = false;
Expand Down Expand Up @@ -281,17 +275,6 @@ protected FetchAndLockBuilder configureTopics(FetchAndLockBuilder builder) {
return builder;
}

protected void configureSorting(FetchAndLockBuilder builder, List<SortingDto> sortings) {
sortings = (sortings == null) ? Collections.emptyList() : sortings;

for (SortingDto sorting : sortings) {
String sortBy = sorting.getSortBy();
if (sortBy != null) {
SORT_BY_FIELD.get(sortBy).accept(sorting, builder);
}
}
}

protected FetchAndLockBuilder getBuilder(ProcessEngine engine) {
ExternalTaskService service = engine.getExternalTaskService();

Expand All @@ -300,17 +283,56 @@ protected FetchAndLockBuilder getBuilder(ProcessEngine engine) {
.maxTasks(maxTasks)
.usePriority(usePriority);

configureSorting(builder, sortings);
SortMapper mapper = new SortMapper(sortings, builder);

return builder;
return mapper.getBuilderWithSortConfigs();
}

protected void configureCreateTime(SortingDto dto, FetchAndLockBuilder builder) {
switch (dto.getSortBy()) {
case SORT_ORDER_ASC_VALUE:
builder.orderByCreateTime().asc();
case SORT_ORDER_DESC_VALUE:
builder.orderByCreateTime().desc();
/**
* Encapsulates the mapping of sorting configurations (field, order) to the respective methods builder config methods
* and applies them.
* <p>
* To achieve that, maps are used internally to map fields and orders to the corresponding builder method.
* It works with case-insensitive orders (e.g will work with "asc", "ASC").
* Fields need are case-sensitive.
*/
static class SortMapper {

protected static Map<String, Consumer<FetchAndLockBuilder>> FIELD_MAPPINGS = Map.of(
"createTime", FetchAndLockBuilder::orderByCreateTime
);

protected static Map<String, Consumer<FetchAndLockBuilder>> ORDER_MAPPINGS = Map.of(
"asc", FetchAndLockBuilder::asc,
"desc", FetchAndLockBuilder::desc
);

protected final List<SortingDto> sortings;
protected final FetchAndLockBuilder builder;

protected SortMapper(List<SortingDto> sortings, FetchAndLockBuilder builder) {
this.sortings = (sortings == null) ? Collections.emptyList() : sortings;
this.builder = builder;
}

/**
* Applies the sorting field mappings to the builder and returns it.
*/
protected FetchAndLockBuilder getBuilderWithSortConfigs() {
sortings.forEach(dto -> {
fieldMappingKey(dto).ifPresent(key -> FIELD_MAPPINGS.get(key).accept(builder));
orderMappingKey(dto).ifPresent(key -> ORDER_MAPPINGS.get(key).accept(builder));
});

return builder;
}

protected Optional<String> fieldMappingKey(SortingDto dto) {
return Optional.ofNullable(dto.getSortBy());
}

protected Optional<String> orderMappingKey(SortingDto dto) {
return Optional.ofNullable(dto.getSortOrder());
}
}

Expand Down
Loading

0 comments on commit 8f1555e

Please sign in to comment.