package com.auth0;

import static java.util.Collections.emptyEnumeration;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;

import org.junit.Test;

public class Auth0ServletCallbackTest {

    @Test(expected = IllegalArgumentException.class)
    public void failOnMissingInitParameters() throws ServletException {
        final Auth0ServletCallback cb = new Auth0ServletCallback();
        cb.init(mock(ServletConfig.class));
    }

    @Test(expected = IllegalArgumentException.class)
    public void failOnMissingServletContextParameters() throws ServletException {
        final Auth0ServletCallback cb = new Auth0ServletCallback();
        final ServletConfig config = mock(ServletConfig.class);
        when(config.getInitParameter(eq("auth0.redirect_on_success"))).thenReturn("");
        when(config.getInitParameter(eq("auth0.redirect_on_error"))).thenReturn("");
        final ServletContext ctx = mock(ServletContext.class);
        when(ctx.getInitParameterNames()).thenReturn(emptyEnumeration());
        when(config.getServletContext()).thenReturn(ctx);
        cb.init(config);
    }
}

@@ -41,36 +41,101 @@
 til.
-Collections.emptyEnumeration
+Arrays.asList;%0Aimport static org.junit.Assert.assertTrue;%0Aimport static org.junit.Assert.fail
 ;%0Aim
@@ -248,24 +248,76 @@
 kito.when;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.List;%0A%0A
 import javax
@@ -495,351 +495,719 @@
 Test
-(expected = IllegalArgumentException.class)%0A    public void failOnMissingInitParameters() throws ServletException %7B%0A        final Auth0ServletCallback cb = new Auth0ServletCallback();%0A        cb.init(mock(ServletConfig.class));
+%0A    public void readsParametersFromConfigOrContext() throws ServletException %7B%0A        final List%3CString%3E parameters = asList(%0A                %22auth0.redirect_on_success%22,%0A                %22auth0.redirect_on_error%22,%0A                %22auth0.client_id%22,%0A                %22auth0.client_secret%22,%0A                %22auth0.domain%22);%0A        for(final String missing : parameters) %7B
 %0A    
-%7D%0A%0A
+ 
     
-@Test(expected = IllegalArgumentException.class)%0A    public void failOnMissingServletContextParameters()
+   final List%3CString%3E defined = new ArrayList%3C%3E(parameters);%0A            defined.remove(missing);%0A            paramsDefinedInConfig(missing, defined);%0A            paramsDefinedInContext(missing, defined);%0A        %7D%0A    %7D%0A%0A    private static void paramsDefinedInContext(final String missing, final List%3CString%3E defined)%0A           
  thr
@@ -1243,29 +1243,24 @@
   final 
-Auth0
 ServletC
 allback 
@@ -1255,46 +1255,60 @@
 letC
-allback cb = new Auth0ServletCallback(
+onfig configWithoutParams = mock(ServletConfig.class
 );%0A 
@@ -1330,26 +1330,34 @@
 rvletCon
-fig config
+text ctxWithParams
  = mock(
@@ -1358,35 +1358,36 @@
  mock(ServletCon
-fig
+text
 .class);%0A       
@@ -1391,67 +1391,108 @@
     
-when(config.getInitParameter(eq(%22auth0.redirect_on_success%22
+for(final String parameter : defined) %7B%0A            when(ctxWithParams.getInitParameter(eq(parameter
 ))).
@@ -1495,36 +1495,58 @@
 ))).thenReturn(%22
+i-am-defined
 %22);%0A
+        %7D%0A
         when(con
@@ -1552,73 +1552,242 @@
 nfig
-.getIni
+Withou
 tParam
+s.g
 et
+S
 er
-(eq(%22auth0.redirect_on_error%22))).thenReturn(%22%22);
+vletContext()).thenReturn(ctxWithParams);%0A        init(missing, configWithoutParams);%0A    %7D%0A%0A    private static void paramsDefinedInConfig(final String missing, final List%3CString%3E defined) throws ServletException %7B
 %0A   
@@ -1807,24 +1807,36 @@
 rvletCon
-text ctx
+fig configWithParams
  = mock(
@@ -1837,36 +1837,35 @@
  mock(ServletCon
-text
+fig
 .class);%0A       
@@ -1869,74 +1869,153 @@
     
-when(ctx.getInitParameterNames()).thenReturn(emptyEnumeration()
+for(final String parameter : defined) %7B%0A            when(configWithParams.getInitParameter(eq(parameter))).thenReturn(%22i-am-defined%22
 );%0A
+        %7D%0A
     
@@ -2029,16 +2029,26 @@
 n(config
+WithParams
 .getServ
@@ -2076,38 +2076,559 @@
 urn(
-ctx);%0A        cb.init(config);
+mock(ServletContext.class));%0A        init(missing, configWithParams);%0A    %7D%0A%0A    private static void init(final String missing, final ServletConfig config) throws ServletException %7B%0A        final Auth0ServletCallback cb = new Auth0ServletCallback();%0A        try %7B%0A            cb.init(config);%0A            fail(%22Expected init to fail on missing parameter: %22 + missing);%0A        %7D%0A        catch(final IllegalArgumentException e) %7B%0A            assertTrue(%22Expected failure to reference parameter: %22 + missing, e.getMessage().contains(missing));%0A        %7D
 %0A   

Rewrite Auth0ServletCallbackTest to test validartion on all parameters individually (for both ServletConfig and ServletContext)