From 533e89662c6f959f86cdfef70813dfdabdae27ae Mon Sep 17 00:00:00 2001 From: AdrianBalunan Date: Fri, 24 Apr 2026 09:43:44 +0200 Subject: [PATCH] Feat: Implemneted CharityUserDAOTest --- .../database/DAO/CharityUserDAOTest.java | 279 +++++++++++++++++- 1 file changed, 278 insertions(+), 1 deletion(-) diff --git a/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/database/DAO/CharityUserDAOTest.java b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/database/DAO/CharityUserDAOTest.java index a77a7ee..0d910b9 100644 --- a/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/database/DAO/CharityUserDAOTest.java +++ b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/database/DAO/CharityUserDAOTest.java @@ -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())); + } +} \ No newline at end of file