CustomHttpErrors { public String getCustomError(HttpRequestWrapper req, HttpResponseWrapper res) { for (MatcherAndError m : matchersAndLogs) { if (m.getMatcher().matchResponse(req, res)) { return m.getCustomError().customError(req, res); } } return null; } private  CustomHttpErrors(List<MatcherAndError> matchersAndLogs); String getCustomError(HttpRequestWrapper req, HttpResponseWrapper res);  }
@Test public void testMatchesCode() throws IOException { HttpRequestWrapper request = createHttpRequest(BQ_TABLES_LIST_URL); HttpResponseWrapper response = createHttpResponse(403); HttpCallCustomError mockCustomError = mock(HttpCallCustomError.class); CustomHttpErrors.Builder builder = new CustomHttpErrors.Builder(); builder.addErrorForCode(403, "Custom Error Msg"); CustomHttpErrors customErrors = builder.build(); String errorMessage = customErrors.getCustomError(request, response); assertEquals("Custom Error Msg", errorMessage); }
@Test public void testNotMatchesCode() throws IOException { HttpRequestWrapper request = createHttpRequest(BQ_TABLES_LIST_URL); HttpResponseWrapper response = createHttpResponse(404); HttpCallCustomError mockCustomError = mock(HttpCallCustomError.class); CustomHttpErrors.Builder builder = new CustomHttpErrors.Builder(); builder.addErrorForCode(403, "Custom Error Msg"); CustomHttpErrors customErrors = builder.build(); String errorMessage = customErrors.getCustomError(request, response); assertNull(errorMessage); }
@Test public void testMatchesCodeAndUrlContains() throws IOException { HttpRequestWrapper request = createHttpRequest(BQ_TABLES_LIST_URL); HttpResponseWrapper response = createHttpResponse(403); HttpCallCustomError mockCustomError = mock(HttpCallCustomError.class); CustomHttpErrors.Builder builder = new CustomHttpErrors.Builder(); builder.addErrorForCodeAndUrlContains(403, "/tables?", "Custom Error Msg"); CustomHttpErrors customErrors = builder.build(); String errorMessage = customErrors.getCustomError(request, response); assertEquals("Custom Error Msg", errorMessage); }
@Test public void testNotMatchesCodeAndUrlContains() throws IOException { HttpRequestWrapper request = createHttpRequest(BQ_TABLES_LIST_URL); HttpResponseWrapper response = createHttpResponse(404); HttpCallCustomError mockCustomError = mock(HttpCallCustomError.class); CustomHttpErrors.Builder builder = new CustomHttpErrors.Builder(); builder.addErrorForCodeAndUrlContains(403, "/doesnotmatch?", "Custom Error Msg"); CustomHttpErrors customErrors = builder.build(); String errorMessage = customErrors.getCustomError(request, response); assertNull(errorMessage); }