Skip to content

Commit

Permalink
Feat: Added UnitTest for CharityRegistry class
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianBalunan committed Mar 4, 2026
1 parent 7e62703 commit 30c5e48
Showing 1 changed file with 87 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package ntnu.systemutvikling.team6.models;

import ntnu.sytemutvikling.team6.models.Charity;
import ntnu.sytemutvikling.team6.models.CharityRegistry;
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 CharityRegistryTest {
private CharityRegistry registry;
private Charity charity;

@BeforeEach
public void setup(){
registry = new CharityRegistry();
charity = new Charity("Charity1", "Something Somewhere Somehow", "Cancer");
}


@Test
void testAddCharitySuccessfully() {
registry.addCharity(charity);

assertEquals(1, registry.getAllCharities().size());
assertTrue(registry.getAllCharities().contains(charity));
}

@Test
void testAddCharityNullThrowsException() {
assertThrows(IllegalArgumentException.class, () -> registry.addCharity(null));
}

@Test
void testGetAllCharitiesReturnsUnmodifiableList() {
registry.addCharity(charity);

List<Charity> charities = registry.getAllCharities();
assertThrows(UnsupportedOperationException.class, () -> charities.add(charity));
}

@Test
void testFindCharityByIdFound() {
registry.addCharity(charity);

Optional<Charity> result = registry.findCharityById(charity.getId());

assertTrue(result.isPresent());
assertEquals(charity, result.get());
}

@Test
void testFindCharityByIdNotFound() {
Optional<Charity> result = registry.findCharityById(UUID.randomUUID());
assertTrue(result.isEmpty());
}

@Test
void testFindCharityByIdNullThrowsException() {
assertThrows(IllegalArgumentException.class, () -> registry.findCharityById(null));
}

@Test
void testRemoveCharitySuccessfully() {
registry.addCharity(charity);

boolean removed = registry.removeCharity(charity.getId());

assertTrue(removed);
assertTrue(registry.getAllCharities().isEmpty());
}

@Test
void testRemoveCharityNotFound() {
boolean removed = registry.removeCharity(UUID.randomUUID());
assertFalse(removed);
}

@Test
void testRemoveCharityNullThrowsException() {
assertThrows(IllegalArgumentException.class, () -> registry.removeCharity(null));
}
}

0 comments on commit 30c5e48

Please sign in to comment.