package ca.ulaval.ticketmaster.users;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.when;

import javax.servlet.http.HttpSession;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
//import org.springframework.mock.web.MockHttpSession;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.validation.support.BindingAwareModelMap;

import ca.ulaval.ticketmaster.dao.util.DataManager;
import ca.ulaval.ticketmaster.users.UserController;
import ca.ulaval.ticketmaster.users.model.User;
import ca.ulaval.ticketmaster.users.model.User.AccessLevel;

//import ca.ulaval.ticketmaster.web.DomaineAffaire.DAUser;

@RunWith(MockitoJUnitRunner.class)
public class UserControllerTest {

    @Mock
    public DataManager datamanager;

    // @InjectMocks
    // public DAUser domaine;

    @InjectMocks
    public UserController controller;

    private BindingAwareModelMap model;

    @Before
    public void setUp() {
	model = new BindingAwareModelMap();
    }
    
    @Test
    public void testCreateUserGET() {
	String ret = controller.CreateUser(model);

	assertNotNull(model.get("user"));
	assertEquals(ret, "CreateUser");

    }

    /*
     * @Test public void testCreateUserPOST() { //Je test directement dans le
     * domaine, puisque le controlleur ne fait qu'un appel .. ?
     * 
     * HttpSession httpSession = new MockHttpSession();
     * 
     * UserViewModel viewModel = new UserViewModel();
     * viewModel.setUsername("bob");
     * 
     * BindingResult result = mock(BindingResult.class);
     * 
     * User user = UserConverter.convert(viewModel);
     * 
     * when(datamanager.saveUser(user)).thenReturn(true);
     * 
     * String ret = domaine.createUser(user, viewModel, model, result,
     * httpSession);
     * 
     * 
     * //assertEquals(model.get("sesusername"), viewModel.getUsername());
     * assertEquals(httpSession.getAttribute("sesusername"),
     * user.getUsername()); assertEquals(ret, "Home");
     * 
     * }
     */
    @Test
    public void testLoginGET() {

	String ret = controller.Login(model);

	assertEquals(ret, "Home");

    }

    @Test
    public void testSuccessfulLogin() {

	HttpSession httpSession = new MockHttpSession();

	String adminUsername = "CarloBoutet";

	User admin = new User(adminUsername);
	admin.setPassword("123");
	admin.setAccessLevel(AccessLevel.Admin);

	when(datamanager.findUser(adminUsername)).thenReturn(admin);

	// FOQQWEQEFQOF
	// when(httpSession.setAttribute("sesusername",
	// admin.getAccessLevel().toString()).thenReturn("bob"));

	String ret = controller.Login(adminUsername, admin.getPassword(), model, httpSession);

	assertEquals(ret, "Home");

	assertNotNull(httpSession.getAttribute("sesusername"));
	assertNotNull(httpSession.getAttribute("sesacceslevel"));

	// assertNotNull(model.get("sesacceslevel"));
	// assertNotNull(model.get("sesusername"));

    }

    @Test
    public void testFailLoginWithExistingUser() {

	HttpSession httpSession = new MockHttpSession();

	User admin = new User("CarloBoutet");
	admin.setPassword("123");

	when(datamanager.findUser(admin.getUsername())).thenReturn(admin);

	String ret = controller.Login(admin.getUsername(), "NOTTHEPASSWORD", model, httpSession);

	assertEquals(ret, "Home");

	assertNull(httpSession.getAttribute("sesacceslevel"));
	assertNull(httpSession.getAttribute("sesusername"));

	// On ne verifie pas le contenu car ce serait restrictif si on change un
	// message d'erreur
	// @TODO demander au prof
	assertNotNull(model.get("errorMsg"));
	// assertNotNull(model.get("currentPage"));

    }

    @Test
    public void testFailLoginWithUnexistingUser() {

	HttpSession httpSession = new MockHttpSession();

	String ret = controller.Login("HEY!!", "FFFFFFUUUUUUUUU", model, httpSession);

	assertEquals(ret, "Home");

	assertNull(httpSession.getAttribute("sesacceslevel"));
	assertNull(httpSession.getAttribute("sesusername"));

	// On ne verifie pas le contenu car ce serait restrictif si on change un
	// message d'erreur
	// @TODO demander au prof
	assertNotNull(model.get("errorMsg"));
	// assertNotNull(model.get("currentPage"));

    }

    @Test
    public void testDisconnect() {

	HttpSession httpSession = new MockHttpSession();

	String ret = controller.Disconnect(model, httpSession);

	assertNull(httpSession.getAttribute("sesacceslevel"));
	assertNull(httpSession.getAttribute("sesusername"));

	assertEquals(ret, "Home");

    }

}

@@ -2314,27 +2314,33 @@
 .Login(model
+, null
 );%0A
-
 %0A%09assertEqua
@@ -2881,32 +2881,38 @@
 del, httpSession
+, null
 );%0A%0A%09assertEqual
@@ -3473,32 +3473,38 @@
 del, httpSession
+, null
 );%0A%0A%09assertEqual
@@ -4004,23 +4004,11 @@
 %22, %22
-FFFFFFUUUUUUUUU
+hey
 %22, m
@@ -4016,32 +4016,38 @@
 del, httpSession
+, null
 );%0A%0A%09assertEqual
@@ -4477,32 +4477,32 @@
 HttpSession();%0A%0A
-
 %09String ret = co
@@ -4539,16 +4539,22 @@
 pSession
+, null
 );%0A%0A%09ass

fix test de login