use of org.neo4j.shell.exception.CommandException in project neo4j by neo4j.
the class CypherShellTest method setWhenOfflineShouldWork.
@Test
public void setWhenOfflineShouldWork() throws ParameterException, CommandException {
CypherShell shell = new OfflineTestShell(logger, mockedBoltStateHandler, mockedPrettyPrinter);
when(mockedBoltStateHandler.isConnected()).thenReturn(false);
when(mockedBoltStateHandler.runCypher(anyString(), anyMap())).thenThrow(new CommandException("not connected"));
Object result = shell.getParameterMap().setParameter("bob", "99");
assertEquals(99L, result);
}
use of org.neo4j.shell.exception.CommandException in project neo4j by neo4j.
the class BoltStateHandlerTest method exceptionFromRunQueryDoesNotResetActualDatabaseNameToUnresolved.
@Test
public void exceptionFromRunQueryDoesNotResetActualDatabaseNameToUnresolved() throws CommandException {
Session sessionMock = mock(Session.class);
Result resultMock = mock(Result.class);
Driver driverMock = stubResultSummaryInAnOpenSession(resultMock, sessionMock, "9.4.1-ALPHA", "my_default_db");
ClientException databaseNotFound = new ClientException("Neo.ClientError.Database.DatabaseNotFound", "blah");
when(sessionMock.run(any(Query.class))).thenThrow(databaseNotFound).thenReturn(resultMock);
BoltStateHandler handler = new BoltStateHandler((s, authToken, config) -> driverMock, false);
handler.connect(config);
try {
handler.runCypher("RETURN \"hello\"", Collections.emptyMap());
fail("should fail on runCypher");
} catch (Exception e) {
assertThat(e, is(databaseNotFound));
assertEquals("my_default_db", handler.getActualDatabaseAsReportedByServer());
}
}
use of org.neo4j.shell.exception.CommandException in project neo4j by neo4j.
the class BoltStateHandler method connect.
@Override
public ConnectionConfig connect(@Nonnull ConnectionConfig connectionConfig, ThrowingAction<CommandException> command) throws CommandException {
if (isConnected()) {
throw new CommandException("Already connected");
}
final AuthToken authToken = AuthTokens.basic(connectionConfig.username(), connectionConfig.password());
try {
String previousDatabaseName = activeDatabaseNameAsSetByUser;
try {
activeDatabaseNameAsSetByUser = connectionConfig.database();
driver = getDriver(connectionConfig, authToken);
reconnect(activeDatabaseNameAsSetByUser, previousDatabaseName, command);
} catch (ServiceUnavailableException | SessionExpiredException e) {
String scheme = connectionConfig.scheme();
String fallbackScheme;
switch(scheme) {
case Scheme.NEO4J_URI_SCHEME:
fallbackScheme = Scheme.BOLT_URI_SCHEME;
break;
case Scheme.NEO4J_LOW_TRUST_URI_SCHEME:
fallbackScheme = Scheme.BOLT_LOW_TRUST_URI_SCHEME;
break;
case Scheme.NEO4J_HIGH_TRUST_URI_SCHEME:
fallbackScheme = Scheme.BOLT_HIGH_TRUST_URI_SCHEME;
break;
default:
throw e;
}
connectionConfig = new ConnectionConfig(fallbackScheme, connectionConfig.host(), connectionConfig.port(), connectionConfig.username(), connectionConfig.password(), connectionConfig.encryption(), connectionConfig.database());
try {
driver = getDriver(connectionConfig, authToken);
reconnect(activeDatabaseNameAsSetByUser, previousDatabaseName, command);
} catch (Throwable fallbackThrowable) {
// Throw the original exception to not cause confusion.
throw e;
}
}
} catch (Throwable t) {
try {
silentDisconnect();
} catch (Exception e) {
t.addSuppressed(e);
}
throw t;
}
return connectionConfig;
}
use of org.neo4j.shell.exception.CommandException in project neo4j by neo4j.
the class Source method execute.
@Override
public void execute(@Nonnull final String argString) throws ExitException, CommandException {
String filename = simpleArgParse(argString, 1, 1, COMMAND_NAME, getUsage())[0];
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(filename)))) {
bufferedReader.lines().forEach(line -> statementParser.parseMoreText(line + "\n"));
List<String> statements = statementParser.consumeStatements();
// Executing this could fail but we try anyway to avoid hiding errors
statementParser.incompleteStatement().ifPresent(statements::add);
for (String statement : statements) {
cypherShell.execute(statement);
}
} catch (IOException e) {
throw new CommandException(format("Cannot find file: '%s'", filename), e);
}
}
use of org.neo4j.shell.exception.CommandException in project neo4j by neo4j.
the class CypherShellVerboseIntegrationTest method cypherWithOrder.
@Test
public void cypherWithOrder() throws CommandException {
// given
String serverVersion = shell.getServerVersion();
assumeThat(version(serverVersion), greaterThanOrEqualTo(version("3.6")));
// Make sure we are creating a new NEW index
try {
shell.execute("DROP INDEX ON :Person(age)");
} catch (Exception e) {
// ignore if the index didn't exist
}
shell.execute("CREATE INDEX ON :Person(age)");
shell.execute("CALL db.awaitIndexes()");
// when
shell.execute("CYPHER RUNTIME=INTERPRETED EXPLAIN MATCH (n:Person) WHERE n.age >= 18 RETURN n.name, n.age ORDER BY n.age");
// then
String actual = linePrinter.output();
assertThat(actual, containsString("Order"));
assertThat(actual, containsString("n.age ASC"));
}
Aggregations