File tree 5 files changed +123
-1
lines changed
test/java/com/hhimanshu/business
5 files changed +123
-1
lines changed Original file line number Diff line number Diff line change
1
+ package com .hhimanshu .business ;
2
+
3
+ import com .hhimanshu .persistence .entities .Book ;
4
+ import com .hhimanshu .persistence .repositories .BookRepository ;
5
+ import java .util .stream .Stream ;
6
+ import java .util .stream .StreamSupport ;
7
+ import org .springframework .beans .factory .annotation .Autowired ;
8
+ import org .springframework .stereotype .Component ;
9
+
10
+ @ Component
11
+ public class BooksManager {
12
+
13
+ private BookRepository bookRepository ;
14
+
15
+ @ Autowired
16
+ public BooksManager (BookRepository bookRepository ) {
17
+ this .bookRepository = bookRepository ;
18
+ }
19
+
20
+ public Stream <Book > getAllBooks () {
21
+ return StreamSupport .stream (bookRepository .findAll ().spliterator (), true );
22
+ }
23
+ }
Original file line number Diff line number Diff line change 5
5
import javax .persistence .GeneratedValue ;
6
6
import javax .persistence .GenerationType ;
7
7
import javax .persistence .Id ;
8
+ import javax .persistence .Table ;
8
9
9
- @ Entity (name = "authors" )
10
+ @ Entity
11
+ @ Table (name = "authors" )
10
12
public class Author {
11
13
12
14
@ Id
Original file line number Diff line number Diff line change
1
+ package com .hhimanshu .persistence .entities ;
2
+
3
+ import javax .persistence .Column ;
4
+ import javax .persistence .Entity ;
5
+ import javax .persistence .GeneratedValue ;
6
+ import javax .persistence .GenerationType ;
7
+ import javax .persistence .Id ;
8
+ import javax .persistence .JoinColumn ;
9
+ import javax .persistence .ManyToOne ;
10
+ import javax .persistence .Table ;
11
+
12
+ @ Entity
13
+ @ Table (name = "books" )
14
+ public class Book {
15
+
16
+ @ Id
17
+ @ GeneratedValue (strategy = GenerationType .AUTO )
18
+ private int id ;
19
+
20
+ @ Column (nullable = false , unique = true )
21
+ private String name ;
22
+
23
+ @ ManyToOne
24
+ @ JoinColumn (name = "author" )
25
+ private Author author ;
26
+
27
+ protected Book () {
28
+ // for JPA
29
+ }
30
+
31
+ public Book (int id , String name , Author author ) {
32
+ this .id = id ;
33
+ this .name = name ;
34
+ this .author = author ;
35
+ }
36
+
37
+ public int getId () {
38
+ return id ;
39
+ }
40
+
41
+ public String getName () {
42
+ return name ;
43
+ }
44
+
45
+ public Author getAuthor () {
46
+ return author ;
47
+ }
48
+
49
+ @ Override
50
+ public String toString () {
51
+ return "Book{" +
52
+ "id=" + id +
53
+ ", name='" + name + '\'' +
54
+ ", author=" + author +
55
+ '}' ;
56
+ }
57
+ }
Original file line number Diff line number Diff line change
1
+ package com .hhimanshu .persistence .repositories ;
2
+
3
+ import com .hhimanshu .persistence .entities .Book ;
4
+ import org .springframework .data .repository .CrudRepository ;
5
+
6
+ public interface BookRepository extends CrudRepository <Book , Integer > {
7
+
8
+ }
Original file line number Diff line number Diff line change
1
+ package com .hhimanshu .business ;
2
+
3
+
4
+ import static org .junit .Assert .assertEquals ;
5
+ import static org .junit .Assert .assertFalse ;
6
+
7
+ import com .hhimanshu .persistence .entities .Book ;
8
+ import java .util .List ;
9
+ import java .util .stream .Collectors ;
10
+ import org .junit .Test ;
11
+ import org .junit .runner .RunWith ;
12
+ import org .springframework .beans .factory .annotation .Autowired ;
13
+ import org .springframework .boot .test .context .SpringBootTest ;
14
+ import org .springframework .test .context .junit4 .SpringRunner ;
15
+
16
+ @ RunWith (SpringRunner .class )
17
+ @ SpringBootTest
18
+ public class BooksManagerTest {
19
+
20
+ @ Autowired
21
+ private BooksManager booksManager ;
22
+
23
+ @ Test
24
+ public void getAllBooksReturnsDataFromDatabase () throws Exception {
25
+ List <Book > books = booksManager .getAllBooks ().collect (Collectors .toList ());
26
+ assertFalse (books .isEmpty ());
27
+ assertEquals (5 , books .size ());
28
+
29
+ // If you want to compare all the authors to what we inserted in '02-insert-data-authors.xml'
30
+ books .forEach (System .out ::println );
31
+ }
32
+ }
You can’t perform that action at this time.
0 commit comments