UkelonnServiceProvider extends UkelonnServiceBase { @Override public List<Transaction> updateJob(UpdatedTransaction editedJob) { String sql = "update transactions set transaction_type_id=?, transaction_time=?, transaction_amount=? where transaction_id=?"; try(Connection connection = datasource.getConnection()) { try(PreparedStatement statement = connection.prepareStatement(sql)) { statement.setInt(1, editedJob.getTransactionTypeId()); statement.setTimestamp(2, new java.sql.Timestamp(editedJob.getTransactionTime().getTime())); statement.setDouble(3, editedJob.getTransactionAmount()); statement.setInt(4, editedJob.getId()); statement.executeUpdate(); } } catch (SQLException e) { throw new UkelonnException(String.format("Failed to update job with id %d", editedJob.getId()) , e); } return getJobs(editedJob.getAccountId()); }  @Activate void activate(); @Reference(target = "(osgi.jndi.service.name=jdbc/ukelonn)") void setDataSource(DataSource datasource); @Override DataSource getDataSource(); @Reference void setUserAdmin(UserManagementService useradmin); @Reference void setLogservice(LogService logservice); @Override LogService getLogservice(); @Override List<Account> getAccounts(); @Override Account getAccount(String username); @Override Account registerPerformedJob(PerformedTransaction job); @Override List<TransactionType> getJobTypes(); @Override List<Transaction> getJobs(int accountId); @Override List<Transaction> getPayments(int accountId); @Override List<Transaction> deleteJobsFromAccount(int accountId, List<Integer> idsOfJobsToDelete); @Override List<Transaction> updateJob(UpdatedTransaction editedJob); @Override List<TransactionType> getPaymenttypes(); @Override Account registerPayment(PerformedTransaction payment); @Override List<TransactionType> modifyJobtype(TransactionType jobtype); @Override List<TransactionType> createJobtype(TransactionType jobtype); @Override List<TransactionType> modifyPaymenttype(TransactionType paymenttype); @Override List<TransactionType> createPaymenttype(TransactionType paymenttype); @Override Account addAccount(User user); @Override List<SumYear> earningsSumOverYear(String username); @Override List<SumYearMonth> earningsSumOverMonth(String username); @Override List<Notification> notificationsTo(String username); @Override void notificationTo(String username, Notification notification); Account mapAccount(ResultSet results);  }
@Test public void testUpdateJob() throws Exception { try { UkelonnServiceProvider ukelonn = getUkelonnServiceSingleton(); String username = "jad"; UserManagementService useradmin = mock(UserManagementService.class); no.priv.bang.osgiservice.users.User user = new no.priv.bang.osgiservice.users.User(1, username, "jad@gmail.com", "Jane", "Doe"); when(useradmin.getUser(anyString())).thenReturn(user); ukelonn.setUserAdmin(useradmin); Account account = ukelonn.getAccount(username); Transaction job = ukelonn.getJobs(account.getAccountId()).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()); List<Transaction> updatedJobs = ukelonn.updateJob(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); } finally { restoreTestDatabase(); } }