From bef4c757daec2957feff3b0f861f254ff00e77fd Mon Sep 17 00:00:00 2001 From: timis1 Date: Thu, 1 Jun 2023 23:10:53 +0300 Subject: [PATCH 01/10] JAVA-20167 Migrate spring-data-mongodb to JDK 17 --- persistence-modules/spring-data-mongodb/pom.xml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/persistence-modules/spring-data-mongodb/pom.xml b/persistence-modules/spring-data-mongodb/pom.xml index afb5e8b9c1..87cf1acaf8 100644 --- a/persistence-modules/spring-data-mongodb/pom.xml +++ b/persistence-modules/spring-data-mongodb/pom.xml @@ -103,13 +103,13 @@ - 3.0.3.RELEASE - 4.3.1 + 3.4.7 + 5.0.0 1.1.3 4.1.0 - 3.2.0.RELEASE - 4.0.5 - 3.2.6 + 3.5.4 + 4.6.1 + 4.6.3 \ No newline at end of file From 2fd4c39a21cea9c62d313475b3df6fc1a3f3b087 Mon Sep 17 00:00:00 2001 From: timis1 Date: Mon, 12 Jun 2023 01:51:42 +0300 Subject: [PATCH 02/10] JAVA-20167 Migrating spring-data-neo4j --- persistence-modules/pom.xml | 6 +- persistence-modules/spring-data-neo4j/pom.xml | 59 +++++--- .../MovieDatabaseNeo4jTestConfiguration.java | 22 ++- .../spring/data/neo4j/domain/Car.java | 4 +- .../spring/data/neo4j/domain/Movie.java | 5 +- .../neo4j/repository/MovieRepository.java | 4 +- .../java/com/baeldung/SpringContextTest.java | 9 +- .../com/baeldung/TestContainersTestBase.java | 132 ++++++++++++++++++ .../baeldung/neo4j/Neo4JServerLiveTest.java | 56 +++++--- .../com/baeldung/neo4j/Neo4jLiveTest.java | 69 +++++---- .../com/baeldung/neo4j/Neo4jOgmLiveTest.java | 31 ++-- .../neo4j/MovieRepositoryIntegrationTest.java | 6 +- 12 files changed, 295 insertions(+), 108 deletions(-) create mode 100644 persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/TestContainersTestBase.java diff --git a/persistence-modules/pom.xml b/persistence-modules/pom.xml index 9361e26d7d..1501d81985 100644 --- a/persistence-modules/pom.xml +++ b/persistence-modules/pom.xml @@ -40,8 +40,6 @@ java-jpa java-jpa-2 java-jpa-3 - - java-mongodb-2 java-mongodb-3 java-mongodb-queries @@ -90,10 +88,10 @@ spring-data-jpa-repo-2 spring-data-jdbc spring-data-keyvalue - + spring-data-mongodb spring-data-mongodb-2 spring-data-mongodb-reactive - + spring-data-neo4j spring-data-redis diff --git a/persistence-modules/spring-data-neo4j/pom.xml b/persistence-modules/spring-data-neo4j/pom.xml index 8c5030779b..705fe35d3c 100644 --- a/persistence-modules/spring-data-neo4j/pom.xml +++ b/persistence-modules/spring-data-neo4j/pom.xml @@ -27,6 +27,11 @@ org.neo4j neo4j-ogm-embedded-driver + ${neo4j-ogm-embedded-driver.version} + + + org.neo4j + neo4j-ogm-bolt-driver ${neo4j-ogm.version} @@ -34,6 +39,12 @@ neo4j-java-driver ${neo4j-java-driver.version} + + org.neo4j + neo4j-jdbc-driver + ${neo4j-jdbc.version} + runtime + org.springframework.data spring-data-neo4j @@ -69,33 +80,43 @@ - - org.neo4j - neo4j-ogm-test - ${neo4j-ogm.version} - test - - - org.neo4j.test - neo4j-harness - ${neo4j.version} - test - org.springframework spring-test ${spring-test.version} + + org.testcontainers + neo4j + ${testcontainers.version} + test + + + org.jetbrains + annotations + + + org.apache.commons + commons-compress + + + javax.xml.bind + jaxb-api + + + - 1.6.2 - 3.4.6 - 5.0.1.RELEASE - 1.1 - 2.0.1.RELEASE - 5.0.1.RELEASE - 3.1.2 + 5.6.0 + 5.8.0 + 7.0.3 + 1.1.2 + 3.1.0 + 6.0.8 + 4.0.4 + 4.0.9 + 3.2.39 \ No newline at end of file diff --git a/persistence-modules/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/config/MovieDatabaseNeo4jTestConfiguration.java b/persistence-modules/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/config/MovieDatabaseNeo4jTestConfiguration.java index a4cbe4b809..f3c89c1bbe 100644 --- a/persistence-modules/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/config/MovieDatabaseNeo4jTestConfiguration.java +++ b/persistence-modules/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/config/MovieDatabaseNeo4jTestConfiguration.java @@ -1,13 +1,16 @@ package com.baeldung.spring.data.neo4j.config; -import org.neo4j.ogm.config.Configuration.Builder; +import org.neo4j.driver.Driver; import org.neo4j.ogm.session.SessionFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; +import org.springframework.data.neo4j.core.DatabaseSelectionProvider; +import org.springframework.data.neo4j.core.UserSelectionProvider; +import org.springframework.data.neo4j.core.transaction.Neo4jTransactionManager; import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories; -import org.springframework.data.neo4j.transaction.Neo4jTransactionManager; +import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; @Configuration @@ -19,18 +22,23 @@ public class MovieDatabaseNeo4jTestConfiguration { @Bean public org.neo4j.ogm.config.Configuration getConfiguration() { - org.neo4j.ogm.config.Configuration config = new Builder().build(); - return config; + return new org.neo4j.ogm.config.Configuration.Builder().build(); } @Bean public SessionFactory getSessionFactory() { return new SessionFactory(getConfiguration(), "com.baeldung.spring.data.neo4j.domain"); } - + @Bean - public Neo4jTransactionManager transactionManager() { - return new Neo4jTransactionManager(getSessionFactory()); + public PlatformTransactionManager transactionManager(Driver driver, + DatabaseSelectionProvider databaseSelectionProvider, UserSelectionProvider userSelectionProvider) { + + return Neo4jTransactionManager + .with(driver) + .withDatabaseSelectionProvider(databaseSelectionProvider) + .withUserSelectionProvider(userSelectionProvider) + .build(); } } diff --git a/persistence-modules/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/domain/Car.java b/persistence-modules/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/domain/Car.java index 455407a92b..0f516d3758 100644 --- a/persistence-modules/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/domain/Car.java +++ b/persistence-modules/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/domain/Car.java @@ -1,5 +1,7 @@ package com.baeldung.spring.data.neo4j.domain; +import static org.neo4j.ogm.annotation.Relationship.Direction.INCOMING; + import org.neo4j.ogm.annotation.GeneratedValue; import org.neo4j.ogm.annotation.Id; import org.neo4j.ogm.annotation.NodeEntity; @@ -12,7 +14,7 @@ public class Car { private String make; - @Relationship(direction = "INCOMING") + @Relationship(direction = INCOMING) private Company company; public Car(String make, String model) { diff --git a/persistence-modules/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/domain/Movie.java b/persistence-modules/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/domain/Movie.java index 996a661b07..bea9f996aa 100644 --- a/persistence-modules/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/domain/Movie.java +++ b/persistence-modules/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/domain/Movie.java @@ -1,5 +1,7 @@ package com.baeldung.spring.data.neo4j.domain; +import static org.neo4j.ogm.annotation.Relationship.Direction.INCOMING; + import com.fasterxml.jackson.annotation.JsonIdentityInfo; import com.voodoodyne.jackson.jsog.JSOGGenerator; @@ -12,7 +14,6 @@ import java.util.Collection; import java.util.List; @JsonIdentityInfo(generator = JSOGGenerator.class) - @NodeEntity public class Movie { @Id @GeneratedValue @@ -23,7 +24,7 @@ public class Movie { private int released; private String tagline; - @Relationship(type = "ACTED_IN", direction = Relationship.INCOMING) + @Relationship(type = "ACTED_IN", direction = INCOMING) private List roles; public Movie() { diff --git a/persistence-modules/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/repository/MovieRepository.java b/persistence-modules/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/repository/MovieRepository.java index dde946ea73..732970c0b5 100644 --- a/persistence-modules/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/repository/MovieRepository.java +++ b/persistence-modules/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/repository/MovieRepository.java @@ -1,8 +1,8 @@ package com.baeldung.spring.data.neo4j.repository; import com.baeldung.spring.data.neo4j.domain.Movie; -import org.springframework.data.neo4j.annotation.Query; import org.springframework.data.neo4j.repository.Neo4jRepository; +import org.springframework.data.neo4j.repository.query.Query; import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; @@ -13,7 +13,7 @@ import java.util.Map; @Repository public interface MovieRepository extends Neo4jRepository { - Movie findByTitle(@Param("title") String title); + Movie findByTitle(String title); @Query("MATCH (m:Movie) WHERE m.title =~ ('(?i).*'+{title}+'.*') RETURN m") Collection findByTitleContaining(@Param("title") String title); diff --git a/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/SpringContextTest.java b/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/SpringContextTest.java index 7029905c49..9c96a98ff2 100644 --- a/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/SpringContextTest.java +++ b/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/SpringContextTest.java @@ -1,19 +1,16 @@ package com.baeldung; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.baeldung.spring.data.neo4j.config.MovieDatabaseNeo4jTestConfiguration; -@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = MovieDatabaseNeo4jTestConfiguration.class) @ActiveProfiles(profiles = "test") -public class SpringContextTest { +class SpringContextTest { @Test - public void whenSpringContextIsBootstrapped_thenNoExceptions() { + void whenSpringContextIsBootstrapped_thenNoExceptions() { } } diff --git a/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/TestContainersTestBase.java b/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/TestContainersTestBase.java new file mode 100644 index 0000000000..e2bd022d12 --- /dev/null +++ b/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/TestContainersTestBase.java @@ -0,0 +1,132 @@ +package com.baeldung; + +import static org.neo4j.ogm.drivers.bolt.driver.BoltDriver.CONFIG_PARAMETER_BOLT_LOGGING; + +import java.util.Locale; +import java.util.Optional; + +import org.neo4j.driver.AccessMode; +import org.neo4j.driver.AuthTokens; +import org.neo4j.driver.GraphDatabase; +import org.neo4j.driver.Logging; +import org.neo4j.driver.Session; +import org.neo4j.driver.SessionConfig; +import org.neo4j.ogm.config.Configuration; +import org.neo4j.ogm.driver.Driver; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testcontainers.containers.Neo4jContainer; +import org.testcontainers.utility.TestcontainersConfiguration; + +/** + * Reference: https://github.com/neo4j/neo4j-ogm/blob/master/neo4j-ogm-tests/neo4j-ogm-integration-tests/src/test/java/org/neo4j/ogm/testutil/TestContainersTestBase.java + */ +public class TestContainersTestBase { + + public static final Logger LOGGER = LoggerFactory.getLogger(TestContainersTestBase.class); + + public static final Driver driver; + + public static final String version; + + public static final String DEFAULT_IMAGE = "neo4j:5"; + + public static final String SYS_PROPERTY_ACCEPT_AND_USE_COMMERCIAL_EDITION = "NEO4J_OGM_NEO4J_ACCEPT_AND_USE_COMMERCIAL_EDITION"; + + public static final String SYS_PROPERTY_IMAGE_NAME = "NEO4J_OGM_NEO4J_IMAGE_NAME"; + + public static final String SYS_PROPERTY_NEO4J_URL = "NEO4J_OGM_NEO4J_URL"; + + public static final String SYS_PROPERTY_NEO4J_PASSWORD = "NEO4J_OGM_NEO4J_PASSWORD"; + + public static final String DEFAULT_PASSWORD = "verysecret"; + + public static Neo4jContainer neo4jServer; + + public static Configuration.Builder baseConfigurationBuilder; + + public static final String NEO4J_URL = Optional.ofNullable(System.getenv(SYS_PROPERTY_NEO4J_URL)).orElse(""); + + public static final String NEO4J_PASSWORD = Optional.ofNullable(System.getenv(SYS_PROPERTY_NEO4J_PASSWORD)).orElse("").trim(); + + static { + + boolean acceptAndUseCommercialEdition = hasAcceptedAndWantsToUseCommercialEdition(); + + if (!(NEO4J_URL.isEmpty() || NEO4J_PASSWORD.isEmpty())) { + LOGGER.info("Using Neo4j instance at {}.", NEO4J_URL); + driver = new org.neo4j.ogm.drivers.bolt.driver.BoltDriver(); + baseConfigurationBuilder = new Configuration.Builder() + .uri(NEO4J_URL) + .verifyConnection(true) + .withCustomProperty(CONFIG_PARAMETER_BOLT_LOGGING, Logging.slf4j()) + .credentials("neo4j", NEO4J_PASSWORD); + driver.configure(baseConfigurationBuilder.build()); + version = extractVersionFromBolt(); + } else { + LOGGER.info("Using Neo4j test container."); + String imageName = Optional.ofNullable(System.getenv(SYS_PROPERTY_IMAGE_NAME)) + .orElse(DEFAULT_IMAGE + (acceptAndUseCommercialEdition ? "-enterprise" : "")); + + version = extractVersionFromDockerImage(imageName); + + boolean containerReuseSupported = TestcontainersConfiguration + .getInstance().environmentSupportsReuse(); + neo4jServer = new Neo4jContainer<>(imageName) + .withReuse(containerReuseSupported); + + if (acceptAndUseCommercialEdition) { + neo4jServer.withEnv("NEO4J_ACCEPT_LICENSE_AGREEMENT", "yes"); + } + neo4jServer.withAdminPassword(DEFAULT_PASSWORD).start(); + + driver = new org.neo4j.ogm.drivers.bolt.driver.BoltDriver(); + + baseConfigurationBuilder = new Configuration.Builder() + .uri(neo4jServer.getBoltUrl()) + .credentials("neo4j", DEFAULT_PASSWORD) + .verifyConnection(true) + .withCustomProperty(CONFIG_PARAMETER_BOLT_LOGGING, Logging.slf4j()); + + driver.configure(baseConfigurationBuilder.build()); + + Runtime.getRuntime().addShutdownHook(new Thread(neo4jServer::stop)); + } + } + + public static org.neo4j.driver.Driver getNewBoltConnection() { + + if (neo4jServer != null) { + return GraphDatabase.driver(neo4jServer.getBoltUrl(), AuthTokens.basic("neo4j", DEFAULT_PASSWORD)); + } else { + return GraphDatabase.driver(NEO4J_URL, AuthTokens.basic("neo4j", NEO4J_PASSWORD)); + } + } + + public static boolean hasAcceptedAndWantsToUseCommercialEdition() { + return Optional.ofNullable( + System.getenv(TestContainersTestBase.SYS_PROPERTY_ACCEPT_AND_USE_COMMERCIAL_EDITION)) + .orElse("no").toLowerCase(Locale.ENGLISH).equals("yes"); + } + + public static Driver getDriver() { + return driver; + } + + private static String extractVersionFromDockerImage(String imageName) { + return imageName.replace("neo4j:", "").replace("neo4j/neo4j-experimental:", "").replace("-enterprise", ""); + } + + private static String extractVersionFromBolt() { + + org.neo4j.driver.Driver driver = getDriver().unwrap(org.neo4j.driver.Driver.class); + + String version; + SessionConfig sessionConfig = SessionConfig.builder().withDefaultAccessMode(AccessMode.READ).build(); + try (Session session = driver.session(sessionConfig)) { + version = (String) session.run("CALL dbms.components() YIELD versions").single().get("versions").asList().get(0); + } + return version.toLowerCase(Locale.ENGLISH); + } + +} \ No newline at end of file diff --git a/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/neo4j/Neo4JServerLiveTest.java b/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/neo4j/Neo4JServerLiveTest.java index bf187ccb2d..3356d90291 100644 --- a/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/neo4j/Neo4JServerLiveTest.java +++ b/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/neo4j/Neo4JServerLiveTest.java @@ -1,45 +1,59 @@ package com.baeldung.neo4j; +import static com.baeldung.TestContainersTestBase.DEFAULT_PASSWORD; +import static com.baeldung.TestContainersTestBase.getNewBoltConnection; +import static com.baeldung.TestContainersTestBase.neo4jServer; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; -import org.junit.Assert; -import org.junit.Ignore; -import org.junit.Test; -import org.neo4j.driver.v1.AuthTokens; -import org.neo4j.driver.v1.Driver; -import org.neo4j.driver.v1.GraphDatabase; -import org.neo4j.driver.v1.Session; -import org.neo4j.driver.v1.StatementResult; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.neo4j.driver.Driver; +import org.neo4j.driver.Result; +import org.neo4j.driver.Session; -@Ignore -public class Neo4JServerLiveTest { +class Neo4JServerLiveTest { + + private static Session session; + private static Driver driver; + + @BeforeAll + public static void setup() { + driver = getNewBoltConnection(); + } + + @AfterAll + public static void tearDown() { + driver.close(); + } @Test - public void standAloneDriver() { - Driver driver = GraphDatabase.driver("bolt://localhost:7687", AuthTokens.basic("neo4j", "12345")); - Session session = driver.session(); - + void standAloneDriver() { + session = driver.session(); session.run("CREATE (baeldung:Company {name:\"Baeldung\"}) " + "-[:owns]-> (tesla:Car {make: 'tesla', model: 'modelX'})" + "RETURN baeldung, tesla"); - StatementResult result = session.run("MATCH (company:Company)-[:owns]-> (car:Car)" + + Result result = session.run("MATCH (company:Company)-[:owns]-> (car:Car)" + "WHERE car.make='tesla' and car.model='modelX'" + "RETURN company.name"); - Assert.assertTrue(result.hasNext()); - Assert.assertEquals(result.next().get("company.name").asString(), "Baeldung"); + assertTrue(result.hasNext()); + assertEquals("Baeldung", result.next().get("company.name").asString()); session.close(); - driver.close(); } @Test - public void standAloneJdbc() throws Exception { - Connection con = DriverManager.getConnection("jdbc:neo4j:bolt://localhost/?user=neo4j,password=12345,scheme=basic"); + void standAloneJdbc() throws Exception { + String uri = "jdbc:neo4j:" + neo4jServer.getBoltUrl() + "/?user=neo4j,password=" + DEFAULT_PASSWORD + ",scheme=basic"; + Connection con = DriverManager.getConnection(uri); // Querying try (Statement stmt = con.createStatement()) { @@ -52,7 +66,7 @@ public class Neo4JServerLiveTest { "RETURN company.name"); while (rs.next()) { - Assert.assertEquals(rs.getString("company.name"), "Baeldung"); + assertEquals("Baeldung", rs.getString("company.name")); } } con.close(); diff --git a/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/neo4j/Neo4jLiveTest.java b/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/neo4j/Neo4jLiveTest.java index 1ff01b93a1..840f4536ca 100644 --- a/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/neo4j/Neo4jLiveTest.java +++ b/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/neo4j/Neo4jLiveTest.java @@ -1,7 +1,10 @@ package com.baeldung.neo4j; +import static org.neo4j.configuration.GraphDatabaseSettings.DEFAULT_DATABASE_NAME; + import java.io.File; +import java.time.Duration; import java.util.Arrays; import java.util.HashMap; import java.util.Map; @@ -10,101 +13,107 @@ import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import org.neo4j.configuration.GraphDatabaseSettings; +import org.neo4j.dbms.api.DatabaseManagementService; +import org.neo4j.dbms.api.DatabaseManagementServiceBuilder; import org.neo4j.graphdb.GraphDatabaseService; import org.neo4j.graphdb.Label; import org.neo4j.graphdb.Node; import org.neo4j.graphdb.NotFoundException; import org.neo4j.graphdb.RelationshipType; import org.neo4j.graphdb.Result; -import org.neo4j.graphdb.factory.GraphDatabaseFactory; +import org.neo4j.graphdb.Transaction; public class Neo4jLiveTest { private static GraphDatabaseService graphDb; + private static Transaction transaction; + private static DatabaseManagementService managementService; @Before public void setUp() { - GraphDatabaseFactory graphDbFactory = new GraphDatabaseFactory(); - graphDb = graphDbFactory.newEmbeddedDatabase(new File("data/cars")); + managementService = new DatabaseManagementServiceBuilder(new File("data/cars").toPath()) + .setConfig( GraphDatabaseSettings.transaction_timeout, Duration.ofSeconds( 60 ) ) + .setConfig( GraphDatabaseSettings.preallocate_logical_logs, false ).build(); + graphDb = managementService.database( DEFAULT_DATABASE_NAME ); } @After public void tearDown() { - graphDb.shutdown(); + managementService.shutdown(); } @Test public void testPersonCar() { - graphDb.beginTx(); - Node car = graphDb.createNode(Label.label("Car")); + transaction = graphDb.beginTx(); + Node car = transaction.createNode(Label.label("Car")); car.setProperty("make", "tesla"); car.setProperty("model", "model3"); - Node owner = graphDb.createNode(Label.label("Person")); + Node owner = transaction.createNode(Label.label("Person")); owner.setProperty("firstName", "baeldung"); owner.setProperty("lastName", "baeldung"); owner.createRelationshipTo(car, RelationshipType.withName("owner")); - Result result = graphDb.execute("MATCH (c:Car) <-[owner]- (p:Person) " + + Result result = transaction.execute("MATCH (c:Car) <-[owner]- (p:Person) " + "WHERE c.make = 'tesla'" + "RETURN p.firstName, p.lastName"); Map firstResult = result.next(); + Assert.assertEquals("baeldung", firstResult.get("p.firstName")); } @Test public void testCreateNode() { - - graphDb.beginTx(); - - Result result = graphDb.execute("CREATE (baeldung:Company {name:\"Baeldung\"})" + + transaction = graphDb.beginTx(); + Result result = transaction.execute("CREATE (baeldung:Company {name:\"Baeldung\"})" + "RETURN baeldung"); Map firstResult = result.next(); Node firstNode = (Node) firstResult.get("baeldung"); + Assert.assertEquals(firstNode.getProperty("name"), "Baeldung"); } @Test public void testCreateNodeAndLink() { - graphDb.beginTx(); - - Result result = graphDb.execute("CREATE (baeldung:Company {name:\"Baeldung\"}) " + + transaction = graphDb.beginTx(); + Result result = transaction.execute("CREATE (baeldung:Company {name:\"Baeldung\"}) " + "-[:owns]-> (tesla:Car {make: 'tesla', model: 'modelX'})" + "RETURN baeldung, tesla"); Map firstResult = result.next(); + Assert.assertTrue(firstResult.containsKey("baeldung")); Assert.assertTrue(firstResult.containsKey("tesla")); } @Test public void testFindAndReturn() { - graphDb.beginTx(); - - graphDb.execute("CREATE (baeldung:Company {name:\"Baeldung\"}) " + + transaction = graphDb.beginTx(); + transaction.execute("CREATE (baeldung:Company {name:\"Baeldung\"}) " + "-[:owns]-> (tesla:Car {make: 'tesla', model: 'modelX'})" + "RETURN baeldung, tesla"); - Result result = graphDb.execute("MATCH (company:Company)-[:owns]-> (car:Car)" + + Result result = transaction.execute("MATCH (company:Company)-[:owns]-> (car:Car)" + "WHERE car.make='tesla' and car.model='modelX'" + "RETURN company.name"); Map firstResult = result.next(); + Assert.assertEquals(firstResult.get("company.name"), "Baeldung"); } @Test public void testUpdate() { - graphDb.beginTx(); - - graphDb.execute("CREATE (baeldung:Company {name:\"Baeldung\"}) " + + transaction = graphDb.beginTx(); + transaction.execute("CREATE (baeldung:Company {name:\"Baeldung\"}) " + "-[:owns]-> (tesla:Car {make: 'tesla', model: 'modelX'})" + "RETURN baeldung, tesla"); - Result result = graphDb.execute("MATCH (car:Car)" + + Result result = transaction.execute("MATCH (car:Car)" + "WHERE car.make='tesla'" + " SET car.milage=120" + " SET car :Car:Electro" + @@ -127,17 +136,16 @@ public class Neo4jLiveTest { @Test public void testDelete() { - graphDb.beginTx(); - - graphDb.execute("CREATE (baeldung:Company {name:\"Baeldung\"}) " + + transaction = graphDb.beginTx(); + transaction.execute("CREATE (baeldung:Company {name:\"Baeldung\"}) " + "-[:owns]-> (tesla:Car {make: 'tesla', model: 'modelX'})" + "RETURN baeldung, tesla"); - graphDb.execute("MATCH (company:Company)" + + transaction.execute("MATCH (company:Company)" + " WHERE company.name='Baeldung'" + " DELETE company"); - Result result = graphDb.execute("MATCH (company:Company)" + + Result result = transaction.execute("MATCH (company:Company)" + " WHERE company.name='Baeldung'" + " RETURN company"); @@ -146,14 +154,13 @@ public class Neo4jLiveTest { @Test public void testBindings() { - graphDb.beginTx(); - + transaction = graphDb.beginTx(); Map params = new HashMap<>(); params.put("name", "baeldung"); params.put("make", "tesla"); params.put("model", "modelS"); - Result result = graphDb.execute("CREATE (baeldung:Company {name:$name}) " + + Result result = transaction.execute("CREATE (baeldung:Company {name:$name}) " + "-[:owns]-> (tesla:Car {make: $make, model: $model})" + "RETURN baeldung, tesla", params); diff --git a/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/neo4j/Neo4jOgmLiveTest.java b/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/neo4j/Neo4jOgmLiveTest.java index 96e5e76402..e824873c32 100644 --- a/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/neo4j/Neo4jOgmLiveTest.java +++ b/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/neo4j/Neo4jOgmLiveTest.java @@ -1,11 +1,13 @@ package com.baeldung.neo4j; +import static com.baeldung.TestContainersTestBase.getDriver; +import static org.junit.jupiter.api.Assertions.assertEquals; + import java.util.HashMap; import java.util.Map; -import org.junit.Assert; -import org.junit.Test; -import org.neo4j.ogm.config.Configuration; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; import org.neo4j.ogm.model.Result; import org.neo4j.ogm.session.Session; import org.neo4j.ogm.session.SessionFactory; @@ -15,13 +17,18 @@ import com.baeldung.spring.data.neo4j.domain.Company; public class Neo4jOgmLiveTest { + private static SessionFactory sessionFactory; + private static Session session; + + @BeforeAll + public static void oneTimeSetUp() { + sessionFactory = new SessionFactory(getDriver(), "com.baeldung.spring.data.neo4j.domain"); + session = sessionFactory.openSession(); + session.purgeDatabase(); + } + @Test - public void testOgm() { - Configuration conf = new Configuration.Builder().build(); - - SessionFactory factory = new SessionFactory(conf, "com.baeldung.spring.data.neo4j.domain"); - Session session = factory.openSession(); - + void testOgm() { Car tesla = new Car("tesla", "modelS"); Company baeldung = new Company("baeldung"); @@ -29,7 +36,7 @@ public class Neo4jOgmLiveTest { session.save(baeldung); - Assert.assertEquals(1, session.countEntitiesOfType(Company.class)); + assertEquals(1, session.countEntitiesOfType(Company.class)); Map params = new HashMap<>(); params.put("make", "tesla"); @@ -39,9 +46,9 @@ public class Neo4jOgmLiveTest { Map firstResult = result.iterator().next(); - Assert.assertEquals(firstResult.size(), 1); + assertEquals(1, firstResult.size()); Company actual = (Company) firstResult.get("company"); - Assert.assertEquals(actual.getName(), baeldung.getName()); + assertEquals(actual.getName(), baeldung.getName()); } } diff --git a/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/spring/data/neo4j/MovieRepositoryIntegrationTest.java b/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/spring/data/neo4j/MovieRepositoryIntegrationTest.java index 3d9215f32f..f91fa2303c 100644 --- a/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/spring/data/neo4j/MovieRepositoryIntegrationTest.java +++ b/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/spring/data/neo4j/MovieRepositoryIntegrationTest.java @@ -75,7 +75,7 @@ public class MovieRepositoryIntegrationTest { public void testCount() { System.out.println("count"); long movieCount = movieRepository.count(); - assertNotNull(movieCount); + assertEquals(1, movieCount); } @@ -83,7 +83,7 @@ public class MovieRepositoryIntegrationTest { @DirtiesContext public void testFindAll() { System.out.println("findAll"); - Collection result = (Collection) movieRepository.findAll(); + Collection result = movieRepository.findAll(); assertNotNull(result); assertEquals(1, result.size()); } @@ -125,7 +125,7 @@ public class MovieRepositoryIntegrationTest { public void testDeleteAll() { System.out.println("deleteAll"); movieRepository.deleteAll(); - Collection result = (Collection) movieRepository.findAll(); + Collection result = movieRepository.findAll(); assertEquals(0, result.size()); } } From b50712ae429aafe125f207fd1417b0052fa85a89 Mon Sep 17 00:00:00 2001 From: timis1 Date: Tue, 13 Jun 2023 00:42:12 +0300 Subject: [PATCH 03/10] JAVA-20167 Changes in MovieRepositoryIntegrationTest --- persistence-modules/spring-data-neo4j/pom.xml | 12 ++++ .../neo4j/MovieRepositoryIntegrationTest.java | 67 +++++++++++++------ 2 files changed, 58 insertions(+), 21 deletions(-) diff --git a/persistence-modules/spring-data-neo4j/pom.xml b/persistence-modules/spring-data-neo4j/pom.xml index 705fe35d3c..a2fa29e753 100644 --- a/persistence-modules/spring-data-neo4j/pom.xml +++ b/persistence-modules/spring-data-neo4j/pom.xml @@ -105,6 +105,18 @@ + + org.neo4j.test + neo4j-harness + 5.5.0 + test + + + org.slf4j + slf4j-nop + + + diff --git a/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/spring/data/neo4j/MovieRepositoryIntegrationTest.java b/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/spring/data/neo4j/MovieRepositoryIntegrationTest.java index f91fa2303c..1878eb12b6 100644 --- a/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/spring/data/neo4j/MovieRepositoryIntegrationTest.java +++ b/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/spring/data/neo4j/MovieRepositoryIntegrationTest.java @@ -1,5 +1,9 @@ package com.baeldung.spring.data.neo4j; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; + import com.baeldung.spring.data.neo4j.config.MovieDatabaseNeo4jTestConfiguration; import com.baeldung.spring.data.neo4j.domain.Movie; import com.baeldung.spring.data.neo4j.domain.Person; @@ -7,37 +11,44 @@ import com.baeldung.spring.data.neo4j.domain.Role; import com.baeldung.spring.data.neo4j.repository.MovieRepository; import com.baeldung.spring.data.neo4j.repository.PersonRepository; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.neo4j.harness.Neo4j; +import org.neo4j.harness.Neo4jBuilders; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.data.neo4j.DataNeo4jTest; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; import java.util.*; -import static junit.framework.TestCase.assertNull; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - -@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = MovieDatabaseNeo4jTestConfiguration.class) @ActiveProfiles(profiles = "test") -public class MovieRepositoryIntegrationTest { +@DataNeo4jTest +class MovieRepositoryIntegrationTest { @Autowired - private MovieRepository movieRepository; + private static MovieRepository movieRepository; @Autowired - private PersonRepository personRepository; + private static PersonRepository personRepository; public MovieRepositoryIntegrationTest() { } - @Before - public void initializeDatabase() { + private static Neo4j embeddedDatabaseServer; + + @BeforeAll + static void initializeNeo4j() { + + embeddedDatabaseServer = Neo4jBuilders.newInProcessBuilder() + .withDisabledServer() + .build(); + System.out.println("seeding embedded database"); Movie italianJob = new Movie(); italianJob.setTitle("The Italian Job"); @@ -60,9 +71,23 @@ public class MovieRepositoryIntegrationTest { movieRepository.save(italianJob); } + @DynamicPropertySource + static void neo4jProperties(DynamicPropertyRegistry registry) { + + registry.add("spring.neo4j.uri", embeddedDatabaseServer::boltURI); + registry.add("spring.neo4j.authentication.username", () -> "neo4j"); + registry.add("spring.neo4j.authentication.password", () -> null); + } + + @AfterAll + static void stopNeo4j() { + + embeddedDatabaseServer.close(); + } + @Test @DirtiesContext - public void testFindByTitle() { + void testFindByTitle() { System.out.println("findByTitle"); String title = "The Italian Job"; Movie result = movieRepository.findByTitle(title); @@ -72,7 +97,7 @@ public class MovieRepositoryIntegrationTest { @Test @DirtiesContext - public void testCount() { + void testCount() { System.out.println("count"); long movieCount = movieRepository.count(); @@ -81,7 +106,7 @@ public class MovieRepositoryIntegrationTest { @Test @DirtiesContext - public void testFindAll() { + void testFindAll() { System.out.println("findAll"); Collection result = movieRepository.findAll(); assertNotNull(result); @@ -90,7 +115,7 @@ public class MovieRepositoryIntegrationTest { @Test @DirtiesContext - public void testFindByTitleContaining() { + void testFindByTitleContaining() { System.out.println("findByTitleContaining"); String title = "Italian"; Collection result = movieRepository.findByTitleContaining(title); @@ -100,7 +125,7 @@ public class MovieRepositoryIntegrationTest { @Test @DirtiesContext - public void testGraph() { + void testGraph() { System.out.println("graph"); List> graph = movieRepository.graph(5); assertEquals(1, graph.size()); @@ -114,7 +139,7 @@ public class MovieRepositoryIntegrationTest { @Test @DirtiesContext - public void testDeleteMovie() { + void testDeleteMovie() { System.out.println("deleteMovie"); movieRepository.delete(movieRepository.findByTitle("The Italian Job")); assertNull(movieRepository.findByTitle("The Italian Job")); @@ -122,7 +147,7 @@ public class MovieRepositoryIntegrationTest { @Test @DirtiesContext - public void testDeleteAll() { + void testDeleteAll() { System.out.println("deleteAll"); movieRepository.deleteAll(); Collection result = movieRepository.findAll(); From 2a4950a9d74ae6334e6059d3dca4c6535315f99b Mon Sep 17 00:00:00 2001 From: timis1 Date: Tue, 13 Jun 2023 20:40:31 +0300 Subject: [PATCH 04/10] JAVA-20167 Additional changes for test MovieRepositoryIntegrationTest --- .gitignore | 5 ++++- persistence-modules/spring-data-neo4j/pom.xml | 4 +++- .../data/neo4j/MovieRepositoryIntegrationTest.java | 12 ++++++++---- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 7725bf202e..f124a385d4 100644 --- a/.gitignore +++ b/.gitignore @@ -120,4 +120,7 @@ libraries-2/src/test/resources/crawler4j/** devDb*.db #jaxb -*.xjb \ No newline at end of file +*.xjb + +#spring-data-neo4j +persistence-modules/spring-data-neo4j/data/** \ No newline at end of file diff --git a/persistence-modules/spring-data-neo4j/pom.xml b/persistence-modules/spring-data-neo4j/pom.xml index a2fa29e753..75d0aefc23 100644 --- a/persistence-modules/spring-data-neo4j/pom.xml +++ b/persistence-modules/spring-data-neo4j/pom.xml @@ -108,7 +108,7 @@ org.neo4j.test neo4j-harness - 5.5.0 + ${neo4j.version} test @@ -129,6 +129,8 @@ 4.0.4 4.0.9 3.2.39 + 2.0.7 + 1.4.7 \ No newline at end of file diff --git a/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/spring/data/neo4j/MovieRepositoryIntegrationTest.java b/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/spring/data/neo4j/MovieRepositoryIntegrationTest.java index 1878eb12b6..5af501c900 100644 --- a/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/spring/data/neo4j/MovieRepositoryIntegrationTest.java +++ b/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/spring/data/neo4j/MovieRepositoryIntegrationTest.java @@ -13,6 +13,7 @@ import com.baeldung.spring.data.neo4j.repository.PersonRepository; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.neo4j.harness.Neo4j; import org.neo4j.harness.Neo4jBuilders; @@ -32,10 +33,10 @@ import java.util.*; class MovieRepositoryIntegrationTest { @Autowired - private static MovieRepository movieRepository; + private MovieRepository movieRepository; @Autowired - private static PersonRepository personRepository; + private PersonRepository personRepository; public MovieRepositoryIntegrationTest() { } @@ -43,11 +44,14 @@ class MovieRepositoryIntegrationTest { private static Neo4j embeddedDatabaseServer; @BeforeAll - static void initializeNeo4j() { - + static void setup() { embeddedDatabaseServer = Neo4jBuilders.newInProcessBuilder() .withDisabledServer() .build(); + } + + @BeforeEach + void initializeNeo4j() { System.out.println("seeding embedded database"); Movie italianJob = new Movie(); From 19039a3d2e80b983b54f41ec6dc275d4b0b21d9a Mon Sep 17 00:00:00 2001 From: n Date: Thu, 15 Jun 2023 20:37:20 +0300 Subject: [PATCH 05/10] Revert "JAVA-20167 Additional changes for test MovieRepositoryIntegrationTest" This reverts commit 2a4950a9d74ae6334e6059d3dca4c6535315f99b. --- .gitignore | 5 +---- persistence-modules/spring-data-neo4j/pom.xml | 4 +--- .../data/neo4j/MovieRepositoryIntegrationTest.java | 12 ++++-------- 3 files changed, 6 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index f124a385d4..7725bf202e 100644 --- a/.gitignore +++ b/.gitignore @@ -120,7 +120,4 @@ libraries-2/src/test/resources/crawler4j/** devDb*.db #jaxb -*.xjb - -#spring-data-neo4j -persistence-modules/spring-data-neo4j/data/** \ No newline at end of file +*.xjb \ No newline at end of file diff --git a/persistence-modules/spring-data-neo4j/pom.xml b/persistence-modules/spring-data-neo4j/pom.xml index 75d0aefc23..a2fa29e753 100644 --- a/persistence-modules/spring-data-neo4j/pom.xml +++ b/persistence-modules/spring-data-neo4j/pom.xml @@ -108,7 +108,7 @@ org.neo4j.test neo4j-harness - ${neo4j.version} + 5.5.0 test @@ -129,8 +129,6 @@ 4.0.4 4.0.9 3.2.39 - 2.0.7 - 1.4.7 \ No newline at end of file diff --git a/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/spring/data/neo4j/MovieRepositoryIntegrationTest.java b/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/spring/data/neo4j/MovieRepositoryIntegrationTest.java index 5af501c900..1878eb12b6 100644 --- a/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/spring/data/neo4j/MovieRepositoryIntegrationTest.java +++ b/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/spring/data/neo4j/MovieRepositoryIntegrationTest.java @@ -13,7 +13,6 @@ import com.baeldung.spring.data.neo4j.repository.PersonRepository; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.neo4j.harness.Neo4j; import org.neo4j.harness.Neo4jBuilders; @@ -33,10 +32,10 @@ import java.util.*; class MovieRepositoryIntegrationTest { @Autowired - private MovieRepository movieRepository; + private static MovieRepository movieRepository; @Autowired - private PersonRepository personRepository; + private static PersonRepository personRepository; public MovieRepositoryIntegrationTest() { } @@ -44,14 +43,11 @@ class MovieRepositoryIntegrationTest { private static Neo4j embeddedDatabaseServer; @BeforeAll - static void setup() { + static void initializeNeo4j() { + embeddedDatabaseServer = Neo4jBuilders.newInProcessBuilder() .withDisabledServer() .build(); - } - - @BeforeEach - void initializeNeo4j() { System.out.println("seeding embedded database"); Movie italianJob = new Movie(); From 17d635bd0dd6da2ffd2bfbd65e67ea8003d705e1 Mon Sep 17 00:00:00 2001 From: n Date: Thu, 15 Jun 2023 20:37:24 +0300 Subject: [PATCH 06/10] Revert "JAVA-20167 Changes in MovieRepositoryIntegrationTest" This reverts commit b50712ae429aafe125f207fd1417b0052fa85a89. --- persistence-modules/spring-data-neo4j/pom.xml | 12 ---- .../neo4j/MovieRepositoryIntegrationTest.java | 67 ++++++------------- 2 files changed, 21 insertions(+), 58 deletions(-) diff --git a/persistence-modules/spring-data-neo4j/pom.xml b/persistence-modules/spring-data-neo4j/pom.xml index a2fa29e753..705fe35d3c 100644 --- a/persistence-modules/spring-data-neo4j/pom.xml +++ b/persistence-modules/spring-data-neo4j/pom.xml @@ -105,18 +105,6 @@ - - org.neo4j.test - neo4j-harness - 5.5.0 - test - - - org.slf4j - slf4j-nop - - - diff --git a/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/spring/data/neo4j/MovieRepositoryIntegrationTest.java b/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/spring/data/neo4j/MovieRepositoryIntegrationTest.java index 1878eb12b6..f91fa2303c 100644 --- a/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/spring/data/neo4j/MovieRepositoryIntegrationTest.java +++ b/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/spring/data/neo4j/MovieRepositoryIntegrationTest.java @@ -1,9 +1,5 @@ package com.baeldung.spring.data.neo4j; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; - import com.baeldung.spring.data.neo4j.config.MovieDatabaseNeo4jTestConfiguration; import com.baeldung.spring.data.neo4j.domain.Movie; import com.baeldung.spring.data.neo4j.domain.Person; @@ -11,44 +7,37 @@ import com.baeldung.spring.data.neo4j.domain.Role; import com.baeldung.spring.data.neo4j.repository.MovieRepository; import com.baeldung.spring.data.neo4j.repository.PersonRepository; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import org.neo4j.harness.Neo4j; -import org.neo4j.harness.Neo4jBuilders; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.data.neo4j.DataNeo4jTest; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.DynamicPropertyRegistry; -import org.springframework.test.context.DynamicPropertySource; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.*; +import static junit.framework.TestCase.assertNull; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = MovieDatabaseNeo4jTestConfiguration.class) @ActiveProfiles(profiles = "test") -@DataNeo4jTest -class MovieRepositoryIntegrationTest { +public class MovieRepositoryIntegrationTest { @Autowired - private static MovieRepository movieRepository; + private MovieRepository movieRepository; @Autowired - private static PersonRepository personRepository; + private PersonRepository personRepository; public MovieRepositoryIntegrationTest() { } - private static Neo4j embeddedDatabaseServer; - - @BeforeAll - static void initializeNeo4j() { - - embeddedDatabaseServer = Neo4jBuilders.newInProcessBuilder() - .withDisabledServer() - .build(); - + @Before + public void initializeDatabase() { System.out.println("seeding embedded database"); Movie italianJob = new Movie(); italianJob.setTitle("The Italian Job"); @@ -71,23 +60,9 @@ class MovieRepositoryIntegrationTest { movieRepository.save(italianJob); } - @DynamicPropertySource - static void neo4jProperties(DynamicPropertyRegistry registry) { - - registry.add("spring.neo4j.uri", embeddedDatabaseServer::boltURI); - registry.add("spring.neo4j.authentication.username", () -> "neo4j"); - registry.add("spring.neo4j.authentication.password", () -> null); - } - - @AfterAll - static void stopNeo4j() { - - embeddedDatabaseServer.close(); - } - @Test @DirtiesContext - void testFindByTitle() { + public void testFindByTitle() { System.out.println("findByTitle"); String title = "The Italian Job"; Movie result = movieRepository.findByTitle(title); @@ -97,7 +72,7 @@ class MovieRepositoryIntegrationTest { @Test @DirtiesContext - void testCount() { + public void testCount() { System.out.println("count"); long movieCount = movieRepository.count(); @@ -106,7 +81,7 @@ class MovieRepositoryIntegrationTest { @Test @DirtiesContext - void testFindAll() { + public void testFindAll() { System.out.println("findAll"); Collection result = movieRepository.findAll(); assertNotNull(result); @@ -115,7 +90,7 @@ class MovieRepositoryIntegrationTest { @Test @DirtiesContext - void testFindByTitleContaining() { + public void testFindByTitleContaining() { System.out.println("findByTitleContaining"); String title = "Italian"; Collection result = movieRepository.findByTitleContaining(title); @@ -125,7 +100,7 @@ class MovieRepositoryIntegrationTest { @Test @DirtiesContext - void testGraph() { + public void testGraph() { System.out.println("graph"); List> graph = movieRepository.graph(5); assertEquals(1, graph.size()); @@ -139,7 +114,7 @@ class MovieRepositoryIntegrationTest { @Test @DirtiesContext - void testDeleteMovie() { + public void testDeleteMovie() { System.out.println("deleteMovie"); movieRepository.delete(movieRepository.findByTitle("The Italian Job")); assertNull(movieRepository.findByTitle("The Italian Job")); @@ -147,7 +122,7 @@ class MovieRepositoryIntegrationTest { @Test @DirtiesContext - void testDeleteAll() { + public void testDeleteAll() { System.out.println("deleteAll"); movieRepository.deleteAll(); Collection result = movieRepository.findAll(); From a0bf12b9d4edee1c831d8b8568381bc807e6a34e Mon Sep 17 00:00:00 2001 From: n Date: Thu, 15 Jun 2023 20:37:30 +0300 Subject: [PATCH 07/10] Revert "JAVA-20167 Migrating spring-data-neo4j" This reverts commit 2fd4c39a21cea9c62d313475b3df6fc1a3f3b087. --- persistence-modules/pom.xml | 6 +- persistence-modules/spring-data-neo4j/pom.xml | 59 +++----- .../MovieDatabaseNeo4jTestConfiguration.java | 22 +-- .../spring/data/neo4j/domain/Car.java | 4 +- .../spring/data/neo4j/domain/Movie.java | 5 +- .../neo4j/repository/MovieRepository.java | 4 +- .../java/com/baeldung/SpringContextTest.java | 9 +- .../com/baeldung/TestContainersTestBase.java | 132 ------------------ .../baeldung/neo4j/Neo4JServerLiveTest.java | 56 +++----- .../com/baeldung/neo4j/Neo4jLiveTest.java | 69 ++++----- .../com/baeldung/neo4j/Neo4jOgmLiveTest.java | 31 ++-- .../neo4j/MovieRepositoryIntegrationTest.java | 6 +- 12 files changed, 108 insertions(+), 295 deletions(-) delete mode 100644 persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/TestContainersTestBase.java diff --git a/persistence-modules/pom.xml b/persistence-modules/pom.xml index 1501d81985..9361e26d7d 100644 --- a/persistence-modules/pom.xml +++ b/persistence-modules/pom.xml @@ -40,6 +40,8 @@ java-jpa java-jpa-2 java-jpa-3 + + java-mongodb-2 java-mongodb-3 java-mongodb-queries @@ -88,10 +90,10 @@ spring-data-jpa-repo-2 spring-data-jdbc spring-data-keyvalue - spring-data-mongodb + spring-data-mongodb-2 spring-data-mongodb-reactive - spring-data-neo4j + spring-data-redis diff --git a/persistence-modules/spring-data-neo4j/pom.xml b/persistence-modules/spring-data-neo4j/pom.xml index 705fe35d3c..8c5030779b 100644 --- a/persistence-modules/spring-data-neo4j/pom.xml +++ b/persistence-modules/spring-data-neo4j/pom.xml @@ -27,11 +27,6 @@ org.neo4j neo4j-ogm-embedded-driver - ${neo4j-ogm-embedded-driver.version} - - - org.neo4j - neo4j-ogm-bolt-driver ${neo4j-ogm.version} @@ -39,12 +34,6 @@ neo4j-java-driver ${neo4j-java-driver.version} - - org.neo4j - neo4j-jdbc-driver - ${neo4j-jdbc.version} - runtime - org.springframework.data spring-data-neo4j @@ -80,43 +69,33 @@ + + org.neo4j + neo4j-ogm-test + ${neo4j-ogm.version} + test + + + org.neo4j.test + neo4j-harness + ${neo4j.version} + test + org.springframework spring-test ${spring-test.version} - - org.testcontainers - neo4j - ${testcontainers.version} - test - - - org.jetbrains - annotations - - - org.apache.commons - commons-compress - - - javax.xml.bind - jaxb-api - - - - 5.6.0 - 5.8.0 - 7.0.3 - 1.1.2 - 3.1.0 - 6.0.8 - 4.0.4 - 4.0.9 - 3.2.39 + 1.6.2 + 3.4.6 + 5.0.1.RELEASE + 1.1 + 2.0.1.RELEASE + 5.0.1.RELEASE + 3.1.2 \ No newline at end of file diff --git a/persistence-modules/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/config/MovieDatabaseNeo4jTestConfiguration.java b/persistence-modules/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/config/MovieDatabaseNeo4jTestConfiguration.java index f3c89c1bbe..a4cbe4b809 100644 --- a/persistence-modules/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/config/MovieDatabaseNeo4jTestConfiguration.java +++ b/persistence-modules/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/config/MovieDatabaseNeo4jTestConfiguration.java @@ -1,16 +1,13 @@ package com.baeldung.spring.data.neo4j.config; -import org.neo4j.driver.Driver; +import org.neo4j.ogm.config.Configuration.Builder; import org.neo4j.ogm.session.SessionFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; -import org.springframework.data.neo4j.core.DatabaseSelectionProvider; -import org.springframework.data.neo4j.core.UserSelectionProvider; -import org.springframework.data.neo4j.core.transaction.Neo4jTransactionManager; import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories; -import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.data.neo4j.transaction.Neo4jTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; @Configuration @@ -22,23 +19,18 @@ public class MovieDatabaseNeo4jTestConfiguration { @Bean public org.neo4j.ogm.config.Configuration getConfiguration() { - return new org.neo4j.ogm.config.Configuration.Builder().build(); + org.neo4j.ogm.config.Configuration config = new Builder().build(); + return config; } @Bean public SessionFactory getSessionFactory() { return new SessionFactory(getConfiguration(), "com.baeldung.spring.data.neo4j.domain"); } - + @Bean - public PlatformTransactionManager transactionManager(Driver driver, - DatabaseSelectionProvider databaseSelectionProvider, UserSelectionProvider userSelectionProvider) { - - return Neo4jTransactionManager - .with(driver) - .withDatabaseSelectionProvider(databaseSelectionProvider) - .withUserSelectionProvider(userSelectionProvider) - .build(); + public Neo4jTransactionManager transactionManager() { + return new Neo4jTransactionManager(getSessionFactory()); } } diff --git a/persistence-modules/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/domain/Car.java b/persistence-modules/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/domain/Car.java index 0f516d3758..455407a92b 100644 --- a/persistence-modules/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/domain/Car.java +++ b/persistence-modules/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/domain/Car.java @@ -1,7 +1,5 @@ package com.baeldung.spring.data.neo4j.domain; -import static org.neo4j.ogm.annotation.Relationship.Direction.INCOMING; - import org.neo4j.ogm.annotation.GeneratedValue; import org.neo4j.ogm.annotation.Id; import org.neo4j.ogm.annotation.NodeEntity; @@ -14,7 +12,7 @@ public class Car { private String make; - @Relationship(direction = INCOMING) + @Relationship(direction = "INCOMING") private Company company; public Car(String make, String model) { diff --git a/persistence-modules/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/domain/Movie.java b/persistence-modules/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/domain/Movie.java index bea9f996aa..996a661b07 100644 --- a/persistence-modules/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/domain/Movie.java +++ b/persistence-modules/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/domain/Movie.java @@ -1,7 +1,5 @@ package com.baeldung.spring.data.neo4j.domain; -import static org.neo4j.ogm.annotation.Relationship.Direction.INCOMING; - import com.fasterxml.jackson.annotation.JsonIdentityInfo; import com.voodoodyne.jackson.jsog.JSOGGenerator; @@ -14,6 +12,7 @@ import java.util.Collection; import java.util.List; @JsonIdentityInfo(generator = JSOGGenerator.class) + @NodeEntity public class Movie { @Id @GeneratedValue @@ -24,7 +23,7 @@ public class Movie { private int released; private String tagline; - @Relationship(type = "ACTED_IN", direction = INCOMING) + @Relationship(type = "ACTED_IN", direction = Relationship.INCOMING) private List roles; public Movie() { diff --git a/persistence-modules/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/repository/MovieRepository.java b/persistence-modules/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/repository/MovieRepository.java index 732970c0b5..dde946ea73 100644 --- a/persistence-modules/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/repository/MovieRepository.java +++ b/persistence-modules/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/repository/MovieRepository.java @@ -1,8 +1,8 @@ package com.baeldung.spring.data.neo4j.repository; import com.baeldung.spring.data.neo4j.domain.Movie; +import org.springframework.data.neo4j.annotation.Query; import org.springframework.data.neo4j.repository.Neo4jRepository; -import org.springframework.data.neo4j.repository.query.Query; import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; @@ -13,7 +13,7 @@ import java.util.Map; @Repository public interface MovieRepository extends Neo4jRepository { - Movie findByTitle(String title); + Movie findByTitle(@Param("title") String title); @Query("MATCH (m:Movie) WHERE m.title =~ ('(?i).*'+{title}+'.*') RETURN m") Collection findByTitleContaining(@Param("title") String title); diff --git a/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/SpringContextTest.java b/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/SpringContextTest.java index 9c96a98ff2..7029905c49 100644 --- a/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/SpringContextTest.java +++ b/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/SpringContextTest.java @@ -1,16 +1,19 @@ package com.baeldung; -import org.junit.jupiter.api.Test; +import org.junit.Test; +import org.junit.runner.RunWith; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.baeldung.spring.data.neo4j.config.MovieDatabaseNeo4jTestConfiguration; +@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = MovieDatabaseNeo4jTestConfiguration.class) @ActiveProfiles(profiles = "test") -class SpringContextTest { +public class SpringContextTest { @Test - void whenSpringContextIsBootstrapped_thenNoExceptions() { + public void whenSpringContextIsBootstrapped_thenNoExceptions() { } } diff --git a/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/TestContainersTestBase.java b/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/TestContainersTestBase.java deleted file mode 100644 index e2bd022d12..0000000000 --- a/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/TestContainersTestBase.java +++ /dev/null @@ -1,132 +0,0 @@ -package com.baeldung; - -import static org.neo4j.ogm.drivers.bolt.driver.BoltDriver.CONFIG_PARAMETER_BOLT_LOGGING; - -import java.util.Locale; -import java.util.Optional; - -import org.neo4j.driver.AccessMode; -import org.neo4j.driver.AuthTokens; -import org.neo4j.driver.GraphDatabase; -import org.neo4j.driver.Logging; -import org.neo4j.driver.Session; -import org.neo4j.driver.SessionConfig; -import org.neo4j.ogm.config.Configuration; -import org.neo4j.ogm.driver.Driver; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.testcontainers.containers.Neo4jContainer; -import org.testcontainers.utility.TestcontainersConfiguration; - -/** - * Reference: https://github.com/neo4j/neo4j-ogm/blob/master/neo4j-ogm-tests/neo4j-ogm-integration-tests/src/test/java/org/neo4j/ogm/testutil/TestContainersTestBase.java - */ -public class TestContainersTestBase { - - public static final Logger LOGGER = LoggerFactory.getLogger(TestContainersTestBase.class); - - public static final Driver driver; - - public static final String version; - - public static final String DEFAULT_IMAGE = "neo4j:5"; - - public static final String SYS_PROPERTY_ACCEPT_AND_USE_COMMERCIAL_EDITION = "NEO4J_OGM_NEO4J_ACCEPT_AND_USE_COMMERCIAL_EDITION"; - - public static final String SYS_PROPERTY_IMAGE_NAME = "NEO4J_OGM_NEO4J_IMAGE_NAME"; - - public static final String SYS_PROPERTY_NEO4J_URL = "NEO4J_OGM_NEO4J_URL"; - - public static final String SYS_PROPERTY_NEO4J_PASSWORD = "NEO4J_OGM_NEO4J_PASSWORD"; - - public static final String DEFAULT_PASSWORD = "verysecret"; - - public static Neo4jContainer neo4jServer; - - public static Configuration.Builder baseConfigurationBuilder; - - public static final String NEO4J_URL = Optional.ofNullable(System.getenv(SYS_PROPERTY_NEO4J_URL)).orElse(""); - - public static final String NEO4J_PASSWORD = Optional.ofNullable(System.getenv(SYS_PROPERTY_NEO4J_PASSWORD)).orElse("").trim(); - - static { - - boolean acceptAndUseCommercialEdition = hasAcceptedAndWantsToUseCommercialEdition(); - - if (!(NEO4J_URL.isEmpty() || NEO4J_PASSWORD.isEmpty())) { - LOGGER.info("Using Neo4j instance at {}.", NEO4J_URL); - driver = new org.neo4j.ogm.drivers.bolt.driver.BoltDriver(); - baseConfigurationBuilder = new Configuration.Builder() - .uri(NEO4J_URL) - .verifyConnection(true) - .withCustomProperty(CONFIG_PARAMETER_BOLT_LOGGING, Logging.slf4j()) - .credentials("neo4j", NEO4J_PASSWORD); - driver.configure(baseConfigurationBuilder.build()); - version = extractVersionFromBolt(); - } else { - LOGGER.info("Using Neo4j test container."); - String imageName = Optional.ofNullable(System.getenv(SYS_PROPERTY_IMAGE_NAME)) - .orElse(DEFAULT_IMAGE + (acceptAndUseCommercialEdition ? "-enterprise" : "")); - - version = extractVersionFromDockerImage(imageName); - - boolean containerReuseSupported = TestcontainersConfiguration - .getInstance().environmentSupportsReuse(); - neo4jServer = new Neo4jContainer<>(imageName) - .withReuse(containerReuseSupported); - - if (acceptAndUseCommercialEdition) { - neo4jServer.withEnv("NEO4J_ACCEPT_LICENSE_AGREEMENT", "yes"); - } - neo4jServer.withAdminPassword(DEFAULT_PASSWORD).start(); - - driver = new org.neo4j.ogm.drivers.bolt.driver.BoltDriver(); - - baseConfigurationBuilder = new Configuration.Builder() - .uri(neo4jServer.getBoltUrl()) - .credentials("neo4j", DEFAULT_PASSWORD) - .verifyConnection(true) - .withCustomProperty(CONFIG_PARAMETER_BOLT_LOGGING, Logging.slf4j()); - - driver.configure(baseConfigurationBuilder.build()); - - Runtime.getRuntime().addShutdownHook(new Thread(neo4jServer::stop)); - } - } - - public static org.neo4j.driver.Driver getNewBoltConnection() { - - if (neo4jServer != null) { - return GraphDatabase.driver(neo4jServer.getBoltUrl(), AuthTokens.basic("neo4j", DEFAULT_PASSWORD)); - } else { - return GraphDatabase.driver(NEO4J_URL, AuthTokens.basic("neo4j", NEO4J_PASSWORD)); - } - } - - public static boolean hasAcceptedAndWantsToUseCommercialEdition() { - return Optional.ofNullable( - System.getenv(TestContainersTestBase.SYS_PROPERTY_ACCEPT_AND_USE_COMMERCIAL_EDITION)) - .orElse("no").toLowerCase(Locale.ENGLISH).equals("yes"); - } - - public static Driver getDriver() { - return driver; - } - - private static String extractVersionFromDockerImage(String imageName) { - return imageName.replace("neo4j:", "").replace("neo4j/neo4j-experimental:", "").replace("-enterprise", ""); - } - - private static String extractVersionFromBolt() { - - org.neo4j.driver.Driver driver = getDriver().unwrap(org.neo4j.driver.Driver.class); - - String version; - SessionConfig sessionConfig = SessionConfig.builder().withDefaultAccessMode(AccessMode.READ).build(); - try (Session session = driver.session(sessionConfig)) { - version = (String) session.run("CALL dbms.components() YIELD versions").single().get("versions").asList().get(0); - } - return version.toLowerCase(Locale.ENGLISH); - } - -} \ No newline at end of file diff --git a/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/neo4j/Neo4JServerLiveTest.java b/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/neo4j/Neo4JServerLiveTest.java index 3356d90291..bf187ccb2d 100644 --- a/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/neo4j/Neo4JServerLiveTest.java +++ b/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/neo4j/Neo4JServerLiveTest.java @@ -1,59 +1,45 @@ package com.baeldung.neo4j; -import static com.baeldung.TestContainersTestBase.DEFAULT_PASSWORD; -import static com.baeldung.TestContainersTestBase.getNewBoltConnection; -import static com.baeldung.TestContainersTestBase.neo4jServer; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; - import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import org.neo4j.driver.Driver; -import org.neo4j.driver.Result; -import org.neo4j.driver.Session; +import org.junit.Assert; +import org.junit.Ignore; +import org.junit.Test; +import org.neo4j.driver.v1.AuthTokens; +import org.neo4j.driver.v1.Driver; +import org.neo4j.driver.v1.GraphDatabase; +import org.neo4j.driver.v1.Session; +import org.neo4j.driver.v1.StatementResult; -class Neo4JServerLiveTest { - - private static Session session; - private static Driver driver; - - @BeforeAll - public static void setup() { - driver = getNewBoltConnection(); - } - - @AfterAll - public static void tearDown() { - driver.close(); - } +@Ignore +public class Neo4JServerLiveTest { @Test - void standAloneDriver() { - session = driver.session(); + public void standAloneDriver() { + Driver driver = GraphDatabase.driver("bolt://localhost:7687", AuthTokens.basic("neo4j", "12345")); + Session session = driver.session(); + session.run("CREATE (baeldung:Company {name:\"Baeldung\"}) " + "-[:owns]-> (tesla:Car {make: 'tesla', model: 'modelX'})" + "RETURN baeldung, tesla"); - Result result = session.run("MATCH (company:Company)-[:owns]-> (car:Car)" + + StatementResult result = session.run("MATCH (company:Company)-[:owns]-> (car:Car)" + "WHERE car.make='tesla' and car.model='modelX'" + "RETURN company.name"); - assertTrue(result.hasNext()); - assertEquals("Baeldung", result.next().get("company.name").asString()); + Assert.assertTrue(result.hasNext()); + Assert.assertEquals(result.next().get("company.name").asString(), "Baeldung"); session.close(); + driver.close(); } @Test - void standAloneJdbc() throws Exception { - String uri = "jdbc:neo4j:" + neo4jServer.getBoltUrl() + "/?user=neo4j,password=" + DEFAULT_PASSWORD + ",scheme=basic"; - Connection con = DriverManager.getConnection(uri); + public void standAloneJdbc() throws Exception { + Connection con = DriverManager.getConnection("jdbc:neo4j:bolt://localhost/?user=neo4j,password=12345,scheme=basic"); // Querying try (Statement stmt = con.createStatement()) { @@ -66,7 +52,7 @@ class Neo4JServerLiveTest { "RETURN company.name"); while (rs.next()) { - assertEquals("Baeldung", rs.getString("company.name")); + Assert.assertEquals(rs.getString("company.name"), "Baeldung"); } } con.close(); diff --git a/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/neo4j/Neo4jLiveTest.java b/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/neo4j/Neo4jLiveTest.java index 840f4536ca..1ff01b93a1 100644 --- a/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/neo4j/Neo4jLiveTest.java +++ b/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/neo4j/Neo4jLiveTest.java @@ -1,10 +1,7 @@ package com.baeldung.neo4j; -import static org.neo4j.configuration.GraphDatabaseSettings.DEFAULT_DATABASE_NAME; - import java.io.File; -import java.time.Duration; import java.util.Arrays; import java.util.HashMap; import java.util.Map; @@ -13,107 +10,101 @@ import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; -import org.neo4j.configuration.GraphDatabaseSettings; -import org.neo4j.dbms.api.DatabaseManagementService; -import org.neo4j.dbms.api.DatabaseManagementServiceBuilder; import org.neo4j.graphdb.GraphDatabaseService; import org.neo4j.graphdb.Label; import org.neo4j.graphdb.Node; import org.neo4j.graphdb.NotFoundException; import org.neo4j.graphdb.RelationshipType; import org.neo4j.graphdb.Result; -import org.neo4j.graphdb.Transaction; +import org.neo4j.graphdb.factory.GraphDatabaseFactory; public class Neo4jLiveTest { private static GraphDatabaseService graphDb; - private static Transaction transaction; - private static DatabaseManagementService managementService; @Before public void setUp() { - managementService = new DatabaseManagementServiceBuilder(new File("data/cars").toPath()) - .setConfig( GraphDatabaseSettings.transaction_timeout, Duration.ofSeconds( 60 ) ) - .setConfig( GraphDatabaseSettings.preallocate_logical_logs, false ).build(); - graphDb = managementService.database( DEFAULT_DATABASE_NAME ); + GraphDatabaseFactory graphDbFactory = new GraphDatabaseFactory(); + graphDb = graphDbFactory.newEmbeddedDatabase(new File("data/cars")); } @After public void tearDown() { - managementService.shutdown(); + graphDb.shutdown(); } @Test public void testPersonCar() { - transaction = graphDb.beginTx(); - Node car = transaction.createNode(Label.label("Car")); + graphDb.beginTx(); + Node car = graphDb.createNode(Label.label("Car")); car.setProperty("make", "tesla"); car.setProperty("model", "model3"); - Node owner = transaction.createNode(Label.label("Person")); + Node owner = graphDb.createNode(Label.label("Person")); owner.setProperty("firstName", "baeldung"); owner.setProperty("lastName", "baeldung"); owner.createRelationshipTo(car, RelationshipType.withName("owner")); - Result result = transaction.execute("MATCH (c:Car) <-[owner]- (p:Person) " + + Result result = graphDb.execute("MATCH (c:Car) <-[owner]- (p:Person) " + "WHERE c.make = 'tesla'" + "RETURN p.firstName, p.lastName"); Map firstResult = result.next(); - Assert.assertEquals("baeldung", firstResult.get("p.firstName")); } @Test public void testCreateNode() { - transaction = graphDb.beginTx(); - Result result = transaction.execute("CREATE (baeldung:Company {name:\"Baeldung\"})" + + + graphDb.beginTx(); + + Result result = graphDb.execute("CREATE (baeldung:Company {name:\"Baeldung\"})" + "RETURN baeldung"); Map firstResult = result.next(); Node firstNode = (Node) firstResult.get("baeldung"); - Assert.assertEquals(firstNode.getProperty("name"), "Baeldung"); } @Test public void testCreateNodeAndLink() { - transaction = graphDb.beginTx(); - Result result = transaction.execute("CREATE (baeldung:Company {name:\"Baeldung\"}) " + + graphDb.beginTx(); + + Result result = graphDb.execute("CREATE (baeldung:Company {name:\"Baeldung\"}) " + "-[:owns]-> (tesla:Car {make: 'tesla', model: 'modelX'})" + "RETURN baeldung, tesla"); Map firstResult = result.next(); - Assert.assertTrue(firstResult.containsKey("baeldung")); Assert.assertTrue(firstResult.containsKey("tesla")); } @Test public void testFindAndReturn() { - transaction = graphDb.beginTx(); - transaction.execute("CREATE (baeldung:Company {name:\"Baeldung\"}) " + + graphDb.beginTx(); + + graphDb.execute("CREATE (baeldung:Company {name:\"Baeldung\"}) " + "-[:owns]-> (tesla:Car {make: 'tesla', model: 'modelX'})" + "RETURN baeldung, tesla"); - Result result = transaction.execute("MATCH (company:Company)-[:owns]-> (car:Car)" + + Result result = graphDb.execute("MATCH (company:Company)-[:owns]-> (car:Car)" + "WHERE car.make='tesla' and car.model='modelX'" + "RETURN company.name"); Map firstResult = result.next(); - Assert.assertEquals(firstResult.get("company.name"), "Baeldung"); } @Test public void testUpdate() { - transaction = graphDb.beginTx(); - transaction.execute("CREATE (baeldung:Company {name:\"Baeldung\"}) " + + graphDb.beginTx(); + + graphDb.execute("CREATE (baeldung:Company {name:\"Baeldung\"}) " + "-[:owns]-> (tesla:Car {make: 'tesla', model: 'modelX'})" + "RETURN baeldung, tesla"); - Result result = transaction.execute("MATCH (car:Car)" + + Result result = graphDb.execute("MATCH (car:Car)" + "WHERE car.make='tesla'" + " SET car.milage=120" + " SET car :Car:Electro" + @@ -136,16 +127,17 @@ public class Neo4jLiveTest { @Test public void testDelete() { - transaction = graphDb.beginTx(); - transaction.execute("CREATE (baeldung:Company {name:\"Baeldung\"}) " + + graphDb.beginTx(); + + graphDb.execute("CREATE (baeldung:Company {name:\"Baeldung\"}) " + "-[:owns]-> (tesla:Car {make: 'tesla', model: 'modelX'})" + "RETURN baeldung, tesla"); - transaction.execute("MATCH (company:Company)" + + graphDb.execute("MATCH (company:Company)" + " WHERE company.name='Baeldung'" + " DELETE company"); - Result result = transaction.execute("MATCH (company:Company)" + + Result result = graphDb.execute("MATCH (company:Company)" + " WHERE company.name='Baeldung'" + " RETURN company"); @@ -154,13 +146,14 @@ public class Neo4jLiveTest { @Test public void testBindings() { - transaction = graphDb.beginTx(); + graphDb.beginTx(); + Map params = new HashMap<>(); params.put("name", "baeldung"); params.put("make", "tesla"); params.put("model", "modelS"); - Result result = transaction.execute("CREATE (baeldung:Company {name:$name}) " + + Result result = graphDb.execute("CREATE (baeldung:Company {name:$name}) " + "-[:owns]-> (tesla:Car {make: $make, model: $model})" + "RETURN baeldung, tesla", params); diff --git a/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/neo4j/Neo4jOgmLiveTest.java b/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/neo4j/Neo4jOgmLiveTest.java index e824873c32..96e5e76402 100644 --- a/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/neo4j/Neo4jOgmLiveTest.java +++ b/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/neo4j/Neo4jOgmLiveTest.java @@ -1,13 +1,11 @@ package com.baeldung.neo4j; -import static com.baeldung.TestContainersTestBase.getDriver; -import static org.junit.jupiter.api.Assertions.assertEquals; - import java.util.HashMap; import java.util.Map; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; +import org.neo4j.ogm.config.Configuration; import org.neo4j.ogm.model.Result; import org.neo4j.ogm.session.Session; import org.neo4j.ogm.session.SessionFactory; @@ -17,18 +15,13 @@ import com.baeldung.spring.data.neo4j.domain.Company; public class Neo4jOgmLiveTest { - private static SessionFactory sessionFactory; - private static Session session; - - @BeforeAll - public static void oneTimeSetUp() { - sessionFactory = new SessionFactory(getDriver(), "com.baeldung.spring.data.neo4j.domain"); - session = sessionFactory.openSession(); - session.purgeDatabase(); - } - @Test - void testOgm() { + public void testOgm() { + Configuration conf = new Configuration.Builder().build(); + + SessionFactory factory = new SessionFactory(conf, "com.baeldung.spring.data.neo4j.domain"); + Session session = factory.openSession(); + Car tesla = new Car("tesla", "modelS"); Company baeldung = new Company("baeldung"); @@ -36,7 +29,7 @@ public class Neo4jOgmLiveTest { session.save(baeldung); - assertEquals(1, session.countEntitiesOfType(Company.class)); + Assert.assertEquals(1, session.countEntitiesOfType(Company.class)); Map params = new HashMap<>(); params.put("make", "tesla"); @@ -46,9 +39,9 @@ public class Neo4jOgmLiveTest { Map firstResult = result.iterator().next(); - assertEquals(1, firstResult.size()); + Assert.assertEquals(firstResult.size(), 1); Company actual = (Company) firstResult.get("company"); - assertEquals(actual.getName(), baeldung.getName()); + Assert.assertEquals(actual.getName(), baeldung.getName()); } } diff --git a/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/spring/data/neo4j/MovieRepositoryIntegrationTest.java b/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/spring/data/neo4j/MovieRepositoryIntegrationTest.java index f91fa2303c..3d9215f32f 100644 --- a/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/spring/data/neo4j/MovieRepositoryIntegrationTest.java +++ b/persistence-modules/spring-data-neo4j/src/test/java/com/baeldung/spring/data/neo4j/MovieRepositoryIntegrationTest.java @@ -75,7 +75,7 @@ public class MovieRepositoryIntegrationTest { public void testCount() { System.out.println("count"); long movieCount = movieRepository.count(); - + assertNotNull(movieCount); assertEquals(1, movieCount); } @@ -83,7 +83,7 @@ public class MovieRepositoryIntegrationTest { @DirtiesContext public void testFindAll() { System.out.println("findAll"); - Collection result = movieRepository.findAll(); + Collection result = (Collection) movieRepository.findAll(); assertNotNull(result); assertEquals(1, result.size()); } @@ -125,7 +125,7 @@ public class MovieRepositoryIntegrationTest { public void testDeleteAll() { System.out.println("deleteAll"); movieRepository.deleteAll(); - Collection result = movieRepository.findAll(); + Collection result = (Collection) movieRepository.findAll(); assertEquals(0, result.size()); } } From caf72a58b28e20479715812caad5f535c6926ea0 Mon Sep 17 00:00:00 2001 From: n Date: Thu, 15 Jun 2023 20:42:58 +0300 Subject: [PATCH 08/10] JAVA-20167 Add the spring-date-neo4j to the jdk8 profile --- persistence-modules/pom.xml | 5 +---- pom.xml | 2 ++ 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/persistence-modules/pom.xml b/persistence-modules/pom.xml index 9361e26d7d..a0246e0dfd 100644 --- a/persistence-modules/pom.xml +++ b/persistence-modules/pom.xml @@ -40,8 +40,6 @@ java-jpa java-jpa-2 java-jpa-3 - - java-mongodb-2 java-mongodb-3 java-mongodb-queries @@ -90,10 +88,9 @@ spring-data-jpa-repo-2 spring-data-jdbc spring-data-keyvalue - + spring-data-mongodb spring-data-mongodb-2 spring-data-mongodb-reactive - spring-data-redis diff --git a/pom.xml b/pom.xml index 7c726c1015..b6552706b5 100644 --- a/pom.xml +++ b/pom.xml @@ -369,6 +369,7 @@ persistence-modules/hibernate-ogm persistence-modules/java-cassandra persistence-modules/spring-data-cassandra-reactive + persistence-modules/spring-data-neo4j @@ -547,6 +548,7 @@ persistence-modules/hibernate-ogm persistence-modules/java-cassandra persistence-modules/spring-data-cassandra-reactive + persistence-modules/spring-data-neo4j From 64dd4ce8d3ae5b8bb71375a1001607efed62c3d0 Mon Sep 17 00:00:00 2001 From: timis1 Date: Thu, 15 Jun 2023 20:59:56 +0300 Subject: [PATCH 09/10] JAVA-20167 Adding additional example for Spring data derived query --- .../derivedquery/repository/UserRepository.java | 4 ++++ .../repository/UserRepositoryIntegrationTest.java | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/persistence-modules/spring-data-jpa-repo/src/main/java/com/baeldung/derivedquery/repository/UserRepository.java b/persistence-modules/spring-data-jpa-repo/src/main/java/com/baeldung/derivedquery/repository/UserRepository.java index 8f86e8ce49..4b2d6c11af 100644 --- a/persistence-modules/spring-data-jpa-repo/src/main/java/com/baeldung/derivedquery/repository/UserRepository.java +++ b/persistence-modules/spring-data-jpa-repo/src/main/java/com/baeldung/derivedquery/repository/UserRepository.java @@ -58,6 +58,10 @@ public interface UserRepository extends JpaRepository { List findByNameOrBirthDateAndActive(@Param(value = "nameParam") String nameParam, @Param(value = "birthDateParam") ZonedDateTime birthDateParam, @Param(value = "activeParam") Boolean activeParam); + List findByNameOrAge(String name, Integer age); + + List findByNameOrAgeAndActive(String name, Integer age, Boolean active); + List findByNameOrderByName(String name); List findByNameOrderByNameDesc(String name); diff --git a/persistence-modules/spring-data-jpa-repo/src/test/java/com/baeldung/derivedquery/repository/UserRepositoryIntegrationTest.java b/persistence-modules/spring-data-jpa-repo/src/test/java/com/baeldung/derivedquery/repository/UserRepositoryIntegrationTest.java index 75e0db253f..719f56e3cd 100644 --- a/persistence-modules/spring-data-jpa-repo/src/test/java/com/baeldung/derivedquery/repository/UserRepositoryIntegrationTest.java +++ b/persistence-modules/spring-data-jpa-repo/src/test/java/com/baeldung/derivedquery/repository/UserRepositoryIntegrationTest.java @@ -164,6 +164,18 @@ public class UserRepositoryIntegrationTest { assertEquals(2, userRepository.findByNameOrBirthDateAndActive(USER_NAME_ADAM, BIRTHDATE, false).size()); } + @Test + public void whenByNameOrAge() { + + assertEquals(2, userRepository.findByNameOrAge(USER_NAME_ADAM, 20).size()); + } + + @Test + public void whenByNameOrAgeAndActive() { + + assertEquals(3, userRepository.findByNameOrAgeAndActive(USER_NAME_ADAM, 20, false).size()); + } + @Test public void whenByNameOrderByName() { From c76a1e8c45ec56aba2c16b6240e418988b1ead08 Mon Sep 17 00:00:00 2001 From: timis1 Date: Sat, 17 Jun 2023 00:41:28 +0300 Subject: [PATCH 10/10] JAVA-20167 Remove methods that are not presented in articles --- .../derivedquery/repository/UserRepository.java | 7 ------- .../UserRepositoryIntegrationTest.java | 16 ++-------------- 2 files changed, 2 insertions(+), 21 deletions(-) diff --git a/persistence-modules/spring-data-jpa-repo/src/main/java/com/baeldung/derivedquery/repository/UserRepository.java b/persistence-modules/spring-data-jpa-repo/src/main/java/com/baeldung/derivedquery/repository/UserRepository.java index 4b2d6c11af..7d4a208d26 100644 --- a/persistence-modules/spring-data-jpa-repo/src/main/java/com/baeldung/derivedquery/repository/UserRepository.java +++ b/persistence-modules/spring-data-jpa-repo/src/main/java/com/baeldung/derivedquery/repository/UserRepository.java @@ -51,13 +51,6 @@ public interface UserRepository extends JpaRepository { List findByAgeIn(Collection ages); - @Query(value = "select * from users where (name = :nameParam OR birth_date <> :birthDateParam)", nativeQuery = true) - List findByNameOrBirthDate(@Param(value = "nameParam") String nameParam, @Param(value = "birthDateParam") ZonedDateTime birthDateParam); - - @Query(value = "select * from users where (name = :nameParam OR birth_date <> :birthDateParam) and active = :activeParam", nativeQuery = true) - List findByNameOrBirthDateAndActive(@Param(value = "nameParam") String nameParam, @Param(value = "birthDateParam") ZonedDateTime birthDateParam, - @Param(value = "activeParam") Boolean activeParam); - List findByNameOrAge(String name, Integer age); List findByNameOrAgeAndActive(String name, Integer age, Boolean active); diff --git a/persistence-modules/spring-data-jpa-repo/src/test/java/com/baeldung/derivedquery/repository/UserRepositoryIntegrationTest.java b/persistence-modules/spring-data-jpa-repo/src/test/java/com/baeldung/derivedquery/repository/UserRepositoryIntegrationTest.java index 719f56e3cd..cf479780a1 100644 --- a/persistence-modules/spring-data-jpa-repo/src/test/java/com/baeldung/derivedquery/repository/UserRepositoryIntegrationTest.java +++ b/persistence-modules/spring-data-jpa-repo/src/test/java/com/baeldung/derivedquery/repository/UserRepositoryIntegrationTest.java @@ -152,28 +152,16 @@ public class UserRepositoryIntegrationTest { assertEquals(3, userRepository.findByAgeIn(ages).size()); } - @Test - public void whenByNameOrBirthDate() { - - assertEquals(4, userRepository.findByNameOrBirthDate(USER_NAME_ADAM, BIRTHDATE).size()); - } - - @Test - public void whenByNameOrBirthDateAndActive() { - - assertEquals(2, userRepository.findByNameOrBirthDateAndActive(USER_NAME_ADAM, BIRTHDATE, false).size()); - } - @Test public void whenByNameOrAge() { - assertEquals(2, userRepository.findByNameOrAge(USER_NAME_ADAM, 20).size()); + assertEquals(3, userRepository.findByNameOrAge(USER_NAME_ADAM, 20).size()); } @Test public void whenByNameOrAgeAndActive() { - assertEquals(3, userRepository.findByNameOrAgeAndActive(USER_NAME_ADAM, 20, false).size()); + assertEquals(2, userRepository.findByNameOrAgeAndActive(USER_NAME_ADAM, 20, false).size()); } @Test