AdminJobs { @Path("delete") @POST @Consumes(MediaType.APPLICATION_JSON) public List<Transaction> delete(AccountWithJobIds accountWithJobIds) { try { return ukelonn.deleteJobsFromAccount(accountWithJobIds.getAccount().getAccountId(), accountWithJobIds.getJobIds()); } catch (Exception e) { String message = "REST endpoint /ukelonn/admin/jobs/delete failed with exception"; logservice.log(LogService.LOG_ERROR, message, e); throw new InternalServerErrorException(String.format("%s, see log for details", message)); } }  @Path("delete") @POST @Consumes(MediaType.APPLICATION_JSON) List<Transaction> delete(AccountWithJobIds accountWithJobIds);  }
@Test public void testDeleteAllJobsOfUser() { AdminJobs resource = new AdminJobs(); UkelonnService ukelonn = mock(UkelonnService.class); resource.ukelonn = ukelonn; Account account = getJodAccount(); List<Transaction> jobs = getJodJobs(); List<Integer> jobIds = Arrays.asList(jobs.get(0).getId(), jobs.get(1).getId()); AccountWithJobIds accountWithJobIds = new AccountWithJobIds(account, jobIds); List<Transaction> jobsAfterDelete = resource.delete(accountWithJobIds); assertEquals(0, jobsAfterDelete.size()); }
@SuppressWarnings("unchecked") @Test public void testDeleteSomeJobsOfUser() { AdminJobs resource = new AdminJobs(); UkelonnService ukelonn = mock(UkelonnService.class); when(ukelonn.deleteJobsFromAccount(anyInt(), anyList())).thenReturn(getFirstJodJob()); resource.ukelonn = ukelonn; Account account = getJodAccount(); List<Transaction> jobs = getJodJobs(); List<Integer> idsOfJobsToDelete = Arrays.asList(jobs.get(0).getId()); AccountWithJobIds accountWithJobIds = new AccountWithJobIds(account, idsOfJobsToDelete); List<Transaction> jobsAfterDelete = resource.delete(accountWithJobIds); assertEquals(1, jobsAfterDelete.size()); }
@SuppressWarnings("unchecked") @Test public void verifyDeletingNoJobsOfUserHasNoEffect() { AdminJobs resource = new AdminJobs(); UkelonnService ukelonn = mock(UkelonnService.class); when(ukelonn.deleteJobsFromAccount(anyInt(), anyList())).thenReturn(getJodJobs()); resource.ukelonn = ukelonn; Account account = getJodAccount(); List<Integer> idsOfJobsToDelete = Collections.emptyList(); AccountWithJobIds accountWithJobIds = new AccountWithJobIds(account, idsOfJobsToDelete); List<Transaction> jobsAfterDelete = resource.delete(accountWithJobIds); assertEquals(2, jobsAfterDelete.size()); }
@Test(expected=InternalServerErrorException.class) public void verifyExceptionIsThrownWhenFailingToSetDeleteJobParameter() throws Exception { UkelonnServiceProvider ukelonn = new UkelonnServiceProvider(); MockLogService logservice = new MockLogService(); ukelonn.setLogservice(logservice); DataSource datasource = mock(DataSource.class); Connection connection = mock(Connection.class); when(datasource.getConnection()).thenReturn(connection); PreparedStatement statement = mock(PreparedStatement.class); doThrow(SQLException.class).when(statement).setInt(anyInt(), anyInt()); when(connection.prepareStatement(anyString())).thenReturn(statement); ukelonn.setDataSource(datasource); AdminJobs resource = new AdminJobs(); resource.ukelonn = ukelonn; resource.logservice = logservice; Account account = getJodAccount(); List<Integer> idsOfJobsToDelete = Arrays.asList(1); AccountWithJobIds accountWithJobIds = new AccountWithJobIds(account, idsOfJobsToDelete); resource.delete(accountWithJobIds); fail("Should never get here!"); }