Skip to content

Commit

Permalink
Updated APICharityScraperTest.
Browse files Browse the repository at this point in the history
Changed test to use mocked http-responses using mockito for more reliable testing.
  • Loading branch information
roaraf committed Mar 3, 2026
1 parent 9ebb476 commit 61520b0
Showing 1 changed file with 72 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,39 +1,93 @@
package ntnu.sytemutvikling.team6.models;

import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.List;

import static org.mockito.Mockito.*;

import static org.junit.jupiter.api.Assertions.*;

class APICharityScraperTest {
@Test
void connectionToDataBaseShouldReturnTrue() throws IOException, URISyntaxException {
var con = new APICharityScraper();
void checkConnectionShouldReturnTrueOn200ResponseCode() throws IOException, URISyntaxException, InterruptedException {
HttpClient mockClient = mock(HttpClient.class);
var mockResponse = mock(HttpResponse.class);

when(mockResponse.statusCode()).thenReturn(200);
when(mockClient.send(any(HttpRequest.class), any(HttpResponse.BodyHandler.class))).thenReturn(mockResponse);

var con = new APICharityScraper(mockClient);

assertTrue(con.checkConnection(), "Get response should be 200 (success)");
}

@Test
void checkConnectionShouldThrowRuntimeExceptionIfConnectionIsNotASuccess() throws URISyntaxException, IOException, InterruptedException {
var mockClient = mock(HttpClient.class);
HttpResponse<String> mockResponse = mock(HttpResponse.class);

when(mockResponse.statusCode()).thenReturn(500);
when(mockResponse.body()).thenReturn("Internal server error.");
when(mockClient.send(any(HttpRequest.class), any(HttpResponse.BodyHandler.class))).thenReturn(mockResponse);

var con = new APICharityScraper(mockClient);

assertThrows(RuntimeException.class, con::checkConnection,
"The connection is not successful and should throw a RuntimeException.");
}

@Test
void ParsedJSONShouldHaveCorrectValues() throws IOException, URISyntaxException {
var con = new APICharityScraper();
String JSONData = "[{\"org_number\":\"938419264\",\"name\":\"Misjonsalliansen\",\"status\":\"approved\"," +
void getJSONDataShouldReturnCorrectJSONString() throws IOException, InterruptedException, URISyntaxException {
String response = "[{\"org_number\":\"938419264\",\"name\":\"Misjonsalliansen\",\"status\":\"approved\"," +
"\"url\":\"https:\\/\\/www.innsamlingskontrollen.no\\/organisasjoner\\/misjonsalliansen\\/\"," +
"\"is_pre_approved\":false}]";
var list = con.parseJSON(JSONData);

assertEquals("938419264", list.getFirst().getOrg_number(),
"Parsed organization number should be correct.");
assertEquals("Misjonsalliansen", list.getFirst().getName(),
"Parsed name should be correct");
assertEquals("approved", list.getFirst().getStatus(),
"Parsed status should be correct.");
assertEquals("https://www.innsamlingskontrollen.no/organisasjoner/misjonsalliansen/",
list.getFirst().getUrl(), "Parsed url should be correct.");
assertFalse(list.getFirst().getIs_pre_approved(), "Parsed is_pre_approved boolean should " +
"be correct.");

var mockClient = mock(HttpClient.class);
var mockResponse = mock(HttpResponse.class);

when(mockResponse.body()).thenReturn(response);
when(mockClient.send(any(HttpRequest.class), any(HttpResponse.BodyHandler.class))).thenReturn(mockResponse);

var con = new APICharityScraper(mockClient);

assertEquals(response, con.getJSONData(), "The extracted JSON data should be the same " +
"as the data from the API.");
}


@Test
void parsedJSONShouldHaveCorrectValues() throws URISyntaxException {
APICharityScraper con = new APICharityScraper(HttpClient.newHttpClient());

String JSONData = "[{\"org_number\":\"938419264\",\"name\":\"Misjonsalliansen\",\"status\":\"approved\"," +
"\"url\":\"https://www.innsamlingskontrollen.no/organisasjoner/misjonsalliansen/\",\"" +
"is_pre_approved\":false}]";
List<APICharityData> list = con.parseJSON(JSONData);

APICharityData d = list.getFirst();

assertEquals("938419264", d.getOrg_number(), "Org_number parameter " +
"should be correct.");
assertEquals("Misjonsalliansen", d.getName(), "Name parameter should be correct.");
assertEquals("approved", d.getStatus(), "Status parameter should be correct.");
assertEquals("https://www.innsamlingskontrollen.no/organisasjoner/misjonsalliansen/", d.getUrl(),
"Url parameter should be correct.");
assertFalse(d.getIs_pre_approved(), "Is_pre_approved parameter should be correct.");
}

@Test
void parsedJSONOfNullShouldReturnEmptyList() throws URISyntaxException {
APICharityScraper con = new APICharityScraper(HttpClient.newHttpClient());

List<APICharityData> list = con.parseJSON(null);

assertTrue(list.isEmpty(), "List should be empty due to only parsing " +
"null objects.");
}

}

0 comments on commit 61520b0

Please sign in to comment.