Skip to content
This repository was archived by the owner on Sep 1, 2023. It is now read-only.

Commit 355a40d

Browse files
GH-16 - Add an example how to combine autoconfig with OGM and TestHarness.
1 parent ad9678c commit 355a40d

File tree

4 files changed

+108
-0
lines changed

4 files changed

+108
-0
lines changed

examples/ogm-integration/pom.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,22 @@
1818
<properties>
1919
<java.version>1.8</java.version>
2020
<neo4j-java-driver-spring-boot-starter.version>${revision}${sha1}${changelist}</neo4j-java-driver-spring-boot-starter.version>
21+
<!-- Change to 4.0.0 if needed and if on JDK 11 + -->
2122
<neo4j-ogm.version>3.2.7</neo4j-ogm.version>
23+
<neo4j.version>3.5.14</neo4j.version>
2224
<testcontainers.version>1.12.4</testcontainers.version>
2325
</properties>
2426

27+
<dependencyManagement>
28+
<dependencies>
29+
<dependency>
30+
<groupId>org.neo4j.test</groupId>
31+
<artifactId>neo4j-harness</artifactId>
32+
<version>${neo4j.version}</version>
33+
</dependency>
34+
</dependencies>
35+
</dependencyManagement>
36+
2537
<dependencies>
2638
<dependency>
2739
<groupId>com.github.ben-manes.caffeine</groupId>
@@ -32,6 +44,12 @@
3244
<artifactId>neo4j-java-driver-spring-boot-starter</artifactId>
3345
<version>${neo4j-java-driver-spring-boot-starter.version}</version>
3446
</dependency>
47+
<dependency>
48+
<groupId>org.neo4j.driver</groupId>
49+
<artifactId>neo4j-java-driver-test-harness-spring-boot-autoconfigure</artifactId>
50+
<version>${neo4j-java-driver-spring-boot-starter.version}</version>
51+
<scope>test</scope>
52+
</dependency>
3553
<dependency>
3654
<groupId>org.springframework.boot</groupId>
3755
<artifactId>spring-boot-starter-actuator</artifactId>

examples/ogm-integration/src/main/java/org/neo4j/doc/driver/springframework/boot/ogm_integration/domain/MovieRepository.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@
1818
*/
1919
package org.neo4j.doc.driver.springframework.boot.ogm_integration.domain;
2020

21+
import java.util.Optional;
22+
2123
import org.springframework.data.repository.CrudRepository;
2224

2325
public interface MovieRepository extends CrudRepository<MovieEntity, Long> {
26+
27+
Optional<MovieEntity> findByTitle(String title);
2428
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright (c) 2019-2020 "Neo4j,"
3+
* Neo4j Sweden AB [https://neo4j.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* https://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.neo4j.doc.driver.springframework.boot.ogm_integration.domain;
20+
21+
import static org.assertj.core.api.Assertions.*;
22+
23+
import java.util.Optional;
24+
25+
import org.junit.jupiter.api.Test;
26+
import org.neo4j.driver.Driver;
27+
import org.neo4j.driver.springframework.boot.autoconfigure.Neo4jDriverAutoConfiguration;
28+
import org.neo4j.driver.springframework.boot.test.autoconfigure.Neo4jTestHarnessAutoConfiguration;
29+
import org.neo4j.harness.ServerControls;
30+
import org.neo4j.harness.TestServerBuilders;
31+
import org.neo4j.ogm.drivers.bolt.driver.BoltDriver;
32+
import org.neo4j.ogm.session.SessionFactory;
33+
import org.springframework.beans.factory.annotation.Autowired;
34+
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
35+
import org.springframework.boot.test.autoconfigure.data.neo4j.DataNeo4jTest;
36+
import org.springframework.boot.test.context.TestConfiguration;
37+
import org.springframework.context.annotation.Bean;
38+
39+
/**
40+
* This tests shows that the Neo4j-OGM with the Java Driver autoconfiguration + test harness works fine with test slices.
41+
*
42+
* @author Michael J. Simons
43+
* @soundtrack Katey Sagal - Covered
44+
*/
45+
@DataNeo4jTest
46+
// This is necessary, as the test slice cannot know about it
47+
@ImportAutoConfiguration({ Neo4jTestHarnessAutoConfiguration.class, Neo4jDriverAutoConfiguration.class })
48+
class TestSliceTestWithHarness {
49+
50+
@TestConfiguration
51+
static class TestHarnessConfig {
52+
53+
@Bean
54+
public ServerControls neo4j() {
55+
return TestServerBuilders.newInProcessBuilder()
56+
.withFixture(""
57+
+ "CREATE (TheMatrix:Movie {title:'The Matrix', released:1999, tagline:'Welcome to the Real World'})\n"
58+
+ "CREATE (TheMatrixReloaded:Movie {title:'The Matrix Reloaded', released:2003, tagline:'Free your mind'})\n"
59+
+ "CREATE (TheMatrixRevolutions:Movie {title:'The Matrix Revolutions', released:2003, tagline:'Everything that has a beginning has an end'})\n"
60+
)
61+
.newServer();
62+
}
63+
}
64+
65+
@Test
66+
public void sessionFactoryShouldUseOGMBoltDriver(@Autowired Driver driver,
67+
@Autowired SessionFactory sessionFactory) {
68+
assertThat(sessionFactory.unwrap(org.neo4j.ogm.driver.Driver.class)).isInstanceOf(BoltDriver.class);
69+
70+
BoltDriver boltDriver = (BoltDriver) sessionFactory.unwrap(org.neo4j.ogm.driver.Driver.class);
71+
assertThat(boltDriver.unwrap(Driver.class)).isSameAs(driver);
72+
}
73+
74+
@Test
75+
public void repositoriesShouldWork(@Autowired MovieRepository movieRepository) {
76+
77+
MovieEntity movieEntity = movieRepository.save(new MovieEntity("Life of Brian"));
78+
assertThat(movieEntity.getId()).isNotNull();
79+
80+
String title = "The Matrix";
81+
Optional<MovieEntity> theMatrix = movieRepository.findByTitle(title);
82+
assertThat(theMatrix).isPresent().map(MovieEntity::getTitle).hasValue(title);
83+
}
84+
}

examples/simple/src/main/java/org/neo4j/doc/driver/springframework/boot/simple/DisplayMoviesComponent.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.neo4j.driver.Driver;
2525
import org.neo4j.driver.Session;
2626
import org.neo4j.driver.SessionConfig;
27+
import org.neo4j.driver.Transaction;
2728
import org.slf4j.Logger;
2829
import org.slf4j.LoggerFactory;
2930
import org.springframework.boot.CommandLineRunner;
@@ -51,6 +52,7 @@ public void run(String... args) {
5152
SessionConfig sessionConfig = SessionConfig.builder().withDefaultAccessMode(AccessMode.READ).build();
5253
LOGGER.info("All the movies:");
5354
try (Session session = driver.session(sessionConfig)) {
55+
5456
session.run("MATCH (m:Movie) RETURN m ORDER BY m.name ASC").stream()
5557
.map(r -> r.get("m").asNode())
5658
.map(n -> n.get("title").asString())

0 commit comments

Comments
 (0)