Add first test for APIWrapper

pull/12/head
Hammy 3 years ago
parent 34d7543ff4
commit ec08a6b232

@ -36,7 +36,7 @@ import static me.goudham.APIUtils.paginationData;
public class APIWrapper { public class APIWrapper {
private final String version = "1.0"; private final String version = "1.0";
private static final String host = "https://mywaifulist.moe/api/v1/"; private static final String host = "https://mywaifulist.moe/api/v1/";
private final String apiKey; private String apiKey;
private final APIMapper apiMapper; private final APIMapper apiMapper;
private final HttpClient httpClient; 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 * 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<Result> futureResult = CompletableFuture.supplyAsync(() -> { CompletableFuture<Result> futureResult = CompletableFuture.supplyAsync(() -> {
HttpRequest request = HttpRequest.newBuilder() HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(host + param)) .uri(URI.create(host + param))
@ -329,4 +329,12 @@ public class APIWrapper {
Result userProfileResult = sendRequest("user/" + userId + "/lists/" + listId); Result userProfileResult = sendRequest("user/" + userId + "/lists/" + listId);
return apiMapper.deserialize(userProfileResult, UserList.class); return apiMapper.deserialize(userProfileResult, UserList.class);
} }
void setApiKey(String apiKey) {
this.apiKey = apiKey;
}
String getApiKey() {
return apiKey;
}
} }

@ -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<String> 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<String> build400HttpResponse() {
return new HttpResponse<>() {
@Override
public int statusCode() {
return 400;
}
@Override
public HttpRequest request() {
return null;
}
@Override
public Optional<HttpResponse<String>> 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> sslSession() {
return Optional.empty();
}
@Override
public URI uri() {
return null;
}
@Override
public HttpClient.Version version() {
return null;
}
};
}
}
Loading…
Cancel
Save