package org.globalbioticinteractions.cmd;

import com.beust.jcommander.JCommander;
import org.apache.commons.io.FileUtils;
import org.globalbioticinteractions.cmd.CmdCheck;
import org.globalbioticinteractions.cmd.CmdLine;
import org.globalbioticinteractions.cmd.CmdList;
import org.globalbioticinteractions.cmd.CmdUpdate;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.io.File;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

public class CmdLineIT {

    @Before
    public void emptyCache() {
        FileUtils.deleteQuietly(new File("target/tmp-dataset"));
    }

    @Test
    public void list() {
        JCommander jc = new CmdLine().buildCommander();
        jc.parse("list");

        Assert.assertEquals(jc.getParsedCommand(), "list");

        JCommander actual = jc.getCommands().get(jc.getParsedCommand());
        Assert.assertEquals(actual.getObjects().size(), 1);
        Assert.assertEquals(actual.getObjects().get(0).getClass(), CmdList.class);

        CmdLine.run(actual);
    }

    @Test
    public void update() {
        JCommander jc = new CmdLine().buildCommander();
        jc.parse("update", "--cache-dir=./bla");
        assertUpdateCmd(jc);

        jc = new CmdLine().buildCommander();
        jc.parse("update", "-c", "./bla");
        assertUpdateCmd(jc);
    }

    @Test
    public void runUpdate() {
        JCommander jc = new CmdLine().buildCommander();
        jc.parse("update", "--cache-dir=./target/tmp-dataset", "globalbioticinteractions/template-dataset");

        JCommander actual = jc.getCommands().get(jc.getParsedCommand());
        Assert.assertEquals(actual.getObjects().size(), 1);
        Assert.assertEquals(actual.getObjects().get(0).getClass(), CmdUpdate.class);

        CmdLine.run(actual);
    }

    @Test
    public void runCheck() {
        JCommander jc = new CmdLine().buildCommander();
        jc.parse("check", "--cache-dir=./target/tmp-dataset", "globalbioticinteractions/template-dataset");

        JCommander actual = jc.getCommands().get(jc.getParsedCommand());
        Assert.assertEquals(actual.getObjects().size(), 1);
        Assert.assertEquals(actual.getObjects().get(0).getClass(), CmdCheck.class);

        CmdLine.run(actual);
    }

    private void assertUpdateCmd(JCommander jc) {
        Assert.assertEquals(jc.getParsedCommand(), "update");

        JCommander actual = jc.getCommands().get(jc.getParsedCommand());
        Assert.assertEquals(actual.getObjects().size(), 1);
        Object cmd = actual.getObjects().get(0);
        Assert.assertEquals(cmd.getClass(), CmdUpdate.class);
        assertThat(((CmdUpdate) cmd).getCacheDir(), is("./bla"));
    }
}
@@ -1102,1617 +1102,430 @@
 oid 
-update() %7B%0A        JCommander jc = new CmdLine().buildCommander();%0A        jc.parse(%22update%22, %22--cache-dir=./bla%22);%0A        assertUpdateCmd(jc);%0A%0A        jc = new CmdLine().buildCommander();%0A        jc.parse(%22update%22, %22-c%22, %22./bla%22);%0A        assertUpdateCmd(jc);%0A    %7D%0A%0A    @Test%0A    public void runUpdate() %7B%0A        JCommander jc = new CmdLine().buildCommander();%0A        jc.parse(%22update%22, %22--cache-dir=./target/tmp-dataset%22, %22globalbioticinteractions/template-dataset%22);%0A%0A        JCommander actual = jc.getCommands().get(jc.getParsedCommand());%0A        Assert.assertEquals(actual.getObjects().size(), 1);%0A        Assert.assertEquals(actual.getObjects().get(0).getClass(), CmdUpdate.class);%0A%0A        CmdLine.run(actual);%0A    %7D%0A%0A    @Test%0A    public void runCheck() %7B%0A        JCommander jc = new CmdLine().buildCommander();%0A        jc.parse(%22check%22, %22--cache-dir=./target/tmp-dataset%22, %22globalbioticinteractions/template-dataset%22);%0A%0A        JCommander actual = jc.getCommands().get(jc.getParsedCommand());%0A        Assert.assertEquals(actual.getObjects().size(), 1);%0A        Assert.assertEquals(actual.getObjects().get(0).getClass(), CmdCheck.class);%0A%0A        CmdLine.run(actual);%0A    %7D%0A%0A    private void assertUpdateCmd(JCommander jc) %7B%0A        Assert.assertEquals(jc.getParsedCommand(), %22update%22);%0A%0A        JCommander actual = jc.getCommands().get(jc.getParsedCommand());%0A        Assert.assertEquals(actual.getObjects().size(), 1);%0A        Object cmd = actual.getObjects().get(0);%0A        Assert.assertEquals(cmd.getClass(), CmdUpdate.class);%0A        assertThat(((CmdUpdate) cmd).getCacheDir(), is(%22./bla%22)
+runCheck() %7B%0A        JCommander jc = new CmdLine().buildCommander();%0A        jc.parse(%22check%22, %22--cache-dir=./target/tmp-dataset%22, %22globalbioticinteractions/template-dataset%22);%0A%0A        JCommander actual = jc.getCommands().get(jc.getParsedCommand());%0A        Assert.assertEquals(actual.getObjects().size(), 1);%0A        Assert.assertEquals(actual.getObjects().get(0).getClass(), CmdCheck.class);%0A%0A        CmdLine.run(actual
 );%0A 

remove duplicate code