### Question:
ClientRequestExceptionMapper implements ExceptionMapper<ClientRequestException> { @Override public Response toResponse(ClientRequestException exception) { String title = createTitle(exception); LOG.info("Client request error not handled in application: {}", title, exception); ApiError error = new ApiError(title, emptyList()); exception.close(); return Response.status(RESPONSE_STATUS_CODE).type(APPLICATION_JSON).entity(error).build(); }  @Override Response toResponse(ClientRequestException exception);  }### Answer:
@Test public void mapNotFound() { ClientRequestException exception = new ClientRequestException(new NotFoundException()); Response response = clientRequestExceptionMapper.toResponse(exception); assertThat(response.getStatus()).isEqualTo(500); assertThat(response.getHeaders().getFirst(CONTENT_TYPE)) .isEqualTo(MediaType.APPLICATION_JSON_TYPE); assertThat(response.getEntity()).isInstanceOf(ApiError.class); assertThat((ApiError) response.getEntity()) .extracting(ApiError::getTitle) .asString() .isNotBlank() .contains("404"); }
@Test public void mapInternalServerError() { ClientRequestException exception = new ClientRequestException(new InternalServerErrorException()); Response response = clientRequestExceptionMapper.toResponse(exception); assertThat(response.getStatus()).isEqualTo(500); assertThat(response.getHeaders().getFirst(CONTENT_TYPE)) .isEqualTo(MediaType.APPLICATION_JSON_TYPE); assertThat(response.getEntity()).isInstanceOf(ApiError.class); assertThat((ApiError) response.getEntity()) .extracting(ApiError::getTitle) .asString() .isNotBlank() .contains("500"); }
@Test public void mapUnknownClientError() { ClientRequestException exception = new ClientRequestException(new ClientErrorException(418)); Response response = clientRequestExceptionMapper.toResponse(exception); assertThat(response.getStatus()).isEqualTo(500); assertThat(response.getHeaders().getFirst(CONTENT_TYPE)) .isEqualTo(MediaType.APPLICATION_JSON_TYPE); assertThat(response.getEntity()).isInstanceOf(ApiError.class); assertThat((ApiError) response.getEntity()) .extracting(ApiError::getTitle) .asString() .isNotBlank() .contains("418"); }
@Test public void jsonProcessing() { ClientRequestException exception = new ClientRequestException( new ProcessingException(new JsonParseException(mock(JsonParser.class), "No message"))); Response response = clientRequestExceptionMapper.toResponse(exception); assertThat(response.getStatus()).isEqualTo(500); assertThat(response.getHeaders().getFirst(CONTENT_TYPE)) .isEqualTo(MediaType.APPLICATION_JSON_TYPE); assertThat(response.getEntity()).isInstanceOf(ApiError.class); assertThat((ApiError) response.getEntity()) .extracting(ApiError::getTitle) .asString() .isNotBlank() .containsIgnoringCase("processing"); }
@Test public void mapConnectionTimeout() { ClientRequestException exception = new ClientRequestException(new ProcessingException(new ConnectTimeoutException())); Response response = clientRequestExceptionMapper.toResponse(exception); assertThat(response.getStatus()).isEqualTo(500); assertThat(response.getHeaders().getFirst(CONTENT_TYPE)) .isEqualTo(MediaType.APPLICATION_JSON_TYPE); assertThat(response.getEntity()).isInstanceOf(ApiError.class); assertThat((ApiError) response.getEntity()) .extracting(ApiError::getTitle) .asString() .isNotBlank() .containsIgnoringCase("connect") .containsIgnoringCase("timeout"); }
@Test public void mapReadTimeout() { ClientRequestException exception = new ClientRequestException(new ProcessingException(new SocketTimeoutException())); Response response = clientRequestExceptionMapper.toResponse(exception); assertThat(response.getStatus()).isEqualTo(500); assertThat(response.getHeaders().getFirst(CONTENT_TYPE)) .isEqualTo(MediaType.APPLICATION_JSON_TYPE); assertThat(response.getEntity()).isInstanceOf(ApiError.class); assertThat((ApiError) response.getEntity()) .extracting(ApiError::getTitle) .asString() .isNotBlank() .containsIgnoringCase("read") .containsIgnoringCase("timeout"); }