diff --git a/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/models/user/InboxTest.java b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/models/user/InboxTest.java new file mode 100644 index 0000000..250d270 --- /dev/null +++ b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/models/user/InboxTest.java @@ -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 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 messages = inbox.getMessages(); + + assertThrows(UnsupportedOperationException.class, () -> messages.add(newMessage2)); + } + + @Test + void findMessageById_shouldReturnMessageWhenFound() { + inbox.addMessage(newMessage); + + Optional result = inbox.findMessageById(newMessage.getId()); + + assertTrue(result.isPresent()); + assertEquals(newMessage, result.get()); + } + + @Test + void findMessageById_shouldReturnEmptyWhenNotFound() { + Optional 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)); + } +}