1+ package alexp .blog .service ;
2+
3+ import alexp .blog .model .Post ;
4+ import alexp .blog .model .Tag ;
5+ import alexp .blog .repository .PostRepository ;
6+ import alexp .blog .repository .TagRepository ;
7+ import org .junit .Before ;
8+ import org .junit .Test ;
9+ import org .mockito .*;
10+ import org .springframework .data .domain .Page ;
11+ import org .springframework .data .domain .PageImpl ;
12+ import org .springframework .data .domain .PageRequest ;
13+ import org .springframework .data .domain .Sort ;
14+
15+ import java .util .ArrayList ;
16+ import java .util .List ;
17+
18+ import static org .hamcrest .CoreMatchers .*;
19+ import static org .hamcrest .MatcherAssert .assertThat ;
20+ import static org .mockito .Matchers .isNotNull ;
21+ import static org .mockito .Matchers .refEq ;
22+ import static org .mockito .Mockito .times ;
23+ import static org .mockito .Mockito .verify ;
24+ import static org .mockito .Mockito .when ;
25+
26+ public class PostServiceTest {
27+
28+ @ Mock
29+ private PostRepository postRepository ;
30+
31+ @ Mock
32+ private TagRepository tagRepository ;
33+
34+ @ InjectMocks
35+ private PostServiceImpl postService ;
36+
37+ @ Before
38+ public void setUp () throws Exception {
39+ MockitoAnnotations .initMocks (this );
40+ }
41+
42+ @ Test
43+ public void shouldGetPostPage () {
44+ final int pageSize = 10 ;
45+
46+ List <Post > posts = new ArrayList <>();
47+ for (int i = 0 ; i < 15 ; i ++) {
48+ Post post = new Post ();
49+ post .setId ((long ) i );
50+ posts .add (post );
51+ }
52+
53+ when (postRepository .findAll (Matchers .any (PageRequest .class )))
54+ .thenReturn (new PageImpl <>(new ArrayList <>()));
55+
56+ when (postRepository .findAll (Matchers .eq (new PageRequest (0 , pageSize , Sort .Direction .DESC , "dateTime" ))))
57+ .thenReturn (new PageImpl <>(posts .subList (0 , 10 )));
58+
59+ when (postRepository .findAll (new PageRequest (1 , pageSize , Sort .Direction .DESC , "dateTime" )))
60+ .thenReturn (new PageImpl <>(posts .subList (10 , 15 )));
61+
62+ Page <Post > page1 = postService .getPostsPage (0 , pageSize );
63+ Page <Post > page2 = postService .getPostsPage (1 , pageSize );
64+
65+ assertThat (page1 .getNumberOfElements (), is (equalTo (10 )));
66+ assertThat (page2 .getNumberOfElements (), is (equalTo (5 )));
67+
68+ verify (postRepository , times (2 )).findAll (Matchers .any (PageRequest .class ));
69+ }
70+
71+ @ Test
72+ public void shouldGetEmptyPostPage () {
73+ when (postRepository .findAll (Matchers .any (PageRequest .class )))
74+ .thenReturn (new PageImpl <>(new ArrayList <>()));
75+
76+ Page <Post > page = postService .getPostsPage (99 , 10 );
77+
78+ assertThat (page .getNumberOfElements (), is (equalTo (0 )));
79+
80+ verify (postRepository , times (1 )).findAll (Matchers .any (PageRequest .class ));
81+ }
82+
83+ @ Test
84+ public void shouldGetPost () {
85+ final long postId = 1L ;
86+
87+ Post post = new Post ();
88+ post .setId (postId );
89+
90+ when (postRepository .findOne (postId ))
91+ .thenReturn (post );
92+
93+ Post retrievedPost = postService .getPost (postId );
94+
95+ assertThat (retrievedPost , is (equalTo (post )));
96+
97+ verify (postRepository , times (1 )).findOne (postId );
98+ }
99+
100+ @ Test
101+ public void shouldReturnNullWhenPostNotExists () {
102+ final long postId = 1L ;
103+
104+ Post retrievedPost = postService .getPost (postId );
105+
106+ assertThat (retrievedPost , is (equalTo (null )));
107+
108+ verify (postRepository , times (1 )).findOne (postId );
109+ }
110+
111+ @ Test
112+ public void shouldAddNewPostAndSetTags () {
113+ Post post = new Post ();
114+ post .setFullPostText ("short text " + Post .shortPartSeparator () + " full text" );
115+
116+ String tags = "c++, hello world" ;
117+
118+ postService .saveNewPost (post , tags );
119+
120+ assertThat (post .getShortTextPart (), containsString ("short text" ));
121+ assertThat (post .getShortTextPart (), not (containsString ("full text" )));
122+
123+ assertThat (post .getFullPostText (), allOf (containsString ("full text" ), containsString ("full text" ), containsString (Post .shortPartSeparator ())));
124+
125+ assertThat (post .getTags ().size (), is (equalTo (2 )));
126+
127+ verify (tagRepository , times (2 )).findByNameIgnoreCase (Matchers .anyString ());
128+ verify (tagRepository , times (2 )).save (Matchers .any (Tag .class ));
129+
130+ verify (postRepository , times (1 )).save (Matchers .any (Post .class ));
131+ }
132+ }
0 commit comments