### Question:
JwtAuthFilter extends AuthFilter<Optional<String>, P> { @Override public void filter(ContainerRequestContext requestContext) { Span span = tracer .buildSpan("validateToken") .withTag(COMPONENT, "JwtAuthFilter") .withTag(AUTHENTICATED, false) .start(); try (Scope ignored = tracer.scopeManager().activate(span)) { final MultivaluedMap<String, String> headers = requestContext.getHeaders(); final String jwt = extractAuthorizationToken(headers); boolean authenticated = authenticate(requestContext, Optional.ofNullable(jwt), SecurityContext.BASIC_AUTH); span.setTag(AUTHENTICATED, authenticated); if (!acceptAnonymous && !authenticated) { throw new JwtAuthException("Credentials are required to access this resource."); } } finally { span.finish(); } } private  JwtAuthFilter(); @Override void filter(ContainerRequestContext requestContext);  }### Answer:
@Test(expected = JwtAuthException.class) public void throwsOnDefaultEmpty() throws AuthenticationException { MultivaluedStringMap headers = new MultivaluedStringMap(); when(requestContext.getHeaders()).thenReturn(headers); when(authenticator.authenticate(credentialsCaptor.capture())).thenReturn(Optional.empty()); JwtAuthFilter authFilter = new Builder<JwtPrincipal>().setAuthenticator(authenticator).buildAuthFilter(); authFilter.filter(requestContext); }
@Test public void shouldAcceptOnDefaultPayload() throws AuthenticationException { MultivaluedStringMap headers = new MultivaluedStringMap(); headers.add(HttpHeaders.AUTHORIZATION, "Bearer MY_JWT"); when(requestContext.getHeaders()).thenReturn(headers); when(authenticator.authenticate(credentialsCaptor.capture())) .thenReturn(Optional.of(JwtPrincipal.emptyPrincipal())); JwtAuthFilter authFilter = new Builder<JwtPrincipal>().setAuthenticator(authenticator).buildAuthFilter(); authFilter.filter(requestContext); assertThat(credentialsCaptor.getValue()).contains("MY_JWT"); }
@Test public void shouldAcceptOnAnonymousEmpty() throws AuthenticationException { MultivaluedStringMap headers = new MultivaluedStringMap(); when(requestContext.getHeaders()).thenReturn(headers); when(authenticator.authenticate(credentialsCaptor.capture())).thenReturn(Optional.empty()); JwtAuthFilter authFilter = new Builder<JwtPrincipal>() .setAcceptAnonymous(true) .setAuthenticator(authenticator) .buildAuthFilter(); authFilter.filter(requestContext); assertThat(credentialsCaptor.getValue()).isEmpty(); }
@Test public void shouldAcceptOnAnonymousPayload() throws AuthenticationException { MultivaluedStringMap headers = new MultivaluedStringMap(); headers.add(HttpHeaders.AUTHORIZATION, "Bearer MY_JWT"); when(requestContext.getHeaders()).thenReturn(headers); when(authenticator.authenticate(credentialsCaptor.capture())) .thenReturn(Optional.of(JwtPrincipal.emptyPrincipal())); JwtAuthFilter authFilter = new Builder<JwtPrincipal>() .setAcceptAnonymous(true) .setAuthenticator(authenticator) .buildAuthFilter(); authFilter.filter(requestContext); assertThat(credentialsCaptor.getValue()).contains("MY_JWT"); }