MaintenanceModeAccessFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request; HttpServletResponse resp = (HttpServletResponse) response; File file = getMaintenanceModeFlagFile(); if (file.exists()) { String method = req.getMethod(); if (!method.equalsIgnoreCase("GET") && !method.equalsIgnoreCase("HEAD")) { String msg = BddException.ACCESS_NOT_ALLOWED_IN_MAINTENANCE_MODE().getLocalizedMessage(); logger.info(method + " " + req.getPathInfo() + " is not allowed in maintenance mode."); resp.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED); resp.getWriter().write(msg); resp.getWriter().flush(); return; } } chain.doFilter(request, response); }  @Override void init(FilterConfig filterConfig); @Override void destroy(); @Override void doFilter(ServletRequest request, ServletResponse response, FilterChain chain);  }
@Test public void testDoFilter() throws IOException, ServletException { MaintenanceModeAccessFilter maintenanceModeAccessFilter = Mockito.spy(new MaintenanceModeAccessFilter()); when(response.getWriter()).thenReturn(printWriter); when(file.exists()).thenReturn(true); Mockito.when(maintenanceModeAccessFilter.getMaintenanceModeFlagFile()).thenReturn(file); Mockito.when(request.getMethod()).thenReturn("GET"); maintenanceModeAccessFilter.doFilter(request, response, chain); verify(request, times(1)).getMethod(); verify(response, times(0)).setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED); verify(response, times(0)).getWriter(); verify(chain, times(1)).doFilter(request, response); Mockito.when(request.getMethod()).thenReturn("HEAD"); maintenanceModeAccessFilter.doFilter(request, response, chain); verify(request, times(2)).getMethod(); verify(response, times(0)).setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED); verify(response, times(0)).getWriter(); verify(chain, times(2)).doFilter(request, response); Mockito.when(request.getMethod()).thenReturn("POST"); maintenanceModeAccessFilter.doFilter(request, response, chain); verify(request, times(3)).getMethod(); verify(response, times(1)).setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED); verify(response, times(2)).getWriter(); verify(chain, times(2)).doFilter(request, response); when(file.exists()).thenReturn(false); maintenanceModeAccessFilter.doFilter(request, response, chain); verify(request, times(3)).getMethod(); verify(response, times(1)).setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED); verify(response, times(2)).getWriter(); verify(chain, times(3)).doFilter(request, response); }