Skip to content

Commit

Permalink
test[DbWrapper]: add tests for both users and donations
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucy Ciara Herud-Thomassen authored and Lucy Ciara Herud-Thomassen committed Mar 15, 2026
1 parent 24229ee commit f3a6e5a
Show file tree
Hide file tree
Showing 3 changed files with 390 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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<Object[]> users;
private Object[] janeDonation;
private Object[] cutoffDonation;
private Object[] freakyDonation;
private Object[] repeatingDonation;
private List<Object[]> donations;
private List<Object[]> donations2;
private List<Object[]> donations3;
private List<Object[]> repeatedDonations;
private List<Object[]> wrongFormatDonations;
private List<Object[]> wrongDatatypeDonations;
private List<Object[]> 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<Object[]>();
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<Object[]>();
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<Object[]>();
this.donations2.add(this.johnDonation);
this.donations2.add(this.janeDonation);

this.donations3 = new ArrayList<Object[]>();
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<Object[]>();
this.repeatedDonations.add(this.repeatingDonation);
this.repeatedDonations.add(this.repeatingDonation);

this.cutoffDonation = new Object[] { 4, 4, 21 };
this.wrongFormatDonations = new ArrayList<Object[]>();
this.wrongFormatDonations.add(cutoffDonation);

this.freakyDonation = new Object[] { 5, 5, "Freaks4Education", "lots", false, new String[6] };
this.wrongDatatypeDonations = new ArrayList<Object[]>();
this.wrongDatatypeDonations.add(freakyDonation);

Object[] nullRow = new Object[] {null, null, null, null, null, null};
this.nullList = new ArrayList<Object[]>();
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());
}
}
191 changes: 191 additions & 0 deletions src/test/java/edu/group5/app/control/wrapper/DbWrapperUserTest.java
Original file line number Diff line number Diff line change
@@ -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<Object[]> users;
private List<Object[]> users2;
private List<Object[]> users3;
private List<Object[]> repeatedUsers;
private List<Object[]> wrongFormatUsers;
private List<Object[]> wrongDatatypeUsers;
private List<Object[]> 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<Object[]>();
this.users.add(this.johnDoe);

this.janeDoe = new Object[] { 2, "Customer", "Jane", "Doe", "janedoe@email.com", "qwerty" };
this.users2 = new ArrayList<Object[]>();
this.users2.add(this.johnDoe);
this.users2.add(this.janeDoe);

this.users3 = new ArrayList<Object[]>();
this.users3.add(this.janeDoe);

this.repeatingJoeJoe = new Object[] { 3, "Customer", "Repeating", "JoeJoe", "repeatingjjoe@email.com",
"passwordpassword" };
this.repeatedUsers = new ArrayList<Object[]>();
this.repeatedUsers.add(this.repeatingJoeJoe);
this.repeatedUsers.add(this.repeatingJoeJoe);

this.cutoffJoh = new Object[] { 4, "Customer", "Cutoff", "Joh" };
this.wrongFormatUsers = new ArrayList<Object[]>();
this.wrongFormatUsers.add(cutoffJoh);

this.freakyBill = new Object[] { 5, "Customer", 6.1805011125, "Bill", true, false };
this.wrongDatatypeUsers = new ArrayList<Object[]>();
this.wrongDatatypeUsers.add(freakyBill);

Object[] nullRow = new Object[] {null, null, null, null, null, null};
this.nullList = new ArrayList<Object[]>();
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());
}


}
5 changes: 0 additions & 5 deletions src/test/java/edu/group5/app/control/wrapper/WrapperTest.java

This file was deleted.

0 comments on commit f3a6e5a

Please sign in to comment.