Search in sources :

Example 1 with AdminCommand

use of io.pravega.cli.admin.AdminCommand in project pravega by pravega.

the class TestUtils method executeCommand.

/**
 * Invoke any command and get the result by using a mock PrintStream object (instead of System.out). The returned
 * String is the output written by the Command that can be check in any test.
 *
 * @param inputCommand Command to execute.
 * @param state        Configuration to execute the command.
 * @return             Output of the command.
 * @throws Exception   If a problem occurs.
 */
public static String executeCommand(String inputCommand, AdminCommandState state) throws Exception {
    Parser.Command pc = Parser.parse(inputCommand);
    CommandArgs args = new CommandArgs(pc.getArgs(), state);
    AdminCommand cmd = AdminCommand.Factory.get(pc.getComponent(), pc.getName(), args);
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try (PrintStream ps = new PrintStream(baos, true, StandardCharsets.UTF_8)) {
        cmd.setOut(ps);
        cmd.execute();
    }
    return baos.toString(StandardCharsets.UTF_8);
}
Also used : CommandArgs(io.pravega.cli.admin.CommandArgs) PrintStream(java.io.PrintStream) AdminCommand(io.pravega.cli.admin.AdminCommand) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Parser(io.pravega.cli.admin.Parser)

Aggregations

AdminCommand (io.pravega.cli.admin.AdminCommand)1 CommandArgs (io.pravega.cli.admin.CommandArgs)1 Parser (io.pravega.cli.admin.Parser)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 PrintStream (java.io.PrintStream)1