System.out.println(character.toString()); } else if (operation.equals("getComics")) { ComicApiClient comicApiClient = new ComicApiClient(marvelApiConfig); MarvelResponse<ComicsDto> comics = comicApiClient.getAll(0, 10); System.out.println(comics.toString()); } else if (operation.equals("getComic")) { ComicApiClient comicApiClient = new ComicApiClient(marvelApiConfig); MarvelResponse<ComicDto> comic = comicApiClient.getComic("42882"); System.out.println(comic.toString()); } else if (operation.equals("getSeries")) {
public MarvelResponse<ComicsDto> getAll(int offset, int limit) throws MarvelApiException { ComicsQuery query = ComicsQuery.Builder.create().withOffset(offset).withLimit(limit).build(); return getAll(query); }
@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); }
@Test(expected = IllegalArgumentException.class) public void shouldThrowExceptionWhenLimitIsZero() throws Exception { ComicApiClient comicApiClient = givenComicApiClient(); enqueueMockResponse(); comicApiClient.getAll(ANY_OFFSET, INVALID_LIMIT); }
@Test public void shouldSendGetComicRequestToTheCorrectEndpoint() throws Exception { ComicApiClient comicApiClient = givenComicApiClient(); enqueueMockResponse("getComic.json"); comicApiClient.getComic(ANY_COMIC_ID); assertRequestSentToContains("comics/" + ANY_COMIC_ID); }
@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 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); }
public MarvelResponse<ComicDto> getComic(String comicId) throws MarvelApiException { if (comicId == null || comicId.isEmpty()) { throw new IllegalArgumentException("The ComicId must not be null or empty"); } ComicApiRest api = getApi(ComicApiRest.class); Call<MarvelResponse<ComicsDto>> call = api.getComic(comicId); MarvelResponse<ComicsDto> comics = execute(call); ComicsDto comicsDto = comics.getResponse(); if (comicsDto != null && comicsDto.getCount() > 0) { ComicDto comicDto = comicsDto.getComics().get(0); MarvelResponse<ComicDto> comicResponse = new MarvelResponse<>(comics); comicResponse.setResponse(comicDto); return comicResponse; } else { throw new MarvelApiException("Comic not found", null); } } }
@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(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(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; } }
.build(); comicApiClient.getAll(query);