CharacterApiClient extends MarvelApiClient { public MarvelResponse<CharacterDto> getCharacter(String characterId) throws MarvelApiException { if (characterId == null || characterId.isEmpty()) { throw new IllegalArgumentException("The CharacterId must not be null or empty"); } CharacterApiRest api = getApi(CharacterApiRest.class); Call<MarvelResponse<CharactersDto>> call = api.getCharacter(characterId); MarvelResponse<CharactersDto> characters = execute(call); CharactersDto charactersDto = characters.getResponse(); if (charactersDto != null && charactersDto.getCount() > 0) { CharacterDto characterDto = charactersDto.getCharacters().get(0); MarvelResponse<CharacterDto> characterResponse = new MarvelResponse<>(characters); characterResponse.setResponse(characterDto); return characterResponse; } else { throw new MarvelApiException("Character not found", null); } } CharacterApiClient(MarvelApiConfig marvelApiConfig); MarvelResponse<CharactersDto> getAll(int offset, int limit); MarvelResponse<CharactersDto> getAll(CharactersQuery charactersQuery); MarvelResponse<CharacterDto> getCharacter(String characterId);  }
@Test(expected = IllegalArgumentException.class) public void shouldNotAcceptEmptyCharacterIds() throws Exception { CharacterApiClient characterApiClient = givenCharacterApiClient(); characterApiClient.getCharacter(INVALID_CHARACTER_ID); }
@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 = 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 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); }