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 SingleRegistrationManagerTest extends AbstractDbEnabledTest
{
	/**
	 * Test of getInstance()
	 * 
	 * @throws Exception
	 */
	@Test
	public void testGetInstance() throws Exception
	{
		SingleRegistrationManager srm1 = SingleRegistrationManager.getInstance();
		SingleRegistrationManager srm2 = SingleRegistrationManager.getInstance();
		assertNotNull(srm1);
		assertTrue(srm1 == srm2);
	}

	/**
	 * Test of getById() with id for existing entry
	 */
	@Test
	public void testGetByIdExisting() throws Exception
	{
		SingleRegistrationManager srm = SingleRegistrationManager.getInstance();
		SingleRegistration sr = new SingleRegistration();
		sr.setValidationToken(UUID.randomUUID().toString()); // has to be set
		srm.save(sr);
		srm.getById(sr.getId());
		srm.delete(sr);
	}

	/**
	 * Test getById() with id for existing entry
	 */
	@Test(expected = ModelNotExistingException.class)
	public void testGetByIdNotExisting() throws ModelNotExistingException
	{
		int id = 1;
		SingleRegistrationManager srm = SingleRegistrationManager.getInstance();
		SingleRegistration sr = srm.getById(id);
	}

	/**
	 * Test getByValidationToken with token for existing entry
	 */
	@Test
	public void testGetByValidationTokenExisting() throws ModelNotExistingException
	{
		SingleRegistrationManager srm = SingleRegistrationManager.getInstance();
		SingleRegistration sr = new SingleRegistration();
		sr.setValidationToken(UUID.randomUUID().toString());
		srm.save(sr);
		srm.getByValidationToken(sr.getValidationToken());
		srm.delete(sr);
	}

	/**
	 * Test getByValidationToken with token for not existing entry
	 */
	@Test(expected = ModelNotExistingException.class)
	public void testGetByValidationTokenNotExisting() throws ModelNotExistingException
	{
		String token = "foobar";
		SingleRegistrationManager srm = SingleRegistrationManager.getInstance();
		srm.getByValidationToken(token);
	}

	/**
	 * Test getAll with existing models
	 */
	@Test
	public void testGetAllExisting()
	{
		SingleRegistrationManager srm = SingleRegistrationManager.getInstance();
		SingleRegistration sr = new SingleRegistration();
		sr.setValidationToken(UUID.randomUUID().toString()); // has to be set
		srm.save(sr);
		List<SingleRegistration> srl = srm.getAll();
		assertNotNull(srl);
		assertTrue(!srl.isEmpty());
		srm.delete(sr);
	}

	/**
	 * Test getAll with no existing models
	 */
	@Test
	public void testGetAllNoneExisting()
	{
		SingleRegistrationManager srm = SingleRegistrationManager.getInstance();
		List<SingleRegistration> srl = srm.getAll();
		assertNotNull(srl);
		assertTrue(srl.isEmpty());
	}

	/**
	 * Test getAllByRunAndDineId() with existing models
	 */
	@Test
	public void testGetAllByRunAndDineIdExisting()
	{
		int runAndDineId = 1;
		SingleRegistrationManager srm = SingleRegistrationManager.getInstance();
		SingleRegistration sr = new SingleRegistration();
		sr.setValidationToken(UUID.randomUUID().toString()); // has to be set
		sr.setRunAndDineId(runAndDineId);
		srm.save(sr);
		List<SingleRegistration> srl = srm.getAllByRunAndDineId(runAndDineId);
		assertNotNull(srl);
		assertTrue(!srl.isEmpty());
		srm.delete(sr);
	}

	/**
	 * Test getAllByRunAndDineId() with no existing models
	 */
	@Test
	public void testGetAllByRunAndDineIdNoneExisting()
	{
		int runAndDineId = 1;
		SingleRegistrationManager srm = SingleRegistrationManager.getInstance();
		List<SingleRegistration> srl = srm.getAllByRunAndDineId(runAndDineId);
		assertNotNull(srl);
		assertTrue(srl.isEmpty());
	}

	/**
	 * Test getAllValidatedByRunAndDineId() with existing models
	 */
	@Test
	public void testGetAllValidatedByRunAndDineIdExisting()
	{
		int runAndDineId = 1;
		SingleRegistrationManager srm = SingleRegistrationManager.getInstance();
		SingleRegistration sr = new SingleRegistration();
		sr.setValidationToken(UUID.randomUUID().toString()); // has to be set
		sr.setIsValidated(true);
		sr.setRunAndDineId(runAndDineId);
		srm.save(sr);
		List<SingleRegistration> srl = srm.getAllValidatedByRunAndDineId(runAndDineId);
		assertNotNull(srl);
		assertTrue(!srl.isEmpty());
		srm.delete(sr);
	}

	/**
	 * Test getAllValidatedByRunAndDineId() with no existing models
	 */
	@Test
	public void testGetAllValidatedByRunAndDineIdNoneExisting()
	{
		int runAndDineId = 1;
		SingleRegistrationManager srm = SingleRegistrationManager.getInstance();
		List<SingleRegistration> srl = srm.getAllValidatedByRunAndDineId(runAndDineId);
		assertNotNull(srl);
		assertTrue(srl.isEmpty());
	}

	/**
	 * Test save() creating entry
	 */
	@Test
	public void testSaveCreate()
	{
		SingleRegistrationManager srm = SingleRegistrationManager.getInstance();
		SingleRegistration sr = new SingleRegistration();
		sr.setValidationToken(UUID.randomUUID().toString()); // has to be set
		srm.save(sr);
		assertTrue(sr.getId() != 0);
		srm.delete(sr);
	}

	/**
	 * Test save() updating entry
	 */
	@Test
	public void testSaveUpdate() throws ModelNotExistingException
	{
		SingleRegistrationManager srm = SingleRegistrationManager.getInstance();
		SingleRegistration sr = new SingleRegistration();
		sr.setValidationToken(UUID.randomUUID().toString()); // has to be set
		sr.setFirstName("foo");
		srm.save(sr);
		sr.setFirstName("bar");
		srm.save(sr);
		assertTrue(srm.getById(sr.getId()).getFirstName().equals("bar"));
		srm.delete(sr);
	}

	/**
	 * Test delete()
	 */
	@Test(expected = ModelNotExistingException.class)
	public void testDelete() throws ModelNotExistingException
	{
		SingleRegistrationManager srm = SingleRegistrationManager.getInstance();
		SingleRegistration sr = new SingleRegistration();
		sr.setValidationToken(UUID.randomUUID().toString()); // has to be set
		srm.save(sr);
		int id = sr.getId();
		srm.delete(sr);
		srm.getById(id);
	}

	/**
	 * Test newObject()
	 */
	@Test
	public void testNewObject()
	{
		SingleRegistrationManager srm = SingleRegistrationManager.getInstance();
		assertNotNull(srm.newObject());
	}
}
