@Test public void testRewardForDining() { // create a new dining of 100.00 charged to credit card '1234123412341234' by merchant // '123457890' as test input Dining dining = Dining.createDining("100.00", "1234123412341234", "1234567890"); // TODO: replace 'null' below with a call to the 'rewardNetwork' to test its // rewardAccountFor(Dining) method RewardConfirmation confirmation = rewardNetwork.rewardAccountFor(dining); // assert the expected reward confirmation results assertNotNull(confirmation); assertNotNull(confirmation.getConfirmationNumber()); // assert an account contribution was made AccountContribution contribution = confirmation.getAccountContribution(); assertNotNull(contribution); // the account number should be '123456789' assertEquals("123456789", contribution.getAccountNumber()); // the total contribution amount should be 8.00 (8% of 100.00) assertEquals(MonetaryAmount.valueOf("8.00"), contribution.getAmount()); // the total contribution amount should have been split into 2 distributions assertEquals(2, contribution.getDistributions().size()); // each distribution should be 4.00 (as both have a 50% allocation) assertEquals( MonetaryAmount.valueOf("4.00"), contribution.getDistribution("Annabelle").getAmount()); assertEquals( MonetaryAmount.valueOf("4.00"), contribution.getDistribution("Corgan").getAmount()); }
public RewardConfirmation confirmReward(AccountContribution contribution, Dining dining) { String sql = "insert into T_REWARD (CONFIRMATION_NUMBER, REWARD_AMOUNT, REWARD_DATE, ACCOUNT_NUMBER, DINING_MERCHANT_NUMBER, DINING_DATE, DINING_AMOUNT) values (?, ?, ?, ?, ?, ?, ?)"; Connection conn = null; PreparedStatement ps = null; try { conn = dataSource.getConnection(); ps = conn.prepareStatement(sql); String confirmationNumber = nextConfirmationNumber(); ps.setString(1, confirmationNumber); ps.setBigDecimal(2, contribution.getAmount().asBigDecimal()); ps.setDate(3, new Date(SimpleDate.today().inMilliseconds())); ps.setString(4, contribution.getAccountNumber()); ps.setString(5, dining.getMerchantNumber()); ps.setDate(6, new Date(dining.getDate().inMilliseconds())); ps.setBigDecimal(7, dining.getAmount().asBigDecimal()); ps.execute(); return new RewardConfirmation(confirmationNumber, contribution); } catch (SQLException e) { throw new RuntimeException("SQL exception occured inserting reward record", e); } finally { if (ps != null) { try { // Close to prevent database cursor exhaustion ps.close(); } catch (SQLException ex) { } } if (conn != null) { try { // Close to prevent database connection exhaustion conn.close(); } catch (SQLException ex) { } } } }