Search in sources :

Example 1 with SessionConfig

use of org.neo4j.driver.SessionConfig 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

Test (org.junit.Test)1 Bookmark (org.neo4j.driver.Bookmark)1 Driver (org.neo4j.driver.Driver)1 Result (org.neo4j.driver.Result)1 Session (org.neo4j.driver.Session)1 SessionConfig (org.neo4j.driver.SessionConfig)1 InternalBookmark (org.neo4j.driver.internal.InternalBookmark)1 ConnectionConfig (org.neo4j.shell.ConnectionConfig)1 FakeDriver (org.neo4j.shell.test.bolt.FakeDriver)1 FakeSession (org.neo4j.shell.test.bolt.FakeSession)1