Skip to content

Feature/51 mvp more unittesting #52

Merged
merged 5 commits into from
Mar 16, 2026
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"));
}
}
}
Original file line number Diff line number Diff line change
@@ -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.*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: */
Expand Down
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));
}
}
Original file line number Diff line number Diff line change
@@ -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<Message> 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<Message> messages = inbox.getMessages();

assertThrows(UnsupportedOperationException.class, () -> messages.add(newMessage2));
}

@Test
void findMessageById_shouldReturnMessageWhenFound() {
inbox.addMessage(newMessage);

Optional<Message> result = inbox.findMessageById(newMessage.getId());

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

@Test
void findMessageById_shouldReturnEmptyWhenNotFound() {
Optional<Message> 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));
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Loading