CancelScheduledJobHandler implements OperationHandler<CancelScheduledJob> { @Override public Void doOperation(final CancelScheduledJob operation, final Context context, final Store store) throws OperationException { if (null == store.getJobTracker()) { throw new OperationException("JobTracker not enabled"); } if (null == operation.getJobId()) { throw new OperationException("job id must be specified"); } if (store.getJobTracker().getJob(operation.getJobId(), context.getUser()).getStatus().equals(JobStatus.SCHEDULED_PARENT)) { store.getJobTracker().getJob(operation.getJobId(), context.getUser()).setStatus(JobStatus.CANCELLED); } else { throw new OperationException("Job with jobId: " + operation.getJobId() + " is not a scheduled job and cannot be cancelled."); } return null; }  @Override Void doOperation(final CancelScheduledJob operation, final Context context, final Store store);  }
@Test public void shouldThrowExceptionWithNoJobId() { CancelScheduledJob operation = new CancelScheduledJob.Builder() .jobId(null) .build(); CancelScheduledJobHandler handler = new CancelScheduledJobHandler(); final Store store = mock(Store.class); final User user = mock(User.class); given(store.getJobTracker()).willReturn(new JobTracker()); try { handler.doOperation(operation, new Context(user), store); fail("Exception expected"); } catch (final OperationException e) { assertTrue(e.getMessage().contains("job id must be specified")); } }
@Test public void shouldThrowExceptionIfJobTrackerIsNotConfigured() { final CancelScheduledJobHandler handler = new CancelScheduledJobHandler(); final CancelScheduledJob operation = mock(CancelScheduledJob.class); final Store store = mock(Store.class); final User user = mock(User.class); given(store.getJobTracker()).willReturn(null); try { handler.doOperation(operation, new Context(user), store); fail("Exception expected"); } catch (final OperationException e) { assertEquals("JobTracker not enabled", e.getMessage()); } }