package com.collective.celos.ci.mode.test;

import com.collective.celos.ci.config.CiCommandLine;
import com.collective.celos.ci.config.deploy.CelosCiContext;
import com.collective.celos.ci.config.deploy.CelosCiTarget;
import org.apache.commons.lang.StringUtils;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.File;
import java.net.URI;
import java.util.Arrays;

/**
 * Created by akonopko on 10/1/14.
 */
public class TestRunTest {

    @Rule
    public TemporaryFolder tempDir = new TemporaryFolder();

    @Test
    public void testCelosCiDeployContext() throws Exception {


        URI hadoopCoreUrl = Thread.currentThread().getContextClassLoader().getResource("com/collective/celos/ci/testing/config/core-site.xml").toURI();
        URI hadoopHdfsUrl = Thread.currentThread().getContextClassLoader().getResource("com/collective/celos/ci/testing/config/hdfs-site.xml").toURI();

        CiCommandLine commandLine = new CiCommandLine("", "TEST", "deploydir", "workflow", "testDir", "uname", false);
        CelosCiTarget target = new CelosCiTarget(hadoopHdfsUrl, hadoopCoreUrl, URI.create("celoswfdir"), URI.create("defdir"), URI.create(""));
        TestCase testCase = new TestCase("tc1", "2013-12-20T16:00Z", "2013-12-20T16:00Z");
        TestRun celosCiTest = new TestRun(target, commandLine, testCase, tempDir.newFolder());

        CelosCiContext context = celosCiTest.getCiContext();

        String tmpDir = new File(System.getProperty("java.io.tmpdir"), "celos").getAbsolutePath();

        Assert.assertEquals(context.getDeployDir(), commandLine.getDeployDir());
        Assert.assertTrue(StringUtils.isNotEmpty(context.getHdfsPrefix()));
        Assert.assertEquals(context.getMode(), commandLine.getMode());
        Assert.assertEquals(context.getTarget().getWorkflowsDirUri(), new File(celosCiTest.getTestCaseTempDir(), "workflows").toURI());
        Assert.assertFalse(context.getTarget().getDefaultsDirUri().toString().startsWith(tmpDir));
        Assert.assertEquals(context.getTarget().getPathToCoreSite(), hadoopCoreUrl);
        Assert.assertEquals(context.getTarget().getPathToHdfsSite(), hadoopHdfsUrl);
        Assert.assertEquals(context.getUserName(), commandLine.getUserName());
        Assert.assertEquals(context.getWorkflowName(), commandLine.getWorkflowName());

        Assert.assertTrue(celosCiTest.getTestCaseTempDir().toString().startsWith(System.getProperty("java.io.tmpdir")));
        Assert.assertTrue(celosCiTest.getTestCaseTempDir().toString().length() > tmpDir.length());
        Assert.assertEquals(celosCiTest.getCelosDbDir(), new File(celosCiTest.getTestCaseTempDir(), "db"));
        Assert.assertEquals(celosCiTest.getCelosWorkflowDir().toURI(), context.getTarget().getWorkflowsDirUri());
    }

    @Test
    public void testCopyRemoteDefaults() throws Exception {

        URI hadoopCoreUrl = Thread.currentThread().getContextClassLoader().getResource("com/collective/celos/ci/testing/config/core-site.xml").toURI();
        URI hadoopHdfsUrl = Thread.currentThread().getContextClassLoader().getResource("com/collective/celos/ci/testing/config/hdfs-site.xml").toURI();

        File deployDir = tempDir.newFolder();
        deployDir.mkdirs();
        File defFile = new File(deployDir, "defaults.js");
        defFile.createNewFile();

        File remoteFolderWf = tempDir.newFolder();
        remoteFolderWf.mkdirs();

        File remoteFolderDef = tempDir.newFolder();
        remoteFolderDef.mkdirs();
        File otherDefFile1 = new File(remoteFolderDef, "some-defaults1.js");
        otherDefFile1.createNewFile();
        File otherDefFile2 = new File(remoteFolderDef, "some-defaults2.js");
        otherDefFile2.createNewFile();

        CiCommandLine commandLine = new CiCommandLine("", "TEST", deployDir.getAbsolutePath(), "myworkflow", "testDir", "uname", false);
        CelosCiTarget target = new CelosCiTarget(hadoopHdfsUrl, hadoopCoreUrl, remoteFolderWf.toURI(), remoteFolderDef.toURI(), URI.create(""));
        TestCase testCase = new TestCase("tc1", "2013-12-20T16:00Z", "2013-12-20T16:00Z");

        TestRun testRun = new TestRun(target, commandLine, testCase, tempDir.newFolder());

        testRun.prepareCelosServerEnv();

        String[] fileNames = testRun.getCelosDefaultsDir().list();
        Arrays.sort(fileNames);

        Assert.assertArrayEquals(fileNames, new String[] {"some-defaults1.js", "some-defaults2.js"});

    }


    @Test
    public void testCopyRemoteDefaultsNoTargetDefaults() throws Exception {

        URI hadoopCoreUrl = Thread.currentThread().getContextClassLoader().getResource("com/collective/celos/ci/testing/config/core-site.xml").toURI();
        URI hadoopHdfsUrl = Thread.currentThread().getContextClassLoader().getResource("com/collective/celos/ci/testing/config/hdfs-site.xml").toURI();

        File deployDir = tempDir.newFolder();
        File emptyDir = tempDir.newFolder();
        deployDir.mkdirs();
        File defFile = new File(deployDir, "defaults.js");
        defFile.createNewFile();

        CiCommandLine commandLine = new CiCommandLine("", "TEST", deployDir.getAbsolutePath(), "myworkflow", "testDir", "uname", false);
        CelosCiTarget target = new CelosCiTarget(hadoopHdfsUrl, hadoopCoreUrl, emptyDir.toURI(), emptyDir.toURI(), URI.create(""));

        TestCase testCase = new TestCase("tc1", "2013-12-20T16:00Z", "2013-12-20T16:00Z");

        TestRun testRun = new TestRun(target, commandLine, testCase, tempDir.newFolder());

        testRun.prepareCelosServerEnv();

        String[] fileNames = testRun.getCelosDefaultsDir().list();

        Assert.assertArrayEquals(fileNames, new String[] {});

    }

}

@@ -280,16 +280,41 @@
 Assert;%0A
+import org.junit.Before;%0A
 import o
@@ -324,24 +324,24 @@
 junit.Rule;%0A
-
 import org.j
@@ -409,24 +409,52 @@
 va.io.File;%0A
+import java.io.IOException;%0A
 import java.
@@ -626,24 +626,161 @@
 yFolder();%0A%0A
+    public File tmpDirFile;%0A%0A    @Before%0A    public void before() throws IOException %7B%0A        tmpDirFile = tempDir.newFolder();%0A    %7D%0A%0A%0A
     @Test%0A  
@@ -1233,33 +1233,52 @@
 %22workflow%22, 
-%22testDir%22
+tmpDirFile.getAbsolutePath()
 , %22uname%22, f
@@ -4078,33 +4078,52 @@
 yworkflow%22, 
-%22testDir%22
+tmpDirFile.getAbsolutePath()
 , %22uname%22, f
@@ -5428,17 +5428,36 @@
 w%22, 
-%22testDir%22
+tmpDirFile.getAbsolutePath()
 , %22u

fix test