|
|
@ -1,5 +1,9 @@
|
|
|
|
package me.goudham;
|
|
|
|
package me.goudham;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.JavaType;
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
import me.goudham.domain.pagination.PaginationData;
|
|
|
|
import me.goudham.domain.pagination.PaginationData;
|
|
|
|
import me.goudham.domain.waifu.FilteredWaifu;
|
|
|
|
import me.goudham.domain.waifu.FilteredWaifu;
|
|
|
|
import me.goudham.domain.waifu.Waifu;
|
|
|
|
import me.goudham.domain.waifu.Waifu;
|
|
|
@ -11,6 +15,7 @@ import org.junit.jupiter.api.BeforeEach;
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
import org.mockito.Mock;
|
|
|
|
import org.mockito.Mock;
|
|
|
|
import org.mockito.MockitoAnnotations;
|
|
|
|
import org.mockito.MockitoAnnotations;
|
|
|
|
|
|
|
|
import org.mockito.Spy;
|
|
|
|
|
|
|
|
|
|
|
|
import javax.net.ssl.SSLSession;
|
|
|
|
import javax.net.ssl.SSLSession;
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.BufferedReader;
|
|
|
@ -29,143 +34,211 @@ import java.util.Objects;
|
|
|
|
import java.util.Optional;
|
|
|
|
import java.util.Optional;
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import static me.goudham.APIUtils.listOf;
|
|
|
|
import static org.hamcrest.MatcherAssert.assertThat;
|
|
|
|
import static org.hamcrest.MatcherAssert.assertThat;
|
|
|
|
import static org.hamcrest.Matchers.is;
|
|
|
|
import static org.hamcrest.Matchers.is;
|
|
|
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
|
|
|
import static org.mockito.Mockito.*;
|
|
|
|
import static org.mockito.Mockito.*;
|
|
|
|
|
|
|
|
|
|
|
|
class MyWaifuClientTest {
|
|
|
|
class MyWaifuClientTest {
|
|
|
|
|
|
|
|
|
|
|
|
@Mock
|
|
|
|
@Mock
|
|
|
|
private HttpClient httpClient;
|
|
|
|
private HttpClient httpClient;
|
|
|
|
|
|
|
|
|
|
|
|
private final String apiKey = "ValidAPIKey";
|
|
|
|
@Spy
|
|
|
|
|
|
|
|
private ObjectMapper objectMapper;
|
|
|
|
private MyWaifuClient sut;
|
|
|
|
|
|
|
|
|
|
|
|
private final String apiKey = "ValidAPIKey";
|
|
|
|
@BeforeEach
|
|
|
|
|
|
|
|
void setUp() {
|
|
|
|
private MyWaifuClient sut;
|
|
|
|
MockitoAnnotations.openMocks(this);
|
|
|
|
|
|
|
|
|
|
|
|
@BeforeEach
|
|
|
|
sut = MyWaifuClient.createDefault(apiKey);
|
|
|
|
void setUp() {
|
|
|
|
APIWrapper apiWrapper = new APIWrapper(apiKey, httpClient);
|
|
|
|
MockitoAnnotations.openMocks(this);
|
|
|
|
sut.setAPIWrapper(apiWrapper);
|
|
|
|
|
|
|
|
}
|
|
|
|
sut = MyWaifuClient.createDefault(apiKey);
|
|
|
|
|
|
|
|
APIWrapper apiWrapper = new APIWrapper(apiKey, httpClient);
|
|
|
|
@Test
|
|
|
|
APIMapper apiMapper = new APIMapper();
|
|
|
|
void successfullyGetWaifu() throws IOException, InterruptedException, APIMapperException, APIResponseException {
|
|
|
|
|
|
|
|
HttpRequest expectedHttpRequest = buildHttpGetRequest(apiKey, "waifu/1");
|
|
|
|
apiMapper.setObjectMapper(objectMapper);
|
|
|
|
int expectedStatusCode = 200;
|
|
|
|
apiWrapper.setApiMapper(apiMapper);
|
|
|
|
String expectedBody = getJsonAsString("getWaifu.json");
|
|
|
|
sut.setAPIWrapper(apiWrapper);
|
|
|
|
Waifu expectedWaifu = TestEntity.getExpectedWaifu();
|
|
|
|
}
|
|
|
|
HttpResponse<String> expectedHttpResponse = buildHttpResponse(expectedStatusCode, expectedBody);
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
doReturn(expectedHttpResponse).when(httpClient).send(expectedHttpRequest, HttpResponse.BodyHandlers.ofString());
|
|
|
|
void successfullyGetWaifu() throws IOException, InterruptedException, APIMapperException, APIResponseException {
|
|
|
|
|
|
|
|
HttpRequest expectedHttpRequest = buildHttpGetRequest(apiKey, "waifu/1");
|
|
|
|
Response<Waifu> actualWaifuResponse = sut.getWaifu(1);
|
|
|
|
int expectedStatusCode = 200;
|
|
|
|
|
|
|
|
String expectedBody = getJsonAsString("getWaifu.json");
|
|
|
|
assertThat(actualWaifuResponse.getStatusCode(), is(expectedStatusCode));
|
|
|
|
Waifu expectedWaifu = TestEntity.getExpectedWaifu();
|
|
|
|
assertThat(actualWaifuResponse.getBody(), is(expectedBody));
|
|
|
|
HttpResponse<String> expectedHttpResponse = buildHttpResponse(expectedStatusCode, expectedBody);
|
|
|
|
assertThat(actualWaifuResponse.getModel(), is(expectedWaifu));
|
|
|
|
|
|
|
|
verify(httpClient, times(1)).send(expectedHttpRequest, HttpResponse.BodyHandlers.ofString());
|
|
|
|
doReturn(expectedHttpResponse).when(httpClient).send(expectedHttpRequest, HttpResponse.BodyHandlers.ofString());
|
|
|
|
verifyNoMoreInteractions(httpClient);
|
|
|
|
|
|
|
|
}
|
|
|
|
Response<Waifu> actualWaifuResponse = sut.getWaifu(1);
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
assertThat(actualWaifuResponse.getStatusCode(), is(expectedStatusCode));
|
|
|
|
void successfullyGetBestWaifus() throws IOException, InterruptedException, APIMapperException, APIResponseException {
|
|
|
|
assertThat(actualWaifuResponse.getBody(), is(expectedBody));
|
|
|
|
HttpRequest expectedHttpRequest = buildHttpGetRequest(apiKey, "airing/best");
|
|
|
|
assertThat(actualWaifuResponse.getModel(), is(expectedWaifu));
|
|
|
|
int expectedStatusCode = 200;
|
|
|
|
verify(httpClient, times(1)).send(expectedHttpRequest, HttpResponse.BodyHandlers.ofString());
|
|
|
|
String expectedBody = getJsonAsString("getBestWaifus.json");
|
|
|
|
verifyNoMoreInteractions(httpClient);
|
|
|
|
List<FilteredWaifu> expectedBestWaifus = TestEntity.getBestWaifus();
|
|
|
|
}
|
|
|
|
HttpResponse<String> expectedHttpResponse = buildHttpResponse(expectedStatusCode, expectedBody);
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
doReturn(expectedHttpResponse).when(httpClient).send(expectedHttpRequest, HttpResponse.BodyHandlers.ofString());
|
|
|
|
void successfullyGetBestWaifus() throws IOException, InterruptedException, APIMapperException, APIResponseException {
|
|
|
|
|
|
|
|
HttpRequest expectedHttpRequest = buildHttpGetRequest(apiKey, "airing/best");
|
|
|
|
Response<List<FilteredWaifu>> actualBestWaifusResponse = sut.getBestWaifus();
|
|
|
|
int expectedStatusCode = 200;
|
|
|
|
|
|
|
|
String expectedBody = getJsonAsString("getBestWaifus.json");
|
|
|
|
assertThat(actualBestWaifusResponse.getStatusCode(), is(expectedStatusCode));
|
|
|
|
List<FilteredWaifu> expectedBestWaifus = TestEntity.getBestWaifus();
|
|
|
|
assertThat(actualBestWaifusResponse.getBody(), is(expectedBody));
|
|
|
|
HttpResponse<String> expectedHttpResponse = buildHttpResponse(expectedStatusCode, expectedBody);
|
|
|
|
assertThat(actualBestWaifusResponse.getModel(), is(expectedBestWaifus));
|
|
|
|
|
|
|
|
verify(httpClient, times(1)).send(expectedHttpRequest, HttpResponse.BodyHandlers.ofString());
|
|
|
|
doReturn(expectedHttpResponse).when(httpClient).send(expectedHttpRequest, HttpResponse.BodyHandlers.ofString());
|
|
|
|
verifyNoMoreInteractions(httpClient);
|
|
|
|
|
|
|
|
}
|
|
|
|
Response<List<FilteredWaifu>> actualBestWaifusResponse = sut.getBestWaifus();
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
assertThat(actualBestWaifusResponse.getStatusCode(), is(expectedStatusCode));
|
|
|
|
void successfullyGetWaifuImages() throws IOException, InterruptedException, APIMapperException, APIResponseException {
|
|
|
|
assertThat(actualBestWaifusResponse.getBody(), is(expectedBody));
|
|
|
|
HttpRequest expectedHttpRequest = buildHttpGetRequest(apiKey, "waifu/1/images?page=1");
|
|
|
|
assertThat(actualBestWaifusResponse.getModel(), is(expectedBestWaifus));
|
|
|
|
int expectedStatusCode = 200;
|
|
|
|
verify(httpClient, times(1)).send(expectedHttpRequest, HttpResponse.BodyHandlers.ofString());
|
|
|
|
String expectedBody = getJsonAsString("getWaifuImages.json");
|
|
|
|
verifyNoMoreInteractions(httpClient);
|
|
|
|
PaginationData<WaifuImage> expectedWaifuImages = TestEntity.getWaifuImages();
|
|
|
|
}
|
|
|
|
HttpResponse<String> expectedHttpResponse = buildHttpResponse(expectedStatusCode, expectedBody);
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
doReturn(expectedHttpResponse).when(httpClient).send(expectedHttpRequest, HttpResponse.BodyHandlers.ofString());
|
|
|
|
void successfullyGetWaifuImages() throws IOException, InterruptedException, APIMapperException, APIResponseException {
|
|
|
|
|
|
|
|
HttpRequest expectedHttpRequest = buildHttpGetRequest(apiKey, "waifu/1/images?page=1");
|
|
|
|
Response<PaginationData<WaifuImage>> actualWaifuImagesResponse = sut.getWaifuImages(1, 1);
|
|
|
|
int expectedStatusCode = 200;
|
|
|
|
|
|
|
|
String expectedBody = getJsonAsString("getWaifuImages.json");
|
|
|
|
assertThat(actualWaifuImagesResponse.getStatusCode(), is(expectedStatusCode));
|
|
|
|
PaginationData<WaifuImage> expectedWaifuImages = TestEntity.getWaifuImages();
|
|
|
|
assertThat(actualWaifuImagesResponse.getBody(), is(expectedBody));
|
|
|
|
HttpResponse<String> expectedHttpResponse = buildHttpResponse(expectedStatusCode, expectedBody);
|
|
|
|
assertThat(actualWaifuImagesResponse.getModel(), is(expectedWaifuImages));
|
|
|
|
|
|
|
|
verify(httpClient, times(1)).send(expectedHttpRequest, HttpResponse.BodyHandlers.ofString());
|
|
|
|
doReturn(expectedHttpResponse).when(httpClient).send(expectedHttpRequest, HttpResponse.BodyHandlers.ofString());
|
|
|
|
verifyNoMoreInteractions(httpClient);
|
|
|
|
|
|
|
|
}
|
|
|
|
Response<PaginationData<WaifuImage>> actualWaifuImagesResponse = sut.getWaifuImages(1, 1);
|
|
|
|
|
|
|
|
|
|
|
|
private String getJsonAsString(String filename) {
|
|
|
|
assertThat(actualWaifuImagesResponse.getStatusCode(), is(expectedStatusCode));
|
|
|
|
InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(filename);
|
|
|
|
assertThat(actualWaifuImagesResponse.getBody(), is(expectedBody));
|
|
|
|
return new BufferedReader(new InputStreamReader(Objects.requireNonNull(resourceAsStream), StandardCharsets.UTF_8))
|
|
|
|
assertThat(actualWaifuImagesResponse.getModel(), is(expectedWaifuImages));
|
|
|
|
.lines()
|
|
|
|
verify(httpClient, times(1)).send(expectedHttpRequest, HttpResponse.BodyHandlers.ofString());
|
|
|
|
.collect(Collectors.joining("\n"));
|
|
|
|
verifyNoMoreInteractions(httpClient);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private HttpRequest buildHttpGetRequest(String apiKey, String param) {
|
|
|
|
@Test
|
|
|
|
return HttpRequest.newBuilder()
|
|
|
|
void successfullyPostBetaSearch() throws IOException, InterruptedException, APIMapperException, APIResponseException {
|
|
|
|
.uri(URI.create("https://mywaifulist.moe/api/v1/" + param))
|
|
|
|
HttpRequest expectedHttpRequest = buildHttpPostRequest(apiKey, "search/beta");
|
|
|
|
.timeout(Duration.ofSeconds(20))
|
|
|
|
int expectedStatusCode = 200;
|
|
|
|
.headers("Content-Type", "application/json", "apikey", apiKey)
|
|
|
|
String expectedBody = getJsonAsString("betaSearch.json");
|
|
|
|
.GET()
|
|
|
|
List<FilteredWaifu> expectedBetaSearch = TestEntity.getBetaSearch();
|
|
|
|
.build();
|
|
|
|
HttpResponse<String> expectedHttpResponse = buildHttpResponse(expectedStatusCode, expectedBody);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
doReturn(expectedHttpResponse).when(httpClient).send(expectedHttpRequest, HttpResponse.BodyHandlers.ofString());
|
|
|
|
private HttpResponse<String> buildHttpResponse(int statusCode, String body) {
|
|
|
|
|
|
|
|
return new HttpResponse<>() {
|
|
|
|
Response<List<FilteredWaifu>> actualBetaSearchResponse = sut.betaSearch("Yumeko");
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public int statusCode() {
|
|
|
|
assertThat(actualBetaSearchResponse.getStatusCode(), is(expectedStatusCode));
|
|
|
|
return statusCode;
|
|
|
|
assertThat(actualBetaSearchResponse.getBody(), is(expectedBody));
|
|
|
|
}
|
|
|
|
assertThat(actualBetaSearchResponse.getModel(), is(expectedBetaSearch));
|
|
|
|
|
|
|
|
verify(httpClient, times(1)).send(expectedHttpRequest, HttpResponse.BodyHandlers.ofString());
|
|
|
|
@Override
|
|
|
|
verifyNoMoreInteractions(httpClient);
|
|
|
|
public HttpRequest request() {
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
@Test
|
|
|
|
|
|
|
|
void failToGetBestWaifusWhereDeserializationGoesWrong() throws IOException, InterruptedException {
|
|
|
|
@Override
|
|
|
|
HttpRequest expectedHttpRequest = buildHttpGetRequest(apiKey, "airing/best");
|
|
|
|
public Optional<HttpResponse<String>> previousResponse() {
|
|
|
|
int expectedStatusCode = 200;
|
|
|
|
return Optional.empty();
|
|
|
|
String expectedBody = getJsonAsString("getBestWaifus.json");
|
|
|
|
}
|
|
|
|
String expectedData = getData(expectedBody);
|
|
|
|
|
|
|
|
HttpResponse<String> expectedHttpResponse = buildHttpResponse(expectedStatusCode, expectedBody);
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public HttpHeaders headers() {
|
|
|
|
JavaType expectedModel = listOf(FilteredWaifu.class);
|
|
|
|
return null;
|
|
|
|
String customExceptionMessage = "If you are seeing this message, " +
|
|
|
|
}
|
|
|
|
"this is more than likely a fault in my logic. " +
|
|
|
|
|
|
|
|
"Please raise an issue including the printed stacktrace :D";
|
|
|
|
@Override
|
|
|
|
String exceptionMessage = "Uh Oh Somebody Did a No No!";
|
|
|
|
public String body() {
|
|
|
|
String expectedExceptionMessage = "\n\n" + customExceptionMessage + "\n\n" + exceptionMessage;
|
|
|
|
return body;
|
|
|
|
|
|
|
|
}
|
|
|
|
doReturn(expectedHttpResponse).when(httpClient).send(expectedHttpRequest, HttpResponse.BodyHandlers.ofString());
|
|
|
|
|
|
|
|
doThrow(new JsonProcessingException(exceptionMessage) {}).when(objectMapper).readValue(expectedData, expectedModel);
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public Optional<SSLSession> sslSession() {
|
|
|
|
Throwable actualException = assertThrows(APIMapperException.class, () -> sut.getBestWaifus());
|
|
|
|
return Optional.empty();
|
|
|
|
|
|
|
|
}
|
|
|
|
assertThat(actualException.getMessage(), is(expectedExceptionMessage));
|
|
|
|
|
|
|
|
verify(httpClient, times(1)).send(expectedHttpRequest, HttpResponse.BodyHandlers.ofString());
|
|
|
|
@Override
|
|
|
|
verifyNoMoreInteractions(httpClient);
|
|
|
|
public URI uri() {
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
private String getData(String jsonBody) throws JsonProcessingException {
|
|
|
|
|
|
|
|
JsonNode parent = objectMapper.readTree(jsonBody);
|
|
|
|
@Override
|
|
|
|
return parent.get("data").toString();
|
|
|
|
public HttpClient.Version version() {
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
private HttpRequest buildHttpPostRequest(String apiKey, String param) {
|
|
|
|
};
|
|
|
|
return HttpRequest.newBuilder()
|
|
|
|
}
|
|
|
|
.uri(URI.create("https://mywaifulist.moe/api/v1/" + param))
|
|
|
|
|
|
|
|
.timeout(Duration.ofSeconds(20))
|
|
|
|
|
|
|
|
.headers("Content-Type", "application/json", "apikey", apiKey)
|
|
|
|
|
|
|
|
.POST(HttpRequest.BodyPublishers.ofString(""))
|
|
|
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private String getJsonAsString(String filename) {
|
|
|
|
|
|
|
|
InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(filename);
|
|
|
|
|
|
|
|
return new BufferedReader(new InputStreamReader(Objects.requireNonNull(resourceAsStream), StandardCharsets.UTF_8))
|
|
|
|
|
|
|
|
.lines()
|
|
|
|
|
|
|
|
.collect(Collectors.joining("\n"));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private HttpRequest buildHttpGetRequest(String apiKey, String param) {
|
|
|
|
|
|
|
|
return HttpRequest.newBuilder()
|
|
|
|
|
|
|
|
.uri(URI.create("https://mywaifulist.moe/api/v1/" + param))
|
|
|
|
|
|
|
|
.timeout(Duration.ofSeconds(20))
|
|
|
|
|
|
|
|
.headers("Content-Type", "application/json", "apikey", apiKey)
|
|
|
|
|
|
|
|
.GET()
|
|
|
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private HttpResponse<String> buildHttpResponse(int statusCode, String body) {
|
|
|
|
|
|
|
|
return new HttpResponse<>() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public int statusCode() {
|
|
|
|
|
|
|
|
return statusCode;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@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 body;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public Optional<SSLSession> sslSession() {
|
|
|
|
|
|
|
|
return Optional.empty();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public URI uri() {
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public HttpClient.Version version() {
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|