@Test
  public void adaptedAsyncServerAuthModuleShouldAdaptSuccessfulValidateRequestCall()
      throws AuthException {

    // Given
    ServerAuthModule authModule = mock(ServerAuthModule.class);
    MessageInfoContext messageInfo = mock(MessageInfoContext.class);
    Subject clientSubject = new Subject();
    Subject serviceSubject = new Subject();

    given(authModule.validateRequest(any(MessageInfo.class), eq(clientSubject), eq(serviceSubject)))
        .willReturn(AuthStatus.SUCCESS);

    // When
    AsyncServerAuthModule asyncAuthModule = JaspiAdapters.adapt(authModule);
    Promise<AuthStatus, AuthenticationException> promise =
        asyncAuthModule.validateRequest(messageInfo, clientSubject, serviceSubject);

    // Then
    assertThat(promise).succeeded().withObject().isEqualTo(AuthStatus.SUCCESS);
  }
  @Test
  public void adaptedAsyncServerAuthModuleShouldAdaptFailedValidateRequestCall()
      throws AuthException {

    // Given
    ServerAuthModule authModule = mock(ServerAuthModule.class);
    MessageInfoContext messageInfo = mock(MessageInfoContext.class);
    Subject clientSubject = new Subject();
    Subject serviceSubject = new Subject();

    doThrow(AuthException.class)
        .when(authModule)
        .validateRequest(any(MessageInfo.class), eq(clientSubject), eq(serviceSubject));

    // When
    AsyncServerAuthModule asyncAuthModule = JaspiAdapters.adapt(authModule);
    Promise<AuthStatus, AuthenticationException> promise =
        asyncAuthModule.validateRequest(messageInfo, clientSubject, serviceSubject);

    // Then
    assertThat(promise).failedWithException().isInstanceOf(AuthenticationException.class);
  }
 private Promise<AuthStatus, AuthenticationException> validateRequest(
     final MessageInfoContext messageInfo,
     final Subject clientSubject,
     final Subject serviceSubject) {
   if (position < authModules.size()) {
     final AsyncServerAuthModule authModule = authModules.get(position);
     return authModule
         .validateRequest(messageInfo, clientSubject, serviceSubject)
         .thenOnResult(
             new ResultHandler<AuthStatus>() {
               @Override
               public void handleResult(AuthStatus authStatus) {
                 if (isSuccess(authStatus)) {
                   /*
                    * Save the index of the authenticating module so that it can
                    * be retrieved when securing the response
                    */
                   logger.trace(
                       "Adding authenticating auth module to private context map, {}",
                       authModule.getClass().getSimpleName());
                   state.setAuthenticatedAuthModuleIndex(position);
                 }
               }
             })
         .thenAsync(
             new AsyncFunction<AuthStatus, AuthStatus, AuthenticationException>() {
               @Override
               public Promise<AuthStatus, AuthenticationException> apply(AuthStatus authStatus) {
                 if (isSendFailure(authStatus)) {
                   return next().validateRequest(messageInfo, clientSubject, serviceSubject);
                 } else {
                   return Promises.newResultPromise(authStatus);
                 }
               }
             });
   } else {
     return Promises.newResultPromise(AuthStatus.SEND_FAILURE);
   }
 }