package de.tuberlin.dima.schubotz.wiki;

import eu.stratosphere.api.common.Plan;
import eu.stratosphere.api.java.ExecutionEnvironment;
import eu.stratosphere.client.LocalExecutor;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.io.*;
import java.net.URI;
import java.util.Arrays;
import java.util.Collection;

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


@RunWith(Parameterized.class)
public class WikiMainIT {
	private Integer numWiki;
	private String inputQuery;
    private String inputLatexWikiMap;
    private String inputTupleWikiMap;
	private String outputDir;
	private Boolean debug;

	@Parameterized.Parameters
	public static Collection<Object[]> inputNumDocs() {
		return Arrays.asList(new Object[][] {
				{new Integer(30040),
                 "/home/jjl4/",
                 "de/tuberlin/dima/schubotz/wiki/wikiTrainingQuery.xml",
                 "de/tuberlin/dima/schubotz/wiki/mappers/wikiTrainingDump.expectedLatex.csv",
                 "de/tuberlin/dima/schubotz/wiki/mappers/wikiTrainingDump.expected.csv",
                 Boolean.valueOf(true)}
		});
	}
	@SuppressWarnings("hiding")
	public WikiMainIT(Integer numWiki,
                      String outputDir,
                      String inputQuery,
                      String inputLatexWikiMap,
                      String inputTupleWikiMap,
                      Boolean debug) {

		this.numWiki = numWiki;
        this.outputDir = outputDir;
		this.inputQuery = inputQuery;
        this.inputLatexWikiMap = inputLatexWikiMap;
        this.inputTupleWikiMap = inputTupleWikiMap;
		this.debug = debug;
	}

	@Test
	public void TestLocalExecution() throws Exception {
		String wikiOutput = outputDir + "wikiProgramOutput.csv";
		try {
			String wikiQueryInput = getClass().getClassLoader().getResource(inputQuery).getPath();
            String wikiLatexMapInput = getClass().getClassLoader().getResource(inputLatexWikiMap).getPath();
            String wikiTupleMapInput = getClass().getClassLoader().getResource(inputTupleWikiMap).getPath();
			WikiProgram.parseArgs(new String[] {"1",
											  wikiOutput,
											  wikiQueryInput,
											  wikiLatexMapInput,
                                              wikiTupleMapInput,
											  String.valueOf(numWiki),
											  debug.toString()
			});
			
		} catch (Exception e) {
			e.printStackTrace();
			fail("Missing input files IO Exception");
			return;
		}
		
		try {
			WikiProgram.ConfigurePlan();
	        ExecutionEnvironment env = WikiProgram.getExecutionEnvironment();
	        Plan plan = env.createProgramPlan();
	        LocalExecutor.execute(plan);
		} catch (Exception e) {
			e.printStackTrace();
			fail("execution failed");
			return;
		}
		
		//Check to make sure correct file output
		BufferedReader br = null; 
		try {
			br = new BufferedReader(new FileReader(new File(new URI(wikiOutput).getPath())));
			assertEquals(Boolean.valueOf(br.readLine() != null), true);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			fail("Files not outputted or given directory is incorrect.");
			return;
		} catch (IOException e) {
			e.printStackTrace();
			fail("IOException for output file");
			return;
		} finally {
			br.close();
		}
	}


}
@@ -656,21 +656,18 @@
 g output
-Dir
 ;%0A
+
 %09private
@@ -857,16 +857,37 @@
 me/jjl4/
+wikiProgramOutput.csv
 %22,%0A     
@@ -1286,19 +1286,16 @@
 g output
-Dir
 ,%0A      
@@ -1510,19 +1510,16 @@
 s.output
-Dir
  = outpu
@@ -1519,19 +1519,16 @@
 = output
-Dir
 ;%0A%09%09this
@@ -1747,67 +1747,8 @@
 n %7B%0A
-%09%09String wikiOutput = outputDir + %22wikiProgramOutput.csv%22;%0A
 %09%09tr
@@ -2116,21 +2116,17 @@
 %09%09%09%09%09%09  
-wikiO
+o
 utput,%0A%09
@@ -2881,13 +2881,9 @@
 URI(
-wikiO
+o
 utpu

refactor parameters in wikimainIT