### Question:
AmbariKerberosAuthenticationFilter extends SpnegoAuthenticationProcessingFilter implements AmbariAuthenticationFilter { @Override public boolean shouldApply(HttpServletRequest httpServletRequest) { if (LOG.isDebugEnabled()) { RequestUtils.logRequestHeadersAndQueryParams(httpServletRequest, LOG); } if (kerberosAuthenticationEnabled) { String header = httpServletRequest.getHeader("Authorization"); return (header != null) && (header.startsWith("Negotiate ") || header.startsWith("Kerberos ")); } else { return false; } } AmbariKerberosAuthenticationFilter(AuthenticationManager authenticationManager,
                                            final AuthenticationEntryPoint entryPoint,
                                            Configuration configuration,
                                            final AmbariAuthenticationEventHandler eventHandler); @Override boolean shouldApply(HttpServletRequest httpServletRequest); @Override boolean shouldIncrementFailureCount(); @Override void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain);  }### Answer:
@Test public void shouldApplyTrue() throws Exception { HttpServletRequest httpServletRequest = createMock(HttpServletRequest.class); expect(httpServletRequest.getHeader("Authorization")).andReturn("Negotiate .....").once(); expect(configuration.getKerberosAuthenticationProperties()).andReturn(createProperties(true)).once(); replayAll(); AmbariKerberosAuthenticationFilter filter = new AmbariKerberosAuthenticationFilter( authenticationManager, entryPoint, configuration, eventHandler ); Assert.assertTrue(filter.shouldApply(httpServletRequest)); verifyAll(); }
@Test public void shouldApplyFalseMissingHeader() throws Exception { HttpServletRequest httpServletRequest = createMock(HttpServletRequest.class); expect(httpServletRequest.getHeader("Authorization")).andReturn(null).once(); expect(configuration.getKerberosAuthenticationProperties()).andReturn(createProperties(true)).once(); replayAll(); AmbariKerberosAuthenticationFilter filter = new AmbariKerberosAuthenticationFilter( authenticationManager, entryPoint, configuration, eventHandler ); Assert.assertFalse(filter.shouldApply(httpServletRequest)); verifyAll(); }
@Test public void shouldApplyNotFalseEnabled() throws Exception { HttpServletRequest httpServletRequest = createMock(HttpServletRequest.class); expect(configuration.getKerberosAuthenticationProperties()).andReturn(createProperties(false)).once(); replayAll(); AmbariKerberosAuthenticationFilter filter = new AmbariKerberosAuthenticationFilter( authenticationManager, entryPoint, configuration, eventHandler ); Assert.assertFalse(filter.shouldApply(httpServletRequest)); verifyAll(); }