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.Product;

/**
 * This class intend to test functionalities related to products.
 * 
 * @author patapouf
 * 
 */
public class ProductManagerTest extends ManagerTestCase {

	@Test
	public void AddProduct() {
		Product product = new Product();
		try {
			managers.getProductManager().add(Arrays.asList(product));
		} catch (ManagerException e) {
			fail("Error adding object", e);
		}
	}

	@Test
	public void Remove() {
		Product p1 = new Product();
		try {
			managers.getProductManager().add(Arrays.asList(p1));
		} catch (ManagerException e) {
			fail("Error adding object", e);
		}

		int id = p1.getId();

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

		try {
			List<Product> list = managers.getProductManager().list();

			for (Product e : list) {
				if (e.getId() == id) {
					Assert.fail("Object still exists");
				}
			}

		} catch (ManagerException e) {
			fail("Error listing object", e);
		}
	}

	@Test
	public void Update() {
		Product p1 = new Product();
		try {
			managers.getProductManager().add(Arrays.asList(p1));
		} catch (ManagerException e) {
			fail("Error adding object", e);
		}

		p1.setName("test1");
		p1.setRefId("refid1");

		// Save modification
		try {
			managers.getProductManager().update(Arrays.asList(p1));
		} catch (ManagerException e) {
			fail("Error updating object", e);
		}

		// Reload product
		Product p2;
		try {
			p2 = managers.getProductManager().get(p1.getId());
		} catch (ManagerException e) {
			fail("Error loading object", e);
			return;
		}

		Assert.assertEquals("Objects not equals", p1.getName(), p2.getName());
		Assert.assertEquals("Objects not equals", p1.getRefId(), p2.getRefId());
	}

}
