diff --git a/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/database/DAO/CategoryDAOTest.java b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/database/DAO/CategoryDAOTest.java new file mode 100644 index 0000000..90c8028 --- /dev/null +++ b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/database/DAO/CategoryDAOTest.java @@ -0,0 +1,4 @@ +package ntnu.systemutvikling.team6.database.DAO; + +public class CategoryDAOTest { +} diff --git a/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/database/DAO/CharityDAOTest.java b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/database/DAO/CharityDAOTest.java new file mode 100644 index 0000000..4d5cf8b --- /dev/null +++ b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/database/DAO/CharityDAOTest.java @@ -0,0 +1,4 @@ +package ntnu.systemutvikling.team6.database.DAO; + +public class CharityDAOTest { +} 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 new file mode 100644 index 0000000..a77a7ee --- /dev/null +++ b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/database/DAO/CharityUserDAOTest.java @@ -0,0 +1,4 @@ +package ntnu.systemutvikling.team6.database.DAO; + +public class CharityUserDAOTest { +} diff --git a/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/database/DAO/FavouritesDAOTest.java b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/database/DAO/FavouritesDAOTest.java new file mode 100644 index 0000000..80b9d09 --- /dev/null +++ b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/database/DAO/FavouritesDAOTest.java @@ -0,0 +1,4 @@ +package ntnu.systemutvikling.team6.database.DAO; + +public class FavouritesDAOTest { +} diff --git a/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/database/DAO/FeedbackDAOTest.java b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/database/DAO/FeedbackDAOTest.java new file mode 100644 index 0000000..34b2e75 --- /dev/null +++ b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/database/DAO/FeedbackDAOTest.java @@ -0,0 +1,5 @@ +package ntnu.systemutvikling.team6.database.DAO; + +public class FeedbackDAOTest +{ +} 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 new file mode 100644 index 0000000..b433eb5 --- /dev/null +++ b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/database/DAO/MessageDAOTest.java @@ -0,0 +1,4 @@ +package ntnu.systemutvikling.team6.database.DAO; + +public class MessageDAOTest { +} diff --git a/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/database/DAO/UserDAOTest.java b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/database/DAO/UserDAOTest.java new file mode 100644 index 0000000..066fc9c --- /dev/null +++ b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/database/DAO/UserDAOTest.java @@ -0,0 +1,4 @@ +package ntnu.systemutvikling.team6.database.DAO; + +public class UserDAOTest { +} diff --git a/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/database/Readers/CharitySelectTest.java b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/database/Readers/CharitySelectTest.java deleted file mode 100644 index e116a63..0000000 --- a/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/database/Readers/CharitySelectTest.java +++ /dev/null @@ -1,331 +0,0 @@ -package ntnu.systemutvikling.team6.database.Readers; - -import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.*; - -import java.sql.*; -import java.util.ArrayList; -import java.util.UUID; - -import ntnu.systemutvikling.team6.database.DAO.CharityDAO; -import ntnu.systemutvikling.team6.database.DatabaseConnection; -import ntnu.systemutvikling.team6.models.Charity; -import ntnu.systemutvikling.team6.models.Feedback; -import ntnu.systemutvikling.team6.models.registry.CharityRegistry; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; - -/** - * Unit tests for {@link CharityDAO}. - * - *

Uses Mockito to mock {@link DatabaseConnection}, {@link Connection}, {@link Statement}, {@link - * PreparedStatement}, and {@link ResultSet} so that no real database connection is required. - */ -@ExtendWith(MockitoExtension.class) -class CharitySelectTest { - - @Mock private DatabaseConnection mockDatabaseConnection; - @Mock private Connection mockConnection; - @Mock private Statement mockStatement; - @Mock private PreparedStatement mockPreparedStatement; - @Mock private ResultSet mockResultSet; - - private CharityDAO charitySelect; - - @BeforeEach - void setUp() { - charitySelect = new CharityDAO(mockDatabaseConnection); - } - - // ------------------------------------------------------------------------- - // getCharitiesFromDB - // ------------------------------------------------------------------------- - - @Test - @DisplayName("getCharitiesFromDB – empty result set returns an empty registry") - void getCharitiesFromDB_emptyResultSet_returnsEmptyRegistry() throws Exception { - when(mockDatabaseConnection.getMySqlConnection()).thenReturn(mockConnection); - when(mockConnection.createStatement()).thenReturn(mockStatement); - when(mockStatement.executeQuery(anyString())).thenReturn(mockResultSet); - when(mockResultSet.next()).thenReturn(false); - - CharityRegistry registry = charitySelect.getCharitiesFromDB(); - - assertNotNull(registry); - assertTrue( - registry.getAllCharities().isEmpty(), - "Registry should contain no charities when the result set is empty"); - } - - @Test - @DisplayName("getCharitiesFromDB – single charity with no feedback is added once") - void getCharitiesFromDB_singleCharityNoFeedback_addedOnce() throws Exception { - when(mockDatabaseConnection.getMySqlConnection()).thenReturn(mockConnection); - when(mockConnection.createStatement()).thenReturn(mockStatement); - when(mockStatement.executeQuery(anyString())).thenReturn(mockResultSet); - - // One row, no feedback - when(mockResultSet.next()).thenReturn(true, false); - String charityId = UUID.randomUUID().toString(); - when(mockResultSet.getString("UUID_charities")).thenReturn(charityId); - when(mockResultSet.getString("org_number")).thenReturn("123456789"); - when(mockResultSet.getString("charity_link")).thenReturn("https://example.org"); - when(mockResultSet.getString("charity_name")).thenReturn("Test Charity"); - when(mockResultSet.getBoolean("pre_approved")).thenReturn(true); - when(mockResultSet.getString("status")).thenReturn("ACTIVE"); - when(mockResultSet.getString("description")).thenReturn("Some description"); - when(mockResultSet.getString("logoURL")).thenReturn("https://logo.png"); - when(mockResultSet.getString("key_values")).thenReturn("80:10:90"); - when(mockResultSet.getBytes("logoBLOB")).thenReturn(null); - when(mockResultSet.getString("category")).thenReturn(null); - - when(mockResultSet.getString("UUID_feedback")).thenReturn(null); - - CharityRegistry registry = charitySelect.getCharitiesFromDB(); - - assertEquals( - 1, registry.getAllCharities().size(), "Registry should contain exactly one charity"); - Charity charity = registry.getAllCharities().get(0); - assertEquals("Test Charity", charity.getName()); - assertTrue(charity.getFeedbacks().isEmpty(), "Charity should have no feedback"); - } - - @Test - @DisplayName("getCharitiesFromDB – single charity with one feedback entry is populated correctly") - void getCharitiesFromDB_singleCharityWithFeedback_feedbackAdded() throws Exception { - when(mockDatabaseConnection.getMySqlConnection()).thenReturn(mockConnection); - when(mockConnection.createStatement()).thenReturn(mockStatement); - when(mockStatement.executeQuery(anyString())).thenReturn(mockResultSet); - - // One row with feedback - when(mockResultSet.next()).thenReturn(true, false); - String charityId = UUID.randomUUID().toString(); - when(mockResultSet.getString("UUID_charities")).thenReturn(charityId); - when(mockResultSet.getString("org_number")).thenReturn("123456789"); - when(mockResultSet.getString("charity_link")).thenReturn("https://example.org"); - when(mockResultSet.getString("charity_name")).thenReturn("Test Charity"); - when(mockResultSet.getBoolean("pre_approved")).thenReturn(false); - when(mockResultSet.getString("status")).thenReturn("PENDING"); - when(mockResultSet.getString("description")).thenReturn("Some description"); - when(mockResultSet.getString("logoURL")).thenReturn("https://logo.png"); - when(mockResultSet.getString("key_values")).thenReturn("80:10:90"); - when(mockResultSet.getBytes("logoBLOB")).thenReturn(null); - when(mockResultSet.getString("category")).thenReturn(null); - - String feedback1Id = UUID.randomUUID().toString(); - String userId = UUID.randomUUID().toString(); - when(mockResultSet.getString("UUID_feedback")).thenReturn(feedback1Id); - when(mockResultSet.getString("UUID_User")).thenReturn(userId); - when(mockResultSet.getString("user_name")).thenReturn("Alice"); - when(mockResultSet.getString("user_email")).thenReturn("alice@example.com"); - when(mockResultSet.getString("user_password")).thenReturn("hashedpw"); - when(mockResultSet.getString("role")).thenReturn("NORMAL_USER"); - when(mockResultSet.getString("feedback_comment")).thenReturn("Great work!"); - when(mockResultSet.getString("feedback_date")).thenReturn("2024-03-15"); - - CharityRegistry registry = charitySelect.getCharitiesFromDB(); - - assertEquals(1, registry.getAllCharities().size()); - Charity charity = registry.getAllCharities().get(0); - assertEquals( - 1, charity.getFeedbacks().size(), "Charity should have exactly one feedback entry"); - - Feedback feedback = charity.getFeedbacks().get(0); - assertEquals(feedback1Id, feedback.getFeedbackId().toString()); - assertEquals("Great work!", feedback.getComment()); - } - - @Test - @DisplayName("getCharitiesFromDB – two different charities across two rows are both added") - void getCharitiesFromDB_twoCharities_bothAdded() throws Exception { - when(mockDatabaseConnection.getMySqlConnection()).thenReturn(mockConnection); - when(mockConnection.createStatement()).thenReturn(mockStatement); - when(mockStatement.executeQuery(anyString())).thenReturn(mockResultSet); - - // First row: charity A, no feedback - // Second row: charity B, no feedback - when(mockResultSet.next()).thenReturn(true, true, false); - String charityId = UUID.randomUUID().toString(); - String charityId2 = UUID.randomUUID().toString(); - - when(mockResultSet.getString("UUID_charities")).thenReturn(charityId, charityId2); - when(mockResultSet.getString("org_number")).thenReturn("111111111", "222222222"); - when(mockResultSet.getString("charity_link")).thenReturn("https://a.org", "https://b.org"); - when(mockResultSet.getString("charity_name")).thenReturn("Charity A", "Charity B"); - when(mockResultSet.getBoolean("pre_approved")).thenReturn(true, false); - when(mockResultSet.getString("status")).thenReturn("ACTIVE", "INACTIVE"); - when(mockResultSet.getString("description")).thenReturn("Some description"); - when(mockResultSet.getString("logoURL")).thenReturn("https://logo.png"); - when(mockResultSet.getString("key_values")).thenReturn("80:10:90"); - when(mockResultSet.getBytes("logoBLOB")).thenReturn(null); - when(mockResultSet.getString("category")).thenReturn(null); - - when(mockResultSet.getString("UUID_feedback")).thenReturn(null, null); - - CharityRegistry registry = charitySelect.getCharitiesFromDB(); - - assertEquals(2, registry.getAllCharities().size(), "Registry should contain two charities"); - } - - @Test - @DisplayName( - "getCharitiesFromDB – same charity UUID across two rows adds feedback without duplicating the charity") - void getCharitiesFromDB_sameCharityTwoRows_onlyOneCharityWithTwoFeedbacks() throws Exception { - when(mockDatabaseConnection.getMySqlConnection()).thenReturn(mockConnection); - when(mockConnection.createStatement()).thenReturn(mockStatement); - when(mockStatement.executeQuery(anyString())).thenReturn(mockResultSet); - - when(mockResultSet.next()).thenReturn(true, true, false); - // Both rows share the same charity UUID - String charityId = UUID.randomUUID().toString(); - when(mockResultSet.getString("UUID_charities")).thenReturn(charityId); - when(mockResultSet.getString("org_number")).thenReturn("123456789"); - when(mockResultSet.getString("charity_link")).thenReturn("https://example.org"); - when(mockResultSet.getString("charity_name")).thenReturn("Test Charity"); - when(mockResultSet.getBoolean("pre_approved")).thenReturn(true); - when(mockResultSet.getString("status")).thenReturn("ACTIVE"); - when(mockResultSet.getString("description")).thenReturn("Some description"); - when(mockResultSet.getString("logoURL")).thenReturn("https://logo.png"); - when(mockResultSet.getString("key_values")).thenReturn("80:10:90"); - when(mockResultSet.getBytes("logoBLOB")).thenReturn(null); - when(mockResultSet.getString("category")).thenReturn(null); - - String feedback1Id = UUID.randomUUID().toString(); - String feedback2Id = UUID.randomUUID().toString(); - String userId = UUID.randomUUID().toString(); - when(mockResultSet.getString("UUID_feedback")).thenReturn(feedback1Id, feedback2Id); - when(mockResultSet.getString("UUID_User")).thenReturn(userId); - when(mockResultSet.getString("user_name")).thenReturn("Alice"); - when(mockResultSet.getString("user_email")).thenReturn("alice@example.com"); - when(mockResultSet.getString("user_password")).thenReturn("hashedpw"); - when(mockResultSet.getString("role")).thenReturn("NORMAL_USER"); - when(mockResultSet.getString("feedback_comment")).thenReturn("First comment", "Second comment"); - when(mockResultSet.getString("feedback_date")).thenReturn("2024-03-15"); - - CharityRegistry registry = charitySelect.getCharitiesFromDB(); - - assertEquals(1, registry.getAllCharities().size(), "The same charity should not be duplicated"); - assertEquals( - 2, - registry.getAllCharities().get(0).getFeedbacks().size(), - "Both feedback entries should be attached to the single charity"); - } - - @Test - @DisplayName("getCharitiesFromDB – SQLException is wrapped in RuntimeException") - void getCharitiesFromDB_sqlException_throwsRuntimeException() throws Exception { - when(mockDatabaseConnection.getMySqlConnection()).thenReturn(mockConnection); - when(mockConnection.createStatement()).thenThrow(new SQLException("DB error")); - - assertThrows( - RuntimeException.class, - () -> charitySelect.getCharitiesFromDB(), - "A SQLException should be rethrown as a RuntimeException"); - } - - // ------------------------------------------------------------------------- - // getFeedbackforCharityUUID - // ------------------------------------------------------------------------- - - @Test - @DisplayName("getFeedbackforCharityUUID – empty result set returns empty list") - void getFeedbackforCharityUUID_emptyResultSet_returnsEmptyList() throws Exception { - when(mockDatabaseConnection.getMySqlConnection()).thenReturn(mockConnection); - when(mockConnection.prepareStatement(anyString())).thenReturn(mockPreparedStatement); - when(mockPreparedStatement.executeQuery()).thenReturn(mockResultSet); - when(mockResultSet.next()).thenReturn(false); - - ArrayList result = charitySelect.getFeedbackforCharityUUID("charity-uuid-1"); - - assertNotNull(result); - assertTrue( - result.isEmpty(), "Should return an empty list when no feedback exists for the given UUID"); - } - - @Test - @DisplayName("getFeedbackforCharityUUID – one row returns one Feedback with correct data") - void getFeedbackforCharityUUID_oneRow_returnsSingleFeedback() throws Exception { - when(mockDatabaseConnection.getMySqlConnection()).thenReturn(mockConnection); - when(mockConnection.prepareStatement(anyString())).thenReturn(mockPreparedStatement); - when(mockPreparedStatement.executeQuery()).thenReturn(mockResultSet); - - when(mockResultSet.next()).thenReturn(true, false); - String feedback1Id = UUID.randomUUID().toString(); - when(mockResultSet.getString("UUID_feedback")).thenReturn(feedback1Id); - String userId = UUID.randomUUID().toString(); - when(mockResultSet.getString("UUID_User")).thenReturn(userId); - when(mockResultSet.getString("user_name")).thenReturn("Bob"); - when(mockResultSet.getString("user_email")).thenReturn("bob@example.com"); - when(mockResultSet.getString("user_password")).thenReturn("secret"); - when(mockResultSet.getString("role")).thenReturn("CHARITY_USER"); - when(mockResultSet.getString("feedback_comment")).thenReturn("Very helpful!"); - when(mockResultSet.getString("feedback_date")).thenReturn("2024-06-01"); - String charityId = UUID.randomUUID().toString(); - ArrayList result = charitySelect.getFeedbackforCharityUUID(charityId); - - assertEquals(1, result.size()); - Feedback feedback = result.get(0); - assertEquals(feedback1Id, feedback.getFeedbackId().toString()); - assertEquals("Very helpful!", feedback.getComment()); - } - - @Test - @DisplayName("getFeedbackforCharityUUID – two rows returns two Feedback objects") - void getFeedbackforCharityUUID_twoRows_returnsTwoFeedbacks() throws Exception { - when(mockDatabaseConnection.getMySqlConnection()).thenReturn(mockConnection); - when(mockConnection.prepareStatement(anyString())).thenReturn(mockPreparedStatement); - when(mockPreparedStatement.executeQuery()).thenReturn(mockResultSet); - - when(mockResultSet.next()).thenReturn(true, true, false); - String feedback1Id = UUID.randomUUID().toString(); - String feedback2Id = UUID.randomUUID().toString(); - when(mockResultSet.getString("UUID_feedback")).thenReturn(feedback1Id, feedback2Id); - String userid = UUID.randomUUID().toString(); - - when(mockResultSet.getString("UUID_User")).thenReturn(userid); - when(mockResultSet.getString("user_name")).thenReturn("Carol"); - when(mockResultSet.getString("user_email")).thenReturn("carol@example.com"); - when(mockResultSet.getString("user_password")).thenReturn("pw"); - when(mockResultSet.getString("role")).thenReturn("NORMAL_USER"); - when(mockResultSet.getString("feedback_comment")).thenReturn("Comment one", "Comment two"); - when(mockResultSet.getString("feedback_date")).thenReturn("2024-07-10"); - - ArrayList result = charitySelect.getFeedbackforCharityUUID("charity-uuid-1"); - - assertEquals( - 2, result.size(), "Should return exactly two Feedback objects for two result rows"); - } - - @Test - @DisplayName("getFeedbackforCharityUUID – UUID is bound to the PreparedStatement parameter") - void getFeedbackforCharityUUID_correctUUIDBindingVerified() throws Exception { - String targetUuid = "charity-uuid-XYZ"; - - when(mockDatabaseConnection.getMySqlConnection()).thenReturn(mockConnection); - when(mockConnection.prepareStatement(anyString())).thenReturn(mockPreparedStatement); - when(mockPreparedStatement.executeQuery()).thenReturn(mockResultSet); - when(mockResultSet.next()).thenReturn(false); - - charitySelect.getFeedbackforCharityUUID(targetUuid); - - verify(mockPreparedStatement).setString(1, targetUuid); - } - - @Test - @DisplayName("getFeedbackforCharityUUID – exception during query is wrapped in RuntimeException") - void getFeedbackforCharityUUID_exception_throwsRuntimeException() throws Exception { - when(mockDatabaseConnection.getMySqlConnection()).thenReturn(mockConnection); - when(mockConnection.prepareStatement(anyString())) - .thenThrow(new SQLException("Prepared statement failed")); - - assertThrows( - RuntimeException.class, - () -> charitySelect.getFeedbackforCharityUUID("charity-uuid-1"), - "Any exception during query execution should be rethrown as RuntimeException"); - } -} diff --git a/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/database/Readers/DonationSelectTest.java b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/database/Readers/DonationSelectTest.java deleted file mode 100644 index 5879231..0000000 --- a/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/database/Readers/DonationSelectTest.java +++ /dev/null @@ -1,243 +0,0 @@ -package ntnu.systemutvikling.team6.database.Readers; - -import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.*; - -import java.sql.*; -import java.time.LocalDate; -import java.util.UUID; -import ntnu.systemutvikling.team6.database.DatabaseConnection; -import ntnu.systemutvikling.team6.models.Donation; -import ntnu.systemutvikling.team6.models.registry.DonationRegistry; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; - -/** - * Unit tests for {@link DonationSelect}. - * - *

Uses Mockito to mock the entire JDBC stack so no real database connection is required. - */ -@ExtendWith(MockitoExtension.class) -class DonationSelectTest { - - @Mock private DatabaseConnection mockDatabaseConnection; - @Mock private Connection mockConnection; - @Mock private Statement mockStatement; - @Mock private ResultSet mockResultSet; - @Mock private Date mockSqlDate; - - private DonationSelect donationSelect; - - @BeforeEach - void setUp() { - donationSelect = new DonationSelect(mockDatabaseConnection); - } - - // ------------------------------------------------------------------------- - // getDonationFromDB - // ------------------------------------------------------------------------- - - @Test - @DisplayName("getDonationFromDB – empty result set returns an empty registry") - void getDonationFromDB_emptyResultSet_returnsEmptyRegistry() throws Exception { - when(mockDatabaseConnection.getMySqlConnection()).thenReturn(mockConnection); - when(mockConnection.createStatement()).thenReturn(mockStatement); - when(mockStatement.executeQuery(anyString())).thenReturn(mockResultSet); - when(mockResultSet.next()).thenReturn(false); - - DonationRegistry registry = donationSelect.getDonationFromDB(); - - assertNotNull(registry); - assertTrue( - registry.getAllDonations().isEmpty(), - "Registry should be empty when the result set has no rows"); - } - - @Test - @DisplayName("getDonationFromDB – single row returns one Donation with correct data") - void getDonationFromDB_singleRow_returnsSingleDonation() throws Exception { - when(mockDatabaseConnection.getMySqlConnection()).thenReturn(mockConnection); - when(mockConnection.createStatement()).thenReturn(mockStatement); - when(mockStatement.executeQuery(anyString())).thenReturn(mockResultSet); - - when(mockResultSet.next()).thenReturn(true, false); - String charityId = UUID.randomUUID().toString(); - stubCharityColumns( - charityId, "123456789", "Test Charity", "https://example.org", true, "ACTIVE"); - String userId = UUID.randomUUID().toString(); - String donationId = UUID.randomUUID().toString(); - stubDonationColumns(donationId, 250.0, LocalDate.of(2024, 5, 20), userId); - - DonationRegistry registry = donationSelect.getDonationFromDB(); - - assertEquals(1, registry.getAllDonations().size()); - Donation donation = registry.getAllDonations().get(0); - assertEquals(donationId, donation.getDonationID().toString()); - assertEquals(250.0, donation.getAmount()); - assertEquals(LocalDate.of(2024, 5, 20), donation.getDate()); - } - - @Test - @DisplayName("getDonationFromDB – single row maps charity fields onto the Donation correctly") - void getDonationFromDB_singleRow_charityMappedCorrectly() throws Exception { - when(mockDatabaseConnection.getMySqlConnection()).thenReturn(mockConnection); - when(mockConnection.createStatement()).thenReturn(mockStatement); - when(mockStatement.executeQuery(anyString())).thenReturn(mockResultSet); - - when(mockResultSet.next()).thenReturn(true, false); - String charityId = UUID.randomUUID().toString(); - String donationId = UUID.randomUUID().toString(); - stubCharityColumns( - charityId, "987654321", "Help Fund", "https://helpfund.org", false, "PENDING"); - String userId = UUID.randomUUID().toString(); - stubDonationColumns(donationId, 100.0, LocalDate.of(2024, 1, 1), userId); - - DonationRegistry registry = donationSelect.getDonationFromDB(); - - Donation donation = registry.getAllDonations().get(0); - assertEquals(charityId, donation.getCharityId().toString()); - assertEquals("987654321", donation.getCharity().getOrg_number()); - assertFalse(donation.getCharity().getPreApproved()); - assertEquals("PENDING", donation.getCharity().getStatus()); - } - - @Test - @DisplayName("getDonationFromDB – two rows returns two Donation objects") - void getDonationFromDB_twoRows_returnsTwoDonations() throws Exception { - when(mockDatabaseConnection.getMySqlConnection()).thenReturn(mockConnection); - when(mockConnection.createStatement()).thenReturn(mockStatement); - when(mockStatement.executeQuery(anyString())).thenReturn(mockResultSet); - - when(mockResultSet.next()).thenReturn(true, true, false); - - String chairtyId = UUID.randomUUID().toString(); - String chairtyId2 = UUID.randomUUID().toString(); - when(mockResultSet.getString("UUID_charities")).thenReturn(chairtyId, chairtyId2); - when(mockResultSet.getString("org_number")).thenReturn("111111111", "222222222"); - when(mockResultSet.getBoolean("pre_approved")).thenReturn(true, false); - when(mockResultSet.getString("status")).thenReturn("ACTIVE", "INACTIVE"); - String donationId = UUID.randomUUID().toString(); - String donationId2 = UUID.randomUUID().toString(); - when(mockResultSet.getString("UUID_Donations")).thenReturn(donationId, donationId2); - when(mockResultSet.getDouble("amount")).thenReturn(500.0, 750.0); - - Date sqlDate = Date.valueOf(LocalDate.of(2024, 8, 10)); - when(mockResultSet.getDate("date")).thenReturn(sqlDate); - - String userId = UUID.randomUUID().toString(); - when(mockResultSet.getString("UUID_User")).thenReturn(userId); - when(mockResultSet.getString("user_name")).thenReturn("Test User"); - when(mockResultSet.getString("user_email")).thenReturn("test@example.com"); - when(mockResultSet.getString("user_password")).thenReturn("password"); - when(mockResultSet.getString("role")).thenReturn("NORMAL_USER"); - - DonationRegistry registry = donationSelect.getDonationFromDB(); - - assertEquals( - 2, - registry.getAllDonations().size(), - "Registry should contain two donations for two result rows"); - } - - @Test - @DisplayName("getDonationFromDB – donation amount of zero is stored correctly") - void getDonationFromDB_zeroAmount_storedCorrectly() throws Exception { - when(mockDatabaseConnection.getMySqlConnection()).thenReturn(mockConnection); - when(mockConnection.createStatement()).thenReturn(mockStatement); - when(mockStatement.executeQuery(anyString())).thenReturn(mockResultSet); - - String charityId = UUID.randomUUID().toString(); - when(mockResultSet.next()).thenReturn(true, false); - stubCharityColumns( - charityId, "123456789", "Test Charity", "https://example.org", true, "ACTIVE"); - String userId = UUID.randomUUID().toString(); - - String donationId = UUID.randomUUID().toString(); - stubDonationColumns(donationId, 0.0, LocalDate.of(2024, 1, 1), userId); - - DonationRegistry registry = donationSelect.getDonationFromDB(); - - assertEquals(0.0, registry.getAllDonations().get(0).getAmount()); - } - - @Test - @DisplayName("getDonationFromDB – large donation amount is stored correctly") - void getDonationFromDB_largeAmount_storedCorrectly() throws Exception { - when(mockDatabaseConnection.getMySqlConnection()).thenReturn(mockConnection); - when(mockConnection.createStatement()).thenReturn(mockStatement); - when(mockStatement.executeQuery(anyString())).thenReturn(mockResultSet); - - when(mockResultSet.next()).thenReturn(true, false); - String charityId = UUID.randomUUID().toString(); - String donationId = UUID.randomUUID().toString(); - stubCharityColumns( - charityId, "123456789", "Test Charity", "https://example.org", true, "ACTIVE"); - String userId = UUID.randomUUID().toString(); - stubDonationColumns(donationId, 1_000_000.99, LocalDate.of(2024, 12, 31), userId); - - DonationRegistry registry = donationSelect.getDonationFromDB(); - - assertEquals(1_000_000.99, registry.getAllDonations().get(0).getAmount(), 0.001); - } - - @Test - @DisplayName("getDonationFromDB – SQLException is wrapped in RuntimeException") - void getDonationFromDB_sqlException_throwsRuntimeException() throws Exception { - when(mockDatabaseConnection.getMySqlConnection()).thenReturn(mockConnection); - when(mockConnection.createStatement()).thenThrow(new SQLException("DB error")); - - assertThrows( - RuntimeException.class, - () -> donationSelect.getDonationFromDB(), - "A SQLException should be rethrown as a RuntimeException"); - } - - @Test - @DisplayName("getDonationFromDB – RuntimeException message contains expected error text") - void getDonationFromDB_sqlException_runtimeExceptionHasExpectedMessage() throws Exception { - when(mockDatabaseConnection.getMySqlConnection()).thenReturn(mockConnection); - when(mockConnection.createStatement()).thenThrow(new SQLException("DB error")); - - RuntimeException ex = - assertThrows(RuntimeException.class, () -> donationSelect.getDonationFromDB()); - assertTrue( - ex.getMessage().contains("ERROR"), "RuntimeException message should contain 'ERROR'"); - } - - // ------------------------------------------------------------------------- - // Helpers - // ------------------------------------------------------------------------- - - /** Stubs all charity-related columns on the mock ResultSet. */ - private void stubCharityColumns( - String uuid, String orgNumber, String name, String link, boolean preApproved, String status) - throws SQLException { - when(mockResultSet.getString("UUID_charities")).thenReturn(uuid); - when(mockResultSet.getString("org_number")).thenReturn(orgNumber); - when(mockResultSet.getBoolean("pre_approved")).thenReturn(preApproved); - when(mockResultSet.getString("status")).thenReturn(status); - } - - /** Stubs all donation-related columns on the mock ResultSet. */ - private void stubDonationColumns(String uuid, double amount, LocalDate date, String userId) - throws SQLException { - when(mockResultSet.getString("UUID_Donations")).thenReturn(uuid); - when(mockResultSet.getDouble("amount")).thenReturn(amount); - when(mockResultSet.getBoolean("isAnonymous")).thenReturn(false); - - Date sqlDate = Date.valueOf(date); - when(mockResultSet.getDate("date")).thenReturn(sqlDate); - - // User fields - when(mockResultSet.getString("UUID_User")).thenReturn(userId); - when(mockResultSet.getString("user_name")).thenReturn("Test User"); - when(mockResultSet.getString("user_email")).thenReturn("test@example.com"); - when(mockResultSet.getString("user_password")).thenReturn("password"); - when(mockResultSet.getString("role")).thenReturn("NORMAL_USER"); - } -} diff --git a/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/database/Readers/UserSelectTest.java b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/database/Readers/UserSelectTest.java deleted file mode 100644 index e408e02..0000000 --- a/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/database/Readers/UserSelectTest.java +++ /dev/null @@ -1,409 +0,0 @@ -package ntnu.systemutvikling.team6.database.Readers; - -import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.*; - -import java.sql.*; -import java.util.UUID; -import ntnu.systemutvikling.team6.database.DatabaseConnection; -import ntnu.systemutvikling.team6.models.registry.UserRegistry; -import ntnu.systemutvikling.team6.models.user.*; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; - -/** - * Unit tests for {@link UserSelect}. - * - *

Uses Mockito to mock the entire JDBC stack ({@link DatabaseConnection}, {@link Connection}, - * {@link Statement}, {@link PreparedStatement}, {@link ResultSet}) so that no real database - * connection is required. - */ -@ExtendWith(MockitoExtension.class) -class UserSelectTest { - - @Mock private DatabaseConnection mockDatabaseConnection; - @Mock private Connection mockConnection; - @Mock private Statement mockStatement; - @Mock private PreparedStatement mockPreparedStatement; - @Mock private ResultSet mockResultSet; - - private static final String USER_UUID = UUID.randomUUID().toString(); - private static final String CHARITY_UUID = UUID.randomUUID().toString(); - private static final String MESSAGE_UUID = "msg-uuid-1"; - - private UserSelect userSelect; - - @BeforeEach - void setUp() { - reset(mockResultSet); - userSelect = new UserSelect(mockDatabaseConnection); - } - - // ------------------------------------------------------------------------- - // getUserFromDBUuid - // ------------------------------------------------------------------------- - - @Test - @DisplayName("getUserFromDBUuid – no matching row returns null") - void getUserFromDBUuid_noRow_returnsNull() throws Exception { - when(mockDatabaseConnection.getMySqlConnection()).thenReturn(mockConnection); - when(mockConnection.prepareStatement(anyString())).thenReturn(mockPreparedStatement); - when(mockPreparedStatement.executeQuery()).thenReturn(mockResultSet); - when(mockResultSet.next()).thenReturn(false); - - User result = userSelect.getUserFromDBUuid(USER_UUID); - - assertNull(result, "Should return null when no user is found"); - } - - @Test - @DisplayName("getUserFromDBUuid – single row without settings returns User with null settings") - void getUserFromDBUuid_noSettings_userSettingsNull() throws Exception { - when(mockDatabaseConnection.getMySqlConnection()).thenReturn(mockConnection); - when(mockConnection.prepareStatement(anyString())).thenReturn(mockPreparedStatement); - when(mockPreparedStatement.executeQuery()).thenReturn(mockResultSet); - - when(mockResultSet.next()).thenReturn(true, false); - stubCoreUserColumns(); - when(mockResultSet.getString("isAnonymous")).thenReturn(null); - when(mockResultSet.getString("UUID_message")).thenReturn(null); - - User result = userSelect.getUserFromDBUuid(USER_UUID); - - assertNotNull(result); - assertNull(result.getSettings(), "Settings should be null when isAnonymous is null"); - assertNotNull(result.getInbox(), "Inbox should always be initialised"); - } - - @Test - @DisplayName("getUserFromDBUuid – single row with settings populates Settings correctly") - void getUserFromDBUuid_withSettings_settingsPopulated() throws Exception { - when(mockDatabaseConnection.getMySqlConnection()).thenReturn(mockConnection); - when(mockConnection.prepareStatement(anyString())).thenReturn(mockPreparedStatement); - when(mockPreparedStatement.executeQuery()).thenReturn(mockResultSet); - when(mockResultSet.getString("isAnonymous")).thenReturn("false"); - when(mockResultSet.getBoolean("isAnonymous")).thenReturn(false); - when(mockResultSet.next()).thenReturn(true, false); - stubCoreUserColumns(); - stubSettingsColumns(false, "ENGLISH", true); - when(mockResultSet.getString("UUID_message")).thenReturn(null); - - User result = userSelect.getUserFromDBUuid(USER_UUID); - - assertNotNull(result.getSettings()); - assertFalse(result.getSettings().isAnonymous()); - assertEquals(Language.ENGLISH, result.getSettings().getLanguage()); - assertTrue(result.getSettings().isLightMode()); - } - - @Test - @DisplayName("getUserFromDBUuid – row with a message adds it to the inbox") - void getUserFromDBUuid_withMessage_messageAddedToInbox() throws Exception { - when(mockDatabaseConnection.getMySqlConnection()).thenReturn(mockConnection); - when(mockConnection.prepareStatement(anyString())).thenReturn(mockPreparedStatement); - when(mockPreparedStatement.executeQuery()).thenReturn(mockResultSet); - - when(mockResultSet.next()).thenReturn(true, false); - stubCoreUserColumns(); - when(mockResultSet.getString("isAnonymous")).thenReturn(null); - stubMessageColumns(); - - User result = userSelect.getUserFromDBUuid(USER_UUID); - - assertEquals( - 1, result.getInbox().getMessages().size(), "Inbox should contain exactly one message"); - } - - @Test - @DisplayName("getUserFromDBUuid – two rows for same UUID adds two messages, one User") - void getUserFromDBUuid_twoRowsSameUuid_oneUserTwoMessages() throws Exception { - when(mockDatabaseConnection.getMySqlConnection()).thenReturn(mockConnection); - when(mockConnection.prepareStatement(anyString())).thenReturn(mockPreparedStatement); - when(mockPreparedStatement.executeQuery()).thenReturn(mockResultSet); - - when(mockResultSet.next()).thenReturn(true, true, false); - when(mockResultSet.getString("UUID_User")).thenReturn(USER_UUID); - when(mockResultSet.getString("user_name")).thenReturn("Alice"); - when(mockResultSet.getString("user_email")).thenReturn("alice@example.com"); - when(mockResultSet.getString("user_password")).thenReturn("hashedpw"); - when(mockResultSet.getString("role")).thenReturn("NORMAL_USER"); - when(mockResultSet.getString("isAnonymous")).thenReturn(null); - when(mockResultSet.getString("UUID_message")).thenReturn("msg-1", "msg-2"); - when(mockResultSet.getString("message_title")).thenReturn("Title 1", "Title 2"); - when(mockResultSet.getString("sender_charity_id")).thenReturn(CHARITY_UUID); - when(mockResultSet.getString("message_content")).thenReturn("Content"); - when(mockResultSet.getString("message_date")).thenReturn("2024-04-01"); - - User result = userSelect.getUserFromDBUuid(USER_UUID); - - assertEquals(2, result.getInbox().getMessages().size()); - } - - @Test - @DisplayName("getUserFromDBUuid – UUID is bound to PreparedStatement parameter 1") - void getUserFromDBUuid_uuidBoundCorrectly() throws Exception { - when(mockDatabaseConnection.getMySqlConnection()).thenReturn(mockConnection); - when(mockConnection.prepareStatement(anyString())).thenReturn(mockPreparedStatement); - when(mockPreparedStatement.executeQuery()).thenReturn(mockResultSet); - when(mockResultSet.next()).thenReturn(false); - - userSelect.getUserFromDBUuid(USER_UUID); - - verify(mockPreparedStatement).setString(1, USER_UUID); - } - - @Test - @DisplayName("getUserFromDBUuid – SQLException is wrapped in RuntimeException") - void getUserFromDBUuid_sqlException_throwsRuntimeException() throws Exception { - when(mockDatabaseConnection.getMySqlConnection()).thenReturn(mockConnection); - when(mockConnection.prepareStatement(anyString())).thenThrow(new SQLException("DB error")); - - assertThrows(RuntimeException.class, () -> userSelect.getUserFromDBUuid(USER_UUID)); - } - - // ------------------------------------------------------------------------- - // getUsersFromDB - // ------------------------------------------------------------------------- - - @Test - @DisplayName("getUsersFromDB – empty result set returns empty registry") - void getUsersFromDB_emptyResultSet_returnsEmptyRegistry() throws Exception { - when(mockDatabaseConnection.getMySqlConnection()).thenReturn(mockConnection); - when(mockConnection.createStatement()).thenReturn(mockStatement); - when(mockStatement.executeQuery(anyString())).thenReturn(mockResultSet); - when(mockResultSet.next()).thenReturn(false); - - UserRegistry registry = userSelect.getUsersFromDB(); - - assertNotNull(registry); - assertTrue(registry.getAllUsers().isEmpty()); - } - - @Test - @DisplayName("getUsersFromDB – two distinct UUIDs produce two User objects") - void getUsersFromDB_twoDistinctUuids_twoUsersInRegistry() throws Exception { - when(mockDatabaseConnection.getMySqlConnection()).thenReturn(mockConnection); - when(mockConnection.createStatement()).thenReturn(mockStatement); - when(mockStatement.executeQuery(anyString())).thenReturn(mockResultSet); - - when(mockResultSet.next()).thenReturn(true, true, false); - String UserId = UUID.randomUUID().toString(); - String User2Id = UUID.randomUUID().toString(); - when(mockResultSet.getString("UUID_User")).thenReturn(UserId, User2Id); - when(mockResultSet.getString("user_name")).thenReturn("Alice", "Bob"); - when(mockResultSet.getString("user_email")).thenReturn("a@x.com", "b@x.com"); - when(mockResultSet.getString("user_password")).thenReturn("pw1", "pw2"); - when(mockResultSet.getString("role")).thenReturn("NORMAL_USER", "CHARITY_USER"); - when(mockResultSet.getString("isAnonymous")).thenReturn(null); - when(mockResultSet.getString("UUID_message")).thenReturn(null); - - UserRegistry registry = userSelect.getUsersFromDB(); - - assertEquals(2, registry.getAllUsers().size()); - } - - @Test - @DisplayName("getUsersFromDB – same UUID across two rows deduplicates to one User") - void getUsersFromDB_sameUuidTwoRows_oneUserWithTwoMessages() throws Exception { - when(mockDatabaseConnection.getMySqlConnection()).thenReturn(mockConnection); - when(mockConnection.createStatement()).thenReturn(mockStatement); - when(mockStatement.executeQuery(anyString())).thenReturn(mockResultSet); - - when(mockResultSet.next()).thenReturn(true, true, false); - when(mockResultSet.getString("UUID_User")).thenReturn(USER_UUID); - when(mockResultSet.getString("user_name")).thenReturn("Alice"); - when(mockResultSet.getString("user_email")).thenReturn("alice@example.com"); - when(mockResultSet.getString("user_password")).thenReturn("hashedpw"); - when(mockResultSet.getString("role")).thenReturn("NORMAL_USER"); - when(mockResultSet.getString("isAnonymous")).thenReturn(null); - when(mockResultSet.getString("UUID_message")).thenReturn("msg-1", "msg-2"); - when(mockResultSet.getString("message_title")).thenReturn("T1", "T2"); - when(mockResultSet.getString("sender_charity_id")).thenReturn(CHARITY_UUID); - when(mockResultSet.getString("message_content")).thenReturn("Body"); - when(mockResultSet.getString("message_date")).thenReturn("2024-05-01"); - - UserRegistry registry = userSelect.getUsersFromDB(); - - assertEquals(1, registry.getAllUsers().size(), "Same UUID should not produce duplicate users"); - assertEquals(2, registry.getAllUsers().get(0).getInbox().getMessages().size()); - } - - @Test - @DisplayName("getUsersFromDB – SQLException is wrapped in RuntimeException") - void getUsersFromDB_sqlException_throwsRuntimeException() throws Exception { - when(mockDatabaseConnection.getMySqlConnection()).thenReturn(mockConnection); - when(mockConnection.createStatement()).thenThrow(new SQLException("DB error")); - - assertThrows(RuntimeException.class, () -> userSelect.getUsersFromDB()); - } - - // ------------------------------------------------------------------------- - // getSettingsForUser - // ------------------------------------------------------------------------- - - @Test - @DisplayName("getSettingsForUser – no row returns null") - void getSettingsForUser_noRow_returnsNull() throws Exception { - when(mockDatabaseConnection.getMySqlConnection()).thenReturn(mockConnection); - when(mockConnection.prepareStatement(anyString())).thenReturn(mockPreparedStatement); - when(mockPreparedStatement.executeQuery()).thenReturn(mockResultSet); - when(mockResultSet.next()).thenReturn(false); - - Settings result = userSelect.getSettingsForUser(USER_UUID); - - assertNull(result, "Should return null when no settings row exists"); - } - - @Test - @DisplayName("getSettingsForUser – matching row returns populated Settings") - void getSettingsForUser_matchingRow_returnsSettings() throws Exception { - when(mockDatabaseConnection.getMySqlConnection()).thenReturn(mockConnection); - when(mockConnection.prepareStatement(anyString())).thenReturn(mockPreparedStatement); - when(mockPreparedStatement.executeQuery()).thenReturn(mockResultSet); - - when(mockResultSet.next()).thenReturn(true, false); - stubSettingsColumns(true, "ENGLISH", false); - - Settings result = userSelect.getSettingsForUser(USER_UUID); - - assertNotNull(result); - // assertTrue(result.isAnonymous()); - assertEquals(Language.ENGLISH, result.getLanguage()); - assertFalse(result.isLightMode()); - } - - @Test - @DisplayName("getSettingsForUser – UUID is bound to PreparedStatement and maxRows set to 1") - void getSettingsForUser_correctBindingAndMaxRows() throws Exception { - when(mockDatabaseConnection.getMySqlConnection()).thenReturn(mockConnection); - when(mockConnection.prepareStatement(anyString())).thenReturn(mockPreparedStatement); - when(mockPreparedStatement.executeQuery()).thenReturn(mockResultSet); - when(mockResultSet.next()).thenReturn(false); - - userSelect.getSettingsForUser(USER_UUID); - - verify(mockPreparedStatement).setString(1, USER_UUID); - verify(mockPreparedStatement).setMaxRows(1); - } - - @Test - @DisplayName("getSettingsForUser – SQLException is wrapped in RuntimeException") - void getSettingsForUser_sqlException_throwsRuntimeException() throws Exception { - when(mockDatabaseConnection.getMySqlConnection()).thenReturn(mockConnection); - when(mockConnection.prepareStatement(anyString())).thenThrow(new SQLException("DB error")); - - assertThrows(RuntimeException.class, () -> userSelect.getSettingsForUser(USER_UUID)); - } - - // ------------------------------------------------------------------------- - // getInboxForUser - // ------------------------------------------------------------------------- - - @Test - @DisplayName("getInboxForUser – no messages returns empty Inbox (never null)") - void getInboxForUser_noMessages_returnsEmptyInbox() throws Exception { - when(mockDatabaseConnection.getMySqlConnection()).thenReturn(mockConnection); - when(mockConnection.prepareStatement(anyString())).thenReturn(mockPreparedStatement); - when(mockPreparedStatement.executeQuery()).thenReturn(mockResultSet); - when(mockResultSet.next()).thenReturn(false); - - Inbox result = userSelect.getInboxForUser(USER_UUID); - - assertNotNull(result, "Inbox should never be null"); - assertTrue(result.getMessages().isEmpty()); - } - - @Test - @DisplayName("getInboxForUser – one message row returns Inbox with one Message") - void getInboxForUser_oneRow_inboxContainsOneMessage() throws Exception { - when(mockDatabaseConnection.getMySqlConnection()).thenReturn(mockConnection); - when(mockConnection.prepareStatement(anyString())).thenReturn(mockPreparedStatement); - when(mockPreparedStatement.executeQuery()).thenReturn(mockResultSet); - - when(mockResultSet.next()).thenReturn(true, false); - when(mockResultSet.getString("message_title")).thenReturn("Hello"); - when(mockResultSet.getString("sender_charity_id")).thenReturn(CHARITY_UUID); - when(mockResultSet.getString("message_date")).thenReturn("2024-06-15"); - when(mockResultSet.getString("message_content")).thenReturn("Hello!"); - - Inbox result = userSelect.getInboxForUser(USER_UUID); - - assertEquals(1, result.getMessages().size()); - assertEquals("Hello", result.getMessages().get(0).getTitle()); - } - - @Test - @DisplayName("getInboxForUser – two message rows returns Inbox with two Messages") - void getInboxForUser_twoRows_inboxContainsTwoMessages() throws Exception { - when(mockDatabaseConnection.getMySqlConnection()).thenReturn(mockConnection); - when(mockConnection.prepareStatement(anyString())).thenReturn(mockPreparedStatement); - when(mockPreparedStatement.executeQuery()).thenReturn(mockResultSet); - - when(mockResultSet.next()).thenReturn(true, true, false); - when(mockResultSet.getString("message_title")).thenReturn("Msg 1", "Msg 2"); - when(mockResultSet.getString("sender_charity_id")).thenReturn(CHARITY_UUID); - when(mockResultSet.getString("message_date")).thenReturn("2024-06-15"); - when(mockResultSet.getString("message_content")).thenReturn("Hello!"); - - Inbox result = userSelect.getInboxForUser(USER_UUID); - - assertEquals(2, result.getMessages().size()); - } - - @Test - @DisplayName("getInboxForUser – UUID is bound to PreparedStatement parameter 1") - void getInboxForUser_uuidBoundCorrectly() throws Exception { - when(mockDatabaseConnection.getMySqlConnection()).thenReturn(mockConnection); - when(mockConnection.prepareStatement(anyString())).thenReturn(mockPreparedStatement); - when(mockPreparedStatement.executeQuery()).thenReturn(mockResultSet); - when(mockResultSet.next()).thenReturn(false); - - userSelect.getInboxForUser(USER_UUID); - - verify(mockPreparedStatement).setString(1, USER_UUID); - } - - @Test - @DisplayName("getInboxForUser – SQLException is wrapped in RuntimeException") - void getInboxForUser_sqlException_throwsRuntimeException() throws Exception { - when(mockDatabaseConnection.getMySqlConnection()).thenReturn(mockConnection); - when(mockConnection.prepareStatement(anyString())).thenThrow(new SQLException("DB error")); - - assertThrows(RuntimeException.class, () -> userSelect.getInboxForUser(USER_UUID)); - } - - // ------------------------------------------------------------------------- - // Helpers - // ------------------------------------------------------------------------- - - /** Stubs the core User columns on the mock ResultSet. */ - private void stubCoreUserColumns() throws SQLException { - when(mockResultSet.getString("UUID_User")).thenReturn(USER_UUID); - when(mockResultSet.getString("user_name")).thenReturn("Alice"); - when(mockResultSet.getString("user_email")).thenReturn("alice@example.com"); - when(mockResultSet.getString("user_password")).thenReturn("hashedpw"); - when(mockResultSet.getString("role")).thenReturn("NORMAL_USER"); - } - - /** Stubs the Settings columns on the mock ResultSet. */ - private void stubSettingsColumns(boolean isAnonymous, String language, boolean lightmode) - throws SQLException { - when(mockResultSet.getBoolean("isAnonymous")).thenReturn(isAnonymous); - when(mockResultSet.getString("language")).thenReturn(language); - when(mockResultSet.getBoolean("lightmode")).thenReturn(lightmode); - } - - /** Stubs the Message columns on the mock ResultSet for a single message row. */ - private void stubMessageColumns() throws SQLException { - when(mockResultSet.getString("UUID_message")).thenReturn(MESSAGE_UUID); - when(mockResultSet.getString("message_title")).thenReturn("Test Message"); - when(mockResultSet.getString("sender_charity_id")).thenReturn(CHARITY_UUID); - when(mockResultSet.getString("message_content")).thenReturn("Hello!"); - when(mockResultSet.getString("message_date")).thenReturn("2024-03-01"); - } -}