@Test public void shouldParseGetAllComicsResponse() throws Exception { ComicApiClient comicApiClient = givenComicApiClient(); enqueueMockResponse("getComics.json"); MarvelResponse<ComicsDto> comics = comicApiClient.getAll(0, ANY_LIMIT); assertBasicMarvelResponse(comics); assertGetAllComicsResponseIsProperlyParsed(comics); }
@Test public void shouldParseGetComicResponse() throws Exception { ComicApiClient comicApiClient = givenComicApiClient(); enqueueMockResponse("getComic.json"); MarvelResponse<ComicDto> comic = comicApiClient.getComic(ANY_COMIC_ID); assertBasicMarvelResponse(comic); ComicDto response = comic.getResponse(); assertIsLornaTheJungleGirl6(response); }
@Test public void shouldRequestComicsByOffsetAndLimitWithAsParams() throws Exception { ComicApiClient comicApiClient = givenComicApiClient(); enqueueMockResponse(); comicApiClient.getAll(ANY_OFFSET, ANY_LIMIT); assertRequestSentToContains("offset=" + ANY_OFFSET, "limit=" + ANY_LIMIT); }
@Test public void shouldUseAllTheRequestParamsAddedFromQueryWhenSendIt() throws Exception { ComicApiClient comicApiClient = givenComicApiClient(); enqueueMockResponse(); .withNoVariants() .withDateDescriptor(ComicsQuery.DateDescriptor.LAST_WEEK) .withDateRange(getAnyDate()) .withTitle(ANY_TITLE) .withTitleStartsWith(ANY_START_TITLE) .addCreator(ANY_CREATOR) .addCharacter(ANY_CHARACTER) .withModifiedSince(getAnyDate()) .withOrderBy(OrderBy.NAME, false) .addEvent(getAnyEvents()) .addSerie(ANY_SERIE) .addStory(ANY_STORY) .addSharedAppearance(ANY_SHARED_APPEARANCES) .addCollaborator(getAnyCollaborators()) .build(); assertRequestSentToContains("format=" + Format.COMIC.toString(), "formatType=" + ComicsQuery.FormatType.COLLECTION, "noVariants=true", "dateDescriptor=" + LAST_WEEK_REQUEST, "dateRange=" + ANY_DATE_RANGE,
@Test(expected = IllegalArgumentException.class) public void shouldThrowExceptionWhenLimitIsZero() throws Exception { ComicApiClient comicApiClient = givenComicApiClient(); enqueueMockResponse(); comicApiClient.getAll(ANY_OFFSET, INVALID_LIMIT); }
@Test(expected = IllegalArgumentException.class) public void shouldNotAcceptEmptyComicIds() throws Exception { ComicApiClient comicApiClient = givenComicApiClient(); comicApiClient.getComic(INVALID_COMIC_ID); }
private ComicApiClient givenComicApiClient() { Retrofit retrofit = new Retrofit.Builder().baseUrl(getBaseEndpoint()) .addConverterFactory(GsonConverterFactory.create()) .build(); MarvelApiConfig marvelApiConfig = new MarvelApiConfig.Builder(ANY_PUBLIC_KEY, ANY_PRIVATE_KEY).baseUrl(ANY_URL) .retrofit(retrofit) .build(); return new ComicApiClient(marvelApiConfig); }
private void assertGetAllComicsResponseIsProperlyParsed(MarvelResponse<ComicsDto> comics) { ComicsDto comicsDto = comics.getResponse(); assertEquals(10, comicsDto.getCount()); assertEquals(10, comicsDto.getLimit()); assertEquals(0, comicsDto.getOffset()); assertEquals(35767, comicsDto.getTotal()); ComicDto comicDto = comicsDto.getComics().get(0); assertIsLornaTheJungleGirl6(comicDto); }
@Test(expected = MarvelApiException.class) public void shouldReturnMarvelExceptionWhenTheIdDoesNotExist() throws Exception { ComicApiClient comicApiClient = givenComicApiClient(); enqueueMockResponse(404, "{\"code\":404,\"status\":\"We couldn't find that comic\"}"); try { comicApiClient.getComic(ANY_NOT_FOUND_ID); } catch (MarvelApiException e) { assertEquals("404", e.getMarvelCode()); assertEquals("We couldn't find that comic", e.getMessage()); throw e; } }
@Test public void shouldSendGetComicRequestToTheCorrectEndpoint() throws Exception { ComicApiClient comicApiClient = givenComicApiClient(); enqueueMockResponse("getComic.json"); comicApiClient.getComic(ANY_COMIC_ID); assertRequestSentToContains("comics/" + ANY_COMIC_ID); }
@Test(expected = MarvelAuthApiException.class) public void shouldThrowMarvelApiExceptionOnAuthHttpError() throws Exception { ComicApiClient comicApiClient = givenComicApiClient(); enqueueMockResponse(401, "{\"code\":\"InvalidCredentials\",\"message\":\"That hash, timestamp and key " + "combination is invalid.\"}"); try { comicApiClient.getAll(ANY_OFFSET, ANY_LIMIT); } catch (MarvelApiException e) { assertEquals("InvalidCredentials", e.getMarvelCode()); assertEquals("That hash, timestamp and key combination is invalid.", e.getMessage()); throw e; } }