diff --git a/src/test/java/runner/security/JwtTest.java b/src/test/java/runner/security/JwtTest.java index 0434cab45..84128f562 100644 --- a/src/test/java/runner/security/JwtTest.java +++ b/src/test/java/runner/security/JwtTest.java @@ -39,6 +39,7 @@ import java.util.ArrayList; +import static org.mockito.ArgumentMatchers.any; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @@ -86,7 +87,7 @@ public void AuthenticatedTest() throws Exception{ public void AuthenticatedTokenAccess() throws Exception{ UserDetails user = new User("user2","password", new ArrayList<>()); String token = jwtUtil.generateToken(user); //bypassing spring security authentication and generate token - Mockito.when(userDetailServices.loadUserByUsername("user2")).thenReturn(user); + Mockito.when(userDetailServices.loadUserByUsername(any())).thenReturn(user); mockMvc.perform(MockMvcRequestBuilders.get("/myaccount/test") .header("Authorization", "Bearer "+ token)) .andExpect(status().isOk()); diff --git a/src/test/java/runner/services/AccountServiceTest.java b/src/test/java/runner/services/AccountServiceTest.java index 955d03f56..74dd05952 100644 --- a/src/test/java/runner/services/AccountServiceTest.java +++ b/src/test/java/runner/services/AccountServiceTest.java @@ -8,6 +8,7 @@ import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; +import org.omg.IOP.TransactionService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.MockBean; @@ -22,9 +23,13 @@ import runner.enums.AccountType; import runner.repositories.AccountRepo; +import java.time.LocalDate; +import java.util.ArrayList; import java.util.HashSet; import java.util.Set; +import static org.mockito.ArgumentMatchers.any; + @ActiveProfiles("test") @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = AppRunner.class) @@ -37,28 +42,49 @@ public class AccountServiceTest { @Mock AccountRepo accountRepo; + @Mock + TransactionServices transactionServices; @InjectMocks AccountServices accountServices; Account account1; Account account2; + Account account3; + Set testAccounts; Login login; Customer customer; - Set testAccounts; Transaction transaction; Set transactionAccount; + Transaction withdrawalTransaction; + Transaction depositTransaction; + ArrayList myTransactionList; @Before public void setup(){ - transactionAccount = new HashSet(); - transactionAccount.add(account1); - transaction = new Transaction(1.00,transactionAccount); - account1 = new Account(1L,"123", AccountType.CHECKING,100.00,"abcdefg", new HashSet()); - account2 = new Account(2L,"321", AccountType.SAVINGS,0.00,"gfedcba", new HashSet()); + account1 = new Account(1L,"12345", AccountType.CHECKING,100.00,"abcdefg", new HashSet()); + account2 = new Account(2L,"54321", AccountType.SAVINGS,0.00,"gfedcba", new HashSet()); + account3 = new Account(2L,"56789", AccountType.SAVINGS,100.00,"qwerty", new HashSet()); testAccounts = new HashSet(); testAccounts.add(account1); testAccounts.add(account2); login = new Login(1L,"user","password",customer); //customer would be null here due to order of code; customer = new Customer(1L,"John","Doe",login,testAccounts); + transactionAccount = new HashSet(); + transactionAccount.add(account1); + transaction = new Transaction(1.00,transactionAccount); + + //=============== setup for transfer tests below, also used in deposit and withdraw ==================== + + withdrawalTransaction = new Transaction(String.format("Withdrawal to %s XXXXXXXX%s",account2.getAccountType(), + account2.getAccountNumber().substring(account2.getAccountNumber().length()-4)), + transaction.getTransactionAmount()*(-1), account1.getBalance(), LocalDate.now()); + + depositTransaction = new Transaction(String.format("Deposit from %s XXXXXXXX%s",account1.getAccountType(), + account1.getAccountNumber().substring(account1.getAccountNumber().length()-4)), + transaction.getTransactionAmount()*(-1), account2.getBalance(), LocalDate.now()); + + myTransactionList = new ArrayList(); + myTransactionList.add(withdrawalTransaction); + myTransactionList.add(depositTransaction); } //PREFACE: we know the behavior of the accountRepo, this is unit test for accountServices; therefore, the results are set by the tester fro the repo @@ -67,7 +93,7 @@ public void setup(){ public void getAllAccountsTest(){ //given: when accountServices.getAlAccounts calls accountRepo.findAccountsByCustomer_LoginUsername, it will return the test Accounts; Set expectedAccounts = testAccounts; - Mockito.when(accountRepo.findAccountsByCustomer_LoginUsername(login.getUsername())).thenReturn(testAccounts); + Mockito.when(accountRepo.findAccountsByCustomer_LoginUsername(any())).thenReturn(testAccounts); //when: telling accountServices to get all accounts Set actualAccounts = accountServices.getAllAccounts(login.getUsername()); //the two sets should be identical @@ -77,7 +103,7 @@ public void getAllAccountsTest(){ @Test public void findAccountByEncryptedUrlTest(){ Account expectedAccount = account1; - Mockito.when(accountRepo.findAccountByEncryptedUrl(account1.getEncryptedUrl())).thenReturn(account1); + Mockito.when(accountRepo.findAccountByEncryptedUrl(any())).thenReturn(account1); Account actualAccount = accountServices.findAccountByEncryptedUrl(account1.getEncryptedUrl()); @@ -87,8 +113,8 @@ public void findAccountByEncryptedUrlTest(){ @Test public void createAccountTest() { Account expectedAccount = account1; - Mockito.when(accountRepo.findAccountByAccountNumber(String.valueOf(Math.floor(Math.random() * 1000000000)))).thenReturn(null); - Mockito.when(accountRepo.save(expectedAccount)).thenReturn(expectedAccount); + Mockito.when(accountRepo.findAccountByAccountNumber(any())).thenReturn(null); + Mockito.when(accountRepo.save(any())).thenReturn(expectedAccount); Account actualAccount = accountServices.createAccount(expectedAccount); @@ -98,8 +124,8 @@ public void createAccountTest() { @Test public void removeAccountTestFalse() { String encryptedUrl = account1.getEncryptedUrl(); - Mockito.when(accountRepo.deleteAccountByEncryptedUrl(encryptedUrl)).thenReturn(account1); - Mockito.when(accountRepo.findAccountByEncryptedUrl(encryptedUrl)).thenReturn(account1); + Mockito.when(accountRepo.deleteAccountByEncryptedUrl(any())).thenReturn(account1); + Mockito.when(accountRepo.findAccountByEncryptedUrl(any())).thenReturn(account1); Boolean deleted = accountServices.removeAccount(encryptedUrl); @@ -109,29 +135,66 @@ public void removeAccountTestFalse() { @Test public void removeAccountTestTrue() { String encryptedUrl = account2.getEncryptedUrl(); - Mockito.when(accountRepo.deleteAccountByEncryptedUrl(encryptedUrl)).thenReturn(account2); - Mockito.when(accountRepo.findAccountByEncryptedUrl(encryptedUrl)).thenReturn(account2); + Mockito.when(accountRepo.deleteAccountByEncryptedUrl(any())).thenReturn(account2); + Mockito.when(accountRepo.findAccountByEncryptedUrl(any())).thenReturn(account2); Boolean deleted = accountServices.removeAccount(encryptedUrl); Assert.assertTrue(deleted); } - @Test void transferMoneyTestTrue(){ + @Test + public void transferMoneyTestTrue() throws Exception { + Account expectedAccount1 = account1; + Account expectedAccount2 = account2; + expectedAccount1.setBalance(account1.getBalance()-transaction.getTransactionAmount()); + expectedAccount2.setBalance(account2.getBalance()+transaction.getTransactionAmount()); + + Mockito.when(transactionServices.setAllTransactions(any(),any(),any())).thenReturn(myTransactionList); + Account[] actualAccounts = accountServices.transferMoney(transaction,account1,account2); + + Assert.assertEquals(actualAccounts[0].getBalance(),expectedAccount1.getBalance()); + Assert.assertEquals(actualAccounts[1].getBalance(),expectedAccount2.getBalance()); + Assert.assertTrue(actualAccounts[0].getTransactions().contains(withdrawalTransaction)); + Assert.assertTrue(actualAccounts[1].getTransactions().contains(depositTransaction)); } - @Test - public void withdrawTest() { + @Test(expected = Exception.class) //account the money is being withdrawn from has insufficient funds + public void transferMoneyTestFalse() throws Exception { + Mockito.when(transactionServices.setAllTransactions(any(),any(),any())).thenReturn(myTransactionList); + Account[] actualAccounts = accountServices.transferMoney(transaction,account2,account1); } + @Test - public void depositTest() { + public void withdrawTest() throws Exception { + String encryptedUrl = account1.getEncryptedUrl(); + Account expectedAccount = account1; + expectedAccount.setBalance(expectedAccount.getBalance()-transaction.getTransactionAmount()); + Mockito.when(accountRepo.findAccountByEncryptedUrl(any())).thenReturn(account3); + Mockito.when(accountRepo.findAccountByAccountNumber(any())).thenReturn(account1); + Mockito.when(transactionServices.setAllTransactions(any(),any(),any())).thenReturn(myTransactionList); + Mockito.when(accountRepo.save(any())).thenReturn(account3, account1); //first and second instance + + Account actualAccount = accountServices.withdraw(transaction,encryptedUrl); + + Assert.assertEquals(expectedAccount.getBalance(),actualAccount.getBalance()); } @Test - public void transferTest() { - } + public void depositTest() throws Exception { + String encryptedUrl = account1.getEncryptedUrl(); + Account expectedAccount = account1; + expectedAccount.setBalance(expectedAccount.getBalance()+transaction.getTransactionAmount()); + Mockito.when(accountRepo.findAccountByEncryptedUrl(any())).thenReturn(account3); + Mockito.when(accountRepo.findAccountByAccountNumber(any())).thenReturn(account1); + Mockito.when(transactionServices.setAllTransactions(any(),any(),any())).thenReturn(myTransactionList); + Mockito.when(accountRepo.save(any())).thenReturn(account3, account1); //first and second instance + + Account actualAccount = accountServices.deposit(transaction,encryptedUrl); + Assert.assertEquals(expectedAccount.getBalance(),actualAccount.getBalance()); + } } diff --git a/src/test/java/runner/services/TransactionServicesTest.java b/src/test/java/runner/services/TransactionServicesTest.java index e5cb89e1a..373cdf73c 100644 --- a/src/test/java/runner/services/TransactionServicesTest.java +++ b/src/test/java/runner/services/TransactionServicesTest.java @@ -40,7 +40,6 @@ public class TransactionServicesTest { TransactionServices transactionServices; Account account1; Account account2; - Set testAccounts; Transaction transaction; Set transactionAccount; @@ -51,16 +50,12 @@ public void setup(){ transaction = new Transaction(1.00,transactionAccount); account1 = new Account(1L,"12345", AccountType.CHECKING,100.00,"abcdefg", new HashSet()); account2 = new Account(2L,"54321", AccountType.SAVINGS,0.00,"gfedcba", new HashSet()); - testAccounts = new HashSet(); - testAccounts.add(account1); - testAccounts.add(account2); } @Test public void setAllTransactionTest(){ - - ArrayList actualTransactionList = transactionServices.setAllTransactions(transaction,account1,account2); + Assert.assertTrue(actualTransactionList.size() == 2); } @Test @@ -77,4 +72,18 @@ public void setOneTransactionTestWithdraw(){ Assert.assertEquals(expected.getTransactionDescription(),actual.getTransactionDescription()); } + @Test + public void setOneTransactionTestDeposit(){ + Transaction expected = new Transaction(String.format("Deposit from %s XXXXXXXX%s",account1.getAccountType(), + account1.getAccountNumber().substring(account1.getAccountNumber().length()-4)), + transaction.getTransactionAmount(), account2.getBalance(), LocalDate.now()); + + Transaction actual = transactionServices.setOneTransaction(transaction,account2,account1,false); + + Assert.assertEquals(expected.getTransactionAmount(),actual.getTransactionAmount()); + Assert.assertEquals(expected.getTransactionBalance(),actual.getTransactionBalance()); + Assert.assertEquals(expected.getTransactionDate(),actual.getTransactionDate()); + Assert.assertEquals(expected.getTransactionDescription(),actual.getTransactionDescription()); + } + } diff --git a/target/test-classes/runner/security/JwtTest.class b/target/test-classes/runner/security/JwtTest.class index 356b71e1a..93726e066 100644 Binary files a/target/test-classes/runner/security/JwtTest.class and b/target/test-classes/runner/security/JwtTest.class differ diff --git a/target/test-classes/runner/services/AccountServiceTest.class b/target/test-classes/runner/services/AccountServiceTest.class index 93595076b..634456e83 100644 Binary files a/target/test-classes/runner/services/AccountServiceTest.class and b/target/test-classes/runner/services/AccountServiceTest.class differ diff --git a/target/test-classes/runner/services/TransactionServicesTest.class b/target/test-classes/runner/services/TransactionServicesTest.class index 6bddb4d11..183c0c738 100644 Binary files a/target/test-classes/runner/services/TransactionServicesTest.class and b/target/test-classes/runner/services/TransactionServicesTest.class differ