From 0fa8c12806653667a551368fc158a31813f7454f Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Thu, 3 Sep 2015 17:06:05 +0200 Subject: [PATCH] #131 - Polished Spring Data Neo4j example. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Switched to milestone version of Spring Data Neo4j. Used Lombok in domain types. Used assertThat(…) matchers in test case for consistency. Inlined Spring configuration into test case. Upgraded to Lombok 1.16.6 along the way. Fixed indentation in pom.xml files to use tabs instead of spaces. Original pull requests: #129, #130. --- neo4j/example/pom.xml | 108 ++++++++---------- .../springdata/neo4j/{domain => }/Actor.java | 44 +++---- .../neo4j/{repo => }/ActorRepository.java | 10 +- .../springdata/neo4j/{domain => }/Movie.java | 40 +++---- .../springdata/neo4j/{domain => }/Role.java | 46 +++----- .../neo4j/ActorRepositoryIntegrationTest.java | 61 +++++++--- .../springdata/neo4j/ExampleConfig.java | 44 ------- neo4j/pom.xml | 30 ++--- pom.xml | 2 +- 9 files changed, 152 insertions(+), 233 deletions(-) rename neo4j/example/src/main/java/example/springdata/neo4j/{domain => }/Actor.java (68%) rename neo4j/example/src/main/java/example/springdata/neo4j/{repo => }/ActorRepository.java (86%) rename neo4j/example/src/main/java/example/springdata/neo4j/{domain => }/Movie.java (65%) rename neo4j/example/src/main/java/example/springdata/neo4j/{domain => }/Role.java (65%) delete mode 100644 neo4j/example/src/test/java/example/springdata/neo4j/ExampleConfig.java diff --git a/neo4j/example/pom.xml b/neo4j/example/pom.xml index a4a14e9d..f0739169 100644 --- a/neo4j/example/pom.xml +++ b/neo4j/example/pom.xml @@ -2,70 +2,58 @@ - 4.0.0 + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 - spring-data-neo4j-example - Spring Data Neo4j - Example - - 4.0.0.BUILD-SNAPSHOT - 1.1.2-SNAPSHOT - 2.2.4 - + spring-data-neo4j-example + Spring Data Neo4j - Example + + 4.0.0.RC2 + 1.1.1 + 2.2.4 + - - org.springframework.data.examples - spring-data-neo4j-examples - 1.0.0.BUILD-SNAPSHOT - ../pom.xml - + + org.springframework.data.examples + spring-data-neo4j-examples + 1.0.0.BUILD-SNAPSHOT + ../pom.xml + - + - - org.springframework.data - spring-data-neo4j - ${spring-data-neo4j.version} - + + org.springframework.data + spring-data-neo4j + ${spring-data-neo4j.version} + - - org.springframework.data - spring-data-neo4j - ${spring-data-neo4j.version} - test-jar - - - org.neo4j - neo4j-ogm - ${neo4j-ogm.version} - test-jar - test - - - org.neo4j.app - neo4j-server - ${neo4j.version} - - - junit - junit - 4.12 - test - - - org.neo4j.test - neo4j-harness - ${neo4j.version} - test - - + + org.springframework.data + spring-data-neo4j + ${spring-data-neo4j.version} + test-jar + + + org.neo4j + neo4j-ogm + ${neo4j-ogm.version} + test-jar + test + + + org.neo4j.app + neo4j-server + ${neo4j.version} + + + org.neo4j.test + neo4j-harness + ${neo4j.version} + test + + + - - - spring-libs-snapshot - Spring - http://repo.spring.io/libs-snapshot - - \ No newline at end of file diff --git a/neo4j/example/src/main/java/example/springdata/neo4j/domain/Actor.java b/neo4j/example/src/main/java/example/springdata/neo4j/Actor.java similarity index 68% rename from neo4j/example/src/main/java/example/springdata/neo4j/domain/Actor.java rename to neo4j/example/src/main/java/example/springdata/neo4j/Actor.java index c45ef0ea..2d5422e8 100644 --- a/neo4j/example/src/main/java/example/springdata/neo4j/domain/Actor.java +++ b/neo4j/example/src/main/java/example/springdata/neo4j/Actor.java @@ -14,7 +14,12 @@ * limitations under the License. */ -package example.springdata.neo4j.domain; +package example.springdata.neo4j; + +import lombok.AccessLevel; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.RequiredArgsConstructor; import java.util.HashSet; import java.util.Set; @@ -27,41 +32,22 @@ import org.neo4j.ogm.annotation.Relationship; * An Actor node entity. * * @author Luanne Misquitta + * @author Oliver Gierke */ @NodeEntity(label = "Actor") +@NoArgsConstructor(force = true, access = AccessLevel.PRIVATE) +@RequiredArgsConstructor +@Getter public class Actor { - @GraphId private Long id; - private String name; - - @Relationship(type = "ACTED_IN") - private Set roles = new HashSet<>(); - - public Actor() { - } - - public Actor(String name) { - this.name = name; - } - - public String getName() { - return name; - } - - public Long getId() { - return id; - } - - public Set getRoles() { - return roles; - } + private @GraphId Long id; + private final String name; + private final @Relationship(type = "ACTED_IN") Set roles = new HashSet<>(); public void actedIn(Movie movie, String roleName) { - Role role = new Role(); - role.setRole(roleName); - role.setActor(this); - role.setMovie(movie); + Role role = new Role(this, roleName, movie); + roles.add(role); movie.getRoles().add(role); } diff --git a/neo4j/example/src/main/java/example/springdata/neo4j/repo/ActorRepository.java b/neo4j/example/src/main/java/example/springdata/neo4j/ActorRepository.java similarity index 86% rename from neo4j/example/src/main/java/example/springdata/neo4j/repo/ActorRepository.java rename to neo4j/example/src/main/java/example/springdata/neo4j/ActorRepository.java index f8acdf4a..c1f18ea0 100644 --- a/neo4j/example/src/main/java/example/springdata/neo4j/repo/ActorRepository.java +++ b/neo4j/example/src/main/java/example/springdata/neo4j/ActorRepository.java @@ -13,15 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package example.springdata.neo4j.repo; +package example.springdata.neo4j; -import example.springdata.neo4j.domain.Actor; import org.springframework.data.neo4j.repository.GraphRepository; /** - * GraphRepository for Actors. + * {@link GraphRepository} for {@link Actor}s. + * * @author Luanne Misquitta */ -public interface ActorRepository extends GraphRepository { - -} +public interface ActorRepository extends GraphRepository {} diff --git a/neo4j/example/src/main/java/example/springdata/neo4j/domain/Movie.java b/neo4j/example/src/main/java/example/springdata/neo4j/Movie.java similarity index 65% rename from neo4j/example/src/main/java/example/springdata/neo4j/domain/Movie.java rename to neo4j/example/src/main/java/example/springdata/neo4j/Movie.java index 33e581e3..eacc9648 100644 --- a/neo4j/example/src/main/java/example/springdata/neo4j/domain/Movie.java +++ b/neo4j/example/src/main/java/example/springdata/neo4j/Movie.java @@ -14,7 +14,12 @@ * limitations under the License. */ -package example.springdata.neo4j.domain; +package example.springdata.neo4j; + +import lombok.AccessLevel; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.RequiredArgsConstructor; import java.util.HashSet; import java.util.Set; @@ -27,34 +32,15 @@ import org.neo4j.ogm.annotation.Relationship; * A Movie node entity. * * @author Luanne Misquitta + * @author Oliver Gierke */ @NodeEntity(label = "Movie") +@NoArgsConstructor(force = true, access = AccessLevel.PRIVATE) +@RequiredArgsConstructor +@Getter public class Movie { - @GraphId private Long id; - private String title; - - @Relationship(type = "ACTED_IN", direction = "INCOMING") - private Set roles = new HashSet<>(); - - public Movie() { - } - - public Movie(String title) { - this.title = title; - } - - public String getTitle() { - return title; - } - - public void setTitle(String title) { - this.title = title; - } - - @Relationship(type = "ACTED_IN", direction = "INCOMING") - public Set getRoles() { - return roles; - } - + private @GraphId Long id; + private final String title; + private final @Relationship(type = "ACTED_IN", direction = "INCOMING") Set roles = new HashSet<>(); } diff --git a/neo4j/example/src/main/java/example/springdata/neo4j/domain/Role.java b/neo4j/example/src/main/java/example/springdata/neo4j/Role.java similarity index 65% rename from neo4j/example/src/main/java/example/springdata/neo4j/domain/Role.java rename to neo4j/example/src/main/java/example/springdata/neo4j/Role.java index 9a7f3e7e..8114b821 100644 --- a/neo4j/example/src/main/java/example/springdata/neo4j/domain/Role.java +++ b/neo4j/example/src/main/java/example/springdata/neo4j/Role.java @@ -13,7 +13,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package example.springdata.neo4j.domain; +package example.springdata.neo4j; + +import lombok.AccessLevel; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.RequiredArgsConstructor; import org.neo4j.ogm.annotation.EndNode; import org.neo4j.ogm.annotation.GraphId; @@ -22,40 +27,17 @@ import org.neo4j.ogm.annotation.StartNode; /** * A Role relationship entity between an actor and movie. + * * @author Luanne Misquitta */ @RelationshipEntity(type = "ACTED_IN") +@NoArgsConstructor(force = true, access = AccessLevel.PRIVATE) +@RequiredArgsConstructor +@Getter public class Role { - @GraphId private Long id; - @StartNode private Actor actor; - @EndNode private Movie movie; - private String role; - - public Role() { - } - - public Actor getActor() { - return actor; - } - - public void setActor(Actor actor) { - this.actor = actor; - } - - public Movie getMovie() { - return movie; - } - - public void setMovie(Movie movie) { - this.movie = movie; - } - - public String getRole() { - return role; - } - - public void setRole(String role) { - this.role = role; - } + private @GraphId Long id; + private final @StartNode Actor actor; + private final String role; + private final @EndNode Movie movie; } diff --git a/neo4j/example/src/test/java/example/springdata/neo4j/ActorRepositoryIntegrationTest.java b/neo4j/example/src/test/java/example/springdata/neo4j/ActorRepositoryIntegrationTest.java index ecc6e2fa..b66284ef 100644 --- a/neo4j/example/src/test/java/example/springdata/neo4j/ActorRepositoryIntegrationTest.java +++ b/neo4j/example/src/test/java/example/springdata/neo4j/ActorRepositoryIntegrationTest.java @@ -13,52 +13,75 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package example.springdata.neo4j; +import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; -import example.springdata.neo4j.domain.Actor; -import example.springdata.neo4j.domain.Movie; -import example.springdata.neo4j.domain.Role; -import example.springdata.neo4j.repo.ActorRepository; import org.junit.Test; import org.junit.runner.RunWith; +import org.neo4j.ogm.session.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.test.annotation.DirtiesContext; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.neo4j.config.Neo4jConfiguration; +import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories; +import org.springframework.data.neo4j.server.InProcessServer; +import org.springframework.data.neo4j.server.Neo4jServer; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.transaction.annotation.EnableTransactionManagement; +import org.springframework.transaction.annotation.Transactional; /** * Simple integration test demonstrating the use of the ActorRepository + * * @author Luanne Misquitta + * @author Oliver Gierke */ -@ContextConfiguration(classes = {ExampleConfig.class}) @RunWith(SpringJUnit4ClassRunner.class) -@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD) +@ContextConfiguration +@Transactional public class ActorRepositoryIntegrationTest { + @Configuration + @EnableTransactionManagement + @EnableNeo4jRepositories + static class ExampleConfig extends Neo4jConfiguration { + + @Override + public Neo4jServer neo4jServer() { + return new InProcessServer(); + } + + @Override + public SessionFactory getSessionFactory() { + return new SessionFactory("example.springdata.neo4j"); + } + } + @Autowired ActorRepository actorRepository; /** - * @see DATAGRAPH-752 - * Test case to demonstrate saving node and relationship entities, and loading them via the repository. + * @see #131 */ @Test public void shouldBeAbleToSaveAndLoadActor() { - Actor daniel = new Actor("Daniel Radcliffe"); Movie goblet = new Movie("Harry Potter and the Goblet of Fire"); - daniel.actedIn(goblet,"Harry Potter"); - actorRepository.save(daniel); //saves the actor and the movie + Actor daniel = new Actor("Daniel Radcliffe"); + daniel.actedIn(goblet, "Harry Potter"); + + actorRepository.save(daniel); // saves the actor and the movie Actor actor = actorRepository.findOne(daniel.getId()); - assertNotNull(actor); - assertEquals(daniel.getName(),actor.getName()); - assertEquals(1,actor.getRoles().size()); - Role role = actor.getRoles().iterator().next(); - assertEquals("Harry Potter", role.getRole()); - } + assertThat(actor, is(notNullValue())); + assertThat(actor.getName(), is(daniel.getName())); + assertThat(actor.getRoles(), hasSize(1)); + + Role role = actor.getRoles().iterator().next(); + + assertThat(role.getRole(), is("Harry Potter")); + } } diff --git a/neo4j/example/src/test/java/example/springdata/neo4j/ExampleConfig.java b/neo4j/example/src/test/java/example/springdata/neo4j/ExampleConfig.java deleted file mode 100644 index 18bab351..00000000 --- a/neo4j/example/src/test/java/example/springdata/neo4j/ExampleConfig.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright 2015 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package example.springdata.neo4j; - -import org.neo4j.ogm.session.SessionFactory; -import org.springframework.context.annotation.Configuration; -import org.springframework.data.neo4j.config.Neo4jConfiguration; -import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories; -import org.springframework.data.neo4j.server.InProcessServer; -import org.springframework.data.neo4j.server.Neo4jServer; -import org.springframework.transaction.annotation.EnableTransactionManagement; - -/** - * @author Luanne Misquitta - */ -@Configuration -@EnableTransactionManagement -@EnableNeo4jRepositories("example.springdata.neo4j.repo") -public class ExampleConfig extends Neo4jConfiguration{ - - @Override - public Neo4jServer neo4jServer() { - return new InProcessServer(); - } - - @Override - public SessionFactory getSessionFactory() { - return new SessionFactory("example.springdata.neo4j.domain"); - } -} diff --git a/neo4j/pom.xml b/neo4j/pom.xml index 9b28ccb1..b99f7f7d 100644 --- a/neo4j/pom.xml +++ b/neo4j/pom.xml @@ -2,24 +2,24 @@ - 4.0.0 + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 - spring-data-neo4j-examples - pom + spring-data-neo4j-examples + pom - - org.springframework.data.examples - spring-data-examples - 1.0.0.BUILD-SNAPSHOT - + + org.springframework.data.examples + spring-data-examples + 1.0.0.BUILD-SNAPSHOT + - Spring Data Neo4j 4 - Examples - Sample projects for Spring Data Neo4j 4 + Spring Data Neo4j 4 - Examples + Sample projects for Spring Data Neo4j 4 - - example - + + example + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 7effc720..8ebb7085 100644 --- a/pom.xml +++ b/pom.xml @@ -69,7 +69,7 @@ org.projectlombok lombok - 1.16.4 + 1.16.6 provided