package com.nflabs.zeppelin.spark;

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

import java.util.Properties;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.nflabs.zeppelin.interpreter.InterpreterContext;
import com.nflabs.zeppelin.interpreter.InterpreterResult;
import com.nflabs.zeppelin.interpreter.InterpreterResult.Code;
import com.nflabs.zeppelin.notebook.Paragraph;


public class SparkInterpreterTest {
	public static SparkInterpreter repl;
  private InterpreterContext context;

	@Before
	public void setUp() throws Exception {
	  if (repl == null) {
		  Properties p = new Properties();

	    repl = new SparkInterpreter(p);
  	  repl.open();
	  }

    context = new InterpreterContext(new Paragraph(null, null));
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testBasicIntp() {
		assertEquals(InterpreterResult.Code.SUCCESS, repl.interpret("val a = 1\nval b = 2", context).code());

		// when interpret incomplete expression
		InterpreterResult incomplete = repl.interpret("val a = \"\"\"", context);
		assertEquals(InterpreterResult.Code.INCOMPLETE, incomplete.code());
		assertTrue(incomplete.message().length()>0); // expecting some error message
		/*
		assertEquals(1, repl.getValue("a"));
		assertEquals(2, repl.getValue("b"));
		repl.interpret("val ver = sc.version");
		assertNotNull(repl.getValue("ver"));
		assertEquals("HELLO\n", repl.interpret("println(\"HELLO\")").message());
		*/
	}

	@Test
	public void testEndWithComment() {
		assertEquals(InterpreterResult.Code.SUCCESS, repl.interpret("val c=1\n//comment", context).code());
	}

	@Test
	public void testSparkSql(){
		repl.interpret("case class Person(name:String, age:Int)", context);
		repl.interpret("val people = sc.parallelize(Seq(Person(\"moon\", 33), Person(\"jobs\", 51), Person(\"gates\", 51), Person(\"park\", 34)))", context);
		assertEquals(Code.SUCCESS, repl.interpret("people.take(3)", context).code());

		// create new interpreter
		Properties p = new Properties();
		SparkInterpreter repl2 = new SparkInterpreter(p);
		repl2.open();

		repl.interpret("case class Man(name:String, age:Int)", context);
		repl.interpret("val man = sc.parallelize(Seq(Man(\"moon\", 33), Man(\"jobs\", 51), Man(\"gates\", 51), Man(\"park\", 34)))", context);
		assertEquals(Code.SUCCESS, repl.interpret("man.take(3)", context).code());
		repl2.getSparkContext().stop();
	}

	@Test
	public void testReferencingUndefinedVal(){
		InterpreterResult result = repl.interpret("def category(min: Int) = {" +
				       "    if (0 <= value) \"error\"" +
                       "}", context);
		assertEquals(Code.ERROR, result.code());
		System.out.println("msg="+result.message());
	}
}

@@ -118,16 +118,37 @@
 tTrue;%0A%0A
+import java.io.File;%0A
 import j
@@ -168,16 +168,16 @@
 erties;%0A
-
 %0Aimport 
@@ -582,16 +582,39 @@
 context;
+%0A  private File tmpDir;
 %0A%0A%09@Befo
@@ -656,16 +656,235 @@
 ption %7B%0A
+    tmpDir = new File(System.getProperty(%22java.io.tmpdir%22)+%22/ZeppelinLTest_%22+System.currentTimeMillis());%0A    System.setProperty(%22zeppelin.dep.localrepo%22, tmpDir.getAbsolutePath()+%22/local-repo%22);%0A%0A    tmpDir.mkdirs();%0A%0A
 %09  if (r
@@ -1117,17 +1117,321 @@
 ption %7B%0A
-
 %09
+  delete(tmpDir);%0A%09%7D%0A%0A  private void delete(File file)%7B%0A    if(file.isFile()) file.delete();%0A    else if(file.isDirectory())%7B%0A      File %5B%5D files = file.listFiles();%0A      if(files!=null && files.length%3E0)%7B%0A        for(File f : files)%7B%0A          delete(f);%0A        %7D%0A      %7D%0A      file.delete();%0A    %7D%0A  
 %7D%0A%0A%09@Tes
@@ -3323,11 +3323,522 @@
 e());%0A%09%7D
+%0A%0A%09@Test%0A%09public void testDependencyLoading() %7B%0A%09  // try to import library does not exist on classpath. it'll fail%0A    assertEquals(InterpreterResult.Code.ERROR, repl.interpret(%22import org.apache.commons.csv.CSVFormat%22, context).code());%0A%0A    // load library from maven repository and try to import again%0A%09  repl.interpret(%22z.load(%5C%22org.apache.commons:commons-csv:1.1%5C%22)%22, context);%0A    assertEquals(InterpreterResult.Code.SUCCESS, repl.interpret(%22import org.apache.commons.csv.CSVFormat%22, context).code());%0A%09%7D
 %0A%7D%0A

Add basic test for dependency library loading