/** * Since this class represents server requests/responses (not clients), we only want to consider HTTP status codes * greater than or equal to 500 to be an error. From a server's perspective, a 4xx response is the correct * response to a bad request, and should therefore not be considered an error (again, from the server's * perspective - the client may feel differently). * * @param response The response object. * @return The value of {@link #getResponseHttpStatus(ResponseInfo)} if it is greater than or equal to 500, * or null otherwise. */ @Override public @Nullable String getErrorResponseTagValue(@Nullable ResponseInfo<?> response) { Integer statusCode = getResponseHttpStatus(response); if (statusCode != null && statusCode >= 500) { return statusCode.toString(); } // Status code does not indicate an error, so return null. return null; }
/** * Since this class represents server requests/responses (not clients), we only want to consider HTTP status codes * greater than or equal to 500 to be an error. From a server's perspective, a 4xx response is the correct * response to a bad request, and should therefore not be considered an error (again, from the server's * perspective - the client may feel differently). * * @param response The response object. * @return The value of {@link #getResponseHttpStatus(ResponseInfo)} if it is greater than or equal to 500, * or null otherwise. */ @Override public @Nullable String getErrorResponseTagValue(@Nullable ResponseInfo<?> response) { Integer statusCode = getResponseHttpStatus(response); if (statusCode != null && statusCode >= 500) { return statusCode.toString(); } // Status code does not indicate an error, so return null. return null; }
@Test public void getResponseHttpStatus_works_as_expected() { // given Integer expectedResult = 42; doReturn(expectedResult).when(responseMock).getHttpStatusCode(); // when Integer result = adapterSpy.getResponseHttpStatus(responseMock); // then assertThat(result).isEqualTo(expectedResult); }
@Test public void getResponseHttpStatus_returns_null_if_passed_null() { // expect assertThat(adapterSpy.getResponseHttpStatus(null)).isNull(); }
@DataProvider(value = { "null | null", "200 | null", "300 | null", "400 | null", "499 | null", "500 | 500", "599 | 599", "999 | 999" }, splitBy = "\\|") @Test public void getErrorResponseTagValue_works_as_expected(Integer statusCode, String expectedTagValue) { // given doReturn(statusCode).when(adapterSpy).getResponseHttpStatus(any(ResponseInfo.class)); // when String result = adapterSpy.getErrorResponseTagValue(responseMock); // then assertThat(result).isEqualTo(expectedTagValue); verify(adapterSpy).getErrorResponseTagValue(responseMock); verify(adapterSpy).getResponseHttpStatus(responseMock); verifyNoMoreInteractions(adapterSpy); }