AdminJobtype { @Path("modify") @POST @Consumes(MediaType.APPLICATION_JSON) public List<TransactionType> modify(TransactionType jobtype) { try { return ukelonn.modifyJobtype(jobtype); } catch (UkelonnException e) { logservice.log(LogService.LOG_ERROR, String.format("REST endpoint /api/jobtype/modify failed to modify jobtype %d in the database", jobtype.getId()), e); throw new InternalServerErrorException("See log for the cause of the problem"); } }  @Path("modify") @POST @Consumes(MediaType.APPLICATION_JSON) List<TransactionType> modify(TransactionType jobtype); @Path("create") @POST @Consumes(MediaType.APPLICATION_JSON) List<TransactionType> create(TransactionType jobtype);  }
@Test public void testModifyJobtype() { AdminJobtype resource = new AdminJobtype(); UkelonnService ukelonn = mock(UkelonnService.class); resource.ukelonn = ukelonn; List<TransactionType> jobtypes = getJobtypes(); TransactionType jobtype = jobtypes.get(0); Double originalAmount = jobtype.getTransactionAmount(); jobtype.setTransactionAmount(originalAmount + 1); when(ukelonn.modifyJobtype(any())).thenReturn(Arrays.asList(jobtype)); List<TransactionType> updatedJobtypes = resource.modify(jobtype); TransactionType updatedJobtype = updatedJobtypes.get(0); assertThat(updatedJobtype.getTransactionAmount()).isGreaterThan(originalAmount); }
@SuppressWarnings("unchecked") @Test(expected=InternalServerErrorException.class) public void testModifyJobtypeFailure() throws Exception { AdminJobtype resource = new AdminJobtype(); UkelonnServiceProvider ukelonn = new UkelonnServiceProvider(); resource.ukelonn = ukelonn; MockLogService logservice = new MockLogService(); resource.logservice = logservice; ukelonn.setLogservice(logservice); 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); when(statement.executeUpdate()).thenThrow(SQLException.class); ukelonn.setDataSource(datasource); TransactionType jobtype = new TransactionType(-2000, "Foo", 3.14, true, false); resource.modify(jobtype); fail("Should never get here!"); }