TSK-447: fixed transferTasks with invalid TaskIdList

This commit is contained in:
Mustapha Zorgati 2018-05-07 11:44:25 +02:00
parent 44dfbb983e
commit 31e24d8965
2 changed files with 55 additions and 48 deletions

View File

@ -527,20 +527,13 @@ public class TaskServiceImpl implements TaskService {
private BulkOperationResults<String, TaskanaException> transferTasks(List<String> taskIdsToBeTransferred, private BulkOperationResults<String, TaskanaException> transferTasks(List<String> taskIdsToBeTransferred,
Workbasket destinationWorkbasket) throws InvalidArgumentException { Workbasket destinationWorkbasket) throws InvalidArgumentException {
// Check pre-conditions with trowing Exceptions // Check pre-conditions with trowing Exceptions
if (taskIdsToBeTransferred == null if (taskIdsToBeTransferred == null) {
|| taskIdsToBeTransferred.isEmpty() || taskIdsToBeTransferred.contains("")) { throw new InvalidArgumentException("TaskIds must not be null.");
throw new InvalidArgumentException(
"TaskIds must not be null,empty or an empty string.");
} }
BulkOperationResults<String, TaskanaException> bulkLog = new BulkOperationResults<>(); BulkOperationResults<String, TaskanaException> bulkLog = new BulkOperationResults<>();
// convert to ArrayList<String> if necessary to prevent a UnsupportedOperationException while removing // convert to ArrayList<String> if necessary to prevent a UnsupportedOperationException while removing
ArrayList<String> taskIds; List<String> taskIds = new ArrayList<>(taskIdsToBeTransferred);
if (!(taskIdsToBeTransferred instanceof ArrayList)) {
taskIds = new ArrayList<>(taskIdsToBeTransferred);
} else {
taskIds = (ArrayList<String>) taskIdsToBeTransferred;
}
// check tasks Ids exist and not empty - log and remove // check tasks Ids exist and not empty - log and remove
Iterator<String> taskIdIterator = taskIds.iterator(); Iterator<String> taskIdIterator = taskIds.iterator();
@ -553,6 +546,11 @@ public class TaskServiceImpl implements TaskService {
} }
} }
// Check pre-conditions with trowing Exceptions after removing invalid invalid arguments.
if (taskIds.isEmpty()) {
throw new InvalidArgumentException("TaskIds must not contain only invalid arguments.");
}
// query for existing tasks. use taskMapper.findExistingTasks because this method // query for existing tasks. use taskMapper.findExistingTasks because this method
// returns only the required information. // returns only the required information.
List<MinimalTaskSummary> taskSummaries; List<MinimalTaskSummary> taskSummaries;

View File

@ -7,14 +7,14 @@ import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.sql.SQLException;
import java.time.Instant; import java.time.Instant;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -23,13 +23,11 @@ import pro.taskana.Task;
import pro.taskana.TaskService; import pro.taskana.TaskService;
import pro.taskana.TaskState; import pro.taskana.TaskState;
import pro.taskana.Workbasket; import pro.taskana.Workbasket;
import pro.taskana.exceptions.ClassificationNotFoundException;
import pro.taskana.exceptions.InvalidArgumentException; import pro.taskana.exceptions.InvalidArgumentException;
import pro.taskana.exceptions.InvalidOwnerException; import pro.taskana.exceptions.InvalidOwnerException;
import pro.taskana.exceptions.InvalidStateException; import pro.taskana.exceptions.InvalidStateException;
import pro.taskana.exceptions.InvalidWorkbasketException; import pro.taskana.exceptions.InvalidWorkbasketException;
import pro.taskana.exceptions.NotAuthorizedException; import pro.taskana.exceptions.NotAuthorizedException;
import pro.taskana.exceptions.TaskAlreadyExistException;
import pro.taskana.exceptions.TaskNotFoundException; import pro.taskana.exceptions.TaskNotFoundException;
import pro.taskana.exceptions.TaskanaException; import pro.taskana.exceptions.TaskanaException;
import pro.taskana.exceptions.WorkbasketNotFoundException; import pro.taskana.exceptions.WorkbasketNotFoundException;
@ -52,8 +50,7 @@ public class TransferTaskAccTest extends AbstractAccTest {
groupNames = {"group_1"}) groupNames = {"group_1"})
@Test @Test
public void testTransferTaskToWorkbasketId() public void testTransferTaskToWorkbasketId()
throws SQLException, NotAuthorizedException, InvalidArgumentException, ClassificationNotFoundException, throws NotAuthorizedException, WorkbasketNotFoundException, InvalidWorkbasketException, TaskNotFoundException,
WorkbasketNotFoundException, TaskAlreadyExistException, InvalidWorkbasketException, TaskNotFoundException,
InvalidStateException, InvalidOwnerException { InvalidStateException, InvalidOwnerException {
TaskService taskService = taskanaEngine.getTaskService(); TaskService taskService = taskanaEngine.getTaskService();
Task task = taskService.getTask("TKI:000000000000000000000000000000000003"); Task task = taskService.getTask("TKI:000000000000000000000000000000000003");
@ -74,8 +71,7 @@ public class TransferTaskAccTest extends AbstractAccTest {
groupNames = {"group_1"}) groupNames = {"group_1"})
@Test @Test
public void testTransferTaskToWorkbasketKeyDomain() public void testTransferTaskToWorkbasketKeyDomain()
throws SQLException, NotAuthorizedException, InvalidArgumentException, ClassificationNotFoundException, throws NotAuthorizedException, WorkbasketNotFoundException, InvalidWorkbasketException, TaskNotFoundException,
WorkbasketNotFoundException, TaskAlreadyExistException, InvalidWorkbasketException, TaskNotFoundException,
InvalidStateException, InvalidOwnerException { InvalidStateException, InvalidOwnerException {
TaskService taskService = taskanaEngine.getTaskService(); TaskService taskService = taskanaEngine.getTaskService();
Task task = taskService.getTask("TKI:000000000000000000000000000000000003"); Task task = taskService.getTask("TKI:000000000000000000000000000000000003");
@ -96,8 +92,7 @@ public class TransferTaskAccTest extends AbstractAccTest {
groupNames = {"group_1"}) groupNames = {"group_1"})
@Test @Test
public void testDomainChangingWhenTransferTask() public void testDomainChangingWhenTransferTask()
throws SQLException, NotAuthorizedException, InvalidArgumentException, ClassificationNotFoundException, throws NotAuthorizedException, WorkbasketNotFoundException, InvalidWorkbasketException, TaskNotFoundException,
WorkbasketNotFoundException, TaskAlreadyExistException, InvalidWorkbasketException, TaskNotFoundException,
InvalidStateException { InvalidStateException {
TaskService taskService = taskanaEngine.getTaskService(); TaskService taskService = taskanaEngine.getTaskService();
Task task = taskService.getTask("TKI:000000000000000000000000000000000000"); Task task = taskService.getTask("TKI:000000000000000000000000000000000000");
@ -114,8 +109,7 @@ public class TransferTaskAccTest extends AbstractAccTest {
groupNames = {"group_1"}) groupNames = {"group_1"})
@Test(expected = NotAuthorizedException.class) @Test(expected = NotAuthorizedException.class)
public void testThrowsExceptionIfTransferWithNoTransferAuthorization() public void testThrowsExceptionIfTransferWithNoTransferAuthorization()
throws SQLException, NotAuthorizedException, InvalidArgumentException, ClassificationNotFoundException, throws NotAuthorizedException, WorkbasketNotFoundException, InvalidWorkbasketException, TaskNotFoundException,
WorkbasketNotFoundException, TaskAlreadyExistException, InvalidWorkbasketException, TaskNotFoundException,
InvalidStateException { InvalidStateException {
TaskService taskService = taskanaEngine.getTaskService(); TaskService taskService = taskanaEngine.getTaskService();
Task task = taskService.getTask("TKI:000000000000000000000000000000000001"); Task task = taskService.getTask("TKI:000000000000000000000000000000000001");
@ -128,8 +122,7 @@ public class TransferTaskAccTest extends AbstractAccTest {
groupNames = {"group_1"}) groupNames = {"group_1"})
@Test(expected = InvalidStateException.class) @Test(expected = InvalidStateException.class)
public void testThrowsExceptionIfTaskIsAlreadyCompleted() public void testThrowsExceptionIfTaskIsAlreadyCompleted()
throws SQLException, NotAuthorizedException, InvalidArgumentException, ClassificationNotFoundException, throws NotAuthorizedException, WorkbasketNotFoundException, InvalidWorkbasketException, TaskNotFoundException,
WorkbasketNotFoundException, TaskAlreadyExistException, InvalidWorkbasketException, TaskNotFoundException,
InvalidStateException { InvalidStateException {
TaskService taskService = taskanaEngine.getTaskService(); TaskService taskService = taskanaEngine.getTaskService();
Task task = taskService.getTask("TKI:100000000000000000000000000000000006"); Task task = taskService.getTask("TKI:100000000000000000000000000000000006");
@ -142,8 +135,7 @@ public class TransferTaskAccTest extends AbstractAccTest {
groupNames = {"group_1"}) groupNames = {"group_1"})
@Test(expected = NotAuthorizedException.class) @Test(expected = NotAuthorizedException.class)
public void testThrowsExceptionIfTransferWithNoAppendAuthorization() public void testThrowsExceptionIfTransferWithNoAppendAuthorization()
throws SQLException, NotAuthorizedException, InvalidArgumentException, ClassificationNotFoundException, throws NotAuthorizedException, WorkbasketNotFoundException, InvalidWorkbasketException, TaskNotFoundException,
WorkbasketNotFoundException, TaskAlreadyExistException, InvalidWorkbasketException, TaskNotFoundException,
InvalidStateException { InvalidStateException {
TaskService taskService = taskanaEngine.getTaskService(); TaskService taskService = taskanaEngine.getTaskService();
Task task = taskService.getTask("TKI:000000000000000000000000000000000002"); Task task = taskService.getTask("TKI:000000000000000000000000000000000002");
@ -156,9 +148,7 @@ public class TransferTaskAccTest extends AbstractAccTest {
groupNames = {"group_1"}) groupNames = {"group_1"})
@Test @Test
public void testBulkTransferTaskToWorkbasketById() public void testBulkTransferTaskToWorkbasketById()
throws SQLException, NotAuthorizedException, InvalidArgumentException, ClassificationNotFoundException, throws NotAuthorizedException, InvalidArgumentException, WorkbasketNotFoundException, TaskNotFoundException {
WorkbasketNotFoundException, TaskAlreadyExistException, InvalidWorkbasketException, TaskNotFoundException,
InvalidStateException, InvalidOwnerException {
Instant before = Instant.now(); Instant before = Instant.now();
TaskService taskService = taskanaEngine.getTaskService(); TaskService taskService = taskanaEngine.getTaskService();
ArrayList<String> taskIdList = new ArrayList<>(); ArrayList<String> taskIdList = new ArrayList<>();
@ -201,29 +191,27 @@ public class TransferTaskAccTest extends AbstractAccTest {
taskIdList.add("TKI:000000000000000000000000000000000006"); // working taskIdList.add("TKI:000000000000000000000000000000000006"); // working
taskIdList.add("TKI:000000000000000000000000000000000041"); // NotAuthorized READ taskIdList.add("TKI:000000000000000000000000000000000041"); // NotAuthorized READ
taskIdList.add("TKI:200000000000000000000000000000000006"); // NotAuthorized TRANSFER taskIdList.add("TKI:200000000000000000000000000000000006"); // NotAuthorized TRANSFER
taskIdList.add(""); // InvalidArgument
taskIdList.add(null); // InvalidArgument (added with ""), duplicate taskIdList.add(null); // InvalidArgument (added with ""), duplicate
taskIdList.add("TKI:000000000000000000000000000000000099"); // TaskNotFound taskIdList.add("TKI:000000000000000000000000000000000099"); // TaskNotFound
taskIdList.add("TKI:100000000000000000000000000000000006"); // already completed taskIdList.add("TKI:100000000000000000000000000000000006"); // already completed
BulkOperationResults<String, TaskanaException> results = taskService BulkOperationResults<String, TaskanaException> results = taskService
.transferTasks("WBI:100000000000000000000000000000000006", taskIdList); .transferTasks("WBI:100000000000000000000000000000000006", taskIdList);
// check for exceptions in bulk
assertTrue(results.containsErrors()); assertTrue(results.containsErrors());
assertThat(results.getErrorMap().values().size(), equalTo(5)); assertThat(results.getErrorMap().values().size(), equalTo(5));
// react to result assertEquals(results.getErrorForId("TKI:000000000000000000000000000000000041").getClass(),
for (String taskId : results.getErrorMap().keySet()) { NotAuthorizedException.class);
TaskanaException ex = results.getErrorForId(taskId); assertEquals(results.getErrorForId("TKI:200000000000000000000000000000000006").getClass(),
if (ex instanceof NotAuthorizedException) { InvalidStateException.class);
System.out.println("NotAuthorizedException on bulkTransfer for taskId=" + taskId); assertEquals(results.getErrorForId("").getClass(), InvalidArgumentException.class);
} else if (ex instanceof InvalidArgumentException) { assertEquals(results.getErrorForId("TKI:000000000000000000000000000000000099").getClass(),
System.out.println("InvalidArgumentException on bulkTransfer for EMPTY/NULL taskId='" + taskId + "'"); TaskNotFoundException.class);
} else if (ex instanceof TaskNotFoundException) { assertEquals(results.getErrorForId("TKI:100000000000000000000000000000000006").getClass(),
System.out.println("TaskNotFoundException on bulkTransfer for taskId=" + taskId); InvalidStateException.class);
} else if (ex instanceof InvalidStateException) {
System.out.println("InvalidStateException on bulkTransfer for taskId=" + taskId); // verify valid requests
} else {
fail("Impossible failure Entry registered");
}
}
Task transferredTask = taskService.getTask("TKI:000000000000000000000000000000000006"); Task transferredTask = taskService.getTask("TKI:000000000000000000000000000000000006");
assertNotNull(transferredTask); assertNotNull(transferredTask);
assertTrue(transferredTask.isTransferred()); assertTrue(transferredTask.isTransferred());
@ -261,18 +249,39 @@ public class TransferTaskAccTest extends AbstractAccTest {
taskService.transferTasks("WBI:100000000000000000000000000000000006", taskIds); taskService.transferTasks("WBI:100000000000000000000000000000000006", taskIds);
} }
@WithAccessId(
userName = "teamlead_1",
groupNames = {"group_1"})
@Test
public void testTransferTasksWithInvalidTasksIdList() throws NotAuthorizedException, WorkbasketNotFoundException {
TaskService taskService = taskanaEngine.getTaskService();
// test with invalid list
try {
taskService.transferTasks("WBI:100000000000000000000000000000000006", null);
Assert.fail("exception was excepted to be thrown");
} catch (InvalidArgumentException e) {
Assert.assertEquals(e.getMessage(), "TaskIds must not be null.");
}
// test with list containing only invalid arguments
try {
List<String> taskIds = Arrays.asList("", "", "", null);
taskService.transferTasks("WBI:100000000000000000000000000000000006", taskIds);
Assert.fail("exception was excepted to be thrown");
} catch (InvalidArgumentException e) {
Assert.assertEquals(e.getMessage(), "TaskIds must not contain only invalid arguments.");
}
}
@WithAccessId( @WithAccessId(
userName = "teamlead_1", userName = "teamlead_1",
groupNames = {"group_1"}) groupNames = {"group_1"})
@Test(expected = InvalidArgumentException.class) @Test(expected = InvalidArgumentException.class)
public void testThrowsExceptionIfEmptyListIsSupplied() public void testThrowsExceptionIfEmptyListIsSupplied()
throws SQLException, NotAuthorizedException, InvalidArgumentException, ClassificationNotFoundException, throws NotAuthorizedException, InvalidArgumentException, WorkbasketNotFoundException {
WorkbasketNotFoundException, TaskAlreadyExistException, InvalidWorkbasketException, TaskNotFoundException,
InvalidStateException, InvalidOwnerException {
TaskService taskService = taskanaEngine.getTaskService(); TaskService taskService = taskanaEngine.getTaskService();
List<String> taskIds = new ArrayList<>(); List<String> taskIds = new ArrayList<>();
taskService.transferTasks("WBI:100000000000000000000000000000000006", taskIds); taskService.transferTasks("WBI:100000000000000000000000000000000006", taskIds);
} }
} }