Skip to content

Commit

Permalink
AAE-28084 Add new fields to task filters and process instance filters (
Browse files Browse the repository at this point in the history
…#1642)

* AAE-28084 add required fields and tests

* AAE-28084 remove blank line
  • Loading branch information
tom-dal authored Dec 11, 2024
1 parent 91729a9 commit 5e1d632
Show file tree
Hide file tree
Showing 14 changed files with 319 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
import java.util.Set;
import org.activiti.cloud.services.query.rest.filter.VariableFilter;

public interface ProcessVariableFilterRequest {
public interface CloudRuntimeEntityFilterRequest {
Set<String> id();
Set<String> parentId();
Set<VariableFilter> processVariableFilters();

CloudRuntimeEntitySort sort();
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

//prettier-ignore
public record ProcessInstanceSearchRequest(
Set<String> id,
Set<String> parentId,
Set<String> name,
Set<String> processDefinitionName,
Set<String> initiator,
Expand All @@ -39,4 +41,4 @@ public record ProcessInstanceSearchRequest(
Set<VariableFilter> processVariableFilters,
Set<ProcessVariableKey> processVariableKeys,
CloudRuntimeEntitySort sort
) implements ProcessVariableFilterRequest{}
) implements CloudRuntimeEntityFilterRequest {}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
public record TaskSearchRequest (
boolean onlyStandalone,
boolean onlyRoot,
Set<String> id,
Set<String> parentId,
Set<String> processInstanceId,
Set<String> name,
Set<String> description,
Set<String> processDefinitionName,
Expand All @@ -48,4 +51,4 @@ public record TaskSearchRequest (
Set<VariableFilter> processVariableFilters,
Set<ProcessVariableKey> processVariableKeys,
CloudRuntimeEntitySort sort
) implements ProcessVariableFilterRequest {}
) implements CloudRuntimeEntityFilterRequest {}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public Predicate toPredicate(
) {
reset();
applyUserRestrictionFilter(root, criteriaBuilder);
applyIdFilter(root);
applyParentIdFilter(root);
applyNameFilter(root, criteriaBuilder);
applyProcessDefinitionNameFilter(root);
applyInitiatorFilter(root);
Expand All @@ -80,6 +82,12 @@ protected SetAttribute<ProcessInstanceEntity, ProcessVariableEntity> getProcessV
return ProcessInstanceEntity_.variables;
}

private void applyParentIdFilter(Root<ProcessInstanceEntity> root) {
if (!CollectionUtils.isEmpty(searchRequest.parentId())) {
predicates.add(root.get(ProcessInstanceEntity_.parentId).in(searchRequest.parentId()));
}
}

private void applyNameFilter(Root<ProcessInstanceEntity> root, CriteriaBuilder criteriaBuilder) {
if (!CollectionUtils.isEmpty(searchRequest.name())) {
addLikeFilters(predicates, searchRequest.name(), root, criteriaBuilder, ProcessInstanceEntity_.name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@
import org.activiti.cloud.services.query.model.ProcessVariableEntity;
import org.activiti.cloud.services.query.model.ProcessVariableEntity_;
import org.activiti.cloud.services.query.rest.filter.VariableType;
import org.activiti.cloud.services.query.rest.payload.CloudRuntimeEntityFilterRequest;
import org.activiti.cloud.services.query.rest.payload.CloudRuntimeEntitySort;
import org.activiti.cloud.services.query.rest.payload.ProcessVariableFilterRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.http.HttpStatus;
import org.springframework.util.CollectionUtils;
import org.springframework.web.server.ResponseStatusException;

public abstract class SpecificationSupport<T, R extends ProcessVariableFilterRequest> implements Specification<T> {
public abstract class SpecificationSupport<T, R extends CloudRuntimeEntityFilterRequest> implements Specification<T> {

protected final R searchRequest;
protected List<Predicate> predicates;
Expand Down Expand Up @@ -132,6 +132,12 @@ protected void applyProcessVariableFilters(
}
}

protected void applyIdFilter(Root<T> root) {
if (!CollectionUtils.isEmpty(searchRequest.id())) {
predicates.add(root.get(getIdAttribute()).in(searchRequest.id()));
}
}

protected void addLikeFilters(
Collection<Predicate> predicates,
Set<String> valuesToFilter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ public Predicate toPredicate(Root<TaskEntity> root, CriteriaQuery<?> query, Crit
reset();
applyUserRestrictionFilter(root, criteriaBuilder);
applyRootTasksFilter(root, criteriaBuilder);
applyIdFilter(root);
applyParentIdFilter(root);
applyProcessInstanceIdFilter(root);
applyStandaloneFilter(root, criteriaBuilder);
applyNameFilter(root, criteriaBuilder);
applyDescriptionFilter(root, criteriaBuilder);
Expand Down Expand Up @@ -132,6 +135,18 @@ protected SetAttribute<TaskEntity, ProcessVariableEntity> getProcessVariablesAtt
return TaskEntity_.processVariables;
}

private void applyParentIdFilter(Root<TaskEntity> root) {
if (!CollectionUtils.isEmpty(searchRequest.parentId())) {
predicates.add(root.get(TaskEntity_.parentTaskId).in(searchRequest.parentId()));
}
}

private void applyProcessInstanceIdFilter(Root<TaskEntity> root) {
if (!CollectionUtils.isEmpty(searchRequest.processInstanceId())) {
predicates.add(root.get(TaskEntity_.processInstanceId).in(searchRequest.processInstanceId()));
}
}

private void applyProcessDefinitionNameFilter(Root<TaskEntity> root, CriteriaBuilder criteriaBuilder) {
if (!CollectionUtils.isEmpty(searchRequest.processDefinitionName())) {
addLikeFilters(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import java.math.BigDecimal;
import java.util.Date;
import java.util.stream.IntStream;
import org.activiti.QueryRestTestApplication;
import org.activiti.api.process.model.ProcessInstance;
import org.activiti.cloud.alfresco.config.AlfrescoWebAutoConfiguration;
Expand Down Expand Up @@ -82,6 +83,50 @@ void cleanUp() {

protected abstract String getSearchEndpoint();

@Test
void should_returnProcessInstances_filteredById() {
IntStream
.range(0, 3)
.forEach(i -> queryTestUtils.buildProcessInstance().withId("id" + i).withInitiator(USER).buildAndSave());

ProcessInstanceSearchRequest request = new ProcessInstanceSearchRequestBuilder().withIds("id0", "id2").build();

given()
.contentType(MediaType.APPLICATION_JSON)
.body(request)
.when()
.post(getSearchEndpoint())
.then()
.statusCode(200)
.body(PROCESS_INSTANCES_JSON_PATH, hasSize(2))
.body(PROCESS_INSTANCE_IDS_JSON_PATH, contains("id0", "id2"));
}

@Test
void should_returnProcessInstances_filteredByParentId() {
queryTestUtils.buildProcessInstance().withId("id1").withInitiator(USER).withParentId("parent1").buildAndSave();

queryTestUtils.buildProcessInstance().withId("id2").withInitiator(USER).withParentId("parent2").buildAndSave();

queryTestUtils.buildProcessInstance().withId("id3").withInitiator(USER).withParentId("parent3").buildAndSave();

queryTestUtils.buildProcessInstance().withId("id4").withInitiator(USER).buildAndSave();

ProcessInstanceSearchRequest request = new ProcessInstanceSearchRequestBuilder()
.withParentIds("parent1", "parent3")
.build();

given()
.contentType(MediaType.APPLICATION_JSON)
.body(request)
.when()
.post(getSearchEndpoint())
.then()
.statusCode(200)
.body(PROCESS_INSTANCES_JSON_PATH, hasSize(2))
.body(PROCESS_INSTANCE_IDS_JSON_PATH, contains("id1", "id3"));
}

@Test
void should_returnProcessInstances_filteredByNameLike() {
ProcessInstanceEntity processInstance1 = queryTestUtils
Expand Down Expand Up @@ -2514,7 +2559,7 @@ void should_returnProcessInstances_sortedBy_BooleanProcessVariable() {
}

/**
* From Postgres documentation: https://www.postgresql.org/docs/current/queries-order.html
* From Postgres documentation: <a href="https://www.postgresql.org/docs/current/queries-order.html">Postgres sorting</a>
* By default, null values sort as if larger than any non-null value;
* that is, NULLS FIRST is the default for DESC order, and NULLS LAST otherwise.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,104 @@ void should_returnTasks_withOnlyRequestedProcessVariables() {
.body(TASKS_JSON_PATH + "[0].processVariables[0].name", is(VAR_NAME));
}

@Test
void should_returnTasks_filteredById() {
IntStream
.range(0, 3)
.forEach(i -> queryTestUtils.buildTask().withId("id" + i).withAssignee(CURRENT_USER).buildAndSave());

TaskSearchRequest request = new TaskSearchRequestBuilder().withId("id0", "id2").build();

given()
.contentType(MediaType.APPLICATION_JSON)
.body(request)
.when()
.post(getSearchEndpointHttpPost())
.then()
.statusCode(200)
.body(TASKS_JSON_PATH, hasSize(2))
.body(TASK_IDS_JSON_PATH, contains("id0", "id2"));
}

@Test
void should_returnTasks_filteredByParentId() {
TaskEntity parent1 = queryTestUtils.buildTask().withAssignee(CURRENT_USER).buildAndSave();
TaskEntity parent2 = queryTestUtils.buildTask().withAssignee(CURRENT_USER).buildAndSave();

queryTestUtils.buildTask().withId(TASK_ID_1).withAssignee(CURRENT_USER).withParentTask(parent1).buildAndSave();

queryTestUtils
.buildTask()
.withAssignee(CURRENT_USER)
.withParentTask(queryTestUtils.buildTask().buildAndSave())
.buildAndSave();

queryTestUtils.buildTask().withId(TASK_ID_3).withAssignee(CURRENT_USER).withParentTask(parent2).buildAndSave();

TaskSearchRequest request = new TaskSearchRequestBuilder()
.withParentId(parent1.getId(), parent2.getId())
.build();

given()
.contentType(MediaType.APPLICATION_JSON)
.body(request)
.when()
.post(getSearchEndpointHttpPost())
.then()
.statusCode(200)
.body(TASKS_JSON_PATH, hasSize(2))
.body(TASK_IDS_JSON_PATH, containsInAnyOrder(TASK_ID_1, TASK_ID_3));
}

@Test
void should_returnTasks_filteredByProcessInstanceId() {
ProcessInstanceEntity processInstance1 = queryTestUtils
.buildProcessInstance()
.withInitiator(CURRENT_USER)
.withProcessDefinitionKey(PROCESS_DEFINITION_KEY)
.buildAndSave();

ProcessInstanceEntity processInstance2 = queryTestUtils
.buildProcessInstance()
.withInitiator(CURRENT_USER)
.withProcessDefinitionKey(UUID.randomUUID().toString())
.buildAndSave();

queryTestUtils
.buildTask()
.withId(TASK_ID_1)
.withAssignee(CURRENT_USER)
.withParentProcess(processInstance1)
.buildAndSave();

queryTestUtils
.buildTask()
.withAssignee(CURRENT_USER)
.withParentProcess(queryTestUtils.buildProcessInstance().buildAndSave())
.buildAndSave();

queryTestUtils
.buildTask()
.withId(TASK_ID_3)
.withAssignee(CURRENT_USER)
.withParentProcess(processInstance2)
.buildAndSave();

TaskSearchRequest request = new TaskSearchRequestBuilder()
.withProcessInstanceId(processInstance1.getId(), processInstance2.getId())
.build();

given()
.contentType(MediaType.APPLICATION_JSON)
.body(request)
.when()
.post(getSearchEndpointHttpPost())
.then()
.statusCode(200)
.body(TASKS_JSON_PATH, hasSize(2))
.body(TASK_IDS_JSON_PATH, containsInAnyOrder(TASK_ID_1, TASK_ID_3));
}

@Test
void should_returnTasks_filteredByProcessVariable_withOnlyRequestedProcessVariables() {
queryTestUtils
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
*/
package org.activiti.cloud.services.query.rest;

import static org.activiti.cloud.services.query.util.ProcessInstanceTestUtils.createProcessInstanceSearchRequest;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;

Expand All @@ -27,6 +28,7 @@
import org.activiti.cloud.services.query.model.ProcessInstanceEntity;
import org.activiti.cloud.services.query.rest.helper.ProcessInstanceControllerHelper;
import org.activiti.cloud.services.query.rest.payload.ProcessInstanceSearchRequest;
import org.activiti.cloud.services.query.util.ProcessInstanceSearchRequestBuilder;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
Expand Down Expand Up @@ -107,15 +109,15 @@ void findById_shouldReturnProcessInstance() {
@Test
void searchProcessInstances_shouldReturnProcessInstances() {
//given
ProcessInstanceSearchRequest searchRequest = createProcessInstanceSearchRequest();
Pageable pageable = PageRequest.of(0, 10);
Page<ProcessInstanceEntity> pageResult = new PageImpl<>(Collections.singletonList(new ProcessInstanceEntity()));
given(processInstanceService.search(searchRequest, pageable)).willReturn(pageResult);
given(processInstanceService.search(any(ProcessInstanceSearchRequest.class), eq(pageable)))
.willReturn(pageResult);
given(processInstanceRepository.mapSubprocesses(pageResult, pageable)).willReturn(pageResult);

//when
Page<ProcessInstanceEntity> result = processInstanceControllerHelper.searchProcessInstances(
searchRequest,
new ProcessInstanceSearchRequestBuilder().build(),
pageable
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public ProcessInstanceBuilder withId(String id) {
return this;
}

public ProcessInstanceBuilder withParentId(String parentId) {
process.setParentId(parentId);
return this;
}

public ProcessInstanceBuilder withName(String name) {
process.setName(name);
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@

public class ProcessInstanceSearchRequestBuilder {

private Set<String> ids;
private Set<String> parentIds;
private Set<String> names;
private Set<String> processDefinitionNames;
private Set<String> initiators;
Expand All @@ -48,6 +50,16 @@ public class ProcessInstanceSearchRequestBuilder {
private Set<ProcessVariableKey> processVariableKeys;
private CloudRuntimeEntitySort sort;

public ProcessInstanceSearchRequestBuilder withIds(String... ids) {
this.ids = Set.of(ids);
return this;
}

public ProcessInstanceSearchRequestBuilder withParentIds(String... parentIds) {
this.parentIds = Set.of(parentIds);
return this;
}

public ProcessInstanceSearchRequestBuilder withNames(String... names) {
this.names = Set.of(names);
return this;
Expand Down Expand Up @@ -157,6 +169,8 @@ public ProcessInstanceSearchRequest build() {
}
}
return new ProcessInstanceSearchRequest(
ids,
parentIds,
names,
processDefinitionNames,
initiators,
Expand Down
Loading

0 comments on commit 5e1d632

Please sign in to comment.