|
1
|
|
|
package com.dawn.jat.illuminati.post.controller; |
|
2
|
|
|
|
|
3
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals; |
|
4
|
|
|
import static org.junit.jupiter.api.Assertions.assertNotEquals; |
|
5
|
|
|
import static org.junit.jupiter.api.Assertions.assertNotNull; |
|
6
|
|
|
import static org.junit.jupiter.api.Assertions.assertThrows; |
|
7
|
|
|
|
|
8
|
|
|
import com.dawn.jat.illuminati.post.dto.PostDto; |
|
9
|
|
|
import com.dawn.jat.illuminati.post.entity.PostEntity; |
|
10
|
|
|
import com.dawn.jat.illuminati.post.exception.PostCannotBeSavedException; |
|
11
|
|
|
import com.dawn.jat.illuminati.post.exception.PostNotFoundException; |
|
12
|
|
|
import com.dawn.jat.illuminati.post.exception.PostSummaryNotFoundException; |
|
13
|
|
|
import com.dawn.jat.illuminati.post.service.PostService; |
|
14
|
|
|
|
|
15
|
|
|
import java.util.ArrayList; |
|
16
|
|
|
import java.util.Arrays; |
|
17
|
|
|
import java.util.HashMap; |
|
18
|
|
|
import java.util.List; |
|
19
|
|
|
import java.util.Optional; |
|
20
|
|
|
|
|
21
|
|
|
import org.junit.jupiter.api.Assertions; |
|
22
|
|
|
import org.junit.jupiter.api.BeforeAll; |
|
23
|
|
|
import org.junit.jupiter.api.Test; |
|
24
|
|
|
import org.junit.jupiter.api.extension.ExtendWith; |
|
25
|
|
|
import org.mockito.InjectMocks; |
|
26
|
|
|
import org.mockito.Mock; |
|
27
|
|
|
import org.mockito.Mockito; |
|
28
|
|
|
import org.mockito.junit.jupiter.MockitoExtension; |
|
29
|
|
|
import org.springframework.boot.test.context.SpringBootTest; |
|
30
|
|
|
import org.springframework.http.HttpStatus; |
|
31
|
|
|
import org.springframework.http.ResponseEntity; |
|
32
|
|
|
|
|
33
|
|
|
@ExtendWith(MockitoExtension.class) |
|
34
|
|
|
@SpringBootTest |
|
35
|
|
|
public class PostControllerTest { |
|
36
|
|
|
private static PostEntity postEntity1; |
|
37
|
|
|
private static PostEntity postEntity2; |
|
38
|
|
|
private static PostDto postDto; |
|
39
|
|
|
|
|
40
|
|
|
@Mock |
|
41
|
|
|
private PostService postService; |
|
42
|
|
|
|
|
43
|
|
|
@InjectMocks |
|
44
|
|
|
private PostController postController; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Initializing postEntity object. |
|
48
|
|
|
*/ |
|
49
|
|
|
@BeforeAll |
|
50
|
|
|
public static void init() { |
|
51
|
|
|
postEntity1 = new PostEntity("how-to-apply-agile-methodology", |
|
52
|
|
|
"How to apply Agile methodology", |
|
53
|
|
|
"Guide", |
|
54
|
|
|
"01/01/2020", |
|
55
|
|
|
new ArrayList<>(Arrays.asList("Agile")), |
|
56
|
|
|
"Phat Ho"); |
|
57
|
|
|
|
|
58
|
|
|
postEntity2 = new PostEntity("getting-started-with-reactjs", |
|
59
|
|
|
"Getting started with ReactJS", |
|
60
|
|
|
"Guide", |
|
61
|
|
|
"02/01/2020", |
|
62
|
|
|
new ArrayList<>(Arrays.asList("Web Development", "Frontend")), |
|
63
|
|
|
"Phat Ho"); |
|
64
|
|
|
|
|
65
|
|
|
HashMap tags = new HashMap(); |
|
66
|
|
|
tags.put("Agile", Boolean.TRUE); |
|
67
|
|
|
postDto = new PostDto("5e80afe11de7a40da7f97052", |
|
68
|
|
|
"how-to-apply-agile-methodology", |
|
69
|
|
|
"How to apply Agile methodology", |
|
70
|
|
|
"Guide", |
|
71
|
|
|
"01/01/2020", |
|
72
|
|
|
"Phat Ho", |
|
73
|
|
|
null, |
|
74
|
|
|
tags); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
@Test |
|
78
|
|
|
public void findBySlug_whenPostIdIsAvail_thenRetrievedPostIsCorrect() { |
|
79
|
|
|
String mockSlug = postEntity1.getSlug(); |
|
80
|
|
|
Optional<PostEntity> mockPostEntities = Optional.of(postEntity1); |
|
81
|
|
|
|
|
82
|
|
|
Mockito.when(postService.findBySlug(mockSlug)).thenReturn(mockPostEntities); |
|
83
|
|
|
|
|
84
|
|
|
ResponseEntity<Object> responseEntity = postController.getPostBySlug(mockSlug); |
|
85
|
|
|
assertNotNull(responseEntity); |
|
86
|
|
|
assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); |
|
87
|
|
|
assertNotEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode()); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
@Test |
|
91
|
|
|
void findBySlug_whenPostIdIsUnAvail_thenRetrievedPostIsIncorrect() { |
|
92
|
|
|
Optional<PostEntity> emptyEntities = Optional.empty(); |
|
93
|
|
|
Mockito.when(postService.findBySlug("")) |
|
94
|
|
|
.thenReturn(emptyEntities); |
|
95
|
|
|
|
|
96
|
|
|
assertThrows(PostNotFoundException.class, () -> { |
|
97
|
|
|
postController.getPostBySlug(""); |
|
98
|
|
|
}); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
@Test |
|
102
|
|
|
public void getPostSummary_whenPostSummaryIdIsAvail_thenRetrievedPostSummaryIsCorrect() { |
|
103
|
|
|
List mockPostEntities = Arrays.asList(postEntity1, postEntity2); |
|
104
|
|
|
Mockito.when(postService.findPostSummary()).thenReturn(mockPostEntities); |
|
105
|
|
|
|
|
106
|
|
|
ResponseEntity<Object> responseEntity = postController.getAllPostSummary(); |
|
107
|
|
|
assertNotNull(responseEntity); |
|
108
|
|
|
assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); |
|
109
|
|
|
assertNotEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode()); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
@Test |
|
113
|
|
|
public void getPostSummary_whenPostSummaryIdIsUnAvail_thenRetrievedPostSummaryIsIncorrect() { |
|
114
|
|
|
Mockito.when(postService.findPostSummary()) |
|
115
|
|
|
.thenReturn(new ArrayList<>()); |
|
116
|
|
|
|
|
117
|
|
|
assertThrows(PostSummaryNotFoundException.class, () -> { |
|
118
|
|
|
postController.getAllPostSummary(); |
|
119
|
|
|
}); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
@Test |
|
123
|
|
|
public void create_whenPostSuccessfullyCreate_sendSuccessResponseWithPost() { |
|
124
|
|
|
Mockito.when(postService.create(postDto)) |
|
125
|
|
|
.thenReturn(postEntity1); |
|
126
|
|
|
|
|
127
|
|
|
ResponseEntity<Object> responseEntity = postController.createPost(postDto); |
|
128
|
|
|
assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
@Test |
|
132
|
|
|
public void savePost_whenPostSuccessfullySaved_sendSuccessResponseWithPost() { |
|
133
|
|
|
Mockito.when(postService.save(postDto)) |
|
134
|
|
|
.thenReturn(postEntity1); |
|
135
|
|
|
|
|
136
|
|
|
ResponseEntity<Object> responseEntity = postController.savePost(postDto); |
|
137
|
|
|
assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
@Test |
|
141
|
|
|
public void savePost_whenPostFailedToSave_throwPostCannotBeSavedException() { |
|
142
|
|
|
Mockito.when(postService.save(postDto)) |
|
143
|
|
|
.thenReturn(null); |
|
144
|
|
|
|
|
145
|
|
|
Assertions.assertThrows(PostCannotBeSavedException.class, () -> { |
|
146
|
|
|
postController.savePost(postDto); |
|
147
|
|
|
}); |
|
148
|
|
|
} |
|
149
|
|
|
} |