@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 public void shouldParseGetCharacterResponse() throws Exception { CharacterApiClient characterApiClient = givenCharacterApiClient(); enqueueMockResponse("getCharacter.json"); MarvelResponse<CharacterDto> character = characterApiClient.getCharacter(THREED_MAN_CHARACTER_ID); assertBasicMarvelResponse(character); CharacterDto response = character.getResponse(); assertIs3dManCharacter(response); }
@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 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 = IllegalArgumentException.class) public void shouldThrowExceptionWhenLimitIsZero() throws Exception { CharacterApiClient characterApiClient = givenCharacterApiClient(); enqueueMockResponse(); characterApiClient.getAll(ANY_OFFSET, INVALID_LIMIT); }
private CharacterApiClient givenCharacterApiClient() { 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 CharacterApiClient(marvelApiConfig); }
@Test(expected = IllegalArgumentException.class) public void shouldNotAcceptEmptyCharacterIds() throws Exception { CharacterApiClient characterApiClient = givenCharacterApiClient(); characterApiClient.getCharacter(INVALID_CHARACTER_ID); }
private void assertGetAllCharactersResponseIsProperlyParsed( MarvelResponse<CharactersDto> characters) { CharactersDto charactersDto = characters.getResponse(); assertEquals(10, charactersDto.getCount()); assertEquals(10, charactersDto.getLimit()); assertEquals(0, charactersDto.getOffset()); assertEquals(1485, charactersDto.getTotal()); CharacterDto firstCharacter = charactersDto.getCharacters().get(0); assertIs3dManCharacter(firstCharacter); }
@Test(expected = MarvelApiException.class) public void shouldReturnMarvelExceptionWhenTheIdDoesNotExist() throws Exception { CharacterApiClient characterApiClient = givenCharacterApiClient(); enqueueMockResponse(404, "{\"code\":404,\"status\":\"We couldn't find that character\"}"); try { characterApiClient.getCharacter(ANY_NOT_FOUND_ID); } catch (MarvelApiException e) { assertEquals("404", e.getMarvelCode()); assertEquals("We couldn't find that character", e.getMessage()); throw e; } }
@Test public void shouldSendGetCharacterRequestToTheCorrectEndpoint() throws Exception { CharacterApiClient characterApiClient = givenCharacterApiClient(); enqueueMockResponse("getCharacter.json"); characterApiClient.getCharacter(THREED_MAN_CHARACTER_ID); assertRequestSentToContains("characters/" + THREED_MAN_CHARACTER_ID); }
@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; } }