From 6e6b4612f8dd914484118cde4fad6f12c879fbc4 Mon Sep 17 00:00:00 2001 From: Sameera Nelson Date: Fri, 13 May 2016 19:58:19 +0530 Subject: [PATCH] Adding test cases --- .../spring/data/neo4j/model/Person.java | 52 +++++++++++++++++++ .../data/neo4j/repostory/BookRepository.java | 6 --- .../data/neo4j/service/BookService.java | 8 +-- .../data/neo4j/service/BookServiceImpl.java | 44 ++++------------ .../spring/data/neo4j/BookServiceTest.java | 50 ++++++++++++++++-- 5 files changed, 111 insertions(+), 49 deletions(-) create mode 100644 spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/model/Person.java 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 new file mode 100644 index 0000000000..53444c8f6c --- /dev/null +++ b/spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/model/Person.java @@ -0,0 +1,52 @@ +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 index 5c980b0381..c76d2862fc 100644 --- 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 @@ -12,11 +12,5 @@ import java.util.Map; @Repository public interface BookRepository extends GraphRepository { - Book findByTitle(@Param("title") String title); - @Query("MATCH (m:Book) WHERE m.title =~ ('(?i).*'+{title}+'.*') RETURN m") - Collection findByTitleContaining(@Param("title") String title); - - @Query("MATCH (m:Book)<-[:ACTED_IN]-(a:Person) RETURN m.title as Book, collect(a.name) as cast LIMIT {limit}") - List> graph(@Param("limit") int limit); } \ No newline at end of file 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 index f8983dfd16..daa988c92d 100644 --- 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 @@ -4,14 +4,14 @@ import com.baeldung.spring.data.neo4j.model.Book; import java.util.Map; -/** - * Created by SDN on 5/13/2016. - */ public interface BookService { - Map graph(int limit); 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 index cd50f8b740..0c07e4acd9 100644 --- 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 @@ -14,49 +14,25 @@ public class BookServiceImpl implements BookService { @Autowired private BookRepository bookRepository; - private Map toD3Format(final 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("book"),"label","book")); - 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(final String key1, final Object value1, final String key2, final Object value2) { - Map result = new HashMap(2); - result.put(key1,value1); - result.put(key2,value2); - return result; - } - - public Map graph(final int limit) { - Iterator> result = bookRepository.graph(limit).iterator(); - return toD3Format(result); - } - 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/test/java/com/baeldung/spring/data/neo4j/BookServiceTest.java b/spring-data-neo4j/src/test/java/com/baeldung/spring/data/neo4j/BookServiceTest.java index 9a791f87da..17c75a1921 100644 --- 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 @@ -4,8 +4,6 @@ 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.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -13,18 +11,17 @@ 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 { - private static final Log LOGGER = LogFactory.getLog(BookServiceTest.class); - @Autowired private BookService bookServiceImpl; @Test - public void testSaveBook() { + public void testSavingBook() { final Person author1 = new Person(); author1.setName("Mark Twain"); author1.setBorn(1835); @@ -36,4 +33,47 @@ public class BookServiceTest { 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); + } }