diff --git a/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/DAO/DonationDAOTest.java b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/DAO/DonationDAOTest.java new file mode 100644 index 0000000..cdef6b0 --- /dev/null +++ b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/DAO/DonationDAOTest.java @@ -0,0 +1,56 @@ +package ntnu.systemutvikling.team6.DAO; + +import ntnu.systemutvikling.team6.database.DatabaseConnection; +import ntnu.systemutvikling.team6.database.DatabaseManager; +import ntnu.systemutvikling.team6.models.Charity; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; + +import static org.junit.jupiter.api.Assertions.*; + +class DonationDAOTest { + + private Charity charity; + + @BeforeEach + void setUp() { + DatabaseManager manager = new DatabaseManager(); + manager.createTables(); + + charity = new Charity( + "123456", + "https://test.org", + "Test Charity", + true, + "approved" + ); + + manager.addAPIDataToTable(java.util.List.of(charity)); + } + + @Test + void addDonationShouldInsertDonationIntoDatabase() throws Exception { + double amount = 100.0; + + DonationDAO.addDonation(charity, amount); + + try (Connection conn = new DatabaseConnection().getMySqlConnection()) { + + PreparedStatement stmt = + conn.prepareStatement("SELECT amount FROM Donations WHERE Charities_UUID_charities = ?"); + + stmt.setString(1, charity.getUUID().toString()); + + ResultSet rs = stmt.executeQuery(); + + assertTrue(rs.next(), "Donation should exist in database"); + + assertEquals(amount, rs.getDouble("amount")); + } + } +} \ No newline at end of file diff --git a/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/database/DatabaseManagerTest.java b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/database/DatabaseManagerTest.java index fbef967..51aa6e6 100644 --- a/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/database/DatabaseManagerTest.java +++ b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/database/DatabaseManagerTest.java @@ -1,6 +1,8 @@ -package ntnu.systemutvikling.team6.models; +package ntnu.systemutvikling.team6.database; import ntnu.systemutvikling.team6.database.DatabaseConnection; +import ntnu.systemutvikling.team6.models.Charity; +import ntnu.systemutvikling.team6.models.CharityRegistry; import ntnu.systemutvikling.team6.scraper.APICharityData; import ntnu.systemutvikling.team6.database.DatabaseManager; import org.junit.jupiter.api.*; diff --git a/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/models/CharityTest.java b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/models/CharityTest.java index 43fa905..b13533c 100644 --- a/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/models/CharityTest.java +++ b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/models/CharityTest.java @@ -17,7 +17,7 @@ public class CharityTest { @BeforeEach public void setup() { - charity = new Charity("1239813", "www.aaaa.com", "Charity1", false, "unverified"); + charity = new Charity("1239813", "www.aaaa.com", "Charity1", false, "approved"); } /** Getters should work: */ diff --git a/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/models/DonationRegistryTest.java b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/models/DonationRegistryTest.java new file mode 100644 index 0000000..b62ad0d --- /dev/null +++ b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/models/DonationRegistryTest.java @@ -0,0 +1,116 @@ +package ntnu.systemutvikling.team6.models; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +import java.time.LocalDate; +import java.util.List; +import java.util.Optional; +import java.util.UUID; + +import ntnu.systemutvikling.team6.models.user.User; +import ntnu.systemutvikling.team6.models.user.Settings; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class DonationRegistryTest { + + private DonationRegistry registry; + private Donation donation; + + @BeforeEach + void setUp() { + registry = new DonationRegistry(); + + Charity charity = mock(Charity.class); + User user = mock(User.class); + Settings settings = mock(Settings.class); + + when(user.getSettings()).thenReturn(settings); + when(settings.isAnonymous()).thenReturn(false); + + donation = new Donation( + 100, + LocalDate.now(), + charity, + user + ); + } + + @Test + void constructor_shouldCreateEmptyRegistry() { + assertTrue(registry.getAllDonations().isEmpty()); + } + + @Test + void addDonation_shouldAddDonation() { + registry.addDonation(donation); + + List donations = registry.getAllDonations(); + + assertEquals(1, donations.size()); + assertTrue(donations.contains(donation)); + } + + @Test + void addDonation_nullDonation_shouldThrowException() { + assertThrows(IllegalArgumentException.class, () -> registry.addDonation(null)); + } + + @Test + void getAllDonations_shouldReturnUnmodifiableList() { + registry.addDonation(donation); + + List donations = registry.getAllDonations(); + + assertThrows(UnsupportedOperationException.class, + () -> donations.add(donation)); + } + + @Test + void findDonationById_shouldReturnDonationIfFound() { + registry.addDonation(donation); + + Optional result = registry.findDonationById(donation.getCharityId()); + + assertTrue(result.isPresent()); + assertEquals(donation, result.get()); + } + + @Test + void findDonationById_shouldReturnEmptyIfNotFound() { + Optional result = registry.findDonationById(UUID.randomUUID()); + + assertTrue(result.isEmpty()); + } + + @Test + void findDonationById_nullId_shouldThrowException() { + assertThrows(IllegalArgumentException.class, + () -> registry.findDonationById(null)); + } + + @Test + void removeDonation_shouldRemoveDonation() { + registry.addDonation(donation); + + boolean removed = registry.removeDonation(donation.getCharityId()); + + assertTrue(removed); + assertTrue(registry.getAllDonations().isEmpty()); + } + + @Test + void removeDonation_shouldReturnFalseIfNotFound() { + boolean removed = registry.removeDonation(UUID.randomUUID()); + + assertFalse(removed); + } + + @Test + void removeDonation_nullId_shouldThrowException() { + assertThrows(IllegalArgumentException.class, + () -> registry.removeDonation(null)); + } +} \ No newline at end of file diff --git a/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/models/user/InboxTest.java b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/models/user/InboxTest.java new file mode 100644 index 0000000..250d270 --- /dev/null +++ b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/models/user/InboxTest.java @@ -0,0 +1,95 @@ +package ntnu.systemutvikling.team6.models.user; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.List; +import java.util.Optional; +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.*; + +public class InboxTest { + private Inbox inbox; + private Message newMessage; + private Message newMessage2; + + @BeforeEach + public void setup(){ + inbox = new Inbox(); + newMessage = new Message("Title", "Someone", "Somewhere"); + newMessage2 = new Message("Title2", "Someone2", "Somewhere2"); + } + + @Test + void constructor_shouldCreateEmptyInbox() { + assertTrue(inbox.getMessages().isEmpty()); + } + + @Test + void addMessage_shouldAddMessageToInbox() { + inbox.addMessage(newMessage); + + List messages = inbox.getMessages(); + assertEquals(1, messages.size()); + assertTrue(messages.contains(newMessage)); + } + + @Test + void addMessage_nullMessage_shouldThrowException() { + assertThrows(IllegalArgumentException.class, () -> inbox.addMessage(null)); + } + + @Test + void getMessages_shouldReturnUnmodifiableList() { + inbox.addMessage(newMessage); + + List messages = inbox.getMessages(); + + assertThrows(UnsupportedOperationException.class, () -> messages.add(newMessage2)); + } + + @Test + void findMessageById_shouldReturnMessageWhenFound() { + inbox.addMessage(newMessage); + + Optional result = inbox.findMessageById(newMessage.getId()); + + assertTrue(result.isPresent()); + assertEquals(newMessage, result.get()); + } + + @Test + void findMessageById_shouldReturnEmptyWhenNotFound() { + Optional result = inbox.findMessageById(UUID.randomUUID()); + + assertTrue(result.isEmpty()); + } + + @Test + void findMessageById_nullId_shouldThrowException() { + assertThrows(IllegalArgumentException.class, () -> inbox.findMessageById(null)); + } + + @Test + void removeMessage_shouldRemoveExistingMessage() { + inbox.addMessage(newMessage); + + boolean removed = inbox.removeMessage(newMessage.getId()); + + assertTrue(removed); + assertTrue(inbox.getMessages().isEmpty()); + } + + @Test + void removeMessage_shouldReturnFalseIfNotFound() { + boolean removed = inbox.removeMessage(UUID.randomUUID()); + + assertFalse(removed); + } + + @Test + void removeMessage_nullId_shouldThrowException() { + assertThrows(IllegalArgumentException.class, () -> inbox.removeMessage(null)); + } +} diff --git a/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/models/user/MessegeTest.java b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/models/user/MessegeTest.java new file mode 100644 index 0000000..ff71841 --- /dev/null +++ b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/models/user/MessegeTest.java @@ -0,0 +1,50 @@ +package ntnu.systemutvikling.team6.models.user; + +import org.junit.jupiter.api.Test; + +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.*; + +public class MessegeTest { + @Test + void shouldThrowExceptionIfNameIsNullOrEmpty() { + assertThrows( + IllegalArgumentException.class, + () -> new Message(null, "Someone", "Something Somewhere Somehow")); + assertThrows( + IllegalArgumentException.class, + () -> new Message("", "Someone", "Something Somewhere Somehow")); + } + + @Test + void shouldThrowExceptionIfFromIsNullOrEmpty() { + assertThrows( + IllegalArgumentException.class, + () -> new Message("Title", null, "Something Somewhere Somehow")); + assertThrows( + IllegalArgumentException.class, + () -> new Message("Title", "", "Something Somewhere Somehow")); + } + @Test + void shouldThrowExceptionIfContentIsNullOrEmpty() { + assertThrows( + IllegalArgumentException.class, + () -> new Message("Title", "Someone", null)); + assertThrows( + IllegalArgumentException.class, + () -> new Message("Title", "Someone", "")); + } + + @Test + void GettersWork(){ + Message newMessage = new Message("Title", "Someone", "Somewhere"); + assertInstanceOf(UUID.class, newMessage.getId()); + assertEquals("Title", newMessage.getTitle()); + assertEquals("Someone", newMessage.getFrom()); + assertEquals("Somewhere", newMessage.getContent()); + assertEquals(LocalDate.now(), newMessage.getTimeAndDate().toLocalDate()); + } +} diff --git a/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/scraper/APICharityScraperTest.java b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/scraper/APICharityScraperTest.java index 36eb452..bf2eacd 100644 --- a/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/scraper/APICharityScraperTest.java +++ b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/scraper/APICharityScraperTest.java @@ -1,5 +1,7 @@ -package ntnu.systemutvikling.team6.models; +package ntnu.systemutvikling.team6.scraper; +import ntnu.systemutvikling.team6.models.Charity; +import ntnu.systemutvikling.team6.models.CharityRegistry; import ntnu.systemutvikling.team6.scraper.APICharityData; import ntnu.systemutvikling.team6.scraper.APICharityScraper; import org.junit.jupiter.api.Test;