WebServiceClient { void validateResponse(HTTPResponse response) { int statusCode = response.statusCode; if (statusCode >= 200 && statusCode < 300) return; if (response.body.length == 0) throw new RemoteServiceException("failed to call remote service, statusCode=" + statusCode, Severity.ERROR, "REMOTE_SERVICE_ERROR", parseHTTPStatus(statusCode)); ErrorResponse error = errorResponse(response); if (error.id != null && error.errorCode != null) { LOGGER.debug("failed to call remote service, statusCode={}, id={}, severity={}, errorCode={}, remoteStackTrace={}", statusCode, error.id, error.severity, error.errorCode, error.stackTrace); throw new RemoteServiceException(error.message, parseSeverity(error.severity), error.errorCode, parseHTTPStatus(statusCode)); } else { throw new RemoteServiceException("failed to call remote service, statusCode=" + statusCode, Severity.ERROR, "REMOTE_SERVICE_ERROR", parseHTTPStatus(statusCode)); } } WebServiceClient(String serviceURL, HTTPClient httpClient, RequestBeanWriter writer, ResponseBeanReader reader); Object execute(HTTPMethod method, String path, Class<T> requestBeanClass, T requestBean, Type responseType); void intercept(WebServiceClientInterceptor interceptor); void logCallWebService(String method); static final String USER_AGENT; }
@Test void validateResponse() { webServiceClient.validateResponse(new HTTPResponse(200, Map.of(), null)); }
@Test void validateResponseWithErrorResponse() { var response = new ErrorResponse(); response.id = "id"; response.severity = "WARN"; response.errorCode = "NOT_FOUND"; response.message = "not found"; assertThatThrownBy(() -> webServiceClient.validateResponse(new HTTPResponse(404, Map.of(), Strings.bytes(JSON.toJSON(response))))) .isInstanceOf(RemoteServiceException.class) .satisfies(throwable -> { RemoteServiceException exception = (RemoteServiceException) throwable; assertThat(exception.severity()).isEqualTo(Severity.WARN); assertThat(exception.errorCode()).isEqualTo(response.errorCode); assertThat(exception.getMessage()).isEqualTo(response.message); assertThat(exception.status).isEqualTo(HTTPStatus.NOT_FOUND); }); }
@Test void validateResponseWithEmptyBody() { assertThatThrownBy(() -> webServiceClient.validateResponse(new HTTPResponse(HTTPStatus.SERVICE_UNAVAILABLE.code, Map.of(), new byte[0]))) .isInstanceOf(RemoteServiceException.class) .satisfies(throwable -> { RemoteServiceException exception = (RemoteServiceException) throwable; assertThat(exception.severity()).isEqualTo(Severity.ERROR); assertThat(exception.errorCode()).isEqualTo("REMOTE_SERVICE_ERROR"); assertThat(exception.getMessage()).isEqualTo("failed to call remote service, statusCode=503"); assertThat(exception.status).isEqualTo(HTTPStatus.SERVICE_UNAVAILABLE); }); }
@Test void validateResponseWith410() { assertThatThrownBy(() -> webServiceClient.validateResponse(new HTTPResponse(HTTPStatus.GONE.code, Map.of(), Strings.bytes("{}")))) .isInstanceOf(RemoteServiceException.class) .satisfies(throwable -> { RemoteServiceException exception = (RemoteServiceException) throwable; assertThat(exception.severity()).isEqualTo(Severity.ERROR); assertThat(exception.errorCode()).isEqualTo("REMOTE_SERVICE_ERROR"); assertThat(exception.getMessage()).isEqualTo("failed to call remote service, statusCode=410"); assertThat(exception.status).isEqualTo(HTTPStatus.GONE); }); }
@Test void validateResponseWithUnexpectedBody() { assertThatThrownBy(() -> webServiceClient.validateResponse(new HTTPResponse(HTTPStatus.SERVICE_UNAVAILABLE.code, Map.of(), Strings.bytes("<html/>")))) .isInstanceOf(RemoteServiceException.class) .satisfies(throwable -> { RemoteServiceException exception = (RemoteServiceException) throwable; assertThat(exception.severity()).isEqualTo(Severity.ERROR); assertThat(exception.errorCode()).isEqualTo("REMOTE_SERVICE_ERROR"); assertThat(exception.getMessage()).startsWith("failed to deserialize remote service error response, statusCode=503"); assertThat(exception.status).isEqualTo(HTTPStatus.SERVICE_UNAVAILABLE); }); }