diff --git a/src/main/java/domain/organization/Organization.java b/src/main/java/domain/organization/Organization.java index f0a470a..eaeb838 100644 --- a/src/main/java/domain/organization/Organization.java +++ b/src/main/java/domain/organization/Organization.java @@ -20,7 +20,7 @@ public class Organization { @JsonProperty("status") private String status; - + @JsonProperty("url") private String url; @@ -29,7 +29,6 @@ public class Organization { public Organization() {} - public String getOrgNumber() { return orgNumber; } public void setOrgNumber(String orgNumber) { this.orgNumber = orgNumber; } 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