public BigDecimal deposit(Long id, BigDecimal amount) { Account account = repository.findOne(id); BigDecimal newBalance = account.getBalance().add(amount); account.setBalance(newBalance); repository.save(account); return newBalance; }
@Override public void updateAccount(Account account) { String sqlTxt = "update account set balance = ? where id = ?"; template.update(sqlTxt, account.getBalance(), account.getId()); }
@Override public Long createAccount(BigDecimal initialBalance) { long id = nextId++; entityManager.persist(new Account(id, initialBalance)); return id; }
@Test public void testCreateAccount() throws Exception { Account account = new Account(99L, new BigDecimal("250.00")); repository.save(account); Long id = account.getId(); assertThat(id, is(notNullValue())); Account again = repository.findOne(id); assertThat(again.getId(), is(id)); assertThat(again.getBalance(), is(closeTo(new BigDecimal("250.0"), new BigDecimal("0.01")))); }
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS) public BigDecimal getBalance(Long id) { return repository.findOne(id).getBalance(); }
@Override public void updateAccount(Account account) { accountMap.put(account.getId(), account); } }
@Test public void testCreateAccount() throws Exception { Account account = new Account(99L, new BigDecimal("250.00")); repository.save(account); Long id = account.getId(); assertThat(id, is(notNullValue())); Account again = repository.findOne(id); assertThat(again.getId(), is(id)); assertThat(again.getBalance(), is(closeTo(new BigDecimal("250.0"), new BigDecimal("0.01")))); }
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS) public BigDecimal getBalance(Long id) { return repository.getAccount(id).getBalance(); }
@Test public void testDeleteAccount() throws Exception { for (Account account : repository.getAccounts()) { repository.deleteAccount(account.getId()); } assertThat(repository.getNumberOfAccounts(), is(0)); } }
public BigDecimal deposit(Long id, BigDecimal amount) { Account account = repository.getAccount(id); BigDecimal newBalance = account.getBalance().add(amount); account.setBalance(newBalance); repository.updateAccount(account); return newBalance; }
@Test public void testGetAccount() throws Exception { Account account = repository.getAccount(1L); assertThat(account.getId(), is(1L)); assertThat(new BigDecimal("100.0"), is(closeTo(account.getBalance(), new BigDecimal("0.01")))); }
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS) public BigDecimal getBalance(Long id) { return repository.getAccount(id).getBalance(); }
@Test public void testDeleteAccount() throws Exception { for (Account account : repository.getAccounts()) { repository.deleteAccount(account.getId()); } assertThat(repository.getNumberOfAccounts(), is(0)); } }
@Override public Long createAccount(BigDecimal initialBalance) { Long id = nextId++; accountMap.put(id, new Account(id, initialBalance)); return id; }
public BigDecimal deposit(Long id, BigDecimal amount) { Account account = repository.getAccount(id); BigDecimal newBalance = account.getBalance().add(amount); account.setBalance(newBalance); repository.updateAccount(account); return newBalance; }
@Test public void testGetAccount() throws Exception { Account account = repository.getAccount(1L); assertThat(account.getId(), is(1L)); assertThat(new BigDecimal("100.0"), is(closeTo(account.getBalance(), new BigDecimal("0.01")))); }
@Test public void testDeleteAccount() throws Exception { // Get the number of accounts before deleting int beforeCount = repository.getNumberOfAccounts(); Account account = repository.getAccount(3L); // Delete an account and check that num is one less repository.deleteAccount(3L); int afterCount = repository.getNumberOfAccounts(); assertThat(afterCount, is(beforeCount - 1)); // Re-add the deleted account repository.createAccount(account.getBalance()); } }
@Override public Account mapRow(ResultSet resultSet, int i) throws SQLException { return new Account(resultSet.getLong("id"), resultSet.getBigDecimal("balance")); } }
@Test public void testUpdateAccount() throws Exception { Account account = repository.findOne(1L); BigDecimal current = account.getBalance(); BigDecimal amount = new BigDecimal("50.0"); account.setBalance(current.add(amount)); repository.save(account); Account again = repository.findOne(1L); assertThat(again.getBalance(), is(closeTo(current.add(amount), new BigDecimal("0.01")))); }
@Test public void testGetAccount() throws Exception { Account account = repository.findOne(1L); assertThat(account.getId(), is(1L)); assertThat(new BigDecimal("100.0"), is(closeTo(account.getBalance(), new BigDecimal("0.01")))); }