From 718c04ceebe6525e1b569879ea8e943edb23a919 Mon Sep 17 00:00:00 2001 From: Sameera Date: Tue, 28 Jun 2016 14:09:00 +0530 Subject: [PATCH] updating test cases --- spring-data-neo4j/pom.xml | 109 ++++++++++++--- .../config/LibraryNeo4jConfiguration.java | 32 ----- .../MovieDatabaseNeo4jConfiguration.java | 38 +++++ .../MovieDatabaseNeo4jTestConfiguration.java | 34 +++++ .../spring/data/neo4j/domain/Movie.java | 63 +++++++++ .../spring/data/neo4j/domain/Person.java | 50 +++++++ .../spring/data/neo4j/domain/Role.java | 50 +++++++ .../spring/data/neo4j/model/Book.java | 54 -------- .../spring/data/neo4j/model/Person.java | 52 ------- .../data/neo4j/repostory/BookRepository.java | 16 --- .../data/neo4j/repostory/MovieRepository.java | 25 ++++ .../neo4j/repostory/PersonRepository.java | 11 ++ .../data/neo4j/service/BookService.java | 18 --- .../data/neo4j/service/BookServiceImpl.java | 38 ----- .../data/neo4j/services/MovieService.java | 50 +++++++ .../spring/data/neo4j/BookServiceTest.java | 79 ----------- .../data/neo4j/MovieRepositoryTest.java | 131 ++++++++++++++++++ 17 files changed, 543 insertions(+), 307 deletions(-) delete mode 100644 spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/config/LibraryNeo4jConfiguration.java create mode 100644 spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/config/MovieDatabaseNeo4jConfiguration.java create mode 100644 spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/config/MovieDatabaseNeo4jTestConfiguration.java create mode 100644 spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/domain/Movie.java create mode 100644 spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/domain/Person.java create mode 100644 spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/domain/Role.java delete mode 100644 spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/model/Book.java delete mode 100644 spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/model/Person.java delete mode 100644 spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/repostory/BookRepository.java create mode 100644 spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/repostory/MovieRepository.java create mode 100644 spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/repostory/PersonRepository.java delete mode 100644 spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/service/BookService.java delete mode 100644 spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/service/BookServiceImpl.java create mode 100644 spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/services/MovieService.java delete mode 100644 spring-data-neo4j/src/test/java/com/baeldung/spring/data/neo4j/BookServiceTest.java create mode 100644 spring-data-neo4j/src/test/java/com/baeldung/spring/data/neo4j/MovieRepositoryTest.java diff --git a/spring-data-neo4j/pom.xml b/spring-data-neo4j/pom.xml index 72e17250db..320b6ce019 100644 --- a/spring-data-neo4j/pom.xml +++ b/spring-data-neo4j/pom.xml @@ -8,12 +8,20 @@ jar + 1.8 UTF-8 - 4.2.5.RELEASE - 4.0.0.RELEASE - 0.8.0 + UTF-8 + 3.0.1 + 4.1.1.RELEASE + Hopper-SR1 + + org.springframework.boot + spring-boot-starter-parent + 1.3.0.RELEASE + + org.springframework.data @@ -22,15 +30,8 @@ - org.springframework - spring-core - ${spring.version} - - - - org.springframework - spring-context - ${spring.version} + org.springframework.boot + spring-boot-starter-data-rest @@ -40,23 +41,95 @@ compile + + + - log4j - log4j - 1.2.16 + org.springframework.boot + spring-boot-starter-test + test + + org.springframework.data + spring-data-neo4j + ${spring-data-neo4j.version} + test-jar + + + + org.neo4j + neo4j-kernel + ${neo4j.version} + test-jar + + + + org.neo4j.app + neo4j-server + ${neo4j.version} + test-jar + + + + org.neo4j + neo4j-ogm-test + 2.0.2 + test + + + + org.neo4j.test + neo4j-harness + ${neo4j.version} + test + + + junit + junit + 4.12 + junit junit 4.12 - org.springframework spring-test - ${spring.version} + 4.2.3.RELEASE + - + + + + + maven-compiler-plugin + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + + spring-milestones + http://repo.spring.io/libs-snapshot + + true + + + + + + spring-milestones + http://repo.spring.io/libs-snapshot + + true + + + diff --git a/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/config/LibraryNeo4jConfiguration.java b/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/config/LibraryNeo4jConfiguration.java deleted file mode 100644 index c497c944d1..0000000000 --- a/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/config/LibraryNeo4jConfiguration.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.baeldung.spring.data.neo4j.config; - - -import org.neo4j.ogm.session.SessionFactory; -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Import; -import org.springframework.data.neo4j.config.Neo4jConfiguration; -import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories; -import org.springframework.data.neo4j.server.Neo4jServer; -import org.springframework.data.neo4j.server.RemoteServer; -import org.springframework.scheduling.annotation.EnableScheduling; -import org.springframework.transaction.annotation.EnableTransactionManagement; - - -@Configuration -@ComponentScan("com.baeldung.spring.data.neo4j") -@EnableNeo4jRepositories(basePackages = "com.baeldung.spring.data.neo4j.repostory") -public class LibraryNeo4jConfiguration extends Neo4jConfiguration { - - public static final String URL = System.getenv("NEO4J_URL") != null ? System.getenv("NEO4J_URL") : "http://localhost:7474"; - - @Override - public Neo4jServer neo4jServer() { - return new RemoteServer(URL,"neo4j","password"); - } - - @Override - public SessionFactory getSessionFactory() { - return new SessionFactory("com.baeldung.spring.data.neo4j.model"); - } -} \ No newline at end of file diff --git a/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/config/MovieDatabaseNeo4jConfiguration.java b/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/config/MovieDatabaseNeo4jConfiguration.java new file mode 100644 index 0000000000..6e57eb1f36 --- /dev/null +++ b/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/config/MovieDatabaseNeo4jConfiguration.java @@ -0,0 +1,38 @@ +package com.baeldung.spring.data.neo4j.config; + +import org.neo4j.ogm.session.SessionFactory; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.neo4j.config.Neo4jConfiguration; +import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories; +import org.springframework.scheduling.annotation.EnableScheduling; +import org.springframework.transaction.annotation.EnableTransactionManagement; + + +@EnableTransactionManagement +@EnableScheduling +@EnableAutoConfiguration +@ComponentScan(basePackages = {"com.baeldung.spring.data.neo4j.services"}) +@Configuration +@EnableNeo4jRepositories(basePackages = "com.baeldung.spring.data.neo4j.repostory") +public class MovieDatabaseNeo4jConfiguration extends Neo4jConfiguration { + + 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; + } + + @Override + public SessionFactory getSessionFactory() { + return new SessionFactory(getConfiguration(), "com.baeldung.spring.data.neo4j.domain"); + } +} diff --git a/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/config/MovieDatabaseNeo4jTestConfiguration.java b/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/config/MovieDatabaseNeo4jTestConfiguration.java new file mode 100644 index 0000000000..2b6394184d --- /dev/null +++ b/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/config/MovieDatabaseNeo4jTestConfiguration.java @@ -0,0 +1,34 @@ +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.data.neo4j.server.Neo4jServer; +import org.springframework.transaction.annotation.EnableTransactionManagement; + + +@EnableTransactionManagement +@ComponentScan(basePackages = {"com.baeldung.spring.data.neo4j.services"}) +@Configuration +@EnableNeo4jRepositories(basePackages = "com.baeldung.spring.data.neo4j.repostory") +@Profile({"embedded", "test"}) +public class MovieDatabaseNeo4jTestConfiguration extends Neo4jConfiguration { + + @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.embedded.driver.EmbeddedDriver"); + return config; + } + + @Override + public SessionFactory getSessionFactory() { + return new SessionFactory(getConfiguration(), "com.baeldung.spring.data.neo4j.domain"); + } +} diff --git a/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/domain/Movie.java b/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/domain/Movie.java new file mode 100644 index 0000000000..8d68b69c05 --- /dev/null +++ b/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/domain/Movie.java @@ -0,0 +1,63 @@ +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 roles; + +// end::movie[] + + public Movie() { } + + public String getTitle() { + return title; + } + + public int getReleased() { + return released; + } + + public String getTagline() { + return tagline; + } + + public Collection 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 roles) { + this.roles = roles; + } + + +} diff --git a/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/domain/Person.java b/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/domain/Person.java new file mode 100644 index 0000000000..d96dc07530 --- /dev/null +++ b/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/domain/Person.java @@ -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 movies; + + public Person() { } + + public String getName() { + return name; + } + + public int getBorn() { + return born; + } + + public List getMovies() { + return movies; + } + + public void setName(String name) { + this.name = name; + } + + public void setBorn(int born) { + this.born = born; + } + + public void setMovies(List movies) { + this.movies = movies; + } + +} diff --git a/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/domain/Role.java b/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/domain/Role.java new file mode 100644 index 0000000000..20512a10ad --- /dev/null +++ b/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/domain/Role.java @@ -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.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 roles; + @StartNode + private Person person; + @EndNode + private Movie movie; + + public Role() { + } + + public Collection getRoles() { + return roles; + } + + public Person getPerson() { + return person; + } + + public Movie getMovie() { + return movie; + } + + public void setRoles(Collection roles) { + this.roles = roles; + } + + public void setPerson(Person person) { + this.person = person; + } + + public void setMovie(Movie movie) { + this.movie = movie; + } +} diff --git a/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/model/Book.java b/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/model/Book.java deleted file mode 100644 index 2c643a4b7c..0000000000 --- a/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/model/Book.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.baeldung.spring.data.neo4j.model; - -import org.neo4j.graphdb.Direction; -import org.neo4j.ogm.annotation.*; - -import java.util.Collection; -import java.util.List; -import java.util.concurrent.atomic.AtomicLong; - -@NodeEntity -public class Book { - - private static final AtomicLong TS = new AtomicLong(); - - @GraphId - private Long id; - private String title; - private int released; - - @Relationship(type="AUTHORED_BY", direction = Relationship.INCOMING) - private Person person; - - public Book() { - this.id = TS.getAndIncrement(); - } - - public Long getId() { - return id; - } - - public String getTitle() { - return title; - } - - public void setTitle(String title) { - this.title = title; - } - - public int getReleased() { - return released; - } - - public void setReleased(int released) { - this.released = released; - } - - public Person getPerson() { - return person; - } - - public void setPerson(Person person) { - this.person = person; - } -} \ No newline at end of file diff --git a/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/model/Person.java b/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/model/Person.java deleted file mode 100644 index 53444c8f6c..0000000000 --- a/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/model/Person.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.baeldung.spring.data.neo4j.model; - -import org.neo4j.ogm.annotation.*; - -import java.util.List; -import java.util.concurrent.atomic.AtomicLong; - -@NodeEntity -public class Person { - - private static final AtomicLong TS = new AtomicLong(); - - @GraphId - private Long id; - private String name; - private int born; - - @Relationship(type = "AUTHORED_BY") - private List books; - - public Person() { - this.id = TS.incrementAndGet(); - } - - public Long getId() { - return id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public int getBorn() { - return born; - } - - public void setBorn(int born) { - this.born = born; - } - - public List getBooks() { - return books; - } - - public void setBooks(List books) { - this.books = books; - } -} diff --git a/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/repostory/BookRepository.java b/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/repostory/BookRepository.java deleted file mode 100644 index c76d2862fc..0000000000 --- a/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/repostory/BookRepository.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.baeldung.spring.data.neo4j.repostory; - -import com.baeldung.spring.data.neo4j.model.Book; -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 BookRepository extends GraphRepository { - -} \ No newline at end of file diff --git a/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/repostory/MovieRepository.java b/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/repostory/MovieRepository.java new file mode 100644 index 0000000000..3c5953eff3 --- /dev/null +++ b/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/repostory/MovieRepository.java @@ -0,0 +1,25 @@ +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.data.rest.core.annotation.RepositoryRestResource; + +import java.util.Collection; +import java.util.List; +import java.util.Map; + + +@RepositoryRestResource(collectionResourceRel = "movies", path = "movies") +public interface MovieRepository extends GraphRepository { + Movie findByTitle(@Param("title") String title); + + @Query("MATCH (m:Movie) WHERE m.title =~ ('(?i).*'+{title}+'.*') RETURN m") + Collection 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> graph(@Param("limit") int limit); +} + + diff --git a/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/repostory/PersonRepository.java b/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/repostory/PersonRepository.java new file mode 100644 index 0000000000..4c287f99a4 --- /dev/null +++ b/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/repostory/PersonRepository.java @@ -0,0 +1,11 @@ +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 { + +} diff --git a/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/service/BookService.java b/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/service/BookService.java deleted file mode 100644 index daa988c92d..0000000000 --- a/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/service/BookService.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.baeldung.spring.data.neo4j.service; - -import com.baeldung.spring.data.neo4j.model.Book; - -import java.util.Map; - -public interface BookService { - - Book save(Book book); - - void delete(long bookId); - - long bookCount(); - - Book findBookById(Long id); - - void deleteAllInGraph(); -} diff --git a/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/service/BookServiceImpl.java b/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/service/BookServiceImpl.java deleted file mode 100644 index 0c07e4acd9..0000000000 --- a/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/service/BookServiceImpl.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.baeldung.spring.data.neo4j.service; - - -import com.baeldung.spring.data.neo4j.model.Book; -import com.baeldung.spring.data.neo4j.repostory.BookRepository; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -import java.util.*; - -@Service -public class BookServiceImpl implements BookService { - - @Autowired - private BookRepository bookRepository; - - public Book save(final Book book){ - return bookRepository.save(book); - } - - public long bookCount(){ - return bookRepository.count(); - } - - public Book findBookById(final Long id){ - return bookRepository.findOne(id); - } - - public void delete(final long bookId){ - bookRepository.delete(bookId); - } - - public void deleteAllInGraph(){ - bookRepository.deleteAll(); - } - - -} \ No newline at end of file diff --git a/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/services/MovieService.java b/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/services/MovieService.java new file mode 100644 index 0000000000..532cc79091 --- /dev/null +++ b/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/services/MovieService.java @@ -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 + MovieRepository movieRepository; + + private Map toD3Format(Iterator> result) { + List> nodes = new ArrayList>(); + List> rels= new ArrayList>(); + int i=0; + while (result.hasNext()) { + Map row = result.next(); + nodes.add(map("title",row.get("movie"),"label","movie")); + int target=i; + i++; + for (Object name : (Collection) row.get("cast")) { + Map 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 map(String key1, Object value1, String key2, Object value2) { + Map result = new HashMap(2); + result.put(key1,value1); + result.put(key2,value2); + return result; + } + + public Map graph(int limit) { + Iterator> result = movieRepository.graph(limit).iterator(); + return toD3Format(result); + } +} diff --git a/spring-data-neo4j/src/test/java/com/baeldung/spring/data/neo4j/BookServiceTest.java b/spring-data-neo4j/src/test/java/com/baeldung/spring/data/neo4j/BookServiceTest.java deleted file mode 100644 index 17c75a1921..0000000000 --- a/spring-data-neo4j/src/test/java/com/baeldung/spring/data/neo4j/BookServiceTest.java +++ /dev/null @@ -1,79 +0,0 @@ -package com.baeldung.spring.data.neo4j; - -import com.baeldung.spring.data.neo4j.config.LibraryNeo4jConfiguration; -import com.baeldung.spring.data.neo4j.model.Book; -import com.baeldung.spring.data.neo4j.model.Person; -import com.baeldung.spring.data.neo4j.service.BookService; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; - -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(classes = LibraryNeo4jConfiguration.class) -public class BookServiceTest { - - @Autowired - private BookService bookServiceImpl; - - @Test - public void testSavingBook() { - final Person author1 = new Person(); - author1.setName("Mark Twain"); - author1.setBorn(1835); - final Book book = new Book(); - book.setTitle("The Adventures of Tom Sawyer"); - book.setReleased(1876); - book.setPerson(author1); - - final Book savedBook = bookServiceImpl.save(book); - assertEquals(book.getTitle(), savedBook.getTitle()); - } - - @Test - public void testFindingTheSavedBook() { - final Person author1 = new Person(); - author1.setName("Edgar Allan Poe"); - author1.setBorn(1809); - final Book book = new Book(); - book.setTitle("The Cask of Amontillado"); - book.setReleased(1846); - book.setPerson(author1); - - bookServiceImpl.save(book); - final Book retrievedBook = bookServiceImpl.findBookById(book.getId()); - assertEquals(book.getTitle(), retrievedBook.getTitle()); - } - - @Test - public void testCountTheSavedBooks() { - long bookCount = bookServiceImpl.bookCount(); - assertEquals(bookCount, 2); - } - @Test - public void testDeletingASavedBook() { - final Person author1 = new Person(); - author1.setName("Rider Haggard"); - author1.setBorn(1856); - final Book book = new Book(); - book.setTitle("King Solomon's Mines"); - book.setReleased(1885); - book.setPerson(author1); - - final Book savedBook = bookServiceImpl.save(book); - bookServiceImpl.delete(savedBook.getId()); - final Book retrievedBook = bookServiceImpl.findBookById(book.getId()); - assertNull(retrievedBook); - } - - @Test - public void testDeleteAllSavedBook() { - bookServiceImpl.deleteAllInGraph(); - final long bookCount = bookServiceImpl.bookCount(); - assertEquals(bookCount, 0); - } -} diff --git a/spring-data-neo4j/src/test/java/com/baeldung/spring/data/neo4j/MovieRepositoryTest.java b/spring-data-neo4j/src/test/java/com/baeldung/spring/data/neo4j/MovieRepositoryTest.java new file mode 100644 index 0000000000..4f09284b6a --- /dev/null +++ b/spring-data-neo4j/src/test/java/com/baeldung/spring/data/neo4j/MovieRepositoryTest.java @@ -0,0 +1,131 @@ +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 MovieRepositoryTest { + + @Autowired + private MovieRepository instance; + + @Autowired + private PersonRepository personRepository; + + public MovieRepositoryTest() { + } + + @Before + public void initializeDatabase() { + System.out.println("seeding embedded database"); + Movie matrix = new Movie(); + matrix.setTitle("The Italian Job"); + matrix.setReleased(1999); + instance.save(matrix); + + Person mark = new Person(); + mark.setName("Mark Wahlberg"); + personRepository.save(mark); + + Role neo = new Role(); + neo.setMovie(matrix); + neo.setPerson(mark); + Collection roleNames = new HashSet(); + roleNames.add("Charlie Croker"); + neo.setRoles(roleNames); + List roles = new ArrayList(); + roles.add(neo); + matrix.setRoles(roles); + instance.save(matrix); + } + + @Test + @DirtiesContext + public void testFindByTitle() { + System.out.println("findByTitle"); + String title = "The Italian Job"; + Movie result = instance.findByTitle(title); + assertNotNull(result); + assertEquals(1999, result.getReleased()); + } + + @Test + @DirtiesContext + public void testCount() { + System.out.println("count"); + long result = instance.count(); + assertNotNull(result); + assertEquals(1, result); + } + + @Test + @DirtiesContext + public void testFindAll() { + System.out.println("findAll"); + Collection result = (Collection) instance.findAll(); + assertNotNull(result); + assertEquals(1, result.size()); + } + + @Test + @DirtiesContext + public void testFindByTitleContaining() { + System.out.println("findByTitleContaining"); + String title = "Italian"; + Collection result = instance.findByTitleContaining(title); + assertNotNull(result); + assertEquals(1,result.size()); + } + + @Test + @DirtiesContext + public void testGraph() { + System.out.println("graph"); + List> graph = instance.graph(5); + assertEquals(1,graph.size()); + Map 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"); + instance.delete( + instance.findByTitle("The Italian Job")); + assertNull(instance.findByTitle("The Italian Job")); + } + + @Test + @DirtiesContext + public void testDeleteAll() { + System.out.println("deleteAll"); + instance.deleteAll(); + Collection result = (Collection) instance.findAll(); + assertEquals(0,result.size()); + } +}