use of org.neo4j.shell.ConnectionConfig 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());
}
use of org.neo4j.shell.ConnectionConfig 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();
}
use of org.neo4j.shell.ConnectionConfig in project neo4j by neo4j.
the class BoltStateHandlerTest method fallbackTest.
private void fallbackTest(String initialScheme, String fallbackScheme, Runnable failer) throws CommandException {
final String[] uriScheme = new String[1];
RecordingDriverProvider provider = new RecordingDriverProvider() {
@Override
public Driver apply(String uri, AuthToken authToken, Config config) {
uriScheme[0] = uri.substring(0, uri.indexOf(':'));
if (uriScheme[0].equals(initialScheme)) {
failer.run();
}
super.apply(uri, authToken, config);
return new FakeDriver();
}
};
BoltStateHandler handler = new BoltStateHandler(provider, false);
ConnectionConfig config = new ConnectionConfig(initialScheme, "", -1, "", "", Encryption.DEFAULT, ABSENT_DB_NAME);
handler.connect(config);
assertEquals(fallbackScheme, uriScheme[0]);
}
use of org.neo4j.shell.ConnectionConfig in project neo4j by neo4j.
the class BoltStateHandlerTest method turnOnEncryptionIfRequested.
@Test
public void turnOnEncryptionIfRequested() throws CommandException {
RecordingDriverProvider provider = new RecordingDriverProvider();
BoltStateHandler handler = new BoltStateHandler(provider, false);
ConnectionConfig config = new ConnectionConfig("bolt", "", -1, "", "", Encryption.TRUE, ABSENT_DB_NAME);
handler.connect(config);
assertTrue(provider.config.encrypted());
}
use of org.neo4j.shell.ConnectionConfig in project neo4j by neo4j.
the class BoltStateHandler method connect.
@Override
public ConnectionConfig connect(@Nonnull ConnectionConfig connectionConfig, ThrowingAction<CommandException> command) throws CommandException {
if (isConnected()) {
throw new CommandException("Already connected");
}
final AuthToken authToken = AuthTokens.basic(connectionConfig.username(), connectionConfig.password());
try {
String previousDatabaseName = activeDatabaseNameAsSetByUser;
try {
activeDatabaseNameAsSetByUser = connectionConfig.database();
driver = getDriver(connectionConfig, authToken);
reconnect(activeDatabaseNameAsSetByUser, previousDatabaseName, command);
} catch (ServiceUnavailableException | SessionExpiredException e) {
String scheme = connectionConfig.scheme();
String fallbackScheme;
switch(scheme) {
case Scheme.NEO4J_URI_SCHEME:
fallbackScheme = Scheme.BOLT_URI_SCHEME;
break;
case Scheme.NEO4J_LOW_TRUST_URI_SCHEME:
fallbackScheme = Scheme.BOLT_LOW_TRUST_URI_SCHEME;
break;
case Scheme.NEO4J_HIGH_TRUST_URI_SCHEME:
fallbackScheme = Scheme.BOLT_HIGH_TRUST_URI_SCHEME;
break;
default:
throw e;
}
connectionConfig = new ConnectionConfig(fallbackScheme, connectionConfig.host(), connectionConfig.port(), connectionConfig.username(), connectionConfig.password(), connectionConfig.encryption(), connectionConfig.database());
try {
driver = getDriver(connectionConfig, authToken);
reconnect(activeDatabaseNameAsSetByUser, previousDatabaseName, command);
} catch (Throwable fallbackThrowable) {
// Throw the original exception to not cause confusion.
throw e;
}
}
} catch (Throwable t) {
try {
silentDisconnect();
} catch (Exception e) {
t.addSuppressed(e);
}
throw t;
}
return connectionConfig;
}
Aggregations