Skip to content

Commit

Permalink
test[OrgAPIWrapper]: add tests for OrgAPIWrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucy Ciara Herud-Thomassen authored and Lucy Ciara Herud-Thomassen committed Mar 3, 2026
1 parent a3752a2 commit fecaec9
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 17 deletions.
1 change: 0 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
<scope>compile</scope>
</dependency>


</dependencies>

<build>
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/edu/group5/app/App.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
package edu.group5.app;

import java.util.HashMap;

import edu.group5.app.control.OrgAPIWrapper;
import tools.jackson.core.type.TypeReference;
import tools.jackson.databind.ObjectMapper;

/**
* Hello world!
*/
public class App {
public static void main(String[] args) {
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"));
}
}
40 changes: 26 additions & 14 deletions src/main/java/edu/group5/app/control/OrgAPIWrapper.java
Original file line number Diff line number Diff line change
@@ -1,37 +1,49 @@
package edu.group5.app.control;

import java.io.IOException;
import java.net.URI;
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 {
private String urlString;
private Object[] data;
private HttpClient client;
private HttpRequest request;

public OrgAPIWrapper(String urlString) {
this.urlString = urlString;
if (urlString == null) {
throw new IllegalArgumentException("url can't be null");
} else if (urlString.isBlank()) {
throw new IllegalArgumentException("url can't be blank");
}
try {
URI uri = URI.create(urlString);
this.client = HttpClient.newHttpClient();
this.request = HttpRequest.newBuilder()
.uri(uri)
.GET()
.build();
} catch (IllegalArgumentException IAe) {
throw new IllegalArgumentException("url has to be valid");
}

}

@Override
public boolean importData() {
URI uri = URI.create(this.urlString);
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(uri)
.GET()
.build();

public boolean importData() throws InterruptedException {
try {
HttpResponse<String> response = client.send(
request, HttpResponse.BodyHandlers.ofString());
HttpResponse<String> response = this.client.send(
this.request, HttpResponse.BodyHandlers.ofString());
this.data = new ObjectMapper().readValue(response.body(), Object[].class);
return true;
} catch (Exception e) {
e.printStackTrace();
} catch (IOException IOe) {
return false;
} catch (StreamReadException e) {
throw new StreamReadException("The URL leads to a website that can't be parsed");
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/edu/group5/app/control/Wrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ abstract class Wrapper {
protected Wrapper() {
}

public abstract boolean importData();
public abstract boolean importData() throws InterruptedException;

public abstract Object getData();
}

0 comments on commit fecaec9

Please sign in to comment.