AdminJobtype { @Path("create") @POST @Consumes(MediaType.APPLICATION_JSON) public List<TransactionType> create(TransactionType jobtype) { try { return ukelonn.createJobtype(jobtype); } catch (UkelonnException e) { logservice.log(LogService.LOG_ERROR, String.format("REST endpoint /api/jobtype/modify failed to create jobtype \"%s\" in the database", jobtype.getTransactionTypeName()), 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 testCreateJobtype() { AdminJobtype resource = new AdminJobtype(); UkelonnService ukelonn = mock(UkelonnService.class); resource.ukelonn = ukelonn; List<TransactionType> originalJobtypes = getJobtypes(); List<TransactionType> newjobtypes = new ArrayList<>(originalJobtypes); TransactionType jobtype = new TransactionType(-1, "Skrubb badegolv", 200.0, true, false); newjobtypes.add(jobtype); when(ukelonn.createJobtype(jobtype)).thenReturn(newjobtypes); List<TransactionType> updatedJobtypes = resource.create(jobtype); assertThat(updatedJobtypes.size()).isGreaterThan(originalJobtypes.size()); }
@SuppressWarnings("unchecked") @Test(expected=InternalServerErrorException.class) public void testCreateJobtypeFailure() 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.create(jobtype); fail("Should never get here!"); }