package com.taxonic.carml.logical_source_resolver;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;

import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.taxonic.carml.engine.EvaluateExpression;
import com.taxonic.carml.logical_source_resolver.LogicalSourceResolver.ExpressionEvaluatorFactory;
import com.univocity.parsers.common.record.Record;
import java.util.List;
import org.junit.Test;
import org.mockito.Mock;

public class CsvResolverTest {
	
	private static final String SOURCE = 
			"Year,Make,Model,Description,Price\r\n" + 
			"1997,Ford,E350,\"ac, abs, moon\",3000.00\r\n" + 
			"1999,Chevy,\"Venture \"\"Extended Edition\"\"\",\"\",4900.00" ;
	
	private static final String SOURCE_DELIM = 
			"Year^Make^Model^Description^Price\r\n" + 
			"1997^Ford^E350^\"ac, abs, moon\"^3000.00\r\n" + 
			"1999^Chevy^\"Venture \"\"Extended Edition\"\"\"^\"\"^4900.00" ;
	
	@Mock
	CsvResolver csvResolver = new CsvResolver();
	
	@Test
	public void sourceIterator_givenCsv_shoulReturnAllRecords() {	
		Iterable<Record> recordIterator = csvResolver.bindSource(SOURCE, null).get();
		assertThat(Iterables.size(recordIterator), is(2));
	}
	
	@Test
	public void sourceIterator_givenRandomDelimitedCsv_shoulReturnAllCorrectRecords() {
		Iterable<Record> recordIterator = csvResolver.bindSource(SOURCE_DELIM, null).get();
		List<Record> records = Lists.newArrayList(recordIterator);
		assertThat(records.size(), is(2));
		assertThat(records.get(0).getValues().length, is(5));
		records.forEach(System.out::println);
	}
	
	@Test
	public void expressionEvaluator_givenExpression_shoulReturnCorrectValue() {
		String expression = "Year";
		Iterable<Record> recordIterator = csvResolver.bindSource(SOURCE, null).get();
		ExpressionEvaluatorFactory<Record> evaluatorFactory = 
				csvResolver.getExpressionEvaluatorFactory();
		
		List<Record> records = Lists.newArrayList(recordIterator);
		EvaluateExpression evaluateExpression = evaluatorFactory.apply(records.get(0));
		assertThat(evaluateExpression.apply(expression).get(), is("1997"));
	}
	
}

@@ -465,20 +465,22 @@
 g.junit.
-Test
+Before
 ;%0D%0Aimpor
@@ -489,20 +489,18 @@
 org.
-mockito.Mock
+junit.Test
 ;%0D%0A%0D
@@ -972,28 +972,80 @@
 %09%0D%0A%09
-@Mock%0D%0A%09CsvResolver 
+private CsvResolver csvResolver;%0D%0A%09%0D%0A%09@Before%0D%0A%09public void init() %7B%0D%0A%09%09
 csvR
@@ -1074,16 +1074,20 @@
 ver();%0D%0A
+%09%7D%0D%0A
 %09%0D%0A%09@Tes
@@ -1573,24 +1573,24 @@
 ), is(2));%0D%0A
+
 %09%09assertThat
@@ -1638,49 +1638,8 @@
 );%0D%0A
-%09%09records.forEach(System.out::println);%0D%0A
 %09%7D%0D%0A

clean up CsvResolverTest