package com.collective.celos.ci.fixture;

import com.collective.celos.ci.config.deploy.CelosCiContext;
import com.collective.celos.ci.fixtures.AbstractFixtureFileWorker;
import com.collective.celos.ci.fixtures.FixturesHdfsWorkerManager;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.fs.Path;
import org.junit.Assert;
import org.junit.Test;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.mockito.Mockito.mock;

/**
 * Created by akonopko on 9/18/14.
 */
public class FixturesHdfsWorkerManagerTest {

    @Test
    public void testProcessLocalDir() throws Exception {

        CelosCiContext context = mock(CelosCiContext.class);
        Map<String, AbstractFixtureFileWorker> fixtureWorkers = Maps.newHashMap();
        TraverseFixtureFileWorker worker = new TraverseFixtureFileWorker();
        fixtureWorkers.put("plain", worker);
        fixtureWorkers.put("avro", worker);
        FixturesHdfsWorkerManager manager = new FixturesHdfsWorkerManager(context, fixtureWorkers);
        File inputFolder = new File(Thread.currentThread().getContextClassLoader().getResource("com/collective/celos/ci/fixtures/input").toURI());
        manager.processLocalDir(inputFolder);

        List<String> relativeVisitedDirs = Lists.newArrayList();
        for (File f : worker.visited) {
            relativeVisitedDirs.add(getRelativePath(f, inputFolder));
        }
        Assert.assertEquals(relativeVisitedDirs, Lists.newArrayList("plain", "PLAIN", "Avro"));
    }

    @Test(expected = RuntimeException.class)
    public void testProcessLocalDirNoWorker() throws Exception {

        CelosCiContext context = mock(CelosCiContext.class);
        Map<String, AbstractFixtureFileWorker> fixtureWorkers = Maps.newHashMap();
        TraverseFixtureFileWorker worker = new TraverseFixtureFileWorker();
        fixtureWorkers.put("plain", worker);
        FixturesHdfsWorkerManager manager = new FixturesHdfsWorkerManager(context, fixtureWorkers);
        File inputFolder = new File(Thread.currentThread().getContextClassLoader().getResource("com/collective/celos/ci/fixtures/input").toURI());
        manager.processLocalDir(inputFolder);

        List<String> relativeVisitedDirs = Lists.newArrayList();
        for (File f : worker.visited) {
            relativeVisitedDirs.add(getRelativePath(f, inputFolder));
        }
        Assert.assertEquals(relativeVisitedDirs, Lists.newArrayList("plain", "PLAIN", "Avro"));
    }


    public String getRelativePath(File file, File base) {
        java.nio.file.Path pathAbsolute = java.nio.file.Paths.get(file.getAbsolutePath());
        java.nio.file.Path pathRelative = java.nio.file.Paths.get(base.getAbsolutePath()).relativize(pathAbsolute);
        return pathRelative.toString();
    }

    public static class TraverseFixtureFileWorker extends AbstractFixtureFileWorker {

        private List<File> visited = Lists.newArrayList();

        public void process(CelosCiContext context, File localDir) throws Exception {
            visited.add(localDir);
        }

        @Override
        public void processPair(CelosCiContext context, File localFile, Path hdfsFile) throws Exception {
        }
    }

}

@@ -35,16 +35,229 @@
 xture;%0A%0A
+import static org.mockito.Mockito.mock;%0A%0Aimport java.io.File;%0Aimport java.util.List;%0Aimport java.util.Map;%0Aimport java.util.Set;%0A%0Aimport org.apache.hadoop.fs.Path;%0Aimport org.junit.Assert;%0Aimport org.junit.Test;%0A%0A
 import c
@@ -533,259 +533,38 @@
 ort 
-org.apache.commons.lang.StringUtils;%0Aimport org.apache.hadoop.fs.Path;%0Aimport org.junit.Assert;%0Aimport org.junit.Test;%0A%0Aimport java.io.File;%0Aimport java.util.HashMap;%0Aimport java.util.List;%0Aimport java.util.Map;%0A%0Aimport static org.mockito.Mockito.mock
+com.google.common.collect.Sets
 ;%0A%0A/
@@ -1322,35 +1322,34 @@
 lder);%0A%0A        
-Lis
+Se
 t%3CString%3E relati
@@ -1364,33 +1364,30 @@
 dDirs = 
-Lis
+Se
 ts.new
-ArrayLis
+HashSe
 t();%0A   
@@ -1552,33 +1552,30 @@
 edDirs, 
-Lis
+Se
 ts.new
-ArrayLis
+HashSe
 t(%22plain
@@ -2281,19 +2281,18 @@
         
-Lis
+Se
 t%3CString
@@ -2315,33 +2315,30 @@
 dDirs = 
-Lis
+Se
 ts.new
-ArrayLis
+HashSe
 t();%0A   
@@ -2503,33 +2503,30 @@
 edDirs, 
-Lis
+Se
 ts.new
-ArrayLis
+HashSe
 t(%22plain

Fix test.