package org.opencb.hpg.bigdata.analysis.tools;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import org.junit.Test;
import org.opencb.hpg.bigdata.analysis.exceptions.AnalysisToolException;

import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

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

/**
 * Created by pfurio on 23/05/17.
 */
public class ToolManagerTest {

    @Test
    public void testToolManager() throws URISyntaxException, AnalysisToolException, IOException {
        Path toolPath = Paths.get(getClass().getResource("/tools").toURI());
        Path testBam = Paths.get(getClass().getResource("/test.bam").toURI());
        Path tmp = Paths.get("/tmp");

        ToolManager toolManager = new ToolManager(toolPath);
        Map<String, String> params = new HashMap<>();
        params.put("input", testBam.toAbsolutePath().toString());
        params.put("output", "/tmp/test.bam.bai");

        String commandLine = toolManager.createCommandLine("samtools", "index", params);
        System.out.println(commandLine);
        Executor.execute(commandLine, tmp, true);

        ObjectReader reader = new ObjectMapper().reader(Status.class);
        Status status = reader.readValue(tmp.resolve("status.json").toFile());
        assertEquals(Status.DONE, status.getName());

        assertTrue(tmp.resolve("test.bam.bai").toFile().exists());
    }

}
@@ -141,24 +141,100 @@
 jectReader;%0A
+import org.apache.commons.lang3.RandomStringUtils;%0Aimport org.junit.Before;%0A
 import org.j
@@ -244,16 +244,16 @@
 t.Test;%0A
-
 import o
@@ -374,32 +374,60 @@
 yntaxException;%0A
+import java.nio.file.Files;%0A
 import java.nio.
@@ -677,16 +677,769 @@
 Test %7B%0A%0A
+    private Path tmpFolder;%0A    private Path toolPath;%0A%0A    @Before%0A    public void setUp() throws Exception %7B%0A        tmpFolder = Paths.get(%22target/test-data/junit-%22 + RandomStringUtils.randomAlphabetic(10));%0A        Files.createDirectories(tmpFolder);%0A%0A        toolPath = tmpFolder.resolve(%22tools%22);%0A        Files.createDirectories(toolPath.resolve(%22samtools%22));%0A%0A        Files.copy(Paths.get(getClass().getResource(%22/tools/samtools/manifest.json%22).toURI()), toolPath.resolve(%22samtools%22).resolve(%22manifest.json%22));%0A        Files.copy(Paths.get(getClass().getResource(%22/tools/samtools/samtools%22).toURI()), toolPath.resolve(%22samtools%22).resolve(%22samtools%22));%0A%0A        toolPath.resolve(%22samtools%22).resolve(%22samtools%22).toFile().setExecutable(true);%0A    %7D%0A%0A
     @Tes
@@ -1542,85 +1542,8 @@
 n %7B%0A
-        Path toolPath = Paths.get(getClass().getResource(%22/tools%22).toURI());%0A
     
@@ -1620,46 +1620,8 @@
 ());
-%0A        Path tmp = Paths.get(%22/tmp%22);
 %0A%0A  
@@ -1832,14 +1832,27 @@
 t%22, 
-%22/
 tmp
-/
+Folder.resolve(%22
 test
@@ -1861,16 +1861,28 @@
 am.bai%22)
+.toString())
 ;%0A%0A     
@@ -2047,16 +2047,22 @@
 ine, tmp
+Folder
 , true);
@@ -2174,24 +2174,30 @@
 eadValue(tmp
+Folder
 .resolve(%22st
@@ -2269,24 +2269,24 @@
 etName());%0A%0A
-
         asse
@@ -2295,16 +2295,22 @@
 True(tmp
+Folder
 .resolve

fix tool manager test (samtools pemission issue)