Search in sources :

Example 1 with Result

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

the class TableOutputFormatterTest method wrapStringContentWithTwoColumns.

@Test
public void wrapStringContentWithTwoColumns() {
    // GIVEN
    Result result = mockResult(asList("c1", "c2"), "a", "b", "aa", "bb", "aaa", "b", "a", "bbb", "aaaa", "bb", "aa", "bbbb", "aaaaa", "bbbbb");
    // WHEN
    ToStringLinePrinter printer = new ToStringLinePrinter();
    new TableOutputFormatter(true, 2).formatAndCount(new ListBoltResult(result.list(), result.consume()), printer);
    String table = printer.result();
    // THEN
    assertThat(table, is(String.join(NEWLINE, "+-------------+", "| c1   | c2   |", "+-------------+", "| \"a\"  | \"b\"  |", "| \"aa\" | \"bb\" |", "| \"aaa | \"b\"  |", "\\ \"    |      |", "| \"a\"  | \"bbb |", "|      \\ \"    |", "| \"aaa | \"bb\" |", "\\ a\"   |      |", "| \"aa\" | \"bbb |", "|      \\ b\"   |", "| \"aaa | \"bbb |", "\\ aa\"  \\ bb\"  |", "+-------------+", NEWLINE)));
}
Also used : ListBoltResult(org.neo4j.shell.state.ListBoltResult) StringContains.containsString(org.hamcrest.core.StringContains.containsString) BoltResult(org.neo4j.shell.state.BoltResult) ListBoltResult(org.neo4j.shell.state.ListBoltResult) Result(org.neo4j.driver.Result) Test(org.junit.Test)

Example 2 with Result

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

the class TableOutputFormatterTest method mockResult.

private Result mockResult(List<String> cols, Object... data) {
    Result result = mock(Result.class);
    Query query = mock(Query.class);
    ResultSummary summary = mock(ResultSummary.class);
    when(summary.query()).thenReturn(query);
    when(result.keys()).thenReturn(cols);
    List<Record> records = new ArrayList<>();
    List<Object> input = asList(data);
    int width = cols.size();
    for (int row = 0; row < input.size() / width; row++) {
        records.add(record(cols, input.subList(row * width, (row + 1) * width)));
    }
    when(result.list()).thenReturn(records);
    when(result.consume()).thenReturn(summary);
    when(result.consume()).thenReturn(summary);
    return result;
}
Also used : Query(org.neo4j.driver.Query) ResultSummary(org.neo4j.driver.summary.ResultSummary) ArrayList(java.util.ArrayList) InternalRecord(org.neo4j.driver.internal.InternalRecord) Record(org.neo4j.driver.Record) Matchers.anyObject(org.mockito.Matchers.anyObject) BoltResult(org.neo4j.shell.state.BoltResult) ListBoltResult(org.neo4j.shell.state.ListBoltResult) Result(org.neo4j.driver.Result)

Example 3 with Result

use of org.neo4j.driver.Result 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));
    }
}
Also used : Driver(org.neo4j.driver.Driver) FakeDriver(org.neo4j.shell.test.bolt.FakeDriver) SessionExpiredException(org.neo4j.driver.exceptions.SessionExpiredException) CommandException(org.neo4j.shell.exception.CommandException) ServiceUnavailableException(org.neo4j.driver.exceptions.ServiceUnavailableException) ClientException(org.neo4j.driver.exceptions.ClientException) Session(org.neo4j.driver.Session) FakeSession(org.neo4j.shell.test.bolt.FakeSession) Result(org.neo4j.driver.Result) Test(org.junit.Test)

Example 4 with Result

use of org.neo4j.driver.Result 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 5 with Result

use of org.neo4j.driver.Result 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");
}
Also used : Driver(org.neo4j.driver.Driver) FakeDriver(org.neo4j.shell.test.bolt.FakeDriver) ClientException(org.neo4j.driver.exceptions.ClientException) Session(org.neo4j.driver.Session) FakeSession(org.neo4j.shell.test.bolt.FakeSession) Result(org.neo4j.driver.Result) Test(org.junit.Test)

Aggregations

Result (org.neo4j.driver.Result)29 Test (org.junit.Test)19 Session (org.neo4j.driver.Session)16 Driver (org.neo4j.driver.Driver)13 StringContains.containsString (org.hamcrest.core.StringContains.containsString)8 BoltResult (org.neo4j.shell.state.BoltResult)8 ListBoltResult (org.neo4j.shell.state.ListBoltResult)8 FakeDriver (org.neo4j.shell.test.bolt.FakeDriver)8 FakeSession (org.neo4j.shell.test.bolt.FakeSession)8 Record (org.neo4j.driver.Record)7 SessionConfig (org.neo4j.driver.SessionConfig)5 ClientException (org.neo4j.driver.exceptions.ClientException)5 SessionExpiredException (org.neo4j.driver.exceptions.SessionExpiredException)5 Query (org.neo4j.driver.Query)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Test (org.junit.jupiter.api.Test)3 Value (org.neo4j.driver.Value)3 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2