<commit_before>package brooklyn.cli;

import brooklyn.cli.commands.CommandExecutionException;
import brooklyn.entity.basic.BasicEntity;
import brooklyn.rest.BrooklynService;
import brooklyn.rest.core.ApplicationManager;
import com.google.common.collect.Iterables;
import com.google.common.io.ByteStreams;
import com.google.common.io.Files;
import com.yammer.dropwizard.logging.Log;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.PrintStream;
import java.io.InputStream;
import static org.testng.Assert.assertEquals;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;

public class ClientTest {

    private final static Log LOG = Log.forClass(ClientTest.class);

    private BrooklynService brooklynServer;
    private ApplicationManager applicationManager;
    private Client brooklynClient;

    private ByteArrayOutputStream outBytes;
    private PrintStream out;
    private ByteArrayOutputStream errBytes;
    private PrintStream err;

    File tempConfigFile;

    protected String standardOut() {
        return outBytes.toString();
    }

    protected String standardErr() {
        return errBytes.toString();
    }

    @BeforeClass
    public void oneTimeSetUp() throws Exception {
        // Create temporary config file
        tempConfigFile = File.createTempFile("server-config",".yml");
        InputStream configInputStream = getClass().getClassLoader().getResourceAsStream("config/config.sample.yml");
        try {
            Files.write(ByteStreams.toByteArray(configInputStream), tempConfigFile);
        } finally {
            configInputStream.close();
        }

        // Start the REST server
        brooklynServer = BrooklynService.newBrooklynService();
        String[] args = {"server",tempConfigFile.getAbsolutePath()};
        brooklynServer.runAsync(args);
        applicationManager = brooklynServer.getApplicationManager();
    }

    @BeforeMethod
    public void setUp() throws Exception {
        // Set i/o streams
        outBytes = new ByteArrayOutputStream();
        out = new PrintStream(outBytes);
        errBytes = new ByteArrayOutputStream();
        err = new PrintStream(errBytes);
        // Create a client instance
        brooklynClient = new Client(out,err);
    }

    @AfterClass
    public void oneTimeTearDown() throws Exception {
        // Kill the REST server and client instance
        brooklynServer.stop();
        // Delete temp file
        tempConfigFile.delete();
    }

    @Test
    public void testCatalogEntitiesCommand() throws Exception {
        try {
            String[] args = {"catalog-entities"};
            brooklynClient.run(args);
            // Check list of entity types includes one of the defaults
            assertThat(standardOut(), containsString(BasicEntity.class.getName()));
        } catch (Exception e) {
            LOG.error("\nstdout="+standardOut()+"\nstderr="+standardErr()+"\n", e);
            throw e;
        }
    }

    @Test
    public void testDeployCreatesApp() throws Exception {
        try {
            String[] args = {"deploy","--format","class", "brooklyn.cli.ExampleApp"};
            brooklynClient.run(args);
            // We should only have 1 app in the server's registry
            assertEquals(applicationManager.registry().size(), 1);
            // The name of that app should match what we have provided in the deploy command
            assertEquals(Iterables.getOnlyElement(applicationManager.registry().keySet()), ExampleApp.class.getName());
        } catch (Exception e) {
            LOG.error("stdout="+standardOut()+"; stderr="+standardErr(), e);
            throw e;
        }
    }

    @Test(dependsOnMethods = {"testDeployCreatesApp"})
    public void testUndeployStopsRunningApp() throws Exception {
        try {
            String[] args = {"undeploy","brooklyn.cli.ExampleApp"};
            brooklynClient.run(args);<commit_msg>Add test for "catalog-policies" command<commit_after>package brooklyn.cli;

import brooklyn.cli.commands.CommandExecutionException;
import brooklyn.entity.basic.BasicEntity;
import brooklyn.policy.basic.GeneralPurposePolicy;
import brooklyn.rest.BrooklynService;
import brooklyn.rest.core.ApplicationManager;
import com.google.common.collect.Iterables;
import com.google.common.io.ByteStreams;
import com.google.common.io.Files;
import com.yammer.dropwizard.logging.Log;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.PrintStream;
import java.io.InputStream;
import static org.testng.Assert.assertEquals;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;

public class ClientTest {

    private final static Log LOG = Log.forClass(ClientTest.class);

    private BrooklynService brooklynServer;
    private ApplicationManager applicationManager;
    private Client brooklynClient;

    private ByteArrayOutputStream outBytes;
    private PrintStream out;
    private ByteArrayOutputStream errBytes;
    private PrintStream err;

    File tempConfigFile;

    protected String standardOut() {
        return outBytes.toString();
    }

    protected String standardErr() {
        return errBytes.toString();
    }

    @BeforeClass
    public void oneTimeSetUp() throws Exception {
        // Create temporary config file
        tempConfigFile = File.createTempFile("server-config",".yml");
        InputStream configInputStream = getClass().getClassLoader().getResourceAsStream("config/config.sample.yml");
        try {
            Files.write(ByteStreams.toByteArray(configInputStream), tempConfigFile);
        } finally {
            configInputStream.close();
        }

        // Start the REST server
        brooklynServer = BrooklynService.newBrooklynService();
        String[] args = {"server",tempConfigFile.getAbsolutePath()};
        brooklynServer.runAsync(args);
        applicationManager = brooklynServer.getApplicationManager();
    }

    @BeforeMethod
    public void setUp() throws Exception {
        // Set i/o streams
        outBytes = new ByteArrayOutputStream();
        out = new PrintStream(outBytes);
        errBytes = new ByteArrayOutputStream();
        err = new PrintStream(errBytes);
        // Create a client instance
        brooklynClient = new Client(out,err);
    }

    @AfterClass
    public void oneTimeTearDown() throws Exception {
        // Kill the REST server and client instance
        brooklynServer.stop();
        // Delete temp file
        tempConfigFile.delete();
    }

    @Test
    public void testCatalogEntitiesCommand() throws Exception {
        try {
            String[] args = {"catalog-entities"};
            brooklynClient.run(args);
            // Check list of entity types includes one of the defaults
            assertThat(standardOut(), containsString(BasicEntity.class.getName()));
        } catch (Exception e) {
            LOG.error("\nstdout="+standardOut()+"\nstderr="+standardErr()+"\n", e);
            throw e;
        }
    }

    @Test
    public void testCatalogPoliciesCommand() throws Exception {
        try {
            String[] args = {"catalog-policies"};
            brooklynClient.run(args);
            // Check list of entity types includes one of the defaults
            assertThat(standardOut(), containsString(GeneralPurposePolicy.class.getName()));
        } catch (Exception e) {
            LOG.error("\nstdout="+standardOut()+"\nstderr="+standardErr()+"\n", e);
            throw e;
        }
    }

    @Test
    public void testDeployCreatesApp() throws Exception {
        try {
            String[] args = {"deploy","--format","class", "brooklyn.cli.ExampleApp"};
            brooklynClient.run(args);
            // We should only have 1 app in the server's registry
            assertEquals(applicationManager.registry().size(), 1);
            // The name of that app should match what we have provided in the deploy command
            assertEquals(Iterables.getOnlyElement(applicationManager.registry().keySet()), ExampleApp.class.getName());
        } catch (Exception e) {
            LOG.error("stdout="+standardOut()+"; stderr="+standardErr(), e);
            throw e;
        }
    }

    @Test(dependsOnMethods = {"testDeployCreatesApp"})
    public void testUndeployStopsRunningApp() throws Exception {
        try {
            String[] args = {"undeploy","brooklyn.cli.ExampleApp"};
            brooklynClient.run(args);
{'fraction_non_alphanumeric': 0.10245948090218747, 'fraction_numerical': 0.00045336053496543126, 'mean_word_length': 2.9747747747747746, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7387992', 'n_tokens_mistral': 2309, 'n_tokens_neox': 2256, 'n_words': 543}