package net.canadensys.processor.geography;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;

import java.io.File;
import java.net.URISyntaxException;

import net.canadensys.FileBasedTest;

import org.geotools.referencing.CRS;
import org.junit.Test;
import org.opengis.referencing.crs.CoordinateReferenceSystem;

/**
 * Test for the CoordinatesToWGS84Processor
 * @author canadensys
 *
 */
public class CoordinatesToWGS84ProcesorTest {
	
	private static final String TEST_FILE_NAME = "/CoordinatesToWGS84.txt";
	
	@Test
	public void testProcessor(){
		final CoordinatesToWGS84Processor ctwProcessor = new CoordinatesToWGS84Processor();
		//test supported syntax (from the test file)
		try {
			final File dateFile = new File(getClass().getResource(TEST_FILE_NAME).toURI());
			FileBasedTest fileBasedTest = new FileBasedTest(dateFile) {
				Double[] output = null;
				Double lat, lng, delta;
				CoordinateReferenceSystem crs;
				@Override
				public void processLine(String[] elements, int lineNumber) {
					if(elements.length == 6){
						try {
							crs = CRS.decode(elements[0]);
						} catch (Exception e) {
							fail("[Line #" + lineNumber + " in " + dateFile.getName()+"] is not valid. Invalid CRS: " + elements[0]);
						}
						output = ctwProcessor.process(Double.parseDouble(elements[1]), Double.parseDouble(elements[2]), crs, null);
						lat = Double.parseDouble(elements[3]);
						lng = Double.parseDouble(elements[4]);
						delta = Double.parseDouble(elements[5]);
						String assertText = "[Line #" + lineNumber + " in " + dateFile.getName()+"]";
						assertNotNull(assertText, output);
						assertEquals(assertText, lat, output[LatLongProcessorHelper.LATITUDE_IDX], delta);
						assertEquals(assertText, lng, output[LatLongProcessorHelper.LONGITUDE_IDX], delta);
					}
					else{
						fail("[Line #" + lineNumber + " in " + dateFile.getName()+"] is not valid.");
					}
				}
			};
			
			fileBasedTest.processFile();
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}
	}

}

@@ -129,16 +129,102 @@
 otNull;%0A
+import static org.junit.Assert.assertNull;%0Aimport static org.junit.Assert.assertTrue;%0A
 import s
@@ -348,16 +348,111 @@
 edTest;%0A
+import net.canadensys.processor.ProcessingResult;%0A%0Aimport org.apache.commons.lang3.StringUtils;
 %0Aimport 
@@ -692,16 +692,65 @@
 est %7B%0A%09%0A
+%09private static final int TEST_FILE_COLUMNS = 7;%0A
 %09private
@@ -813,16 +813,423 @@
 84.txt%22;
+%0A%0A%09@Test%0A%09public void testWithNoCRS()%7B%0A%09%09CoordinatesToWGS84Processor ctwProcessor = new CoordinatesToWGS84Processor();%0A%09%09ProcessingResult pr = new ProcessingResult();%0A%09%09Double%5B%5D output = ctwProcessor.process(1d, 2d, null, pr);%0A%09%09assertNull(output%5BLatLongProcessorHelper.LATITUDE_IDX%5D);%0A%09%09assertNull(output%5BLatLongProcessorHelper.LONGITUDE_IDX%5D);%0A%09%09assertTrue(StringUtils.isNotBlank(pr.getErrorString()));%0A%09%7D
 %0A%09%0A%09@Tes
@@ -1748,9 +1748,25 @@
  == 
-6
+TEST_FILE_COLUMNS
 )%7B%0A%09
@@ -2743,11 +2743,10 @@
 %0A%09%09%7D%0A%09%7D%0A
-%0A
 %7D%0A

fix broken test