Repository navigation
feature(COG-1143): W0 smoke suite for deposit, withdraw, between-accounts and recipient transfer #106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
patrickbradley-cog
wants to merge
3
commits into
devin/cog-1141-w0-testcontainers-mysql-harness-for-springboottest-boot-154
Choose a base branch
from
devin/cog-1143-w0-smoke-suite-deposit-withdraw-between-accounts-and
base: devin/cog-1141-w0-testcontainers-mysql-harness-for-springboottest-boot-154
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feature(COG-1143): W0 smoke suite for deposit, withdraw, between-accounts and recipient transfer #106
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
9b10175
feature(COG-1143): W0 smoke suite for deposit, withdraw, between-acco…
devin-ai-integration[bot] 5faa676
feature(COG-1143): assert recipient list contents and cover savings r…
devin-ai-integration[bot] 3c92928
feature(COG-1143): use canonical harness mockMvc, txsmoke- test data,…
devin-ai-integration[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
350 changes: 350 additions & 0 deletions
350
UserFront/src/test/java/com/userFront/MoneyMovementSmokeTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,350 @@ | ||
| package com.userFront; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertNotNull; | ||
| import static org.junit.Assert.assertNull; | ||
| import static org.junit.Assert.assertTrue; | ||
| import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user; | ||
| import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
| import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | ||
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model; | ||
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl; | ||
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
|
|
||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.test.web.servlet.MvcResult; | ||
| import org.springframework.test.web.servlet.ResultActions; | ||
|
|
||
| import com.userFront.dao.PrimaryAccountDao; | ||
| import com.userFront.dao.PrimaryTransactionDao; | ||
| import com.userFront.dao.RecipientDao; | ||
| import com.userFront.dao.SavingsAccountDao; | ||
| import com.userFront.dao.SavingsTransactionDao; | ||
| import com.userFront.domain.PrimaryAccount; | ||
| import com.userFront.domain.PrimaryTransaction; | ||
| import com.userFront.domain.Recipient; | ||
| import com.userFront.domain.SavingsAccount; | ||
| import com.userFront.domain.SavingsTransaction; | ||
| import com.userFront.domain.User; | ||
| import com.userFront.domain.security.Role; | ||
| import com.userFront.domain.security.UserRole; | ||
| import com.userFront.service.UserService; | ||
|
|
||
| /** | ||
| * Regression contract for money movement on Boot 1.5.4: deposit, withdraw, | ||
| * between-accounts transfer, the recipient lifecycle and recipient transfer. | ||
| * Asserts on the persisted rows, and records today's behaviour as-is. | ||
| */ | ||
| public class MoneyMovementSmokeTest extends AbstractIntegrationTest { | ||
|
|
||
| private static final AtomicInteger SEQ = new AtomicInteger(); | ||
|
|
||
| @Autowired | ||
| private UserService userService; | ||
|
|
||
| @Autowired | ||
| private PrimaryAccountDao primaryAccountDao; | ||
|
|
||
| @Autowired | ||
| private SavingsAccountDao savingsAccountDao; | ||
|
|
||
| @Autowired | ||
| private PrimaryTransactionDao primaryTransactionDao; | ||
|
|
||
| @Autowired | ||
| private SavingsTransactionDao savingsTransactionDao; | ||
|
|
||
| @Autowired | ||
| private RecipientDao recipientDao; | ||
|
|
||
| private String username; | ||
|
|
||
| @Before | ||
| public void createUser() { | ||
| int n = SEQ.incrementAndGet(); | ||
| username = "txsmoke-" + System.nanoTime() + "-" + n; | ||
|
|
||
| User user = new User(); | ||
| user.setUsername(username); | ||
| user.setPassword("password"); | ||
| user.setFirstName("Smoke"); | ||
| user.setLastName("TxSmoke"); | ||
| user.setEmail(username + "@example.com"); | ||
| user.setPhone("5551234567"); | ||
|
|
||
| Role role = new Role(); | ||
| role.setRoleId(1); | ||
| role.setName("ROLE_USER"); | ||
|
|
||
| Set<UserRole> roles = new HashSet<>(); | ||
| roles.add(new UserRole(user, role)); | ||
|
|
||
| userService.createUser(user, roles); | ||
| } | ||
|
|
||
| @Test | ||
| public void depositCreditsPrimaryAccountAndWritesLedgerRow() throws Exception { | ||
| mockMvc.perform(post("/account/deposit").with(user(username)).param("accountType", "Primary").param("amount", | ||
| "250.00")).andExpect(status().is3xxRedirection()).andExpect(redirectedUrl("/userFront")); | ||
|
|
||
| assertEquals(0, new BigDecimal("250.00").compareTo(primaryBalance())); | ||
|
|
||
| PrimaryTransaction transaction = onlyPrimaryTransaction(); | ||
| assertEquals("Deposit to Primary Account", transaction.getDescription()); | ||
| assertEquals("Account", transaction.getType()); | ||
| assertEquals("Finished", transaction.getStatus()); | ||
| assertEquals(250.00, transaction.getAmount(), 0.001); | ||
| assertEquals(0, new BigDecimal("250.00").compareTo(transaction.getAvailableBalance())); | ||
| } | ||
|
|
||
| @Test | ||
| public void depositCreditsSavingsAccountAndWritesLedgerRow() throws Exception { | ||
| mockMvc.perform(post("/account/deposit").with(user(username)).param("accountType", "Savings").param("amount", | ||
| "75.50")).andExpect(status().is3xxRedirection()); | ||
|
|
||
| assertEquals(0, new BigDecimal("75.50").compareTo(savingsBalance())); | ||
|
|
||
| SavingsTransaction transaction = onlySavingsTransaction(); | ||
| assertEquals("Deposit to savings Account", transaction.getDescription()); | ||
| assertEquals(75.50, transaction.getAmount(), 0.001); | ||
| assertEquals(0, new BigDecimal("75.50").compareTo(transaction.getAvailableBalance())); | ||
| } | ||
|
|
||
| @Test | ||
| public void withdrawDebitsPrimaryAccountAndWritesLedgerRow() throws Exception { | ||
| deposit("Primary", "400.00"); | ||
|
|
||
| mockMvc.perform(post("/account/withdraw").with(user(username)).param("accountType", "Primary").param("amount", | ||
| "150.00")).andExpect(status().is3xxRedirection()).andExpect(redirectedUrl("/userFront")); | ||
|
|
||
| assertEquals(0, new BigDecimal("250.00").compareTo(primaryBalance())); | ||
|
|
||
| PrimaryTransaction withdrawal = lastPrimaryTransaction(); | ||
| assertEquals("Withdraw from Primary Account", withdrawal.getDescription()); | ||
| // The withdrawal is recorded as a positive amount; only availableBalance moves down. | ||
| assertEquals(150.00, withdrawal.getAmount(), 0.001); | ||
| assertEquals(0, new BigDecimal("250.00").compareTo(withdrawal.getAvailableBalance())); | ||
| } | ||
|
|
||
| @Test | ||
| public void withdrawDebitsSavingsAccountAndWritesLedgerRow() throws Exception { | ||
| deposit("Savings", "300.00"); | ||
|
|
||
| mockMvc.perform(post("/account/withdraw").with(user(username)).param("accountType", "Savings").param("amount", | ||
| "120.00")).andExpect(status().is3xxRedirection()); | ||
|
|
||
| assertEquals(0, new BigDecimal("180.00").compareTo(savingsBalance())); | ||
|
|
||
| SavingsTransaction withdrawal = lastSavingsTransaction(); | ||
| assertEquals("Withdraw from savings Account", withdrawal.getDescription()); | ||
| assertEquals(120.00, withdrawal.getAmount(), 0.001); | ||
| assertEquals(0, new BigDecimal("180.00").compareTo(withdrawal.getAvailableBalance())); | ||
| } | ||
|
|
||
| @Test | ||
| public void betweenAccountsTransferPrimaryToSavingsMovesBalancesAndConservesTotal() throws Exception { | ||
| deposit("Primary", "500.00"); | ||
| BigDecimal totalBefore = primaryBalance().add(savingsBalance()); | ||
|
|
||
| mockMvc.perform(post("/transfer/betweenAccounts").with(user(username)).param("transferFrom", "Primary") | ||
| .param("transferTo", "Savings").param("amount", "200.00")).andExpect(status().is3xxRedirection()) | ||
| .andExpect(redirectedUrl("/userFront")); | ||
|
|
||
| assertEquals(0, new BigDecimal("300.00").compareTo(primaryBalance())); | ||
| assertEquals(0, new BigDecimal("200.00").compareTo(savingsBalance())); | ||
| assertEquals(0, totalBefore.compareTo(primaryBalance().add(savingsBalance()))); | ||
|
|
||
| PrimaryTransaction transfer = lastPrimaryTransaction(); | ||
| assertEquals("Between account transfer from Primary to Savings", transfer.getDescription()); | ||
| assertEquals("Account", transfer.getType()); | ||
| assertEquals(200.00, transfer.getAmount(), 0.001); | ||
| // Only the sending ledger is written in this direction. | ||
| assertTrue(savingsTransactions().isEmpty()); | ||
| } | ||
|
|
||
| @Test | ||
| public void betweenAccountsTransferSavingsToPrimaryMovesBalancesAndConservesTotal() throws Exception { | ||
| deposit("Savings", "500.00"); | ||
| List<PrimaryTransaction> primaryBefore = primaryTransactions(); | ||
| BigDecimal totalBefore = primaryBalance().add(savingsBalance()); | ||
|
|
||
| mockMvc.perform(post("/transfer/betweenAccounts").with(user(username)).param("transferFrom", "Savings") | ||
| .param("transferTo", "Primary").param("amount", "125.00")).andExpect(status().is3xxRedirection()); | ||
|
|
||
| assertEquals(0, new BigDecimal("125.00").compareTo(primaryBalance())); | ||
| assertEquals(0, new BigDecimal("375.00").compareTo(savingsBalance())); | ||
| assertEquals(0, totalBefore.compareTo(primaryBalance().add(savingsBalance()))); | ||
|
|
||
| SavingsTransaction transfer = lastSavingsTransaction(); | ||
| assertEquals("Between account transfer from Savings to Primary", transfer.getDescription()); | ||
| assertEquals("Transfer", transfer.getType()); | ||
| assertEquals(125.00, transfer.getAmount(), 0.001); | ||
| // Only the sending ledger is written in this direction. | ||
| assertEquals(primaryBefore.size(), primaryTransactions().size()); | ||
| } | ||
|
|
||
| @Test | ||
| public void recipientLifecycleSavesListsAndDeletes() throws Exception { | ||
| String recipientName = "txsmoke-payee-" + System.nanoTime(); | ||
|
|
||
| mockMvc.perform(post("/transfer/recipient/save").with(user(username)).param("name", recipientName) | ||
| .param("email", "txsmoke-payee@example.com").param("phone", "5559876543").param("accountNumber", "987654321") | ||
| .param("description", "Rent")).andExpect(status().is3xxRedirection()) | ||
| .andExpect(redirectedUrl("/transfer/recipient")); | ||
|
|
||
| Recipient saved = recipientDao.findByName(recipientName); | ||
| assertNotNull(saved); | ||
| assertEquals("txsmoke-payee@example.com", saved.getEmail()); | ||
| assertEquals("987654321", saved.getAccountNumber()); | ||
| assertEquals(username, saved.getUser().getUsername()); | ||
|
|
||
| MvcResult listed = mockMvc.perform(get("/transfer/recipient").with(user(username))).andExpect(status().isOk()) | ||
| .andExpect(view().name("recipient")).andExpect(model().attributeExists("recipientList")).andReturn(); | ||
| assertEquals(Collections.singletonList(recipientName), recipientNames(listed)); | ||
|
|
||
| deleteRecipient(recipientName).andExpect(status().isOk()).andExpect(view().name("recipient")); | ||
|
|
||
| assertNull(recipientDao.findByName(recipientName)); | ||
| } | ||
|
|
||
| @Test | ||
| public void transferToSomeoneElseDebitsTheSendingAccount() throws Exception { | ||
| deposit("Primary", "600.00"); | ||
| String recipientName = "txsmoke-payee-" + System.nanoTime(); | ||
| saveRecipient(recipientName); | ||
|
|
||
| mockMvc.perform(post("/transfer/toSomeoneElse").with(user(username)).param("recipientName", recipientName) | ||
| .param("accountType", "Primary").param("amount", "175.00")).andExpect(status().is3xxRedirection()) | ||
| .andExpect(redirectedUrl("/userFront")); | ||
|
|
||
| assertEquals(0, new BigDecimal("425.00").compareTo(primaryBalance())); | ||
|
|
||
| PrimaryTransaction debit = lastPrimaryTransaction(); | ||
| assertEquals("Transfer to recipient " + recipientName, debit.getDescription()); | ||
| assertEquals("Transfer", debit.getType()); | ||
| assertEquals("Finished", debit.getStatus()); | ||
| assertEquals(175.00, debit.getAmount(), 0.001); | ||
| assertEquals(0, new BigDecimal("425.00").compareTo(debit.getAvailableBalance())); | ||
| } | ||
|
|
||
| @Test | ||
| public void transferToSomeoneElseDebitsTheSendingSavingsAccount() throws Exception { | ||
| deposit("Savings", "600.00"); | ||
| String recipientName = "txsmoke-payee-" + System.nanoTime(); | ||
| saveRecipient(recipientName); | ||
|
|
||
| mockMvc.perform(post("/transfer/toSomeoneElse").with(user(username)).param("recipientName", recipientName) | ||
| .param("accountType", "Savings").param("amount", "175.00")).andExpect(status().is3xxRedirection()) | ||
| .andExpect(redirectedUrl("/userFront")); | ||
|
|
||
| assertEquals(0, new BigDecimal("425.00").compareTo(savingsBalance())); | ||
|
|
||
| SavingsTransaction debit = lastSavingsTransaction(); | ||
| assertEquals("Transfer to recipient " + recipientName, debit.getDescription()); | ||
| assertEquals("Transfer", debit.getType()); | ||
| assertEquals("Finished", debit.getStatus()); | ||
| assertEquals(175.00, debit.getAmount(), 0.001); | ||
| assertEquals(0, new BigDecimal("425.00").compareTo(debit.getAvailableBalance())); | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| private List<String> recipientNames(MvcResult result) { | ||
| List<Recipient> recipients = (List<Recipient>) result.getModelAndView().getModel().get("recipientList"); | ||
| List<String> names = new ArrayList<>(); | ||
| for (Recipient recipient : recipients) { | ||
| names.add(recipient.getName()); | ||
| } | ||
| return names; | ||
| } | ||
|
|
||
| /** Delete is a GET today; COG-1155 converts it to a POST. */ | ||
| private ResultActions deleteRecipient(String recipientName) throws Exception { | ||
| return mockMvc.perform(get("/transfer/recipient/delete").with(user(username)).param("recipientName", recipientName)); | ||
| } | ||
|
|
||
| private void deposit(String accountType, String amount) throws Exception { | ||
| mockMvc.perform(post("/account/deposit").with(user(username)).param("accountType", accountType).param("amount", | ||
| amount)).andExpect(status().is3xxRedirection()); | ||
| } | ||
|
|
||
| private void saveRecipient(String recipientName) throws Exception { | ||
| mockMvc.perform(post("/transfer/recipient/save").with(user(username)).param("name", recipientName) | ||
| .param("email", "txsmoke-payee@example.com").param("phone", "5559876543").param("accountNumber", "987654321") | ||
| .param("description", "Rent")).andExpect(status().is3xxRedirection()); | ||
| } | ||
|
|
||
| private BigDecimal primaryBalance() { | ||
| return reloadPrimaryAccount().getAccountBalance(); | ||
| } | ||
|
|
||
| private BigDecimal savingsBalance() { | ||
| return reloadSavingsAccount().getAccountBalance(); | ||
| } | ||
|
|
||
| private PrimaryAccount reloadPrimaryAccount() { | ||
| return primaryAccountDao.findByAccountNumber(userService.findByUsername(username).getPrimaryAccount() | ||
| .getAccountNumber()); | ||
| } | ||
|
|
||
| private SavingsAccount reloadSavingsAccount() { | ||
| return savingsAccountDao.findByAccountNumber(userService.findByUsername(username).getSavingsAccount() | ||
| .getAccountNumber()); | ||
| } | ||
|
|
||
| private List<PrimaryTransaction> primaryTransactions() { | ||
| Long accountId = reloadPrimaryAccount().getId(); | ||
| List<PrimaryTransaction> transactions = new ArrayList<>(); | ||
| for (PrimaryTransaction transaction : primaryTransactionDao.findAll()) { | ||
| if (accountId.equals(transaction.getPrimaryAccount().getId())) { | ||
| transactions.add(transaction); | ||
| } | ||
| } | ||
| Collections.sort(transactions, (a, b) -> a.getId().compareTo(b.getId())); | ||
| return transactions; | ||
| } | ||
|
|
||
| private List<SavingsTransaction> savingsTransactions() { | ||
| Long accountId = reloadSavingsAccount().getId(); | ||
| List<SavingsTransaction> transactions = new ArrayList<>(); | ||
| for (SavingsTransaction transaction : savingsTransactionDao.findAll()) { | ||
| if (accountId.equals(transaction.getSavingsAccount().getId())) { | ||
| transactions.add(transaction); | ||
| } | ||
| } | ||
| Collections.sort(transactions, (a, b) -> a.getId().compareTo(b.getId())); | ||
| return transactions; | ||
| } | ||
|
|
||
| private PrimaryTransaction onlyPrimaryTransaction() { | ||
| List<PrimaryTransaction> transactions = primaryTransactions(); | ||
| assertEquals(1, transactions.size()); | ||
| return transactions.get(0); | ||
| } | ||
|
|
||
| private SavingsTransaction onlySavingsTransaction() { | ||
| List<SavingsTransaction> transactions = savingsTransactions(); | ||
| assertEquals(1, transactions.size()); | ||
| return transactions.get(0); | ||
| } | ||
|
|
||
| private PrimaryTransaction lastPrimaryTransaction() { | ||
| List<PrimaryTransaction> transactions = primaryTransactions(); | ||
| return transactions.get(transactions.size() - 1); | ||
| } | ||
|
|
||
| private SavingsTransaction lastSavingsTransaction() { | ||
| List<SavingsTransaction> transactions = savingsTransactions(); | ||
| return transactions.get(transactions.size() - 1); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.