-
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: Added and implemented MessageDAOTest
- Loading branch information
AdrianBalunan
committed
Apr 24, 2026
1 parent
8a6dde7
commit 9c8edc1
Showing
1 changed file
with
168 additions
and
1 deletion.
There are no files selected for viewing
169 changes: 168 additions & 1 deletion
169
...helpapplication/src/test/java/ntnu/systemutvikling/team6/database/DAO/MessageDAOTest.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,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); | ||
| } | ||
| } |