Search in sources :

Example 6 with ClientException

use of org.neo4j.driver.exceptions.ClientException in project neo4j by neo4j.

the class InteractiveShellRunnerTest method setup.

@Before
public void setup() throws Exception {
    statementParser = new ShellStatementParser();
    logger = mock(Logger.class);
    cmdExecuter = mock(StatementExecuter.class);
    txHandler = mock(TransactionHandler.class);
    databaseManager = mock(DatabaseManager.class);
    connectionConfig = mock(ConnectionConfig.class);
    historyFile = temp.newFile();
    badLineError = new ClientException("Found a bad line");
    userMessagesHandler = mock(UserMessagesHandler.class);
    when(databaseManager.getActualDatabaseAsReportedByServer()).thenReturn("mydb");
    when(userMessagesHandler.getWelcomeMessage()).thenReturn("Welcome to cypher-shell!");
    when(userMessagesHandler.getExitMessage()).thenReturn("Exit message");
    when(connectionConfig.username()).thenReturn("myusername");
    doThrow(badLineError).when(cmdExecuter).execute(contains("bad"));
    doReturn(System.out).when(logger).getOutputStream();
}
Also used : ShellStatementParser(org.neo4j.shell.parser.ShellStatementParser) StatementExecuter(org.neo4j.shell.StatementExecuter) DatabaseManager(org.neo4j.shell.DatabaseManager) TransactionHandler(org.neo4j.shell.TransactionHandler) ClientException(org.neo4j.driver.exceptions.ClientException) Logger(org.neo4j.shell.log.Logger) AnsiLogger(org.neo4j.shell.log.AnsiLogger) ConnectionConfig(org.neo4j.shell.ConnectionConfig) UserMessagesHandler(org.neo4j.shell.UserMessagesHandler) Before(org.junit.Before)

Example 7 with ClientException

use of org.neo4j.driver.exceptions.ClientException in project neo4j by neo4j.

the class BoltStateHandlerTest method handleErrorsOnRollback.

@Test
public void handleErrorsOnRollback() throws CommandException {
    reset(mockDriver);
    var mockSession = spy(FakeSession.class);
    var mockTx = mock(Transaction.class);
    doThrow(new ClientException("Failed to rollback :(")).when(mockTx).rollback();
    when(mockSession.beginTransaction()).thenReturn(mockTx);
    when(mockDriver.session(any())).thenReturn(mockSession);
    boltStateHandler.connect();
    boltStateHandler.beginTransaction();
    assertThrows(ClientException.class, boltStateHandler::rollbackTransaction);
    assertFalse(boltStateHandler.isTransactionOpen());
}
Also used : ClientException(org.neo4j.driver.exceptions.ClientException) Test(org.junit.Test)

Example 8 with ClientException

use of org.neo4j.driver.exceptions.ClientException 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());
    }
}
Also used : Driver(org.neo4j.driver.Driver) FakeDriver(org.neo4j.shell.test.bolt.FakeDriver) ClientException(org.neo4j.driver.exceptions.ClientException) SessionExpiredException(org.neo4j.driver.exceptions.SessionExpiredException) CommandException(org.neo4j.shell.exception.CommandException) ServiceUnavailableException(org.neo4j.driver.exceptions.ServiceUnavailableException) ClientException(org.neo4j.driver.exceptions.ClientException) Session(org.neo4j.driver.Session) FakeSession(org.neo4j.shell.test.bolt.FakeSession) Result(org.neo4j.driver.Result) Test(org.junit.Test)

Example 9 with ClientException

use of org.neo4j.driver.exceptions.ClientException in project neo4j by neo4j.

the class BoltStateHandler method getPing.

private ThrowingAction<CommandException> getPing() {
    return () -> {
        try {
            Result run = session.run("CALL db.ping()");
            ResultSummary summary = run.consume();
            BoltStateHandler.this.protocolVersion = summary.server().protocolVersion();
            updateActualDbName(summary);
        } catch (ClientException e) {
            // In older versions there is no db.ping procedure, use legacy method.
            if (procedureNotFound(e)) {
                Result run = session.run(isSystemDb() ? "CALL db.indexes()" : "RETURN 1");
                ResultSummary summary = run.consume();
                BoltStateHandler.this.protocolVersion = summary.server().protocolVersion();
                updateActualDbName(summary);
            } else {
                throw e;
            }
        }
    };
}
Also used : ResultSummary(org.neo4j.driver.summary.ResultSummary) ClientException(org.neo4j.driver.exceptions.ClientException) Result(org.neo4j.driver.Result)

Example 10 with ClientException

use of org.neo4j.driver.exceptions.ClientException in project neo4j by neo4j.

the class InteractiveShellRunnerTest method testSwitchToNonExistingDatabase.

@Test
public void testSwitchToNonExistingDatabase() throws Exception {
    // given
    String input = ":use foo;\n";
    TestInteractiveShellRunner sr = setupInteractiveTestShellRunner(input);
    // when
    when(sr.mockedBoltStateHandler.getActualDatabaseAsReportedByServer()).thenReturn("mydb");
    doThrow(new ClientException("Non existing")).when(sr.mockedBoltStateHandler).setActiveDatabase("foo");
    sr.runner.runUntilEnd();
    // then
    assertThat(sr.output.toString(), containsString("myusername@mydb> "));
    assertThat(sr.error.toString(), containsString("Non existing"));
}
Also used : CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ClientException(org.neo4j.driver.exceptions.ClientException) Test(org.junit.Test)

Aggregations

ClientException (org.neo4j.driver.exceptions.ClientException)11 Test (org.junit.Test)6 Result (org.neo4j.driver.Result)3 Before (org.junit.Before)2 Driver (org.neo4j.driver.Driver)2 Session (org.neo4j.driver.Session)2 ServiceUnavailableException (org.neo4j.driver.exceptions.ServiceUnavailableException)2 ConnectionConfig (org.neo4j.shell.ConnectionConfig)2 ShellStatementParser (org.neo4j.shell.parser.ShellStatementParser)2 FakeDriver (org.neo4j.shell.test.bolt.FakeDriver)2 FakeSession (org.neo4j.shell.test.bolt.FakeSession)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 PrintStream (java.io.PrintStream)1 Nonnull (javax.annotation.Nonnull)1 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)1 Test (org.junit.jupiter.api.Test)1 DiscoveryException (org.neo4j.driver.exceptions.DiscoveryException)1 SessionExpiredException (org.neo4j.driver.exceptions.SessionExpiredException)1 ResultSummary (org.neo4j.driver.summary.ResultSummary)1 CypherShell (org.neo4j.shell.CypherShell)1