|
2 | 2 |
|
3 | 3 | import static org.assertj.core.api.Assertions.assertThat;
|
4 | 4 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
| 5 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 7 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
5 | 8 |
|
6 | 9 | import com.codedifferently.lesson17.bank.exceptions.AccountNotFoundException;
|
7 | 10 | import com.codedifferently.lesson17.bank.exceptions.CheckVoidedException;
|
| 11 | +import java.util.HashSet; |
8 | 12 | import java.util.Set;
|
9 | 13 | import java.util.UUID;
|
10 | 14 | import org.junit.jupiter.api.BeforeEach;
|
@@ -107,4 +111,95 @@ void testWithdrawFunds_AccountNotFound() {
|
107 | 111 | .isThrownBy(() -> classUnderTest.withdrawFunds(nonExistingAccountNumber, 50.0))
|
108 | 112 | .withMessage("Account not found");
|
109 | 113 | }
|
| 114 | + |
| 115 | + @Test |
| 116 | + public void testDepositFundsIntoSavingsAccount() { |
| 117 | + BankAtm atm = new BankAtm(); |
| 118 | + SavingsAccount savingsAccount = new SavingsAccount("12345", 1000.00, Set.of(customer1)); |
| 119 | + atm.addAccount(savingsAccount); |
| 120 | + |
| 121 | + // Valid deposit |
| 122 | + atm.depositFunds("12345", 500.00); |
| 123 | + assertEquals(1500.00, savingsAccount.getBalance(), 0.01); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void testDepositCheckIntoSavingsAccount() { |
| 128 | + // Arrange |
| 129 | + BankAtm atm = new BankAtm(); |
| 130 | + Set<Customer> owners = new HashSet<>(); |
| 131 | + owners.add(new Customer(UUID.randomUUID(), "Customer1")); |
| 132 | + SavingsAccount savingsAccount = new SavingsAccount("12345", 1000.00, Set.of(customer1)); |
| 133 | + |
| 134 | + atm.addAccount(savingsAccount); // Add savings account to ATM |
| 135 | + |
| 136 | + // Act & Assert: Check that the exception is thrown when attempting to deposit a check into a |
| 137 | + // savings account |
| 138 | + Check check = new Check("234", 100.00, savingsAccount); // Creating a check to deposit |
| 139 | + |
| 140 | + // Assert that an UnsupportedOperationException is thrown |
| 141 | + UnsupportedOperationException exception = |
| 142 | + assertThrows( |
| 143 | + UnsupportedOperationException.class, |
| 144 | + () -> { |
| 145 | + atm.depositFunds("12345", check); // Attempt to deposit check |
| 146 | + }); |
| 147 | + |
| 148 | + // Optionally, you can also assert the message of the exception if you want more specific |
| 149 | + // validation |
| 150 | + assertEquals("Cannot deposit checks into a Savings Account.", exception.getMessage()); |
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + public void testAddBusinessCheckingAccountWithBusinessOwner() { |
| 155 | + // Arrange |
| 156 | + BankAtm atm = new BankAtm(); |
| 157 | + |
| 158 | + // Create business and personal customers |
| 159 | + BusinessCustomer businessCustomer = new BusinessCustomer(UUID.randomUUID(), "Business1"); |
| 160 | + Customer personalCustomer = new Customer(UUID.randomUUID(), "Personal1"); |
| 161 | + |
| 162 | + // Create a set of owners, including a business customer |
| 163 | + Set<Customer> owners = new HashSet<>(); |
| 164 | + owners.add(businessCustomer); |
| 165 | + owners.add(personalCustomer); |
| 166 | + |
| 167 | + // Create a BusinessCheckingAccount with the above owners |
| 168 | + BusinessCheckingAccount businessAccount = new BusinessCheckingAccount("67890", 1000.00, owners); |
| 169 | + |
| 170 | + // Act: Add the BusinessCheckingAccount to the ATM |
| 171 | + atm.addAccount(businessAccount); |
| 172 | + |
| 173 | + // Assert: Ensure no exceptions were thrown and the account was added |
| 174 | + assertNotNull(atm.accountByNumber.get("67890")); |
| 175 | + } |
| 176 | + |
| 177 | + @Test |
| 178 | + public void testAddBusinessCheckingAccountWithoutBusinessOwner() { |
| 179 | + // Arrange |
| 180 | + BankAtm atm = new BankAtm(); |
| 181 | + |
| 182 | + // Create only personal customers (no business owner) |
| 183 | + Customer personalCustomer1 = new Customer(UUID.randomUUID(), "Personal1"); |
| 184 | + Customer personalCustomer2 = new Customer(UUID.randomUUID(), "Personal2"); |
| 185 | + |
| 186 | + // Create a set of owners with only personal customers |
| 187 | + Set<Customer> owners = new HashSet<>(); |
| 188 | + owners.add(personalCustomer1); |
| 189 | + owners.add(personalCustomer2); |
| 190 | + |
| 191 | + // Act & Assert: Try to add a BusinessCheckingAccount and expect an exception |
| 192 | + IllegalArgumentException exception = |
| 193 | + assertThrows( |
| 194 | + IllegalArgumentException.class, |
| 195 | + () -> { |
| 196 | + BusinessCheckingAccount businessAccount = |
| 197 | + new BusinessCheckingAccount("67890", 1000.00, owners); |
| 198 | + atm.addAccount(businessAccount); |
| 199 | + }); |
| 200 | + |
| 201 | + // Assert: Ensure the correct exception is thrown |
| 202 | + assertEquals( |
| 203 | + "A BusinessCheckingAccount must have at least one business owner.", exception.getMessage()); |
| 204 | + } |
110 | 205 | }
|
0 commit comments