package org.aegee.runanddine.registration;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.util.List;
import java.util.UUID;

import org.aegee.runanddine.AbstractDbEnabledTest;
import org.aegee.runanddine.util.data.ModelNotExistingException;
import org.junit.Test;

public class GroupRegistrationManagerTest extends AbstractDbEnabledTest
{
	/**
	 * Test of getInstance()
	 * 
	 * @throws Exception
	 */
	@Test
	public void testGetInstance() throws Exception
	{
		GroupRegistrationManager grm1 = GroupRegistrationManager.getInstance();
		GroupRegistrationManager grm2 = GroupRegistrationManager.getInstance();
		assertNotNull(grm1);
		assertTrue(grm1 == grm2);
	}

	/**
	 * Test of getById() with id for existing entry
	 */
	@Test
	public void testGetByIdExisting() throws ModelNotExistingException
	{
		GroupRegistrationManager grm = GroupRegistrationManager.getInstance();
		GroupRegistration gr = new GroupRegistration();
		gr.setValidationToken1(UUID.randomUUID().toString()); // has to be set
		gr.setValidationToken2(UUID.randomUUID().toString()); // has to be set
		grm.save(gr);
		grm.getById(gr.getId());
		grm.delete(gr);
	}

	/**
	 * Test getById() with id for existing entry
	 */
	@Test(expected = ModelNotExistingException.class)
	public void testGetByIdNotExisting() throws ModelNotExistingException
	{
		int id = 1;
		GroupRegistrationManager grm = GroupRegistrationManager.getInstance();
		GroupRegistration gr = grm.getById(id);
	}

	/**
	 * Test getByValidationToken with token for existing entry
	 */
	@Test
	public void testGetByValidationTokenExisting() throws ModelNotExistingException
	{
		GroupRegistrationManager grm = GroupRegistrationManager.getInstance();
		GroupRegistration gr = new GroupRegistration();
		gr.setValidationToken1(UUID.randomUUID().toString());
		gr.setValidationToken2(UUID.randomUUID().toString()); // has to be set
		grm.save(gr);
		grm.getByValidationToken(gr.getValidationToken1());
		grm.getByValidationToken(gr.getValidationToken2());
		grm.delete(gr);
	}

	/**
	 * Test getByValidationToken with token for not existing entry
	 */
	@Test(expected = ModelNotExistingException.class)
	public void testGetByValidationTokenNotExisting() throws ModelNotExistingException
	{
		String token = "foobar";
		GroupRegistrationManager grm = GroupRegistrationManager.getInstance();
		grm.getByValidationToken(token);
	}

	/**
	 * Test getAll with existing models
	 */
	@Test
	public void testGetAllExisting() throws ModelNotExistingException
	{
		GroupRegistrationManager grm = GroupRegistrationManager.getInstance();
		GroupRegistration gr = new GroupRegistration();
		gr.setValidationToken1(UUID.randomUUID().toString()); // has to be set
		gr.setValidationToken2(UUID.randomUUID().toString()); // has to be set
		grm.save(gr);
		List<GroupRegistration> grl = grm.getAll();
		assertNotNull(grl);
		assertTrue(!grl.isEmpty());
		grm.delete(gr);
	}

	/**
	 * Test getAll with no existing models
	 */
	@Test
	public void testGetAllNoneExisting()
	{
		GroupRegistrationManager grm = GroupRegistrationManager.getInstance();
		List<GroupRegistration> grl = grm.getAll();
		assertNotNull(grl);
		assertTrue(grl.isEmpty());
	}

	/**
	 * Test getAllByRunAndDineId() with existing models
	 */
	@Test
	public void testGetAllByRunAndDineIdExisting()
	{
		int runAndDineId = 1;
		GroupRegistrationManager grm = GroupRegistrationManager.getInstance();
		GroupRegistration gr = new GroupRegistration();
		gr.setValidationToken1(UUID.randomUUID().toString()); // has to be set
		gr.setValidationToken2(UUID.randomUUID().toString()); // has to be set
		gr.setRunAndDineId(runAndDineId);
		grm.save(gr);
		List<GroupRegistration> grl = grm.getAllByRunAndDineId(runAndDineId);
		assertNotNull(grl);
		assertTrue(!grl.isEmpty());
		grm.delete(gr);
	}

	/**
	 * Test getAllByRunAndDineId() with no existing models
	 */
	@Test
	public void testGetAllByRunAndDineIdNoneExisting()
	{
		int runAndDineId = 1;
		GroupRegistrationManager grm = GroupRegistrationManager.getInstance();
		List<GroupRegistration> grl = grm.getAllByRunAndDineId(runAndDineId);
		assertNotNull(grl);
		assertTrue(grl.isEmpty());
	}

	/**
	 * Test getAllValidatedByRunAndDineId() with existing models
	 */
	@Test
	public void testGetAllValidatedByRunAndDineIdExisting()
	{
		int runAndDineId = 1;
		GroupRegistrationManager grm = GroupRegistrationManager.getInstance();
		GroupRegistration gr = new GroupRegistration();
		gr.setValidationToken1(UUID.randomUUID().toString()); // has to be set
		gr.setValidationToken2(UUID.randomUUID().toString()); // has to be set
		gr.setIsValidated1(true);
		gr.setIsValidated2(true);
		gr.setRunAndDineId(runAndDineId);
		grm.save(gr);
		List<GroupRegistration> grl = grm.getAllValidatedByRunAndDineId(runAndDineId);
		assertNotNull(grl);
		assertTrue(!grl.isEmpty());
		grm.delete(gr);
	}

	/**
	 * Test getAllValidatedByRunAndDineId() with no existing models
	 */
	@Test
	public void testGetAllValidatedByRunAndDineIdNoneExisting()
	{
		int runAndDineId = 1;
		GroupRegistrationManager grm = GroupRegistrationManager.getInstance();
		List<GroupRegistration> grl = grm.getAllValidatedByRunAndDineId(runAndDineId);
		assertNotNull(grl);
		assertTrue(grl.isEmpty());
	}

	/**
	 * Test save() creating entry
	 */
	@Test
	public void testSaveCreate()
	{
		GroupRegistrationManager grm = GroupRegistrationManager.getInstance();
		GroupRegistration gr = new GroupRegistration();
		gr.setValidationToken1(UUID.randomUUID().toString()); // has to be set
		gr.setValidationToken2(UUID.randomUUID().toString()); // has to be set
		grm.save(gr);
		assertTrue(gr.getId() != 0);
		grm.delete(gr);
	}

	/**
	 * Test save() updating entry
	 */
	@Test
	public void testSaveUpdate() throws ModelNotExistingException
	{
		GroupRegistrationManager grm = GroupRegistrationManager.getInstance();
		GroupRegistration gr = new GroupRegistration();
		gr.setValidationToken1(UUID.randomUUID().toString()); // has to be set
		gr.setValidationToken2(UUID.randomUUID().toString()); // has to be set
		gr.setFirstName1("foo");
		grm.save(gr);
		gr.setFirstName1("bar");
		grm.save(gr);
		assertTrue(grm.getById(gr.getId()).getFirstName1().equals("bar"));
		grm.delete(gr);
	}

	/**
	 * Test delete()
	 */
	@Test(expected = ModelNotExistingException.class)
	public void testDelete() throws ModelNotExistingException
	{
		GroupRegistrationManager grm = GroupRegistrationManager.getInstance();
		GroupRegistration gr = new GroupRegistration();
		gr.setValidationToken1(UUID.randomUUID().toString()); // has to be set
		gr.setValidationToken2(UUID.randomUUID().toString()); // has to be set
		grm.save(gr);
		int id = gr.getId();
		grm.delete(gr);
		grm.getById(id);
	}

	/**
	 * Test newObject()
	 */
	@Test
	public void testNewObject()
	{
		GroupRegistrationManager grm = GroupRegistrationManager.getInstance();
		assertNotNull(grm.newObject());
	}
}
