updating test cases

This commit is contained in:
Sameera 2016-06-28 14:09:00 +05:30
parent 6e6b4612f8
commit 718c04ceeb
17 changed files with 543 additions and 307 deletions

View File

@ -8,12 +8,20 @@
<packaging>jar</packaging>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.2.5.RELEASE</spring.version>
<spring-data-neo4j.version>4.0.0.RELEASE</spring-data-neo4j.version>
<nosqlunit.version>0.8.0</nosqlunit.version>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<neo4j.version>3.0.1</neo4j.version>
<spring-data-neo4j.version>4.1.1.RELEASE</spring-data-neo4j.version>
<spring-data-releasetrain.version>Hopper-SR1</spring-data-releasetrain.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
@ -22,15 +30,8 @@
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
@ -40,23 +41,95 @@
<scope>compile</scope>
</dependency>
<!-- end::dependencies[] -->
<!-- Test Dependencies -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-neo4j</artifactId>
<version>${spring-data-neo4j.version}</version>
<type>test-jar</type>
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-kernel</artifactId>
<version>${neo4j.version}</version>
<type>test-jar</type>
</dependency>
<dependency>
<groupId>org.neo4j.app</groupId>
<artifactId>neo4j-server</artifactId>
<version>${neo4j.version}</version>
<type>test-jar</type>
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-ogm-test</artifactId>
<version>2.0.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.neo4j.test</groupId>
<artifactId>neo4j-harness</artifactId>
<version>${neo4j.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<version>4.2.3.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<url>http://repo.spring.io/libs-snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<url>http://repo.spring.io/libs-snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>

View File

@ -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");
}
}

View File

@ -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");
}
}

View File

@ -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");
}
}

View File

@ -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<Role> roles;
// end::movie[]
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;
}
}

View File

@ -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;
}
}

View File

@ -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<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;
}
}

View File

@ -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;
}
}

View File

@ -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<Book> 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<Book> getBooks() {
return books;
}
public void setBooks(List<Book> books) {
this.books = books;
}
}

View File

@ -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<Book> {
}

View File

@ -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> {
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);
}

View File

@ -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<Person> {
}

View File

@ -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();
}

View File

@ -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();
}
}

View File

@ -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<String, Object> toD3Format(Iterator<Map<String, Object>> result) {
List<Map<String,Object>> nodes = new ArrayList<Map<String,Object>>();
List<Map<String,Object>> rels= new ArrayList<Map<String,Object>>();
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<String,Object>(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);
}
}

View File

@ -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);
}
}

View File

@ -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<String> roleNames = new HashSet();
roleNames.add("Charlie Croker");
neo.setRoles(roleNames);
List<Role> 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<Movie> result = (Collection<Movie>) instance.findAll();
assertNotNull(result);
assertEquals(1, result.size());
}
@Test
@DirtiesContext
public void testFindByTitleContaining() {
System.out.println("findByTitleContaining");
String title = "Italian";
Collection<Movie> result = instance.findByTitleContaining(title);
assertNotNull(result);
assertEquals(1,result.size());
}
@Test
@DirtiesContext
public void testGraph() {
System.out.println("graph");
List<Map<String,Object>> graph = instance.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");
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<Movie> result = (Collection<Movie>) instance.findAll();
assertEquals(0,result.size());
}
}