package org.jasig.cas.web.flow;

import org.jasig.cas.authentication.AuthenticationException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.springframework.binding.message.MessageContext;

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

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");
    }
    
}

@@ -80,106 +80,450 @@
 tion
-Exception;%0Aimport org.junit.Test;%0Aimport org.junit.runner.RunWith;%0Aimport org.junit.runners.JUnit4
+;%0Aimport org.jasig.cas.authentication.AuthenticationException;%0Aimport org.jasig.cas.authentication.ContextualAuthenticationPolicy;%0Aimport org.jasig.cas.ticket.InvalidTicketException;%0Aimport org.jasig.cas.ticket.UnsatisfiedAuthenticationPolicyException;%0Aimport org.junit.Test;%0Aimport org.junit.runner.RunWith;%0Aimport org.junit.runners.JUnit4;%0Aimport org.mockito.ArgumentCaptor;%0Aimport org.springframework.binding.message.DefaultMessageResolver
 ;%0Aim
@@ -733,16 +733,43 @@
 til.Map;
+%0Aimport java.util.Optional;
 %0A%0Aimport
@@ -1990,15 +1990,1583 @@
 ;%0A    %7D%0A
+%0A
     
+@Test%0A    public void handleUnknownTicketExceptionByDefault() %7B%0A        final AuthenticationExceptionHandler handler = new AuthenticationExceptionHandler();%0A        final MessageContext ctx = mock(MessageContext.class);%0A%0A        final String id = handler.handle(new InvalidTicketException(%22TGT%22), ctx);%0A        assertEquals(id, %22UNKNOWN%22);%0A        verifyZeroInteractions(ctx);%0A    %7D%0A%0A    @Test%0A    public void handleUnsatisfiedAuthenticationPolicyExceptionByDefault() %7B%0A        final AuthenticationExceptionHandler handler = new AuthenticationExceptionHandler();%0A        final MessageContext ctx = mock(MessageContext.class);%0A%0A        final ContextualAuthenticationPolicy%3C?%3E policy = new ContextualAuthenticationPolicy%3CObject%3E() %7B%0A            @Override%0A            public Optional%3CString%3E getCode() %7B%0A                return Optional.of(%22CUSTOM_CODE%22);%0A            %7D%0A%0A            @Override%0A            public Object getContext() %7B%0A                return null;%0A            %7D%0A%0A            @Override%0A            public boolean isSatisfiedBy(final Authentication authentication) %7B%0A                return false;%0A            %7D%0A        %7D;%0A        final String id = handler.handle(new UnsatisfiedAuthenticationPolicyException(policy), ctx);%0A        assertEquals(id, %22UnsatisfiedAuthenticationPolicyException%22);%0A        ArgumentCaptor%3CDefaultMessageResolver%3E message = ArgumentCaptor.forClass(DefaultMessageResolver.class);%0A        verify(ctx, times(1)).addMessage(message.capture());%0A        assertArrayEquals(new String%5B%5D%7B%22CUSTOM_CODE%22%7D, message.getValue().getCodes());%0A    %7D
 %0A%7D%0A

add tests for AbstractTicketException handling logic