Search in sources :

Example 11 with Driver

use of org.neo4j.driver.Driver in project beam by apache.

the class Neo4jIOIT method testWriteUnwind.

@Test
public void testWriteUnwind() throws Exception {
    PCollection<String> stringsCollections = writeUnwindPipeline.apply(Create.of(Arrays.asList("one", "two", "three")));
    // Every row is represented by a Map<String, Object> in the parameters map.
    // We accumulate the rows and 'unwind' those to Neo4j for performance reasons.
    // 
    SerializableFunction<String, Map<String, Object>> parametersMapper = name -> Collections.singletonMap("name", name);
    Neo4jIO.WriteUnwind<String> read = Neo4jIO.<String>writeUnwind().withDriverConfiguration(Neo4jTestUtil.getDriverConfiguration(containerHostname, containerPort)).withSessionConfig(SessionConfig.forDatabase(Neo4jTestUtil.NEO4J_DATABASE)).withBatchSize(5000).withUnwindMapName("rows").withCypher("UNWIND $rows AS row MERGE(n:Num { name : row.name })").withParametersFunction(parametersMapper).withCypherLogging();
    stringsCollections.apply(read);
    // Now run this pipeline
    // 
    PipelineResult pipelineResult = writeUnwindPipeline.run();
    Assert.assertEquals(PipelineResult.State.DONE, pipelineResult.getState());
    // 
    try (Driver driver = Neo4jTestUtil.getDriver(containerHostname, containerPort)) {
        try (Session session = Neo4jTestUtil.getSession(driver, true)) {
            List<String> names = session.readTransaction(tx -> {
                List<String> list = new ArrayList<>();
                Result result = tx.run("MATCH(n:Num) RETURN n.name");
                while (result.hasNext()) {
                    Record record = result.next();
                    list.add(record.get(0).asString());
                }
                return list;
            });
            assertThat(names, containsInAnyOrder("one", "two", "three"));
        }
    }
}
Also used : Session(org.neo4j.driver.Session) Arrays(java.util.Arrays) SerializableCoder(org.apache.beam.sdk.coders.SerializableCoder) BeforeClass(org.junit.BeforeClass) IsIterableContainingInOrder.contains(org.hamcrest.collection.IsIterableContainingInOrder.contains) DockerImageName(org.testcontainers.utility.DockerImageName) IsIterableContainingInAnyOrder.containsInAnyOrder(org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder) PipelineResult(org.apache.beam.sdk.PipelineResult) RunWith(org.junit.runner.RunWith) SerializableFunction(org.apache.beam.sdk.transforms.SerializableFunction) ImmutableMap(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap) ArrayList(java.util.ArrayList) Create(org.apache.beam.sdk.transforms.Create) Map(java.util.Map) TestPipeline(org.apache.beam.sdk.testing.TestPipeline) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Row(org.apache.beam.sdk.values.Row) DoFn(org.apache.beam.sdk.transforms.DoFn) AfterClass(org.junit.AfterClass) Driver(org.neo4j.driver.Driver) Neo4jContainer(org.testcontainers.containers.Neo4jContainer) PAssert(org.apache.beam.sdk.testing.PAssert) Test(org.junit.Test) JUnit4(org.junit.runners.JUnit4) PCollection(org.apache.beam.sdk.values.PCollection) Schema(org.apache.beam.sdk.schemas.Schema) Result(org.neo4j.driver.Result) List(java.util.List) Rule(org.junit.Rule) SessionConfig(org.neo4j.driver.SessionConfig) ParDo(org.apache.beam.sdk.transforms.ParDo) Assert(org.junit.Assert) Collections(java.util.Collections) Record(org.neo4j.driver.Record) ArrayList(java.util.ArrayList) PipelineResult(org.apache.beam.sdk.PipelineResult) Driver(org.neo4j.driver.Driver) PipelineResult(org.apache.beam.sdk.PipelineResult) Result(org.neo4j.driver.Result) Record(org.neo4j.driver.Record) ImmutableMap(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap) Map(java.util.Map) Session(org.neo4j.driver.Session) Test(org.junit.Test)

Example 12 with Driver

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

the class Neo4jReactiveHealthIndicatorTests method neo4jIsDown.

@Test
void neo4jIsDown() {
    Driver driver = mock(Driver.class);
    given(driver.rxSession(any(SessionConfig.class))).willThrow(ServiceUnavailableException.class);
    Neo4jReactiveHealthIndicator healthIndicator = new Neo4jReactiveHealthIndicator(driver);
    healthIndicator.health().as(StepVerifier::create).consumeNextWith((health) -> {
        assertThat(health.getStatus()).isEqualTo(Status.DOWN);
        assertThat(health.getDetails()).containsKeys("error");
    }).verifyComplete();
}
Also used : Status(org.springframework.boot.actuate.health.Status) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Driver(org.neo4j.driver.Driver) StepVerifier(reactor.test.StepVerifier) RxSession(org.neo4j.driver.reactive.RxSession) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SessionExpiredException(org.neo4j.driver.exceptions.SessionExpiredException) BDDMockito.then(org.mockito.BDDMockito.then) Mono(reactor.core.publisher.Mono) Mockito.times(org.mockito.Mockito.times) RxResult(org.neo4j.driver.reactive.RxResult) Test(org.junit.jupiter.api.Test) Values(org.neo4j.driver.Values) SessionConfig(org.neo4j.driver.SessionConfig) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BDDMockito.given(org.mockito.BDDMockito.given) ServiceUnavailableException(org.neo4j.driver.exceptions.ServiceUnavailableException) ResultSummary(org.neo4j.driver.summary.ResultSummary) Record(org.neo4j.driver.Record) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) Driver(org.neo4j.driver.Driver) SessionConfig(org.neo4j.driver.SessionConfig) StepVerifier(reactor.test.StepVerifier) Test(org.junit.jupiter.api.Test)

Example 13 with Driver

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

the class Neo4jReactiveHealthIndicatorTests method neo4jIsUp.

@Test
void neo4jIsUp() {
    ResultSummary resultSummary = ResultSummaryMock.createResultSummary("My Home", "test");
    Driver driver = mockDriver(resultSummary, "4711", "ultimate collectors edition");
    Neo4jReactiveHealthIndicator healthIndicator = new Neo4jReactiveHealthIndicator(driver);
    healthIndicator.health().as(StepVerifier::create).consumeNextWith((health) -> {
        assertThat(health.getStatus()).isEqualTo(Status.UP);
        assertThat(health.getDetails()).containsEntry("server", "4711@My Home");
        assertThat(health.getDetails()).containsEntry("edition", "ultimate collectors edition");
    }).verifyComplete();
}
Also used : Status(org.springframework.boot.actuate.health.Status) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Driver(org.neo4j.driver.Driver) StepVerifier(reactor.test.StepVerifier) RxSession(org.neo4j.driver.reactive.RxSession) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SessionExpiredException(org.neo4j.driver.exceptions.SessionExpiredException) BDDMockito.then(org.mockito.BDDMockito.then) Mono(reactor.core.publisher.Mono) Mockito.times(org.mockito.Mockito.times) RxResult(org.neo4j.driver.reactive.RxResult) Test(org.junit.jupiter.api.Test) Values(org.neo4j.driver.Values) SessionConfig(org.neo4j.driver.SessionConfig) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BDDMockito.given(org.mockito.BDDMockito.given) ServiceUnavailableException(org.neo4j.driver.exceptions.ServiceUnavailableException) ResultSummary(org.neo4j.driver.summary.ResultSummary) Record(org.neo4j.driver.Record) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) ResultSummary(org.neo4j.driver.summary.ResultSummary) Driver(org.neo4j.driver.Driver) StepVerifier(reactor.test.StepVerifier) Test(org.junit.jupiter.api.Test)

Example 14 with Driver

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

the class Neo4jHealthIndicatorTests method mockDriver.

private Driver mockDriver(ResultSummary resultSummary, String version, String edition) {
    Result statementResult = mockStatementResult(resultSummary, version, edition);
    Session session = mock(Session.class);
    given(session.run(anyString())).willReturn(statementResult);
    Driver driver = mock(Driver.class);
    given(driver.session(any(SessionConfig.class))).willReturn(session);
    return driver;
}
Also used : Driver(org.neo4j.driver.Driver) SessionConfig(org.neo4j.driver.SessionConfig) Result(org.neo4j.driver.Result) Session(org.neo4j.driver.Session)

Example 15 with Driver

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

the class Neo4jHealthIndicatorTests method neo4jIsUpWithoutDatabaseName.

@Test
void neo4jIsUpWithoutDatabaseName() {
    ResultSummary resultSummary = ResultSummaryMock.createResultSummary("My Home", null);
    Driver driver = mockDriver(resultSummary, "4711", "some edition");
    Health health = new Neo4jHealthIndicator(driver).health();
    assertThat(health.getStatus()).isEqualTo(Status.UP);
    assertThat(health.getDetails()).containsEntry("server", "4711@My Home");
    assertThat(health.getDetails()).doesNotContainKey("database");
    assertThat(health.getDetails()).containsEntry("edition", "some edition");
}
Also used : Health(org.springframework.boot.actuate.health.Health) ResultSummary(org.neo4j.driver.summary.ResultSummary) Driver(org.neo4j.driver.Driver) Test(org.junit.jupiter.api.Test)

Aggregations

Driver (org.neo4j.driver.Driver)33 Session (org.neo4j.driver.Session)22 Result (org.neo4j.driver.Result)16 Test (org.junit.Test)15 FakeDriver (org.neo4j.shell.test.bolt.FakeDriver)13 Test (org.junit.jupiter.api.Test)12 SessionConfig (org.neo4j.driver.SessionConfig)11 FakeSession (org.neo4j.shell.test.bolt.FakeSession)11 ResultSummary (org.neo4j.driver.summary.ResultSummary)8 Record (org.neo4j.driver.Record)7 SessionExpiredException (org.neo4j.driver.exceptions.SessionExpiredException)7 ServiceUnavailableException (org.neo4j.driver.exceptions.ServiceUnavailableException)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 BeforeClass (org.junit.BeforeClass)3 Health (org.springframework.boot.actuate.health.Health)3 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 List (java.util.List)2