### Question:
AmbariJwtAuthenticationFilter implements AmbariAuthenticationFilter { @Override public boolean shouldApply(HttpServletRequest httpServletRequest) { boolean shouldApply = false; JwtAuthenticationProperties jwtProperties = propertiesProvider.get(); if (jwtProperties != null && jwtProperties.isEnabledForAmbari()) { String serializedJWT = getJWTFromCookie(httpServletRequest); shouldApply = (serializedJWT != null && isAuthenticationRequired(serializedJWT)); } return shouldApply; } AmbariJwtAuthenticationFilter(AuthenticationEntryPoint ambariEntryPoint,
                                JwtAuthenticationPropertiesProvider propertiesProvider,
                                AmbariJwtAuthenticationProvider authenticationProvider,
                                AmbariAuthenticationEventHandler eventHandler); @Override boolean shouldApply(HttpServletRequest httpServletRequest); @Override boolean shouldIncrementFailureCount(); @Override void init(FilterConfig filterConfig); @Override void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain); @Override void destroy();  }### Answer:
@Test public void testShouldApplyTrue() throws JOSEException { JwtAuthenticationPropertiesProvider jwtAuthenticationPropertiesProvider = createMock(JwtAuthenticationPropertiesProvider.class); expect(jwtAuthenticationPropertiesProvider.get()).andReturn(createTestProperties()).anyTimes(); SignedJWT token = getInvalidToken(); Cookie cookie = createMock(Cookie.class); expect(cookie.getName()).andReturn("non-default").atLeastOnce(); expect(cookie.getValue()).andReturn(token.serialize()).atLeastOnce(); HttpServletRequest request = createMock(HttpServletRequest.class); expect(request.getCookies()).andReturn(new Cookie[]{cookie}); AmbariAuthenticationEventHandler eventHandler = createNiceMock(AmbariAuthenticationEventHandler.class); replayAll(); AmbariJwtAuthenticationFilter filter = new AmbariJwtAuthenticationFilter(null, jwtAuthenticationPropertiesProvider, null, eventHandler); assertTrue(filter.shouldApply(request)); verifyAll(); }
@Test public void testShouldApplyTrueBadToken() throws JOSEException { JwtAuthenticationPropertiesProvider jwtAuthenticationPropertiesProvider = createMock(JwtAuthenticationPropertiesProvider.class); expect(jwtAuthenticationPropertiesProvider.get()).andReturn(createTestProperties()).anyTimes(); Cookie cookie = createMock(Cookie.class); expect(cookie.getName()).andReturn("non-default").atLeastOnce(); expect(cookie.getValue()).andReturn("bad token").atLeastOnce(); HttpServletRequest request = createMock(HttpServletRequest.class); expect(request.getCookies()).andReturn(new Cookie[]{cookie}); AmbariAuthenticationEventHandler eventHandler = createNiceMock(AmbariAuthenticationEventHandler.class); replayAll(); AmbariJwtAuthenticationFilter filter = new AmbariJwtAuthenticationFilter(null, jwtAuthenticationPropertiesProvider, null, eventHandler); assertTrue(filter.shouldApply(request)); verifyAll(); }
@Test public void testShouldApplyFalseMissingCookie() throws JOSEException { JwtAuthenticationPropertiesProvider jwtAuthenticationPropertiesProvider = createMock(JwtAuthenticationPropertiesProvider.class); expect(jwtAuthenticationPropertiesProvider.get()).andReturn(createTestProperties()).anyTimes(); Cookie cookie = createMock(Cookie.class); expect(cookie.getName()).andReturn("some-other-cookie").atLeastOnce(); HttpServletRequest request = createMock(HttpServletRequest.class); expect(request.getCookies()).andReturn(new Cookie[]{cookie}); AmbariAuthenticationEventHandler eventHandler = createNiceMock(AmbariAuthenticationEventHandler.class); replayAll(); AmbariJwtAuthenticationFilter filter = new AmbariJwtAuthenticationFilter(null, jwtAuthenticationPropertiesProvider, null, eventHandler); assertFalse(filter.shouldApply(request)); verifyAll(); }
@Test public void testShouldApplyFalseNotEnabled() throws JOSEException { JwtAuthenticationPropertiesProvider jwtAuthenticationPropertiesProvider = createMock(JwtAuthenticationPropertiesProvider.class); expect(jwtAuthenticationPropertiesProvider.get()).andReturn(null).anyTimes(); HttpServletRequest request = createMock(HttpServletRequest.class); AmbariAuthenticationEventHandler eventHandler = createNiceMock(AmbariAuthenticationEventHandler.class); replayAll(); AmbariJwtAuthenticationFilter filter = new AmbariJwtAuthenticationFilter(null, jwtAuthenticationPropertiesProvider, null, eventHandler); assertFalse(filter.shouldApply(request)); verify(request); }