TSK-2013 selectAndClaim TasK return Optional and does not throw Exception if Task is notFound
This commit is contained in:
parent
0d66e9b09d
commit
37280cc73b
|
@ -3,6 +3,7 @@ package pro.taskana.task.api;
|
|||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import pro.taskana.classification.api.exceptions.ClassificationNotFoundException;
|
||||
import pro.taskana.classification.api.models.Classification;
|
||||
|
@ -190,7 +191,7 @@ public interface TaskService {
|
|||
* @throws NotAuthorizedOnWorkbasketException if the current user has no {@linkplain
|
||||
* WorkbasketPermission#READ} for the {@linkplain Workbasket} the {@linkplain Task} is in
|
||||
*/
|
||||
Task selectAndClaim(TaskQuery taskQuery)
|
||||
Optional<Task> selectAndClaim(TaskQuery taskQuery)
|
||||
throws InvalidOwnerException, NotAuthorizedOnWorkbasketException;
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package pro.taskana.task.internal;
|
||||
|
||||
import static java.util.function.Predicate.not;
|
||||
import static pro.taskana.common.internal.util.CheckedFunction.wrap;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
|
@ -581,30 +582,11 @@ public class TaskServiceImpl implements TaskService {
|
|||
}
|
||||
|
||||
@Override
|
||||
public Task selectAndClaim(TaskQuery taskQuery)
|
||||
throws InvalidOwnerException, NotAuthorizedOnWorkbasketException {
|
||||
|
||||
try {
|
||||
|
||||
taskanaEngine.openConnection();
|
||||
|
||||
((TaskQueryImpl) taskQuery).selectAndClaimEquals(true);
|
||||
|
||||
TaskSummary taskSummary = taskQuery.single();
|
||||
|
||||
if (taskSummary == null) {
|
||||
throw new SystemException(
|
||||
"No tasks matched the specified filter and sorting options,"
|
||||
+ " task query returned nothing!");
|
||||
}
|
||||
|
||||
return claim(taskSummary.getId());
|
||||
|
||||
} catch (TaskNotFoundException | InvalidTaskStateException e) {
|
||||
throw new SystemException("Caught exception ", e);
|
||||
} finally {
|
||||
taskanaEngine.returnConnection();
|
||||
}
|
||||
public Optional<Task> selectAndClaim(TaskQuery taskQuery) {
|
||||
((TaskQueryImpl) taskQuery).selectAndClaimEquals(true);
|
||||
return taskanaEngine.executeInDatabaseConnection(
|
||||
() ->
|
||||
Optional.ofNullable(taskQuery.single()).map(TaskSummary::getId).map(wrap(this::claim)));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -1,23 +1,21 @@
|
|||
package acceptance.task.claim;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
import acceptance.AbstractAccTest;
|
||||
import java.security.PrivilegedAction;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
import javax.security.auth.Subject;
|
||||
import org.assertj.core.api.ThrowableAssert.ThrowingCallable;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
import pro.taskana.common.api.BaseQuery.SortDirection;
|
||||
import pro.taskana.common.api.exceptions.SystemException;
|
||||
import pro.taskana.common.api.security.UserPrincipal;
|
||||
import pro.taskana.common.internal.util.CheckedConsumer;
|
||||
import pro.taskana.common.test.security.JaasExtension;
|
||||
|
@ -58,15 +56,11 @@ class SelectAndClaimTaskAccTest extends AbstractAccTest {
|
|||
|
||||
@Test
|
||||
@WithAccessId(user = "admin")
|
||||
void should_ThrowException_When_TryingToSelectAndClaimNonExistingTask() {
|
||||
void should_ReturnEmptyOptional_When_TryingToSelectAndClaimNonExistingTask() throws Exception {
|
||||
|
||||
TaskQuery query = taskanaEngine.getTaskService().createTaskQuery().idIn("notexisting");
|
||||
ThrowingCallable call = () -> taskanaEngine.getTaskService().selectAndClaim(query);
|
||||
assertThatThrownBy(call)
|
||||
.isInstanceOf(SystemException.class)
|
||||
.hasMessageContaining(
|
||||
"No tasks matched the specified filter and sorting options, "
|
||||
+ "task query returned nothing!");
|
||||
Optional<Task> task = taskanaEngine.getTaskService().selectAndClaim(query);
|
||||
assertThat(task).isEmpty();
|
||||
}
|
||||
|
||||
private Runnable getRunnableTest(List<Task> selectedAndClaimedTasks, List<String> accessIds) {
|
||||
|
@ -76,10 +70,10 @@ class SelectAndClaimTaskAccTest extends AbstractAccTest {
|
|||
|
||||
Consumer<TaskService> consumer =
|
||||
CheckedConsumer.wrap(
|
||||
taskService -> {
|
||||
Task task = taskService.selectAndClaim(getTaskQuery());
|
||||
selectedAndClaimedTasks.add(task);
|
||||
});
|
||||
taskService ->
|
||||
taskService
|
||||
.selectAndClaim(getTaskQuery())
|
||||
.ifPresent(selectedAndClaimedTasks::add));
|
||||
PrivilegedAction<Void> action =
|
||||
() -> {
|
||||
consumer.accept(taskanaEngine.getTaskService());
|
||||
|
|
|
@ -5,6 +5,7 @@ import static java.util.function.Predicate.not;
|
|||
import java.beans.ConstructorProperties;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.stream.Collectors;
|
||||
|
@ -233,7 +234,7 @@ public class TaskController {
|
|||
* @param filterCustomFields the filter parameters regarding TaskCustomFields
|
||||
* @param filterCustomIntFields the filter parameters regarding TaskCustomIntFields
|
||||
* @param sortParameter the sort parameters
|
||||
* @return the claimed Task
|
||||
* @return the claimed Task or 404 if no Task is found
|
||||
* @throws InvalidOwnerException if the Task is already claimed by someone else
|
||||
* @throws NotAuthorizedOnWorkbasketException if the current user has no read permission for the
|
||||
* Workbasket the Task is in
|
||||
|
@ -254,9 +255,11 @@ public class TaskController {
|
|||
filterCustomIntFields.apply(query);
|
||||
sortParameter.apply(query);
|
||||
|
||||
Task selectedAndClaimedTask = taskService.selectAndClaim(query);
|
||||
Optional<Task> selectedAndClaimedTask = taskService.selectAndClaim(query);
|
||||
|
||||
return ResponseEntity.ok(taskRepresentationModelAssembler.toModel(selectedAndClaimedTask));
|
||||
return selectedAndClaimedTask
|
||||
.map(task -> ResponseEntity.ok(taskRepresentationModelAssembler.toModel(task)))
|
||||
.orElseGet(() -> ResponseEntity.notFound().build());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -51,7 +51,7 @@ public class TaskRepresentationModelAssembler
|
|||
|
||||
@NonNull
|
||||
@Override
|
||||
public TaskRepresentationModel toModel(@NonNull Task task) {
|
||||
public TaskRepresentationModel toModel(Task task) {
|
||||
TaskRepresentationModel repModel = new TaskRepresentationModel();
|
||||
repModel.setTaskId(task.getId());
|
||||
repModel.setExternalId(task.getExternalId());
|
||||
|
|
Loading…
Reference in New Issue