package brooklyn.cli;

import brooklyn.rest.BrooklynService;
import org.junit.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class ClientTest {

    BrooklynService brooklynServer;
    Client brooklynClient;

    @BeforeClass
    public void testSetUp() throws Exception {
        // Start the REST server
        brooklynServer = BrooklynService.newBrooklynService();
        String[] args = {"server","config.sample.yml"};
        brooklynServer.runAsync(args);
    }

    @AfterClass
    public void testTearDown() {
        // Kill the REST server
        brooklynServer = null;
    }

    @Test(enabled = true)
    public void testCatalogEntitiesCommand() {
        System.out.println("testCatalogEntitiesCommand");
        String[] args = {"catalog-entities"};
        Client.main(args);
        //TODO: check that client output matches what we expect (need to make the client more testable)
    }


}

@@ -58,33 +58,8 @@
 ce;%0A
-import org.junit.Assert;%0A
 impo
@@ -179,352 +179,1111 @@
 st;%0A
-%0Apublic class ClientTest %7B%0A%0A    BrooklynService brooklynServer;%0A    Client brooklynClient;%0A%0A    @BeforeClass%0A    public void testSetUp() throws Exception %7B%0A        // Start the REST server%0A        brooklynServer = BrooklynService.newBrooklynService();%0A        String%5B%5D args = %7B%22server%22,%22config.sample.yml%22%7D;%0A        brooklynServer.runAsync(args
+import java.io.ByteArrayOutputStream;%0Aimport java.io.PrintStream;%0A%0Aimport static org.hamcrest.MatcherAssert.assertThat;%0Aimport static org.hamcrest.Matchers.containsString;%0A%0Apublic class ClientTest %7B%0A%0A    BrooklynService brooklynServer;%0A    Client brooklynClient;%0A%0A    private ByteArrayOutputStream outBytes;%0A    private PrintStream out;%0A    private ByteArrayOutputStream errBytes;%0A    private PrintStream err;%0A%0A    protected String standardOut() %7B%0A        return outBytes.toString();%0A    %7D%0A%0A    protected String standardErr() %7B%0A        return errBytes.toString();%0A    %7D%0A%0A    @BeforeClass%0A    public void testSetUp() throws Exception %7B%0A        // Start the REST server%0A        brooklynServer = BrooklynService.newBrooklynService();%0A        String%5B%5D args = %7B%22server%22,%22config.sample.yml%22%7D;%0A        brooklynServer.runAsync(args);%0A        // Rewire the output/error stream of the client%0A        outBytes = new ByteArrayOutputStream();%0A        out = new PrintStream(outBytes);%0A        errBytes = new ByteArrayOutputStream();%0A        err = new PrintStream(errBytes);%0A        brooklynClient = new Client(out,err
 );%0A 
@@ -1360,32 +1360,52 @@
  the REST server
+ and client instance
 %0A        brookly
@@ -1420,16 +1420,166 @@
 = null;%0A
+        brooklynClient = null;%0A        // Reset i/o streams%0A        outBytes = null;%0A        out = null;%0A        errBytes = null;%0A        err = null;%0A
     %7D%0A%0A 
@@ -1652,67 +1652,70 @@
 d() 
-%7B%0A        System.out.println(%22testC
+throws Exception %7B%0A        // Run the c
 atalog
-E
+-e
 ntities
-C
+ c
 ommand
-%22);
 %0A   
@@ -1769,18 +1769,25 @@
     
+brooklyn
 Client.
-mai
+ru
 n(ar
@@ -1805,15 +1805,10 @@
   //
-TODO: c
+ C
 heck
@@ -1813,23 +1813,16 @@
 ck that 
-client 
 output m
@@ -1846,48 +1846,105 @@
 pect
- (need to make the client more testable)
+%0A        assertThat(standardOut(), containsString(%22brooklyn.entity.webapp.jboss.JBoss6Server%22));%0A
 %0A   

Implement assert statement for catalog-entities command