diff --git a/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/models/CharityTest.java b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/models/CharityTest.java index 11615fc0..79118e13 100644 --- a/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/models/CharityTest.java +++ b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/models/CharityTest.java @@ -2,8 +2,15 @@ import static org.junit.jupiter.api.Assertions.*; +import java.time.LocalDate; import java.util.ArrayList; +import java.util.List; import java.util.UUID; + +import ntnu.systemutvikling.team6.models.user.Inbox; +import ntnu.systemutvikling.team6.models.user.Role; +import ntnu.systemutvikling.team6.models.user.Settings; +import ntnu.systemutvikling.team6.models.user.User; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -50,4 +57,157 @@ public void testIsVerifiedReturnsCorrectly() { charity.setVerified(); assertEquals("approved", charity.getStatus()); } + + @Test + public void testDatabaseSelectConstructor() { + UUID uuid = UUID.randomUUID(); + String orgNumber = "123"; + boolean preApproved = true; + String status = "approved"; + Charity selectCharity = + new Charity( + uuid.toString(), + orgNumber, + preApproved, + status); + + assertEquals(uuid, selectCharity.getUUID()); + assertEquals(orgNumber, selectCharity.getOrg_number()); + assertEquals(preApproved, selectCharity.getPreApproved()); + assertEquals(status, selectCharity.getStatus()); + } + + @Test + void testDatabaseConstructor() { + UUID uuid = UUID.randomUUID(); + String orgNumber = "123"; + String name = "testChair"; + boolean preApproved = true; + String status = "approved"; + String description = "describe"; + String logoURL = "www.image.png"; + String keyValues = "90,2;10,5;98,0"; + + byte[] logoBlob = new byte[] {1, 2, 3, 4, 5}; + + Charity databaseCharity = + new Charity( + uuid.toString(), + orgNumber, + name, + logoURL, + status, + preApproved, + description, + logoURL, + keyValues, + logoBlob + ); + + assertEquals(uuid, databaseCharity.getUUID()); + assertEquals(orgNumber, databaseCharity.getOrg_number()); + assertEquals(name, databaseCharity.getName()); + assertEquals(logoURL, databaseCharity.getLogoURL()); + assertEquals(status, databaseCharity.getStatus()); + assertEquals(preApproved, databaseCharity.getPreApproved()); + assertEquals(description, databaseCharity.getDescription()); + assertEquals(keyValues, databaseCharity.getKeyValues()); + assertEquals(logoBlob, databaseCharity.getLogoBlob()); + } + + @Test + void getAndSetFeedbackShouldBeCorrect() { + String validUsername = "username"; + String validEmail = "Email@gmail.com"; + String validPassword = "Password"; + Role validRole = Role.NORMAL_USER; + Settings validSettings = new Settings(); + Inbox validInbox = new Inbox(); + User user = + new User( + validUsername, + validEmail, + validPassword, + validRole, + validSettings, + validInbox); + Feedback feedback = + new Feedback( + UUID.randomUUID().toString(), + user, + "Feedback.", + LocalDate.now() + ); + ArrayListfeedbackList = new ArrayList<>(); + feedbackList.add(feedback); + + charity.setFeedbacks(feedbackList); + + assertSame(charity.getFeedbacks(), feedbackList); + } + + @Test + void setNameShouldBeCorrect() { + String name = "Bob's charity"; + + charity.setName(name); + + assertEquals(name, charity.getName()); + } + + @Test + void setCategoryShouldBeCorrect() { + String category = "Ghana"; + List categoryList = new ArrayList<>(); + categoryList.add(category); + + charity.setCategory(categoryList); + + assertEquals(category, charity.getCategory().getFirst()); + } + + @Test + void setDescriptionShouldBeCorrect() { + String description = "blablabla"; + + charity.setDescription(description); + + assertEquals(description, charity.getDescription()); + } + + @Test + void setLogoUrlShouldBeCorrect() { + String logoURL = "www.image.png"; + + charity.setLogoURL(logoURL); + + assertEquals(logoURL, charity.getLogoURL()); + } + + @Test + void setKeyValuesShouldBeCorrect() { + String keyValues = "98,2;5,6;75,4"; + + charity.setKeyValues(keyValues); + + assertEquals(keyValues, charity.getKeyValues()); + } + + @Test + void setLogoBlobShouldBeCorrect() { + byte[] logoBlob = new byte[] {1, 2, 3, 4, 5}; + + charity.setLogoBlob(logoBlob); + + assertEquals(logoBlob, charity.getLogoBlob()); + } + + @Test + void setUUIDFromStringShouldBeCorrect() { + UUID uuid = UUID.randomUUID(); + + charity.setUUIDFromString(uuid.toString()); + + assertEquals(uuid, charity.getUUID()); + } } diff --git a/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/models/DonationTest.java b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/models/DonationTest.java index fba07b6a..4a960099 100644 --- a/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/models/DonationTest.java +++ b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/models/DonationTest.java @@ -4,12 +4,13 @@ import java.time.LocalDate; import java.time.LocalDateTime; +import java.util.UUID; + import ntnu.systemutvikling.team6.models.user.*; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; class DonationTest { - private Settings settings; private User user; private Charity charity; @@ -20,7 +21,6 @@ public void setup() { user = new User( - "Name", "username", "Valid@gmail.com", "123", @@ -43,6 +43,19 @@ void testDonationInitialization() { assertEquals(user, donation.getDonor()); } + @Test + void testDonationFromDatabaseInitialization() { + LocalDateTime now = LocalDateTime.now(); + + Donation donation = new Donation(UUID.randomUUID().toString(), 500.0, LocalDate.from(now), charity, user, false); + + assertNotNull(donation.getCharityId()); + assertEquals(500.0, donation.getAmount()); + assertEquals(LocalDate.from(now), donation.getDate()); + assertEquals(charity, donation.getCharity()); + assertEquals(user, donation.getDonor()); + } + @Test void testAnonymousFlagWhenUserIsAnonymous() { user.setSettings(new Settings(false, Language.ENGLISH, false)); diff --git a/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/models/FeedbackTest.java b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/models/FeedbackTest.java index b8cc6b8a..5e4af2d2 100644 --- a/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/models/FeedbackTest.java +++ b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/models/FeedbackTest.java @@ -2,8 +2,11 @@ import static org.junit.jupiter.api.Assertions.*; +import java.time.LocalDate; import java.time.LocalDateTime; import java.time.chrono.ChronoLocalDate; +import java.util.UUID; + import ntnu.systemutvikling.team6.models.user.Inbox; import ntnu.systemutvikling.team6.models.user.Role; import ntnu.systemutvikling.team6.models.user.Settings; @@ -14,15 +17,12 @@ class FeedbackTest { private User user; - private Settings settings; // -- Setup -- @BeforeEach public void setup() { - settings = new Settings(); // default anonymous = true user = new User( - "Name", "username", "Valid@gmail.com", "123", @@ -48,6 +48,20 @@ void testFeedbackInitialization() { !feedback.getDate().isBefore(ChronoLocalDate.from(before)) && !feedback.getDate().isAfter(ChronoLocalDate.from(after))); } +@Test + void testFeedBackFromDatabaseInitialization() { + LocalDate date = LocalDate.now(); + Feedback feedback = new Feedback( + UUID.randomUUID().toString(), + user, + "Nice work!", + date); + + assertNotNull(feedback.getFeedbackId()); + assertEquals("Nice work!", feedback.getComment()); + assertEquals(user, feedback.getUser()); + assertEquals(date, feedback.getDate()); + } @Test void testAnonymousFlagWhenUserIsAnonymous() { diff --git a/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/models/CharityRegistryTest.java b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/models/registry/CharityRegistryTest.java similarity index 76% rename from helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/models/CharityRegistryTest.java rename to helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/models/registry/CharityRegistryTest.java index c0e32aaf..ed227924 100644 --- a/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/models/CharityRegistryTest.java +++ b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/models/registry/CharityRegistryTest.java @@ -1,11 +1,12 @@ -package ntnu.systemutvikling.team6.models; +package ntnu.systemutvikling.team6.models.registry; import static org.junit.jupiter.api.Assertions.*; import java.util.List; import java.util.Optional; import java.util.UUID; -import ntnu.systemutvikling.team6.models.registry.CharityRegistry; + +import ntnu.systemutvikling.team6.models.Charity; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -66,6 +67,15 @@ void testFindCharityByOrgNumber() { assertEquals(charity, result.get()); } + @Test + void testFindCharityByOrgNumberNullThrowsException() { + registry.addCharity(charity); + + assertThrows( + IllegalArgumentException.class, + () -> registry.findCharityByOrgnumber(null)); + } + @Test void testFindCharityByIdNotFound() { Optional result = registry.findCharityByUUID(UUID.randomUUID()); @@ -87,6 +97,16 @@ void testRemoveCharitySuccessfully() { assertTrue(registry.getAllCharities().isEmpty()); } + @Test + void testRemoveCharitySuccessfullyOrgNumber() { + registry.addCharity(charity); + + boolean removed = registry.removeCharity(charity.getOrg_number()); + + assertTrue(removed); + assertTrue(registry.getAllCharities().isEmpty()); + } + @Test void testRemoveCharityNotFound() { boolean removed = registry.removeCharityUUID(UUID.randomUUID()); @@ -97,4 +117,11 @@ void testRemoveCharityNotFound() { void testRemoveCharityNullThrowsException() { assertThrows(IllegalArgumentException.class, () -> registry.removeCharity(null)); } + + @Test + void testRemoveCharityUUIDNullThrowsException() { + UUID uuid = null; + assertThrows(IllegalArgumentException.class, () -> registry.removeCharityUUID(uuid)); + + } } diff --git a/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/models/DonationRegistryTest.java b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/models/registry/DonationRegistryTest.java similarity index 94% rename from helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/models/DonationRegistryTest.java rename to helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/models/registry/DonationRegistryTest.java index 77189b2c..3ac15c9e 100644 --- a/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/models/DonationRegistryTest.java +++ b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/models/registry/DonationRegistryTest.java @@ -1,4 +1,4 @@ -package ntnu.systemutvikling.team6.models; +package ntnu.systemutvikling.team6.models.registry; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.*; @@ -7,7 +7,9 @@ import java.util.List; import java.util.Optional; import java.util.UUID; -import ntnu.systemutvikling.team6.models.registry.DonationRegistry; + +import ntnu.systemutvikling.team6.models.Charity; +import ntnu.systemutvikling.team6.models.Donation; import ntnu.systemutvikling.team6.models.user.Settings; import ntnu.systemutvikling.team6.models.user.User; import org.junit.jupiter.api.BeforeEach; diff --git a/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/models/registry/UserRegistryTest.java b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/models/registry/UserRegistryTest.java new file mode 100644 index 00000000..f4ca6253 --- /dev/null +++ b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/models/registry/UserRegistryTest.java @@ -0,0 +1,106 @@ +package ntnu.systemutvikling.team6.models.registry; + +import ntnu.systemutvikling.team6.models.user.Inbox; +import ntnu.systemutvikling.team6.models.user.Role; +import ntnu.systemutvikling.team6.models.user.Settings; +import ntnu.systemutvikling.team6.models.user.User; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.Optional; +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.*; + +class UserRegistryTest { + User user; + UserRegistry userRegistry; + + @BeforeEach + void setUp() { + String validUsername = "username"; + String validEmail = "Email@gmail.com"; + String validPassword = "Password"; + Role validRole = Role.NORMAL_USER; + Settings validSettings = new Settings(); + Inbox validInbox = new Inbox(); + this.user = + new User( + validUsername, + validEmail, + validPassword, + validRole, + validSettings, + validInbox + ); + userRegistry = new UserRegistry(); + } + + @Test + void userRegistryShouldStartEmpty() { + assertEquals(0, userRegistry.getAllUsers().size()); + } + + @Test + void addUserTest() { + userRegistry.addUser(user); + + assertEquals(user, userRegistry.getAllUsers().getFirst()); + } + + @Test + void addingNullUserShouldThrow() { + assertThrows( + IllegalArgumentException.class, + () -> userRegistry.addUser(null) + ); + } + + @Test + void removeUserTest() { + userRegistry.addUser(user); + + userRegistry.removeUserByUUID(user.getId()); + + assertEquals(0, userRegistry.getAllUsers().size()); + } + + @Test + void removeNullUserShouldThrow() { + userRegistry.addUser(user); + + assertThrows( + IllegalArgumentException.class, + () -> userRegistry.removeUserByUUID(null) + ); + } + + @Test + void getUserByUUIDTest() { + userRegistry.addUser(user); + + Optional result = userRegistry.findUserById(user.getId()); + + assertTrue(result.isPresent()); + assertEquals(result.get().getId(), user.getId()); + } + + @Test + void getUserByUUIDNonExistentUserTest() { + userRegistry.addUser(user); + + Optional result = userRegistry.findUserById(UUID.randomUUID()); + + assertFalse(result.isPresent()); + } + + @Test + void getNullUserByUUIDShouldThrow() { + userRegistry.addUser(user); + + assertThrows( + IllegalArgumentException.class, + () -> userRegistry.findUserById(null) + ); + } +} \ No newline at end of file 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 index f3dc6ad5..dad9c3de 100644 --- a/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/models/user/InboxTest.java +++ b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/models/user/InboxTest.java @@ -5,6 +5,8 @@ import java.util.List; import java.util.Optional; import java.util.UUID; + +import ntnu.systemutvikling.team6.models.Charity; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -15,9 +17,14 @@ public class InboxTest { @BeforeEach public void setup() { + Charity charity = new Charity( + UUID.randomUUID().toString(), + "123", + false, + "approved"); inbox = new Inbox(); - newMessage = new Message("Title", UUID.randomUUID(), "Somewhere"); - newMessage2 = new Message("Title2", UUID.randomUUID(), "Somewhere2"); + newMessage = new Message("Title", charity, "Somewhere"); + newMessage2 = new Message("Title2", charity, "Somewhere2"); } @Test diff --git a/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/models/user/MessageTest.java b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/models/user/MessageTest.java new file mode 100644 index 00000000..9b41b666 --- /dev/null +++ b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/models/user/MessageTest.java @@ -0,0 +1,147 @@ +package ntnu.systemutvikling.team6.models.user; + +import static org.junit.jupiter.api.Assertions.*; +import java.time.LocalDate; +import java.util.UUID; +import ntnu.systemutvikling.team6.models.Charity; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; + +public class MessageTest { + private Charity charity; + + @BeforeEach + void setUp() { + charity = new Charity( + UUID.randomUUID().toString(), + "123", + false, + "approved"); + } + @Nested + class FirstConstructorTest { + @Test + void shouldThrowExceptionIfNameIsNullOrEmpty() { + assertThrows( + IllegalArgumentException.class, + () -> new Message( + null, + charity, + "Something Somewhere Somehow")); + assertThrows( + IllegalArgumentException.class, + () -> new Message("", + charity, + "Something Somewhere Somehow")); + } + + @Test + void shouldThrowExceptionIfFromIsNull() { + assertThrows( + IllegalArgumentException.class, + () -> new Message("Title", null, "Something Somewhere Somehow")); + } + + @Test + void shouldThrowExceptionIfContentIsNullOrEmpty() { + assertThrows( + IllegalArgumentException.class, () -> new Message( + "Title", + charity, + null)); + assertThrows(IllegalArgumentException.class, () -> new Message( + "Title", + charity, + "")); + } + + @Test + void GettersWork() { + Message newMessage = new Message( + "Title", + charity, + "Somewhere"); + assertInstanceOf(UUID.class, newMessage.getId()); + assertEquals("Title", newMessage.getTitle()); + assertEquals(charity, newMessage.getFrom()); + assertEquals("Somewhere", newMessage.getContent()); + assertEquals(LocalDate.now(), newMessage.getTimeAndDate()); + } + } + + @Nested + class secondConstructorTest { + @Test + void shouldThrowExceptionIfNameIsNullOrEmpty() { + assertThrows( + IllegalArgumentException.class, + () -> new Message( + null, + charity, + "Something Somewhere Somehow", + LocalDate.now())); + assertThrows( + IllegalArgumentException.class, + () -> new Message("", + charity, + "Something Somewhere Somehow", + LocalDate.now())); + } + + @Test + void shouldThrowExceptionIfFromIsNull() { + assertThrows( + IllegalArgumentException.class, + () -> new Message( + "Title", + null, + "Something Somewhere Somehow", + LocalDate.now())); + } + + @Test + void shouldThrowExceptionIfContentIsNullOrEmpty() { + assertThrows( + IllegalArgumentException.class, () -> new Message( + "Title", + charity, + null, + LocalDate.now())); + assertThrows(IllegalArgumentException.class, () -> new Message( + "Title", + charity, + "", + LocalDate.now())); + } + + @Test + void shouldThrowExceptionIfDateIsNull() { + assertThrows( + IllegalArgumentException.class, + () -> new Message( + "Title", + charity, + "Something Somewhere Somehow", + null)); + } + + @Test + void GettersWork() { + Message newMessage = new Message( + "Title", + charity, + "Somewhere", + LocalDate.now()); + assertInstanceOf(UUID.class, newMessage.getId()); + assertEquals("Title", newMessage.getTitle()); + assertEquals(charity, newMessage.getFrom()); + assertEquals("Somewhere", newMessage.getContent()); + assertEquals(LocalDate.now(), newMessage.getTimeAndDate()); + } + + } + + + +} diff --git a/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/models/user/MessegeTest.java b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/models/user/MessegeTest.java deleted file mode 100644 index 1faa9869..00000000 --- a/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/models/user/MessegeTest.java +++ /dev/null @@ -1,44 +0,0 @@ -package ntnu.systemutvikling.team6.models.user; - -import static org.junit.jupiter.api.Assertions.*; - -import java.time.LocalDate; -import java.util.UUID; -import org.junit.jupiter.api.Test; - -public class MessegeTest { - @Test - void shouldThrowExceptionIfNameIsNullOrEmpty() { - assertThrows( - IllegalArgumentException.class, - () -> new Message(null, UUID.randomUUID(), "Something Somewhere Somehow")); - assertThrows( - IllegalArgumentException.class, - () -> new Message("", UUID.randomUUID(), "Something Somewhere Somehow")); - } - - @Test - void shouldThrowExceptionIfFromIsNullOrEmpty() { - assertThrows( - IllegalArgumentException.class, - () -> new Message("Title", null, "Something Somewhere Somehow")); - } - - @Test - void shouldThrowExceptionIfContentIsNullOrEmpty() { - assertThrows( - IllegalArgumentException.class, () -> new Message("Title", UUID.randomUUID(), null)); - assertThrows(IllegalArgumentException.class, () -> new Message("Title", UUID.randomUUID(), "")); - } - - @Test - void GettersWork() { - UUID uuid = UUID.randomUUID(); - Message newMessage = new Message("Title", uuid, "Somewhere"); - assertInstanceOf(UUID.class, newMessage.getId()); - assertEquals("Title", newMessage.getTitle()); - assertEquals(uuid, newMessage.getFrom()); - assertEquals("Somewhere", newMessage.getContent()); - assertEquals(LocalDate.now(), newMessage.getTimeAndDate()); - } -} diff --git a/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/models/user/UserTest.java b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/models/user/UserTest.java index 0c9cc93d..d2a7c578 100644 --- a/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/models/user/UserTest.java +++ b/helpmehelpapplication/src/test/java/ntnu/systemutvikling/team6/models/user/UserTest.java @@ -5,11 +5,12 @@ import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; +import java.util.UUID; + class UserTest { @Nested class constructorTests { - private final String validDisplayName = "Name"; private final String validUsername = "username"; private final String validEmail = "Email@gmail.com"; private final String validPassword = "Password"; @@ -17,35 +18,6 @@ class constructorTests { private final Settings validSettings = new Settings(); private final Inbox validInbox = new Inbox(); - @Test - void shouldThrowIfDisplayNameIsNull() { - assertThrows( - IllegalArgumentException.class, - () -> - new User( - null, - validUsername, - validEmail, - validPassword, - validRole, - validSettings, - validInbox)); - } - - @Test - void shouldThrowIfDisplayNameIsBlank() { - assertThrows( - IllegalArgumentException.class, - () -> - new User( - " ", - validUsername, - validEmail, - validPassword, - validRole, - validSettings, - validInbox)); - } @Test void shouldThrowIfUsernameIsNull() { @@ -53,7 +25,6 @@ void shouldThrowIfUsernameIsNull() { IllegalArgumentException.class, () -> new User( - validDisplayName, null, validEmail, validPassword, @@ -68,7 +39,6 @@ void shouldThrowIfUsernameIsBlank() { IllegalArgumentException.class, () -> new User( - validDisplayName, " ", validEmail, validPassword, @@ -86,7 +56,6 @@ void shouldThrowIfEmailIsNull() { IllegalArgumentException.class, () -> new User( - validDisplayName, validUsername, null, validPassword, @@ -101,7 +70,6 @@ void shouldThrowIfEmailIsBlank() { IllegalArgumentException.class, () -> new User( - validDisplayName, validUsername, " ", validPassword, @@ -116,7 +84,6 @@ void shouldThrowIfEmailDoesNotContainAt() { IllegalArgumentException.class, () -> new User( - validDisplayName, validUsername, "test.gmail.com", validPassword, @@ -131,7 +98,6 @@ void shouldThrowIfEmailDoesNotContainPeriod() { IllegalArgumentException.class, () -> new User( - validDisplayName, validUsername, "test@gmailcom", validPassword, @@ -147,7 +113,6 @@ void shouldThrowIfPasswordIsNull() { IllegalArgumentException.class, () -> new User( - validDisplayName, validUsername, validEmail, null, @@ -162,7 +127,6 @@ void shouldThrowIfRoleIsNull() { IllegalArgumentException.class, () -> new User( - validDisplayName, validUsername, validEmail, validPassword, @@ -177,7 +141,6 @@ void shouldThrowIfPasswordIsBlank() { IllegalArgumentException.class, () -> new User( - validDisplayName, validUsername, validEmail, " ", @@ -192,7 +155,6 @@ void shouldThrowIfSettingsIsNull() { IllegalArgumentException.class, () -> new User( - validDisplayName, validUsername, validEmail, validPassword, @@ -207,7 +169,6 @@ void shouldThrowIfInboxIsNull() { IllegalArgumentException.class, () -> new User( - validDisplayName, validUsername, validEmail, validPassword, @@ -220,7 +181,6 @@ void shouldThrowIfInboxIsNull() { void shouldCreateUser() { User user = new User( - validDisplayName, validUsername, validEmail, validPassword, @@ -229,12 +189,462 @@ void shouldCreateUser() { validInbox); assertAll( - () -> assertEquals(validDisplayName, user.getDisplayName()), () -> assertEquals(validUsername, user.getUsername()), () -> assertEquals(validEmail, user.getEmail()), () -> assertEquals(validRole, user.getRole()), () -> assertEquals(validSettings, user.getSettings()), () -> assertEquals(validInbox, user.getInbox())); } + + @Test + void shouldBeCorrectPassword() { + User user = + new User( + validUsername, + validEmail, + validPassword, + validRole, + validSettings, + validInbox); + + assertTrue(user.checkPassword(validPassword)); + } + + @Test + void getPasswordHashShouldNotBePlaintext() { + User user = + new User( + validUsername, + validEmail, + validPassword, + validRole, + validSettings, + validInbox); + + String hash = user.getPasswordHash(); + + assertNotEquals(hash, validPassword); + } + + @Test + void getPasswordHashShouldUseSalt() { + User user1 = + new User( + validUsername, + validEmail, + validPassword, + validRole, + validSettings, + validInbox); + + User user2 = + new User( + validUsername, + validEmail, + validPassword, + validRole, + validSettings, + validInbox); + + assertNotEquals(user1.getPasswordHash(), user2.getPasswordHash()); + } + + @Test + void setNullUsernameShouldThrow() { + User user = + new User( + validUsername, + validEmail, + validPassword, + validRole, + validSettings, + validInbox); + + assertThrows( + IllegalArgumentException.class, + () -> user.setUsername(null)); + } + + @Test + void setBlankUsernameShouldThrow() { + User user = + new User( + validUsername, + validEmail, + validPassword, + validRole, + validSettings, + validInbox); + + assertThrows( + IllegalArgumentException.class, + () -> user.setUsername(" ")); + } + + @Test + void setUserNameShouldBeCorrect() { + User user = + new User( + validUsername, + validEmail, + validPassword, + validRole, + validSettings, + validInbox); + + String newUsername = "usernamer123"; + + user.setUsername(newUsername); + + assertEquals(newUsername, user.getUsername()); + } + + @Test + void setEmailShouldChangeEmail() { + User user = + new User( + validUsername, + validEmail, + validPassword, + validRole, + validSettings, + validInbox); + + String newEmail = "email@email.com"; + user.setEmail(newEmail); + + assertEquals(newEmail, user.getEmail()); + } + + @Test + void setNullEmailShouldThrow() { + User user = + new User( + validUsername, + validEmail, + validPassword, + validRole, + validSettings, + validInbox); + + assertThrows( + IllegalArgumentException.class, + () -> user.setEmail(null) + ); + } + + @Test + void setBlankEmailShouldThrow() { + User user = + new User( + validUsername, + validEmail, + validPassword, + validRole, + validSettings, + validInbox); + + assertThrows( + IllegalArgumentException.class, + () -> user.setEmail(" ") + ); + } + + @Test + void setEmailNotContainingAtSymbolShouldThrow() { + User user = + new User( + validUsername, + validEmail, + validPassword, + validRole, + validSettings, + validInbox); + + assertThrows( + IllegalArgumentException.class, + () -> user.setEmail("email.email.com") + ); + } + + @Test + void setEmailNotContainingPeriodShouldThrow() { + User user = + new User( + validUsername, + validEmail, + validPassword, + validRole, + validSettings, + validInbox); + + assertThrows( + IllegalArgumentException.class, + () -> user.setEmail("email@emailcom") + ); + } + + @Test + void setPasswordShouldChangePassword() { + User user = + new User( + validUsername, + validEmail, + validPassword, + validRole, + validSettings, + validInbox); + + String newPassword = "p@ssword"; + user.setPassword(newPassword); + + assertTrue(user.checkPassword(newPassword)); + assertFalse(user.checkPassword(validPassword)); + } + + @Test + void setSettingsShouldChangeSettings() { + User user = + new User( + validUsername, + validEmail, + validPassword, + validRole, + validSettings, + validInbox); + + Settings newSettings = new Settings(); + user.setSettings(newSettings); + + assertSame(newSettings, user.getSettings()); + } + + @Test + void setSettingsNullShouldThrow() { + User user = + new User( + validUsername, + validEmail, + validPassword, + validRole, + validSettings, + validInbox); + + assertThrows( + IllegalArgumentException.class, + () -> user.setSettings(null) + ); + } + + @Test + void setInboxShouldChangeInbox() { + User user = + new User( + validUsername, + validEmail, + validPassword, + validRole, + validSettings, + validInbox); + + Inbox newInbox = new Inbox(); + user.setInbox(newInbox); + + assertSame(newInbox, user.getInbox()); + } + + @Test + void setNullInboxShouldThrow() { + User user = + new User( + validUsername, + validEmail, + validPassword, + validRole, + validSettings, + validInbox); + + assertThrows( + IllegalArgumentException.class, + () -> user.setInbox(null) + ); + } + + @Test + void setRoleShouldChangeRole() { + User user = + new User( + validUsername, + validEmail, + validPassword, + validRole, + validSettings, + validInbox); + + user.setRole(Role.ADMIN); + + assertSame(Role.ADMIN, user.getRole()); + } + + @Test + void setNullRoleShouldThrow() { + User user = + new User( + validUsername, + validEmail, + validPassword, + validRole, + validSettings, + validInbox); + + assertThrows( + IllegalArgumentException.class, + () -> user.setRole(null) + ); + } } -} \ No newline at end of file + + @Nested + class secondConstructorTests { + private final UUID validUUID = UUID.randomUUID(); + private final String validUsername = "username"; + private final String validEmail = "Email@gmail.com"; + private final String validPassword = "Password"; + private final Role validRole = Role.NORMAL_USER; + + @Test + void shouldThrowIfUUIDIsNull() { + assertThrows( + IllegalArgumentException.class, + () -> + new User( + null, + validUsername, validEmail, + validPassword, + validRole.toString())); + } + + @Test + void shouldThrowIfUUIDIsBlank() { + assertThrows( + IllegalArgumentException.class, + () -> + new User( + " ", + validUsername, + validEmail, + validPassword, + validRole.toString())); + } + + @Test + void shouldThrowIfUsernameIsNull() { + assertThrows( + IllegalArgumentException.class, + () -> + new User( + validUUID.toString(), + null, + validEmail, + validPassword, + validRole.toString())); + + } + + @Test + void shouldThrowIfUsernameIsBlank() { + assertThrows( + IllegalArgumentException.class, + () -> + new User( + validUUID.toString(), + " ", + validEmail, + validPassword, + validRole.toString())); + } + + @Test + void shouldThrowIfEmailIsNull() { + assertThrows( + IllegalArgumentException.class, + () -> + new User( + validUUID.toString(), + validUsername, + null, + validPassword, + validRole.toString())); + } + + @Test + void shouldThrowIfEmailIsBlank() { + assertThrows( + IllegalArgumentException.class, + () -> + new User( + validUUID.toString(), + validUsername, + " ", + validPassword, + validRole.toString())); + } + + @Test + void shouldThrowIfEmailDoesNotContainAtSymbol() { + assertThrows( + IllegalArgumentException.class, + () -> + new User( + validUUID.toString(), + validUsername, + "emailemail.com", + validPassword, + validRole.toString())); + } + + @Test + void shouldThrowIfEmailDoesNotContainPeriod() { + assertThrows( + IllegalArgumentException.class, + () -> + new User( + validUUID.toString(), + validUsername, + "email@emailcom", + validPassword, + validRole.toString())); + } + + @Test + void shouldThrowIfRoleIsNull() { + assertThrows( + IllegalArgumentException.class, + () -> + new User( + validUUID.toString(), + validUsername, + validEmail, + validPassword, + null)); + } + + @Test + void shouldCreateUser() { + User user = + new User( + validUUID.toString(), + validUsername, + validEmail, + validPassword, + validRole.toString()); + + assertAll( + () -> assertEquals(validUUID, user.getId()), + () -> assertEquals(validUsername, user.getUsername()), + () -> assertEquals(validEmail, user.getEmail()), + () -> assertEquals(validRole, user.getRole())); + } + } + +}