UkelonnServiceProvider extends UkelonnServiceBase { @Override public Account getAccount(String username) { try(Connection connection = datasource.getConnection()) { try(PreparedStatement statement = connection.prepareStatement("select * from accounts_view where username=?")) { statement.setString(1, username); try(ResultSet resultset = statement.executeQuery()) { if (resultset.next()) { return mapAccount(resultset); } throw new UkelonnException(String.format("Got an empty ResultSet while fetching account from the database for user \\\"%s\\\"", username)); } } } catch (SQLException e) { throw new UkelonnException(String.format("Caught SQLException while fetching account from the database for user \"%s\"", username), e); } }  @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 testGetAccount() { UkelonnServiceProvider provider = 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); provider.setUserAdmin(useradmin); Account account = provider.getAccount("jad"); assertEquals("jad", account.getUsername()); assertEquals("Jane", account.getFirstName()); assertEquals("Doe", account.getLastName()); List<Transaction> jobs = provider.getJobs(account.getAccountId()); assertEquals(10, jobs.size()); List<Transaction> payments = provider.getPayments(account.getAccountId()); assertEquals(10, payments.size()); }
@Test public void testGetAccountInfoFromDatabaseAccountHasNoTransactions() { UkelonnServiceProvider provider = getUkelonnServiceSingleton(); assertThrows(UkelonnException.class, () -> { provider.getAccount("on"); }); }
@Test public void testGetAccountInfoFromDatabaseWhenAccountDoesNotExist() { UkelonnServiceProvider ukelonn = getUkelonnServiceSingleton(); assertThrows(UkelonnException.class, () -> { ukelonn.getAccount("unknownuser"); }); }