TSK-250: CancelClaim with force-flag.

This commit is contained in:
Marcel Lengl 2018-02-07 16:24:19 +01:00 committed by BerndBreier
parent b361d59a5e
commit e6218d0540
4 changed files with 322 additions and 64 deletions

View File

@ -56,6 +56,38 @@ public interface TaskService {
Task claim(String taskId, boolean forceClaim) Task claim(String taskId, boolean forceClaim)
throws TaskNotFoundException, InvalidStateException, InvalidOwnerException; throws TaskNotFoundException, InvalidStateException, InvalidOwnerException;
/**
* Unclaim a existing Task which was claimed and owned by you before.
*
* @param taskId
* id of the task which should be unclaimed.
* @return updated unclaimed task
* @throws TaskNotFoundException
* if the task can´t be found or does not exist
* @throws InvalidStateException
* when the task is already completed.
* @throws InvalidOwnerException
* when the unclaim is not forced and user is diffrent.
*/
Task cancelClaim(String taskId) throws TaskNotFoundException, InvalidStateException, InvalidOwnerException;
/**
* Unclaim a existing Task which was claimed and owned by you before. Also there can be enabled a force flag for
* admins.
*
* @param taskId
* id of the task which should be unclaimed.
* @return updated unclaimed task
* @throws TaskNotFoundException
* if the task can´t be found or does not exist
* @throws InvalidStateException
* when the task is already completed.
* @throws InvalidOwnerException
* when the unclaim is not forced and user is diffrent.
*/
Task cancelClaim(String taskId, boolean forceUnclaim)
throws TaskNotFoundException, InvalidStateException, InvalidOwnerException;
/** /**
* Complete a claimed Task as owner/admin and update State and Timestamps. * Complete a claimed Task as owner/admin and update State and Timestamps.
* *

View File

@ -96,11 +96,12 @@ public class TaskServiceImpl implements TaskService {
taskId); taskId);
throw new InvalidStateException("Task is already completed"); throw new InvalidStateException("Task is already completed");
} }
if (state == TaskState.CLAIMED && !forceClaim) { if (state == TaskState.CLAIMED && !forceClaim && !task.getOwner().equals(userId)) {
LOGGER.warn( LOGGER.warn(
"Method claim() found that task {} is claimed by {} and forceClaim is false. Throwing InvalidOwnerException", "Method claim() found that task {} is claimed by {} and forceClaim is false. Throwing InvalidOwnerException",
taskId, task.getOwner()); taskId, task.getOwner());
throw new InvalidOwnerException("Task is already claimed by user " + 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(); Instant now = Instant.now();
task.setOwner(userId); task.setOwner(userId);
@ -110,7 +111,6 @@ public class TaskServiceImpl implements TaskService {
task.setState(TaskState.CLAIMED); task.setState(TaskState.CLAIMED);
taskMapper.update(task); taskMapper.update(task);
LOGGER.debug("Method claim() claimed task '{}' for user '{}'.", taskId, userId); LOGGER.debug("Method claim() claimed task '{}' for user '{}'.", taskId, userId);
} finally { } finally {
taskanaEngineImpl.returnConnection(); taskanaEngineImpl.returnConnection();
LOGGER.debug("exit from claim()"); LOGGER.debug("exit from claim()");
@ -118,6 +118,50 @@ public class TaskServiceImpl implements TaskService {
return task; return task;
} }
@Override
public Task cancelClaim(String taskId) throws TaskNotFoundException, InvalidStateException, InvalidOwnerException {
return this.cancelClaim(taskId, false);
}
@Override
public Task cancelClaim(String taskId, boolean forceUnclaim)
throws TaskNotFoundException, InvalidStateException, InvalidOwnerException {
String userId = CurrentUserContext.getUserid();
LOGGER.debug("entry to cancelClaim(taskId = {}) with userId = {}, forceFlag = {}", taskId, userId,
forceUnclaim);
TaskImpl task = null;
try {
taskanaEngineImpl.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 {
taskanaEngineImpl.returnConnection();
LOGGER.debug("exit from cancelClaim(taskId = {}) with userId = {}, forceFlag = {}", taskId, userId,
forceUnclaim);
}
return task;
}
@Override @Override
public Task completeTask(String taskId) public Task completeTask(String taskId)
throws TaskNotFoundException, InvalidOwnerException, InvalidStateException { throws TaskNotFoundException, InvalidOwnerException, InvalidStateException {
@ -139,7 +183,7 @@ public class TaskServiceImpl implements TaskService {
LOGGER.warn("Method completeTask() does expect a task which need to be CLAIMED before. TaskId={}", LOGGER.warn("Method completeTask() does expect a task which need to be CLAIMED before. TaskId={}",
taskId); taskId);
throw new InvalidStateException(taskId); throw new InvalidStateException(taskId);
} else if (CurrentUserContext.getUserid() != task.getOwner()) { } else if (!CurrentUserContext.getAccessIds().contains(task.getOwner())) {
LOGGER.warn( LOGGER.warn(
"Method completeTask() does expect to be invoced by the task-owner or a administrator. TaskId={}, TaskOwner={}, CurrentUser={}", "Method completeTask() does expect to be invoced by the task-owner or a administrator. TaskId={}, TaskOwner={}, CurrentUser={}",
taskId, task.getOwner(), CurrentUserContext.getUserid()); taskId, task.getOwner(), CurrentUserContext.getUserid());

View File

@ -7,6 +7,8 @@ import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import java.sql.SQLException; import java.sql.SQLException;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList; import java.util.ArrayList;
import org.h2.store.fs.FileUtils; import org.h2.store.fs.FileUtils;
@ -80,7 +82,6 @@ public class WorkOnTaskAccTest extends AbstractAccTest {
taskService.claim(task.getId()); taskService.claim(task.getId());
} }
@Ignore
@WithAccessId( @WithAccessId(
userName = "user_1_2", userName = "user_1_2",
groupNames = {"group_1"}) groupNames = {"group_1"})
@ -109,7 +110,6 @@ public class WorkOnTaskAccTest extends AbstractAccTest {
taskService.claim(task.getId()); taskService.claim(task.getId());
} }
@Ignore
@WithAccessId( @WithAccessId(
userName = "user_1_2", userName = "user_1_2",
groupNames = {"group_1"}) groupNames = {"group_1"})
@ -121,7 +121,7 @@ public class WorkOnTaskAccTest extends AbstractAccTest {
TaskService taskService = taskanaEngine.getTaskService(); TaskService taskService = taskanaEngine.getTaskService();
Task claimedTask = taskService.getTask("TKI:000000000000000000000000000000000029"); Task claimedTask = taskService.getTask("TKI:000000000000000000000000000000000029");
// taskService.cancelClaim(claimedTask.getId()); taskService.cancelClaim(claimedTask.getId());
Task unclaimedTask = taskService.getTask("TKI:000000000000000000000000000000000029"); Task unclaimedTask = taskService.getTask("TKI:000000000000000000000000000000000029");
assertNotNull(unclaimedTask); assertNotNull(unclaimedTask);
@ -131,7 +131,6 @@ public class WorkOnTaskAccTest extends AbstractAccTest {
assertNull(unclaimedTask.getOwner()); assertNull(unclaimedTask.getOwner());
} }
@Ignore
@WithAccessId( @WithAccessId(
userName = "user_1_2", userName = "user_1_2",
groupNames = {"group_1"}) groupNames = {"group_1"})
@ -143,10 +142,9 @@ public class WorkOnTaskAccTest extends AbstractAccTest {
TaskService taskService = taskanaEngine.getTaskService(); TaskService taskService = taskanaEngine.getTaskService();
Task claimedTask = taskService.getTask("TKI:000000000000000000000000000000000030"); Task claimedTask = taskService.getTask("TKI:000000000000000000000000000000000030");
// taskService.cancelClaim(claimedTask.getId()); taskService.cancelClaim(claimedTask.getId());
} }
@Ignore
@WithAccessId( @WithAccessId(
userName = "user_1_2", userName = "user_1_2",
groupNames = {"group_1"}) groupNames = {"group_1"})
@ -158,10 +156,16 @@ public class WorkOnTaskAccTest extends AbstractAccTest {
TaskService taskService = taskanaEngine.getTaskService(); TaskService taskService = taskanaEngine.getTaskService();
Task claimedTask = taskService.getTask("TKI:000000000000000000000000000000000031"); Task claimedTask = taskService.getTask("TKI:000000000000000000000000000000000031");
// taskService.cancelClaim(claimedTask.getId(), true); taskService.cancelClaim(claimedTask.getId(), true);
Task unclaimedTask = taskService.getTask("TKI:000000000000000000000000000000000031");
assertNotNull(unclaimedTask);
assertEquals(TaskState.READY, unclaimedTask.getState());
assertNull(unclaimedTask.getClaimed());
assertTrue(unclaimedTask.isRead());
assertNull(unclaimedTask.getOwner());
} }
@Ignore
@WithAccessId( @WithAccessId(
userName = "user_1_2", userName = "user_1_2",
groupNames = {"group_1"}) groupNames = {"group_1"})
@ -170,6 +174,7 @@ public class WorkOnTaskAccTest extends AbstractAccTest {
throws SQLException, NotAuthorizedException, InvalidArgumentException, ClassificationNotFoundException, throws SQLException, NotAuthorizedException, InvalidArgumentException, ClassificationNotFoundException,
WorkbasketNotFoundException, TaskAlreadyExistException, InvalidWorkbasketException, TaskNotFoundException, WorkbasketNotFoundException, TaskAlreadyExistException, InvalidWorkbasketException, TaskNotFoundException,
InvalidStateException, InvalidOwnerException { InvalidStateException, InvalidOwnerException {
Instant before = Instant.now().minus(Duration.ofSeconds(3L));
TaskService taskService = taskanaEngine.getTaskService(); TaskService taskService = taskanaEngine.getTaskService();
Task claimedTask = taskService.getTask("TKI:000000000000000000000000000000000032"); Task claimedTask = taskService.getTask("TKI:000000000000000000000000000000000032");
@ -179,7 +184,9 @@ public class WorkOnTaskAccTest extends AbstractAccTest {
assertNotNull(completedTask); assertNotNull(completedTask);
assertEquals(TaskState.COMPLETED, completedTask.getState()); assertEquals(TaskState.COMPLETED, completedTask.getState());
assertNotNull(completedTask.getCompleted()); assertNotNull(completedTask.getCompleted());
assertEquals(completedTask.getCompleted(), claimedTask.getModified()); assertEquals(completedTask.getCompleted(), completedTask.getModified());
assertTrue(completedTask.getCompleted().isAfter(before));
assertTrue(completedTask.getModified().isAfter(before));
assertTrue(completedTask.isRead()); assertTrue(completedTask.isRead());
assertEquals("user_1_2", completedTask.getOwner()); assertEquals("user_1_2", completedTask.getOwner());
} }
@ -254,7 +261,7 @@ public class WorkOnTaskAccTest extends AbstractAccTest {
ConcurrencyException, AttachmentPersistenceException { ConcurrencyException, AttachmentPersistenceException {
TaskService taskService = taskanaEngine.getTaskService(); TaskService taskService = taskanaEngine.getTaskService();
ArrayList<String> taskIdList = new ArrayList(); ArrayList<String> taskIdList = new ArrayList<>();
taskIdList.add("TKI:000000000000000000000000000000000100"); taskIdList.add("TKI:000000000000000000000000000000000100");
taskIdList.add("TKI:000000000000000000000000000000000101"); taskIdList.add("TKI:000000000000000000000000000000000101");
@ -280,7 +287,7 @@ public class WorkOnTaskAccTest extends AbstractAccTest {
ConcurrencyException, AttachmentPersistenceException { ConcurrencyException, AttachmentPersistenceException {
TaskService taskService = taskanaEngine.getTaskService(); TaskService taskService = taskanaEngine.getTaskService();
ArrayList<String> taskIdList = new ArrayList(); ArrayList<String> taskIdList = new ArrayList<>();
taskIdList.add("TKI:000000000000000000000000000000000102"); taskIdList.add("TKI:000000000000000000000000000000000102");
taskIdList.add("TKI:000000000000000000000000000000000103"); taskIdList.add("TKI:000000000000000000000000000000000103");
taskIdList.add("TKI:000000000000000000000000000000000033"); taskIdList.add("TKI:000000000000000000000000000000000033");

View File

@ -5,6 +5,7 @@ import static org.hamcrest.core.IsNot.not;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doReturn;
@ -37,6 +38,7 @@ import pro.taskana.Attachment;
import pro.taskana.Classification; import pro.taskana.Classification;
import pro.taskana.ClassificationSummary; import pro.taskana.ClassificationSummary;
import pro.taskana.Task; import pro.taskana.Task;
import pro.taskana.TaskService;
import pro.taskana.TaskSummary; import pro.taskana.TaskSummary;
import pro.taskana.Workbasket; import pro.taskana.Workbasket;
import pro.taskana.WorkbasketService; import pro.taskana.WorkbasketService;
@ -67,14 +69,11 @@ import pro.taskana.security.CurrentUserContext;
* *
* @author EH * @author EH
*/ */
// @RunWith(MockitoJUnitRunner.class)
@RunWith(PowerMockRunner.class) @RunWith(PowerMockRunner.class)
@PrepareForTest(CurrentUserContext.class) @PrepareForTest(CurrentUserContext.class)
@PowerMockIgnore("javax.management.*") @PowerMockIgnore("javax.management.*")
public class TaskServiceImplTest { public class TaskServiceImplTest {
private static final int SLEEP_TIME = 100;
@InjectMocks @InjectMocks
private TaskServiceImpl cut; private TaskServiceImpl cut;
@ -344,7 +343,8 @@ public class TaskServiceImplTest {
} catch (TaskAlreadyExistException ex) { } catch (TaskAlreadyExistException ex) {
verify(taskanaEngineImpl, times(1)).openConnection(); verify(taskanaEngineImpl, times(1)).openConnection();
verify(taskanaEngineImpl, times(1)).returnConnection(); verify(taskanaEngineImpl, times(1)).returnConnection();
verifyNoMoreInteractions(attachmentMapperMock, taskanaEngineConfigurationMock, taskanaEngineMock, verifyNoMoreInteractions(attachmentMapperMock, taskanaEngineConfigurationMock,
taskanaEngineMock,
taskanaEngineImpl, taskMapperMock, objectReferenceMapperMock, workbasketServiceMock, sqlSessionMock, taskanaEngineImpl, taskMapperMock, objectReferenceMapperMock, workbasketServiceMock, sqlSessionMock,
classificationQueryImplMock, classificationQueryImplMock,
classificationServiceImplMock); classificationServiceImplMock);
@ -370,7 +370,8 @@ public class TaskServiceImplTest {
verify(workbasketServiceMock, times(1)).checkAuthorization(task.getWorkbasketKey(), verify(workbasketServiceMock, times(1)).checkAuthorization(task.getWorkbasketKey(),
WorkbasketAuthorization.APPEND); WorkbasketAuthorization.APPEND);
verify(taskanaEngineImpl, times(1)).returnConnection(); verify(taskanaEngineImpl, times(1)).returnConnection();
verifyNoMoreInteractions(attachmentMapperMock, taskanaEngineConfigurationMock, taskanaEngineMock, verifyNoMoreInteractions(attachmentMapperMock, taskanaEngineConfigurationMock,
taskanaEngineMock,
taskanaEngineImpl, taskMapperMock, objectReferenceMapperMock, workbasketServiceMock, sqlSessionMock, taskanaEngineImpl, taskMapperMock, objectReferenceMapperMock, workbasketServiceMock, sqlSessionMock,
classificationQueryImplMock, classificationQueryImplMock,
classificationServiceImplMock); classificationServiceImplMock);
@ -393,7 +394,8 @@ public class TaskServiceImplTest {
verify(taskanaEngineImpl, times(1)).openConnection(); verify(taskanaEngineImpl, times(1)).openConnection();
verify(workbasketServiceMock, times(1)).getWorkbasketByKey(task.getWorkbasketKey()); verify(workbasketServiceMock, times(1)).getWorkbasketByKey(task.getWorkbasketKey());
verify(taskanaEngineImpl, times(1)).returnConnection(); verify(taskanaEngineImpl, times(1)).returnConnection();
verifyNoMoreInteractions(attachmentMapperMock, taskanaEngineConfigurationMock, taskanaEngineMock, verifyNoMoreInteractions(attachmentMapperMock, taskanaEngineConfigurationMock,
taskanaEngineMock,
taskanaEngineImpl, taskMapperMock, objectReferenceMapperMock, workbasketServiceMock, sqlSessionMock, taskanaEngineImpl, taskMapperMock, objectReferenceMapperMock, workbasketServiceMock, sqlSessionMock,
classificationQueryImplMock, classificationQueryImplMock,
classificationServiceImplMock); classificationServiceImplMock);
@ -401,63 +403,124 @@ public class TaskServiceImplTest {
} }
} }
@Test
public void testClaimDefaultFlag()
throws TaskNotFoundException, InvalidStateException, InvalidOwnerException {
TaskServiceImpl cutSpy = Mockito.spy(cut);
TaskImpl expectedTask = createUnitTestTask("1", "Unit Test Task 1", "1", null);
doReturn(expectedTask).when(cutSpy).claim(expectedTask.getId(), false);
cutSpy.claim(expectedTask.getId());
verify(cutSpy, times(1)).claim(expectedTask.getId(), false);
verifyNoMoreInteractions(attachmentMapperMock, taskanaEngineConfigurationMock, taskanaEngineMock,
taskanaEngineImpl, taskMapperMock, objectReferenceMapperMock, workbasketServiceMock, sqlSessionMock,
classificationQueryImplMock);
}
@Test @Test
public void testClaimSuccessfulToOwner() throws Exception { public void testClaimSuccessfulToOwner() throws Exception {
Classification dummyClassification = createDummyClassification(); TaskServiceImpl cutSpy = Mockito.spy(cut);
TaskImpl expectedTask = createUnitTestTask("1", "Unit Test Task 1", "1", dummyClassification); TaskImpl expectedTask = createUnitTestTask("1", "Unit Test Task 1", "1", null);
doReturn(null).when(attachmentMapperMock).findAttachmentsByTaskId(expectedTask.getId()); Mockito.doReturn(expectedTask).when(cutSpy).getTask(expectedTask.getId());
Mockito.doReturn(expectedTask).when(taskMapperMock).findById(expectedTask.getId());
Thread.sleep(SLEEP_TIME); // to have different timestamps
String expectedOwner = "John Does"; String expectedOwner = "John Does";
Instant before = Instant.now().minus(Duration.ofSeconds(3L));
PowerMockito.mockStatic(CurrentUserContext.class); PowerMockito.mockStatic(CurrentUserContext.class);
Mockito.when(CurrentUserContext.getUserid()).thenReturn(expectedOwner); Mockito.when(CurrentUserContext.getUserid()).thenReturn(expectedOwner);
doReturn(classificationQueryImplMock).when(classificationServiceImplMock).createClassificationQuery();
doReturn(classificationQueryImplMock).when(classificationQueryImplMock).domainIn(any());
doReturn(classificationQueryImplMock).when(classificationQueryImplMock).keyIn(any());
doReturn(new ArrayList<>()).when(classificationQueryImplMock).list();
doReturn(dummyClassification).when(
classificationServiceImplMock)
.getClassification(dummyClassification.getKey(), dummyClassification.getDomain());
List<ClassificationSummaryImpl> classificationList = Arrays Task acturalTask = cutSpy.claim(expectedTask.getId(), true);
.asList((ClassificationSummaryImpl) dummyClassification.asSummary());
doReturn(classificationList).when(
classificationQueryImplMock)
.list();
// Mockito.doReturn(expectedOwner).when(currentUserContext).getUserid(); verify(taskanaEngineImpl, times(1)).openConnection();
Task acturalTask = cut.claim(expectedTask.getId(), true); verify(cutSpy, times(1)).getTask(expectedTask.getId());
verify(taskanaEngineImpl, times(2)).openConnection();
verify(taskMapperMock, times(1)).findById(expectedTask.getId());
verify(attachmentMapperMock, times(1)).findAttachmentsByTaskId(expectedTask.getId());
verify(classificationQueryImplMock, times(1)).domainIn(any());
verify(classificationQueryImplMock, times(1)).keyIn(any());
verify(classificationQueryImplMock, times(1)).list();
verify(taskMapperMock, times(1)).update(any()); verify(taskMapperMock, times(1)).update(any());
verify(taskanaEngineImpl, times(2)).returnConnection(); verify(taskanaEngineImpl, times(1)).returnConnection();
verifyNoMoreInteractions(attachmentMapperMock, taskanaEngineConfigurationMock, taskanaEngineMock, verifyNoMoreInteractions(attachmentMapperMock, taskanaEngineConfigurationMock, taskanaEngineMock,
taskanaEngineImpl, taskMapperMock, objectReferenceMapperMock, workbasketServiceMock, sqlSessionMock, taskanaEngineImpl, taskMapperMock, objectReferenceMapperMock, workbasketServiceMock, sqlSessionMock,
classificationQueryImplMock); classificationQueryImplMock);
assertThat(acturalTask.getState(), equalTo(TaskState.CLAIMED)); assertThat(acturalTask.getState(), equalTo(TaskState.CLAIMED));
assertThat(acturalTask.getCreated(), not(equalTo(expectedTask.getModified()))); assertThat(acturalTask.getCreated(), not(equalTo(expectedTask.getModified())));
assertThat(acturalTask.getClaimed(), not(equalTo(null))); assertTrue(acturalTask.getClaimed().isAfter(before));
assertTrue(acturalTask.getModified().isAfter(before));
assertThat(acturalTask.getOwner(), equalTo(expectedOwner)); assertThat(acturalTask.getOwner(), equalTo(expectedOwner));
assertThat(acturalTask.isRead(), equalTo(true));
} }
@Test(expected = TaskNotFoundException.class) @Test(expected = TaskNotFoundException.class)
public void testClaimThrowinTaskNotFoundException() throws Exception { public void testClaimThrowinTaskNotFoundException() throws Exception {
try { TaskImpl expectedTask = null;
TaskImpl expectedTask = null; Mockito.doReturn(expectedTask).when(taskMapperMock).findById(any());
Mockito.doReturn(expectedTask).when(taskMapperMock).findById(any());
// Mockito.doReturn("OWNER").when(currentUserContext).getUserid();
try {
cut.claim("1", true); cut.claim("1", true);
} catch (Exception e) { } catch (Exception e) {
verify(taskanaEngineImpl, times(2)).openConnection(); verify(taskanaEngineImpl, times(2)).openConnection();
verify(taskMapperMock, times(1)).findById(any()); verify(taskMapperMock, times(1)).findById(any());
verify(taskanaEngineImpl, times(2)).returnConnection(); verify(taskanaEngineImpl, times(2)).returnConnection();
verifyNoMoreInteractions(attachmentMapperMock, taskanaEngineConfigurationMock,
taskanaEngineMock,
taskanaEngineImpl, taskMapperMock, objectReferenceMapperMock, workbasketServiceMock, sqlSessionMock,
classificationQueryImplMock);
throw e;
}
}
@Test(expected = InvalidStateException.class)
public void testClaimWithInvalidState() throws Exception {
TaskService cutSpy = Mockito.spy(cut);
TaskImpl task = createUnitTestTask("1", "taskName", "wbKey", null);
task.setState(TaskState.COMPLETED);
doReturn(task).when(cutSpy).getTask(task.getId());
try {
cutSpy.claim("1", true);
} catch (Exception e) {
verify(taskanaEngineImpl, times(1)).openConnection();
verify(cutSpy, times(1)).getTask(task.getId());
verify(taskanaEngineImpl, times(1)).returnConnection();
verifyNoMoreInteractions(attachmentMapperMock, taskanaEngineConfigurationMock,
taskanaEngineMock,
taskanaEngineImpl, taskMapperMock, objectReferenceMapperMock, workbasketServiceMock, sqlSessionMock,
classificationQueryImplMock);
throw e;
}
}
@Test(expected = InvalidOwnerException.class)
public void testClaimWithInvalidOwner() throws Exception {
TaskService cutSpy = Mockito.spy(cut);
TaskImpl task = createUnitTestTask("1", "taskName", "wbKey", null);
task.setState(TaskState.CLAIMED);
task.setOwner("Max Mustermann");
doReturn(task).when(cutSpy).getTask(task.getId());
try {
cutSpy.claim("1");
} catch (Exception e) {
verify(taskanaEngineImpl, times(1)).openConnection();
verify(cutSpy, times(1)).getTask(task.getId());
verify(taskanaEngineImpl, times(1)).returnConnection();
verifyNoMoreInteractions(attachmentMapperMock, taskanaEngineConfigurationMock,
taskanaEngineMock,
taskanaEngineImpl, taskMapperMock, objectReferenceMapperMock, workbasketServiceMock, sqlSessionMock,
classificationQueryImplMock);
throw e;
}
}
@Test(expected = InvalidStateException.class)
public void testCancelClaimForcedWithInvalidState()
throws TaskNotFoundException,
InvalidStateException, InvalidOwnerException {
TaskServiceImpl cutSpy = Mockito.spy(cut);
TaskImpl expectedTask = createUnitTestTask("1", "Unit Test Task 1", "1", null);
expectedTask.setState(TaskState.COMPLETED);
Mockito.doReturn(expectedTask).when(cutSpy).getTask(expectedTask.getId());
try {
cutSpy.cancelClaim(expectedTask.getId());
} catch (InvalidStateException e) {
verify(taskanaEngineImpl, times(1)).openConnection();
verify(cutSpy, times(1)).getTask(expectedTask.getId());
verify(taskanaEngineImpl, times(1)).returnConnection();
verifyNoMoreInteractions(attachmentMapperMock, taskanaEngineConfigurationMock, taskanaEngineMock, verifyNoMoreInteractions(attachmentMapperMock, taskanaEngineConfigurationMock, taskanaEngineMock,
taskanaEngineImpl, taskMapperMock, objectReferenceMapperMock, workbasketServiceMock, sqlSessionMock, taskanaEngineImpl, taskMapperMock, objectReferenceMapperMock, workbasketServiceMock, sqlSessionMock,
classificationQueryImplMock); classificationQueryImplMock);
@ -465,6 +528,105 @@ public class TaskServiceImplTest {
} }
} }
@Test(expected = InvalidOwnerException.class)
public void testCancelClaimNotForcedWithInvalidOwner()
throws TaskNotFoundException,
InvalidStateException, InvalidOwnerException {
TaskServiceImpl cutSpy = Mockito.spy(cut);
TaskImpl expectedTask = createUnitTestTask("1", "Unit Test Task 1", "1", null);
expectedTask.setOwner("Thomas");
expectedTask.setState(TaskState.CLAIMED);
Mockito.doReturn(expectedTask).when(cutSpy).getTask(expectedTask.getId());
PowerMockito.mockStatic(CurrentUserContext.class);
Mockito.when(CurrentUserContext.getUserid()).thenReturn("Heinz");
try {
cutSpy.cancelClaim(expectedTask.getId());
} catch (InvalidOwnerException e) {
verify(taskanaEngineImpl, times(1)).openConnection();
verify(cutSpy, times(1)).getTask(expectedTask.getId());
verify(taskanaEngineImpl, times(1)).returnConnection();
verifyNoMoreInteractions(attachmentMapperMock, taskanaEngineConfigurationMock, taskanaEngineMock,
taskanaEngineImpl, taskMapperMock, objectReferenceMapperMock, workbasketServiceMock, sqlSessionMock,
classificationQueryImplMock);
throw e;
}
}
@Test
public void testCancelClaimDefaultFlag()
throws TaskNotFoundException, InvalidStateException, InvalidOwnerException {
TaskServiceImpl cutSpy = Mockito.spy(cut);
TaskImpl expectedTask = createUnitTestTask("1", "Unit Test Task 1", "1", null);
doReturn(expectedTask).when(cutSpy).cancelClaim(expectedTask.getId(), false);
cutSpy.cancelClaim(expectedTask.getId());
verify(cutSpy, times(1)).cancelClaim(expectedTask.getId(), false);
verifyNoMoreInteractions(attachmentMapperMock, taskanaEngineConfigurationMock, taskanaEngineMock,
taskanaEngineImpl, taskMapperMock, objectReferenceMapperMock, workbasketServiceMock, sqlSessionMock,
classificationQueryImplMock);
}
@Test
public void testCancelClaimSuccesfullForced()
throws TaskNotFoundException,
InvalidStateException, InvalidOwnerException {
TaskServiceImpl cutSpy = Mockito.spy(cut);
String owner = "John Does";
TaskImpl expectedTask = createUnitTestTask("1", "Unit Test Task 1", "1", null);
expectedTask.setOwner("Some other owner");
expectedTask.setState(TaskState.CLAIMED);
Mockito.doReturn(expectedTask).when(cutSpy).getTask(expectedTask.getId());
Instant before = Instant.now().minus(Duration.ofSeconds(3L));
PowerMockito.mockStatic(CurrentUserContext.class);
Mockito.when(CurrentUserContext.getUserid()).thenReturn(owner);
Task acturalTask = cutSpy.cancelClaim(expectedTask.getId(), true);
verify(taskanaEngineImpl, times(1)).openConnection();
verify(cutSpy, times(1)).getTask(expectedTask.getId());
verify(taskMapperMock, times(1)).update(any());
verify(taskanaEngineImpl, times(1)).returnConnection();
verifyNoMoreInteractions(attachmentMapperMock, taskanaEngineConfigurationMock, taskanaEngineMock,
taskanaEngineImpl, taskMapperMock, objectReferenceMapperMock, workbasketServiceMock, sqlSessionMock,
classificationQueryImplMock);
assertThat(acturalTask.getState(), equalTo(TaskState.READY));
assertThat(acturalTask.getClaimed(), equalTo(null));
assertTrue(acturalTask.getModified().isAfter(before));
assertThat(acturalTask.getOwner(), equalTo(null));
assertThat(acturalTask.isRead(), equalTo(true));
}
@Test
public void testCancelClaimInvalidState()
throws TaskNotFoundException, InvalidStateException, InvalidOwnerException {
TaskServiceImpl cutSpy = Mockito.spy(cut);
String owner = "John Does";
TaskImpl expectedTask = createUnitTestTask("1", "Unit Test Task 1", "1", null);
expectedTask.setOwner("Some other owner");
expectedTask.setState(TaskState.CLAIMED);
Mockito.doReturn(expectedTask).when(cutSpy).getTask(expectedTask.getId());
Instant before = Instant.now().minus(Duration.ofSeconds(3L));
PowerMockito.mockStatic(CurrentUserContext.class);
Mockito.when(CurrentUserContext.getUserid()).thenReturn(owner);
Task acturalTask = cutSpy.cancelClaim(expectedTask.getId(), true);
verify(taskanaEngineImpl, times(1)).openConnection();
verify(cutSpy, times(1)).getTask(expectedTask.getId());
verify(taskMapperMock, times(1)).update(any());
verify(taskanaEngineImpl, times(1)).returnConnection();
verifyNoMoreInteractions(attachmentMapperMock, taskanaEngineConfigurationMock, taskanaEngineMock,
taskanaEngineImpl, taskMapperMock, objectReferenceMapperMock, workbasketServiceMock, sqlSessionMock,
classificationQueryImplMock);
assertThat(acturalTask.getState(), equalTo(TaskState.READY));
assertThat(acturalTask.getClaimed(), equalTo(null));
assertTrue(acturalTask.getModified().isAfter(before));
assertThat(acturalTask.getOwner(), equalTo(null));
assertThat(acturalTask.isRead(), equalTo(true));
}
@Test @Test
public void testCompleteTaskDefault() public void testCompleteTaskDefault()
throws TaskNotFoundException, InvalidOwnerException, InvalidStateException, InterruptedException, throws TaskNotFoundException, InvalidOwnerException, InvalidStateException, InterruptedException,
@ -561,7 +723,8 @@ public class TaskServiceImplTest {
verify(taskanaEngineImpl, times(1)).openConnection(); verify(taskanaEngineImpl, times(1)).openConnection();
verify(cutSpy, times(1)).getTask(task.getId()); verify(cutSpy, times(1)).getTask(task.getId());
verify(taskanaEngineImpl, times(1)).returnConnection(); verify(taskanaEngineImpl, times(1)).returnConnection();
verifyNoMoreInteractions(attachmentMapperMock, taskanaEngineConfigurationMock, taskanaEngineMock, verifyNoMoreInteractions(attachmentMapperMock, taskanaEngineConfigurationMock,
taskanaEngineMock,
taskanaEngineImpl, taskMapperMock, objectReferenceMapperMock, workbasketServiceMock, sqlSessionMock, taskanaEngineImpl, taskMapperMock, objectReferenceMapperMock, workbasketServiceMock, sqlSessionMock,
classificationQueryImplMock); classificationQueryImplMock);
throw e; throw e;
@ -586,7 +749,8 @@ public class TaskServiceImplTest {
verify(taskanaEngineImpl, times(1)).openConnection(); verify(taskanaEngineImpl, times(1)).openConnection();
verify(cutSpy, times(1)).getTask(task.getId()); verify(cutSpy, times(1)).getTask(task.getId());
verify(taskanaEngineImpl, times(1)).returnConnection(); verify(taskanaEngineImpl, times(1)).returnConnection();
verifyNoMoreInteractions(attachmentMapperMock, taskanaEngineConfigurationMock, taskanaEngineMock, verifyNoMoreInteractions(attachmentMapperMock, taskanaEngineConfigurationMock,
taskanaEngineMock,
taskanaEngineImpl, taskMapperMock, objectReferenceMapperMock, workbasketServiceMock, sqlSessionMock, taskanaEngineImpl, taskMapperMock, objectReferenceMapperMock, workbasketServiceMock, sqlSessionMock,
classificationQueryImplMock); classificationQueryImplMock);
throw e; throw e;
@ -606,7 +770,8 @@ public class TaskServiceImplTest {
verify(taskanaEngineImpl, times(1)).openConnection(); verify(taskanaEngineImpl, times(1)).openConnection();
verify(cutSpy, times(1)).getTask(taskId); verify(cutSpy, times(1)).getTask(taskId);
verify(taskanaEngineImpl, times(1)).returnConnection(); verify(taskanaEngineImpl, times(1)).returnConnection();
verifyNoMoreInteractions(attachmentMapperMock, taskanaEngineConfigurationMock, taskanaEngineMock, verifyNoMoreInteractions(attachmentMapperMock, taskanaEngineConfigurationMock,
taskanaEngineMock,
taskanaEngineImpl, taskMapperMock, objectReferenceMapperMock, workbasketServiceMock, sqlSessionMock, taskanaEngineImpl, taskMapperMock, objectReferenceMapperMock, workbasketServiceMock, sqlSessionMock,
classificationQueryImplMock); classificationQueryImplMock);
throw e; throw e;
@ -779,7 +944,8 @@ public class TaskServiceImplTest {
verify(workbasketServiceMock, times(1)).checkAuthorization(destinationWorkbasketKey, verify(workbasketServiceMock, times(1)).checkAuthorization(destinationWorkbasketKey,
WorkbasketAuthorization.APPEND); WorkbasketAuthorization.APPEND);
verify(taskanaEngineImpl, times(1)).returnConnection(); verify(taskanaEngineImpl, times(1)).returnConnection();
verifyNoMoreInteractions(attachmentMapperMock, taskanaEngineConfigurationMock, taskanaEngineMock, verifyNoMoreInteractions(attachmentMapperMock, taskanaEngineConfigurationMock,
taskanaEngineMock,
taskanaEngineImpl, taskMapperMock, objectReferenceMapperMock, workbasketServiceMock, sqlSessionMock, taskanaEngineImpl, taskMapperMock, objectReferenceMapperMock, workbasketServiceMock, sqlSessionMock,
classificationQueryImplMock); classificationQueryImplMock);
throw e; throw e;
@ -800,7 +966,8 @@ public class TaskServiceImplTest {
} catch (Exception e) { } catch (Exception e) {
verify(taskanaEngineImpl, times(1)).openConnection(); verify(taskanaEngineImpl, times(1)).openConnection();
verify(taskanaEngineImpl, times(1)).returnConnection(); verify(taskanaEngineImpl, times(1)).returnConnection();
verifyNoMoreInteractions(attachmentMapperMock, taskanaEngineConfigurationMock, taskanaEngineMock, verifyNoMoreInteractions(attachmentMapperMock, taskanaEngineConfigurationMock,
taskanaEngineMock,
taskanaEngineImpl, taskMapperMock, objectReferenceMapperMock, workbasketServiceMock, sqlSessionMock, taskanaEngineImpl, taskMapperMock, objectReferenceMapperMock, workbasketServiceMock, sqlSessionMock,
classificationQueryImplMock); classificationQueryImplMock);
throw e; throw e;
@ -825,7 +992,8 @@ public class TaskServiceImplTest {
verify(workbasketServiceMock, times(1)).checkAuthorization(destinationWorkbasketKey, verify(workbasketServiceMock, times(1)).checkAuthorization(destinationWorkbasketKey,
WorkbasketAuthorization.APPEND); WorkbasketAuthorization.APPEND);
verify(taskanaEngineImpl, times(1)).returnConnection(); verify(taskanaEngineImpl, times(1)).returnConnection();
verifyNoMoreInteractions(attachmentMapperMock, taskanaEngineConfigurationMock, taskanaEngineMock, verifyNoMoreInteractions(attachmentMapperMock, taskanaEngineConfigurationMock,
taskanaEngineMock,
taskanaEngineImpl, taskMapperMock, objectReferenceMapperMock, workbasketServiceMock, sqlSessionMock, taskanaEngineImpl, taskMapperMock, objectReferenceMapperMock, workbasketServiceMock, sqlSessionMock,
classificationQueryImplMock); classificationQueryImplMock);
throw e; throw e;
@ -854,7 +1022,8 @@ public class TaskServiceImplTest {
verify(workbasketServiceMock, times(1)).checkAuthorization(task.getWorkbasketKey(), verify(workbasketServiceMock, times(1)).checkAuthorization(task.getWorkbasketKey(),
WorkbasketAuthorization.TRANSFER); WorkbasketAuthorization.TRANSFER);
verify(taskanaEngineImpl, times(1)).returnConnection(); verify(taskanaEngineImpl, times(1)).returnConnection();
verifyNoMoreInteractions(attachmentMapperMock, taskanaEngineConfigurationMock, taskanaEngineMock, verifyNoMoreInteractions(attachmentMapperMock, taskanaEngineConfigurationMock,
taskanaEngineMock,
taskanaEngineImpl, taskMapperMock, objectReferenceMapperMock, workbasketServiceMock, sqlSessionMock, taskanaEngineImpl, taskMapperMock, objectReferenceMapperMock, workbasketServiceMock, sqlSessionMock,
classificationQueryImplMock); classificationQueryImplMock);
throw e; throw e;
@ -896,7 +1065,8 @@ public class TaskServiceImplTest {
} catch (Exception e) { } catch (Exception e) {
verify(taskanaEngineImpl, times(1)).openConnection(); verify(taskanaEngineImpl, times(1)).openConnection();
verify(taskanaEngineImpl, times(1)).returnConnection(); verify(taskanaEngineImpl, times(1)).returnConnection();
verifyNoMoreInteractions(attachmentMapperMock, taskanaEngineConfigurationMock, taskanaEngineMock, verifyNoMoreInteractions(attachmentMapperMock, taskanaEngineConfigurationMock,
taskanaEngineMock,
taskanaEngineImpl, taskanaEngineImpl,
taskMapperMock, objectReferenceMapperMock, workbasketServiceMock, sqlSessionMock, taskMapperMock, objectReferenceMapperMock, workbasketServiceMock, sqlSessionMock,
classificationQueryImplMock); classificationQueryImplMock);
@ -950,7 +1120,8 @@ public class TaskServiceImplTest {
verify(taskanaEngineImpl, times(1)).openConnection(); verify(taskanaEngineImpl, times(1)).openConnection();
verify(taskMapperMock, times(1)).findById(task.getId()); verify(taskMapperMock, times(1)).findById(task.getId());
verify(taskanaEngineImpl, times(1)).returnConnection(); verify(taskanaEngineImpl, times(1)).returnConnection();
verifyNoMoreInteractions(attachmentMapperMock, taskanaEngineConfigurationMock, taskanaEngineMock, verifyNoMoreInteractions(attachmentMapperMock, taskanaEngineConfigurationMock,
taskanaEngineMock,
taskanaEngineImpl, taskMapperMock, objectReferenceMapperMock, workbasketServiceMock, sqlSessionMock, taskanaEngineImpl, taskMapperMock, objectReferenceMapperMock, workbasketServiceMock, sqlSessionMock,
classificationQueryImplMock); classificationQueryImplMock);
throw e; throw e;
@ -1060,7 +1231,8 @@ public class TaskServiceImplTest {
verify(taskMapperMock, times(1)).findTaskSummariesByWorkbasketKey(workbasketKey); verify(taskMapperMock, times(1)).findTaskSummariesByWorkbasketKey(workbasketKey);
verify(taskanaEngineImpl, times(1)).returnConnection(); verify(taskanaEngineImpl, times(1)).returnConnection();
verify(workbasketServiceMock, times(1)).getWorkbasketByKey(any()); verify(workbasketServiceMock, times(1)).getWorkbasketByKey(any());
verifyNoMoreInteractions(attachmentMapperMock, taskMapperMock, taskanaEngineImpl, workbasketServiceMock); verifyNoMoreInteractions(attachmentMapperMock, taskMapperMock, taskanaEngineImpl,
workbasketServiceMock);
assertThat(actualResultList, equalTo(expectedResultList)); assertThat(actualResultList, equalTo(expectedResultList));
assertThat(actualResultList.size(), equalTo(expectedResultList.size())); assertThat(actualResultList.size(), equalTo(expectedResultList.size()));
@ -1227,9 +1399,12 @@ public class TaskServiceImplTest {
task.setWorkbasketKey(workbasketKey); task.setWorkbasketKey(workbasketKey);
task.setDomain(""); task.setDomain("");
task.setAttachments(new ArrayList<>()); task.setAttachments(new ArrayList<>());
Instant now = Instant.now(); Instant now = Instant.now().minus(Duration.ofMinutes(1L));
task.setCreated(now); task.setCreated(now);
task.setModified(now); task.setModified(now);
if (classification == null) {
classification = createDummyClassification();
}
task.setClassificationSummary(classification.asSummary()); task.setClassificationSummary(classification.asSummary());
task.setClassificationKey(classification.getKey()); task.setClassificationKey(classification.getKey());
task.setDomain(classification.getDomain()); task.setDomain(classification.getDomain());