Search in sources :

Example 6 with CommandHelper

use of org.neo4j.shell.commands.CommandHelper in project neo4j by neo4j.

the class InteractiveShellRunnerTest method setupInteractiveTestShellRunner.

private TestInteractiveShellRunner setupInteractiveTestShellRunner(String input) throws Exception {
    // NOTE: Tests using this will test a bit more of the stack using OfflineTestShell
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    ByteArrayOutputStream error = new ByteArrayOutputStream();
    BoltStateHandler mockedBoltStateHandler = mock(BoltStateHandler.class);
    when(mockedBoltStateHandler.getProtocolVersion()).thenReturn("");
    final PrettyPrinter mockedPrettyPrinter = mock(PrettyPrinter.class);
    Logger logger = new AnsiLogger(false, Format.VERBOSE, new PrintStream(output), new PrintStream(error));
    OfflineTestShell offlineTestShell = new OfflineTestShell(logger, mockedBoltStateHandler, mockedPrettyPrinter);
    CommandHelper commandHelper = new CommandHelper(logger, Historian.empty, offlineTestShell);
    offlineTestShell.setCommandHelper(commandHelper);
    InputStream inputStream = new ByteArrayInputStream(input.getBytes());
    InteractiveShellRunner runner = new InteractiveShellRunner(offlineTestShell, offlineTestShell, offlineTestShell, logger, new ShellStatementParser(), inputStream, historyFile, userMessagesHandler, connectionConfig);
    return new TestInteractiveShellRunner(runner, output, error, mockedBoltStateHandler);
}
Also used : PrintStream(java.io.PrintStream) ShellStatementParser(org.neo4j.shell.parser.ShellStatementParser) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OfflineTestShell(org.neo4j.shell.OfflineTestShell) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Logger(org.neo4j.shell.log.Logger) AnsiLogger(org.neo4j.shell.log.AnsiLogger) AnsiLogger(org.neo4j.shell.log.AnsiLogger) CommandHelper(org.neo4j.shell.commands.CommandHelper) PrettyPrinter(org.neo4j.shell.prettyprint.PrettyPrinter) ByteArrayInputStream(java.io.ByteArrayInputStream) BoltStateHandler(org.neo4j.shell.state.BoltStateHandler)

Example 7 with CommandHelper

use of org.neo4j.shell.commands.CommandHelper in project neo4j by neo4j.

the class Main method runShell.

int runShell(@Nonnull CliArgs cliArgs, @Nonnull CypherShell shell, Logger logger) {
    ConnectionConfig connectionConfig = new ConnectionConfig(cliArgs.getScheme(), cliArgs.getHost(), cliArgs.getPort(), cliArgs.getUsername(), cliArgs.getPassword(), cliArgs.getEncryption(), cliArgs.getDatabase());
    try {
        // If user is passing in a cypher statement just run that and be done with it
        if (cliArgs.getCypher().isPresent()) {
            // Can only prompt for password if input has not been redirected
            connectMaybeInteractively(shell, connectionConfig, !cliArgs.getNonInteractive() && isInputInteractive(), !cliArgs.getNonInteractive() && isOutputInteractive(), !cliArgs.getNonInteractive(), /*Don't ask for password if using --non-interactive*/
            () -> shell.execute(cliArgs.getCypher().get()));
            return EXIT_SUCCESS;
        } else {
            // Can only prompt for password if input has not been redirected
            var newConnectionConfig = connectMaybeInteractively(shell, connectionConfig, !cliArgs.getNonInteractive() && isInputInteractive(), !cliArgs.getNonInteractive() && isOutputInteractive(), !cliArgs.getNonInteractive());
            if (!newConnectionConfig.driverUrl().equals(connectionConfig.driverUrl())) {
                var fallbackWarning = "Failed to connect to " + connectionConfig.driverUrl() + ", fallback to " + newConnectionConfig.driverUrl();
                logger.printIfVerbose(AnsiFormattedText.s().colorOrange().append(fallbackWarning).formattedString());
            }
            // Construct shellrunner after connecting, due to interrupt handling
            ShellRunner shellRunner = ShellRunner.getShellRunner(cliArgs, shell, logger, newConnectionConfig);
            CommandHelper commandHelper = new CommandHelper(logger, shellRunner.getHistorian(), shell);
            shell.setCommandHelper(commandHelper);
            return shellRunner.runUntilEnd();
        }
    } catch (Throwable e) {
        logger.printError(e);
        return EXIT_FAILURE;
    }
}
Also used : CommandHelper(org.neo4j.shell.commands.CommandHelper)

Example 8 with CommandHelper

use of org.neo4j.shell.commands.CommandHelper in project neo4j by neo4j.

the class CypherShellTest method setup.

@Before
public void setup() {
    when(mockedBoltStateHandler.getProtocolVersion()).thenReturn("");
    doReturn(System.out).when(logger).getOutputStream();
    offlineTestShell = new OfflineTestShell(logger, mockedBoltStateHandler, mockedPrettyPrinter);
    CommandHelper commandHelper = new CommandHelper(logger, Historian.empty, offlineTestShell);
    offlineTestShell.setCommandHelper(commandHelper);
}
Also used : CommandHelper(org.neo4j.shell.commands.CommandHelper) Before(org.junit.Before)

Example 9 with CommandHelper

use of org.neo4j.shell.commands.CommandHelper in project neo4j by neo4j.

the class MainIntegrationTest method startsAgainstSystemDatabaseWhenDefaultDatabaseUnavailableIfInteractive.

@Test
public void startsAgainstSystemDatabaseWhenDefaultDatabaseUnavailableIfInteractive() throws Exception {
    shell.setCommandHelper(new CommandHelper(mock(Logger.class), Historian.empty, shell));
    assertEquals("", connectionConfig.username());
    assertEquals("", connectionConfig.password());
    // when
    main.connectMaybeInteractively(shell, connectionConfig, true, true, true);
    // Multiple databases are only available from 4.0
    assumeTrue(majorVersion(shell.getServerVersion()) >= 4);
    // then
    // should be connected
    assertTrue(shell.isConnected());
    // should have prompted and set the username and password
    String expectedLoginOutput = format("username: neo4j%npassword: ***%n");
    assertEquals(expectedLoginOutput, baos.toString());
    assertEquals("neo4j", connectionConfig.username());
    assertEquals("neo", connectionConfig.password());
    // Stop the default database
    shell.execute(":use " + SYSTEM_DB_NAME);
    shell.execute("STOP DATABASE " + DatabaseManager.DEFAULT_DEFAULT_DB_NAME);
    try {
        shell.disconnect();
        // Connect to system database
        CliArgs cliArgs = new CliArgs();
        cliArgs.setUsername("neo4j", "");
        cliArgs.setPassword("neo", "");
        cliArgs.setDatabase("system");
        ShellAndConnection sac = getShell(cliArgs);
        // Use the new shell and connection config from here on
        shell = sac.shell;
        connectionConfig = sac.connectionConfig;
        main.connectMaybeInteractively(shell, connectionConfig, true, false, true);
        // then
        assertTrue(shell.isConnected());
    } finally {
        // Start the default database again
        ensureDefaultDatabaseStarted();
    }
}
Also used : CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) CommandHelper(org.neo4j.shell.commands.CommandHelper) CliArgs(org.neo4j.shell.cli.CliArgs) Test(org.junit.Test)

Example 10 with CommandHelper

use of org.neo4j.shell.commands.CommandHelper in project neo4j by neo4j.

the class MainIntegrationTest method switchingToUnavailableDefaultDatabaseIfInteractive.

@Test
public void switchingToUnavailableDefaultDatabaseIfInteractive() throws Exception {
    shell.setCommandHelper(new CommandHelper(mock(Logger.class), Historian.empty, shell));
    inputBuffer.put(String.format("neo4j%nneo%n").getBytes());
    assertEquals("", connectionConfig.username());
    assertEquals("", connectionConfig.password());
    // when
    main.connectMaybeInteractively(shell, connectionConfig, true, true, true);
    // Multiple databases are only available from 4.0
    assumeTrue(majorVersion(shell.getServerVersion()) >= 4);
    // then
    // should be connected
    assertTrue(shell.isConnected());
    // should have prompted and set the username and password
    String expectedLoginOutput = format("username: neo4j%npassword: ***%n");
    assertEquals(expectedLoginOutput, baos.toString());
    assertEquals("neo4j", connectionConfig.username());
    assertEquals("neo", connectionConfig.password());
    // Stop the default database
    shell.execute(":use " + SYSTEM_DB_NAME);
    shell.execute("STOP DATABASE " + DatabaseManager.DEFAULT_DEFAULT_DB_NAME);
    try {
        // Should get exception that database is unavailable when trying to connect
        shell.execute(":use");
        fail("No exception thrown");
    } catch (TransientException | ServiceUnavailableException e) {
        expectDatabaseUnavailable(e, "neo4j");
    } finally {
        // Start the default database again
        ensureDefaultDatabaseStarted();
    }
}
Also used : TransientException(org.neo4j.driver.exceptions.TransientException) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ServiceUnavailableException(org.neo4j.driver.exceptions.ServiceUnavailableException) CommandHelper(org.neo4j.shell.commands.CommandHelper) Test(org.junit.Test)

Aggregations

CommandHelper (org.neo4j.shell.commands.CommandHelper)10 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)5 Test (org.junit.Test)5 ServiceUnavailableException (org.neo4j.driver.exceptions.ServiceUnavailableException)3 TransientException (org.neo4j.driver.exceptions.TransientException)3 CliArgs (org.neo4j.shell.cli.CliArgs)2 AnsiLogger (org.neo4j.shell.log.AnsiLogger)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1 PrintStream (java.io.PrintStream)1 Before (org.junit.Before)1 CypherShell (org.neo4j.shell.CypherShell)1 OfflineTestShell (org.neo4j.shell.OfflineTestShell)1 ShellParameterMap (org.neo4j.shell.ShellParameterMap)1 Begin (org.neo4j.shell.commands.Begin)1 Command (org.neo4j.shell.commands.Command)1 Logger (org.neo4j.shell.log.Logger)1 ShellStatementParser (org.neo4j.shell.parser.ShellStatementParser)1 PrettyConfig (org.neo4j.shell.prettyprint.PrettyConfig)1