/**
   * Calls each {@code AsyncServerAuthContext} in parallel to clean the client subject and only
   * return a successful promise if all complete successfully otherwise returns the first exception
   * in a failed promise.
   *
   * @param context {@inheritDoc}
   * @param clientSubject {@inheritDoc}
   * @return {@inheritDoc}
   */
  @Override
  public Promise<Void, AuthenticationException> cleanSubject(
      MessageContext context, Subject clientSubject) {

    List<Promise<Void, AuthenticationException>> promises = new ArrayList<>();
    for (AsyncServerAuthModule serverAuthModule : authModules) {
      promises.add(serverAuthModule.cleanSubject(context, clientSubject));
    }
    return Promises.when(promises).thenAsync(ON_SUCCESS_RETURN_VOID);
  }
  @Test
  public void adaptedAsyncServerAuthModuleShouldAdaptSuccessfulCleanSubjectCall()
      throws AuthException {

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

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

    // Then
    assertThat(promise).succeeded().withObject().isNull();
    verify(authModule).cleanSubject(any(MessageInfo.class), eq(clientSubject));
  }
  @Test
  public void adaptedAsyncServerAuthModuleShouldAdaptFailedCleanSubjectCall() throws AuthException {

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

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

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

    // Then
    assertThat(promise).failedWithException().isInstanceOf(AuthenticationException.class);
  }