Search in sources :

Example 6 with ConnectionConfig

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());
}
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 7 with ConnectionConfig

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();
}
Also used : ShellStatementParser(org.neo4j.shell.parser.ShellStatementParser) StatementExecuter(org.neo4j.shell.StatementExecuter) DatabaseManager(org.neo4j.shell.DatabaseManager) TransactionHandler(org.neo4j.shell.TransactionHandler) ClientException(org.neo4j.driver.exceptions.ClientException) Logger(org.neo4j.shell.log.Logger) AnsiLogger(org.neo4j.shell.log.AnsiLogger) ConnectionConfig(org.neo4j.shell.ConnectionConfig) UserMessagesHandler(org.neo4j.shell.UserMessagesHandler) Before(org.junit.Before)

Example 8 with ConnectionConfig

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]);
}
Also used : ConnectionConfig(org.neo4j.shell.ConnectionConfig) Config(org.neo4j.driver.Config) SessionConfig(org.neo4j.driver.SessionConfig) AuthToken(org.neo4j.driver.AuthToken) FakeDriver(org.neo4j.shell.test.bolt.FakeDriver) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ConnectionConfig(org.neo4j.shell.ConnectionConfig)

Example 9 with ConnectionConfig

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());
}
Also used : ConnectionConfig(org.neo4j.shell.ConnectionConfig) Test(org.junit.Test)

Example 10 with ConnectionConfig

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;
}
Also used : AuthToken(org.neo4j.driver.AuthToken) SessionExpiredException(org.neo4j.driver.exceptions.SessionExpiredException) CommandException(org.neo4j.shell.exception.CommandException) ServiceUnavailableException(org.neo4j.driver.exceptions.ServiceUnavailableException) ConnectionConfig(org.neo4j.shell.ConnectionConfig) SessionExpiredException(org.neo4j.driver.exceptions.SessionExpiredException) Versions.isPasswordChangeRequiredException(org.neo4j.shell.util.Versions.isPasswordChangeRequiredException) ServiceUnavailableException(org.neo4j.driver.exceptions.ServiceUnavailableException) ClientException(org.neo4j.driver.exceptions.ClientException) Neo4jException(org.neo4j.driver.exceptions.Neo4jException) CommandException(org.neo4j.shell.exception.CommandException)

Aggregations

ConnectionConfig (org.neo4j.shell.ConnectionConfig)13 Test (org.junit.Test)8 CypherShell (org.neo4j.shell.CypherShell)6 ShellParameterMap (org.neo4j.shell.ShellParameterMap)6 PrettyConfig (org.neo4j.shell.prettyprint.PrettyConfig)6 StringLinePrinter (org.neo4j.shell.StringLinePrinter)4 ClientException (org.neo4j.driver.exceptions.ClientException)3 FakeDriver (org.neo4j.shell.test.bolt.FakeDriver)3 Before (org.junit.Before)2 AuthToken (org.neo4j.driver.AuthToken)2 Bookmark (org.neo4j.driver.Bookmark)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 FakeSession (org.neo4j.shell.test.bolt.FakeSession)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1 Config (org.neo4j.driver.Config)1