From ec08a6b232c20405f1c7169d2c6419429bf6341e Mon Sep 17 00:00:00 2001 From: Hammy Date: Sun, 13 Jun 2021 07:18:29 +0100 Subject: [PATCH] Add first test for APIWrapper --- src/main/java/me/goudham/APIWrapper.java | 12 ++- src/test/java/me/goudham/APIWrapperTest.java | 108 +++++++++++++++++++ 2 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 src/test/java/me/goudham/APIWrapperTest.java diff --git a/src/main/java/me/goudham/APIWrapper.java b/src/main/java/me/goudham/APIWrapper.java index a7fca8a..7af966a 100644 --- a/src/main/java/me/goudham/APIWrapper.java +++ b/src/main/java/me/goudham/APIWrapper.java @@ -36,7 +36,7 @@ import static me.goudham.APIUtils.paginationData; public class APIWrapper { private final String version = "1.0"; private static final String host = "https://mywaifulist.moe/api/v1/"; - private final String apiKey; + private String apiKey; private final APIMapper apiMapper; private final HttpClient httpClient; @@ -67,7 +67,7 @@ public class APIWrapper { * cannot be decoded or the thread was interrupted while waiting to receive the data * */ - private Result sendRequest(String param) throws APIResponseException { + Result sendRequest(String param) throws APIResponseException { CompletableFuture futureResult = CompletableFuture.supplyAsync(() -> { HttpRequest request = HttpRequest.newBuilder() .uri(URI.create(host + param)) @@ -329,4 +329,12 @@ public class APIWrapper { Result userProfileResult = sendRequest("user/" + userId + "/lists/" + listId); return apiMapper.deserialize(userProfileResult, UserList.class); } + + void setApiKey(String apiKey) { + this.apiKey = apiKey; + } + + String getApiKey() { + return apiKey; + } } diff --git a/src/test/java/me/goudham/APIWrapperTest.java b/src/test/java/me/goudham/APIWrapperTest.java new file mode 100644 index 0000000..7f10a8c --- /dev/null +++ b/src/test/java/me/goudham/APIWrapperTest.java @@ -0,0 +1,108 @@ +package me.goudham; + +import me.goudham.exception.APIResponseException; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import javax.net.ssl.SSLSession; +import java.io.IOException; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpHeaders; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.time.Duration; +import java.util.Optional; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.mockito.Mockito.*; + +class APIWrapperTest { + + @Mock + private + HttpClient httpClient; + + private APIWrapper sut; + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + sut = new APIWrapper("ValidAPIKey", httpClient); + } + + @Test + void successfullyReturn400WhenApiTokenInvalid() throws APIResponseException, IOException, InterruptedException { + sut.setApiKey("InvalidAPIKey"); + HttpRequest expectedHttpRequest = buildHttpRequest(sut); + HttpResponse expectedHttpResponse = build400HttpResponse(); + + doReturn(expectedHttpResponse).when(httpClient).send(expectedHttpRequest, HttpResponse.BodyHandlers.ofString()); + doReturn(HttpClient.Version.HTTP_2).when(httpClient).version(); + + Result actualResult = sut.sendRequest("waifu/1"); + + assertThat(actualResult.getStatusCode(), is(400)); + assertThat(actualResult.getBody(), is("{\"message\":\"Access denied - please check your token\",\"code\":400}")); + verify(httpClient, times(1)).send(expectedHttpRequest, HttpResponse.BodyHandlers.ofString()); + verify(httpClient, times(1)).version(); + verifyNoMoreInteractions(httpClient); + } + + private HttpRequest buildHttpRequest(APIWrapper sut) { + return HttpRequest.newBuilder() + .uri(URI.create("https://mywaifulist.moe/api/v1/waifu/1")) + .version(HttpClient.Version.HTTP_2) + .timeout(Duration.ofSeconds(20)) + .headers("Content-Type", "application/json", "apikey", sut.getApiKey()) + .GET() + .build(); + } + + private HttpResponse build400HttpResponse() { + return new HttpResponse<>() { + @Override + public int statusCode() { + return 400; + } + + @Override + public HttpRequest request() { + return null; + } + + @Override + public Optional> previousResponse() { + return Optional.empty(); + } + + @Override + public HttpHeaders headers() { + return null; + } + + @Override + public String body() { + return "{\"message\":\"Access denied - please check your token\",\"code\":400}"; + } + + @Override + public Optional sslSession() { + return Optional.empty(); + } + + @Override + public URI uri() { + return null; + } + + @Override + public HttpClient.Version version() { + return null; + } + }; + } +} \ No newline at end of file