-
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.
Feat> DatbaseManagerTest and DonationTest is refurbisehed
- Loading branch information
AdrianBalunan
committed
Mar 15, 2026
1 parent
3c3035e
commit 2318570
Showing
3 changed files
with
180 additions
and
207 deletions.
There are no files selected for viewing
167 changes: 167 additions & 0 deletions
167
...elpapplication/src/test/java/ntnu/systemutvikling/team6/database/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,167 @@ | ||
| package ntnu.systemutvikling.team6.models; | ||
|
|
||
| import ntnu.systemutvikling.team6.database.DatabaseConnection; | ||
| import ntnu.systemutvikling.team6.scraper.APICharityData; | ||
| import ntnu.systemutvikling.team6.database.DatabaseManager; | ||
| import org.junit.jupiter.api.*; | ||
| import java.sql.*; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
|
|
||
| import org.h2.jdbc.JdbcSQLNonTransientException; | ||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| class DatabaseManagerTest { | ||
|
|
||
| private DatabaseManager dbManager; | ||
|
|
||
| @BeforeEach | ||
| public void setUp() throws SQLException{ | ||
| this.dbManager = new DatabaseManager(); | ||
| } | ||
|
|
||
| // Make sure you're connected to the NTNU network for this to work | ||
| @Test | ||
| public void testConnectionShouldReturnTrue(){ | ||
| assertTrue(dbManager.testConnection()); | ||
| } | ||
| @Test | ||
| void createCharitiesTableShouldCreateTableSuccessfully() throws SQLException { | ||
| dbManager.createTables(); | ||
|
|
||
| try (Connection conn = new DatabaseConnection().getMySqlConnection()) { | ||
| ResultSet rs = conn.getMetaData().getTables(null, null, | ||
| "Charities", null); | ||
|
|
||
| assertTrue(rs.next()); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void updateCharitiesShouldInsertCorrectData() throws SQLException { | ||
| dbManager.createTables(); | ||
|
|
||
| String org_number = "12345"; | ||
| String name = "Test Charity"; | ||
| String status = "approved"; | ||
| String url = "https://www.svindel.no"; | ||
| boolean is_pre_approved = false; | ||
|
|
||
| Charity charity = new Charity( | ||
| org_number, | ||
| url, | ||
| name, | ||
| is_pre_approved, | ||
| status | ||
| ); | ||
| dbManager.addAPIDataToTable(List.of(charity)); | ||
|
|
||
| Charity charity2 = new Charity( | ||
| org_number, | ||
| url, | ||
| name + " Updated", | ||
| is_pre_approved, | ||
| status | ||
| ); | ||
| dbManager.addAPIDataToTable(List.of(charity2)); | ||
|
|
||
| dbManager.createTables(); | ||
|
|
||
| CharityRegistry registry = dbManager.getCharitiesFromDB(); | ||
| Charity insertedCharity = registry.findCharityByOrgnumber("12345").get(); | ||
| assertEquals("Test Charity Updated", insertedCharity.getName()); | ||
|
|
||
| } | ||
|
|
||
| @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 Charity( | ||
| org_number, | ||
| url, | ||
| name, | ||
| is_pre_approved, | ||
| status | ||
|
|
||
| ); | ||
|
|
||
| org_number = "23456"; | ||
| name = "SvindelKoin"; | ||
| status = "approved"; | ||
| url = "https://www.svindel.net"; | ||
| is_pre_approved = true; | ||
|
|
||
| var charity2 = new Charity( | ||
| org_number, | ||
| url, | ||
| name, | ||
| is_pre_approved, | ||
| status | ||
| ); | ||
|
|
||
| org_number = "345672"; | ||
| name = "Arme Svindlere"; | ||
| status = "approved"; | ||
| url = "https://www.armesvindlere.com"; | ||
| is_pre_approved = false; | ||
|
|
||
| var charity3 = new Charity( | ||
| org_number, | ||
| url, | ||
| name, | ||
| is_pre_approved, | ||
| status | ||
| ); | ||
|
|
||
| List<Charity> charityListBefore = new ArrayList<>(); | ||
| charityListBefore.add(charity1); | ||
| charityListBefore.add(charity2); | ||
| charityListBefore.add(charity3); | ||
|
|
||
| dbManager.addAPIDataToTable(charityListBefore); | ||
|
|
||
| List<Charity> charityListNew = new ArrayList<>(); | ||
| charityListNew.add(charity1); | ||
| charityListNew.add(charity3); | ||
|
|
||
| dbManager.addAPIDataToTable(charityListNew); | ||
|
|
||
| try (Connection conn = new DatabaseConnection().getMySqlConnection()) { | ||
| 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{ | ||
| Charity charity = new Charity( | ||
| "99999", | ||
| "https://temp.no", | ||
| "Temp Charity", | ||
| false, | ||
| "approved" | ||
| ); | ||
|
|
||
| dbManager.addAPIDataToTable(List.of(charity)); | ||
|
|
||
| try (Connection conn = new DatabaseConnection().getMySqlConnection()) { | ||
| PreparedStatement stmt = conn.prepareStatement("SELECT * FROM temp_api_charities"); | ||
|
|
||
|
|
||
| assertThrows(java.sql.SQLSyntaxErrorException.class, () -> { | ||
| ResultSet rs = stmt.executeQuery(); | ||
| }); | ||
| } | ||
| } | ||
| } |
194 changes: 0 additions & 194 deletions
194
...ehelpapplication/src/test/java/ntnu/systemutvikling/team6/models/DatabaseManagerTest.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.