diff --git a/google_checks.xml b/google_checks.xml new file mode 100644 index 0000000..2434e8d --- /dev/null +++ b/google_checks.xml @@ -0,0 +1,482 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/java/edu/group5/app/App.java b/src/main/java/edu/group5/app/App.java index 5886cd4..dc19a9f 100644 --- a/src/main/java/edu/group5/app/App.java +++ b/src/main/java/edu/group5/app/App.java @@ -1,5 +1,6 @@ package edu.group5.app; +import java.sql.Wrapper; import java.util.HashMap; import edu.group5.app.control.OrgAPIWrapper; @@ -11,14 +12,6 @@ */ public class App { public static void main(String[] args) throws InterruptedException { - OrgAPIWrapper orgWrap = new OrgAPIWrapper("https://app.innsamlingskontrollen.no/api/public/v1/all"); - System.out.println(); - System.out.println(); - orgWrap.importData(); - Object[] imports = orgWrap.getData(); - ObjectMapper objectMapper = new ObjectMapper(); - HashMap map = objectMapper.convertValue(imports[0], new TypeReference>() { - }); - System.out.println(map.get("org_number")); + Wrapper wrap = new Wrapper(); } } diff --git a/src/main/java/edu/group5/app/control/OrgAPIWrapper.java b/src/main/java/edu/group5/app/control/OrgApiWrapper.java similarity index 62% rename from src/main/java/edu/group5/app/control/OrgAPIWrapper.java rename to src/main/java/edu/group5/app/control/OrgApiWrapper.java index f8bc7ab..24c82ee 100644 --- a/src/main/java/edu/group5/app/control/OrgAPIWrapper.java +++ b/src/main/java/edu/group5/app/control/OrgApiWrapper.java @@ -5,16 +5,24 @@ import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; - import tools.jackson.core.exc.StreamReadException; import tools.jackson.databind.ObjectMapper; -public class OrgAPIWrapper extends Wrapper { +/** + * A Class for Wrapping an API. + */ +public class OrgApiWrapper extends Wrapper { private Object[] data; private HttpClient client; private HttpRequest request; - public OrgAPIWrapper(String urlString) { + /** + * The constructor, which takes a url String and constructs a URI and HttpRequest object from it. + * If the url is invalid, it will throw a fitting exception. + * + * @param urlString A string of the URL that's being connected to. + */ + public OrgApiWrapper(String urlString) { if (urlString == null) { throw new IllegalArgumentException("url can't be null"); } else if (urlString.isBlank()) { @@ -33,6 +41,15 @@ public OrgAPIWrapper(String urlString) { } + /** + * A method for importing data from the wrapped API. + * + * @return Returns a boolean, which indicates if the import was successful. Will be False if, for + * example, there is no internet connection. + * + * @throws InterruptedException This exception is thrown whenever the program is interrupted, like + * by ctrl + c. + */ @Override public boolean importData() throws InterruptedException { try { @@ -47,6 +64,11 @@ public boolean importData() throws InterruptedException { } } + /** + * A method for accessing the imported data. + * + * @return Returns an array with HashMaps, which is how data is structured in the API. + */ @Override public Object[] getData() { return this.data; diff --git a/src/main/java/edu/group5/app/control/Wrapper.java b/src/main/java/edu/group5/app/control/Wrapper.java index 012ea2c..ea55179 100644 --- a/src/main/java/edu/group5/app/control/Wrapper.java +++ b/src/main/java/edu/group5/app/control/Wrapper.java @@ -1,11 +1,28 @@ package edu.group5.app.control; +/** + * An abstract class for all Wrappers of datasets. + */ abstract class Wrapper { protected Wrapper() { } + /** + * An abstract method for importing data from the dataset that child methods wrap. + * + * @return Returns a boolean, which indicates if the import was successful. Will be False if, for + * example, there is no internet connection. + * + * @throws InterruptedException This exception is thrown whenever the program is interrupted, like + * by ctrl + c. + */ public abstract boolean importData() throws InterruptedException; + /** + * An abstract method to access the imported data. + * + * @return Returns a fitting parsed Object directly from the dataset. + */ public abstract Object getData(); } 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 be0e810..42844e1 100644 --- a/src/main/java/edu/group5/app/model/organization/Organization.java +++ b/src/main/java/edu/group5/app/model/organization/Organization.java @@ -7,13 +7,13 @@ * *

* An organization is identified by an organization number, a name, - * trust status, website URL, pre-approval status, and a textual description. + * 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
  • + *
  • name and websiteUrl must not be null or blank
  • *
  • description must not be null
  • *
*/ @@ -21,7 +21,7 @@ public record Organization( int orgNumber, String name, boolean trusted, - String websiteURL, + String websiteUrl, boolean isPreApproved, String description) { /** @@ -30,16 +30,16 @@ public record 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 + * @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 NullPointerException if name, websiteUrl or description is null * @throws IllegalArgumentException if orgNumber is negative, or if name or - * websiteURL is blank + * websiteUrl is blank */ - public Organization(int orgNumber, String name, boolean trusted, String websiteURL, boolean isPreApproved, + public Organization(int orgNumber, String name, boolean trusted, String websiteUrl, boolean isPreApproved, String description) { if (orgNumber < 0) { throw new IllegalArgumentException("orgNumber cannot be negative"); @@ -47,15 +47,15 @@ public Organization(int orgNumber, String name, boolean trusted, String websiteU 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.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"); + if (websiteUrl.isBlank()) { + throw new IllegalArgumentException("websiteUrl cannot be blank"); } } } diff --git a/src/test/java/edu/group5/app/control/OrgAPIWrapperTest.java b/src/test/java/edu/group5/app/control/OrgAPIWrapperTest.java deleted file mode 100644 index 6f22c85..0000000 --- a/src/test/java/edu/group5/app/control/OrgAPIWrapperTest.java +++ /dev/null @@ -1,74 +0,0 @@ -package edu.group5.app.control; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -import tools.jackson.core.exc.StreamReadException; - -import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import java.lang.IllegalArgumentException; - -public class OrgAPIWrapperTest { - String testURL; - String wrongURL; - String wrongURL2; - - @BeforeEach - void init() { - this.testURL = "https://app.innsamlingskontrollen.no/api/public/v1/all"; - this.wrongURL = "This is not a URL"; - this.wrongURL2 = "https://www.google.com"; - } - - @Test - public void nullURLThrowsException() { - IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, - () -> new OrgAPIWrapper(null)); - assertEquals("url can't be null", exception.getMessage()); - } - - @Test - public void emptyURLThrowsException() { - IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> new OrgAPIWrapper("")); - assertEquals("url can't be blank", exception.getMessage()); - } - - @Test - public void faultyURLThrowsException() { - IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, - () -> new OrgAPIWrapper(this.wrongURL)); - assertEquals("url has to be valid", exception.getMessage()); - } - - // @Test - // public void noConnectionReturnsFalseImport() { - // assertDoesNotThrow(() -> { - // OrgAPIWrapper api = new OrgAPIWrapper(this.testURL); - // assertFalse(api.importData()); - // }); - // } - - @Test - public void importsNonEmptyData() { - assertDoesNotThrow(() -> { - OrgAPIWrapper api = new OrgAPIWrapper(testURL); - assertTrue(api.importData()); - assertFalse(api.getData().length == 0); - }); - } - - @Test - public void nonParseableSiteThrowsExceptionWhenImporting() { - assertDoesNotThrow(() -> { - OrgAPIWrapper api = new OrgAPIWrapper(this.wrongURL2); - StreamReadException exception = assertThrows(StreamReadException.class, () -> api.importData()); - assertEquals("The URL leads to a website that can't be parsed", exception.getMessage()); - }); - } - -} diff --git a/src/test/java/edu/group5/app/control/OrgApiWrapperTest.java b/src/test/java/edu/group5/app/control/OrgApiWrapperTest.java new file mode 100644 index 0000000..53f08d7 --- /dev/null +++ b/src/test/java/edu/group5/app/control/OrgApiWrapperTest.java @@ -0,0 +1,98 @@ +package edu.group5.app.control; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.lang.IllegalArgumentException; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import tools.jackson.core.exc.StreamReadException; + +/** + * A test class for the OrgApiWrapper class. + */ +public class OrgApiWrapperTest { + String testUrl; + String wrongUrl; + String wrongUrl2; + + /** + * Initiates the urlStrings used for testing. + */ + @BeforeEach + void init() { + this.testUrl = "https://app.innsamlingskontrollen.no/api/public/v1/all"; + this.wrongUrl = "This is not a URL"; + this.wrongUrl2 = "https://www.google.com"; + } + + /** + * Checks if inputting null as the urlString throws the expected exception during construction. + */ + @Test + public void nullUrlThrowsException() { + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, + () -> new OrgApiWrapper(null)); + assertEquals("url can't be null", exception.getMessage()); + } + + /** + * Checks if inputting an empty urlString throws the expected exception during construction. + */ + @Test + public void emptyUrlThrowsException() { + IllegalArgumentException exception = assertThrows( + IllegalArgumentException.class, () -> new OrgApiWrapper("")); + assertEquals("url can't be blank", exception.getMessage()); + } + + /** + * Checks if an invalid urlString throws the expected exception during construction. + */ + @Test + public void faultyUrlThrowsException() { + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, + () -> new OrgApiWrapper(this.wrongUrl)); + assertEquals("url has to be valid", exception.getMessage()); + } + + // /** + // * Checks if import returns False when there's no internet connection. + // */ + // @Test + // public void noConnectionReturnsFalseImport() { + // assertDoesNotThrow(() -> { + // OrgAPIWrapper api = new OrgAPIWrapper(this.testUrl); + // assertFalse(api.importData()); + // }); + // } + + /** + * Checks if import actually imports something. + */ + @Test + public void importsNonEmptyData() { + assertDoesNotThrow(() -> { + OrgApiWrapper api = new OrgApiWrapper(testUrl); + assertTrue(api.importData()); + assertFalse(api.getData().length == 0); + }); + } + + /** + * Checks if an unparseable website throws the expected exception. + */ + @Test + public void nonParseableSiteThrowsExceptionWhenImporting() { + assertDoesNotThrow(() -> { + OrgApiWrapper api = new OrgApiWrapper(this.wrongUrl2); + StreamReadException exception = assertThrows( + StreamReadException.class, () -> api.importData()); + assertEquals("The URL leads to a website that can't be parsed", exception.getMessage()); + }); + } + +}