44import com .baeldung .spring .data .neo4j .model .Book ;
55import com .baeldung .spring .data .neo4j .model .Person ;
66import com .baeldung .spring .data .neo4j .service .BookService ;
7- import org .apache .commons .logging .Log ;
8- import org .apache .commons .logging .LogFactory ;
97import org .junit .Test ;
108import org .junit .runner .RunWith ;
119import org .springframework .beans .factory .annotation .Autowired ;
1210import org .springframework .test .context .ContextConfiguration ;
1311import org .springframework .test .context .junit4 .SpringJUnit4ClassRunner ;
1412
1513import static org .junit .Assert .assertEquals ;
14+ import static org .junit .Assert .assertNull ;
1615
1716@ RunWith (SpringJUnit4ClassRunner .class )
1817@ ContextConfiguration (classes = LibraryNeo4jConfiguration .class )
1918public 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