### Question:
CompositeUriAuthorizationFilter extends AuthorizationFilter { @Override protected boolean ensureUserIsAuthorized(HttpServletRequest request, HttpServletResponse response) throws IOException { try { String uri = ServletUtil.decodeUri(request); String normalizedUri = ServletUtil.removeInvalidForwardSlashes(uri); LOGGER.trace("Looking for a matching authorization filter for request to \"{}\"...", uri); LOGGER.trace("Registered authorization filters: {}", uriAuthorizationFilters); for (UriAuthorizationFilter uriAuthorizationFilter : uriAuthorizationFilters) { if (normalizedUri.matches(uriAuthorizationFilter.getUriRegex())) { return ensureUserIsAuthorized(uriAuthorizationFilter, request, response); } } LOGGER.trace("No matching authorization filter for request to \"{}\".", uri); return true; } catch (Exception e) { ResponseEntity<String> responseEntity = exceptionMapper.handleException(e); ServletUtil.send(response, responseEntity.getStatusCodeValue(), responseEntity.getBody()); return false; } } @Inject  CompositeUriAuthorizationFilter(List<UriAuthorizationFilter> uriAuthorizationFilters, CFExceptionMapper exceptionMapper);   }### Answer:
@Test void testUriMatching() throws IOException { Mockito.when(request.getRequestURI()) .thenReturn(FOO_REQUEST_URI); assertTrue(compositeUriAuthorizationFilter.ensureUserIsAuthorized(request, response)); Mockito.verify(fooUriAuthorizationFilter) .ensureUserIsAuthorized(request, response); Mockito.verify(barUriAuthorizationFilter, Mockito.never()) .ensureUserIsAuthorized(request, response); }
@Test void testUriMatchingWithoutAnyMatchingFilters() throws IOException { Mockito.when(request.getRequestURI()) .thenReturn(BAZ_REQUEST_URI); assertTrue(compositeUriAuthorizationFilter.ensureUserIsAuthorized(request, response)); Mockito.verify(fooUriAuthorizationFilter, Mockito.never()) .ensureUserIsAuthorized(request, response); Mockito.verify(barUriAuthorizationFilter, Mockito.never()) .ensureUserIsAuthorized(request, response); }
@Test void testUriWhichContainsLotsOfForwardSlashes() throws IOException { Mockito.when(request.getRequestURI()) .thenReturn(BAR_REQUEST_WITH_SLASHES_URI); assertFalse(compositeUriAuthorizationFilter.ensureUserIsAuthorized(request, response)); Mockito.verify(fooUriAuthorizationFilter, Mockito.never()) .ensureUserIsAuthorized(request, response); Mockito.verify(barUriAuthorizationFilter) .ensureUserIsAuthorized(request, response); }
@Test void testWithAuthorizationException() throws IOException { Mockito.when(request.getRequestURI()) .thenReturn(FOO_REQUEST_URI); Mockito.when(fooUriAuthorizationFilter.ensureUserIsAuthorized(request, response)) .thenThrow(new SLException("...")); PrintWriter writer = Mockito.mock(PrintWriter.class); Mockito.when(response.getWriter()) .thenReturn(writer); assertFalse(compositeUriAuthorizationFilter.ensureUserIsAuthorized(request, response)); Mockito.verify(response) .setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value()); Mockito.verify(writer) .print("..."); Mockito.verify(writer) .flush(); }
@Test void testWithUnauthorizedUser() throws IOException { Mockito.when(request.getRequestURI()) .thenReturn(BAR_REQUEST_URI); assertFalse(compositeUriAuthorizationFilter.ensureUserIsAuthorized(request, response)); Mockito.verify(barUriAuthorizationFilter) .ensureUserIsAuthorized(request, response); }