group persistence modules (#2890)
* move security content from spring-security-rest-full * swagger update * move query language to new module * rename spring-security-rest-full to spring-rest-full * group persistence modules
This commit is contained in:
committed by
Grzegorz Piwowarek
parent
dbeb5f8ba4
commit
26c50909be
+27
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.spring.data.neo4j.config;
|
||||
|
||||
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.data.neo4j.repository.config.EnableNeo4jRepositories;
|
||||
|
||||
@ComponentScan(basePackages = { "com.baeldung.spring.data.neo4j.services" })
|
||||
@Configuration
|
||||
@EnableNeo4jRepositories(basePackages = "com.baeldung.spring.data.neo4j.repostory")
|
||||
public class MovieDatabaseNeo4jConfiguration {
|
||||
|
||||
public static final String URL = System.getenv("NEO4J_URL") != null ? System.getenv("NEO4J_URL") : "http://neo4j:movies@localhost:7474";
|
||||
|
||||
@Bean
|
||||
public org.neo4j.ogm.config.Configuration getConfiguration() {
|
||||
org.neo4j.ogm.config.Configuration config = new org.neo4j.ogm.config.Configuration();
|
||||
config.driverConfiguration().setDriverClassName("org.neo4j.ogm.drivers.http.driver.HttpDriver").setURI(URL);
|
||||
return config;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SessionFactory getSessionFactory() {
|
||||
return new SessionFactory(getConfiguration(), "com.baeldung.spring.data.neo4j.domain");
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.spring.data.neo4j.config;
|
||||
|
||||
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.config.Neo4jConfiguration;
|
||||
import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
@Configuration
|
||||
@EnableTransactionManagement
|
||||
@ComponentScan(basePackages = { "com.baeldung.spring.data.neo4j.services" })
|
||||
@EnableNeo4jRepositories(basePackages = "com.baeldung.spring.data.neo4j.repostory")
|
||||
@Profile({ "embedded", "test" })
|
||||
public class MovieDatabaseNeo4jTestConfiguration extends Neo4jConfiguration {
|
||||
|
||||
@Bean
|
||||
public org.neo4j.ogm.config.Configuration getConfiguration() {
|
||||
final org.neo4j.ogm.config.Configuration config = new org.neo4j.ogm.config.Configuration();
|
||||
config.driverConfiguration()
|
||||
.setDriverClassName("org.neo4j.ogm.drivers.embedded.driver.EmbeddedDriver");
|
||||
return config;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SessionFactory getSessionFactory() {
|
||||
return new SessionFactory(getConfiguration(), "com.baeldung.spring.data.neo4j.domain");
|
||||
}
|
||||
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
package com.baeldung.spring.data.neo4j.domain;
|
||||
|
||||
import org.neo4j.ogm.annotation.GraphId;
|
||||
import org.neo4j.ogm.annotation.NodeEntity;
|
||||
import org.neo4j.ogm.annotation.Relationship;
|
||||
|
||||
@NodeEntity
|
||||
public class Car {
|
||||
@GraphId
|
||||
private Long id;
|
||||
|
||||
private String make;
|
||||
|
||||
@Relationship(direction = "INCOMING")
|
||||
private Company company;
|
||||
|
||||
public Car(String make, String model) {
|
||||
this.make = make;
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getMake() {
|
||||
return make;
|
||||
}
|
||||
|
||||
public void setMake(String make) {
|
||||
this.make = make;
|
||||
}
|
||||
|
||||
public String getModel() {
|
||||
return model;
|
||||
}
|
||||
|
||||
public void setModel(String model) {
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
private String model;
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package com.baeldung.spring.data.neo4j.domain;
|
||||
|
||||
import org.neo4j.ogm.annotation.NodeEntity;
|
||||
import org.neo4j.ogm.annotation.Relationship;
|
||||
|
||||
@NodeEntity
|
||||
public class Company {
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
|
||||
@Relationship(type="owns")
|
||||
private Car car;
|
||||
|
||||
public Company(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Car getCar() {
|
||||
return car;
|
||||
}
|
||||
|
||||
public void setCar(Car car) {
|
||||
this.car = car;
|
||||
}
|
||||
}
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
package com.baeldung.spring.data.neo4j.domain;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
|
||||
import com.voodoodyne.jackson.jsog.JSOGGenerator;
|
||||
import org.neo4j.ogm.annotation.GraphId;
|
||||
import org.neo4j.ogm.annotation.NodeEntity;
|
||||
import org.neo4j.ogm.annotation.Relationship;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
@JsonIdentityInfo(generator = JSOGGenerator.class)
|
||||
|
||||
@NodeEntity
|
||||
public class Movie {
|
||||
@GraphId
|
||||
Long id;
|
||||
|
||||
private String title;
|
||||
|
||||
private int released;
|
||||
private String tagline;
|
||||
|
||||
@Relationship(type = "ACTED_IN", direction = Relationship.INCOMING)
|
||||
private List<Role> roles;
|
||||
|
||||
public Movie() {
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public int getReleased() {
|
||||
return released;
|
||||
}
|
||||
|
||||
public String getTagline() {
|
||||
return tagline;
|
||||
}
|
||||
|
||||
public Collection<Role> getRoles() {
|
||||
return roles;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public void setReleased(int released) {
|
||||
this.released = released;
|
||||
}
|
||||
|
||||
public void setTagline(String tagline) {
|
||||
this.tagline = tagline;
|
||||
}
|
||||
|
||||
public void setRoles(List<Role> roles) {
|
||||
this.roles = roles;
|
||||
}
|
||||
|
||||
}
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
package com.baeldung.spring.data.neo4j.domain;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
|
||||
import com.voodoodyne.jackson.jsog.JSOGGenerator;
|
||||
import org.neo4j.ogm.annotation.GraphId;
|
||||
import org.neo4j.ogm.annotation.NodeEntity;
|
||||
import org.neo4j.ogm.annotation.Relationship;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@JsonIdentityInfo(generator = JSOGGenerator.class)
|
||||
@NodeEntity
|
||||
public class Person {
|
||||
@GraphId
|
||||
Long id;
|
||||
|
||||
private String name;
|
||||
private int born;
|
||||
|
||||
@Relationship(type = "ACTED_IN")
|
||||
private List<Movie> movies;
|
||||
|
||||
public Person() {
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public int getBorn() {
|
||||
return born;
|
||||
}
|
||||
|
||||
public List<Movie> getMovies() {
|
||||
return movies;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void setBorn(int born) {
|
||||
this.born = born;
|
||||
}
|
||||
|
||||
public void setMovies(List<Movie> movies) {
|
||||
this.movies = movies;
|
||||
}
|
||||
|
||||
}
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
package com.baeldung.spring.data.neo4j.domain;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
|
||||
import com.voodoodyne.jackson.jsog.JSOGGenerator;
|
||||
import org.neo4j.ogm.annotation.EndNode;
|
||||
import org.neo4j.ogm.annotation.GraphId;
|
||||
import org.neo4j.ogm.annotation.RelationshipEntity;
|
||||
import org.neo4j.ogm.annotation.StartNode;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
@JsonIdentityInfo(generator = JSOGGenerator.class)
|
||||
@RelationshipEntity(type = "ACTED_IN")
|
||||
public class Role {
|
||||
@GraphId
|
||||
Long id;
|
||||
private Collection<String> roles;
|
||||
@StartNode
|
||||
private Person person;
|
||||
@EndNode
|
||||
private Movie movie;
|
||||
|
||||
public Role() {
|
||||
}
|
||||
|
||||
public Collection<String> getRoles() {
|
||||
return roles;
|
||||
}
|
||||
|
||||
public Person getPerson() {
|
||||
return person;
|
||||
}
|
||||
|
||||
public Movie getMovie() {
|
||||
return movie;
|
||||
}
|
||||
|
||||
public void setRoles(Collection<String> roles) {
|
||||
this.roles = roles;
|
||||
}
|
||||
|
||||
public void setPerson(Person person) {
|
||||
this.person = person;
|
||||
}
|
||||
|
||||
public void setMovie(Movie movie) {
|
||||
this.movie = movie;
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.spring.data.neo4j.repostory;
|
||||
|
||||
import com.baeldung.spring.data.neo4j.domain.Movie;
|
||||
import org.springframework.data.neo4j.annotation.Query;
|
||||
import org.springframework.data.neo4j.repository.GraphRepository;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Repository
|
||||
public interface MovieRepository extends GraphRepository<Movie> {
|
||||
|
||||
Movie findByTitle(@Param("title") String title);
|
||||
|
||||
@Query("MATCH (m:Movie) WHERE m.title =~ ('(?i).*'+{title}+'.*') RETURN m")
|
||||
Collection<Movie> findByTitleContaining(@Param("title") String title);
|
||||
|
||||
@Query("MATCH (m:Movie)<-[:ACTED_IN]-(a:Person) RETURN m.title as movie, collect(a.name) as cast LIMIT {limit}")
|
||||
List<Map<String, Object>> graph(@Param("limit") int limit);
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.spring.data.neo4j.repostory;
|
||||
|
||||
import com.baeldung.spring.data.neo4j.domain.Person;
|
||||
import org.springframework.data.neo4j.repository.GraphRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface PersonRepository extends GraphRepository<Person> {
|
||||
}
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
package com.baeldung.spring.data.neo4j.services;
|
||||
|
||||
import com.baeldung.spring.data.neo4j.repostory.MovieRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
@Service
|
||||
@Transactional
|
||||
public class MovieService {
|
||||
|
||||
@Autowired
|
||||
private MovieRepository movieRepository;
|
||||
|
||||
private Map<String, Object> toD3Format(Iterator<Map<String, Object>> result) {
|
||||
List<Map<String, Object>> nodes = new ArrayList<>();
|
||||
List<Map<String, Object>> rels = new ArrayList<>();
|
||||
int i = 0;
|
||||
while (result.hasNext()) {
|
||||
Map<String, Object> row = result.next();
|
||||
nodes.add(map("title", row.get("movie"), "label", "movie"));
|
||||
int target = i;
|
||||
i++;
|
||||
for (Object name : (Collection) row.get("cast")) {
|
||||
Map<String, Object> actor = map("title", name, "label", "actor");
|
||||
int source = nodes.indexOf(actor);
|
||||
if (source == -1) {
|
||||
nodes.add(actor);
|
||||
source = i++;
|
||||
}
|
||||
rels.add(map("source", source, "target", target));
|
||||
}
|
||||
}
|
||||
return map("nodes", nodes, "links", rels);
|
||||
}
|
||||
|
||||
private Map<String, Object> map(String key1, Object value1, String key2, Object value2) {
|
||||
Map<String, Object> result = new HashMap<>(2);
|
||||
result.put(key1, value1);
|
||||
result.put(key2, value2);
|
||||
return result;
|
||||
}
|
||||
|
||||
public Map<String, Object> graph(int limit) {
|
||||
Iterator<Map<String, Object>> result = movieRepository.graph(limit).iterator();
|
||||
return toD3Format(result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>web - %date [%thread] %-5level %logger{36} - %message%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<logger name="org.springframework" level="WARN" />
|
||||
<logger name="org.springframework.transaction" level="WARN" />
|
||||
|
||||
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
|
||||
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 855 B |
+60
@@ -0,0 +1,60 @@
|
||||
package com.baeldung.neo4j;
|
||||
|
||||
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;
|
||||
|
||||
@Ignore
|
||||
public class Neo4JServerLiveTest {
|
||||
|
||||
@Test
|
||||
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");
|
||||
|
||||
StatementResult 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");
|
||||
|
||||
session.close();
|
||||
driver.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
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()) {
|
||||
stmt.execute("CREATE (baeldung:Company {name:\"Baeldung\"}) " +
|
||||
"-[:owns]-> (tesla:Car {make: 'tesla', model: 'modelX'})" +
|
||||
"RETURN baeldung, tesla");
|
||||
|
||||
ResultSet rs = stmt.executeQuery("MATCH (company:Company)-[:owns]-> (car:Car)" +
|
||||
"WHERE car.make='tesla' and car.model='modelX'" +
|
||||
"RETURN company.name");
|
||||
|
||||
while (rs.next()) {
|
||||
Assert.assertEquals(rs.getString("company.name"), "Baeldung");
|
||||
}
|
||||
}
|
||||
con.close();
|
||||
}
|
||||
}
|
||||
+167
@@ -0,0 +1,167 @@
|
||||
package com.baeldung.neo4j;
|
||||
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
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;
|
||||
|
||||
public class Neo4jLiveTest {
|
||||
|
||||
private static GraphDatabaseService graphDb;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
GraphDatabaseFactory graphDbFactory = new GraphDatabaseFactory();
|
||||
graphDb = graphDbFactory.newEmbeddedDatabase(new File("data/cars"));
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
graphDb.shutdown();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPersonCar() {
|
||||
graphDb.beginTx();
|
||||
Node car = graphDb.createNode(Label.label("Car"));
|
||||
car.setProperty("make", "tesla");
|
||||
car.setProperty("model", "model3");
|
||||
|
||||
Node owner = graphDb.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) " +
|
||||
"WHERE c.make = 'tesla'" +
|
||||
"RETURN p.firstName, p.lastName");
|
||||
|
||||
Map<String, Object> 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\"})" +
|
||||
"RETURN baeldung");
|
||||
|
||||
Map<String, Object> 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\"}) " +
|
||||
"-[:owns]-> (tesla:Car {make: 'tesla', model: 'modelX'})" +
|
||||
"RETURN baeldung, tesla");
|
||||
|
||||
Map<String, Object> 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\"}) " +
|
||||
"-[:owns]-> (tesla:Car {make: 'tesla', model: 'modelX'})" +
|
||||
"RETURN baeldung, tesla");
|
||||
|
||||
Result result = graphDb.execute("MATCH (company:Company)-[:owns]-> (car:Car)" +
|
||||
"WHERE car.make='tesla' and car.model='modelX'" +
|
||||
"RETURN company.name");
|
||||
|
||||
Map<String, Object> firstResult = result.next();
|
||||
Assert.assertEquals(firstResult.get("company.name"), "Baeldung");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdate() {
|
||||
graphDb.beginTx();
|
||||
|
||||
graphDb.execute("CREATE (baeldung:Company {name:\"Baeldung\"}) " +
|
||||
"-[:owns]-> (tesla:Car {make: 'tesla', model: 'modelX'})" +
|
||||
"RETURN baeldung, tesla");
|
||||
|
||||
Result result = graphDb.execute("MATCH (car:Car)" +
|
||||
"WHERE car.make='tesla'" +
|
||||
" SET car.milage=120" +
|
||||
" SET car :Car:Electro" +
|
||||
" SET car.model=NULL" +
|
||||
" RETURN car");
|
||||
|
||||
Map<String, Object> firstResult = result.next();
|
||||
Node car = (Node) firstResult.get("car");
|
||||
|
||||
Assert.assertEquals(car.getProperty("milage"), 120L);
|
||||
Assert.assertEquals(car.getLabels(), Arrays.asList(Label.label("Car"), Label.label("Electro")));
|
||||
|
||||
try {
|
||||
car.getProperty("model");
|
||||
Assert.fail();
|
||||
} catch (NotFoundException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDelete() {
|
||||
graphDb.beginTx();
|
||||
|
||||
graphDb.execute("CREATE (baeldung:Company {name:\"Baeldung\"}) " +
|
||||
"-[:owns]-> (tesla:Car {make: 'tesla', model: 'modelX'})" +
|
||||
"RETURN baeldung, tesla");
|
||||
|
||||
graphDb.execute("MATCH (company:Company)" +
|
||||
" WHERE company.name='Baeldung'" +
|
||||
" DELETE company");
|
||||
|
||||
Result result = graphDb.execute("MATCH (company:Company)" +
|
||||
" WHERE company.name='Baeldung'" +
|
||||
" RETURN company");
|
||||
|
||||
Assert.assertFalse(result.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBindings() {
|
||||
graphDb.beginTx();
|
||||
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
params.put("name", "baeldung");
|
||||
params.put("make", "tesla");
|
||||
params.put("model", "modelS");
|
||||
|
||||
Result result = graphDb.execute("CREATE (baeldung:Company {name:$name}) " +
|
||||
"-[:owns]-> (tesla:Car {make: $make, model: $model})" +
|
||||
"RETURN baeldung, tesla", params);
|
||||
|
||||
Map<String, Object> firstResult = result.next();
|
||||
Assert.assertTrue(firstResult.containsKey("baeldung"));
|
||||
Assert.assertTrue(firstResult.containsKey("tesla"));
|
||||
|
||||
Node car = (Node) firstResult.get("tesla");
|
||||
Assert.assertEquals(car.getProperty("model"), "modelS");
|
||||
}
|
||||
}
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
package com.baeldung.neo4j;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
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;
|
||||
|
||||
import com.baeldung.spring.data.neo4j.domain.Car;
|
||||
import com.baeldung.spring.data.neo4j.domain.Company;
|
||||
import org.neo4j.ogm.transaction.Transaction;
|
||||
|
||||
public class Neo4jOgmLiveTest {
|
||||
|
||||
@Test
|
||||
public void testOgm() {
|
||||
Configuration conf = new Configuration();
|
||||
conf.driverConfiguration().setDriverClassName("org.neo4j.ogm.drivers.embedded.driver.EmbeddedDriver");
|
||||
|
||||
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");
|
||||
|
||||
baeldung.setCar(tesla);
|
||||
|
||||
session.save(baeldung);
|
||||
|
||||
Assert.assertEquals(1, session.countEntitiesOfType(Company.class));
|
||||
|
||||
Map<String, String> params = new HashMap<>();
|
||||
params.put("make", "tesla");
|
||||
Result result = session.query("MATCH (car:Car) <-[:owns]- (company:Company)" +
|
||||
" WHERE car.make=$make" +
|
||||
" RETURN company", params);
|
||||
|
||||
Map<String, Object> firstResult = result.iterator().next();
|
||||
|
||||
Assert.assertEquals(firstResult.size(), 1);
|
||||
|
||||
Company actual = (Company) firstResult.get("company");
|
||||
Assert.assertEquals(actual.getName(), baeldung.getName());
|
||||
}
|
||||
}
|
||||
+130
@@ -0,0 +1,130 @@
|
||||
package com.baeldung.spring.data.neo4j;
|
||||
|
||||
import com.baeldung.spring.data.neo4j.config.MovieDatabaseNeo4jTestConfiguration;
|
||||
import com.baeldung.spring.data.neo4j.domain.Movie;
|
||||
import com.baeldung.spring.data.neo4j.domain.Person;
|
||||
import com.baeldung.spring.data.neo4j.domain.Role;
|
||||
import com.baeldung.spring.data.neo4j.repostory.MovieRepository;
|
||||
import com.baeldung.spring.data.neo4j.repostory.PersonRepository;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
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 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 {
|
||||
|
||||
@Autowired
|
||||
private MovieRepository movieRepository;
|
||||
|
||||
@Autowired
|
||||
private PersonRepository personRepository;
|
||||
|
||||
public MovieRepositoryIntegrationTest() {
|
||||
}
|
||||
|
||||
@Before
|
||||
public void initializeDatabase() {
|
||||
System.out.println("seeding embedded database");
|
||||
Movie italianJob = new Movie();
|
||||
italianJob.setTitle("The Italian Job");
|
||||
italianJob.setReleased(1999);
|
||||
movieRepository.save(italianJob);
|
||||
|
||||
Person mark = new Person();
|
||||
mark.setName("Mark Wahlberg");
|
||||
personRepository.save(mark);
|
||||
|
||||
Role charlie = new Role();
|
||||
charlie.setMovie(italianJob);
|
||||
charlie.setPerson(mark);
|
||||
Collection<String> roleNames = new HashSet();
|
||||
roleNames.add("Charlie Croker");
|
||||
charlie.setRoles(roleNames);
|
||||
List<Role> roles = new ArrayList();
|
||||
roles.add(charlie);
|
||||
italianJob.setRoles(roles);
|
||||
movieRepository.save(italianJob);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DirtiesContext
|
||||
public void testFindByTitle() {
|
||||
System.out.println("findByTitle");
|
||||
String title = "The Italian Job";
|
||||
Movie result = movieRepository.findByTitle(title);
|
||||
assertNotNull(result);
|
||||
assertEquals(1999, result.getReleased());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DirtiesContext
|
||||
public void testCount() {
|
||||
System.out.println("count");
|
||||
long movieCount = movieRepository.count();
|
||||
assertNotNull(movieCount);
|
||||
assertEquals(1, movieCount);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DirtiesContext
|
||||
public void testFindAll() {
|
||||
System.out.println("findAll");
|
||||
Collection<Movie> result = (Collection<Movie>) movieRepository.findAll();
|
||||
assertNotNull(result);
|
||||
assertEquals(1, result.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DirtiesContext
|
||||
public void testFindByTitleContaining() {
|
||||
System.out.println("findByTitleContaining");
|
||||
String title = "Italian";
|
||||
Collection<Movie> result = movieRepository.findByTitleContaining(title);
|
||||
assertNotNull(result);
|
||||
assertEquals(1, result.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DirtiesContext
|
||||
public void testGraph() {
|
||||
System.out.println("graph");
|
||||
List<Map<String, Object>> graph = movieRepository.graph(5);
|
||||
assertEquals(1, graph.size());
|
||||
Map<String, Object> map = graph.get(0);
|
||||
assertEquals(2, map.size());
|
||||
String[] cast = (String[]) map.get("cast");
|
||||
String movie = (String) map.get("movie");
|
||||
assertEquals("The Italian Job", movie);
|
||||
assertEquals("Mark Wahlberg", cast[0]);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DirtiesContext
|
||||
public void testDeleteMovie() {
|
||||
System.out.println("deleteMovie");
|
||||
movieRepository.delete(movieRepository.findByTitle("The Italian Job"));
|
||||
assertNull(movieRepository.findByTitle("The Italian Job"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DirtiesContext
|
||||
public void testDeleteAll() {
|
||||
System.out.println("deleteAll");
|
||||
movieRepository.deleteAll();
|
||||
Collection<Movie> result = (Collection<Movie>) movieRepository.findAll();
|
||||
assertEquals(0, result.size());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>web - %date [%thread] %-5level %logger{36} - %message%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<logger name="org.springframework" level="WARN" />
|
||||
<logger name="org.springframework.transaction" level="WARN" />
|
||||
|
||||
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
|
||||
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
Reference in New Issue
Block a user