### Question:
TaskPersisterJpa implements TaskPersister { @Override public boolean markTaskToBeCancelled(String id) { TaskStatusInfo tis = em.find(TaskStatusInfo.class, id, LockModeType.PESSIMISTIC_WRITE); if (tis != null && !tis.isCancelRequested() && (tis.getTaskStatus() == TaskStatus.NEW || tis.getTaskStatus() == TaskStatus.RUNNING || tis.getTaskStatus() == TaskStatus.FAILOVER)) { tis.setCancelRequested(true); return true; } return false; } TaskPersisterJpa(); @Override String createTask(String taskType, Map<String, Object> taskConfig); @Override List<TaskStatusInfo> listTasks(String taskTypeFilter, List<TaskStatus> taskStatusFilter, int from, int size); @Override TaskStatusInfo getTaskStatusInfo(String id); @Override boolean markTaskToBeCancelled(String id); @Override boolean changeTaskStatus(String id, TaskStatus taskStatusNew, String message); @Override void writeTaskLog(String id, String message); @Override TaskStatusInfo getTaskToRun(String nodeId); @Override void heartbeat(String nodeId, Set<String> runningTasksId, long failoverTimeout); static final long FAILOVER_DELAY_10; static final long FAILOVER_DELAY_30; static final long FAILOVER_DELAY_100; }### Answer:
@Test public void markTaskToBeCancelled() { TaskPersisterJpa tested = getTested(); markTaskToBeCancelledImpl(tested, null, false); { Mockito.reset(tested.em); TaskStatusInfo tsi = createTaskStatusInfoWithStatus(TaskStatus.NEW); markTaskToBeCancelledImpl(tested, tsi, true); Assert.assertTrue(tsi.isCancelRequested()); } { Mockito.reset(tested.em); TaskStatusInfo tsi = createTaskStatusInfoWithStatus(TaskStatus.NEW); tsi.setCancelRequested(true); markTaskToBeCancelledImpl(tested, tsi, false); Assert.assertTrue(tsi.isCancelRequested()); } { Mockito.reset(tested.em); TaskStatusInfo tsi = createTaskStatusInfoWithStatus(TaskStatus.RUNNING); markTaskToBeCancelledImpl(tested, tsi, true); Assert.assertTrue(tsi.isCancelRequested()); } { Mockito.reset(tested.em); TaskStatusInfo tsi = createTaskStatusInfoWithStatus(TaskStatus.FAILOVER); markTaskToBeCancelledImpl(tested, tsi, true); Assert.assertTrue(tsi.isCancelRequested()); } { Mockito.reset(tested.em); TaskStatusInfo tsi = createTaskStatusInfoWithStatus(TaskStatus.CANCELED); markTaskToBeCancelledImpl(tested, tsi, false); Assert.assertFalse(tsi.isCancelRequested()); } { Mockito.reset(tested.em); TaskStatusInfo tsi = createTaskStatusInfoWithStatus(TaskStatus.FINISHED_OK); markTaskToBeCancelledImpl(tested, tsi, false); Assert.assertFalse(tsi.isCancelRequested()); } { Mockito.reset(tested.em); TaskStatusInfo tsi = createTaskStatusInfoWithStatus(TaskStatus.FINISHED_ERROR); markTaskToBeCancelledImpl(tested, tsi, false); Assert.assertFalse(tsi.isCancelRequested()); } }