Skip to content

Feat/wrapper #43

Merged
merged 3 commits into from
Mar 7, 2026
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
482 changes: 482 additions & 0 deletions google_checks.xml

Large diffs are not rendered by default.

11 changes: 2 additions & 9 deletions src/main/java/edu/group5/app/App.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package edu.group5.app;

import java.sql.Wrapper;
import java.util.HashMap;

import edu.group5.app.control.OrgAPIWrapper;
Expand All @@ -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<String, Object> map = objectMapper.convertValue(imports[0], new TypeReference<HashMap<String, Object>>() {
});
System.out.println(map.get("org_number"));
Wrapper wrap = new Wrapper();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -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 {
Expand All @@ -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;
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/edu/group5/app/control/Wrapper.java
Original file line number Diff line number Diff line change
@@ -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();
}
20 changes: 10 additions & 10 deletions src/main/java/edu/group5/app/model/organization/Organization.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@
*
* <p>
* 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.
*
* <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>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,
String websiteUrl,
boolean isPreApproved,
String description) {
/**
Expand All @@ -30,32 +30,32 @@ 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");
}
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");
}
}
}
74 changes: 0 additions & 74 deletions src/test/java/edu/group5/app/control/OrgAPIWrapperTest.java

This file was deleted.

98 changes: 98 additions & 0 deletions src/test/java/edu/group5/app/control/OrgApiWrapperTest.java
Original file line number Diff line number Diff line change
@@ -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());
});
}

}