package org.jasig.cas.web.flow;

import com.google.common.collect.Lists;
import org.jasig.cas.authentication.Authentication;
import org.jasig.cas.authentication.AuthenticationException;
import org.jasig.cas.authentication.ContextualAuthenticationPolicy;
import org.jasig.cas.authentication.PreventedException;
import org.jasig.cas.ticket.InvalidTicketException;
import org.jasig.cas.ticket.UnsatisfiedAuthenticationPolicyException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.ArgumentCaptor;
import org.springframework.binding.message.DefaultMessageResolver;
import org.springframework.binding.message.MessageContext;

import javax.security.auth.login.AccountNotFoundException;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

/**
 * @author Marvin S. Addison
 * @since 4.0.0
 */
@RunWith(JUnit4.class)
public class AuthenticationExceptionHandlerTests {
 
    @Test
    public void handleAccountNotFoundExceptionByDefefault() {
        final AuthenticationExceptionHandler handler = new AuthenticationExceptionHandler();
        final MessageContext ctx = mock(MessageContext.class);
        
        final Map<String, Class<? extends Exception>> map = new HashMap<>();
        map.put("notFound", AccountNotFoundException.class);
        final String id = handler.handle(new AuthenticationException(map), ctx);
        assertEquals(id, AccountNotFoundException.class.getSimpleName());
    }

    @Test
    public void handleUnknownExceptionByDefefault() {
        final AuthenticationExceptionHandler handler = new AuthenticationExceptionHandler();
        final MessageContext ctx = mock(MessageContext.class);
        
        final Map<String, Class<? extends Exception>> map = new HashMap<>();
        map.put("unknown", GeneralSecurityException.class);
        final String id = handler.handle(new AuthenticationException(map), ctx);
        assertEquals(id, "UNKNOWN");
    }

    @Test
    public void handleUnknownTicketExceptionByDefault() {
        final AuthenticationExceptionHandler handler = new AuthenticationExceptionHandler();
        final MessageContext ctx = mock(MessageContext.class);

        final String id = handler.handle(new InvalidTicketException("TGT"), ctx);
        assertEquals(id, "UNKNOWN");
        verifyZeroInteractions(ctx);
    }

    @Test
    public void handleUnsatisfiedAuthenticationPolicyExceptionByDefault() {
        final AuthenticationExceptionHandler handler = new AuthenticationExceptionHandler();
        final MessageContext ctx = mock(MessageContext.class);

        final ContextualAuthenticationPolicy<?> policy = new TestContextualAuthenticationPolicy();
        final String id = handler.handle(new UnsatisfiedAuthenticationPolicyException(policy), ctx);
        assertEquals(id, "UnsatisfiedAuthenticationPolicyException");
        final ArgumentCaptor<DefaultMessageResolver> message = ArgumentCaptor.forClass(DefaultMessageResolver.class);
        verify(ctx, times(1)).addMessage(message.capture());
        assertArrayEquals(new String[]{policy.getCode().get()}, message.getValue().getCodes());
    }

    @Test
    public void customExceptionsSetterWithNullDefaultValue(){
        AuthenticationExceptionHandler handler = new AuthenticationExceptionHandler();
        assertFalse(handler.containsCustomErrors());

        List<Class<? extends Exception>> nullCustomErrors = new ArrayList<>();
        nullCustomErrors.add(null);
        handler.setErrors(nullCustomErrors);
        assertFalse(handler.containsCustomErrors());
    }

    @Test
    public void customExceptionsSetterWithTwoNewValues() {
        AuthenticationExceptionHandler handler = new AuthenticationExceptionHandler();
        List<Class<? extends Exception>> twoCustomErrors = new ArrayList<>();
        twoCustomErrors.add(GeneralSecurityException.class);
        twoCustomErrors.add(PreventedException.class);

        handler.setErrors(twoCustomErrors);
        assertTrue(handler.containsCustomErrors());
        assertTrue(handler.getErrors().containsAll(twoCustomErrors));
        assertEquals(12, handler.getErrors().size());
    }

    private static class TestContextualAuthenticationPolicy implements ContextualAuthenticationPolicy<Object> {
        @Override
        public Optional<String> getCode() {
            return Optional.of("CUSTOM_CODE");
        }

        @Override
        public Object getContext() {
            return null;
        }

        @Override
        public boolean isSatisfiedBy(final Authentication authentication) {
            return false;
        }
    }
}

@@ -30,48 +30,8 @@
 w;%0A%0A
-import com.google.common.collect.Lists;%0A
 impo
@@ -3353,32 +3353,38 @@
 alue()%7B%0A        
+final 
 AuthenticationEx
@@ -3500,32 +3500,38 @@
 rs());%0A%0A        
+final 
 List%3CClass%3C? ext
@@ -3803,16 +3803,22 @@
         
+final 
 Authenti
@@ -3896,16 +3896,22 @@
         
+final 
 List%3CCla

Make checkstyle happy