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);
}
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;
}
}
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);
}
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();
}
}
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();
}
}
Aggregations