package brooklyn.cli;

import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;
import groovy.lang.GroovyClassLoader;

import org.iq80.cli.Cli;
import org.iq80.cli.ParseException;
import org.testng.annotations.Test;

import brooklyn.cli.Main.BrooklynCommand;
import brooklyn.cli.Main.HelpCommand;
import brooklyn.cli.Main.LaunchCommand;
import brooklyn.entity.basic.AbstractApplication;
import brooklyn.util.ResourceUtils;

public class CliTest {

    @Test
    public void testLoadApplicationFromClasspath() throws Exception {
        LaunchCommand launchCommand = new Main.LaunchCommand();
        ResourceUtils resourceUtils = new ResourceUtils(this);
        GroovyClassLoader loader = new GroovyClassLoader(CliTest.class.getClassLoader());
        String appName = ExampleApp.class.getName();
        AbstractApplication app = launchCommand.loadApplicationFromClasspathOrParse(resourceUtils, loader, appName);
        assertTrue(app instanceof ExampleApp, "app="+app);
    }

    @Test
    public void testLoadApplicationByParsingFile() throws Exception {
        LaunchCommand launchCommand = new Main.LaunchCommand();
        ResourceUtils resourceUtils = new ResourceUtils(this);
        GroovyClassLoader loader = new GroovyClassLoader(CliTest.class.getClassLoader());
        String appName = "ExampleAppInFile.groovy"; // file found in src/test/resources (contains empty app)
        AbstractApplication app = launchCommand.loadApplicationFromClasspathOrParse(resourceUtils, loader, appName);
        assertTrue(app.getClass().getName().equals("ExampleAppInFile"), "app="+app);
    }
    
    @Test
    public void testLaunchCommand() throws ParseException {
        Cli<BrooklynCommand> cli = Main.buildCli();
        BrooklynCommand command = cli.parse("launch", "--app", "my.App", "--location", "localhost");
        assertTrue(command instanceof LaunchCommand);
        String details = command.toString();
        assertTrue(details.contains("app=my.App"));   
        assertTrue(details.contains("script=null"));
        assertTrue(details.contains("location=[localhost]"));
        assertTrue(details.contains("port=8081"));
        assertTrue(details.contains("noConsole=false"));
        assertTrue(details.contains("noShutdwonOnExit=false"));
    }

    @Test(expectedExceptions = ParseException.class, expectedExceptionsMessageRegExp = "Required option '-a' is missing")
    public void testMissingAppOption() throws ParseException {
        Cli<BrooklynCommand> cli = Main.buildCli();
        cli.parse("launch", "blah", "my.App");
        fail("Should throw ParseException");
    }
    
    public void testHelpCommand() {
        Cli<BrooklynCommand> cli = Main.buildCli();
        BrooklynCommand command = cli.parse("help");
        assertTrue(command instanceof HelpCommand);
        command = cli.parse();
        assertTrue(command instanceof HelpCommand);
    }
    
    // An empty app to be used for testing
    @SuppressWarnings("serial")
    public static class ExampleApp extends AbstractApplication { }
}

@@ -1888,27 +1888,39 @@
 aunchCommand
+, %22%22+command
 );%0A
-
         Stri
@@ -2001,16 +2001,25 @@
 my.App%22)
+, details
 );   %0A  
@@ -2066,16 +2066,25 @@
 t=null%22)
+, details
 );%0A     
@@ -2128,9 +2128,8 @@
 ion=
-%5B
 loca
@@ -2137,11 +2137,19 @@
 host
-%5D
 %22)
+, details
 );%0A 
@@ -2195,16 +2195,25 @@
 t=8081%22)
+, details
 );%0A     
@@ -2257,24 +2257,33 @@
 sole=false%22)
+, details
 );%0A        a
@@ -2334,16 +2334,25 @@
 =false%22)
+, details
 );%0A    %7D

fix CLI locations test