package cl.fatman.capital.fund;

import static org.hamcrest.Matchers.*;

import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.time.LocalDate;
import java.time.Month;
import java.util.List;
import java.util.ArrayList;

public class PersistenceDataTest {
	
	private PersistenceData persistence;
	private List<FundType> ftList;
	private List<Fund> fList;
	private List<FundRate> frList;
	
	@Before
	public void setUp() {
		ftList = new ArrayList<FundType>();
		fList = new ArrayList<Fund>();
		frList = new ArrayList<FundRate>();
		LocalDate fDate = LocalDate.now();
		persistence = PersistenceData.getInstance();
		persistence.setUp();
		FundType type = new FundType(1, "Deuda < 90 días");
		ftList.add(type);
		for (int i = 1; i <= 10; i++) {
			Fund temp = new Fund("Fake " + i, "0-k", "s-"+i, "VTR", type);
			fList.add(temp);
		}
		for (Fund fund : fList) {
			FundRate ft = new FundRate(0.003, fDate, fund);
			frList.add(ft);
		}
	}
	
	@After
	public void tearDown() {
		persistence.tearDown();
	}
	
	@Test
	public void createFundTypeTest() {
		persistence.insertObjectList(ftList);
		List<?> rList = persistence.selectAllObjects("from FundType", FundType.class);
		assertEquals("Stored list size should be equal to received list size", ftList.size(), rList.size());
	}
	
	@Test
	public void createFundTest() {
		persistence.insertObjectList(ftList);
		persistence.insertObjectList(fList);
		List<?> rList = persistence.selectAllObjects("from Fund", Fund.class);
		assertEquals("Stored list size should be equal to received list size", fList.size(), rList.size());
	}
	
	@Test
	public void createFundRateTest() {
		persistence.insertObjectList(ftList);
		persistence.insertObjectList(fList);
		persistence.insertObjectList(frList);
		List<?> rList = persistence.selectAllObjects("from FundRate", FundRate.class);
		assertEquals("Stored list size should be equal to received list size", frList.size(), rList.size());
	}
	
	@Test
	public void duplicationFailTest() {
		persistence.insertObjectList(ftList);
		List<Fund> tempList = new ArrayList<Fund>();
		for (int i = 0; i < 9; i++) {
			Fund temp = new Fund("Arcano", "0-k", "UNICO", "VTR", ftList.get(0));
			tempList.add(temp);
		}
		persistence.insertObjectList(tempList);
		List<?> rList = persistence.selectAllObjects("from Fund", Fund.class);
		assertEquals("Received list should be empty.", 0, rList.size());
	}
	
	@Test
	public void getFundByTypeTest() {
		persistence.insertObjectList(ftList);
		persistence.insertObjectList(fList);
		persistence.insertObjectList(frList);
		FundType type = ftList.get(0);
		List<?> rList = persistence.getFundByType(type);
		assertThat("Fund list should be greater than 9.", rList.size(), greaterThan(9));
	}
	
	@Test
	public void getUpdateDateTest() {
		List<FundRate> tmpList = new ArrayList<FundRate>();
		persistence.insertObjectList(ftList);
		persistence.insertObjectList(fList);
		Fund fund = fList.get(0);
		LocalDate fDate = LocalDate.of(LocalDate.now().getYear(), Month.DECEMBER, 31);
		for (int i = 1; i <= 10; i++) {
			fDate = fDate.plusDays(1);
			FundRate fr = new FundRate(0.003, fDate, fund);
			tmpList.add(fr);
		}
		persistence.insertObjectList(tmpList);
		List<?> rList = persistence.getUpdateDate();
		LocalDate tmpDate = (LocalDate) rList.get(0);
		assertTrue("The last store date should be equal to the received date.", tmpDate.isEqual(fDate));
	}
}
@@ -1062,32 +1062,618 @@
 ();%0A%09%7D%0A%09%0A%09@Test%0A
+%09public void createFomentUnitTest() %7B%0A%09%09List%3CFomentUnit%3E ufList = new ArrayList%3CFomentUnit%3E();%0A%09%09LocalDate ufDate = LocalDate.of(LocalDate.now().getYear(), Month.DECEMBER, 31);%0A%09%09for (int i = 1; i %3C= 10; i++) %7B%0A%09%09%09double value = 3.1415 * i;%0A%09%09%09ufDate = ufDate.plusDays(1);%0A%09%09%09FomentUnit uf = new FomentUnit(value, ufDate);%0A%09%09%09ufList.add(uf);%0A%09%09%7D%0A%09%09persistence.insertObjectList(ufList);%0A%09%09List%3C?%3E rList = persistence.selectAllObjects(%22from FomentUnit%22, FomentUnit.class);%0A%09%09assertEquals(%22Stored list size should be equal to received list size%22, ufList.size(), rList.size());%0A%09%7D%0A%09%0A%09@Test%0A
 %09public void cre

Add junit to test the FomentUnit store procedure.