package org.aegee.runanddine.pathfinder;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.util.List;

import org.aegee.runanddine.AbstractDbEnabledTest;
import org.aegee.runanddine.util.data.ModelNotExistingException;
import org.junit.Test;

public class OptGroupManagerTest extends AbstractDbEnabledTest
{
	/**
	 * Test of getInstance()
	 * 
	 * @throws Exception
	 */
	@Test
	public void testGetInstance() throws Exception
	{
		OptGroupManager ogm1 = OptGroupManager.getInstance();
		OptGroupManager ogm2 = OptGroupManager.getInstance();
		assertNotNull(ogm1);
		assertTrue(ogm1 == ogm2);
	}

	/**
	 * Test of getById() with id for existing entry
	 */
	@Test
	public void testGetByIdExisting() throws Exception
	{
		OptGroupManager ogm = OptGroupManager.getInstance();
		OptGroup og = new OptGroup(null);
		ogm.save(og);
		ogm.getById(og.getId());
		ogm.delete(og);
	}

	/**
	 * Test getById() with id for existing entry
	 */
	@Test(expected = ModelNotExistingException.class)
	public void testGetByIdNotExisting() throws ModelNotExistingException
	{
		int id = 1;
		OptGroupManager ogm = OptGroupManager.getInstance();
		OptGroup ae = ogm.getById(id);
	}

	/**
	 * Test getAll with existing models
	 */
	@Test
	public void testGetAllExisting()
	{
		OptGroupManager ogm = OptGroupManager.getInstance();
		OptGroup og = new OptGroup(null);
		ogm.save(og);
		List<OptGroup> ogl = ogm.getAll();
		assertNotNull(ogl);
		assertTrue(!ogl.isEmpty());
		ogm.delete(og);
	}

	/**
	 * Test getAll with no existing models
	 */
	@Test
	public void testGetAllNoneExisting()
	{
		OptGroupManager ogm = OptGroupManager.getInstance();
		List<OptGroup> ogl = ogm.getAll();
		assertNotNull(ogl);
		assertTrue(ogl.isEmpty());
	}

	/**
	 * Test getAllByRunAndDineId() with existing models
	 */
	@Test
	public void testGetAllByRunAndDineIdExisting()
	{
		int runAndDineId = 1;
		OptGroupManager ogm = OptGroupManager.getInstance();
		OptGroup og = new OptGroup(null);
		og.setRunAndDineId(runAndDineId);
		ogm.save(og);
		List<OptGroup> ogl = ogm.getAllByRunAndDineId(runAndDineId);
		assertNotNull(ogl);
		assertTrue(!ogl.isEmpty());
		ogm.delete(og);
	}

	/**
	 * Test getAllByRunAndDineId() with no existing models
	 */
	@Test
	public void testGetAllByRunAndDineIdNoneExisting()
	{
		int runAndDineId = 1;
		OptGroupManager ogm = OptGroupManager.getInstance();
		List<OptGroup> ogl = ogm.getAllByRunAndDineId(runAndDineId);
		assertNotNull(ogl);
		assertTrue(ogl.isEmpty());
	}

	/**
	 * Test save() creating entry
	 */
	@Test
	public void testSaveCreate()
	{
		OptGroupManager ogm = OptGroupManager.getInstance();
		OptGroup og = new OptGroup(null);
		ogm.save(og);
		assertTrue(og.getId() != 0);
		ogm.delete(og);
	}

	/**
	 * Test save() updating entry
	 */
	@Test
	public void testSaveUpdate() throws ModelNotExistingException
	{
		OptGroupManager ogm = OptGroupManager.getInstance();
		OptGroup og = new OptGroup(null);
		og.setRunAndDineId(1);
		ogm.save(og);
		og.setRunAndDineId(2);
		ogm.save(og);
		assertEquals(ogm.getById(og.getId()).getRunAndDineId(), 2);
		ogm.delete(og);
	}

	/**
	 * Test delete()
	 */
	@Test(expected = ModelNotExistingException.class)
	public void testDelete() throws ModelNotExistingException
	{
		OptGroupManager ogm = OptGroupManager.getInstance();
		OptGroup og = new OptGroup(null);
		ogm.save(og);
		int id = og.getId();
		ogm.delete(og);
		ogm.getById(id);
	}

	/**
	 * Test newObject()
	 */
	@Test(expected = UnsupportedOperationException.class)
	public void testNewObject()
	{
		OptGroupManager ogm = OptGroupManager.getInstance();
		ogm.newObject();
	}
}
