JobResource extends ResourceBase { @Path("/update") @POST @Consumes(MediaType.APPLICATION_JSON) public List<Transaction> doUpdateJob(UpdatedTransaction editedJob) { try { return ukelonn.updateJob(editedJob); } catch (UkelonnException e) { logservice.log(LogService.LOG_ERROR, "REST endpoint /api/job/update failed", e); throw new InternalServerErrorException("See log for details"); } }  @Path("/register") @POST @Consumes(MediaType.APPLICATION_JSON) Account doRegisterJob(PerformedTransaction performedJob); @Path("/update") @POST @Consumes(MediaType.APPLICATION_JSON) List<Transaction> doUpdateJob(UpdatedTransaction editedJob);  }
@Test public void testUpdateJob() { UkelonnService ukelonn = mock(UkelonnService.class); JobResource resource = new JobResource(); MockLogService logservice = new MockLogService(); resource.logservice = logservice; resource.ukelonn = ukelonn; Account account = getJadAccount(); Transaction job = getJadJobs().get(0); int jobId = job.getId(); Integer originalTransactionTypeId = job.getTransactionType().getId(); Date originalTransactionTime = job.getTransactionTime(); double originalTransactionAmount = job.getTransactionAmount(); TransactionType newJobType = findJobTypeWithDifferentIdAndAmount(ukelonn, originalTransactionTypeId, originalTransactionAmount); Date now = new Date(); UpdatedTransaction editedJob = new UpdatedTransaction(jobId, account.getAccountId(), newJobType.getId(), now, newJobType.getTransactionAmount()); when(ukelonn.updateJob(any())).thenReturn(Arrays.asList(convertUpdatedTransaction(editedJob))); List<Transaction> updatedJobs = resource.doUpdateJob(editedJob); Transaction editedJobFromDatabase = updatedJobs.stream().filter(t->t.getId() == job.getId()).collect(Collectors.toList()).get(0); assertEquals(editedJob.getTransactionTypeId(), editedJobFromDatabase.getTransactionType().getId().intValue()); assertThat(editedJobFromDatabase.getTransactionTime().getTime()).isGreaterThan(originalTransactionTime.getTime()); assertEquals(editedJob.getTransactionAmount(), editedJobFromDatabase.getTransactionAmount(), 0.0); }
@Test(expected=InternalServerErrorException.class) public void testUpdateJobGetSQLException() throws Exception { UkelonnServiceProvider ukelonn = new UkelonnServiceProvider(); DataSource datasource = mock(DataSource.class); Connection connection = mock(Connection.class); when(datasource.getConnection()).thenReturn(connection); PreparedStatement statement = mock(PreparedStatement.class); when(connection.prepareStatement(anyString())).thenReturn(statement); doThrow(SQLException.class).when(statement).setInt(anyInt(), anyInt()); ukelonn.setDataSource(datasource); JobResource resource = new JobResource(); resource.ukelonn = ukelonn; MockLogService logservice = new MockLogService(); resource.logservice = logservice; resource.doUpdateJob(new UpdatedTransaction()); fail("Should never get here"); }