Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/test/java/runner/security/JwtTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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());
Expand Down
103 changes: 83 additions & 20 deletions src/test/java/runner/services/AccountServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand All @@ -37,28 +42,49 @@
public class AccountServiceTest {
@Mock
AccountRepo accountRepo;
@Mock
TransactionServices transactionServices;
@InjectMocks
AccountServices accountServices;
Account account1;
Account account2;
Account account3;
Set<Account> testAccounts;
Login login;
Customer customer;
Set<Account> testAccounts;
Transaction transaction;
Set<Account> transactionAccount;
Transaction withdrawalTransaction;
Transaction depositTransaction;
ArrayList<Transaction> myTransactionList;

@Before
public void setup(){
transactionAccount = new HashSet<Account>();
transactionAccount.add(account1);
transaction = new Transaction(1.00,transactionAccount);
account1 = new Account(1L,"123", AccountType.CHECKING,100.00,"abcdefg", new HashSet<Transaction>());
account2 = new Account(2L,"321", AccountType.SAVINGS,0.00,"gfedcba", new HashSet<Transaction>());
account1 = new Account(1L,"12345", AccountType.CHECKING,100.00,"abcdefg", new HashSet<Transaction>());
account2 = new Account(2L,"54321", AccountType.SAVINGS,0.00,"gfedcba", new HashSet<Transaction>());
account3 = new Account(2L,"56789", AccountType.SAVINGS,100.00,"qwerty", new HashSet<Transaction>());
testAccounts = new HashSet<Account>();
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<Account>();
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<Transaction>();
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
Expand All @@ -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<Account> 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<Account> actualAccounts = accountServices.getAllAccounts(login.getUsername());
//the two sets should be identical
Expand All @@ -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());

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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());
}

}
21 changes: 15 additions & 6 deletions src/test/java/runner/services/TransactionServicesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public class TransactionServicesTest {
TransactionServices transactionServices;
Account account1;
Account account2;
Set<Account> testAccounts;
Transaction transaction;
Set<Account> transactionAccount;

Expand All @@ -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<Transaction>());
account2 = new Account(2L,"54321", AccountType.SAVINGS,0.00,"gfedcba", new HashSet<Transaction>());
testAccounts = new HashSet<Account>();
testAccounts.add(account1);
testAccounts.add(account2);
}

@Test
public void setAllTransactionTest(){


ArrayList<Transaction> actualTransactionList = transactionServices.setAllTransactions(transaction,account1,account2);
Assert.assertTrue(actualTransactionList.size() == 2);
}

@Test
Expand All @@ -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());
}

}
Binary file modified target/test-classes/runner/security/JwtTest.class
Binary file not shown.
Binary file modified target/test-classes/runner/services/AccountServiceTest.class
Binary file not shown.
Binary file modified target/test-classes/runner/services/TransactionServicesTest.class
Binary file not shown.