Search in sources :

Example 1 with Bookmark

use of org.neo4j.driver.Bookmark 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 2 with Bookmark

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

Example 3 with Bookmark

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

the class BoltStateHandler method closeSession.

/**
 * Closes the session, if there is any. Saves a bookmark for the database currently connected to.
 *
 * @param databaseName the name of the database currently connected to
 */
private void closeSession(String databaseName) {
    if (session != null) {
        // Save the last bookmark and close the session
        final Bookmark bookmarkForPreviousDB = session.lastBookmark();
        session.close();
        bookmarks.put(databaseName, bookmarkForPreviousDB);
    }
}
Also used : Bookmark(org.neo4j.driver.Bookmark)

Example 4 with Bookmark

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

the class BoltStateHandler method reconnect.

private void reconnect(String databaseToConnectTo, String previousDatabase, ThrowingAction<CommandException> command) throws CommandException {
    SessionConfig.Builder builder = SessionConfig.builder();
    builder.withDefaultAccessMode(AccessMode.WRITE);
    if (!ABSENT_DB_NAME.equals(databaseToConnectTo)) {
        builder.withDatabase(databaseToConnectTo);
    }
    closeSession(previousDatabase);
    final Bookmark bookmarkForDBToConnectTo = bookmarks.get(databaseToConnectTo);
    if (bookmarkForDBToConnectTo != null) {
        builder.withBookmarks(bookmarkForDBToConnectTo);
    }
    session = driver.session(builder.build());
    // Set this to null first in case run throws an exception
    resetActualDbName();
    connect(command);
}
Also used : Bookmark(org.neo4j.driver.Bookmark) SessionConfig(org.neo4j.driver.SessionConfig)

Aggregations

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