package org.slc.sli.admin.client;

import java.io.IOException;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import junit.framework.Assert;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;

import org.slc.sli.admin.test.bootstrap.WebContextTestExecutionListener;

/**
 * 
 * @author scole
 * 
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/spring/commonCtx.xml" })
@TestExecutionListeners({ WebContextTestExecutionListener.class, DependencyInjectionTestExecutionListener.class,
        DirtiesContextTestExecutionListener.class })
public class AuthenticationInterceptorTest {
    
    private static final String RAW_JSON = "{\"authenticated\":false,\"redirect_user\":\"http://testapi1.slidev.org:8080/disco/realms/list.do\"}";
    private AuthenticationInterceptor interceptor;
    
    @Before
    public void init() {
        interceptor = new AuthenticationInterceptor();

    }
    
    @Test
    public void testNoSessionHaveCookie() throws IOException {
        MockHttpServletRequest request = new MockHttpServletRequest();
        HttpServletResponse response = new MockHttpServletResponse();
        request.setCookies(new Cookie("iPlanetDirectoryPro", "1234567890"));
        
        try {
            interceptor.preHandle(request, response, null);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        Assert.assertEquals("SessionId was not set in session attributes", "1234567890", request.getSession()
                .getAttribute("ADMIN_SESSION_ID"));
    }
    
    @Test
    public void testHaveSession() throws IOException {
        MockHttpServletRequest request = new MockHttpServletRequest();
        HttpServletResponse response = new MockHttpServletResponse();
        request.getSession().setAttribute("ADMIN_SESSION_ID", "1234567890");
        
        try {
            interceptor.preHandle(request, response, null);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        Assert.assertEquals("SessionId was not set in session attributes", "1234567890", request.getSession()
                .getAttribute("ADMIN_SESSION_ID"));
    }
    
    @Test
    public void testNoSessionNoCookie() throws IOException {
        MockHttpServletRequest request = new MockHttpServletRequest();
        MockHttpServletResponse response = new MockHttpServletResponse();
        
        RESTClient rest = Mockito.mock(RESTClient.class);
        JsonParser parser = new JsonParser();
        JsonObject jsonArray = parser.parse(RAW_JSON).getAsJsonObject();
        Mockito.when(rest.sessionCheck(null)).thenReturn(jsonArray);
        interceptor.setRESTClient(rest);
        
        try {
            interceptor.preHandle(request, response, null);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String expectedUrl = "http://testapi1.slidev.org:8080/disco/realms/list.do?RelayState=http";
            
        Assert.assertTrue("Redirect url is not correct", response.getRedirectedUrl().startsWith(expectedUrl));
    }
}

@@ -1304,19 +1304,26 @@
  String 
-RAW
+NO_SESSION
 _JSON = 
@@ -1426,16 +1426,125 @@
 do%5C%22%7D%22;%0A
+    private static final String VALID_SESSION_JSON = %22%7B%5C%22authenticated%5C%22:true,%5C%22full_name%5C%22:%5C%22Test User%5C%22%7D%22;%0A
     priv
@@ -1586,16 +1586,45 @@
 ceptor;%0A
+    private RESTClient rest;%0A
     %0A   
@@ -1712,16 +1712,103 @@
 ptor();%0A
+        rest = Mockito.mock(RESTClient.class);%0A        interceptor.setRESTClient(rest);
 %0A    %7D%0A 
@@ -2103,32 +2103,238 @@
 90%22));%0A        %0A
+        JsonParser parser = new JsonParser();%0A        JsonObject jsonArray = parser.parse(VALID_SESSION_JSON).getAsJsonObject();%0A        Mockito.when(rest.sessionCheck(%221234567890%22)).thenReturn(jsonArray);%0A
         try %7B%0A  
@@ -2676,32 +2676,150 @@
 _SESSION_ID%22));%0A
+        Assert.assertEquals(%22Full name does not match%22, %22Test User%22, request.getSession().getAttribute(%22USER_NAME%22));%0A
     %7D%0A    %0A    @
@@ -3097,32 +3097,41 @@
 890%22);%0A        %0A
+        %0A
         try %7B%0A  
@@ -3712,74 +3712,8 @@
 ();%0A
-        %0A        RESTClient rest = Mockito.mock(RESTClient.class);
 %0A   
@@ -3803,11 +3803,18 @@
 rse(
-RAW
+NO_SESSION
 _JSO
@@ -3908,49 +3908,8 @@
 y);%0A
-        interceptor.setRESTClient(rest);%0A
     

add full_name support in test