Adding test cases

This commit is contained in:
Sameera Nelson 2016-05-13 19:58:19 +05:30
parent 6f95540287
commit 6e6b4612f8
5 changed files with 111 additions and 49 deletions

View File

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

@ -12,11 +12,5 @@ import java.util.Map;
@Repository
public interface BookRepository extends GraphRepository<Book> {
Book findByTitle(@Param("title") String title);
@Query("MATCH (m:Book) WHERE m.title =~ ('(?i).*'+{title}+'.*') RETURN m")
Collection<Book> 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<Map<String,Object>> graph(@Param("limit") int limit);
}

View File

@ -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<String, Object> graph(int limit);
Book save(Book book);
void delete(long bookId);
long bookCount();
Book findBookById(Long id);
void deleteAllInGraph();

View File

@ -14,49 +14,25 @@ public class BookServiceImpl implements BookService {
@Autowired
private BookRepository bookRepository;
private Map<String, Object> toD3Format(final 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("book"),"label","book"));
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(final String key1, final Object value1, final String key2, final 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(final int limit) {
Iterator<Map<String, Object>> 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();
}
}

View File

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