CharacterApiClient extends MarvelApiClient { public MarvelResponse<CharactersDto> getAll(int offset, int limit) throws MarvelApiException { CharactersQuery query = CharactersQuery.Builder.create().withOffset(offset).withLimit(limit).build(); return getAll(query); } CharacterApiClient(MarvelApiConfig marvelApiConfig); MarvelResponse<CharactersDto> getAll(int offset, int limit); MarvelResponse<CharactersDto> getAll(CharactersQuery charactersQuery); MarvelResponse<CharacterDto> getCharacter(String characterId);  }
@Test public void shouldRequestCharactersByOffsetAndLimitWithAsParams() throws Exception { CharacterApiClient characterApiClient = givenCharacterApiClient(); enqueueMockResponse(); characterApiClient.getAll(ANY_OFFSET, ANY_LIMIT); assertRequestSentToContains("offset=" + ANY_OFFSET, "limit=" + ANY_LIMIT); }
@Test(expected = IllegalArgumentException.class) public void shouldThrowExceptionWhenLimitIsZero() throws Exception { CharacterApiClient characterApiClient = givenCharacterApiClient(); enqueueMockResponse(); characterApiClient.getAll(ANY_OFFSET, INVALID_LIMIT); }
@Test public void shouldRequestCharactersUsingAllTheRequestParams() throws Exception { CharacterApiClient characterApiClient = givenCharacterApiClient(); enqueueMockResponse(); CharactersQuery query = CharactersQuery.Builder.create() .withName(ANY_NAME) .withNameStartWith(ANY_START_NAME) .withLimit(ANY_LIMIT) .withOffset(ANY_OFFSET) .withModifiedSince(getAnyDate()) .withOrderBy(OrderBy.NAME, false) .addComic(ANY_COMIC_1) .addComic(ANY_COMIC_2) .addEvents(getAnyEvents()) .addSerie(ANY_SERIE) .addStory(ANY_STORY) .build(); characterApiClient.getAll(query); assertRequestSentToContains("offset=" + ANY_OFFSET, "limit=" + ANY_LIMIT, "name=" + ANY_NAME, "nameStartsWith=" + ANY_START_NAME, "modifiedSince=" + ANY_MODIFIED_SINCE, "orderBy=" + ORDER_NAME_DESCEDANT_VALUE, "comics=" + COMICS_REQUEST, "events=" + EVENTS_REQUEST, "series=" + ANY_SERIE, "stories=" + ANY_STORY); }
@Test(expected = MarvelAuthApiException.class) public void shouldThrowMarvelApiExceptionOnAuthHttpError() throws Exception { CharacterApiClient characterApiClient = givenCharacterApiClient(); enqueueMockResponse(401, "{\"code\":\"InvalidCredentials\",\"message\":\"That hash, timestamp and key " + "combination is invalid.\"}"); try { characterApiClient.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; } }
@Test public void shouldParseGetAllCharactersResponse() throws Exception { CharacterApiClient characterApiClient = givenCharacterApiClient(); enqueueMockResponse("getCharacters.json"); MarvelResponse<CharactersDto> characters = characterApiClient.getAll(0, ANY_LIMIT); assertBasicMarvelResponse(characters); assertGetAllCharactersResponseIsProperlyParsed(characters); }