AdminPaymenttype { @Path("modify") @POST @Consumes(MediaType.APPLICATION_JSON) public List<TransactionType> modify(TransactionType paymenttype) { try { return ukelonn.modifyPaymenttype(paymenttype); } catch (UkelonnException e) { logservice.log(LogService.LOG_ERROR, String.format("REST endpoint /api/paymenttype/modify failed to modify payment type %d in the database", paymenttype.getId()), 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 testModifyPaymenttype() { AdminPaymenttype resource = new AdminPaymenttype(); UkelonnService ukelonn = mock(UkelonnService.class); resource.ukelonn = ukelonn; List<TransactionType> paymenttypes = getPaymenttypes(); TransactionType paymenttype = paymenttypes.get(1); Double originalAmount = paymenttype.getTransactionAmount(); paymenttype.setTransactionAmount(originalAmount + 1); when(ukelonn.modifyPaymenttype(paymenttype)).thenReturn(Arrays.asList(paymenttype)); List<TransactionType> updatedPaymenttypes = resource.modify(paymenttype); TransactionType updatedPaymenttype = updatedPaymenttypes.get(0); assertThat(updatedPaymenttype.getTransactionAmount()).isGreaterThan(originalAmount); }
@SuppressWarnings("unchecked") @Test(expected=InternalServerErrorException.class) public void testModifyPaymenttypeFailure() throws Exception { AdminPaymenttype resource = new AdminPaymenttype(); 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 paymenttype = new TransactionType(-2001, "Bar", 0.0, false, true); resource.modify(paymenttype); fail("Should never get here!"); }