-
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.
- Loading branch information
AdrianBalunan
committed
Mar 16, 2026
1 parent
462baf8
commit a180e03
Showing
2 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/DAO/DonationDAOTest.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 |
|---|---|---|
| @@ -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")); | ||
| } | ||
| } | ||
| } |
116 changes: 116 additions & 0 deletions
116
...helpapplication/src/test/java/ntnu/systemutvikling/team6/models/DonationRegistryTest.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 |
|---|---|---|
| @@ -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<Donation> 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<Donation> donations = registry.getAllDonations(); | ||
|
|
||
| assertThrows(UnsupportedOperationException.class, | ||
| () -> donations.add(donation)); | ||
| } | ||
|
|
||
| @Test | ||
| void findDonationById_shouldReturnDonationIfFound() { | ||
| registry.addDonation(donation); | ||
|
|
||
| Optional<Donation> result = registry.findDonationById(donation.getCharityId()); | ||
|
|
||
| assertTrue(result.isPresent()); | ||
| assertEquals(donation, result.get()); | ||
| } | ||
|
|
||
| @Test | ||
| void findDonationById_shouldReturnEmptyIfNotFound() { | ||
| Optional<Donation> 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)); | ||
| } | ||
| } |