From f3a6e5a9b9ffd560084d6d6d75710f1fbe22c317 Mon Sep 17 00:00:00 2001 From: Lucy Ciara Herud-Thomassen <86323303+LucyCiara@users.noreply.github.com> Date: Sun, 15 Mar 2026 12:42:37 +0100 Subject: [PATCH] test[DbWrapper]: add tests for both users and donations --- .../wrapper/DbWrapperDonationsTest.java | 199 ++++++++++++++++++ .../control/wrapper/DbWrapperUserTest.java | 191 +++++++++++++++++ .../app/control/wrapper/WrapperTest.java | 5 - 3 files changed, 390 insertions(+), 5 deletions(-) create mode 100644 src/test/java/edu/group5/app/control/wrapper/DbWrapperDonationsTest.java create mode 100644 src/test/java/edu/group5/app/control/wrapper/DbWrapperUserTest.java delete mode 100644 src/test/java/edu/group5/app/control/wrapper/WrapperTest.java diff --git a/src/test/java/edu/group5/app/control/wrapper/DbWrapperDonationsTest.java b/src/test/java/edu/group5/app/control/wrapper/DbWrapperDonationsTest.java new file mode 100644 index 0000000..f31d57c --- /dev/null +++ b/src/test/java/edu/group5/app/control/wrapper/DbWrapperDonationsTest.java @@ -0,0 +1,199 @@ +package edu.group5.app.control.wrapper; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.sql.Timestamp; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Date; +import java.util.List; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class DbWrapperDonationsTest { + private Object[] johnDonation; + private List users; + private Object[] janeDonation; + private Object[] cutoffDonation; + private Object[] freakyDonation; + private Object[] repeatingDonation; + private List donations; + private List donations2; + private List donations3; + private List repeatedDonations; + private List wrongFormatDonations; + private List wrongDatatypeDonations; + private List nullList; + + private static final int PRECISION = 5; + + private DbWrapper db; + + @BeforeEach + void init() { + this.db = new DbWrapper(true); + String[] firstNames = new String[] { "John", "Jane", "Cutoff", "Freaky", "Repeating" }; + String[] lastNames = new String[] { "Doe", "Doe", "Joh", "Bill", "JoeJoe" }; + this.users = new ArrayList(); + for (int i = 0; i < 5; i++) { + Object[] row = new Object[6]; + row[0] = i + 1; + row[1] = "Customer"; + row[2] = firstNames[i]; + row[3] = lastNames[i]; + row[4] = firstNames[i] + lastNames[i] + "@email.com"; + row[5] = "password"; + users.add(row); + } + + this.johnDonation = new Object[] { 1, 1, 39, new BigDecimal(20.02), new Timestamp(new Date().getTime()), + "Paypal" }; + this.donations = new ArrayList(); + this.donations.add(this.johnDonation); + + this.janeDonation = new Object[] { 2, 2, 39, new BigDecimal(20.00), new Timestamp(new Date().getTime()), + "Visa debit card" }; + this.donations2 = new ArrayList(); + this.donations2.add(this.johnDonation); + this.donations2.add(this.janeDonation); + + this.donations3 = new ArrayList(); + this.donations3.add(this.janeDonation); + + this.repeatingDonation = new Object[] { 3, 3, 333, new BigDecimal(18181818.18), + new Timestamp(new Date().getTime()), "Klarna installations payment" }; + this.repeatedDonations = new ArrayList(); + this.repeatedDonations.add(this.repeatingDonation); + this.repeatedDonations.add(this.repeatingDonation); + + this.cutoffDonation = new Object[] { 4, 4, 21 }; + this.wrongFormatDonations = new ArrayList(); + this.wrongFormatDonations.add(cutoffDonation); + + this.freakyDonation = new Object[] { 5, 5, "Freaks4Education", "lots", false, new String[6] }; + this.wrongDatatypeDonations = new ArrayList(); + this.wrongDatatypeDonations.add(freakyDonation); + + Object[] nullRow = new Object[] {null, null, null, null, null, null}; + this.nullList = new ArrayList(); + this.nullList.add(nullRow); + + this.db.connect(); + this.db.exportUsers(users); + } + + private static boolean donationEquals(Object[] array1, Object[] array2) { + Object[] tempArray1 = array1.clone(); + tempArray1[3] = ((BigDecimal) tempArray1[3]).setScale(2, RoundingMode.HALF_UP); + Object[] tempArray2 = array2.clone(); + tempArray2[3] = ((BigDecimal) tempArray2[3]).setScale(2, RoundingMode.HALF_UP); + return Arrays.equals(tempArray1, tempArray2); + } + + @Test + public void importDonationsIsOnlyExportDonationsTest() { + assertDoesNotThrow(() -> { + assertTrue(this.db.importDonations((int) this.users.get(0)[0]).size() == 0); + assertTrue(this.db.exportDonations(this.donations) == 1); + assertTrue(this.db.importDonations((int) this.users.get(0)[0]).size() == 1); + assertTrue(donationEquals(this.donations.get(0), this.db.importDonations((int) this.users.get(0)[0]).get(0))); + assertTrue(this.db.disconnect()); + }); + } + + @Test + public void nullDataInExportDonationsThrowsExpectedException() { + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> { + this.db.exportDonations(null); + }); + assertTrue(this.db.disconnect()); + assertEquals("data can't be null", exception.getMessage()); + } + + @Test + public void wronglyFormattedDonationsThrowsExpectedException() { + assertTrue(this.db.importDonations((int) this.users.get(2)[0]).size() == 0); + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> { + this.db.exportDonations(this.wrongFormatDonations); + }); + assertTrue(this.db.importDonations((int) this.users.get(2)[0]).size() == 0); + assertTrue(this.db.disconnect()); + assertEquals("data's arrays must have a length of 6", exception.getMessage()); + } + + @Test + public void wronglyDatatypedDonationsThrowsExpectedException() { + assertTrue(this.db.importDonations((int) this.users.get(3)[0]).size() == 0); + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> { + this.db.exportDonations(this.wrongDatatypeDonations); + }); + assertTrue(this.db.importDonations((int) this.users.get(3)[0]).size() == 0); + assertTrue(this.db.disconnect()); + assertEquals("One or more rows in data contains a wrong datatype", exception.getMessage()); + } + + @Test + public void addingSameDonationTwiceThrowsExpectedException() { + assertTrue(this.db.importDonations((int) this.users.get(0)[0]).size() == 0); + assertEquals(1, this.db.exportDonations(this.donations)); + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> { + this.db.exportDonations(this.donations); + }); + assertTrue(this.db.importDonations((int) this.users.get(0)[0]).size() == 1); + assertTrue(this.db.disconnect()); + assertEquals("data can't contain existing rows", exception.getMessage()); + } + + @Test + public void addingSameDonationTwiceThrowsExpectedException2() { + assertTrue(this.db.importDonations((int) this.users.get(0)[0]).size() == 0); + assertTrue(this.db.importDonations((int) this.users.get(1)[0]).size() == 0); + assertEquals(2, this.db.exportDonations(this.donations2)); + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> { + this.db.exportDonations(this.donations); + }); + assertTrue(this.db.importDonations((int) this.users.get(0)[0]).size() == 1); + assertTrue(this.db.importDonations((int) this.users.get(1)[0]).size() == 1); + assertTrue(this.db.disconnect()); + assertEquals("data can't contain existing rows", exception.getMessage()); + } + + @Test + public void addingDifferentDonationsThrowsNoException() { + assertDoesNotThrow(() -> { + assertTrue(this.db.importDonations((int) this.users.get(0)[0]).size() == 0); + assertTrue(this.db.importDonations((int) this.users.get(1)[0]).size() == 0); + assertEquals(1, this.db.exportDonations(this.donations)); + assertEquals(1, this.db.exportDonations(this.donations3)); + assertTrue(this.db.importDonations((int) this.users.get(0)[0]).size() == 1); + assertTrue(this.db.importDonations((int) this.users.get(1)[0]).size() == 1); + assertTrue(this.db.disconnect()); + }); + } + + @Test + public void addingDonationListWithDuplicateIdsThrowsExpectedException() { + assertTrue(this.db.importDonations((int) this.users.get(4)[0]).size() == 0); + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> { + this.db.exportDonations(this.repeatedDonations); + }); + assertTrue(this.db.importDonations((int) this.users.get(4)[0]).size() == 0); + assertTrue(this.db.disconnect()); + assertEquals("data can't contain duplicate rows", exception.getMessage()); + } + + @Test + public void addingDonationListWithNullInRowThrowsExpectedException() { + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> { + this.db.exportDonations(this.nullList); + }); + assertTrue(this.db.disconnect()); + assertEquals("One or more rows in data contains null values", exception.getMessage()); + } +} diff --git a/src/test/java/edu/group5/app/control/wrapper/DbWrapperUserTest.java b/src/test/java/edu/group5/app/control/wrapper/DbWrapperUserTest.java new file mode 100644 index 0000000..10c514f --- /dev/null +++ b/src/test/java/edu/group5/app/control/wrapper/DbWrapperUserTest.java @@ -0,0 +1,191 @@ +package edu.group5.app.control.wrapper; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.math.BigDecimal; +import java.util.Date; +import java.sql.SQLException; +import java.sql.Time; +import java.sql.Timestamp; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; + +import javafx.util.converter.BigDecimalStringConverter; + +public class DbWrapperUserTest { + private Object[] johnDoe; + private Object[] janeDoe; + private Object[] cutoffJoh; + private Object[] freakyBill; + private Object[] repeatingJoeJoe; + private List users; + private List users2; + private List users3; + private List repeatedUsers; + private List wrongFormatUsers; + private List wrongDatatypeUsers; + private List nullList; + + private DbWrapper db; + + @BeforeEach + void init() { + this.db = new DbWrapper(true); + this.johnDoe = new Object[] { 1, "Customer", "John", "Doe", "johndoe@email.com", "password" }; + this.users = new ArrayList(); + this.users.add(this.johnDoe); + + this.janeDoe = new Object[] { 2, "Customer", "Jane", "Doe", "janedoe@email.com", "qwerty" }; + this.users2 = new ArrayList(); + this.users2.add(this.johnDoe); + this.users2.add(this.janeDoe); + + this.users3 = new ArrayList(); + this.users3.add(this.janeDoe); + + this.repeatingJoeJoe = new Object[] { 3, "Customer", "Repeating", "JoeJoe", "repeatingjjoe@email.com", + "passwordpassword" }; + this.repeatedUsers = new ArrayList(); + this.repeatedUsers.add(this.repeatingJoeJoe); + this.repeatedUsers.add(this.repeatingJoeJoe); + + this.cutoffJoh = new Object[] { 4, "Customer", "Cutoff", "Joh" }; + this.wrongFormatUsers = new ArrayList(); + this.wrongFormatUsers.add(cutoffJoh); + + this.freakyBill = new Object[] { 5, "Customer", 6.1805011125, "Bill", true, false }; + this.wrongDatatypeUsers = new ArrayList(); + this.wrongDatatypeUsers.add(freakyBill); + + Object[] nullRow = new Object[] {null, null, null, null, null, null}; + this.nullList = new ArrayList(); + this.nullList.add(nullRow); + } + + @Test + public void nonTestDbWrapperThrowsNoException() { + assertDoesNotThrow(() -> new DbWrapper(false)); + } + + @Test + public void importUsersIsOnlyExportUsersTest() { + assertDoesNotThrow(() -> { + assertTrue(this.db.connect()); + assertTrue(this.db.importUsers().size() == 0); + assertTrue(this.db.exportUsers(this.users) == 1); + assertTrue(this.db.importUsers().size() == 1); + assertTrue(Arrays.equals(this.db.importUsers().get(0), this.johnDoe)); + assertTrue(this.db.disconnect()); + }); + } + + @Test + public void nullDataInExportUsersThrowsExpectedException() { + assertTrue(this.db.connect()); + assertTrue(this.db.importUsers().size() == 0); + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> { + this.db.exportUsers(null); + }); + assertTrue(this.db.importUsers().size() == 0); + assertTrue(this.db.disconnect()); + assertEquals("data can't be null", exception.getMessage()); + } + + @Test + public void wronglyFormattedUsersThrowsExpectedException() { + assertTrue(this.db.connect()); + assertTrue(this.db.importUsers().size() == 0); + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> { + this.db.exportUsers(this.wrongFormatUsers); + }); + assertTrue(this.db.importUsers().size() == 0); + assertTrue(this.db.disconnect()); + assertEquals("data's arrays must have a length of 6", exception.getMessage()); + } + + @Test + public void wronglyDatatypedUsersThrowsExpectedException() { + assertTrue(this.db.connect()); + assertTrue(this.db.importUsers().size() == 0); + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> { + this.db.exportUsers(this.wrongDatatypeUsers); + }); + assertTrue(this.db.importUsers().size() == 0); + assertTrue(this.db.disconnect()); + assertEquals("One or more rows in data contains a wrong datatype", exception.getMessage()); + } + + @Test + public void addingSameUserTwiceThrowsExpectedException() { + assertTrue(this.db.connect()); + assertTrue(this.db.importUsers().size() == 0); + assertEquals(1, this.db.exportUsers(this.users)); + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> { + this.db.exportUsers(this.users); + }); + assertTrue(this.db.importUsers().size() == 1); + assertTrue(this.db.disconnect()); + assertEquals("data can't contain existing rows", exception.getMessage()); + } + + @Test + public void addingSameUserTwiceThrowsExpectedException2() { + assertTrue(this.db.connect()); + assertTrue(this.db.importUsers().size() == 0); + assertEquals(2, this.db.exportUsers(this.users2)); + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> { + this.db.exportUsers(this.users); + }); + assertTrue(this.db.importUsers().size() == 2); + assertTrue(this.db.disconnect()); + assertEquals("data can't contain existing rows", exception.getMessage()); + } + + @Test + public void addingDifferentUsersThrowsNoException() { + assertDoesNotThrow(() -> { + assertTrue(this.db.connect()); + assertTrue(this.db.importUsers().size() == 0); + assertEquals(1, this.db.exportUsers(this.users)); + assertEquals(1, this.db.exportUsers(this.users3)); + assertTrue(this.db.importUsers().size() == 2); + assertTrue(this.db.disconnect()); + }); + } + + @Test + public void addingUserListWithDuplicateIdsThrowsExpectedException() { + assertTrue(this.db.connect()); + assertTrue(this.db.importUsers().size() == 0); + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> { + this.db.exportUsers(this.repeatedUsers); + }); + assertTrue(this.db.importUsers().size() == 0); + assertTrue(this.db.disconnect()); + assertEquals("data can't contain duplicate rows", exception.getMessage()); + } + + @Test + public void addingUserListWithNullInRowThrowsExpectedException() { + assertTrue(this.db.connect()); + assertTrue(this.db.importUsers().size() == 0); + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> { + this.db.exportUsers(this.nullList); + }); + assertTrue(this.db.importUsers().size() == 0); + assertTrue(this.db.disconnect()); + assertEquals("One or more rows in data contains null values", exception.getMessage()); + } + + +} diff --git a/src/test/java/edu/group5/app/control/wrapper/WrapperTest.java b/src/test/java/edu/group5/app/control/wrapper/WrapperTest.java deleted file mode 100644 index cfec876..0000000 --- a/src/test/java/edu/group5/app/control/wrapper/WrapperTest.java +++ /dev/null @@ -1,5 +0,0 @@ -package edu.group5.app.control.wrapper; - -public class WrapperTest { - -}