Search in sources :

Example 1 with Session

use of org.neo4j.driver.Session in project neo4j by neo4j.

the class BoltStateHandlerTest method exceptionsFromSilentDisconnectAreSuppressedToReportOriginalErrors.

@Test
public void exceptionsFromSilentDisconnectAreSuppressedToReportOriginalErrors() {
    Session session = mock(Session.class);
    Result resultMock = mock(Result.class);
    RuntimeException originalException = new RuntimeException("original exception");
    RuntimeException thrownFromSilentDisconnect = new RuntimeException("exception from silent disconnect");
    Driver mockedDriver = stubResultSummaryInAnOpenSession(resultMock, session, "neo4j-version");
    OfflineBoltStateHandler boltStateHandler = new OfflineBoltStateHandler(mockedDriver);
    when(resultMock.consume()).thenThrow(originalException);
    doThrow(thrownFromSilentDisconnect).when(session).close();
    try {
        boltStateHandler.connect();
        fail("should fail on silent disconnect");
    } catch (Exception e) {
        assertThat(e.getSuppressed()[0], is(thrownFromSilentDisconnect));
        assertThat(e, is(originalException));
    }
}
Also used : Driver(org.neo4j.driver.Driver) FakeDriver(org.neo4j.shell.test.bolt.FakeDriver) 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 2 with Session

use of org.neo4j.driver.Session in project neo4j by neo4j.

the class BoltStateHandlerTest method shouldChangePasswordAndKeepSystemDbBookmark.

@Test
public void shouldChangePasswordAndKeepSystemDbBookmark() throws CommandException {
    // Given
    ConnectionConfig config = new ConnectionConfig("bolt", "", -1, "", "", Encryption.DEFAULT, ABSENT_DB_NAME);
    config.setNewPassword("newPW");
    Bookmark bookmark = InternalBookmark.parse("myBookmark");
    Session sessionMock = mock(Session.class);
    Result resultMock = mock(Result.class);
    Driver driverMock = stubResultSummaryInAnOpenSession(resultMock, sessionMock, "Neo4j/9.4.1-ALPHA", "my_default_db");
    when(sessionMock.run("CALL dbms.security.changePassword($n)", Values.parameters("n", config.newPassword()))).thenReturn(resultMock);
    when(sessionMock.lastBookmark()).thenReturn(bookmark);
    BoltStateHandler handler = new OfflineBoltStateHandler(driverMock);
    // When
    handler.changePassword(config);
    // Then
    assertEquals("newPW", config.password());
    assertNull(config.newPassword());
    assertNull(handler.session);
    // When connecting to system db again
    handler.connect(new ConnectionConfig("bolt", "", -1, "", "", Encryption.DEFAULT, SYSTEM_DB_NAME));
    // Then use bookmark for system DB
    verify(driverMock).session(SessionConfig.builder().withDefaultAccessMode(AccessMode.WRITE).withDatabase(SYSTEM_DB_NAME).withBookmarks(bookmark).build());
}
Also used : InternalBookmark(org.neo4j.driver.internal.InternalBookmark) Bookmark(org.neo4j.driver.Bookmark) Driver(org.neo4j.driver.Driver) FakeDriver(org.neo4j.shell.test.bolt.FakeDriver) ConnectionConfig(org.neo4j.shell.ConnectionConfig) Session(org.neo4j.driver.Session) FakeSession(org.neo4j.shell.test.bolt.FakeSession) Result(org.neo4j.driver.Result) Test(org.junit.Test)

Example 3 with Session

use of org.neo4j.driver.Session in project neo4j by neo4j.

the class BoltStateHandlerTest method fallbackToLegacyPing.

@Test
public void fallbackToLegacyPing() throws CommandException {
    // given
    Session sessionMock = mock(Session.class);
    Result failing = mock(Result.class);
    Result other = mock(Result.class, RETURNS_DEEP_STUBS);
    when(failing.consume()).thenThrow(new ClientException("Neo.ClientError.Procedure.ProcedureNotFound", "No procedure CALL db.ping(()"));
    when(sessionMock.run("CALL db.ping()")).thenReturn(failing);
    when(sessionMock.run("RETURN 1")).thenReturn(other);
    Driver driverMock = mock(Driver.class);
    when(driverMock.session(any())).thenReturn(sessionMock);
    OfflineBoltStateHandler boltStateHandler = new OfflineBoltStateHandler(driverMock);
    // when
    boltStateHandler.connect();
    // then
    verify(sessionMock).run("RETURN 1");
}
Also used : Driver(org.neo4j.driver.Driver) FakeDriver(org.neo4j.shell.test.bolt.FakeDriver) 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 4 with Session

use of org.neo4j.driver.Session in project neo4j by neo4j.

the class BoltStateHandlerTest method triesAgainOnSessionExpired.

@Test
public void triesAgainOnSessionExpired() throws Exception {
    Session sessionMock = mock(Session.class);
    Result resultMock = mock(Result.class);
    Record recordMock = mock(Record.class);
    Value valueMock = mock(Value.class);
    Driver driverMock = stubResultSummaryInAnOpenSession(resultMock, sessionMock, "neo4j-version");
    when(resultMock.list()).thenReturn(singletonList(recordMock));
    when(valueMock.toString()).thenReturn("999");
    when(recordMock.get(0)).thenReturn(valueMock);
    when(sessionMock.run(any(Query.class))).thenThrow(new SessionExpiredException("leaderswitch")).thenReturn(resultMock);
    OfflineBoltStateHandler boltStateHandler = new OfflineBoltStateHandler(driverMock);
    boltStateHandler.connect();
    BoltResult boltResult = boltStateHandler.runCypher("RETURN 999", new HashMap<>()).get();
    verify(driverMock, times(2)).session(any());
    verify(sessionMock, times(2)).run(any(Query.class));
    assertEquals("999", boltResult.getRecords().get(0).get(0).toString());
}
Also used : Query(org.neo4j.driver.Query) HashMap(java.util.HashMap) Value(org.neo4j.driver.Value) Driver(org.neo4j.driver.Driver) FakeDriver(org.neo4j.shell.test.bolt.FakeDriver) Record(org.neo4j.driver.Record) SessionExpiredException(org.neo4j.driver.exceptions.SessionExpiredException) Session(org.neo4j.driver.Session) FakeSession(org.neo4j.shell.test.bolt.FakeSession) Result(org.neo4j.driver.Result) Test(org.junit.Test)

Example 5 with Session

use of org.neo4j.driver.Session in project neo4j by neo4j.

the class BoltStateHandlerTest method shouldKeepOneBookmarkPerDatabase.

@SuppressWarnings("OptionalGetWithoutIsPresent")
@Test
public void shouldKeepOneBookmarkPerDatabase() throws CommandException {
    ConnectionConfig config = new ConnectionConfig("bolt", "", -1, "", "", Encryption.DEFAULT, "database1");
    Bookmark db1Bookmark = InternalBookmark.parse("db1");
    Bookmark db2Bookmark = InternalBookmark.parse("db2");
    // A couple of these mock calls are now redundant with what is called in stubResultSummaryInAnOpenSession
    Result resultMock = mock(Result.class);
    Session db1SessionMock = mock(Session.class);
    when(db1SessionMock.isOpen()).thenReturn(true);
    when(db1SessionMock.lastBookmark()).thenReturn(db1Bookmark);
    when(db1SessionMock.run("CALL db.ping()")).thenReturn(resultMock);
    Session db2SessionMock = mock(Session.class);
    when(db2SessionMock.isOpen()).thenReturn(true);
    when(db2SessionMock.lastBookmark()).thenReturn(db2Bookmark);
    when(db2SessionMock.run("CALL db.ping()")).thenReturn(resultMock);
    Driver driverMock = stubResultSummaryInAnOpenSession(resultMock, db1SessionMock, "Neo4j/9.4.1-ALPHA", "database1");
    when(driverMock.session(any())).thenAnswer(arg -> {
        SessionConfig sc = (SessionConfig) arg.getArguments()[0];
        switch(sc.database().get()) {
            case "database1":
                return db1SessionMock;
            case "database2":
                return db2SessionMock;
            default:
                return null;
        }
    });
    BoltStateHandler handler = new OfflineBoltStateHandler(driverMock);
    // When
    handler.connect(config);
    // Then no bookmark yet for db1
    verify(driverMock).session(SessionConfig.builder().withDefaultAccessMode(AccessMode.WRITE).withDatabase("database1").build());
    // When
    handler.setActiveDatabase("database2");
    // Then no bookmark yet for db2
    verify(driverMock).session(SessionConfig.builder().withDefaultAccessMode(AccessMode.WRITE).withDatabase("database2").build());
    // When
    handler.setActiveDatabase("database1");
    // Then use bookmark for db1
    verify(driverMock).session(SessionConfig.builder().withDefaultAccessMode(AccessMode.WRITE).withDatabase("database1").withBookmarks(db1Bookmark).build());
    // When
    handler.setActiveDatabase("database2");
    // Then use bookmark for db2
    verify(driverMock).session(SessionConfig.builder().withDefaultAccessMode(AccessMode.WRITE).withDatabase("database2").withBookmarks(db2Bookmark).build());
}
Also used : InternalBookmark(org.neo4j.driver.internal.InternalBookmark) Bookmark(org.neo4j.driver.Bookmark) Driver(org.neo4j.driver.Driver) FakeDriver(org.neo4j.shell.test.bolt.FakeDriver) SessionConfig(org.neo4j.driver.SessionConfig) ConnectionConfig(org.neo4j.shell.ConnectionConfig) Result(org.neo4j.driver.Result) Session(org.neo4j.driver.Session) FakeSession(org.neo4j.shell.test.bolt.FakeSession) Test(org.junit.Test)

Aggregations

Session (org.neo4j.driver.Session)24 Driver (org.neo4j.driver.Driver)20 Result (org.neo4j.driver.Result)17 Test (org.junit.Test)13 FakeSession (org.neo4j.shell.test.bolt.FakeSession)10 FakeDriver (org.neo4j.shell.test.bolt.FakeDriver)9 Test (org.junit.jupiter.api.Test)6 SessionConfig (org.neo4j.driver.SessionConfig)6 Record (org.neo4j.driver.Record)5 SessionExpiredException (org.neo4j.driver.exceptions.SessionExpiredException)4 HashMap (java.util.HashMap)3 BeforeClass (org.junit.BeforeClass)3 ClientException (org.neo4j.driver.exceptions.ClientException)3 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 List (java.util.List)2 Map (java.util.Map)2 PipelineResult (org.apache.beam.sdk.PipelineResult)2 SerializableCoder (org.apache.beam.sdk.coders.SerializableCoder)2