From 1960fced52e4bde825bc224758ae3ad86afddebd Mon Sep 17 00:00:00 2001 From: Lucy Ciara Herud-Thomassen <86323303+LucyCiara@users.noreply.github.com> Date: Thu, 9 Apr 2026 10:44:59 +0200 Subject: [PATCH 1/8] style[DbWrapper]: add javadocs and split lines --- .../group5/app/model/wrapper/DbWrapper.java | 87 +++++++++++++++++-- 1 file changed, 82 insertions(+), 5 deletions(-) diff --git a/src/main/java/edu/group5/app/model/wrapper/DbWrapper.java b/src/main/java/edu/group5/app/model/wrapper/DbWrapper.java index 073fd81..2357e87 100644 --- a/src/main/java/edu/group5/app/model/wrapper/DbWrapper.java +++ b/src/main/java/edu/group5/app/model/wrapper/DbWrapper.java @@ -15,6 +15,9 @@ import java.util.logging.Level; import java.util.logging.Logger; +/** + * A class for wrapping the database. + */ public class DbWrapper { protected Connection connection; private static final String CONNECTION_TYPE = "jdbc:h2:"; @@ -24,6 +27,11 @@ public class DbWrapper { private List donations; private Logger logger = Logger.getLogger(DbWrapper.class.getName()); + /** + * The constructor, which constructs a String for connecting to the database. + * + * @param test Whether to construct the connection String for testing (in-memory) or not. + */ public DbWrapper(boolean test) { if (test) { this.connectionString = CONNECTION_TYPE + "mem:test;" + DB_SCRIPT + "test_init.sql'"; @@ -33,6 +41,11 @@ public DbWrapper(boolean test) { this.logger.info("connectionString constructed"); } + /** + * Connects to the database, and returns the result, logging failures. + * + * @return True if successful, false if not. + */ public boolean connect() { try { this.connection = DriverManager.getConnection(this.connectionString); @@ -49,7 +62,13 @@ public boolean connect() { } } + /** + * Disconnects the database connection, logging failures. + * + * @return True if successful, false if not. + */ public boolean disconnect() { + // We are not interested in whether it fails to close, as we check its closed status later. try{ this.connection.close(); } catch (Exception e) {}; try { return this.connection.isClosed(); @@ -59,12 +78,25 @@ public boolean disconnect() { } } + /** + * Closes queries and results. + * + * @param results The ResultSet to close, can be null. + * @param ps The PreparedStatement to close, can be null. + */ private void close(ResultSet results, PreparedStatement ps) { + // This method can take null arguments, so an exception is expected. try { results.close(); } catch (Exception e) {} try { ps.close(); } catch (Exception e) {} this.logger.info("results and ps closed"); } + /** + * Gets all users from the database. + * + * @return The users from the database returned as a List of Object arrays, where each Object + * array represents a user and a row in the users table in the database. + */ public List importUsers() { PreparedStatement ps = null; ResultSet results = null; @@ -93,7 +125,17 @@ public List importUsers() { return this.users; } - public int exportUsers(List data) { + /** + * Puts new users into the database. + * + * @param data The new users to put into the database. Each Object array in the List is a new + * user to add as a row. + * @return The number of rows affected in the transaction. + * @throws IllegalArgumentException This exception is thrown when data is null, its rows are not + * of length 6, any of the rows are null, any of the rows are duplicates or existing rows in + * the database, or any of the values in the rows can't be cast to the correct data-types. + */ + public int exportUsers(List data) throws IllegalArgumentException { this.importUsers(); if (data == null) { @@ -102,12 +144,14 @@ public int exportUsers(List data) { if (data.isEmpty()) { return 0; } + // TODO: change to check length for every row. if (data.get(0).length != 6) { throw new IllegalArgumentException("data's arrays must have a length of 6"); } if (data.stream().anyMatch(i -> Arrays.asList(i).contains(null))) { throw new IllegalArgumentException("One or more rows in data contains null values"); } + // TODO: change how existing rows are checked. if (this.users.size() > 0) { if ((int) data.getLast()[0] <= (int) this.users.getLast()[0]) { throw new IllegalArgumentException("data can't contain existing rows"); @@ -123,7 +167,13 @@ public int exportUsers(List data) { int rowsAffected = 0; try { ps = this.connection.prepareStatement( - "INSERT INTO users (user_id, role, first_name, last_name, email, password_hash) VALUES (?, ?, ?, ?, ?, ?)"); + """ + INSERT INTO users + (user_id, role, first_name, last_name, email, password_hash) + VALUES + (?, ?, ?, ?, ?, ?) + """ + ); for (Object[] row : data) { try { ps.setInt(1, (int) row[0]); @@ -144,10 +194,21 @@ public int exportUsers(List data) { return rowsAffected; } + /** + * Imports all donations. + * + * @return A List of Object arrays for each donation in the database. + */ public List fetchAllDonations() { return this.importDonations(0, true); } + /** + * Imports the donations of a specific user based on a given user_id. + * + * @param user_id The id of the user to get the donations of. + * @return A List of Object arrays for each donation in the database. + */ public List importDonations(int user_id) { return this.importDonations(user_id, false); } @@ -155,7 +216,7 @@ public List importDonations(int user_id) { private List importDonations(int user_id, boolean all) { PreparedStatement ps = null; ResultSet results = null; - try{ + try { if (all) { ps = this.connection.prepareStatement("SELECT * FROM donations"); } else { @@ -185,7 +246,17 @@ private List importDonations(int user_id, boolean all) { return this.donations; } - public int exportDonations(List data) { + /** + * Puts new donations into the database. + * + * @param data The new donation to put into the database. Each Object array in the List is a new + * donations to add as a row. + * @return The number of rows affected in the transaction. + * @throws IllegalArgumentException This exception is thrown when data is null, its rows are not + * of length 6, any of the rows are null, any of the rows are duplicates or existing rows in + * the database, or any of the values in the rows can't be cast to the correct data-types. + */ + public int exportDonations(List data) throws IllegalArgumentException { this.fetchAllDonations(); if (data == null) { @@ -212,7 +283,13 @@ public int exportDonations(List data) { int rowsAffected = 0; try { ps = this.connection.prepareStatement( - "INSERT INTO donations (donation_id, user_id, organization_id, amount, dating, payment_method) VALUES (?, (SELECT user_id FROM users WHERE user_id = ?), ?, ?, ?, ?)"); + """ + INSERT INTO donations + (donation_id, user_id, organization_id, amount, dating, payment_method) + VALUES + (?, (SELECT user_id FROM users WHERE user_id = ?), ?, ?, ?, ?) + """ + ); for (Object[] row : data) { try { for (int i = 0; i < 3; i++) { From bdd4854b66e2513da5a85db9c4bd66f1499c5297 Mon Sep 17 00:00:00 2001 From: Lucy Ciara Herud-Thomassen <86323303+LucyCiara@users.noreply.github.com> Date: Thu, 9 Apr 2026 11:08:13 +0200 Subject: [PATCH 2/8] style[DbWrapper]: add whitespaces after try --- src/main/java/edu/group5/app/model/wrapper/DbWrapper.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/edu/group5/app/model/wrapper/DbWrapper.java b/src/main/java/edu/group5/app/model/wrapper/DbWrapper.java index 2357e87..f453cc3 100644 --- a/src/main/java/edu/group5/app/model/wrapper/DbWrapper.java +++ b/src/main/java/edu/group5/app/model/wrapper/DbWrapper.java @@ -69,7 +69,7 @@ public boolean connect() { */ public boolean disconnect() { // We are not interested in whether it fails to close, as we check its closed status later. - try{ this.connection.close(); } catch (Exception e) {}; + try { this.connection.close(); } catch (Exception e) {}; try { return this.connection.isClosed(); } catch (Exception e) { @@ -100,7 +100,7 @@ private void close(ResultSet results, PreparedStatement ps) { public List importUsers() { PreparedStatement ps = null; ResultSet results = null; - try{ + try { ps = this.connection.prepareStatement("SELECT * FROM users"); results = ps.executeQuery(); List data = new ArrayList(); From c3b05558c529e463293699a442933407ff7ee316 Mon Sep 17 00:00:00 2001 From: Lucy Ciara Herud-Thomassen <86323303+LucyCiara@users.noreply.github.com> Date: Thu, 9 Apr 2026 11:20:20 +0200 Subject: [PATCH 3/8] style[ParameterValidator]: make code conform to google checks --- .../group5/app/utils/ParameterValidator.java | 36 +++++++++++++------ 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/src/main/java/edu/group5/app/utils/ParameterValidator.java b/src/main/java/edu/group5/app/utils/ParameterValidator.java index 1a44e46..996bf6e 100644 --- a/src/main/java/edu/group5/app/utils/ParameterValidator.java +++ b/src/main/java/edu/group5/app/utils/ParameterValidator.java @@ -1,21 +1,24 @@ package edu.group5.app.utils; import java.math.BigDecimal; + /** - * ParameterValidator is a utility class that provides static methods for validating various types of parameters. - * It includes methods for checking strings, integers, objects, and BigDecimal values to ensure they meet specific - * criteria such as not being null, not being blank, or being positive. - * + * ParameterValidator is a utility class that provides static methods for validating various types + * of parameters. + * It includes methods for checking strings, integers, objects, and BigDecimal values to ensure + * they meet specific criteria such as not being null, not being blank, or being positive. */ public final class ParameterValidator { /** * Validates that a string parameter is not null and not blank. + * * @param stringArg the string parameter to validate * @param variableName the name of the variable being validated, used in exception messages * @throws IllegalArgumentException if the string is null or blank */ - public static final void stringChecker(String stringArg, String variableName) throws IllegalArgumentException { + public static final void stringChecker(String stringArg, String variableName) + throws IllegalArgumentException { nullCheck(stringArg, variableName); if (stringArg.isBlank()) { throw new IllegalArgumentException(String.format("%s can't be blank", variableName)); @@ -24,33 +27,41 @@ public static final void stringChecker(String stringArg, String variableName) th /** * Validates that an integer parameter is not null and is a positive integer. + * * @param intArg the integer parameter to validate * @param variableName the name of the variable being validated, used in exception messages * @throws IllegalArgumentException if the integer is null or not a positive integer */ - public static final void intChecker(int intArg, String variableName) throws IllegalArgumentException { + public static final void intChecker(int intArg, String variableName) + throws IllegalArgumentException { if (intArg <= 0) { - throw new IllegalArgumentException(String.format("%s must be a positive integer", variableName)); + throw new IllegalArgumentException( + String.format("%s must be a positive integer", variableName) + ); } } /** * Validates that an object parameter is not null. + * * @param objectArg the object parameter to validate * @param variableName the name of the variable being validated, used in exception messages * @throws IllegalArgumentException if the object is null */ - public static final void objectChecker(Object objectArg, String variableName) throws IllegalArgumentException { + public static final void objectChecker(Object objectArg, String variableName) + throws IllegalArgumentException { nullCheck(objectArg, variableName); } /** * Validates that a BigDecimal parameter is not null and is greater than zero. + * * @param bigDecimalArg the BigDecimal parameter to validate * @param variableName the name of the variable being validated, used in exception messages * @throws IllegalArgumentException if the BigDecimal is null or not greater than zero */ - public static final void bigDecimalChecker(BigDecimal bigDecimalArg, String variableName) throws IllegalArgumentException { + public static final void bigDecimalChecker(BigDecimal bigDecimalArg, String variableName) + throws IllegalArgumentException { nullCheck(bigDecimalArg, variableName); if (bigDecimalArg.compareTo(BigDecimal.ZERO) <= 0) { throw new IllegalArgumentException(String.format("%s must be larger than 0", variableName)); @@ -58,12 +69,15 @@ public static final void bigDecimalChecker(BigDecimal bigDecimalArg, String vari } /** - * Helper method to check if a variable is null and throw an IllegalArgumentException with a formatted message if it is. + * Helper method to check if a variable is null and throw an IllegalArgumentException with a + * formatted message if it is. + * * @param variable the variable to check for null * @param variableName the name of the variable being checked, used in the exception message * @throws IllegalArgumentException if the variable is null */ - private static final void nullCheck(Object variable, String variableName) throws IllegalArgumentException { + private static final void nullCheck(Object variable, String variableName) + throws IllegalArgumentException { if (variable == null) { throw new IllegalArgumentException(String.format("%s can't be null", variableName)); } From ab64cda1599c09cccfca940bae3bf9565feeac04 Mon Sep 17 00:00:00 2001 From: Lucy Ciara Herud-Thomassen <86323303+LucyCiara@users.noreply.github.com> Date: Thu, 9 Apr 2026 11:47:07 +0200 Subject: [PATCH 4/8] refactor[DbWrapper&ParameterValidator]: move parameter validation in exports to ParameterValidator class --- .../group5/app/model/wrapper/DbWrapper.java | 45 +++---------------- .../group5/app/utils/ParameterValidator.java | 43 ++++++++++++++++++ 2 files changed, 50 insertions(+), 38 deletions(-) diff --git a/src/main/java/edu/group5/app/model/wrapper/DbWrapper.java b/src/main/java/edu/group5/app/model/wrapper/DbWrapper.java index f453cc3..c61bbc8 100644 --- a/src/main/java/edu/group5/app/model/wrapper/DbWrapper.java +++ b/src/main/java/edu/group5/app/model/wrapper/DbWrapper.java @@ -1,5 +1,6 @@ package edu.group5.app.model.wrapper; +import java.lang.reflect.Parameter; import java.math.BigDecimal; import java.sql.Connection; import java.sql.DriverManager; @@ -15,6 +16,8 @@ import java.util.logging.Level; import java.util.logging.Logger; +import edu.group5.app.utils.ParameterValidator; + /** * A class for wrapping the database. */ @@ -137,30 +140,11 @@ public List importUsers() { */ public int exportUsers(List data) throws IllegalArgumentException { this.importUsers(); - - if (data == null) { - throw new IllegalArgumentException("data can't be null"); - } + if (data.isEmpty()) { return 0; } - // TODO: change to check length for every row. - if (data.get(0).length != 6) { - throw new IllegalArgumentException("data's arrays must have a length of 6"); - } - if (data.stream().anyMatch(i -> Arrays.asList(i).contains(null))) { - throw new IllegalArgumentException("One or more rows in data contains null values"); - } - // TODO: change how existing rows are checked. - if (this.users.size() > 0) { - if ((int) data.getLast()[0] <= (int) this.users.getLast()[0]) { - throw new IllegalArgumentException("data can't contain existing rows"); - } - } - Set ids = new HashSet<>(); - if (data.stream().anyMatch(i -> !ids.add(i[0]))) { - throw new IllegalArgumentException("data can't contain duplicate rows"); - } + ParameterValidator.exportChecker(data, "data", this.users); PreparedStatement ps = null; @@ -258,26 +242,11 @@ private List importDonations(int user_id, boolean all) { */ public int exportDonations(List data) throws IllegalArgumentException { this.fetchAllDonations(); - - if (data == null) { - throw new IllegalArgumentException("data can't be null"); - } + if (data.isEmpty()) { return 0; } - if (data.get(0).length != 6) { - throw new IllegalArgumentException("data's arrays must have a length of 6"); - } - if (data.stream().anyMatch(i -> Arrays.asList(i).contains(null))) { - throw new IllegalArgumentException("One or more rows in data contains null values"); - } - if (this.donations.size() > 0 && (int) data.getLast()[0] <= (int) this.donations.getLast()[0]) { - throw new IllegalArgumentException("data can't contain existing rows"); - } - Set ids = new HashSet<>(); - if (data.stream().anyMatch(i -> !ids.add(i[0]))) { - throw new IllegalArgumentException("data can't contain duplicate rows"); - } + ParameterValidator.exportChecker(data, "data", this.donations); PreparedStatement ps = null; int rowsAffected = 0; diff --git a/src/main/java/edu/group5/app/utils/ParameterValidator.java b/src/main/java/edu/group5/app/utils/ParameterValidator.java index 996bf6e..36a0d14 100644 --- a/src/main/java/edu/group5/app/utils/ParameterValidator.java +++ b/src/main/java/edu/group5/app/utils/ParameterValidator.java @@ -1,6 +1,10 @@ package edu.group5.app.utils; import java.math.BigDecimal; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; /** * ParameterValidator is a utility class that provides static methods for validating various types @@ -82,4 +86,43 @@ private static final void nullCheck(Object variable, String variableName) throw new IllegalArgumentException(String.format("%s can't be null", variableName)); } } + + /** + * A method for checking if the data to be exported is valid. + * + * @param data The data to export. + * @param dataName The name of the variable of the data to export. + * @param oldData The existing data to compare to for checking row existence. + * @throws IllegalArgumentException This exception is thrown when data is null, its rows are not + * of length 6, any of the rows are null, any of the rows are duplicates or existing rows in + * the database, or any of the values in the rows can't be cast to the correct data-types. + */ + public static final void exportChecker( + List data, String dataName, List oldData + ) throws IllegalArgumentException { + if (data == null) { + throw new IllegalArgumentException(String.format("%s can't be null", dataName)); + } + // TODO: change to check length for every row. + if (data.get(0).length != 6) { + throw new IllegalArgumentException( + String.format("%s's arrays must have a length of 6", dataName) + ); + } + if (data.stream().anyMatch(i -> Arrays.asList(i).contains(null))) { + throw new IllegalArgumentException( + String.format("One or more rows in %s contains null values", dataName) + ); + } + // TODO: change how existing rows are checked. + if (oldData.size() > 0) { + if ((int) data.getLast()[0] <= (int) oldData.getLast()[0]) { + throw new IllegalArgumentException("data can't contain existing rows"); + } + } + Set ids = new HashSet<>(); + if (data.stream().anyMatch(i -> !ids.add(i[0]))) { + throw new IllegalArgumentException("data can't contain duplicate rows"); + } + } } From 2f72f759b3a656979fcecf969a81ac23f2ee3145 Mon Sep 17 00:00:00 2001 From: Lucy Ciara Herud-Thomassen <86323303+LucyCiara@users.noreply.github.com> Date: Thu, 9 Apr 2026 12:42:28 +0200 Subject: [PATCH 5/8] refactor[OrgApiWrapper]: put off parameter validation to ParameterValidator --- .../java/edu/group5/app/model/wrapper/OrgApiWrapper.java | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/main/java/edu/group5/app/model/wrapper/OrgApiWrapper.java b/src/main/java/edu/group5/app/model/wrapper/OrgApiWrapper.java index 910b110..2a493e4 100644 --- a/src/main/java/edu/group5/app/model/wrapper/OrgApiWrapper.java +++ b/src/main/java/edu/group5/app/model/wrapper/OrgApiWrapper.java @@ -1,5 +1,6 @@ package edu.group5.app.model.wrapper; +import edu.group5.app.utils.ParameterValidator; import java.io.IOException; import java.net.URI; import java.net.http.HttpClient; @@ -24,11 +25,7 @@ public class OrgApiWrapper extends Wrapper { * @param urlString A string of the URL that's being connected to. */ public OrgApiWrapper(String urlString) { - if (urlString == null) { - throw new IllegalArgumentException("url can't be null"); - } else if (urlString.isBlank()) { - throw new IllegalArgumentException("url can't be blank"); - } + ParameterValidator.stringChecker(urlString, "url"); try { URI uri = URI.create(urlString); this.client = HttpClient.newHttpClient(); From 281d68dbba5e843e2e69de88b1638c7618660bd3 Mon Sep 17 00:00:00 2001 From: Lucy Ciara Herud-Thomassen <86323303+LucyCiara@users.noreply.github.com> Date: Thu, 9 Apr 2026 12:43:55 +0200 Subject: [PATCH 6/8] chore[utils] remove placeholder util classes --- src/main/java/edu/group5/app/utils/Utilities.java | 5 ----- src/test/java/edu/group5/app/utils/UtilitiesTest.java | 5 ----- 2 files changed, 10 deletions(-) delete mode 100644 src/main/java/edu/group5/app/utils/Utilities.java delete mode 100644 src/test/java/edu/group5/app/utils/UtilitiesTest.java diff --git a/src/main/java/edu/group5/app/utils/Utilities.java b/src/main/java/edu/group5/app/utils/Utilities.java deleted file mode 100644 index ce21d22..0000000 --- a/src/main/java/edu/group5/app/utils/Utilities.java +++ /dev/null @@ -1,5 +0,0 @@ -package edu.group5.app.utils; - -public class Utilities { - -} diff --git a/src/test/java/edu/group5/app/utils/UtilitiesTest.java b/src/test/java/edu/group5/app/utils/UtilitiesTest.java deleted file mode 100644 index 88aa0c9..0000000 --- a/src/test/java/edu/group5/app/utils/UtilitiesTest.java +++ /dev/null @@ -1,5 +0,0 @@ -package edu.group5.app.utils; - -public class UtilitiesTest { - -} From 1a5616e521fbe63361f93797d0217249ce555046 Mon Sep 17 00:00:00 2001 From: Lucy Ciara Herud-Thomassen <86323303+LucyCiara@users.noreply.github.com> Date: Thu, 9 Apr 2026 12:45:41 +0200 Subject: [PATCH 7/8] fix[DbWrapper&ParameterValidator]: fix checks so empty data exports skip data validation --- .../group5/app/model/wrapper/DbWrapper.java | 18 ++----- .../group5/app/utils/ParameterValidator.java | 47 +++++++++---------- 2 files changed, 26 insertions(+), 39 deletions(-) diff --git a/src/main/java/edu/group5/app/model/wrapper/DbWrapper.java b/src/main/java/edu/group5/app/model/wrapper/DbWrapper.java index c61bbc8..f357ae1 100644 --- a/src/main/java/edu/group5/app/model/wrapper/DbWrapper.java +++ b/src/main/java/edu/group5/app/model/wrapper/DbWrapper.java @@ -1,6 +1,6 @@ package edu.group5.app.model.wrapper; -import java.lang.reflect.Parameter; +import edu.group5.app.utils.ParameterValidator; import java.math.BigDecimal; import java.sql.Connection; import java.sql.DriverManager; @@ -9,15 +9,10 @@ import java.sql.SQLException; import java.sql.Timestamp; import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashSet; import java.util.List; -import java.util.Set; import java.util.logging.Level; import java.util.logging.Logger; -import edu.group5.app.utils.ParameterValidator; - /** * A class for wrapping the database. */ @@ -141,11 +136,7 @@ public List importUsers() { public int exportUsers(List data) throws IllegalArgumentException { this.importUsers(); - if (data.isEmpty()) { - return 0; - } - ParameterValidator.exportChecker(data, "data", this.users); - + ParameterValidator.exportChecker(data, "data", this.users, 6); PreparedStatement ps = null; int rowsAffected = 0; @@ -243,10 +234,7 @@ private List importDonations(int user_id, boolean all) { public int exportDonations(List data) throws IllegalArgumentException { this.fetchAllDonations(); - if (data.isEmpty()) { - return 0; - } - ParameterValidator.exportChecker(data, "data", this.donations); + ParameterValidator.exportChecker(data, "data", this.donations, 6); PreparedStatement ps = null; int rowsAffected = 0; diff --git a/src/main/java/edu/group5/app/utils/ParameterValidator.java b/src/main/java/edu/group5/app/utils/ParameterValidator.java index 36a0d14..3bc3e5a 100644 --- a/src/main/java/edu/group5/app/utils/ParameterValidator.java +++ b/src/main/java/edu/group5/app/utils/ParameterValidator.java @@ -98,31 +98,30 @@ private static final void nullCheck(Object variable, String variableName) * the database, or any of the values in the rows can't be cast to the correct data-types. */ public static final void exportChecker( - List data, String dataName, List oldData + List data, String dataName, List oldData, int rowLength ) throws IllegalArgumentException { - if (data == null) { - throw new IllegalArgumentException(String.format("%s can't be null", dataName)); - } - // TODO: change to check length for every row. - if (data.get(0).length != 6) { - throw new IllegalArgumentException( - String.format("%s's arrays must have a length of 6", dataName) - ); - } - if (data.stream().anyMatch(i -> Arrays.asList(i).contains(null))) { - throw new IllegalArgumentException( - String.format("One or more rows in %s contains null values", dataName) - ); - } - // TODO: change how existing rows are checked. - if (oldData.size() > 0) { - if ((int) data.getLast()[0] <= (int) oldData.getLast()[0]) { - throw new IllegalArgumentException("data can't contain existing rows"); + objectChecker(data, dataName); + if (!data.isEmpty()) { + if (data.stream().anyMatch(i -> i.length != rowLength)) { + throw new IllegalArgumentException( + String.format("%s's arrays must have a length of 6", dataName) + ); } - } - Set ids = new HashSet<>(); - if (data.stream().anyMatch(i -> !ids.add(i[0]))) { - throw new IllegalArgumentException("data can't contain duplicate rows"); - } + if (data.stream().anyMatch(i -> Arrays.asList(i).contains(null))) { + throw new IllegalArgumentException( + String.format("One or more rows in %s contains null values", dataName) + ); + } + + Set ids = new HashSet<>(); + if (data.stream().anyMatch(i -> !ids.add(i[0]))) { + throw new IllegalArgumentException("data can't contain duplicate rows"); + } + if (oldData.size() > 0) { + if (oldData.stream().anyMatch(i -> !ids.add(i[0]))) { + throw new IllegalArgumentException("data can't contain existing rows"); + } + } + } } } From ac865375627a2193382b03d6b3cfc1641df2aeab Mon Sep 17 00:00:00 2001 From: Lucy Ciara Herud-Thomassen <86323303+LucyCiara@users.noreply.github.com> Date: Thu, 9 Apr 2026 12:51:28 +0200 Subject: [PATCH 8/8] style[test/wrapper]: order imports and split too long lines --- .../model/wrapper/DbWrapperDonationsTest.java | 32 ++++++++++++------- .../app/model/wrapper/DbWrapperUserTest.java | 22 ++++--------- .../app/model/wrapper/OrgApiWrapperTest.java | 2 -- 3 files changed, 27 insertions(+), 29 deletions(-) diff --git a/src/test/java/edu/group5/app/model/wrapper/DbWrapperDonationsTest.java b/src/test/java/edu/group5/app/model/wrapper/DbWrapperDonationsTest.java index cf76092..1db7a90 100644 --- a/src/test/java/edu/group5/app/model/wrapper/DbWrapperDonationsTest.java +++ b/src/test/java/edu/group5/app/model/wrapper/DbWrapperDonationsTest.java @@ -12,12 +12,12 @@ import java.util.Arrays; import java.util.Date; import java.util.List; - import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import edu.group5.app.model.wrapper.DbWrapper; - +/** + * A test class for interactions with the donations table using the DbWrapper. + */ public class DbWrapperDonationsTest { private Object[] johnDonation; private List users; @@ -54,13 +54,15 @@ void init() { users.add(row); } - this.johnDonation = new Object[] { 1, 1, 39, new BigDecimal(20.02), new Timestamp(new Date().getTime()), - "Paypal" }; + 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.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); @@ -104,7 +106,12 @@ public void importDonationsIsOnlyExportDonationsTest() { 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( + donationEquals( + this.donations.get(0), + this.db.importDonations((int) this.users.get(0)[0]).get(0) + ) + ); assertTrue(this.db.disconnect()); }); } @@ -157,9 +164,12 @@ 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); - }); + 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()); diff --git a/src/test/java/edu/group5/app/model/wrapper/DbWrapperUserTest.java b/src/test/java/edu/group5/app/model/wrapper/DbWrapperUserTest.java index 7aca3c0..00107de 100644 --- a/src/test/java/edu/group5/app/model/wrapper/DbWrapperUserTest.java +++ b/src/test/java/edu/group5/app/model/wrapper/DbWrapperUserTest.java @@ -2,27 +2,18 @@ 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 edu.group5.app.model.wrapper.DbWrapper; -import javafx.util.converter.BigDecimalStringConverter; - +/** + * A class for testing interactions with the users table of the database using DbWrapper. + */ public class DbWrapperUserTest { private Object[] johnDoe; private Object[] janeDoe; @@ -54,8 +45,9 @@ void init() { this.users3 = new ArrayList(); this.users3.add(this.janeDoe); - this.repeatingJoeJoe = new Object[] { 3, "Customer", "Repeating", "JoeJoe", "repeatingjjoe@email.com", - "passwordpassword" }; + 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); @@ -187,6 +179,4 @@ public void addingUserListWithNullInRowThrowsExpectedException() { 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/model/wrapper/OrgApiWrapperTest.java b/src/test/java/edu/group5/app/model/wrapper/OrgApiWrapperTest.java index 0a643f2..0165456 100644 --- a/src/test/java/edu/group5/app/model/wrapper/OrgApiWrapperTest.java +++ b/src/test/java/edu/group5/app/model/wrapper/OrgApiWrapperTest.java @@ -9,8 +9,6 @@ import java.lang.IllegalArgumentException; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; - -import edu.group5.app.model.wrapper.OrgApiWrapper; import tools.jackson.core.exc.StreamReadException; /**