-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feat/organizationRepo' of git.ntnu.no:Group-5/Help-Me-H…
…elp into feat/organizationRepo
- Loading branch information
Showing
5 changed files
with
197 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package edu.group5.app.model; | ||
|
|
||
| /** | ||
| * Represents a repository | ||
| */ | ||
| public abstract class Repository { | ||
| protected final Object content; | ||
|
|
||
| public Repository(Object content) { | ||
| this.content = content; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the content of the repo | ||
| * @return content of the repo | ||
| */ | ||
| public Object getContent() { | ||
| return content; | ||
| } | ||
| } |
49 changes: 47 additions & 2 deletions
49
src/main/java/edu/group5/app/model/organization/Organization.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,50 @@ | ||
| package edu.group5.app.model.organization; | ||
|
|
||
| public class Organization { | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| /** | ||
| * Represents an organization. | ||
| * | ||
| * <p>An organization is identified by an organization number, a name, | ||
| * trust status, website URL, pre-approval status, and a textual description. | ||
| * | ||
| * <p>Instances are validated on creation: | ||
| * <ul> | ||
| * <li>orgNumber must be non-negative</li> | ||
| * <li>name and websiteURL must not be null or blank</li> | ||
| * <li>description must not be null</li> | ||
| * </ul> | ||
| */ | ||
| 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"); | ||
| } | ||
| } | ||
| } |
40 changes: 40 additions & 0 deletions
40
src/main/java/edu/group5/app/model/organization/OrganizationRepo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package edu.group5.app.model.organization; | ||
|
|
||
| import edu.group5.app.model.Repository; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Objects; | ||
|
|
||
| /** | ||
| * Handles business logic associated with organizations | ||
| */ | ||
| public class OrganizationRepo extends Repository { | ||
| private final HashMap<Integer, Organization> content; | ||
|
|
||
| /** | ||
| * Creates a new Organization Repository | ||
| * | ||
| * @param content holds all current organizations in the repository | ||
| * @throws NullPointerException if content is null | ||
| */ | ||
| public OrganizationRepo(HashMap<Integer, Organization> content) { | ||
| this.content = Objects.requireNonNull(content, "content cannot be null"); | ||
| super(content); | ||
| } | ||
|
|
||
| /** | ||
| * Gets all trusted organizations in the repository | ||
| * @return all organizations with trusted = true | ||
| */ | ||
| public HashMap<Integer, Organization> getTrustedOrganizations() { | ||
| HashMap<Integer, Organization> trustedOrgs = new HashMap<>(); | ||
|
|
||
| content.forEach((orgNr, org) -> { | ||
| if (org.trusted()) { | ||
| trustedOrgs.put(orgNr, org); | ||
| } | ||
| }); | ||
|
|
||
| return trustedOrgs; | ||
| } | ||
| } |
4 changes: 4 additions & 0 deletions
4
src/test/java/edu/group5/app/model/organization/OrganizationRepoTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| import static org.junit.jupiter.api.Assertions.*; | ||
| class OrganizationRepoTest { | ||
|
|
||
| } |
86 changes: 86 additions & 0 deletions
86
src/test/java/edu/group5/app/model/organization/OrganizationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| package edu.group5.app.model.organization; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| class OrganizationTest { | ||
|
|
||
| @Test | ||
| void constructor_ThrowsWhenOrgNumberIsNegative() { | ||
| assertThrows(IllegalArgumentException.class, () -> {new Organization( | ||
| -1, | ||
| "Org", | ||
| true, | ||
| null, | ||
| 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 | ||
| ); | ||
| }); | ||
| } | ||
| } |