package eu.dnetlib.r2d2.neo4j;

import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.neo4j.graphdb.Transaction;

import eu.dnetlib.r2d2.neo4j.dao.GroupDao;
import eu.dnetlib.r2d2.neo4j.dao.ProfileDao;
import eu.dnetlib.r2d2.neo4j.domain.Neo4jGroup;
import eu.dnetlib.r2d2.neo4j.domain.Neo4jProfile;
import eu.dnetlib.r2d2.neo4j.domain.Neo4jReadingList;

public class TestNeo extends BaseTestCase {
	
	private static Neo4jProfile profile = null;
	private static String profileId = null;
	private static Neo4jGroup group = null;
	private static String groupId = null;
	
	@BeforeClass
	public static void setup() {
		Logger rootLogger = Logger.getRootLogger();
		if (!rootLogger.getAllAppenders().hasMoreElements()) {
			rootLogger.setLevel(Level.INFO);
			rootLogger.addAppender(new ConsoleAppender(new PatternLayout(PatternLayout.TTCC_CONVERSION_PATTERN)));

			Logger pkgLogger = rootLogger.getLoggerRepository().getLogger(
					"eu.dnetlib");
			pkgLogger.setLevel(Level.DEBUG);
		}
	}
	
	private Transaction tx = null;
	
	@Before
	public void before() {
		tx = graphdb.beginTx();
	}
	
	@After
	public void after() {
		tx.success();
		tx.finish();
	}
	
	@Test
	public void testCreate() {
		profile = profileDao.newBean();
		
		profile.setAvatarUrl("http://www.thewiplist.com/photos/G/13796672_ga.jpg");
		profile.setName("Georgios Amanatidis");
		
		profileId = profile.getId();
		
		assertNotNull(profileId);
	}

	@Test
	public void testGetById() throws Exception {
		assertNotNull(profileId);

		profile = profileDao.getBean(profileId);
		
		assertNotNull(profile);
		assertEquals(profile.getId(), profileId);
		assertEquals(profile.getName(), "Georgios Amanatidis");
	}
	
	@Test
	public void testEdit() {
		profile = profileDao.getBean(profileId);
		assertNotNull(profile);
		assertEquals(profile.getId(), profileId);
		assertEquals(profile.getName(), "Georgios Amanatidis");
		
		profile.setName("Karataidis Gewrgios");
		
		profile = profileDao.getBean(profileId);
		
		assertNotNull(profile);
		assertEquals(profile.getName(), "Karataidis Gewrgios");
	}	
	
	@Test
	public void testCreateGroup() {
		group = groupDao.newBean();
		
		group.setName("Coolers");
		group.setDescription("The cool group of cool users");
		group.setAvatarUrl("http://blogs.targetx.com/pbu/Sam/SM131~Mom-Says-I-m-Cool-Posters.jpg");
		
		groupId = group.getId();
		
		assertNotNull(groupId);
	}
	
	@Test
	public void testAddUserToGroup() {
		groupDao.addUserToGroup(groupId, profileId);
		
		Iterable<Neo4jGroup> groups = groupDao.getUserGroups(profileId);
		
		assertNotNull(groups);
		assertTrue(groups.iterator().hasNext());
		assertEquals(groupId, groups.iterator().next().getId());
		assertFalse(groups.iterator().hasNext());
	}
	
	@Test
	public void testRemoveUserFromGroup() {
		groupDao.removeUserFromGroup(groupId, profileId);
		
		Iterable<Neo4jGroup> groups = groupDao.getUserGroups(profileId);
		
		assertNotNull(groups);
		assertFalse(groups.iterator().hasNext());
	}
	
	@Test
	public void testNewReadingList() {
		Neo4jReadingList rl = rlDao.newBean();
		
		rl.setAvatarUrl("234324534");
		rl.setDescription("I rock. You?");
		rl.setName("Rocking rockers");
		
		String rlId = rl.getId();
		
		assertNotNull(rlId);
	}

	public ProfileDao getUserManager() {
		return profileDao;
	}

	public void setUserManager(ProfileDao profileDao) {
		this.profileDao = profileDao;
	}

	public GroupDao getGroupManager() {
		return groupDao;
	}

	public void setGroupManager(GroupDao groupDao) {
		this.groupDao = groupDao;
	}
}