496 lines
17 KiB
Java
496 lines
17 KiB
Java
import bank.*;
|
|
import bank.exceptions.*;
|
|
import org.junit.jupiter.api.*;
|
|
import org.junit.jupiter.params.ParameterizedTest;
|
|
import org.junit.jupiter.params.provider.CsvSource;
|
|
import org.junit.jupiter.params.provider.ValueSource;
|
|
|
|
import java.io.File;
|
|
import java.util.List;
|
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
|
|
@DisplayName("Test methods for PrivateBank")
|
|
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
|
|
|
public class PrivateBankTest {
|
|
|
|
static PrivateBank privateBank;
|
|
static PrivateBank copyPrivateBank;
|
|
|
|
@DisplayName("Set up a PrivateBank")
|
|
@BeforeAll
|
|
public static void init() throws AccountAlreadyExistsException, TransactionAlreadyExistException,
|
|
AccountDoesNotExistException, TransactionAttributeException {
|
|
|
|
String path = "accountdata";
|
|
|
|
final File folder = new File(path);
|
|
if (folder.exists()) {
|
|
final File[] listOfFiles = folder.listFiles();
|
|
assert listOfFiles != null;
|
|
for (File file : listOfFiles)
|
|
if (file.delete()) System.out.println(file.getName() + " deleted");
|
|
}
|
|
|
|
privateBank = new PrivateBank(
|
|
"Testbank",
|
|
0,
|
|
0,
|
|
path
|
|
);
|
|
|
|
privateBank.createAccount("Konto_1");
|
|
privateBank.createAccount("Konto_2");
|
|
privateBank.addTransaction(
|
|
"Konto_1",
|
|
new Payment(
|
|
"01.01.2000",
|
|
-100,
|
|
"Payment",
|
|
0.9,
|
|
0.25
|
|
)
|
|
);
|
|
privateBank.addTransaction(
|
|
"Konto_2",
|
|
new IncomingTransfer(
|
|
"01.01.2000",
|
|
100,
|
|
"1 -> 2",
|
|
"Konto_1",
|
|
"Konto_2"
|
|
)
|
|
);
|
|
copyPrivateBank = new PrivateBank(privateBank);
|
|
}
|
|
|
|
|
|
@DisplayName("Testing constructor")
|
|
@Test
|
|
@Order(0)
|
|
public void constructorTest() {
|
|
assertAll("PrivateBank",
|
|
() -> assertEquals("Testbank", privateBank.getName()),
|
|
() -> assertEquals("accountdata", privateBank.getDirectoryName()),
|
|
() -> assertEquals(0, privateBank.getIncomingInterest()),
|
|
() -> assertEquals(0, privateBank.getOutgoingInterest()));
|
|
}
|
|
|
|
@DisplayName("Testing copy constructor")
|
|
@Test
|
|
@Order(1)
|
|
public void copyConstructorTest() {
|
|
assertAll("CopyPrivateBank",
|
|
() -> assertEquals(privateBank.getName(), copyPrivateBank.getName()),
|
|
() -> assertEquals(privateBank.getDirectoryName(), copyPrivateBank.getDirectoryName()),
|
|
() -> assertEquals(privateBank.getIncomingInterest(), copyPrivateBank.getIncomingInterest()),
|
|
() -> assertEquals(privateBank.getOutgoingInterest(), copyPrivateBank.getOutgoingInterest()));
|
|
}
|
|
|
|
@DisplayName("Create a duplicate account")
|
|
@ParameterizedTest
|
|
@Order(2)
|
|
@ValueSource(strings = {"Konto_1", "Konto_2"})
|
|
public void createDuplicateAccountTest(String account) {
|
|
Exception e = assertThrows(AccountAlreadyExistsException.class,
|
|
() -> privateBank.createAccount(account));
|
|
System.out.println(e.getMessage());
|
|
}
|
|
|
|
@DisplayName("Create a valid account")
|
|
@ParameterizedTest
|
|
@Order(3)
|
|
@ValueSource(strings = {"Konto_3", "Konto_4", "Konto_5"})
|
|
public void createValidAccountTest(String account) {
|
|
assertDoesNotThrow(
|
|
() -> privateBank.createAccount(account)
|
|
);
|
|
}
|
|
|
|
@DisplayName("Create a valid account with a transactions list")
|
|
@ParameterizedTest
|
|
@Order(4)
|
|
@ValueSource(strings = {"Konto_6", "Konto_7", "Konto_8"})
|
|
public void createValidAccountWithTransactionsListTest(String account) {
|
|
assertDoesNotThrow(
|
|
() -> privateBank.createAccount(
|
|
account,
|
|
List.of(
|
|
new Payment(
|
|
"01.01.2000",
|
|
-100,
|
|
"Payment 02",
|
|
0.8,
|
|
0.5
|
|
),
|
|
new IncomingTransfer(
|
|
"01.01.2000",
|
|
100,
|
|
"IncomingTransfer from Hans",
|
|
"Hans",
|
|
account
|
|
),
|
|
new OutgoingTransfer(
|
|
"01.01.2000",
|
|
100,
|
|
"OutgoingTransfer to Hans",
|
|
account,
|
|
"Hans"
|
|
)
|
|
)
|
|
)
|
|
);
|
|
}
|
|
|
|
@DisplayName("Create a duplicate account with a transactions list")
|
|
@ParameterizedTest
|
|
@Order(5)
|
|
@ValueSource(strings = {"Konto_1", "Konto_2", "Konto_3", "Konto_4", "Konto_5", "Konto_6", "Konto_7", "Konto_8"})
|
|
public void createInvalidAccountWithTransactionsListTest(String account) {
|
|
Exception e = assertThrows(AccountAlreadyExistsException.class,
|
|
() -> privateBank.createAccount(
|
|
account,
|
|
List.of(
|
|
new Payment(
|
|
"01.01.2000",
|
|
0,
|
|
"P",
|
|
0,
|
|
0
|
|
)
|
|
)
|
|
)
|
|
);
|
|
System.out.println(e.getMessage());
|
|
}
|
|
|
|
@DisplayName("Add a valid transaction to a valid account")
|
|
@ParameterizedTest
|
|
@Order(6)
|
|
@ValueSource(strings = {"Konto_1", "Konto_2", "Konto_3", "Konto_4", "Konto_5", "Konto_6", "Konto_7", "Konto_8"})
|
|
public void addValidTransactionValidAccountTest(String account) {
|
|
assertDoesNotThrow(
|
|
() -> privateBank.addTransaction(
|
|
account,
|
|
new IncomingTransfer(
|
|
"01.01.2000",
|
|
300,
|
|
"IncomingTransfer from Tom",
|
|
"Tom",
|
|
account
|
|
)
|
|
)
|
|
);
|
|
}
|
|
|
|
@DisplayName("Add a duplicate transaction to a valid account")
|
|
@ParameterizedTest
|
|
@Order(7)
|
|
@ValueSource(strings = {"Konto_6", "Konto_7", "Konto_8"})
|
|
public void addDuplicateTransactionTest(String account) {
|
|
Exception e = assertThrows(TransactionAlreadyExistException.class,
|
|
() -> privateBank.addTransaction(
|
|
account,
|
|
new Payment(
|
|
"01.01.2000",
|
|
-100,
|
|
"Payment 02",
|
|
privateBank.getIncomingInterest(),
|
|
privateBank.getOutgoingInterest()
|
|
)
|
|
)
|
|
);
|
|
System.out.println(e.getMessage());
|
|
}
|
|
|
|
@DisplayName("Add a valid transaction to an invalid account")
|
|
@ParameterizedTest
|
|
@Order(8)
|
|
@ValueSource(strings = {"Konto_X", "Konto_Y", "Konto_Z"})
|
|
public void addTransactionInvalidAccountTest(String account) {
|
|
Exception e = assertThrows(AccountDoesNotExistException.class,
|
|
() -> privateBank.addTransaction(
|
|
account,
|
|
new Payment(
|
|
"01.01.2001",
|
|
-100,
|
|
"Payment",
|
|
0.9,
|
|
0.25
|
|
)
|
|
)
|
|
);
|
|
System.out.println(e.getMessage());
|
|
}
|
|
|
|
@DisplayName("Remove a valid transaction")
|
|
@ParameterizedTest
|
|
@Order(9)
|
|
@ValueSource(strings = {"Konto_6", "Konto_7", "Konto_8"})
|
|
public void removeValidTransactionTest(String account) {
|
|
assertDoesNotThrow(
|
|
() -> privateBank.removeTransaction(
|
|
account,
|
|
new Payment(
|
|
"01.01.2000",
|
|
-100,
|
|
"Payment 02",
|
|
privateBank.getIncomingInterest(),
|
|
privateBank.getOutgoingInterest()
|
|
)
|
|
)
|
|
);
|
|
}
|
|
|
|
@DisplayName("Remove an invalid transaction")
|
|
@ParameterizedTest
|
|
@Order(10)
|
|
@ValueSource(strings = {"Konto_6", "Konto_7", "Konto_8"})
|
|
public void removeInvalidTransactionTest(String account) {
|
|
Exception e = assertThrows(
|
|
TransactionDoesNotExistException.class,
|
|
() -> privateBank.removeTransaction(
|
|
account,
|
|
new Payment(
|
|
"01.01.2002",
|
|
-100,
|
|
"Payment",
|
|
0.9,
|
|
0.25
|
|
)
|
|
)
|
|
);
|
|
System.out.println(e.getMessage());
|
|
}
|
|
|
|
@DisplayName("Contains a transaction is true")
|
|
@ParameterizedTest
|
|
@Order(11)
|
|
@ValueSource(strings = {"Konto_1", "Konto_2", "Konto_3", "Konto_4", "Konto_5", "Konto_6", "Konto_7", "Konto_8"})
|
|
public void containsTransactionTrueTest(String account) {
|
|
assertTrue(
|
|
privateBank.containsTransaction(
|
|
account,
|
|
new IncomingTransfer(
|
|
"01.01.2000",
|
|
300,
|
|
"IncomingTransfer from Tom",
|
|
"Tom",
|
|
account
|
|
)
|
|
)
|
|
);
|
|
System.out.println("containsTransactionTrueTest in <" + account + "> is correct.");
|
|
}
|
|
|
|
@DisplayName("Contains a transaction is false")
|
|
@ParameterizedTest
|
|
@Order(12)
|
|
@ValueSource(strings = {"Konto_1", "Konto_2", "Konto_3", "Konto_4", "Konto_5", "Konto_6", "Konto_7", "Konto_8"})
|
|
public void containsTransactionFalseTest(String account) {
|
|
assertFalse(
|
|
privateBank.containsTransaction(
|
|
account,
|
|
new IncomingTransfer(
|
|
"01.01.2020",
|
|
0,
|
|
"IncomingTransfer from Tom",
|
|
"Tom",
|
|
account
|
|
)
|
|
)
|
|
);
|
|
System.out.println("containsTransactionFalseTest in <" + account + "> is correct.");
|
|
}
|
|
|
|
@DisplayName("Get account balance")
|
|
@ParameterizedTest
|
|
@Order(14)
|
|
@CsvSource({"Konto_1, 200", "Konto_4, 300", "Konto_6, 300"})
|
|
public void getAccountBalanceTest(String account, double balance) {
|
|
System.out.println(
|
|
"Expected <" + balance + "> in account <" + account + ">"
|
|
);
|
|
assertEquals(
|
|
balance,
|
|
privateBank.getAccountBalance(account)
|
|
);
|
|
}
|
|
|
|
@DisplayName("Get transactions list")
|
|
@Test
|
|
@Order(15)
|
|
public void getTransactionTest() {
|
|
List<Transaction> transactionList = List.of(
|
|
new IncomingTransfer(
|
|
"01.01.2000",
|
|
100,
|
|
"IncomingTransfer from Hans",
|
|
"Hans",
|
|
"Konto_6"
|
|
),
|
|
new OutgoingTransfer(
|
|
"01.01.2000",
|
|
100,
|
|
"OutgoingTransfer to Hans",
|
|
"Konto_6",
|
|
"Hans"
|
|
),
|
|
new IncomingTransfer(
|
|
"01.01.2000",
|
|
300,
|
|
"IncomingTransfer from Tom",
|
|
"Tom",
|
|
"Konto_6"
|
|
)
|
|
);
|
|
assertEquals(
|
|
transactionList,
|
|
privateBank.getTransactions("Konto_6")
|
|
);
|
|
System.out.println("getTransactionTest in <Konto_6> is correct.");
|
|
}
|
|
|
|
@DisplayName("Get transactions list by type")
|
|
@Test
|
|
@Order(16)
|
|
public void getTransactionsByTypeTest() {
|
|
List<Transaction> transactionList = List.of(
|
|
new IncomingTransfer(
|
|
"01.01.2000",
|
|
300,
|
|
"IncomingTransfer from Tom",
|
|
"Tom",
|
|
"Konto_1"
|
|
)
|
|
);
|
|
assertEquals(
|
|
transactionList,
|
|
privateBank.getTransactionsByType(
|
|
"Konto_1",
|
|
false
|
|
)
|
|
);
|
|
System.out.println("getTransactionByTypeTest in <Konto_1> is correct.");
|
|
}
|
|
|
|
@Test
|
|
@Order(17)
|
|
@DisplayName("Get sorted transactions list")
|
|
public void getTransactionsSortedTest() {
|
|
assertEquals(
|
|
List.of(
|
|
new OutgoingTransfer(
|
|
"01.01.2000",
|
|
100,
|
|
"OutgoingTransfer to Hans",
|
|
"Konto_6",
|
|
"Hans"
|
|
),
|
|
new IncomingTransfer(
|
|
"01.01.2000",
|
|
100,
|
|
"IncomingTransfer from Hans",
|
|
"Hans",
|
|
"Konto_6"
|
|
),
|
|
new IncomingTransfer(
|
|
"01.01.2000",
|
|
300,
|
|
"IncomingTransfer from Tom",
|
|
"Tom",
|
|
"Konto_6"
|
|
)
|
|
),
|
|
privateBank.getTransactionsSorted(
|
|
"Konto_6",
|
|
true
|
|
)
|
|
);
|
|
}
|
|
|
|
@DisplayName("Serialize Accounts")
|
|
@Order(18)
|
|
@ParameterizedTest
|
|
@ValueSource(strings = {"Konto_1", "Konto_2", "Konto_3", "Konto_4", "Konto_5", "Konto_6", "Konto_7", "Konto_8"})
|
|
public void serializeAccountsTest(String account) {
|
|
assertDoesNotThrow(
|
|
() -> privateBank.writeAccount(account)
|
|
);
|
|
System.out.println("SerializeAccountsTest in <" + account + "> is correct.");
|
|
}
|
|
|
|
@DisplayName("Deserialize Accounts")
|
|
@Order(19)
|
|
@Test
|
|
public void DeserializeAccountsTest() {
|
|
assertThrows(
|
|
AccountAlreadyExistsException.class, () -> privateBank.readAccounts()
|
|
);
|
|
}
|
|
|
|
|
|
@DisplayName("Delete existing account")
|
|
@Order(20)
|
|
@Test
|
|
public void testDeleteExistingAccount() {
|
|
assertDoesNotThrow(
|
|
() -> privateBank.createAccount(
|
|
"Konto_X",
|
|
List.of(
|
|
new OutgoingTransfer(
|
|
"01.01.2000",
|
|
100,
|
|
"OutgoingTransfer to Hans",
|
|
"Konto_X",
|
|
"Hans"
|
|
),
|
|
new IncomingTransfer(
|
|
"01.01.2000",
|
|
100,
|
|
"IncomingTransfer from Hans",
|
|
"Hans",
|
|
"Konto_X"
|
|
),
|
|
new IncomingTransfer(
|
|
"01.01.2000",
|
|
300,
|
|
"IncomingTransfer from Tom",
|
|
"Tom",
|
|
"Konto_X"
|
|
)
|
|
|
|
)
|
|
)
|
|
);
|
|
|
|
assertDoesNotThrow(
|
|
() -> privateBank.writeAccount("Konto_X")
|
|
);
|
|
|
|
assertTrue(
|
|
() -> privateBank.containsAccount("Konto_X")
|
|
);
|
|
|
|
assertDoesNotThrow(
|
|
() -> privateBank.deleteAccount("Konto_X")
|
|
);
|
|
|
|
assertFalse(
|
|
() -> privateBank.containsAccount("Konto_X")
|
|
);
|
|
|
|
}
|
|
|
|
@DisplayName("Delete non existing account")
|
|
@Order(21)
|
|
@Test
|
|
public void testDeleteNonExistingAccount() {
|
|
assertThrows(
|
|
AccountDoesNotExistException.class, () -> privateBank.deleteAccount("Konto_X")
|
|
);
|
|
}
|
|
|
|
}
|