AdminPaymenttype { @Path("create") @POST @Consumes(MediaType.APPLICATION_JSON) public List<TransactionType> create(TransactionType paymenttype) { try { return ukelonn.createPaymenttype(paymenttype); } catch (UkelonnException e) { logservice.log(LogService.LOG_ERROR, String.format("REST endpoint /api/jobtype/modify failed to create payment type \"%s\" in the database", paymenttype.getTransactionTypeName()), e); throw new InternalServerErrorException("See log for the cause of the problem"); } }  @Path("modify") @POST @Consumes(MediaType.APPLICATION_JSON) List<TransactionType> modify(TransactionType paymenttype); @Path("create") @POST @Consumes(MediaType.APPLICATION_JSON) List<TransactionType> create(TransactionType paymenttype);  }
@Test public void testCreatePaymenttype() { AdminPaymenttype resource = new AdminPaymenttype(); UkelonnService ukelonn = mock(UkelonnService.class); resource.ukelonn = ukelonn; List<TransactionType> originalPaymenttypes = getPaymenttypes(); List<TransactionType> newPaymenttypes = new ArrayList<>(originalPaymenttypes); TransactionType paymenttype = new TransactionType(-2001, "Bar", 0.0, false, true); newPaymenttypes.add(paymenttype); when(ukelonn.createPaymenttype(any())).thenReturn(newPaymenttypes); List<TransactionType> updatedPaymenttypes = resource.create(paymenttype); assertThat(updatedPaymenttypes.size()).isGreaterThan(originalPaymenttypes.size()); }
@SuppressWarnings("unchecked") @Test(expected=InternalServerErrorException.class) public void testCreatePaymenttypeFailure() throws Exception { AdminPaymenttype resource = new AdminPaymenttype(); UkelonnServiceProvider ukelonn = new UkelonnServiceProvider(); resource.ukelonn = ukelonn; MockLogService logservice = new MockLogService(); ukelonn.setLogservice(logservice); resource.logservice = 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 paymenttype = new TransactionType(-2001, "Bar", 0.0, false, true); resource.create(paymenttype); fail("Should never get here!"); }