package cl.fatman.capital.fund;

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

import java.time.LocalDate;
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 selectFundTypeTest() {
		persistence.insertObjectList(ftList);
		FundType rFundType = persistence.selectFundType(1);
		assertNotNull("The fund type received should not be null", rFundType);
	}
	
	@Test
	public void selectFundTest() {
		persistence.insertObjectList(ftList);
		persistence.insertObjectList(fList);
		List<?> rList = persistence.selectAllObjects("from Fund", Fund.class);
		Fund temp = (Fund) rList.get(0);
		Fund rFund = persistence.selectFund(temp.getId());
		assertNotNull("The fund received should not be null", rFund);
	}
	
	@Test
	public void tooManyConnectionsTest() {
		persistence.insertObjectList(ftList);
		persistence.insertObjectList(fList);
		persistence.insertObjectList(frList);
		for (int i = 1; i <= 500; i++) {
			Fund temp = new Fund("Fake " + i, "0-k", "s-"+i, "VTR", ftList.get(0));
			persistence.selectFund(temp.getId());
		}
		List<?> rList = persistence.selectAllObjects("from Fund", Fund.class);
		assertEquals("Stored list size should be equal to received list size", fList.size(), rList.size());
	}
}
@@ -2482,32 +2482,600 @@
 d);%0A%09%7D%0A%09%0A%09@Test%0A
+%09public void duplicationFundTest() %7B%0A%09%09persistence.insertObjectList(ftList);%0A%09%09List%3CFund%3E tempList = new ArrayList%3CFund%3E();%0A%09%09for (int i = 0; i %3C 9; i++) %7B%0A%09%09%09Fund temp = new Fund(%22Arcano%22, %220-k%22, %22UNICO%22, %22VTR%22, ftList.get(0));%0A%09%09%09tempList.add(temp);%0A%09%09%7D%0A%09%09Fund temp = new Fund(%22Arcano%22, %221-k%22, %22A%22, %22VTR%22, ftList.get(0));%0A%09%09tempList.add(temp);%0A%09%09persistence.insertObjectList(tempList);%0A%09%09List%3C?%3E rList = persistence.selectAllObjects(%22from Fund%22, Fund.class);%0A%09%09assertEquals(%22The list size should be 2, duplicate object are not store.%22, 2, rList.size());%0A%09%7D%0A%09%0A%09@Test%0A
 %09public void too

Add a junit to test possible duplication problems.