JAVA-20167 Migrating spring-data-neo4j
This commit is contained in:
parent
3b49a56372
commit
2fd4c39a21
|
@ -40,8 +40,6 @@
|
||||||
<module>java-jpa</module> <!-- long running -->
|
<module>java-jpa</module> <!-- long running -->
|
||||||
<module>java-jpa-2</module>
|
<module>java-jpa-2</module>
|
||||||
<module>java-jpa-3</module>
|
<module>java-jpa-3</module>
|
||||||
<!-- enable it when persistence-modules is migrated to JDK9+ -->
|
|
||||||
<!-- <module>java-mongodb</module>--> <!-- long running -->
|
|
||||||
<module>java-mongodb-2</module> <!-- long running -->
|
<module>java-mongodb-2</module> <!-- long running -->
|
||||||
<module>java-mongodb-3</module> <!-- long running -->
|
<module>java-mongodb-3</module> <!-- long running -->
|
||||||
<module>java-mongodb-queries</module> <!-- long running -->
|
<module>java-mongodb-queries</module> <!-- long running -->
|
||||||
|
@ -90,10 +88,10 @@
|
||||||
<module>spring-data-jpa-repo-2</module>
|
<module>spring-data-jpa-repo-2</module>
|
||||||
<module>spring-data-jdbc</module>
|
<module>spring-data-jdbc</module>
|
||||||
<module>spring-data-keyvalue</module>
|
<module>spring-data-keyvalue</module>
|
||||||
<!-- <module>spring-data-mongodb</module> FAILED -->
|
<module>spring-data-mongodb</module>
|
||||||
<module>spring-data-mongodb-2</module>
|
<module>spring-data-mongodb-2</module>
|
||||||
<module>spring-data-mongodb-reactive</module>
|
<module>spring-data-mongodb-reactive</module>
|
||||||
<!-- <module>spring-data-neo4j</module> FAILED -->
|
<module>spring-data-neo4j</module>
|
||||||
<module>spring-data-redis</module>
|
<module>spring-data-redis</module>
|
||||||
<!-- Moved to JDK9+ profiles-->
|
<!-- Moved to JDK9+ profiles-->
|
||||||
<!-- <module>spring-data-rest</module>-->
|
<!-- <module>spring-data-rest</module>-->
|
||||||
|
|
|
@ -27,6 +27,11 @@
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.neo4j</groupId>
|
<groupId>org.neo4j</groupId>
|
||||||
<artifactId>neo4j-ogm-embedded-driver</artifactId>
|
<artifactId>neo4j-ogm-embedded-driver</artifactId>
|
||||||
|
<version>${neo4j-ogm-embedded-driver.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.neo4j</groupId>
|
||||||
|
<artifactId>neo4j-ogm-bolt-driver</artifactId>
|
||||||
<version>${neo4j-ogm.version}</version>
|
<version>${neo4j-ogm.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
|
@ -34,6 +39,12 @@
|
||||||
<artifactId>neo4j-java-driver</artifactId>
|
<artifactId>neo4j-java-driver</artifactId>
|
||||||
<version>${neo4j-java-driver.version}</version>
|
<version>${neo4j-java-driver.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.neo4j</groupId>
|
||||||
|
<artifactId>neo4j-jdbc-driver</artifactId>
|
||||||
|
<version>${neo4j-jdbc.version}</version>
|
||||||
|
<scope>runtime</scope>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.data</groupId>
|
<groupId>org.springframework.data</groupId>
|
||||||
<artifactId>spring-data-neo4j</artifactId>
|
<artifactId>spring-data-neo4j</artifactId>
|
||||||
|
@ -69,33 +80,43 @@
|
||||||
</exclusion>
|
</exclusion>
|
||||||
</exclusions>
|
</exclusions>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>org.neo4j</groupId>
|
|
||||||
<artifactId>neo4j-ogm-test</artifactId>
|
|
||||||
<version>${neo4j-ogm.version}</version>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.neo4j.test</groupId>
|
|
||||||
<artifactId>neo4j-harness</artifactId>
|
|
||||||
<version>${neo4j.version}</version>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework</groupId>
|
<groupId>org.springframework</groupId>
|
||||||
<artifactId>spring-test</artifactId>
|
<artifactId>spring-test</artifactId>
|
||||||
<version>${spring-test.version}</version>
|
<version>${spring-test.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.testcontainers</groupId>
|
||||||
|
<artifactId>neo4j</artifactId>
|
||||||
|
<version>${testcontainers.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
<exclusions>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>org.jetbrains</groupId>
|
||||||
|
<artifactId>annotations</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-compress</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>javax.xml.bind</groupId>
|
||||||
|
<artifactId>jaxb-api</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
</exclusions>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<neo4j-java-driver.version>1.6.2</neo4j-java-driver.version>
|
<neo4j-java-driver.version>5.6.0</neo4j-java-driver.version>
|
||||||
<neo4j.version>3.4.6</neo4j.version>
|
<neo4j.version>5.8.0</neo4j.version>
|
||||||
<spring-data-neo4j.version>5.0.1.RELEASE</spring-data-neo4j.version>
|
<spring-data-neo4j.version>7.0.3</spring-data-neo4j.version>
|
||||||
<jackson-jsog.version>1.1</jackson-jsog.version>
|
<jackson-jsog.version>1.1.2</jackson-jsog.version>
|
||||||
<spring-boot.version>2.0.1.RELEASE</spring-boot.version>
|
<spring-boot.version>3.1.0</spring-boot.version>
|
||||||
<spring-test.version>5.0.1.RELEASE</spring-test.version>
|
<spring-test.version>6.0.8</spring-test.version>
|
||||||
<neo4j-ogm.version>3.1.2</neo4j-ogm.version>
|
<neo4j-ogm.version>4.0.4</neo4j-ogm.version>
|
||||||
|
<neo4j-jdbc.version>4.0.9</neo4j-jdbc.version>
|
||||||
|
<neo4j-ogm-embedded-driver.version>3.2.39</neo4j-ogm-embedded-driver.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
|
@ -1,13 +1,16 @@
|
||||||
package com.baeldung.spring.data.neo4j.config;
|
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.neo4j.ogm.session.SessionFactory;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.context.annotation.Profile;
|
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.repository.config.EnableNeo4jRepositories;
|
||||||
import org.springframework.data.neo4j.transaction.Neo4jTransactionManager;
|
import org.springframework.transaction.PlatformTransactionManager;
|
||||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
|
@ -19,8 +22,7 @@ public class MovieDatabaseNeo4jTestConfiguration {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public org.neo4j.ogm.config.Configuration getConfiguration() {
|
public org.neo4j.ogm.config.Configuration getConfiguration() {
|
||||||
org.neo4j.ogm.config.Configuration config = new Builder().build();
|
return new org.neo4j.ogm.config.Configuration.Builder().build();
|
||||||
return config;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
@ -29,8 +31,14 @@ public class MovieDatabaseNeo4jTestConfiguration {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public Neo4jTransactionManager transactionManager() {
|
public PlatformTransactionManager transactionManager(Driver driver,
|
||||||
return new Neo4jTransactionManager(getSessionFactory());
|
DatabaseSelectionProvider databaseSelectionProvider, UserSelectionProvider userSelectionProvider) {
|
||||||
|
|
||||||
|
return Neo4jTransactionManager
|
||||||
|
.with(driver)
|
||||||
|
.withDatabaseSelectionProvider(databaseSelectionProvider)
|
||||||
|
.withUserSelectionProvider(userSelectionProvider)
|
||||||
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package com.baeldung.spring.data.neo4j.domain;
|
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.GeneratedValue;
|
||||||
import org.neo4j.ogm.annotation.Id;
|
import org.neo4j.ogm.annotation.Id;
|
||||||
import org.neo4j.ogm.annotation.NodeEntity;
|
import org.neo4j.ogm.annotation.NodeEntity;
|
||||||
|
@ -12,7 +14,7 @@ public class Car {
|
||||||
|
|
||||||
private String make;
|
private String make;
|
||||||
|
|
||||||
@Relationship(direction = "INCOMING")
|
@Relationship(direction = INCOMING)
|
||||||
private Company company;
|
private Company company;
|
||||||
|
|
||||||
public Car(String make, String model) {
|
public Car(String make, String model) {
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package com.baeldung.spring.data.neo4j.domain;
|
package com.baeldung.spring.data.neo4j.domain;
|
||||||
|
|
||||||
|
import static org.neo4j.ogm.annotation.Relationship.Direction.INCOMING;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
|
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
|
||||||
import com.voodoodyne.jackson.jsog.JSOGGenerator;
|
import com.voodoodyne.jackson.jsog.JSOGGenerator;
|
||||||
|
|
||||||
|
@ -12,7 +14,6 @@ import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@JsonIdentityInfo(generator = JSOGGenerator.class)
|
@JsonIdentityInfo(generator = JSOGGenerator.class)
|
||||||
|
|
||||||
@NodeEntity
|
@NodeEntity
|
||||||
public class Movie {
|
public class Movie {
|
||||||
@Id @GeneratedValue
|
@Id @GeneratedValue
|
||||||
|
@ -23,7 +24,7 @@ public class Movie {
|
||||||
private int released;
|
private int released;
|
||||||
private String tagline;
|
private String tagline;
|
||||||
|
|
||||||
@Relationship(type = "ACTED_IN", direction = Relationship.INCOMING)
|
@Relationship(type = "ACTED_IN", direction = INCOMING)
|
||||||
private List<Role> roles;
|
private List<Role> roles;
|
||||||
|
|
||||||
public Movie() {
|
public Movie() {
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
package com.baeldung.spring.data.neo4j.repository;
|
package com.baeldung.spring.data.neo4j.repository;
|
||||||
|
|
||||||
import com.baeldung.spring.data.neo4j.domain.Movie;
|
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.Neo4jRepository;
|
||||||
|
import org.springframework.data.neo4j.repository.query.Query;
|
||||||
import org.springframework.data.repository.query.Param;
|
import org.springframework.data.repository.query.Param;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
@ -13,7 +13,7 @@ import java.util.Map;
|
||||||
@Repository
|
@Repository
|
||||||
public interface MovieRepository extends Neo4jRepository<Movie, Long> {
|
public interface MovieRepository extends Neo4jRepository<Movie, Long> {
|
||||||
|
|
||||||
Movie findByTitle(@Param("title") String title);
|
Movie findByTitle(String title);
|
||||||
|
|
||||||
@Query("MATCH (m:Movie) WHERE m.title =~ ('(?i).*'+{title}+'.*') RETURN m")
|
@Query("MATCH (m:Movie) WHERE m.title =~ ('(?i).*'+{title}+'.*') RETURN m")
|
||||||
Collection<Movie> findByTitleContaining(@Param("title") String title);
|
Collection<Movie> findByTitleContaining(@Param("title") String title);
|
||||||
|
|
|
@ -1,19 +1,16 @@
|
||||||
package com.baeldung;
|
package com.baeldung;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.springframework.test.context.ActiveProfiles;
|
import org.springframework.test.context.ActiveProfiles;
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
|
||||||
|
|
||||||
import com.baeldung.spring.data.neo4j.config.MovieDatabaseNeo4jTestConfiguration;
|
import com.baeldung.spring.data.neo4j.config.MovieDatabaseNeo4jTestConfiguration;
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
|
||||||
@ContextConfiguration(classes = MovieDatabaseNeo4jTestConfiguration.class)
|
@ContextConfiguration(classes = MovieDatabaseNeo4jTestConfiguration.class)
|
||||||
@ActiveProfiles(profiles = "test")
|
@ActiveProfiles(profiles = "test")
|
||||||
public class SpringContextTest {
|
class SpringContextTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,45 +1,59 @@
|
||||||
package com.baeldung.neo4j;
|
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.Connection;
|
||||||
import java.sql.DriverManager;
|
import java.sql.DriverManager;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.Statement;
|
import java.sql.Statement;
|
||||||
|
|
||||||
import org.junit.Assert;
|
import org.junit.jupiter.api.AfterAll;
|
||||||
import org.junit.Ignore;
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
import org.junit.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.neo4j.driver.v1.AuthTokens;
|
import org.neo4j.driver.Driver;
|
||||||
import org.neo4j.driver.v1.Driver;
|
import org.neo4j.driver.Result;
|
||||||
import org.neo4j.driver.v1.GraphDatabase;
|
import org.neo4j.driver.Session;
|
||||||
import org.neo4j.driver.v1.Session;
|
|
||||||
import org.neo4j.driver.v1.StatementResult;
|
|
||||||
|
|
||||||
@Ignore
|
class Neo4JServerLiveTest {
|
||||||
public class Neo4JServerLiveTest {
|
|
||||||
|
|
||||||
@Test
|
private static Session session;
|
||||||
public void standAloneDriver() {
|
private static Driver driver;
|
||||||
Driver driver = GraphDatabase.driver("bolt://localhost:7687", AuthTokens.basic("neo4j", "12345"));
|
|
||||||
Session session = driver.session();
|
|
||||||
|
|
||||||
session.run("CREATE (baeldung:Company {name:\"Baeldung\"}) " +
|
@BeforeAll
|
||||||
"-[:owns]-> (tesla:Car {make: 'tesla', model: 'modelX'})" +
|
public static void setup() {
|
||||||
"RETURN baeldung, tesla");
|
driver = getNewBoltConnection();
|
||||||
|
}
|
||||||
|
|
||||||
StatementResult result = session.run("MATCH (company:Company)-[:owns]-> (car:Car)" +
|
@AfterAll
|
||||||
"WHERE car.make='tesla' and car.model='modelX'" +
|
public static void tearDown() {
|
||||||
"RETURN company.name");
|
|
||||||
|
|
||||||
Assert.assertTrue(result.hasNext());
|
|
||||||
Assert.assertEquals(result.next().get("company.name").asString(), "Baeldung");
|
|
||||||
|
|
||||||
session.close();
|
|
||||||
driver.close();
|
driver.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void standAloneJdbc() throws Exception {
|
void standAloneDriver() {
|
||||||
Connection con = DriverManager.getConnection("jdbc:neo4j:bolt://localhost/?user=neo4j,password=12345,scheme=basic");
|
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)" +
|
||||||
|
"WHERE car.make='tesla' and car.model='modelX'" +
|
||||||
|
"RETURN company.name");
|
||||||
|
|
||||||
|
assertTrue(result.hasNext());
|
||||||
|
assertEquals("Baeldung", result.next().get("company.name").asString());
|
||||||
|
|
||||||
|
session.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void standAloneJdbc() throws Exception {
|
||||||
|
String uri = "jdbc:neo4j:" + neo4jServer.getBoltUrl() + "/?user=neo4j,password=" + DEFAULT_PASSWORD + ",scheme=basic";
|
||||||
|
Connection con = DriverManager.getConnection(uri);
|
||||||
|
|
||||||
// Querying
|
// Querying
|
||||||
try (Statement stmt = con.createStatement()) {
|
try (Statement stmt = con.createStatement()) {
|
||||||
|
@ -52,7 +66,7 @@ public class Neo4JServerLiveTest {
|
||||||
"RETURN company.name");
|
"RETURN company.name");
|
||||||
|
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
Assert.assertEquals(rs.getString("company.name"), "Baeldung");
|
assertEquals("Baeldung", rs.getString("company.name"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
con.close();
|
con.close();
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
package com.baeldung.neo4j;
|
package com.baeldung.neo4j;
|
||||||
|
|
||||||
|
|
||||||
|
import static org.neo4j.configuration.GraphDatabaseSettings.DEFAULT_DATABASE_NAME;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.time.Duration;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
@ -10,101 +13,107 @@ import org.junit.After;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
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.GraphDatabaseService;
|
||||||
import org.neo4j.graphdb.Label;
|
import org.neo4j.graphdb.Label;
|
||||||
import org.neo4j.graphdb.Node;
|
import org.neo4j.graphdb.Node;
|
||||||
import org.neo4j.graphdb.NotFoundException;
|
import org.neo4j.graphdb.NotFoundException;
|
||||||
import org.neo4j.graphdb.RelationshipType;
|
import org.neo4j.graphdb.RelationshipType;
|
||||||
import org.neo4j.graphdb.Result;
|
import org.neo4j.graphdb.Result;
|
||||||
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
|
import org.neo4j.graphdb.Transaction;
|
||||||
|
|
||||||
public class Neo4jLiveTest {
|
public class Neo4jLiveTest {
|
||||||
|
|
||||||
private static GraphDatabaseService graphDb;
|
private static GraphDatabaseService graphDb;
|
||||||
|
private static Transaction transaction;
|
||||||
|
private static DatabaseManagementService managementService;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
GraphDatabaseFactory graphDbFactory = new GraphDatabaseFactory();
|
managementService = new DatabaseManagementServiceBuilder(new File("data/cars").toPath())
|
||||||
graphDb = graphDbFactory.newEmbeddedDatabase(new File("data/cars"));
|
.setConfig( GraphDatabaseSettings.transaction_timeout, Duration.ofSeconds( 60 ) )
|
||||||
|
.setConfig( GraphDatabaseSettings.preallocate_logical_logs, false ).build();
|
||||||
|
graphDb = managementService.database( DEFAULT_DATABASE_NAME );
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
public void tearDown() {
|
public void tearDown() {
|
||||||
graphDb.shutdown();
|
managementService.shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPersonCar() {
|
public void testPersonCar() {
|
||||||
graphDb.beginTx();
|
transaction = graphDb.beginTx();
|
||||||
Node car = graphDb.createNode(Label.label("Car"));
|
Node car = transaction.createNode(Label.label("Car"));
|
||||||
car.setProperty("make", "tesla");
|
car.setProperty("make", "tesla");
|
||||||
car.setProperty("model", "model3");
|
car.setProperty("model", "model3");
|
||||||
|
|
||||||
Node owner = graphDb.createNode(Label.label("Person"));
|
Node owner = transaction.createNode(Label.label("Person"));
|
||||||
owner.setProperty("firstName", "baeldung");
|
owner.setProperty("firstName", "baeldung");
|
||||||
owner.setProperty("lastName", "baeldung");
|
owner.setProperty("lastName", "baeldung");
|
||||||
|
|
||||||
owner.createRelationshipTo(car, RelationshipType.withName("owner"));
|
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'" +
|
"WHERE c.make = 'tesla'" +
|
||||||
"RETURN p.firstName, p.lastName");
|
"RETURN p.firstName, p.lastName");
|
||||||
|
|
||||||
Map<String, Object> firstResult = result.next();
|
Map<String, Object> firstResult = result.next();
|
||||||
|
|
||||||
Assert.assertEquals("baeldung", firstResult.get("p.firstName"));
|
Assert.assertEquals("baeldung", firstResult.get("p.firstName"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCreateNode() {
|
public void testCreateNode() {
|
||||||
|
transaction = graphDb.beginTx();
|
||||||
graphDb.beginTx();
|
Result result = transaction.execute("CREATE (baeldung:Company {name:\"Baeldung\"})" +
|
||||||
|
|
||||||
Result result = graphDb.execute("CREATE (baeldung:Company {name:\"Baeldung\"})" +
|
|
||||||
"RETURN baeldung");
|
"RETURN baeldung");
|
||||||
|
|
||||||
Map<String, Object> firstResult = result.next();
|
Map<String, Object> firstResult = result.next();
|
||||||
Node firstNode = (Node) firstResult.get("baeldung");
|
Node firstNode = (Node) firstResult.get("baeldung");
|
||||||
|
|
||||||
Assert.assertEquals(firstNode.getProperty("name"), "Baeldung");
|
Assert.assertEquals(firstNode.getProperty("name"), "Baeldung");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCreateNodeAndLink() {
|
public void testCreateNodeAndLink() {
|
||||||
graphDb.beginTx();
|
transaction = graphDb.beginTx();
|
||||||
|
Result result = transaction.execute("CREATE (baeldung:Company {name:\"Baeldung\"}) " +
|
||||||
Result result = graphDb.execute("CREATE (baeldung:Company {name:\"Baeldung\"}) " +
|
|
||||||
"-[:owns]-> (tesla:Car {make: 'tesla', model: 'modelX'})" +
|
"-[:owns]-> (tesla:Car {make: 'tesla', model: 'modelX'})" +
|
||||||
"RETURN baeldung, tesla");
|
"RETURN baeldung, tesla");
|
||||||
|
|
||||||
Map<String, Object> firstResult = result.next();
|
Map<String, Object> firstResult = result.next();
|
||||||
|
|
||||||
Assert.assertTrue(firstResult.containsKey("baeldung"));
|
Assert.assertTrue(firstResult.containsKey("baeldung"));
|
||||||
Assert.assertTrue(firstResult.containsKey("tesla"));
|
Assert.assertTrue(firstResult.containsKey("tesla"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testFindAndReturn() {
|
public void testFindAndReturn() {
|
||||||
graphDb.beginTx();
|
transaction = graphDb.beginTx();
|
||||||
|
transaction.execute("CREATE (baeldung:Company {name:\"Baeldung\"}) " +
|
||||||
graphDb.execute("CREATE (baeldung:Company {name:\"Baeldung\"}) " +
|
|
||||||
"-[:owns]-> (tesla:Car {make: 'tesla', model: 'modelX'})" +
|
"-[:owns]-> (tesla:Car {make: 'tesla', model: 'modelX'})" +
|
||||||
"RETURN baeldung, tesla");
|
"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'" +
|
"WHERE car.make='tesla' and car.model='modelX'" +
|
||||||
"RETURN company.name");
|
"RETURN company.name");
|
||||||
|
|
||||||
Map<String, Object> firstResult = result.next();
|
Map<String, Object> firstResult = result.next();
|
||||||
|
|
||||||
Assert.assertEquals(firstResult.get("company.name"), "Baeldung");
|
Assert.assertEquals(firstResult.get("company.name"), "Baeldung");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testUpdate() {
|
public void testUpdate() {
|
||||||
graphDb.beginTx();
|
transaction = graphDb.beginTx();
|
||||||
|
transaction.execute("CREATE (baeldung:Company {name:\"Baeldung\"}) " +
|
||||||
graphDb.execute("CREATE (baeldung:Company {name:\"Baeldung\"}) " +
|
|
||||||
"-[:owns]-> (tesla:Car {make: 'tesla', model: 'modelX'})" +
|
"-[:owns]-> (tesla:Car {make: 'tesla', model: 'modelX'})" +
|
||||||
"RETURN baeldung, tesla");
|
"RETURN baeldung, tesla");
|
||||||
|
|
||||||
Result result = graphDb.execute("MATCH (car:Car)" +
|
Result result = transaction.execute("MATCH (car:Car)" +
|
||||||
"WHERE car.make='tesla'" +
|
"WHERE car.make='tesla'" +
|
||||||
" SET car.milage=120" +
|
" SET car.milage=120" +
|
||||||
" SET car :Car:Electro" +
|
" SET car :Car:Electro" +
|
||||||
|
@ -127,17 +136,16 @@ public class Neo4jLiveTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDelete() {
|
public void testDelete() {
|
||||||
graphDb.beginTx();
|
transaction = graphDb.beginTx();
|
||||||
|
transaction.execute("CREATE (baeldung:Company {name:\"Baeldung\"}) " +
|
||||||
graphDb.execute("CREATE (baeldung:Company {name:\"Baeldung\"}) " +
|
|
||||||
"-[:owns]-> (tesla:Car {make: 'tesla', model: 'modelX'})" +
|
"-[:owns]-> (tesla:Car {make: 'tesla', model: 'modelX'})" +
|
||||||
"RETURN baeldung, tesla");
|
"RETURN baeldung, tesla");
|
||||||
|
|
||||||
graphDb.execute("MATCH (company:Company)" +
|
transaction.execute("MATCH (company:Company)" +
|
||||||
" WHERE company.name='Baeldung'" +
|
" WHERE company.name='Baeldung'" +
|
||||||
" DELETE company");
|
" DELETE company");
|
||||||
|
|
||||||
Result result = graphDb.execute("MATCH (company:Company)" +
|
Result result = transaction.execute("MATCH (company:Company)" +
|
||||||
" WHERE company.name='Baeldung'" +
|
" WHERE company.name='Baeldung'" +
|
||||||
" RETURN company");
|
" RETURN company");
|
||||||
|
|
||||||
|
@ -146,14 +154,13 @@ public class Neo4jLiveTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBindings() {
|
public void testBindings() {
|
||||||
graphDb.beginTx();
|
transaction = graphDb.beginTx();
|
||||||
|
|
||||||
Map<String, Object> params = new HashMap<>();
|
Map<String, Object> params = new HashMap<>();
|
||||||
params.put("name", "baeldung");
|
params.put("name", "baeldung");
|
||||||
params.put("make", "tesla");
|
params.put("make", "tesla");
|
||||||
params.put("model", "modelS");
|
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})" +
|
"-[:owns]-> (tesla:Car {make: $make, model: $model})" +
|
||||||
"RETURN baeldung, tesla", params);
|
"RETURN baeldung, tesla", params);
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,13 @@
|
||||||
package com.baeldung.neo4j;
|
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.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.junit.Assert;
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
import org.junit.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.neo4j.ogm.config.Configuration;
|
|
||||||
import org.neo4j.ogm.model.Result;
|
import org.neo4j.ogm.model.Result;
|
||||||
import org.neo4j.ogm.session.Session;
|
import org.neo4j.ogm.session.Session;
|
||||||
import org.neo4j.ogm.session.SessionFactory;
|
import org.neo4j.ogm.session.SessionFactory;
|
||||||
|
@ -15,13 +17,18 @@ import com.baeldung.spring.data.neo4j.domain.Company;
|
||||||
|
|
||||||
public class Neo4jOgmLiveTest {
|
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
|
@Test
|
||||||
public void testOgm() {
|
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");
|
Car tesla = new Car("tesla", "modelS");
|
||||||
Company baeldung = new Company("baeldung");
|
Company baeldung = new Company("baeldung");
|
||||||
|
|
||||||
|
@ -29,7 +36,7 @@ public class Neo4jOgmLiveTest {
|
||||||
|
|
||||||
session.save(baeldung);
|
session.save(baeldung);
|
||||||
|
|
||||||
Assert.assertEquals(1, session.countEntitiesOfType(Company.class));
|
assertEquals(1, session.countEntitiesOfType(Company.class));
|
||||||
|
|
||||||
Map<String, String> params = new HashMap<>();
|
Map<String, String> params = new HashMap<>();
|
||||||
params.put("make", "tesla");
|
params.put("make", "tesla");
|
||||||
|
@ -39,9 +46,9 @@ public class Neo4jOgmLiveTest {
|
||||||
|
|
||||||
Map<String, Object> firstResult = result.iterator().next();
|
Map<String, Object> firstResult = result.iterator().next();
|
||||||
|
|
||||||
Assert.assertEquals(firstResult.size(), 1);
|
assertEquals(1, firstResult.size());
|
||||||
|
|
||||||
Company actual = (Company) firstResult.get("company");
|
Company actual = (Company) firstResult.get("company");
|
||||||
Assert.assertEquals(actual.getName(), baeldung.getName());
|
assertEquals(actual.getName(), baeldung.getName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -75,7 +75,7 @@ public class MovieRepositoryIntegrationTest {
|
||||||
public void testCount() {
|
public void testCount() {
|
||||||
System.out.println("count");
|
System.out.println("count");
|
||||||
long movieCount = movieRepository.count();
|
long movieCount = movieRepository.count();
|
||||||
assertNotNull(movieCount);
|
|
||||||
assertEquals(1, movieCount);
|
assertEquals(1, movieCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -83,7 +83,7 @@ public class MovieRepositoryIntegrationTest {
|
||||||
@DirtiesContext
|
@DirtiesContext
|
||||||
public void testFindAll() {
|
public void testFindAll() {
|
||||||
System.out.println("findAll");
|
System.out.println("findAll");
|
||||||
Collection<Movie> result = (Collection<Movie>) movieRepository.findAll();
|
Collection<Movie> result = movieRepository.findAll();
|
||||||
assertNotNull(result);
|
assertNotNull(result);
|
||||||
assertEquals(1, result.size());
|
assertEquals(1, result.size());
|
||||||
}
|
}
|
||||||
|
@ -125,7 +125,7 @@ public class MovieRepositoryIntegrationTest {
|
||||||
public void testDeleteAll() {
|
public void testDeleteAll() {
|
||||||
System.out.println("deleteAll");
|
System.out.println("deleteAll");
|
||||||
movieRepository.deleteAll();
|
movieRepository.deleteAll();
|
||||||
Collection<Movie> result = (Collection<Movie>) movieRepository.findAll();
|
Collection<Movie> result = movieRepository.findAll();
|
||||||
assertEquals(0, result.size());
|
assertEquals(0, result.size());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue