/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.zeppelin.spark;

import static org.junit.Assert.assertEquals;

import java.io.File;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Properties;

import org.apache.zeppelin.display.AngularObjectRegistry;
import org.apache.zeppelin.user.AuthenticationInfo;
import org.apache.zeppelin.display.GUI;
import org.apache.zeppelin.interpreter.*;
import org.apache.zeppelin.interpreter.InterpreterResult.Code;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class DepInterpreterTest {
  private DepInterpreter dep;
  private InterpreterContext context;
  private File tmpDir;
  private SparkInterpreter repl;

  private Properties getTestProperties() {
    Properties p = new Properties();
    p.setProperty("zeppelin.dep.localrepo", "local-repo");
    p.setProperty("zeppelin.dep.additionalRemoteRepository", "spark-packages,http://dl.bintray.com/spark-packages/maven,false;");
    return p;
  }

  @Before
  public void setUp() throws Exception {
    tmpDir = new File(System.getProperty("java.io.tmpdir") + "/ZeppelinLTest_" + System.currentTimeMillis());
    System.setProperty("zeppelin.dep.localrepo", tmpDir.getAbsolutePath() + "/local-repo");

    tmpDir.mkdirs();

    Properties p = getTestProperties();

    dep = new DepInterpreter(p);
    dep.open();

    InterpreterGroup intpGroup = new InterpreterGroup();
    intpGroup.put("note", new LinkedList<Interpreter>());
    intpGroup.get("note").add(new SparkInterpreter(p));
    intpGroup.get("note").add(dep);
    dep.setInterpreterGroup(intpGroup);

    context = new InterpreterContext("note", "id", null, "title", "text", new AuthenticationInfo(),
        new HashMap<String, Object>(), new GUI(),
        new AngularObjectRegistry(intpGroup.getId(), null),
        null,
        new LinkedList<InterpreterContextRunner>(), null);
  }

  @After
  public void tearDown() throws Exception {
    dep.close();
    delete(tmpDir);
  }

  private void delete(File file) {
    if (file.isFile()) file.delete();
    else if (file.isDirectory()) {
      File[] files = file.listFiles();
      if (files != null && files.length > 0) {
        for (File f : files) {
          delete(f);
        }
      }
      file.delete();
    }
  }

  @Test
  public void testDefault() {
    dep.getDependencyContext().reset();
    InterpreterResult ret = dep.interpret("z.load(\"org.apache.commons:commons-csv:1.1\")", context);
    assertEquals(Code.SUCCESS, ret.code());

    assertEquals(1, dep.getDependencyContext().getFiles().size());
    assertEquals(1, dep.getDependencyContext().getFilesDist().size());

    // Add a test for the spark-packages repo - default in additionalRemoteRepository
    ret = dep.interpret("z.load(\"amplab:spark-indexedrdd:0.3\")", context);
    assertEquals(Code.SUCCESS, ret.code());

    // Reset at the end of the test
    dep.getDependencyContext().reset();
  }
}

@@ -892,20 +892,27 @@
 java.io.
-File
+IOException
 ;%0Aimport
@@ -1315,78 +1315,178 @@
 nit.
-Test;%0A%0Apublic class DepInterpreterTest %7B%0A  private DepInterpreter dep;
+Rule;%0Aimport org.junit.Test;%0Aimport org.junit.rules.TemporaryFolder;%0A%0Apublic class DepInterpreterTest %7B%0A%0A  @Rule%0A  public TemporaryFolder tmpDir = new TemporaryFolder();%0A
 %0A  p
@@ -1492,16 +1492,19 @@
 private 
+Dep
 Interpre
@@ -1510,46 +1510,12 @@
 eter
-Context context;%0A  private File tmpDir
+ dep
 ;%0A  
@@ -1522,21 +1522,16 @@
 private 
-Spark
 Interpre
@@ -1537,13 +1537,23 @@
 eter
- repl
+Context context
 ;%0A%0A 
@@ -1591,16 +1591,35 @@
 erties()
+ throws IOException
  %7B%0A    P
@@ -1698,20 +1698,44 @@
 o%22, 
-%22local-repo%22
+tmpDir.newFolder().getAbsolutePath()
 );%0A 
@@ -1937,233 +1937,8 @@
 n %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
     
@@ -2634,324 +2634,8 @@
 ();%0A
-    delete(tmpDir);%0A  %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 %3E 0) %7B%0A        for (File f : files) %7B%0A          delete(f);%0A        %7D%0A      %7D%0A      file.delete();%0A    %7D%0A
   %7D%0A

Fix possible reason of DepInterpreterTest failure