@Test
  public void adaptedAsyncServerAuthModuleShouldAdaptGetModuleIdCall() {

    // Given
    ServerAuthModule authModule = mock(ServerAuthModule.class);

    // When
    AsyncServerAuthModule asyncAuthModule = JaspiAdapters.adapt(authModule);
    String moduleId = asyncAuthModule.getModuleId();

    // Then
    Assertions.assertThat(moduleId).isEqualTo(authModule.getClass().getCanonicalName());
  }
  @Test
  public void adaptedMessageInfoShouldAdaptGetMapCall() {

    // Given
    MessageInfoContext messageInfoContext = mock(MessageInfoContext.class);

    // When
    MessageInfo messageInfo = JaspiAdapters.adapt(messageInfoContext);
    messageInfo.getMap();

    // Then
    verify(messageInfoContext).getRequestContextMap();
  }
  @Test
  public void adaptedAuthenticationExceptionShouldHaveSameDetailMessage() {

    // Given
    AuthException authException = new AuthException("MESSAGE");

    // When
    AuthenticationException authenticationException = JaspiAdapters.adapt(authException);

    // Then
    Assertions.assertThat(authenticationException.getMessage()).isEqualTo("MESSAGE");
    Assertions.assertThat(authenticationException.getCause()).isNull();
  }
  @Test
  public void adaptedMessageInfoShouldAdaptSetResponseMessageCall() {

    // Given
    MessageInfoContext messageInfoContext = mock(MessageInfoContext.class);
    Response response = new Response();

    // When
    MessageInfo messageInfo = JaspiAdapters.adapt(messageInfoContext);
    messageInfo.setResponseMessage(response);

    // Then
    verify(messageInfoContext).setResponse(response);
  }
  @Test
  public void adaptedAsyncServerAuthModuleShouldAdaptGetSupportedMessageTypesCall() {

    // Given
    ServerAuthModule authModule = mock(ServerAuthModule.class);

    given(authModule.getSupportedMessageTypes())
        .willReturn(new Class<?>[] {Request.class, Response.class});

    // When
    AsyncServerAuthModule asyncAuthModule = JaspiAdapters.adapt(authModule);
    asyncAuthModule.getSupportedMessageTypes();

    // Then
    verify(authModule).getSupportedMessageTypes();
  }
  @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 adaptedAsyncServerAuthContextShouldAdaptSuccessfulSecureResponseCall()
      throws AuthException {

    // Given
    ServerAuthContext authContext = mock(ServerAuthContext.class);
    MessageContext messageContext = mock(MessageContext.class);
    Subject serviceSubject = new Subject();

    given(authContext.secureResponse(any(MessageInfo.class), eq(serviceSubject)))
        .willReturn(AuthStatus.SEND_SUCCESS);

    // When
    AsyncServerAuthContext asyncAuthContext = JaspiAdapters.adapt(authContext);
    Promise<AuthStatus, AuthenticationException> promise =
        asyncAuthContext.secureResponse(messageContext, serviceSubject);

    // Then
    assertThat(promise).succeeded().withObject().isEqualTo(AuthStatus.SEND_SUCCESS);
  }
  @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);
  }
  @Test
  public void adaptedAsyncServerAuthModuleShouldAdaptSuccessfulGetInitializeCall()
      throws AuthException {

    // Given
    ServerAuthModule authModule = mock(ServerAuthModule.class);
    MessagePolicy requestPolicy = mock(MessagePolicy.class);
    MessagePolicy responsePolicy = mock(MessagePolicy.class);
    CallbackHandler handler = mock(CallbackHandler.class);
    Map<String, Object> options = Collections.emptyMap();

    // When
    AsyncServerAuthModule asyncAuthModule = JaspiAdapters.adapt(authModule);
    Promise<Void, AuthenticationException> promise =
        asyncAuthModule.initialize(requestPolicy, responsePolicy, handler, options);

    // Then
    assertThat(promise).succeeded().withObject().isNull();
    verify(authModule).initialize(requestPolicy, responsePolicy, handler, options);
  }
  @Test
  public void adaptedAsyncServerAuthModuleShouldAdaptFailedSecureResponseCall()
      throws AuthException {

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

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

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

    // Then
    assertThat(promise).failedWithException().isInstanceOf(AuthenticationException.class);
  }
  @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 adaptedAsyncServerAuthContextShouldAdaptFailedValidateRequestCall()
      throws AuthException {

    // Given
    ServerAuthContext authContext = mock(ServerAuthContext.class);
    MessageContext messageContext = mock(MessageContext.class);
    Subject clientSubject = new Subject();
    Subject serviceSubject = new Subject();

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

    // When
    AsyncServerAuthContext asyncAuthContext = JaspiAdapters.adapt(authContext);
    Promise<AuthStatus, AuthenticationException> promise =
        asyncAuthContext.validateRequest(messageContext, clientSubject, serviceSubject);

    // Then
    assertThat(promise).failedWithException().isInstanceOf(AuthenticationException.class);
  }
  @Test
  public void adaptedAsyncServerAuthModuleShouldAdaptFailedGetInitializeCall()
      throws AuthException {

    // Given
    ServerAuthModule authModule = mock(ServerAuthModule.class);
    MessagePolicy requestPolicy = mock(MessagePolicy.class);
    MessagePolicy responsePolicy = mock(MessagePolicy.class);
    CallbackHandler handler = mock(CallbackHandler.class);
    Map<String, Object> options = Collections.emptyMap();

    doThrow(AuthException.class)
        .when(authModule)
        .initialize(requestPolicy, responsePolicy, handler, options);

    // When
    AsyncServerAuthModule asyncAuthModule = JaspiAdapters.adapt(authModule);
    Promise<Void, AuthenticationException> promise =
        asyncAuthModule.initialize(requestPolicy, responsePolicy, handler, options);

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