UkelonnServiceProvider extends UkelonnServiceBase { @Override public Account registerPayment(PerformedTransaction payment) { int accountId = payment.getAccount().getAccountId(); int transactionTypeId = payment.getTransactionTypeId(); double amount = 0 - payment.getTransactionAmount(); Date transactionDate = new Date(); try(Connection connection = datasource.getConnection()) { try(PreparedStatement statement = connection.prepareStatement("insert into transactions (account_id,transaction_type_id,transaction_amount, transaction_time) values (?, ?, ?, ?)")) { statement.setInt(1, accountId); statement.setInt(2, transactionTypeId); statement.setDouble(3, amount); statement.setTimestamp(4, new java.sql.Timestamp(transactionDate.getTime())); statement.executeUpdate(); } } catch (SQLException e) { String message = String.format("Failed to register payment accountId: %d transactionTypeId: %d amount: %f", accountId, transactionTypeId, amount); logError(message, e); return null; } return getAccount(payment.getAccount().getUsername()); }  @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 testRegisterPayment() { UkelonnServiceProvider ukelonn = getUkelonnServiceSingleton(); UserManagementService useradmin = mock(UserManagementService.class); no.priv.bang.osgiservice.users.User user = new no.priv.bang.osgiservice.users.User(1, "jad", "jad@gmail.com", "Jane", "Doe"); when(useradmin.getUser(anyString())).thenReturn(user); ukelonn.setUserAdmin(useradmin); Account account = ukelonn.getAccount("jad"); double originalBalance = account.getBalance(); List<TransactionType> paymenttypes = ukelonn.getPaymenttypes(); PerformedTransaction payment = new PerformedTransaction(account, paymenttypes.get(0).getId(), account.getBalance(), new Date()); Account result = ukelonn.registerPayment(payment); assertEquals("jad", result.getUsername()); assertThat(result.getBalance()).isLessThan(originalBalance); }