-
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.
test: added positive and negative tests to DonationRepositoryTest
- Loading branch information
MatheaGjerde
committed
Mar 3, 2026
1 parent
b0614fd
commit 632e8fa
Showing
1 changed file
with
184 additions
and
0 deletions.
There are no files selected for viewing
184 changes: 184 additions & 0 deletions
184
src/test/java/edu/group5/app/model/donation/DonationRepositoryTest.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,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<Integer, Donation> 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<Integer, Donation> 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<Integer, Donation> 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<Integer, Donation> 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<Integer, Donation> 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<Integer, Donation> 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<Integer, Donation> 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<Integer, Donation> 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<Integer, Donation> sorted = repo.filterByOrganization(201); | ||
|
|
||
| assertEquals(1, sorted.size()); | ||
| assertTrue(sorted.containsKey(1)); | ||
| assertFalse(sorted.containsKey(2)); | ||
| } | ||
|
|
||
| } |