diff --git a/helpmehelpapplication/src/main/java/ntnu/sytemutvikling/team6/models/DatabaseManager.java b/helpmehelpapplication/src/main/java/ntnu/sytemutvikling/team6/models/DatabaseManager.java index eacb4e7..3d80458 100644 --- a/helpmehelpapplication/src/main/java/ntnu/sytemutvikling/team6/models/DatabaseManager.java +++ b/helpmehelpapplication/src/main/java/ntnu/sytemutvikling/team6/models/DatabaseManager.java @@ -50,6 +50,38 @@ public DatabaseManager() { } } + /** + * Constructs a new {@code DatabaseManager} using database credentials + *

+ * Used primarily for JUnit tests. Production should use the constructor + * using environment variables. + *

+ * + * @param databaseURL the url to the database + * @param username the username used to log in to the database + * @param password the password used to log in to the database + * @throws IllegalArgumentException if databaseURL, username, or password is + * {@code null} or blank + */ + + public DatabaseManager(String databaseURL, String username, String password) { + this.databaseURL = databaseURL; + this.username = username; + this.password = password; + + if (this.databaseURL == null || this.databaseURL.isBlank()) { + throw new IllegalArgumentException("Database environment variable URL has not been set"); + } + + if (this.username == null || this.username.isBlank()) { + throw new IllegalArgumentException("Username environment variable has not been set"); + } + + if (this.password == null || this.password.isBlank()) { + throw new IllegalArgumentException("Password environment variable has not been set"); + } + } + /** * Creates the {@code charities} table if it does not already exist. *

@@ -74,7 +106,7 @@ url VARCHAR(255), s.execute(sql_query); } catch (SQLException e) { e.printStackTrace(); - throw new RuntimeException("Error creating tables."); + throw new RuntimeException("Error creating table."); } } @@ -125,7 +157,7 @@ INSERT INTO charities (org_number, name, status, url, is_pre_approved) // Temporary table for parity check - sql_query = "CREATE TEMPORARY TABLE temp (org_number VARCHAR(100) PRIMARY KEY)"; + sql_query = "CREATE TEMPORARY TABLE IF NOT EXISTS temp (org_number VARCHAR(100) PRIMARY KEY)"; try (PreparedStatement temp_create = conn.prepareStatement(sql_query)) { temp_create.execute(); }