From e2760fbb7f9f8fe35a7087523f2591976f9f12b4 Mon Sep 17 00:00:00 2001 From: haavajor Date: Thu, 5 Mar 2026 11:36:45 +0100 Subject: [PATCH] Unit tests for Donation, User, and Organization classes --- .../domain/organization/Organization.java | 4 + src/main/java/persistence/UserDao.java | 2 +- src/test/java/domain/DonationTest.java | 115 ++++++++++ src/test/java/domain/OrganizationTest.java | 202 ++++++++++++++++++ src/test/java/domain/UserTest.java | 132 ++++++++++++ 5 files changed, 454 insertions(+), 1 deletion(-) diff --git a/src/main/java/domain/organization/Organization.java b/src/main/java/domain/organization/Organization.java index c4d5d3c..611e18a 100644 --- a/src/main/java/domain/organization/Organization.java +++ b/src/main/java/domain/organization/Organization.java @@ -21,6 +21,8 @@ public Organization(String name, String orgNr, Cause cause, String description, if (orgNr == null || !orgNr.matches("\\d{9}")) { throw new IllegalArgumentException("Organization number must be 9 digits"); } + + //Legg til exception for contactEmail == null if (contactEmail != null) { contactEmail = contactEmail.trim(); @@ -35,6 +37,7 @@ public Organization(String name, String orgNr, Cause cause, String description, } } + //Legg til exception for website == null if (website != null) { website = website.trim(); @@ -42,6 +45,7 @@ public Organization(String name, String orgNr, Cause cause, String description, throw new IllegalArgumentException("Website cannot be blank"); } + //Legg til feil for domain extension (.com f.eks) if (!website.matches("^https?://.+")) { //Got help from ChatGPT for regex throw new IllegalArgumentException("Website must start with http:// or https://"); } diff --git a/src/main/java/persistence/UserDao.java b/src/main/java/persistence/UserDao.java index 5a77341..bf9edc4 100644 --- a/src/main/java/persistence/UserDao.java +++ b/src/main/java/persistence/UserDao.java @@ -4,7 +4,7 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; -import model.User; +import domain.user.User; public class UserDao { diff --git a/src/test/java/domain/DonationTest.java b/src/test/java/domain/DonationTest.java index bb9c875..ea73049 100644 --- a/src/test/java/domain/DonationTest.java +++ b/src/test/java/domain/DonationTest.java @@ -1,5 +1,120 @@ package domain; +import domain.donation.Cause; +import domain.donation.Donation; +import domain.organization.Organization; +import domain.user.User; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.math.BigDecimal; +import java.time.Duration; +import java.time.LocalDateTime; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + class DonationTest { + //User Variables + private final String testUserName = "testUser"; + private final String testPhoneNumber = "90909090"; + private final String testEmail = "test@email.com"; + private final String testPassword = "password"; + + //Organization Variables + private final String testName = "Name"; + private final String testOrgNr = "123456789"; + private final Cause testCause = Cause.HEALTH; + private final String testDescription = "Description"; + private final String testContactEmail = "test@mail.com"; + private final String testWebsite = "https://test.com"; + private final boolean testVerified = true; + + //Donation Variables + private Long testID = 123L; + private BigDecimal testAmount = new BigDecimal("1500"); + private LocalDateTime testDateTime = LocalDateTime.of(2026, 3, 5, 8, 30, 0); + private User testUser; + private Organization testOrganization; + + @BeforeEach + public void init() { + testUser = new User(testUserName, testPhoneNumber, testPassword, testEmail); + testOrganization = new Organization(testName, testOrgNr, testCause, testDescription, testContactEmail, testWebsite, testVerified); + } + + + @Test + public void DonationTestPositive(){ + Donation donation = new Donation(testAmount, testUser, testOrganization); + } + + @Test + public void DonationTestUserNull(){ + testUser = null; + NullPointerException Exception = assertThrows(NullPointerException.class, + () -> new Donation (testAmount, testUser, testOrganization)); + + assertEquals("User cannot be null", Exception.getMessage()); + } + + @Test + public void DonationOrganizationNull(){ + testOrganization = null; + NullPointerException Exception = assertThrows(NullPointerException.class, + () -> new Donation (testAmount, testUser, testOrganization)); + + assertEquals("Organization cannot be null", Exception.getMessage()); + } + + @Test + public void DonationTestAmountNegative(){ + testAmount = new BigDecimal("-23"); + IllegalArgumentException Exception = assertThrows(IllegalArgumentException.class, + () -> new Donation (testAmount, testUser, testOrganization)); + + assertEquals("Amount must be greater than 0", Exception.getMessage()); + } + + //Getter tests + /* id is never set, getter makes a NullPointerException + @Test + public void DonationTestIdGet(){ + Donation donation = new Donation(testAmount, testUser, testOrganization); + assertEquals(0L, donation.getId()); + + } + */ + + @Test + public void DonationTestAmountGet(){ + Donation donation = new Donation(testAmount, testUser, testOrganization); + assertEquals(testAmount, donation.getAmount()); + + } + + @Test + public void DonationTestDateTimeGet(){ + LocalDateTime now = LocalDateTime.now(); + Donation donation = new Donation(testAmount, testUser, testOrganization); + + assertTrue(Duration.between(now, donation.getDateTime()).abs().toMillis() < 1000); + + } + + @Test + public void DonationTestUserGet(){ + Donation donation = new Donation(testAmount, testUser, testOrganization); + assertEquals(testUser, donation.getUser()); + + } + + @Test + public void DonationTestOrganizationGet(){ + Donation donation = new Donation(testAmount, testUser, testOrganization); + assertEquals(testOrganization, donation.getOrganization()); + + } } \ No newline at end of file diff --git a/src/test/java/domain/OrganizationTest.java b/src/test/java/domain/OrganizationTest.java index 815aa53..47c8024 100644 --- a/src/test/java/domain/OrganizationTest.java +++ b/src/test/java/domain/OrganizationTest.java @@ -1,5 +1,207 @@ package domain; +import domain.donation.Cause; +import domain.organization.Organization; +import domain.user.User; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + class OrganizationTest { + private String testName = "Name"; + private String testOrgNr = "123456789"; + private Cause testCause = Cause.HEALTH; + private String testDescription = "Description"; + private String testContactEmail = "test@mail.com"; + private String testWebsite = "https://test.com"; + private boolean testVerified = true; + + //Positive tests + @Test + public void organizationTestPositive(){ + Organization organization = new Organization(testName, testOrgNr, testCause, testDescription, testContactEmail, testWebsite, testVerified); + } + + //Negative tests + @Test + public void organizationTestNameBlank(){ + testName = ""; + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, + () -> new Organization("", testOrgNr, testCause, testDescription, testContactEmail, testWebsite, testVerified)); + + assertEquals("Name cannot be null or blank", exception.getMessage()); + } + + @Test + public void organizationTestNameNull(){ + testName = null; + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, + () -> new Organization(testName, testOrgNr, testCause, testDescription, testContactEmail, testWebsite, testVerified)); + + assertEquals("Name cannot be null or blank", exception.getMessage()); + } + + @Test + public void organizationTestOrgNrNull(){ + testOrgNr = null; + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, + () -> new Organization(testName, testOrgNr, testCause, testDescription, testContactEmail, testWebsite, testVerified)); + + assertEquals("Organization number must be 9 digits", exception.getMessage()); + } + + @Test + public void organizationTestOrgNrLetters(){ + testOrgNr = "12345678a"; + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, + () -> new Organization(testName, testOrgNr, testCause, testDescription, testContactEmail, testWebsite, testVerified)); + + assertEquals("Organization number must be 9 digits", exception.getMessage()); + } + + @Test + public void organizationTestOrgNrShort(){ + testOrgNr = "12345"; + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, + () -> new Organization(testName, testOrgNr, testCause, testDescription, testContactEmail, testWebsite, testVerified)); + + assertEquals("Organization number must be 9 digits", exception.getMessage()); + } + + /* Mangler exception for når email er null + @Test + public void organizationTestContactEmailNull(){ + testContactEmail = null; + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, + () -> new Organization(testName, testOrgNr, testCause, testDescription, testContactEmail, testWebsite, testVerified)); + } + */ + + @Test + public void organizationTestContactEmailTrim(){ + testContactEmail = "test@mail.com "; + Organization organization = new Organization(testName, testOrgNr, testCause, testDescription, testContactEmail, testWebsite, testVerified); + } + + @Test + public void organizationTestContactEmailBlank(){ + testContactEmail = ""; + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, + () -> new Organization(testName, testOrgNr, testCause, testDescription, testContactEmail, testWebsite, testVerified)); + + assertEquals("Email cannot be blank", exception.getMessage()); + } + + @Test + public void organizationTestContactEmailMissingAt(){ + testContactEmail = "test.com"; + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, + () -> new Organization(testName, testOrgNr, testCause, testDescription, testContactEmail, testWebsite, testVerified)); + + assertEquals("Invalid email format", exception.getMessage()); + } + + @Test + public void organizationTestContactEmailDoubleAt(){ + testContactEmail = "test@@mail.com"; + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, + () -> new Organization(testName, testOrgNr, testCause, testDescription, testContactEmail, testWebsite, testVerified)); + + assertEquals("Invalid email format", exception.getMessage()); + } + + @Test + public void organizationTestContactEmailMisplacedAt(){ + testContactEmail = "@testmail.com"; + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, + () -> new Organization(testName, testOrgNr, testCause, testDescription, testContactEmail, testWebsite, testVerified)); + + assertEquals("Invalid email format", exception.getMessage()); + } + + @Test + public void organizationTestContactEmailMissingDot(){ + testContactEmail = "test@mailcom"; + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, + () -> new Organization(testName, testOrgNr, testCause, testDescription, testContactEmail, testWebsite, testVerified)); + + assertEquals("Invalid email format", exception.getMessage()); + } + + @Test + public void organizationTestWebsiteMissingHttps(){ + testWebsite = "test.com"; + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, + () -> new Organization(testName, testOrgNr, testCause, testDescription, testContactEmail, testWebsite, testVerified)); + + assertEquals("Website must start with http:// or https://", exception.getMessage()); + } + + /* Mangler exception når url mangler domain extension (.com f.eks) + @Test + public void organizationTestWebsiteMissingDot(){ + testWebsite = "https://testcom"; + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, + () -> new Organization(testName, testOrgNr, testCause, testDescription, testContactEmail, testWebsite, testVerified)); + + assertEquals("Website must start with http:// or https://", exception.getMessage()); + } + */ + + /* Mangler exception når website er null + @Test + public void organizationTestWebsiteMissingDot(){ + testWebsite = null; + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, + () -> new Organization(testName, testOrgNr, testCause, testDescription, testContactEmail, testWebsite, testVerified)); + + assertEquals("Website must start with http:// or https://", exception.getMessage()); + } + */ + + + + //Getter tests + @Test + public void organizationTestNameGet(){ + Organization Organization = new Organization(testName, testOrgNr, testCause, testDescription, testContactEmail, testWebsite, testVerified); + assertEquals(testName, Organization.getName()); + } + + @Test + public void organizationTestOrgNrGet(){ + Organization Organization = new Organization(testName, testOrgNr, testCause, testDescription, testContactEmail, testWebsite, testVerified); + assertEquals(testOrgNr, Organization.getOrgNr()); + } + + @Test + public void organizationTestCauseGet(){ + Organization Organization = new Organization(testName, testOrgNr, testCause, testDescription, testContactEmail, testWebsite, testVerified); + assertEquals(testCause, Organization.getCause()); + } + + @Test + public void organizationDescriptionGet(){ + Organization Organization = new Organization(testName, testOrgNr, testCause, testDescription, testContactEmail, testWebsite, testVerified); + assertEquals(testDescription, Organization.getDescription()); + } + + @Test + public void organizationTestContactEmailGet(){ + Organization Organization = new Organization(testName, testOrgNr, testCause, testDescription, testContactEmail, testWebsite, testVerified); + assertEquals(testContactEmail, Organization.getContactEmail()); + } + + @Test + public void organizationTestWebsiteGet(){ + Organization Organization = new Organization(testName, testOrgNr, testCause, testDescription, testContactEmail, testWebsite, testVerified); + assertEquals(testWebsite, Organization.getWebsite()); + } + @Test + public void organizationTestIsVerifiedGet(){ + Organization Organization = new Organization(testName, testOrgNr, testCause, testDescription, testContactEmail, testWebsite, testVerified); + assertEquals(testVerified, Organization.isVerified()); + } } \ No newline at end of file diff --git a/src/test/java/domain/UserTest.java b/src/test/java/domain/UserTest.java index c21aa90..b1c8f14 100644 --- a/src/test/java/domain/UserTest.java +++ b/src/test/java/domain/UserTest.java @@ -1,5 +1,137 @@ package domain; +import domain.user.User; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + class UserTest { + private String testUserName = "testUser"; + private String testPhoneNumber = "90909090"; + private String testEmail = "test@email.com"; + private final String testPassword = "password"; + + //Positive tests + @Test + public void userTestPositive(){ + User User = new User(testUserName, testPhoneNumber, testPassword, testEmail); + } + + //Negative tests + @Test + public void userTestUserNameBlank(){ + testUserName = ""; + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, + () -> new User(testUserName, testPhoneNumber, testPassword, testEmail)); + + assertEquals("Username has to be filled in", exception.getMessage()); + } + + @Test + public void userTestUserNameNull(){ + testUserName = null; + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, + () -> new User(testUserName, testPhoneNumber, testPassword, testEmail)); + + assertEquals("Username has to be filled in", exception.getMessage()); + } + + @Test + public void userTestPhoneNumberBlank(){ + testPhoneNumber = ""; + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, + () -> new User(testUserName, testPhoneNumber, testPassword, testEmail)); + + assertEquals("Phonenumber has to be filled in", exception.getMessage()); + } + + @Test + public void userTestPhoneNumberNull(){ + testPhoneNumber = null; + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, + () -> new User(testUserName, testPhoneNumber, testPassword, testEmail)); + + assertEquals("Phonenumber has to be filled in", exception.getMessage()); + } + + @Test + public void userTestPhoneNumberShort() { + testPhoneNumber = "909090"; + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, + () -> new User(testUserName, testPhoneNumber, testPassword, testEmail)); + + assertEquals("Fill in a phonenumber with 8 digits", exception.getMessage()); + } + + @Test + public void userTestEmailBlank(){ + testEmail = ""; + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, + () -> new User(testUserName, testPhoneNumber, testPassword, testEmail)); + + assertEquals("E-mail has to be filled in", exception.getMessage()); + } + + @Test + public void userTestEmailNull(){ + testEmail = null; + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, + () -> new User(testUserName, testPhoneNumber, testPassword, testEmail)); + + assertEquals("E-mail has to be filled in", exception.getMessage()); + } + + //Getter tests + @Test + public void userTestUserNameGet(){ + User User = new User(testUserName, testPhoneNumber, testPassword, testEmail); + assertEquals(User.getUsername(), testUserName); + } + + @Test + public void userTestPhoneNumberGet(){ + User User = new User(testUserName, testPhoneNumber, testPassword, testEmail); + assertEquals(User.getPhoneNumber(), testPhoneNumber); + } + + @Test + public void userTestEmailGet(){ + User User = new User(testUserName, testPhoneNumber, testPassword, testEmail); + assertEquals(User.getEMail(), testEmail); + } + + @Test + public void userTestPasswordGet(){ + User User = new User(testUserName, testPhoneNumber, testPassword, testEmail); + assertEquals(User.getPassword(), testPassword); + } + + //Setter tests + @Test + public void userTestPasswordSet(){ + User User = new User(testUserName, testPhoneNumber, testPassword, testEmail); + String newPassword = "NewPassoword"; + User.setPassword(newPassword); + assertEquals(newPassword, User.getPassword()); + } + + @Test + public void userTestUserNameSet(){ + User User = new User(testUserName, testPhoneNumber, testPassword, testEmail); + String newUserName = "NewUsername"; + User.setUsername(newUserName); + assertEquals(newUserName, User.getUsername()); + } + + @Test + public void userTestPhoneNumberSet(){ + User User = new User(testUserName, testPhoneNumber, testPassword, testEmail); + String newPhoneNumber = "123456asdsad7"; //Trenger input validering + User.setPhonenumber(newPhoneNumber); + assertEquals(newPhoneNumber, User.getPhoneNumber()); + } + + + } \ No newline at end of file