-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added some coverage tests to check if it functions as intended. Does not currently have 100% coverage.
- Loading branch information
Showing
1 changed file
with
235 additions
and
0 deletions.
There are no files selected for viewing
235 changes: 235 additions & 0 deletions
235
...mehelpapplication/src/test/java/ntnu/sytemutvikling/team6/models/DatabaseManagerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,235 @@ | ||
| package ntnu.sytemutvikling.team6.models; | ||
|
|
||
| import org.junit.jupiter.api.*; | ||
| import java.sql.*; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import org.h2.jdbc.JdbcSQLNonTransientException; | ||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| class DatabaseManagerTest { | ||
|
|
||
| private DatabaseManager dbManager; | ||
| private String db_url; | ||
| private String username; | ||
| private String password; | ||
|
|
||
| @BeforeEach | ||
| void setUp() throws SQLException{ | ||
| db_url = "jdbc:h2:mem:testdb;MODE=MySQL;DB_CLOSE_DELAY=-1"; | ||
| username = "user"; | ||
| password = "123"; | ||
| dbManager = new DatabaseManager(db_url, username, password); | ||
|
|
||
| // Removes tables due to quirk with H2 that keeps temporary tables | ||
| try (Connection conn = DriverManager.getConnection(db_url, username, password)) { | ||
| PreparedStatement endstmt = conn.prepareStatement("DROP TABLE IF EXISTS charities, temp"); | ||
| endstmt.execute(); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void constructorShouldThrowIllegalArgumentExceptionIfDatabaseURLIsNull() { | ||
| assertThrows(IllegalArgumentException.class, () -> new DatabaseManager(null, username, password), | ||
| "DatabaseURL should not be allowed to be null."); | ||
| } | ||
|
|
||
| @Test | ||
| void constructorShouldThrowIllegalArgumentExceptionIfDatabaseURLIsBlankOrEmpty() { | ||
| assertThrows(IllegalArgumentException.class, () -> new DatabaseManager(" ", username, password), | ||
| "DatabaseURL should not be allowed to be blank."); | ||
| assertThrows(IllegalArgumentException.class, () -> new DatabaseManager("", username, password), | ||
| "DatabaseURL should not be allowed to be empty."); | ||
| } | ||
|
|
||
| @Test | ||
| void constructorShouldThrowIllegalArgumentExceptionIfUsernameIsNull() { | ||
| assertThrows(IllegalArgumentException.class, () -> new DatabaseManager(db_url, null, password), | ||
| "Database username should not be allowed to be null."); | ||
| } | ||
|
|
||
| @Test | ||
| void constructorShouldThrowIllegalArgumentExceptionIfUsernameIsBlankOrEmpty() { | ||
| assertThrows(IllegalArgumentException.class, () -> new DatabaseManager(db_url, " ", password), | ||
| "Username should not be allowed to be blank."); | ||
| assertThrows(IllegalArgumentException.class, () -> new DatabaseManager(db_url, "", password), | ||
| "Username should not be allowed to be empty."); | ||
| } | ||
|
|
||
| @Test | ||
| void constructorShouldThrowIllegalArgumentExceptionIfPasswordIsNull() { | ||
| assertThrows(IllegalArgumentException.class, () -> new DatabaseManager(db_url, username, null), | ||
| "Database password should not be allowed to be null."); | ||
| } | ||
|
|
||
| @Test | ||
| void constructorShouldThrowIllegalArgumentExceptionIfPasswordIsBlankOrEmpty() { | ||
| assertThrows(IllegalArgumentException.class, () -> new DatabaseManager(db_url, username, " "), | ||
| "Password should not be allowed to be blank."); | ||
| assertThrows(IllegalArgumentException.class, () -> new DatabaseManager(db_url, username, ""), | ||
| "Password should not be allowed to be empty."); | ||
| } | ||
|
|
||
| @Test | ||
| void createCharitiesTableShouldCreateTableSuccessfully() throws SQLException { | ||
| dbManager.createCharitiesTable(); | ||
|
|
||
| try (Connection conn = DriverManager.getConnection(db_url, username, password)) { | ||
| ResultSet rs = conn.getMetaData().getTables(null, null, | ||
| "CHARITIES", null); | ||
|
|
||
| assertTrue(rs.next(), "Connection to the database should be successful."); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void updateCharitiesShouldInsertCorrectData() throws SQLException { | ||
| String org_number = "12345"; | ||
| String name = "Svindelorg"; | ||
| String status = "approved"; | ||
| String url = "https://www.svindel.no"; | ||
| boolean is_pre_approved = false; | ||
|
|
||
| var charity = new APICharityData( | ||
| org_number, | ||
| name, | ||
| status, | ||
| url, | ||
| is_pre_approved | ||
| ); | ||
|
|
||
| dbManager.createCharitiesTable(); | ||
| dbManager.updateCharities(List.of(charity)); | ||
|
|
||
| try (Connection conn = DriverManager.getConnection(db_url, username, password); | ||
| PreparedStatement stmt = conn.prepareStatement("SELECT * FROM charities WHERE org_number = ?")) { | ||
|
|
||
| stmt.setString(1, org_number); | ||
| ResultSet rs = stmt.executeQuery(); | ||
|
|
||
| assertTrue(rs.next()); | ||
| assertEquals(org_number, rs.getString("org_number"), "Organization number should " + | ||
| "be correct."); | ||
| assertEquals(name, rs.getString("name"), "Name of the organization should be correct."); | ||
| assertEquals(status, rs.getString("status"), "Status of the organization should be " + | ||
| "correct."); | ||
| assertEquals(url, rs.getString("url"), "Url of the organization should be correct."); | ||
| assertEquals(is_pre_approved, rs.getBoolean("is_pre_approved"), "Is_pre_approved " + | ||
| "value should be correct."); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void updateCharitiesShouldRemoveDataNotInList() throws SQLException { | ||
| String org_number = "12345"; | ||
| String name = "Svindelorg"; | ||
| String status = "approved"; | ||
| String url = "https://www.svindel.no"; | ||
| boolean is_pre_approved = false; | ||
|
|
||
| var charity1 = new APICharityData( | ||
| org_number, | ||
| name, | ||
| status, | ||
| url, | ||
| is_pre_approved | ||
| ); | ||
|
|
||
| org_number = "23456"; | ||
| name = "SvindelKoin"; | ||
| status = "approved"; | ||
| url = "https://www.svindel.net"; | ||
| is_pre_approved = true; | ||
|
|
||
| var charity2 = new APICharityData( | ||
| org_number, | ||
| name, | ||
| status, | ||
| url, | ||
| is_pre_approved | ||
| ); | ||
|
|
||
| org_number = "345672"; | ||
| name = "Arme Svindlere"; | ||
| status = "approved"; | ||
| url = "https://www.armesvindlere.com"; | ||
| is_pre_approved = false; | ||
|
|
||
| var charity3 = new APICharityData( | ||
| org_number, | ||
| name, | ||
| status, | ||
| url, | ||
| is_pre_approved | ||
| ); | ||
|
|
||
| var charityListBefore = new ArrayList<APICharityData>(); | ||
| charityListBefore.add(charity1); | ||
| charityListBefore.add(charity2); | ||
| charityListBefore.add(charity3); | ||
|
|
||
| var charityListAfter = new ArrayList<APICharityData>(); | ||
| charityListAfter.add(charity1); | ||
| charityListAfter.add(charity3); | ||
|
|
||
| dbManager.createCharitiesTable(); | ||
| dbManager.updateCharities(charityListBefore); | ||
|
|
||
| try (Connection conn = DriverManager.getConnection(db_url, username, password)) { | ||
| PreparedStatement stmt = conn.prepareStatement("SELECT COUNT(org_number) AS number_b FROM charities"); | ||
|
|
||
| ResultSet rs = stmt.executeQuery(); | ||
|
|
||
| assertTrue(rs.next(), "Charities count row should exist."); | ||
| assertEquals(3, rs.getInt("number_b"), "The amount of org_numbers in the table" + | ||
| "should be 3."); | ||
| PreparedStatement endstmt = conn.prepareStatement("DROP TABLE IF EXISTS temp"); | ||
| endstmt.execute(); | ||
| } | ||
|
|
||
| dbManager.updateCharities(charityListAfter); | ||
|
|
||
| try (Connection conn = DriverManager.getConnection(db_url, username, password)) { | ||
| PreparedStatement stmt = conn.prepareStatement("SELECT COUNT(org_number) AS number_a FROM charities"); | ||
|
|
||
| ResultSet rs = stmt.executeQuery(); | ||
|
|
||
| assertTrue(rs.next(), "Charities count row should exist."); | ||
| assertEquals(2, rs.getInt("number_a"), "The amount of org_numbers in the table" + | ||
| "should be 2 due to removal of 1 table."); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void tempTableShouldNotExistAfterUpdating() throws SQLException{ | ||
| try (Connection conn = DriverManager.getConnection(db_url, username, password)) { | ||
| PreparedStatement endstmt = conn.prepareStatement("DROP TABLE IF EXISTS charities, temp"); | ||
| endstmt.execute(); | ||
| } | ||
| String org_number = "12345"; | ||
| String name = "Svindelorg"; | ||
| String status = "approved"; | ||
| String url = "https://www.svindel.no"; | ||
| boolean is_pre_approved = false; | ||
|
|
||
| var charity = new APICharityData( | ||
| org_number, | ||
| name, | ||
| status, | ||
| url, | ||
| is_pre_approved | ||
| ); | ||
|
|
||
| dbManager.createCharitiesTable(); | ||
| dbManager.updateCharities(List.of(charity)); | ||
|
|
||
| try (Connection conn = DriverManager.getConnection(db_url, username, password)) { | ||
| PreparedStatement stmt = conn.prepareStatement("SELECT * FROM temp"); | ||
|
|
||
| ResultSet rs = stmt.executeQuery(); | ||
|
|
||
| assertThrows(JdbcSQLNonTransientException.class, () -> rs.getString("org_number"), | ||
| "Exception about table being empty should be thrown."); | ||
| } | ||
| } | ||
| } |