package org.apereo.cas.authentication.mfa.trigger;

import org.apereo.cas.authentication.AuthenticationException;
import org.apereo.cas.authentication.mfa.TestMultifactorAuthenticationProvider;
import org.apereo.cas.authentication.principal.Service;
import org.apereo.cas.authentication.trigger.GlobalMultifactorAuthenticationTrigger;
import org.apereo.cas.configuration.CasConfigurationProperties;

import lombok.val;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

/**
 * This is {@link GlobalMultifactorAuthenticationTriggerTests}.
 *
 * @author Misagh Moayyed
 * @since 6.1.0
 */
@Tag("MFA")
public class GlobalMultifactorAuthenticationTriggerTests extends BaseMultifactorAuthenticationTriggerTests {
    @Test
    public void verifyOperationByProvider() {
        val props = new CasConfigurationProperties();
        props.getAuthn().getMfa().setGlobalProviderId(TestMultifactorAuthenticationProvider.ID);
        val trigger = new GlobalMultifactorAuthenticationTrigger(props, applicationContext,
            (providers, service, principal) -> providers.iterator().next());
        val result = trigger.isActivated(authentication, registeredService, this.httpRequest, mock(Service.class));
        assertTrue(result.isPresent());
    }

    @Test
    public void verifyOperationByManyProviders() {
        val props = new CasConfigurationProperties();
        props.getAuthn().getMfa().setGlobalProviderId(TestMultifactorAuthenticationProvider.ID + ",mfa-invalid");
        val trigger = new GlobalMultifactorAuthenticationTrigger(props, applicationContext,
            (providers, service, principal) -> providers.iterator().next());
        assertThrows(AuthenticationException.class,
            () -> trigger.isActivated(authentication, registeredService, this.httpRequest, mock(Service.class)));
    }

    @Test
    public void verifyOperationByUnresolvedProvider() {
        val props = new CasConfigurationProperties();
        props.getAuthn().getMfa().setGlobalProviderId("does-not-exist");
        val trigger = new GlobalMultifactorAuthenticationTrigger(props, applicationContext,
            (providers, service, principal) -> providers.iterator().next());
        assertThrows(AuthenticationException.class,
            () -> trigger.isActivated(authentication, registeredService, this.httpRequest, mock(Service.class)));
    }

    @Test
    public void verifyOperationByUndefinedProvider() {
        val props = new CasConfigurationProperties();
        val trigger = new GlobalMultifactorAuthenticationTrigger(props, applicationContext,
            (providers, service, principal) -> providers.iterator().next());
        val result = trigger.isActivated(authentication, registeredService, this.httpRequest, mock(Service.class));
        assertFalse(result.isPresent());
    }
}

@@ -1338,32 +1338,36 @@
 esent());%0A    %7D%0A
+    
 %0A    @Test%0A    p
@@ -1914,32 +1914,931 @@
 lass)));%0A    %7D%0A%0A
+    @Test%0A    public void verifyOperationByValidProviders() %7B%0A        val props = new CasConfigurationProperties();%0A%0A        val otherProvider = new TestMultifactorAuthenticationProvider();%0A        otherProvider.setId(%22mfa-other%22);%0A        TestMultifactorAuthenticationProvider.registerProviderIntoApplicationContext(applicationContext, otherProvider);%0A%0A        props.getAuthn().getMfa().setGlobalProviderId(TestMultifactorAuthenticationProvider.ID + ',' + otherProvider.getId());%0A        val trigger = new GlobalMultifactorAuthenticationTrigger(props, applicationContext,%0A            (providers, service, principal) -%3E providers.iterator().next());%0A        val result = trigger.isActivated(authentication, registeredService, this.httpRequest, mock(Service.class));%0A        assertTrue(result.isPresent());%0A        assertEquals(TestMultifactorAuthenticationProvider.ID, result.get().getId());%0A    %7D%0A%0A
     @Test%0A    pu

add tests