From 786c6990cb748b8c4d690696168f3d0d2cd0ee0f Mon Sep 17 00:00:00 2001 From: Hammy Date: Mon, 14 Jun 2021 02:06:31 +0100 Subject: [PATCH] Update tests --- src/test/java/me/goudham/APIWrapperTest.java | 42 ++++++++++++++------ 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/src/test/java/me/goudham/APIWrapperTest.java b/src/test/java/me/goudham/APIWrapperTest.java index 7f10a8c..22fd00b 100644 --- a/src/test/java/me/goudham/APIWrapperTest.java +++ b/src/test/java/me/goudham/APIWrapperTest.java @@ -36,37 +36,53 @@ class APIWrapperTest { @Test void successfullyReturn400WhenApiTokenInvalid() throws APIResponseException, IOException, InterruptedException { + HttpRequest expectedHttpRequest = buildHttpRequest("InvalidAPIKey"); + int expectedStatusCode = 400; + String expectedBody = "{\"message\":\"Access denied - please check your token\",\"code\":400}"; + HttpResponse expectedHttpResponse = buildHttpResponse(expectedStatusCode, expectedBody); + + doReturn(expectedHttpResponse).when(httpClient).send(expectedHttpRequest, HttpResponse.BodyHandlers.ofString()); + sut.setApiKey("InvalidAPIKey"); - HttpRequest expectedHttpRequest = buildHttpRequest(sut); - HttpResponse expectedHttpResponse = build400HttpResponse(); + Result actualResult = sut.sendGetRequest("waifu/1"); + + assertThat(actualResult.getStatusCode(), is(expectedStatusCode)); + assertThat(actualResult.getBody(), is(expectedBody)); + verify(httpClient, times(1)).send(expectedHttpRequest, HttpResponse.BodyHandlers.ofString()); + verifyNoMoreInteractions(httpClient); + } + + @Test + void successfullyReturn200WhenApiTokenValid() throws APIResponseException, IOException, InterruptedException { + HttpRequest expectedHttpRequest = buildHttpRequest("ValidAPIKey"); + int expectedStatusCode = 200; + String expectedBody = "{\"message\":\"Token Valid\",\"code\":200}"; + HttpResponse expectedHttpResponse = buildHttpResponse(expectedStatusCode, expectedBody); doReturn(expectedHttpResponse).when(httpClient).send(expectedHttpRequest, HttpResponse.BodyHandlers.ofString()); - doReturn(HttpClient.Version.HTTP_2).when(httpClient).version(); - Result actualResult = sut.sendRequest("waifu/1"); + Result actualResult = sut.sendGetRequest("waifu/1"); - assertThat(actualResult.getStatusCode(), is(400)); - assertThat(actualResult.getBody(), is("{\"message\":\"Access denied - please check your token\",\"code\":400}")); + assertThat(actualResult.getStatusCode(), is(expectedStatusCode)); + assertThat(actualResult.getBody(), is(expectedBody)); verify(httpClient, times(1)).send(expectedHttpRequest, HttpResponse.BodyHandlers.ofString()); - verify(httpClient, times(1)).version(); verifyNoMoreInteractions(httpClient); } - private HttpRequest buildHttpRequest(APIWrapper sut) { + private HttpRequest buildHttpRequest(String apiKey) { 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()) + .headers("Content-Type", "application/json", "apikey", apiKey) .GET() .build(); } - private HttpResponse build400HttpResponse() { + private HttpResponse buildHttpResponse(int statusCode, String body) { return new HttpResponse<>() { @Override public int statusCode() { - return 400; + return statusCode; } @Override @@ -86,7 +102,7 @@ class APIWrapperTest { @Override public String body() { - return "{\"message\":\"Access denied - please check your token\",\"code\":400}"; + return body; } @Override