package org.yacos.tests.core;

import static org.junit.Assert.*;

import java.util.Date;
import java.util.List;

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

import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.yacos.core.conferences.Conference;
import org.yacos.core.conferences.IConferenceManager;
import org.yacos.core.exceptions.PKAlreadyUsedException;
import org.yacos.core.users.IUserManager;
import org.yacos.core.users.Role;
import org.yacos.core.users.User;


public class ConferenceManagerTest {
	static IConferenceManager cm;
	static IUserManager um;
	int id;
	Conference conf;
	
	@BeforeClass
	public static void setUpManager() throws NamingException, PKAlreadyUsedException {
		Context context = new InitialContext();
		cm = (IConferenceManager) context.lookup("ConferenceManagerBean/remote");
		um = (IUserManager) context.lookup("UserManagerBean/remote");
		
		if(!um.exists("max")){
			um.addUser("max","pw","maxime","dagn","ipint", "e@e.fr");
		}
	}
	
	@Before
	public void setUpConferences() {
		conf = cm.addConference("titre","desc conf","info en plus",new Date(),new Date(),new Date(),new Date(),new Date());
		id = conf.getId();
	}
	
	@After
	public void unSetUpConferences(){
		conf = cm.getConference(id);
		if (conf != null){
			cm.remove(conf);
		}
	}
	
	@Test
	public void getConferenceTest(){
		conf = cm.getConference(id);
		assertNotNull(conf);
	}
	
	@Test
	public void updateConferenceTest(){
		conf = cm.getConference(id);
		conf.setTitle("autre titre");
		cm.update(conf);
		conf = cm.getConference(id);
		assertEquals("autre titre", conf.getTitle());
	}
	
	public void removeConferenceTest(){
		conf = cm.getConference(id);
		cm.remove(conf);
	}
	
	@Test
	public void getConferencesTest(){
		conf = cm.addConference("titre2","desc conf","info en plus",new Date(),new Date(),new Date(),new Date(),new Date());
		List<Conference> list = cm.getConferences();
		assertFalse("liste vide",list.isEmpty());
		
	}
	
	
	@Test
	public void addRoleForUserTest(){
		User user = um.getUser("max");
		cm.addRole(Role.RoleType.AUTHOR, user , conf);	
	}
	
		
	@Test
	public void getRolesTest(){
		User user = um.getUser("max");
		List<Role> list = cm.getRoles(user,conf);
		assertFalse("liste vide",list.isEmpty());
		Role role = list.get(0);
		System.out.println(role.getType());
		assertNotNull(role);	
	}
	
	@Test
	public void getConferencesForUserTest(){
		User user = um.getUser("max");
		List<Conference> list = cm.getConferences(user);
		conf = list.get(0);
		assertNotNull(conf);
	}

	@Test
	public void getRolesForUserConfTest(){
		User user = um.getUser("max");
		List<Role> list = cm.getRoles(user, conf);
		assertFalse("liste vide",list.isEmpty());
		Role role = list.get(0);
		assertNotNull(role);	
	}
		
	
}
