Search in sources :

Example 1 with InvalidStateException

use of pro.taskana.exceptions.InvalidStateException in project taskana by Taskana.

the class TaskServiceImplTest method testCompleteTaskNotForcedNotClaimedBefore.

@Test(expected = InvalidStateException.class)
public void testCompleteTaskNotForcedNotClaimedBefore() throws TaskNotFoundException, InvalidStateException, InvalidOwnerException, ClassificationNotFoundException, NotAuthorizedException {
    final boolean isForced = false;
    TaskServiceImpl cutSpy = Mockito.spy(cut);
    Classification dummyClassification = createDummyClassification();
    TaskImpl task = createUnitTestTask("1", "Unit Test Task 1", "1", dummyClassification);
    task.setState(TaskState.READY);
    task.setClaimed(null);
    doReturn(task).when(cutSpy).getTask(task.getId());
    try {
        cutSpy.completeTask(task.getId(), isForced);
    } catch (InvalidStateException e) {
        verify(taskanaEngineMock, times(1)).openConnection();
        verify(cutSpy, times(1)).getTask(task.getId());
        verify(taskanaEngineMock, times(1)).returnConnection();
        verifyNoMoreInteractions(attachmentMapperMock, taskanaEngineConfigurationMock, taskanaEngineMock, taskMapperMock, objectReferenceMapperMock, workbasketServiceMock, sqlSessionMock, classificationQueryImplMock);
        throw e;
    }
}
Also used : Classification(pro.taskana.Classification) InvalidStateException(pro.taskana.exceptions.InvalidStateException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 2 with InvalidStateException

use of pro.taskana.exceptions.InvalidStateException in project taskana by Taskana.

the class DeleteTaskAccTest method testForceDeleteTaskIfNotCompleted.

@WithAccessId(userName = "user_1_2", groupNames = { "group_1", "admin" })
@Test(expected = TaskNotFoundException.class)
public void testForceDeleteTaskIfNotCompleted() throws SQLException, TaskNotFoundException, InvalidStateException, NotAuthorizedException {
    TaskService taskService = taskanaEngine.getTaskService();
    Task task = taskService.getTask("TKI:000000000000000000000000000000000027");
    try {
        taskService.deleteTask(task.getId());
        fail("Should not be possible to delete claimed task without force flag");
    } catch (InvalidStateException ex) {
        taskService.deleteTask(task.getId(), true);
    }
    taskService.getTask("TKI:000000000000000000000000000000000027");
}
Also used : Task(pro.taskana.Task) TaskService(pro.taskana.TaskService) InvalidStateException(pro.taskana.exceptions.InvalidStateException) AbstractAccTest(acceptance.AbstractAccTest) Test(org.junit.Test) WithAccessId(pro.taskana.security.WithAccessId)

Example 3 with InvalidStateException

use of pro.taskana.exceptions.InvalidStateException in project taskana by Taskana.

the class TaskServiceImpl method cancelClaim.

@Override
public Task cancelClaim(String taskId, boolean forceUnclaim) throws TaskNotFoundException, InvalidStateException, InvalidOwnerException, NotAuthorizedException {
    String userId = CurrentUserContext.getUserid();
    LOGGER.debug("entry to cancelClaim(taskId = {}) with userId = {}, forceFlag = {}", taskId, userId, forceUnclaim);
    TaskImpl task = null;
    try {
        taskanaEngine.openConnection();
        task = (TaskImpl) getTask(taskId);
        TaskState state = task.getState();
        if (state == TaskState.COMPLETED) {
            LOGGER.warn("Method cancelClaim() found that task {} is already completed. Throwing InvalidStateException", taskId);
            throw new InvalidStateException("Task is already completed");
        }
        if (state == TaskState.CLAIMED && !forceUnclaim && !userId.equals(task.getOwner())) {
            LOGGER.warn("Method cancelClaim() found that task {} is claimed by {} and forceClaim is false. Throwing InvalidOwnerException", taskId, task.getOwner());
            throw new InvalidOwnerException("Task is already claimed by an other user = " + task.getOwner());
        }
        Instant now = Instant.now();
        task.setOwner(null);
        task.setModified(now);
        task.setClaimed(null);
        task.setRead(true);
        task.setState(TaskState.READY);
        taskMapper.update(task);
        LOGGER.debug("Method cancelClaim() unclaimed task '{}' for user '{}'.", taskId, userId);
    } finally {
        taskanaEngine.returnConnection();
        LOGGER.debug("exit from cancelClaim(taskId = {}) with userId = {}, forceFlag = {}", taskId, userId, forceUnclaim);
    }
    return task;
}
Also used : Instant(java.time.Instant) InvalidOwnerException(pro.taskana.exceptions.InvalidOwnerException) InvalidStateException(pro.taskana.exceptions.InvalidStateException) TaskState(pro.taskana.TaskState)

Example 4 with InvalidStateException

use of pro.taskana.exceptions.InvalidStateException in project taskana by Taskana.

the class TaskServiceImpl method claim.

@Override
public Task claim(String taskId, boolean forceClaim) throws TaskNotFoundException, InvalidStateException, InvalidOwnerException, NotAuthorizedException {
    String userId = CurrentUserContext.getUserid();
    LOGGER.debug("entry to claim(id = {}, forceClaim = {}, userId = {})", taskId, forceClaim, userId);
    TaskImpl task = null;
    try {
        taskanaEngine.openConnection();
        task = (TaskImpl) getTask(taskId);
        TaskState state = task.getState();
        if (state == TaskState.COMPLETED) {
            LOGGER.warn("Method claim() found that task {} is already completed. Throwing InvalidStateException", taskId);
            throw new InvalidStateException("Task is already completed");
        }
        if (state == TaskState.CLAIMED && !forceClaim && !task.getOwner().equals(userId)) {
            LOGGER.warn("Method claim() found that task {} is claimed by {} and forceClaim is false. Throwing InvalidOwnerException", taskId, task.getOwner());
            throw new InvalidOwnerException("You´re not the owner of this task and it is already claimed by other user " + task.getOwner());
        }
        Instant now = Instant.now();
        task.setOwner(userId);
        task.setModified(now);
        task.setClaimed(now);
        task.setRead(true);
        task.setState(TaskState.CLAIMED);
        taskMapper.update(task);
        LOGGER.debug("Method claim() claimed task '{}' for user '{}'.", taskId, userId);
    } finally {
        taskanaEngine.returnConnection();
        LOGGER.debug("exit from claim()");
    }
    return task;
}
Also used : Instant(java.time.Instant) InvalidOwnerException(pro.taskana.exceptions.InvalidOwnerException) InvalidStateException(pro.taskana.exceptions.InvalidStateException) TaskState(pro.taskana.TaskState)

Example 5 with InvalidStateException

use of pro.taskana.exceptions.InvalidStateException in project taskana by Taskana.

the class TaskServiceImpl method deleteTasks.

@Override
public BulkOperationResults<String, TaskanaException> deleteTasks(List<String> taskIds) throws InvalidArgumentException {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("entry to deleteTasks(tasks = {})", LoggerUtils.listToString(taskIds));
    }
    try {
        taskanaEngine.openConnection();
        if (taskIds == null) {
            throw new InvalidArgumentException("TaskIds can´t be NULL as parameter for deleteTasks().");
        }
        BulkOperationResults<String, TaskanaException> bulkLog = new BulkOperationResults<>();
        List<MinimalTaskSummary> taskSummaries = taskMapper.findExistingTasks(taskIds);
        Iterator<String> taskIdIterator = taskIds.iterator();
        while (taskIdIterator.hasNext()) {
            String currentTaskId = taskIdIterator.next();
            if (currentTaskId == null || currentTaskId.equals("")) {
                bulkLog.addError("", new InvalidArgumentException("IDs with EMPTY or NULL value are not allowed."));
                taskIdIterator.remove();
            } else {
                MinimalTaskSummary foundSummary = taskSummaries.stream().filter(taskState -> currentTaskId.equals(taskState.getTaskId())).findFirst().orElse(null);
                if (foundSummary == null) {
                    bulkLog.addError(currentTaskId, new TaskNotFoundException(currentTaskId, "Task with id " + currentTaskId + " was not found."));
                    taskIdIterator.remove();
                } else {
                    if (!TaskState.COMPLETED.equals(foundSummary.getTaskState())) {
                        bulkLog.addError(currentTaskId, new InvalidStateException(currentTaskId));
                        taskIdIterator.remove();
                    }
                }
            }
        }
        if (!taskIds.isEmpty()) {
            taskMapper.deleteMultiple(taskIds);
        }
        return bulkLog;
    } finally {
        LOGGER.debug("exit from deleteTasks()");
        taskanaEngine.returnConnection();
    }
}
Also used : InvalidArgumentException(pro.taskana.exceptions.InvalidArgumentException) TaskNotFoundException(pro.taskana.exceptions.TaskNotFoundException) InvalidStateException(pro.taskana.exceptions.InvalidStateException) TaskanaException(pro.taskana.exceptions.TaskanaException)

Aggregations

InvalidStateException (pro.taskana.exceptions.InvalidStateException)8 Instant (java.time.Instant)4 InvalidOwnerException (pro.taskana.exceptions.InvalidOwnerException)4 Test (org.junit.Test)3 TaskService (pro.taskana.TaskService)3 TaskState (pro.taskana.TaskState)3 TaskNotFoundException (pro.taskana.exceptions.TaskNotFoundException)3 TaskanaException (pro.taskana.exceptions.TaskanaException)3 AbstractAccTest (acceptance.AbstractAccTest)2 ArrayList (java.util.ArrayList)2 Classification (pro.taskana.Classification)2 Task (pro.taskana.Task)2 InvalidArgumentException (pro.taskana.exceptions.InvalidArgumentException)2 Duration (java.time.Duration)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1