### Question:
ClientCertificateFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { if (!(request instanceof HttpServletRequest) || !(response instanceof HttpServletResponse)) { throw new IllegalStateException("Invalid request/response object"); } HttpServletResponse httpResponse = (HttpServletResponse) response; X509Certificate[] certificates = (X509Certificate[]) request. getAttribute("javax.servlet.request.X509Certificate"); if (!enableTLS || isValid(certificates)) { chain.doFilter(request, response); } else { httpResponse.sendError(Response.Status.FORBIDDEN.getStatusCode(), "Request not authorized, valid certificates not presented"); } }  @Override void init(FilterConfig filterConfig); @Override void doFilter(ServletRequest request,
                         ServletResponse response,
                         FilterChain chain); @Override void destroy();  }### Answer:
@Test public void testNoCertSecurityDisabled() throws Exception { this.enableTLS = false; when(mockRequest.getAttribute("javax.servlet.request.X509Certificate")). thenReturn(null); doFilter(mockRequest, mockResponseNoCertSecurityDisabled, mockChain); }
@Test public void testValidCertificate() throws Exception { this.enableTLS = true; when(mockRequest.getAttribute("javax.servlet.request.X509Certificate")). thenReturn(new X509Certificate[]{mockCertificateSuccess}); doFilter(mockRequest, mockResponseSuccess, mockChain); }
@Test public void testExpiredCertificate() throws Exception { this.enableTLS = true; doThrow(new CertificateExpiredException()).when(mockCertificateExpired).checkValidity(); when(mockRequest.getAttribute("javax.servlet.request.X509Certificate")). thenReturn(new X509Certificate[]{mockCertificateExpired}); doFilter(mockRequest, mockResponseExpired, mockChain); verify(mockResponseExpired).sendError(Response.Status.FORBIDDEN.getStatusCode(), "Request not authorized, valid certificates not presented"); }
@Test public void testInvalidCertificate() throws Exception { this.enableTLS = true; doThrow(new CertificateNotYetValidException()).when(mockCertificateNotValid).checkValidity(); when(mockRequest.getAttribute("javax.servlet.request.X509Certificate")). thenReturn(new X509Certificate[] {mockCertificateNotValid}); doFilter(mockRequest, mockResponseInvalid, mockChain); verify(mockResponseInvalid).sendError(Response.Status.FORBIDDEN.getStatusCode(), "Request not authorized, valid certificates not presented"); }