Search in sources :

Example 16 with Session

use of org.neo4j.driver.Session in project spring-boot by spring-projects.

the class Neo4jAutoConfigurationIntegrationTests method driverCanHandleRequest.

@Test
void driverCanHandleRequest() {
    try (Session session = this.driver.session();
        Transaction tx = session.beginTransaction()) {
        Result statementResult = tx.run("MATCH (n:Thing) RETURN n LIMIT 1");
        assertThat(statementResult.hasNext()).isFalse();
        tx.commit();
    }
}
Also used : Transaction(org.neo4j.driver.Transaction) Session(org.neo4j.driver.Session) Result(org.neo4j.driver.Result) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 17 with Session

use of org.neo4j.driver.Session in project spring-boot by spring-projects.

the class Neo4jHealthIndicator method runHealthCheckQuery.

private void runHealthCheckQuery(Health.Builder builder) {
    // all possible workloads
    try (Session session = this.driver.session(DEFAULT_SESSION_CONFIG)) {
        Result result = session.run(CYPHER);
        Record record = result.single();
        ResultSummary resultSummary = result.consume();
        this.healthDetailsHandler.addHealthDetails(builder, new Neo4jHealthDetails(record, resultSummary));
    }
}
Also used : ResultSummary(org.neo4j.driver.summary.ResultSummary) Record(org.neo4j.driver.Record) Session(org.neo4j.driver.Session) Result(org.neo4j.driver.Result)

Example 18 with Session

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

the class BoltStateHandlerTest method silentDisconnectCleansUp.

@Test
public void silentDisconnectCleansUp() throws Exception {
    // given
    boltStateHandler.connect();
    Session session = boltStateHandler.session;
    assertNotNull(session);
    assertNotNull(boltStateHandler.driver);
    assertTrue(boltStateHandler.session.isOpen());
    // when
    boltStateHandler.silentDisconnect();
    // then
    assertFalse(session.isOpen());
}
Also used : Session(org.neo4j.driver.Session) FakeSession(org.neo4j.shell.test.bolt.FakeSession) Test(org.junit.Test)

Example 19 with Session

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

the class BoltStateHandlerTest method whenInTransactionHandlerLetsTransactionDoTheWork.

@Test
public void whenInTransactionHandlerLetsTransactionDoTheWork() throws CommandException {
    Transaction transactionMock = mock(Transaction.class);
    Session sessionMock = mock(Session.class);
    when(sessionMock.beginTransaction()).thenReturn(transactionMock);
    Driver driverMock = stubResultSummaryInAnOpenSession(mock(Result.class), sessionMock, "neo4j-version");
    Result result = mock(Result.class);
    when(transactionMock.run(any(Query.class))).thenReturn(result);
    OfflineBoltStateHandler boltStateHandler = new OfflineBoltStateHandler(driverMock);
    boltStateHandler.connect();
    boltStateHandler.beginTransaction();
    BoltResult boltResult = boltStateHandler.runCypher("UNWIND [1,2] as num RETURN *", Collections.emptyMap()).get();
    assertEquals(result, boltResult.iterate());
    boltStateHandler.commitTransaction();
    assertFalse(boltStateHandler.isTransactionOpen());
}
Also used : Transaction(org.neo4j.driver.Transaction) Query(org.neo4j.driver.Query) Driver(org.neo4j.driver.Driver) FakeDriver(org.neo4j.shell.test.bolt.FakeDriver) Session(org.neo4j.driver.Session) FakeSession(org.neo4j.shell.test.bolt.FakeSession) Result(org.neo4j.driver.Result) Test(org.junit.Test)

Example 20 with Session

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

the class BoltStateHandlerTest method shouldRunCypherQuery.

@Test
public void shouldRunCypherQuery() throws CommandException {
    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))).thenReturn(resultMock);
    OfflineBoltStateHandler boltStateHandler = new OfflineBoltStateHandler(driverMock);
    boltStateHandler.connect();
    BoltResult boltResult = boltStateHandler.runCypher("RETURN 999", new HashMap<>()).get();
    verify(sessionMock).run(any(Query.class));
    assertEquals("999", boltResult.getRecords().get(0).get(0).toString());
}
Also used : Query(org.neo4j.driver.Query) HashMap(java.util.HashMap) Value(org.neo4j.driver.Value) Driver(org.neo4j.driver.Driver) FakeDriver(org.neo4j.shell.test.bolt.FakeDriver) Record(org.neo4j.driver.Record) Session(org.neo4j.driver.Session) FakeSession(org.neo4j.shell.test.bolt.FakeSession) Result(org.neo4j.driver.Result) Test(org.junit.Test)

Aggregations

Session (org.neo4j.driver.Session)24 Driver (org.neo4j.driver.Driver)20 Result (org.neo4j.driver.Result)17 Test (org.junit.Test)13 FakeSession (org.neo4j.shell.test.bolt.FakeSession)10 FakeDriver (org.neo4j.shell.test.bolt.FakeDriver)9 Test (org.junit.jupiter.api.Test)6 SessionConfig (org.neo4j.driver.SessionConfig)6 Record (org.neo4j.driver.Record)5 SessionExpiredException (org.neo4j.driver.exceptions.SessionExpiredException)4 HashMap (java.util.HashMap)3 BeforeClass (org.junit.BeforeClass)3 ClientException (org.neo4j.driver.exceptions.ClientException)3 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 List (java.util.List)2 Map (java.util.Map)2 PipelineResult (org.apache.beam.sdk.PipelineResult)2 SerializableCoder (org.apache.beam.sdk.coders.SerializableCoder)2