diff --git a/src/main/java/edu/group5/app/model/Repository.java b/src/main/java/edu/group5/app/model/Repository.java new file mode 100644 index 0000000..6850bfe --- /dev/null +++ b/src/main/java/edu/group5/app/model/Repository.java @@ -0,0 +1,22 @@ +package edu.group5.app.model; + +import java.util.Map; + +/** + * Represents a repository + */ +public abstract class Repository { + protected final Map content; + + public Repository(Map content) { + this.content = content; + } + + /** + * Gets the content of the repo + * @return content of the repo + */ + public Map getContent() { + return content; + } +} diff --git a/src/main/java/edu/group5/app/model/organization/Organization.java b/src/main/java/edu/group5/app/model/organization/Organization.java index 825945f..941ed5a 100644 --- a/src/main/java/edu/group5/app/model/organization/Organization.java +++ b/src/main/java/edu/group5/app/model/organization/Organization.java @@ -1,5 +1,56 @@ package edu.group5.app.model.organization; -public class Organization { - +import java.util.Objects; + +/** + * Represents an organization. + * + *

An organization is identified by an organization number, a name, + * trust status, website URL, pre-approval status, and a textual description. + * + *

Instances are validated on creation: + *

    + *
  • orgNumber must be non-negative
  • + *
  • name and websiteURL must not be null or blank
  • + *
  • description must not be null
  • + *
+ */ +public record Organization( + int orgNumber, + String name, + boolean trusted, + String websiteURL, + boolean isPreApproved, + String description +) { + /** + * Creates a new organization. + * + * @param orgNumber the organization number; must be non-negative + * @param name the organization name; must not be null or blank + * @param trusted whether the organization is trusted + * @param websiteURL the organization's website URL; must not be null or blank + * @param isPreApproved whether the organization is pre-approved + * @param description a textual description of the organization; must not be null + * @throws NullPointerException if name, websiteURL or description is null + * @throws IllegalArgumentException if orgNumber is negative, or if name or websiteURL is blank + */ + public Organization(int orgNumber, String name, boolean trusted, String websiteURL, boolean isPreApproved, String description) { + if (orgNumber < 0) { + throw new IllegalArgumentException("orgNumber cannot be negative"); + } + this.orgNumber = orgNumber; + this.name = Objects.requireNonNull(name, "name cannot be null"); + this.trusted = trusted; + this.websiteURL = Objects.requireNonNull(websiteURL, "websiteURL cannot be null"); + this.isPreApproved = isPreApproved; + this.description = Objects.requireNonNull(description, "description cannot be null"); + + if (name.isBlank()) { + throw new IllegalArgumentException("name cannot be blank"); + } + if (websiteURL.isBlank()) { + throw new IllegalArgumentException("websiteURL cannot be blank"); + } + } } diff --git a/src/main/java/edu/group5/app/model/organization/OrganizationRepository.java b/src/main/java/edu/group5/app/model/organization/OrganizationRepository.java new file mode 100644 index 0000000..14a77d0 --- /dev/null +++ b/src/main/java/edu/group5/app/model/organization/OrganizationRepository.java @@ -0,0 +1,38 @@ +package edu.group5.app.model.organization; + +import edu.group5.app.model.Repository; + +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; + +/** + * Handles the business logic associated with organizations + */ +public class OrganizationRepository extends Repository { + /** + * Creates a new Organization Repository + * + * @param content holds all current organizations in the repository; must not be null + * @throws NullPointerException if content is null + */ + public OrganizationRepository(Map content) { + super(Objects.requireNonNull(content, "content cannot be null")); + } + + /** + * Gets all trusted organizations in the repository + * @return all organizations with trusted = true + */ + public Map getTrustedOrganizations() { + Map trustedOrganizations = new HashMap<>(); + + content.forEach((orgNr, org) -> { + if (org.trusted()) { + trustedOrganizations.put(orgNr, org); + } + }); + + return trustedOrganizations; + } +} diff --git a/src/test/java/edu/group5/app/model/organization/OrganizationRepositoryTest.java b/src/test/java/edu/group5/app/model/organization/OrganizationRepositoryTest.java new file mode 100644 index 0000000..3a3392e --- /dev/null +++ b/src/test/java/edu/group5/app/model/organization/OrganizationRepositoryTest.java @@ -0,0 +1,48 @@ +package edu.group5.app.model.organization; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.HashMap; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.*; + +class OrganizationRepositoryTest { + + private OrganizationRepository repository; + + @BeforeEach + void setUp() { + Map content = new HashMap<>(); + + Organization trustedOrganization1 = new Organization(1, "Trusted Org1", true, "org.com", true, "description"); + Organization trustedOrganization2 = new Organization(2, "Trusted Org2", true, "org.com", true, "description"); + Organization untrustedOrganization1 = new Organization(3, "Untrusted Org1", false, "org.com", true, "description"); + Organization untrustedOrganization2 = new Organization(4, "Untrusted Org2", false, "org.com", true, "description"); + + content.put(1, trustedOrganization1); + content.put(2, trustedOrganization2); + content.put(3, untrustedOrganization1); + content.put(4, untrustedOrganization2); + + repository = new OrganizationRepository(content); + } + + @Test + void constructor_ThrowsWhenContentIsNull() { + assertThrows(NullPointerException.class, () -> new OrganizationRepository(null)); + } + + @Test + void getTrustedOrganizations_OnlyReturnsTrustedOrganizations() { + Map trusted = repository.getTrustedOrganizations(); + + assertEquals(2, trusted.size()); + assertTrue(trusted.containsKey(1)); + assertTrue(trusted.containsKey(2)); + assertFalse(trusted.containsKey(3)); + assertFalse(trusted.containsKey(4)); + assertTrue(trusted.values().stream().allMatch(Organization::trusted)); + } +} \ No newline at end of file diff --git a/src/test/java/edu/group5/app/model/organization/OrganizationTest.java b/src/test/java/edu/group5/app/model/organization/OrganizationTest.java index 45e04ca..7f1b898 100644 --- a/src/test/java/edu/group5/app/model/organization/OrganizationTest.java +++ b/src/test/java/edu/group5/app/model/organization/OrganizationTest.java @@ -1,5 +1,101 @@ package edu.group5.app.model.organization; -public class OrganizationTest { - -} +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class OrganizationTest { + + @Test + void constructor_CreatesAnOrganizationWhenInputIsValid() { + Organization org = new Organization( + 1, + "Org", + true, + "org.com", + true, + "Org description" + ); + + assertAll( + () -> assertEquals(1, org.orgNumber()), + () -> assertEquals("Org", org.name()), + () -> assertTrue(org.trusted()), + () -> assertEquals("org.com", org.websiteURL()), + () -> assertTrue(org.isPreApproved()), + () -> assertEquals("Org description", org.description()) + ); + } + + @Test + void constructor_ThrowsWhenOrgNumberIsNegative() { + assertThrows(IllegalArgumentException.class, () -> new Organization( + -1, + "Org", + true, + "org.com", + true, + "Org description" + )); + } + + @Test + void constructor_ThrowsWhenNameIsNull() { + assertThrows(NullPointerException.class, () -> new Organization( + 1, + null, + true, + "org.com", + true, + "Org description" + )); + } + + @Test + void constructor_ThrowsWhenNameIsBlank() { + assertThrows(IllegalArgumentException.class, () -> new Organization( + 1, + "", + true, + "org.com", + true, + "Org description" + )); + } + + @Test + void constructor_ThrowsWhenWebsiteURLIsNull() { + assertThrows(NullPointerException.class, () -> new Organization( + 1, + "Org", + true, + null, + true, + "Org description" + )); + } + + @Test + void constructor_ThrowsWhenWebsiteURLIsBlank() { + assertThrows(IllegalArgumentException.class, () -> new Organization( + 1, + "Org", + true, + "", + true, + "Org description" + )); + } + + @Test + void constructor_ThrowsWhenDescriptionIsNull() { + assertThrows(NullPointerException.class, () -> new Organization( + 1, + "Org", + true, + "org.com", + true, + null + )); + } +} \ No newline at end of file