@Override public PaginatedCollection<Character> getPage(Page page) throws MarvelApiException { int offset = page.getOffset(); int limit = page.getLimit(); MarvelResponse<CharactersDto> charactersApiResponse = characterApiClient.getAll(offset, limit); CharactersDto charactersDto = charactersApiResponse.getResponse(); Collection<Character> characters = mapper.reverseMap(charactersDto.getCharacters()); PaginatedCollection<Character> charactersPage = new PaginatedCollection<>(characters); charactersPage.setPage(page); charactersPage.setHasMore( charactersDto.getOffset() + charactersDto.getCount() < charactersDto.getTotal()); return charactersPage; } }
public MarvelResponse<CharactersDto> getAll(int offset, int limit) throws MarvelApiException { CharactersQuery query = CharactersQuery.Builder.create().withOffset(offset).withLimit(limit).build(); return getAll(query); }
@Test(expected = IllegalArgumentException.class) public void shouldThrowExceptionWhenLimitIsZero() throws Exception { CharacterApiClient characterApiClient = givenCharacterApiClient(); enqueueMockResponse(); characterApiClient.getAll(ANY_OFFSET, INVALID_LIMIT); }
CharactersQuery spider = CharactersQuery.Builder.create().withOffset(0).withLimit(10).build(); MarvelResponse<CharactersDto> all = characterApiClient.getAll(spider); System.out.println(all.toString()); } else if (operation.equals("getCharacter")) {
@Test public void shouldRequestCharactersByOffsetAndLimitWithAsParams() throws Exception { CharacterApiClient characterApiClient = givenCharacterApiClient(); enqueueMockResponse(); characterApiClient.getAll(ANY_OFFSET, ANY_LIMIT); assertRequestSentToContains("offset=" + ANY_OFFSET, "limit=" + ANY_LIMIT); }
@Test public void shouldParseGetAllCharactersResponse() throws Exception { CharacterApiClient characterApiClient = givenCharacterApiClient(); enqueueMockResponse("getCharacters.json"); MarvelResponse<CharactersDto> characters = characterApiClient.getAll(0, ANY_LIMIT); assertBasicMarvelResponse(characters); assertGetAllCharactersResponseIsProperlyParsed(characters); }
@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 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); }