### Question:
LogsearchFilter implements Filter { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) servletRequest; if (requestMatcher.matches(request)) { StatusMessage errorResponse = statusProvider.getStatusMessage(request.getRequestURI()); if (errorResponse != null) { logger.info("{} request is filtered out: {}", request.getRequestURL(), errorResponse.getMessage()); HttpServletResponse resp = (HttpServletResponse) servletResponse; resp.setStatus(errorResponse.getStatusCode()); resp.setContentType("application/json"); resp.getWriter().print(toJson(errorResponse)); return; } } filterChain.doFilter(servletRequest, servletResponse); } LogsearchFilter(RequestMatcher requestMatcher, StatusProvider statusProvider);  LogsearchFilter(RequestMatcher requestMatcher, StatusProvider statusProvider, boolean enabled); @Override void init(FilterConfig filterConfig); @Override void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain); @Override void destroy();  }### Answer:
@Test public void testDoFilterCallsFilterChainDoFilterIfRequestURIDoesNotMatch() throws Exception { expect(requestMatcher.matches(servletRequest)).andReturn(false); filterChain.doFilter(servletRequest, servletResponse); expectLastCall(); replay(requestMatcher, statusProvider, servletRequest, servletResponse, filterChain); LogsearchFilter filter = new LogsearchFilter(requestMatcher, statusProvider); filter.doFilter(servletRequest, servletResponse, filterChain); verify(requestMatcher, statusProvider, servletRequest, servletResponse, filterChain); }
@Test public void testDoFilterCallsFilterChainDoFilterIfNoError() throws Exception { expect(requestMatcher.matches(servletRequest)).andReturn(true).anyTimes(); expect(statusProvider.getStatusMessage(REQUEST_URI)).andReturn(null); filterChain.doFilter(servletRequest, servletResponse); expectLastCall(); replay(requestMatcher, statusProvider, servletRequest, servletResponse, filterChain); LogsearchFilter filter = new LogsearchFilter(requestMatcher, statusProvider); filter.doFilter(servletRequest, servletResponse, filterChain); verify(requestMatcher, statusProvider, servletRequest, servletResponse, filterChain); }
@Test public void testDoFilterWritesStatusMessageInCaseOfAnError() throws Exception { StatusMessage statusMessage = StatusMessage.with(INTERNAL_SERVER_ERROR, "Error occurred"); StringWriter stringWriter = new StringWriter(); expect(requestMatcher.matches(servletRequest)).andReturn(true).anyTimes(); expect(statusProvider.getStatusMessage(REQUEST_URI)).andReturn(statusMessage); expect(servletRequest.getRequestURL()).andReturn(new StringBuffer(REQUEST_URI)).anyTimes(); servletResponse.setStatus(statusMessage.getStatus()); expectLastCall(); servletResponse.setContentType("application/json"); expectLastCall(); expect(servletResponse.getWriter()).andReturn(new PrintWriter(stringWriter)); replay(requestMatcher, statusProvider, servletRequest, servletResponse, filterChain); LogsearchFilter filter = new LogsearchFilter(requestMatcher, statusProvider); filter.doFilter(servletRequest, servletResponse, filterChain); verify(requestMatcher, statusProvider, servletRequest, servletResponse, filterChain); Map<String, Object> map = JSONUtil.jsonToMapObject(stringWriter.toString()); assertThat(map, is(not(nullValue()))); assertThat(map.get("status"), is(statusMessage.getStatus())); assertThat(map.get("message"), is(statusMessage.getMessage())); }