### Question:
KnoxAuthenticationFilter extends NiFiAuthenticationFilter { @Override public Authentication attemptAuthentication(final HttpServletRequest request) { if (!request.isSecure()) { return null; } final NiFiProperties properties = getProperties(); if (!properties.isKnoxSsoEnabled()) { return null; } final String knoxJwt = getJwtFromCookie(request, properties.getKnoxCookieName()); if (knoxJwt == null) { return null; } else { return new KnoxAuthenticationRequestToken(knoxJwt, request.getRemoteAddr()); } }  @Override Authentication attemptAuthentication(final HttpServletRequest request); String getJwtFromCookie(final HttpServletRequest request, final String cookieName);  }### Answer:
@Test public void testInsecureHttp() throws Exception { final HttpServletRequest request = mock(HttpServletRequest.class); when(request.isSecure()).thenReturn(false); assertNull(knoxAuthenticationFilter.attemptAuthentication(request)); }
@Test public void testNullCookies() throws Exception { final HttpServletRequest request = mock(HttpServletRequest.class); when(request.isSecure()).thenReturn(true); when(request.getCookies()).thenReturn(null); assertNull(knoxAuthenticationFilter.attemptAuthentication(request)); }
@Test public void testNoCookies() throws Exception { final HttpServletRequest request = mock(HttpServletRequest.class); when(request.isSecure()).thenReturn(true); when(request.getCookies()).thenReturn(new Cookie[] {}); assertNull(knoxAuthenticationFilter.attemptAuthentication(request)); }
@Test public void testWrongCookieName() throws Exception { final String jwt = "my-jwt"; final Cookie knoxCookie = mock(Cookie.class); when(knoxCookie.getName()).thenReturn("not-hadoop-jwt"); when(knoxCookie.getValue()).thenReturn(jwt); final HttpServletRequest request = mock(HttpServletRequest.class); when(request.isSecure()).thenReturn(true); when(request.getCookies()).thenReturn(new Cookie[] {knoxCookie}); final KnoxAuthenticationRequestToken authRequest = (KnoxAuthenticationRequestToken) knoxAuthenticationFilter.attemptAuthentication(request); assertNull(authRequest); }
@Test public void testKnoxCookie() throws Exception { final String jwt = "my-jwt"; final Cookie knoxCookie = mock(Cookie.class); when(knoxCookie.getName()).thenReturn(COOKIE_NAME); when(knoxCookie.getValue()).thenReturn(jwt); final HttpServletRequest request = mock(HttpServletRequest.class); when(request.isSecure()).thenReturn(true); when(request.getCookies()).thenReturn(new Cookie[] {knoxCookie}); final KnoxAuthenticationRequestToken authRequest = (KnoxAuthenticationRequestToken) knoxAuthenticationFilter.attemptAuthentication(request); assertNotNull(authRequest); assertEquals(jwt, authRequest.getToken()); }