Skip to content

Commit 6e6b461

Browse files
committed
Adding test cases
1 parent 6f95540 commit 6e6b461

5 files changed

Lines changed: 111 additions & 49 deletions

File tree

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.baeldung.spring.data.neo4j.model;
2+
3+
import org.neo4j.ogm.annotation.*;
4+
5+
import java.util.List;
6+
import java.util.concurrent.atomic.AtomicLong;
7+
8+
@NodeEntity
9+
public class Person {
10+
11+
private static final AtomicLong TS = new AtomicLong();
12+
13+
@GraphId
14+
private Long id;
15+
private String name;
16+
private int born;
17+
18+
@Relationship(type = "AUTHORED_BY")
19+
private List<Book> books;
20+
21+
public Person() {
22+
this.id = TS.incrementAndGet();
23+
}
24+
25+
public Long getId() {
26+
return id;
27+
}
28+
29+
public String getName() {
30+
return name;
31+
}
32+
33+
public void setName(String name) {
34+
this.name = name;
35+
}
36+
37+
public int getBorn() {
38+
return born;
39+
}
40+
41+
public void setBorn(int born) {
42+
this.born = born;
43+
}
44+
45+
public List<Book> getBooks() {
46+
return books;
47+
}
48+
49+
public void setBooks(List<Book> books) {
50+
this.books = books;
51+
}
52+
}

‎spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/repostory/BookRepository.java‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,5 @@
1212

1313
@Repository
1414
public interface BookRepository extends GraphRepository<Book> {
15-
Book findByTitle(@Param("title") String title);
1615

17-
@Query("MATCH (m:Book) WHERE m.title =~ ('(?i).*'+{title}+'.*') RETURN m")
18-
Collection<Book> findByTitleContaining(@Param("title") String title);
19-
20-
@Query("MATCH (m:Book)<-[:ACTED_IN]-(a:Person) RETURN m.title as Book, collect(a.name) as cast LIMIT {limit}")
21-
List<Map<String,Object>> graph(@Param("limit") int limit);
2216
}

‎spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/service/BookService.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44

55
import java.util.Map;
66

7-
/**
8-
* Created by SDN on 5/13/2016.
9-
*/
107
public interface BookService {
11-
Map<String, Object> graph(int limit);
128

139
Book save(Book book);
1410

11+
void delete(long bookId);
12+
13+
long bookCount();
14+
1515
Book findBookById(Long id);
1616

1717
void deleteAllInGraph();

‎spring-data-neo4j/src/main/java/com/baeldung/spring/data/neo4j/service/BookServiceImpl.java‎

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,49 +14,25 @@ public class BookServiceImpl implements BookService {
1414
@Autowired
1515
private BookRepository bookRepository;
1616

17-
private Map<String, Object> toD3Format(final Iterator<Map<String, Object>> result) {
18-
List<Map<String,Object>> nodes = new ArrayList<Map<String,Object>>();
19-
List<Map<String,Object>> rels= new ArrayList<Map<String,Object>>();
20-
int i=0;
21-
while (result.hasNext()) {
22-
Map<String, Object> row = result.next();
23-
nodes.add(map("title",row.get("book"),"label","book"));
24-
int target=i;
25-
i++;
26-
for (Object name : (Collection) row.get("cast")) {
27-
Map<String, Object> actor = map("title", name,"label","actor");
28-
int source = nodes.indexOf(actor);
29-
if (source == -1) {
30-
nodes.add(actor);
31-
source = i++;
32-
}
33-
rels.add(map("source",source,"target",target));
34-
}
35-
}
36-
return map("nodes", nodes, "links", rels);
37-
}
38-
39-
private Map<String, Object> map(final String key1, final Object value1, final String key2, final Object value2) {
40-
Map<String, Object> result = new HashMap<String,Object>(2);
41-
result.put(key1,value1);
42-
result.put(key2,value2);
43-
return result;
44-
}
45-
46-
public Map<String, Object> graph(final int limit) {
47-
Iterator<Map<String, Object>> result = bookRepository.graph(limit).iterator();
48-
return toD3Format(result);
49-
}
50-
5117
public Book save(final Book book){
5218
return bookRepository.save(book);
5319
}
5420

21+
public long bookCount(){
22+
return bookRepository.count();
23+
}
24+
5525
public Book findBookById(final Long id){
5626
return bookRepository.findOne(id);
5727
}
5828

29+
public void delete(final long bookId){
30+
bookRepository.delete(bookId);
31+
}
32+
5933
public void deleteAllInGraph(){
6034
bookRepository.deleteAll();
6135
}
36+
37+
6238
}

‎spring-data-neo4j/src/test/java/com/baeldung/spring/data/neo4j/BookServiceTest.java‎

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,24 @@
44
import com.baeldung.spring.data.neo4j.model.Book;
55
import com.baeldung.spring.data.neo4j.model.Person;
66
import com.baeldung.spring.data.neo4j.service.BookService;
7-
import org.apache.commons.logging.Log;
8-
import org.apache.commons.logging.LogFactory;
97
import org.junit.Test;
108
import org.junit.runner.RunWith;
119
import org.springframework.beans.factory.annotation.Autowired;
1210
import org.springframework.test.context.ContextConfiguration;
1311
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
1412

1513
import static org.junit.Assert.assertEquals;
14+
import static org.junit.Assert.assertNull;
1615

1716
@RunWith(SpringJUnit4ClassRunner.class)
1817
@ContextConfiguration(classes = LibraryNeo4jConfiguration.class)
1918
public class BookServiceTest {
2019

21-
private static final Log LOGGER = LogFactory.getLog(BookServiceTest.class);
22-
2320
@Autowired
2421
private BookService bookServiceImpl;
2522

2623
@Test
27-
public void testSaveBook() {
24+
public void testSavingBook() {
2825
final Person author1 = new Person();
2926
author1.setName("Mark Twain");
3027
author1.setBorn(1835);
@@ -36,4 +33,47 @@ public void testSaveBook() {
3633
final Book savedBook = bookServiceImpl.save(book);
3734
assertEquals(book.getTitle(), savedBook.getTitle());
3835
}
36+
37+
@Test
38+
public void testFindingTheSavedBook() {
39+
final Person author1 = new Person();
40+
author1.setName("Edgar Allan Poe");
41+
author1.setBorn(1809);
42+
final Book book = new Book();
43+
book.setTitle("The Cask of Amontillado");
44+
book.setReleased(1846);
45+
book.setPerson(author1);
46+
47+
bookServiceImpl.save(book);
48+
final Book retrievedBook = bookServiceImpl.findBookById(book.getId());
49+
assertEquals(book.getTitle(), retrievedBook.getTitle());
50+
}
51+
52+
@Test
53+
public void testCountTheSavedBooks() {
54+
long bookCount = bookServiceImpl.bookCount();
55+
assertEquals(bookCount, 2);
56+
}
57+
@Test
58+
public void testDeletingASavedBook() {
59+
final Person author1 = new Person();
60+
author1.setName("Rider Haggard");
61+
author1.setBorn(1856);
62+
final Book book = new Book();
63+
book.setTitle("King Solomon's Mines");
64+
book.setReleased(1885);
65+
book.setPerson(author1);
66+
67+
final Book savedBook = bookServiceImpl.save(book);
68+
bookServiceImpl.delete(savedBook.getId());
69+
final Book retrievedBook = bookServiceImpl.findBookById(book.getId());
70+
assertNull(retrievedBook);
71+
}
72+
73+
@Test
74+
public void testDeleteAllSavedBook() {
75+
bookServiceImpl.deleteAllInGraph();
76+
final long bookCount = bookServiceImpl.bookCount();
77+
assertEquals(bookCount, 0);
78+
}
3979
}

0 commit comments

Comments
 (0)