From 9c8edc13d3ff717f8c829b5ca9a109eb574c0cd0 Mon Sep 17 00:00:00 2001 From: AdrianBalunan Date: Fri, 24 Apr 2026 09:42:40 +0200 Subject: [PATCH] Feat: Added and implemented MessageDAOTest --- .../team6/database/DAO/MessageDAOTest.java | 169 +++++++++++++++++- 1 file changed, 168 insertions(+), 1 deletion(-) diff --git a/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/database/DAO/MessageDAOTest.java b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/database/DAO/MessageDAOTest.java index b433eb5..7db9e7f 100644 --- a/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/database/DAO/MessageDAOTest.java +++ b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/database/DAO/MessageDAOTest.java @@ -1,4 +1,171 @@ package ntnu.systemutvikling.team6.database.DAO; +import ntnu.systemutvikling.team6.database.DatabaseConnection; +import ntnu.systemutvikling.team6.models.Charity; +import ntnu.systemutvikling.team6.models.user.Message; +import org.junit.jupiter.api.*; +import java.sql.*; +import java.time.LocalDate; +import java.util.UUID; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + public class MessageDAOTest { -} + private DatabaseConnection mockDbConnection; + private Connection mockDonorConn; // used by getDonorIdsForCharity() + private PreparedStatement mockDonorStmt; + private ResultSet mockDonorRs; + + private Connection mockInsertConn; // used by the INSERT batch + private PreparedStatement mockInsertStmt; + + private MessageDAO messageDAO; + + @BeforeEach + void setUp() throws SQLException { + mockDbConnection = mock(DatabaseConnection.class); + + mockDonorConn = mock(Connection.class); + mockDonorStmt = mock(PreparedStatement.class); + mockDonorRs = mock(ResultSet.class); + + mockInsertConn = mock(Connection.class); + mockInsertStmt = mock(PreparedStatement.class); + + + when(mockDbConnection.getMySqlConnection()) + .thenReturn(mockDonorConn) + .thenReturn(mockInsertConn); + + // Wire donor connection + when(mockDonorConn.prepareStatement(anyString())).thenReturn(mockDonorStmt); + when(mockDonorStmt.executeQuery()).thenReturn(mockDonorRs); + + // Wire insert connection + when(mockInsertConn.prepareStatement(anyString())).thenReturn(mockInsertStmt); + + messageDAO = new MessageDAO(mockDbConnection); + } + + // ---------------------------------------------------------------- + // Helpers + // ---------------------------------------------------------------- + + private Message buildTestMessage(String charityId) { + Charity charity = new Charity( + charityId, + "123456789", + "HelpOrg", + "helporg.com", + "active", + true, + "We help people", + null, + null, + null + ); + return new Message("Important Update", charity, "Things are going well.", LocalDate.now()); + } + + // ---------------------------------------------------------------- + // addMessage() + // ---------------------------------------------------------------- + + @Test + void addMessage_returnsTrueWhenDonorsExistAndBatchSucceeds() throws SQLException { + String charityId = UUID.randomUUID().toString(); + String donorId = UUID.randomUUID().toString(); + + // Simulate one donor found + when(mockDonorRs.next()).thenReturn(true, false); + when(mockDonorRs.getString("user_id")).thenReturn(donorId); + + // Batch returns one affected row + when(mockInsertStmt.executeBatch()).thenReturn(new int[]{1}); + + boolean result = messageDAO.addMessage(buildTestMessage(charityId)); + + assertTrue(result); + } + + @Test + void addMessage_returnsFalseWhenNoDonorsExist() throws SQLException { + when(mockDonorRs.next()).thenReturn(false); + + boolean result = messageDAO.addMessage(buildTestMessage(UUID.randomUUID().toString())); + + assertFalse(result); + verify(mockDbConnection, times(1)).getMySqlConnection(); + verifyNoInteractions(mockInsertConn); + } + + @Test + void addMessage_sendsOneBatchEntryPerDonor() throws SQLException { + String charityId = UUID.randomUUID().toString(); + String donorId1 = UUID.randomUUID().toString(); + String donorId2 = UUID.randomUUID().toString(); + String donorId3 = UUID.randomUUID().toString(); + + + when(mockDonorRs.next()).thenReturn(true, true, true, false); + when(mockDonorRs.getString("user_id")).thenReturn(donorId1, donorId2, donorId3); + when(mockInsertStmt.executeBatch()).thenReturn(new int[]{1, 1, 1}); + + messageDAO.addMessage(buildTestMessage(charityId)); + + verify(mockInsertStmt, times(3)).addBatch(); + verify(mockInsertStmt, times(1)).executeBatch(); + } + + @Test + void addMessage_setsCorrectCharityIdOnEveryBatchEntry() throws SQLException { + String charityId = UUID.randomUUID().toString(); + String donorId = UUID.randomUUID().toString(); + + when(mockDonorRs.next()).thenReturn(true, false); + when(mockDonorRs.getString("user_id")).thenReturn(donorId); + when(mockInsertStmt.executeBatch()).thenReturn(new int[]{1}); + + messageDAO.addMessage(buildTestMessage(charityId)); + + verify(mockInsertStmt).setString(5, charityId); + verify(mockInsertStmt).setString(6, donorId); + } + + @Test + void addMessage_throwsRuntimeExceptionWhenDonorQueryFails() throws SQLException { + when(mockDonorConn.prepareStatement(anyString())) + .thenThrow(new SQLException("Donor query failed")); + + assertThrows(RuntimeException.class, + () -> messageDAO.addMessage(buildTestMessage(UUID.randomUUID().toString()))); + } + + @Test + void addMessage_throwsRuntimeExceptionWhenBatchFails() throws SQLException { + String donorId = UUID.randomUUID().toString(); + + when(mockDonorRs.next()).thenReturn(true, false); + when(mockDonorRs.getString("user_id")).thenReturn(donorId); + + when(mockInsertConn.prepareStatement(anyString())) + .thenThrow(new SQLException("Batch insert failed")); + + assertThrows(RuntimeException.class, + () -> messageDAO.addMessage(buildTestMessage(UUID.randomUUID().toString()))); + } + + @Test + void addMessage_returnsFalseWhenBatchReturnsEmptyArray() throws SQLException { + String donorId = UUID.randomUUID().toString(); + + when(mockDonorRs.next()).thenReturn(true, false); + when(mockDonorRs.getString("user_id")).thenReturn(donorId); + + when(mockInsertStmt.executeBatch()).thenReturn(new int[]{}); + + boolean result = messageDAO.addMessage(buildTestMessage(UUID.randomUUID().toString())); + + assertFalse(result); + } +} \ No newline at end of file