Skip to content

Commit

Permalink
Closes #2338 - Add query of tasks without owner
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesrdi committed Aug 3, 2023
1 parent b2dabba commit b40eaa1
Show file tree
Hide file tree
Showing 6 changed files with 474 additions and 213 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public static StringBuilder whereIn(String collection, String column, StringBuil
.append("</choose>");
if (column.matches("t.CUSTOM_\\d+")) {
sb.append("<if test='" + collection + "ContainsNull'> OR " + column + " IS NULL </if>");
} else if (column.matches("t.OWNER")) {
sb.append("<if test='" + collection + "ContainsNull'> OR " + column + " IS NULL </if>");
}
return sb.append(")</if> ");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1814,6 +1814,7 @@ class Owner {
TaskSummary taskSummary1;
TaskSummary taskSummary2;
TaskSummary taskSummary3;
TaskSummary taskSummary4;

@WithAccessId(user = "user-1-1")
@BeforeAll
Expand All @@ -1822,6 +1823,7 @@ void setup() throws Exception {
taskSummary1 = taskInWorkbasket(wb).owner("user-2-1").buildAndStoreAsSummary(taskService);
taskSummary2 = taskInWorkbasket(wb).owner("user-1-2").buildAndStoreAsSummary(taskService);
taskSummary3 = taskInWorkbasket(wb).owner("user-1-3").buildAndStoreAsSummary(taskService);
taskSummary4 = taskInWorkbasket(wb).owner(null).buildAndStoreAsSummary(taskService);
}

@WithAccessId(user = "user-1-1")
Expand Down Expand Up @@ -1859,6 +1861,29 @@ void should_ApplyFilter_When_QueryingForOwnerNotLike() {

assertThat(list).containsExactly(taskSummary1);
}

@WithAccessId(user = "user-1-1")
@Test
void should_ReturnTaskWithOwnerNull_When_QueryingForOwnerIn() {
String[] nullArray = {null};
List<TaskSummary> list =
taskService.createTaskQuery().workbasketIdIn(wb.getId()).ownerIn(nullArray).list();

assertThat(list).containsExactly(taskSummary4);
}

@WithAccessId(user = "user-1-1")
@Test
void should_ReturnTaskWithOwnerNullAndUser11_When_QueryingForOwnerIn() {
List<TaskSummary> list =
taskService
.createTaskQuery()
.workbasketIdIn(wb.getId())
.ownerIn("user-1-2", null)
.list();

assertThat(list).containsExactlyInAnyOrder(taskSummary2, taskSummary4);
}
}

@Nested
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ public class TaskQueryImpl implements TaskQuery {
private String[] parentBusinessProcessIdLike;
private String[] parentBusinessProcessIdNotLike;
private String[] ownerIn;
private boolean ownerInContainsNull;
private String[] ownerNotIn;
private String[] ownerLike;
private String[] ownerNotLike;
Expand Down Expand Up @@ -865,7 +866,15 @@ public TaskQuery orderByParentBusinessProcessId(SortDirection sortDirection) {

@Override
public TaskQuery ownerIn(String... owners) {
this.ownerIn = owners;
List<String> conditionList = new ArrayList<>(Arrays.asList(owners));
boolean containsNull = conditionList.contains(null);
if (containsNull) {
conditionList.remove(null);
ownerInContainsNull = true;
this.ownerIn = conditionList.toArray(new String[owners.length - 1]);
} else {
this.ownerIn = owners;
}
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.servlet.http.HttpServletRequest;
import pro.taskana.common.api.exceptions.InvalidArgumentException;

public class QueryParamsValidator {

Expand All @@ -32,5 +35,23 @@ public static void validateParams(HttpServletRequest request, Class<?>... filter
if (!providedParams.isEmpty()) {
throw new IllegalArgumentException("Unknown request parameters found: " + providedParams);
}
checkExactParam(request, "owner-is-null");
}

private static void checkExactParam(HttpServletRequest request, String queryParameter) {
String queryString = request.getQueryString();
boolean hasOwnerIsNullParam = queryString != null && queryString.contains(queryParameter);
if (hasOwnerIsNullParam) {
Pattern pattern = Pattern.compile("\\b" + queryParameter + "(&|$)");
// Create a Matcher object to perform the match
Matcher matcher = pattern.matcher(queryString);

// Check if the "owner-is-null" substring is present in the query string
boolean hasExactOwnerIsNullParam = matcher.find();
if (!hasExactOwnerIsNullParam) {
throw new InvalidArgumentException(
"It is prohibited to use the param " + queryParameter + " with values.");
}
}
}
}
Loading

0 comments on commit b40eaa1

Please sign in to comment.