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));
}
}
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());
}
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");
}
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());
}
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());
}
Aggregations