diff --git a/src/test/java/edu/group5/app/model/donation/DonationRepositoryTest.java b/src/test/java/edu/group5/app/model/donation/DonationRepositoryTest.java index e06a350..b55ef57 100644 --- a/src/test/java/edu/group5/app/model/donation/DonationRepositoryTest.java +++ b/src/test/java/edu/group5/app/model/donation/DonationRepositoryTest.java @@ -1,4 +1,188 @@ package edu.group5.app.model.donation; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.math.BigDecimal; +import java.sql.Timestamp; +import java.util.HashMap; + +import static org.junit.jupiter.api.Assertions.*; + public class DonationRepositoryTest { + private DonationRepository repo; + private Timestamp now; + private Donation donation1; + private Donation donation2; + private Donation donation3; + private Donation donation4; + + @BeforeEach + void setUp() { + repo = new DonationRepository(new HashMap<>()); + + Timestamp ts1 = Timestamp.valueOf("2025-01-01 10:00:00"); + Timestamp ts2 = Timestamp.valueOf("2025-01-01 10:00:01"); + now = new Timestamp(System.currentTimeMillis()); + + donation1 = new Donation(1, 102, 202, new BigDecimal("500.0"), ts1, "Card"); + donation2 = new Donation(2, 102, 202, new BigDecimal("500.0"), ts2, "PayPal"); + donation3 = new Donation(3, 103, 203, new BigDecimal("200.0"), now, "PayPal"); + donation4 = new Donation(1, 101, 201, new BigDecimal("500.0"), now, "Card"); + + + } + @Test + void testThrowsExceptionIfContentIsNull() { + IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, + () -> new DonationRepository(null)); + assertEquals("Content cannot be null", thrown.getMessage()); + } + + @Test + void testAddDonation() { + assertTrue(repo.addDonation(donation1)); + assertEquals(1, repo.getContent().size()); + + assertTrue(repo.addDonation(donation3)); + assertEquals(2, repo.getContent().size()); + } + @Test + void testAddDonationWithDuplicateId() { + assertTrue(repo.addDonation(donation1)); + assertFalse(repo.addDonation(donation4)); + assertEquals(1, repo.getContent().size()); + } + @Test + void testAddNullDonation() { + IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, + () -> repo.addDonation(null)); + assertEquals("Donation cannot be null",thrown.getMessage()); + } + + @Test + void testSortByDate() { + repo.addDonation(donation3); + repo.addDonation(donation1); + repo.addDonation(donation2); + + HashMap sorted = repo.sortByDate(); + + Integer[] keys = sorted.keySet().toArray(new Integer[0]); + assertEquals(1, keys[0]); + assertEquals(2, keys[1]); + assertEquals(3, keys[2]); + + } + @Test + void testSortByDateWithSameDate() { + + repo.addDonation(donation3); + repo.addDonation(donation4); + + HashMap sorted = repo.sortByDate(); + + assertEquals(2, sorted.size()); + assertTrue(sorted.containsKey(1)); + assertTrue(sorted.containsKey(3)); + } + @Test + void testSortByDateWithSameDonation() { + Timestamp sameDate = Timestamp.valueOf("2025-01-01 10:00:00"); + + Donation d1 = new Donation(1, 101, 201, + new BigDecimal("500.0"), sameDate, "Card"); + + repo.addDonation(d1); + repo.addDonation(donation4); + + HashMap sorted = repo.sortByDate(); + + assertEquals(1, sorted.size()); + assertTrue(sorted.containsKey(1)); + assertFalse(sorted.containsKey(2)); + } + @Test + void TestSortByAmountTest() { + repo.addDonation(donation2); + repo.addDonation(donation3); + + HashMap sorted = repo.sortByAmount(); + Integer[] keys = sorted.keySet().toArray(new Integer[0]); + + assertEquals(3, keys[0]); + assertEquals(2, keys[1]); + + + } + @Test + void testSortByAmountWithSameAmount() { + repo.addDonation(donation1); + repo.addDonation(donation2); + + HashMap sorted = repo.sortByAmount(); + + assertEquals(2, sorted.size()); + assertTrue(sorted.containsKey(1)); + assertTrue(sorted.containsKey(2)); + } + @Test + void testSortByAmountWithSameDonation() { + + Donation d1 = new Donation(1, 101, 201, + new BigDecimal("500.0"), now, "Card"); + + repo.addDonation(d1); + repo.addDonation(donation4); + + HashMap sorted = repo.sortByAmount(); + + assertEquals(1, sorted.size()); + assertTrue(sorted.containsKey(1)); + assertFalse(sorted.containsKey(2)); + } + @Test + void testFilterByOrganization() { + repo.addDonation(donation1); + repo.addDonation(donation2); + + HashMap filtered = repo.filterByOrganization(202); + + assertEquals(2, filtered.size()); + assertTrue(filtered.containsKey(1)); + assertTrue(filtered.containsKey(2)); + assertFalse(filtered.containsKey(3)); + } + @Test + void testFilterByOrganizationNoMatch() { + repo.addDonation(donation1); + repo.addDonation(donation2); + + HashMap filtered = repo.filterByOrganization(999); + + assertTrue(filtered.isEmpty()); + } + @Test + void testThrowsExceptionWhenOrgNumberIsNegative() { + IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, + () -> repo.filterByOrganization(-1)); + assertEquals("Organization number must be positive",thrown.getMessage()); + } + @Test + void testFilterByOrganizationWithSameDonation() { + Timestamp sameDate = Timestamp.valueOf("2025-01-01 10:00:00"); + + Donation d1 = new Donation(1, 101, 201, + new BigDecimal("500.0"), sameDate, "Card"); + + repo.addDonation(d1); + repo.addDonation(donation4); + + HashMap sorted = repo.filterByOrganization(201); + + assertEquals(1, sorted.size()); + assertTrue(sorted.containsKey(1)); + assertFalse(sorted.containsKey(2)); + } + }