package com.planimod.core;

import java.util.Arrays;
import java.util.List;

import org.junit.Assert;
import org.junit.Test;

import com.patrikdufresne.managers.ManagerException;
import com.planimod.core.Section;

/**
 * This test case cover all operation done using the section.
 * 
 * @author patapouf
 * 
 */
public class SectionManagerTest extends ManagerTestCase {
	/**
	 * Add the object.
	 */
	@Test
	public void AddSection() {
		Section section = new Section();
		try {
			managers.getSectionManager().add(Arrays.asList(section));
		} catch (ManagerException e) {
			fail("Error adding Section", e);
		}
	}

	/**
	 * Update the object.
	 */
	@Test
	public void UpdateSection() {
		Section section = new Section();
		try {
			managers.getSectionManager().add(Arrays.asList(section));
		} catch (ManagerException e) {
			fail("Error adding object", e);
		}
		section.setName("Section 1");

		try {
			managers.getSectionManager().update(Arrays.asList(section));
		} catch (ManagerException e) {
			fail("Error updating object", e);
		}
	}

	/**
	 * Create and then remove the object.
	 */
	@Test
	public void RemoveSection() {
		Section section = new Section();
		try {
			managers.getSectionManager().add(Arrays.asList(section));
		} catch (ManagerException e) {
			fail("Error adding object", e);
		}

		int id = section.getId();

		try {
			managers.getSectionManager().remove(Arrays.asList(section));
		} catch (ManagerException e) {
			fail("Error removing object", e);
		}

		try {
			List<Section> list = managers.getSectionManager().list();
			for (Section e : list) {
				if (e.getId() == id) {
					Assert.fail("Object still exists");
				}
			}
		} catch (ManagerException e) {
			fail("Error listing object", e);
		}

	}
}
