-
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: Implemneted CharityUserDAOTest
- Loading branch information
AdrianBalunan
committed
Apr 24, 2026
1 parent
7d7a214
commit 533e896
Showing
1 changed file
with
278 additions
and
1 deletion.
There are no files selected for viewing
279 changes: 278 additions & 1 deletion
279
...application/src/test/java/ntnu/systemutvikling/team6/database/DAO/CharityUserDAOTest.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 |
|---|---|---|
| @@ -1,4 +1,281 @@ | ||
| package ntnu.systemutvikling.team6.database.DAO; | ||
|
|
||
| import ntnu.systemutvikling.team6.database.DatabaseConnection; | ||
| import ntnu.systemutvikling.team6.models.Charity; | ||
| import org.junit.jupiter.api.*; | ||
| import java.sql.*; | ||
| import java.util.UUID; | ||
| import static org.junit.jupiter.api.Assertions.*; | ||
| import static org.mockito.Mockito.*; | ||
|
|
||
| public class CharityUserDAOTest { | ||
| } | ||
|
|
||
| // --- Mocks --- | ||
| private DatabaseConnection mockDbConnection; | ||
| private Connection mockConn; | ||
| private PreparedStatement mockStmt; | ||
| private ResultSet mockRs; | ||
|
|
||
| private CharityUserDAO charityUserDAO; | ||
|
|
||
| @BeforeEach | ||
| void setUp() throws SQLException { | ||
| mockDbConnection = mock(DatabaseConnection.class); | ||
| mockConn = mock(Connection.class); | ||
| mockStmt = mock(PreparedStatement.class); | ||
| mockRs = mock(ResultSet.class); | ||
|
|
||
| when(mockDbConnection.getMySqlConnection()).thenReturn(mockConn); | ||
| when(mockConn.prepareStatement(anyString())).thenReturn(mockStmt); | ||
|
|
||
| charityUserDAO = new CharityUserDAO(mockDbConnection); | ||
| } | ||
|
|
||
| // ---------------------------------------------------------------- | ||
| // Helpers | ||
| // ---------------------------------------------------------------- | ||
|
|
||
| private Charity buildTestCharity(String charityId) { | ||
| return new Charity( | ||
| charityId, | ||
| "123456789", | ||
| "HelpOrg", | ||
| "helporg.com", | ||
| "active", | ||
| true, | ||
| "We help people", | ||
| null, | ||
| null, | ||
| null | ||
| ); | ||
| } | ||
|
|
||
| private void stubFullCharityRow(String charityId) throws SQLException { | ||
| when(mockRs.getString("UUID_charities")).thenReturn(charityId); | ||
| when(mockRs.getString("org_number")).thenReturn("123456789"); | ||
| when(mockRs.getString("charity_name")).thenReturn("HelpOrg"); | ||
| when(mockRs.getString("charity_link")).thenReturn("helporg.com"); | ||
| when(mockRs.getString("status")).thenReturn("active"); | ||
| when(mockRs.getBoolean("pre_approved")).thenReturn(true); | ||
| when(mockRs.getString("description")).thenReturn("We help people"); | ||
| when(mockRs.getString("logoURL")).thenReturn(null); | ||
| when(mockRs.getString("key_values")).thenReturn(null); | ||
| when(mockRs.getBytes("logoBLOB")).thenReturn(null); | ||
| when(mockRs.getString("category")).thenReturn(null); | ||
| } | ||
|
|
||
| // ---------------------------------------------------------------- | ||
| // updateCharityVanityName() | ||
| // ---------------------------------------------------------------- | ||
|
|
||
| @Test | ||
| void updateCharityVanityName_returnsTrueOnSuccess() throws SQLException { | ||
| when(mockStmt.executeUpdate()).thenReturn(1); | ||
|
|
||
| assertTrue(charityUserDAO.updateCharityVanityName( | ||
| buildTestCharity(UUID.randomUUID().toString()) | ||
| )); | ||
| } | ||
|
|
||
| @Test | ||
| void updateCharityVanityName_returnsFalseWhenNoRowsAffected() throws SQLException { | ||
| when(mockStmt.executeUpdate()).thenReturn(0); | ||
|
|
||
| assertFalse(charityUserDAO.updateCharityVanityName( | ||
| buildTestCharity(UUID.randomUUID().toString()) | ||
| )); | ||
| } | ||
|
|
||
| @Test | ||
| void updateCharityVanityName_returnsFalseOnSQLException() throws SQLException { | ||
| when(mockStmt.executeUpdate()).thenThrow(new SQLException("Update failed")); | ||
|
|
||
| assertFalse(charityUserDAO.updateCharityVanityName( | ||
| buildTestCharity(UUID.randomUUID().toString()) | ||
| )); | ||
| } | ||
|
|
||
| @Test | ||
| void updateCharityVanityName_setsCorrectParameters() throws SQLException { | ||
| when(mockStmt.executeUpdate()).thenReturn(1); | ||
|
|
||
| String charityId = UUID.randomUUID().toString(); | ||
| Charity charity = buildTestCharity(charityId); | ||
|
|
||
| charityUserDAO.updateCharityVanityName(charity); | ||
|
|
||
| verify(mockStmt).setString(1, charity.getName()); // charity_name | ||
| verify(mockStmt).setString(2, charityId); // UUID_charity | ||
| } | ||
|
|
||
| // ---------------------------------------------------------------- | ||
| // updateCharityVanityDescription() | ||
| // ---------------------------------------------------------------- | ||
|
|
||
| @Test | ||
| void updateCharityVanityDescription_returnsTrueOnSuccess() throws SQLException { | ||
| when(mockStmt.executeUpdate()).thenReturn(1); | ||
|
|
||
| assertTrue(charityUserDAO.updateCharityVanityDescription( | ||
| buildTestCharity(UUID.randomUUID().toString()) | ||
| )); | ||
| } | ||
|
|
||
| @Test | ||
| void updateCharityVanityDescription_returnsFalseWhenNoRowsAffected() throws SQLException { | ||
| when(mockStmt.executeUpdate()).thenReturn(0); | ||
|
|
||
| assertFalse(charityUserDAO.updateCharityVanityDescription( | ||
| buildTestCharity(UUID.randomUUID().toString()) | ||
| )); | ||
| } | ||
|
|
||
| @Test | ||
| void updateCharityVanityDescription_returnsFalseOnSQLException() throws SQLException { | ||
| when(mockStmt.executeUpdate()).thenThrow(new SQLException("Update failed")); | ||
|
|
||
| assertFalse(charityUserDAO.updateCharityVanityDescription( | ||
| buildTestCharity(UUID.randomUUID().toString()) | ||
| )); | ||
| } | ||
|
|
||
| @Test | ||
| void updateCharityVanityDescription_setsCorrectParameters() throws SQLException { | ||
| when(mockStmt.executeUpdate()).thenReturn(1); | ||
|
|
||
| String charityId = UUID.randomUUID().toString(); | ||
| Charity charity = buildTestCharity(charityId); | ||
|
|
||
| charityUserDAO.updateCharityVanityDescription(charity); | ||
|
|
||
| verify(mockStmt).setString(1, charity.getDescription()); // description | ||
| verify(mockStmt).setString(2, charityId); // UUID_charity | ||
| } | ||
|
|
||
| // ---------------------------------------------------------------- | ||
| // getUserCharityUser() | ||
| // ---------------------------------------------------------------- | ||
|
|
||
| @Test | ||
| void getUserCharityUser_throwsOnNullUuid() { | ||
| assertThrows(IllegalArgumentException.class, | ||
| () -> charityUserDAO.getUserCharityUser(null)); | ||
| } | ||
|
|
||
| @Test | ||
| void getUserCharityUser_throwsOnBlankUuid() { | ||
| assertThrows(IllegalArgumentException.class, | ||
| () -> charityUserDAO.getUserCharityUser(" ")); | ||
| } | ||
|
|
||
| @Test | ||
| void getUserCharityUser_returnsCharityWhenFound() throws SQLException { | ||
| when(mockStmt.executeQuery()).thenReturn(mockRs); | ||
| when(mockRs.next()).thenReturn(true, false); | ||
|
|
||
| String charityId = UUID.randomUUID().toString(); | ||
| stubFullCharityRow(charityId); | ||
|
|
||
| Charity result = charityUserDAO.getUserCharityUser(UUID.randomUUID().toString()); | ||
|
|
||
| assertNotNull(result); | ||
| assertEquals(charityId, result.getUUID().toString()); | ||
| assertEquals("HelpOrg", result.getName()); | ||
| assertEquals("We help people", result.getDescription()); | ||
| } | ||
|
|
||
| @Test | ||
| void getUserCharityUser_returnsNullWhenNotFound() throws SQLException { | ||
| when(mockStmt.executeQuery()).thenReturn(mockRs); | ||
| when(mockRs.next()).thenReturn(false); | ||
|
|
||
| Charity result = charityUserDAO.getUserCharityUser(UUID.randomUUID().toString()); | ||
|
|
||
| assertNull(result); | ||
| } | ||
|
|
||
| @Test | ||
| void getUserCharityUser_accumulatesCategoriesAcrossRows() throws SQLException { | ||
| when(mockStmt.executeQuery()).thenReturn(mockRs); | ||
| when(mockRs.next()).thenReturn(true, true, true, false); | ||
|
|
||
| String charityId = UUID.randomUUID().toString(); | ||
| // Same charity across all rows, different category each time | ||
| when(mockRs.getString("UUID_charities")).thenReturn(charityId); | ||
| when(mockRs.getString("org_number")).thenReturn("123456789"); | ||
| when(mockRs.getString("charity_name")).thenReturn("HelpOrg"); | ||
| when(mockRs.getString("charity_link")).thenReturn("helporg.com"); | ||
| when(mockRs.getString("status")).thenReturn("active"); | ||
| when(mockRs.getBoolean("pre_approved")).thenReturn(true); | ||
| when(mockRs.getString("description")).thenReturn("We help"); | ||
| when(mockRs.getString("logoURL")).thenReturn(null); | ||
| when(mockRs.getString("key_values")).thenReturn(null); | ||
| when(mockRs.getBytes("logoBLOB")).thenReturn(null); | ||
| when(mockRs.getString("category")).thenReturn("Education", "Youth", "Health"); | ||
|
|
||
| Charity result = charityUserDAO.getUserCharityUser(UUID.randomUUID().toString()); | ||
|
|
||
| assertNotNull(result); | ||
| assertEquals(3, result.getCategory().size()); | ||
| assertTrue(result.getCategory().contains("Education")); | ||
| assertTrue(result.getCategory().contains("Youth")); | ||
| assertTrue(result.getCategory().contains("Health")); | ||
| } | ||
|
|
||
| @Test | ||
| void getUserCharityUser_doesNotAddDuplicateCategories() throws SQLException { | ||
| when(mockStmt.executeQuery()).thenReturn(mockRs); | ||
| when(mockRs.next()).thenReturn(true, true, false); | ||
|
|
||
| String charityId = UUID.randomUUID().toString(); | ||
| when(mockRs.getString("UUID_charities")).thenReturn(charityId); | ||
| when(mockRs.getString("org_number")).thenReturn("111"); | ||
| when(mockRs.getString("charity_name")).thenReturn("Org"); | ||
| when(mockRs.getString("charity_link")).thenReturn("org.com"); | ||
| when(mockRs.getString("status")).thenReturn("active"); | ||
| when(mockRs.getBoolean("pre_approved")).thenReturn(false); | ||
| when(mockRs.getString("description")).thenReturn("desc"); | ||
| when(mockRs.getString("logoURL")).thenReturn(null); | ||
| when(mockRs.getString("key_values")).thenReturn(null); | ||
| when(mockRs.getBytes("logoBLOB")).thenReturn(null); | ||
| // Same category name twice — should only be added once | ||
| when(mockRs.getString("category")).thenReturn("Health", "Health"); | ||
|
|
||
| Charity result = charityUserDAO.getUserCharityUser(UUID.randomUUID().toString()); | ||
|
|
||
| assertEquals(1, result.getCategory().size()); | ||
| } | ||
|
|
||
| @Test | ||
| void getUserCharityUser_ignoresNullCategory() throws SQLException { | ||
| when(mockStmt.executeQuery()).thenReturn(mockRs); | ||
| when(mockRs.next()).thenReturn(true, false); | ||
|
|
||
| String charityId = UUID.randomUUID().toString(); | ||
| stubFullCharityRow(charityId); // category stubbed as null | ||
|
|
||
| Charity result = charityUserDAO.getUserCharityUser(UUID.randomUUID().toString()); | ||
|
|
||
| assertNotNull(result); | ||
| assertTrue(result.getCategory().isEmpty()); | ||
| } | ||
|
|
||
| @Test | ||
| void getUserCharityUser_passesCorrectUuidToQuery() throws SQLException { | ||
| when(mockStmt.executeQuery()).thenReturn(mockRs); | ||
| when(mockRs.next()).thenReturn(false); | ||
|
|
||
| String userId = UUID.randomUUID().toString(); | ||
| charityUserDAO.getUserCharityUser(userId); | ||
|
|
||
| verify(mockStmt).setString(1, userId); | ||
| } | ||
|
|
||
| @Test | ||
| void getUserCharityUser_throwsRuntimeExceptionOnSQLException() throws SQLException { | ||
| when(mockConn.prepareStatement(anyString())).thenThrow(new SQLException("DB down")); | ||
|
|
||
| assertThrows(RuntimeException.class, | ||
| () -> charityUserDAO.getUserCharityUser(UUID.randomUUID().toString())); | ||
| } | ||
| } |