package org.yacos.tests.core;

import static org.junit.Assert.*;

import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;

import org.junit.Before;
import org.junit.Test;
import org.yacos.core.conferences.Conference;
import org.yacos.core.users.IUserManager;
import org.yacos.core.users.Role;
import org.yacos.core.users.User;

public class UserTest {
	private IUserManager manager;
	User user;
	@Before
	public void setUp() throws Exception {
		Context context = new InitialContext();
		manager = (IUserManager) context.lookup("UserManagerBean/remote");
		
		if(manager.exists("toto")){	
			manager.removeUser(manager.getUser("toto"));
		}
		manager.addUser("toto","mp","bruno","dupont","ipint","e@e.fr");
		user = manager.getUser("toto");
	}
	
	@Test
	public void testManagerPresence(){
		assertNotNull(manager);
	}
	
	@Test
	public void testUserWritingReading(){
		
		User user = manager.getUser("toto");
		assertNotNull(user);
		assertEquals("toto", user.getLogin());
		assertEquals("dupont", user.getLastName());
		assertEquals("bruno", user.getFirstName());
	}
	
	@Test
	public void testUpdateUser(){
		
		
		//on change l'email
		user.setEmail("a@a.fr");
		manager.UpdateUser(user);
		user = manager.getUser("toto");
		assertEquals("a@a.fr", user.getEmail());
	}
	
	@Test
	public void testRemoveUser(){
		
		manager.removeUser(user);
		user = manager.getUser("toto");
		assertNull(user);
	}
	
	@Test
	public void testAddRole(Role role){
		
		int count = user.getRoles().size();
		user.addRole(role);
		assertTrue(count+1 == user.getRoles().size());
	}
	
	@Test
	public void testGetConferences(){
		//List<Conference> list = user.getConferences();
		//assertNotNull(list);
		
	}
}
