JwtAuthenticationProvider extends TimeMeasuringAuthenticationProvider<AuthenticationResult> { @SuppressWarnings("ConstantConditions") private CompletableFuture<AuthenticationResult> getAuthenticationResult(final JsonWebToken jwt, final DittoHeaders dittoHeaders) { return jwtValidator.validate(jwt) .thenApply(validationResult -> { if (!validationResult.isValid()) { final Throwable reasonForInvalidity = validationResult.getReasonForInvalidity(); LOGGER.withCorrelationId(dittoHeaders).debug("The JWT is invalid.", reasonForInvalidity); throw buildJwtUnauthorizedException(dittoHeaders, reasonForInvalidity); } final AuthenticationResult authenticationResult = tryToGetAuthenticationResult(jwt, dittoHeaders); LOGGER.withCorrelationId(dittoHeaders).info("Completed JWT authentication successfully."); return authenticationResult; }); } private  JwtAuthenticationProvider(final JwtAuthenticationResultProvider jwtAuthenticationResultProvider,
            final JwtValidator jwtValidator); static JwtAuthenticationProvider newInstance(
            final JwtAuthenticationResultProvider jwtAuthenticationResultProvider, final JwtValidator jwtValidator); @Override boolean isApplicable(final RequestContext requestContext); @Override AuthorizationContextType getType(final RequestContext requestContext);  }
@Test public void doExtractAuthenticationWhenAuthorizationContextProviderErrors() { when(jwtValidator.validate(any(JsonWebToken.class))) .thenReturn(CompletableFuture.completedFuture(BinaryValidationResult.valid())); when(authenticationContextProvider.getAuthenticationResult(any(JsonWebToken.class), any(DittoHeaders.class))) .thenThrow(new RuntimeException("Something happened")); final RequestContext requestContext = mockRequestContext(VALID_AUTHORIZATION_HEADER); final AuthenticationResult authenticationResult = underTest.authenticate(requestContext, knownDittoHeaders); verify(authenticationContextProvider).getAuthenticationResult(any(JsonWebToken.class), any(DittoHeaders.class)); softly.assertThat(authenticationResult.isSuccess()).isFalse(); softly.assertThat(authenticationResult.getReasonOfFailure()) .isExactlyInstanceOf(GatewayAuthenticationFailedException.class); final GatewayAuthenticationFailedException reasonOfFailure = (GatewayAuthenticationFailedException) authenticationResult.getReasonOfFailure(); softly.assertThat(reasonOfFailure).hasMessage("The JWT could not be verified."); softly.assertThat(reasonOfFailure.getDescription()).contains("Something happened"); softly.assertThat(reasonOfFailure.getDittoHeaders().getCorrelationId()) .isEqualTo(knownDittoHeaders.getCorrelationId()); }