private void givenConnectionExceptionObtainingComicSeries() throws Exception { when(comicSeriesRepository.getComicSeriesDetail(any(Integer.class))).thenThrow( new MarvelApiException(ANY_EXCEPTION, new UnknownHostException())); }
@Override public Error create(Exception exception) { Throwable targetException = exception; if (exception instanceof InvocationTargetException) { targetException = ((InvocationTargetException) exception).getTargetException(); } if (targetException instanceof MarvelApiException) { MarvelApiException marvelApiException = (MarvelApiException) targetException; if (marvelApiException.getCause() instanceof UnknownHostException) { return new ConnectionError(); } } return new UnknownError(); } }
@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; } }
@Override public Response intercept(Chain chain) throws IOException { String timestamp = String.valueOf(timeProvider.currentTimeMillis()); String hash = null; try { hash = authHashGenerator.generateHash(timestamp, publicKey, privateKey); } catch (MarvelApiException e) { e.printStackTrace(); } Request request = chain.request(); HttpUrl url = request.url() .newBuilder() .addQueryParameter(TIMESTAMP_KEY, timestamp) .addQueryParameter(APIKEY_KEY, publicKey) .addQueryParameter(HASH_KEY, hash) .build(); request = request.newBuilder().url(url).build(); return chain.proceed(request); } }
@Test(expected = MarvelApiException.class) public void shouldReturnMarvelExceptionWhenTheIdDoesNotExist() throws Exception { SeriesApiClient seriesApiClient = givenSeriesApiClient(); enqueueMockResponse(404, "{\"code\":404,\"status\":\"We couldn't find that series\"}"); try { seriesApiClient.getSeriesById(ANY_NOT_FOUND_ID); } catch (MarvelApiException e) { assertEquals("404", e.getMarvelCode()); assertEquals("We couldn't find that series", e.getMessage()); throw e; } }
private void givenConnectionExceptionObtainingACharacter() throws Exception { when(charactersRepository.getByKey(anyString())).thenThrow( new MarvelApiException(ANY_EXCEPTION, new UnknownHostException())); }
@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; } }
private void givenConnectionExceptionObtainingCharacters() throws Exception { when(charactersRepository.getAll(ReadPolicy.CACHE_ONLY)).thenReturn(new ArrayList<Character>()); when(charactersRepository.getPage(any(Page.class))).thenThrow( new MarvelApiException(ANY_EXCEPTION, new UnknownHostException())); }
@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; } }
private void givenExceptionObtainingCharacters() throws Exception { when(charactersRepository.getAll(ReadPolicy.CACHE_ONLY)).thenReturn(new ArrayList<Character>()); when(charactersRepository.getPage(any(Page.class))).thenThrow( new MarvelApiException(ANY_EXCEPTION, null)); }
@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; } }
String generateHash(String timestamp, String publicKey, String privateKey) throws MarvelApiException { try { String value = timestamp + privateKey + publicKey; MessageDigest md5Encoder = MessageDigest.getInstance("MD5"); byte[] md5Bytes = md5Encoder.digest(value.getBytes()); StringBuilder md5 = new StringBuilder(); for (int i = 0; i < md5Bytes.length; ++i) { md5.append(Integer.toHexString((md5Bytes[i] & 0xFF) | 0x100).substring(1, 3)); } return md5.toString(); }catch (NoSuchAlgorithmException e) { throw new MarvelApiException("cannot generate the api key", e); } } }
@Test(expected = MarvelAuthApiException.class) public void shouldThrowMarvelApiExceptionOnAuthHttpError() throws Exception { SeriesApiClient seriesApiClient = givenSeriesApiClient(); enqueueMockResponse(401, "{\"code\":\"InvalidCredentials\",\"message\":\"That hash, timestamp and key " + "combination is invalid.\"}"); try { seriesApiClient.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; } }
public <T> T execute(Call<T> call) throws MarvelApiException { Response<T> response = null; try { response = call.execute(); } catch (IOException e) { throw new MarvelApiException("Network error", e); } if (response.isSuccessful()) { return response.body(); } else { parseError(response); return null; } }
public MarvelResponse<SeriesDto> getSeriesById(String seriesId) throws MarvelApiException { if (seriesId == null || seriesId.isEmpty()) { throw new IllegalArgumentException("The seriesId must not be null or empty"); } SeriesApiRest api = getApi(SeriesApiRest.class); Call<MarvelResponse<SeriesCollectionDto>> call = api.getSerie(seriesId); MarvelResponse<SeriesCollectionDto> series = execute(call); SeriesCollectionDto seriesCollectionDto = series.getResponse(); if (seriesCollectionDto != null && seriesCollectionDto.getCount() > 0) { SeriesDto seriesDto = seriesCollectionDto.getSeries().get(0); MarvelResponse<SeriesDto> serieResponse = new MarvelResponse<>(series); serieResponse.setResponse(seriesDto); return serieResponse; } else { throw new MarvelApiException("Series not found", null); } }
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); } } }
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); } } }
private void parseError(Response execute) throws MarvelApiException { String marvelCode = ""; String marvelDescription = ""; if (execute.errorBody() != null) { Gson gson = new Gson(); try { String errorBody = execute.errorBody().string(); MarvelError marvelError = gson.fromJson(errorBody, MarvelError.class); marvelCode = marvelError.getCode(); marvelDescription = marvelError.getMessage(); if (marvelDescription == null || "".equals(marvelDescription)) { marvelDescription = marvelError.getStatus(); } } catch (IOException e) { } } if(execute.code() == INVALID_AUTH_CODE) { throw new MarvelAuthApiException(execute.code(), marvelCode, marvelDescription, null); } else { throw new MarvelApiException(execute.code(), marvelCode, marvelDescription, null); } } }