-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathPostControllerTest.java
413 lines (343 loc) · 14.6 KB
/
PostControllerTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
package com.example.demo;
import com.example.demo.domain.Comment;
import com.example.demo.domain.Post;
import com.example.demo.domain.PostId;
import com.example.demo.repository.CommentRepository;
import com.example.demo.repository.PostRepository;
import com.example.demo.web.CommentForm;
import com.example.demo.web.CreatePostCommand;
import com.example.demo.web.PostController;
import com.example.demo.web.UpdateStatusRequest;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.reactive.ReactiveSecurityAutoConfiguration;
import org.springframework.boot.autoconfigure.security.reactive.ReactiveUserDetailsServiceAutoConfiguration;
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.web.reactive.function.BodyInserters;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.IntStream;
import static java.util.stream.Collectors.toList;
import static org.hamcrest.CoreMatchers.containsString;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.isA;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.verifyNoMoreInteractions;
@WebFluxTest(
controllers = PostController.class,
excludeAutoConfiguration = {
ReactiveUserDetailsServiceAutoConfiguration.class,
ReactiveSecurityAutoConfiguration.class
}
)
@Slf4j
@DisplayName("API endpoints /posts")
@TestInstance(TestInstance.Lifecycle.PER_METHOD)
class PostControllerTest {
@Autowired
private WebTestClient client;
@MockBean
private PostRepository posts;
@MockBean
private CommentRepository comments;
@BeforeAll
public static void beforeAll() {
log.debug("before all...");
}
@AfterAll
public static void afterAll() {
log.debug("after all...");
}
@BeforeEach
void beforeEach() {
log.debug("before each...");
}
@AfterEach
void afterEach() {
log.debug("after each...");
reset(comments);
reset(posts);
}
@Nested
@DisplayName("/posts GET")
class GettingAllPosts {
@Test
@DisplayName("should return 200 when getting posts with keyword and pagination")
void shouldBeOkWhenGettingPostsWithKeywordAndPagination() {
List<Post> data = IntStream.range(1, 6)// 5 posts will be created.
.mapToObj(n -> Post.builder().id("" + n).title("my " + n + " blog post")
.content("content of my " + n + " blog post").status(Post.Status.PUBLISHED)
.createdDate(LocalDateTime.now()).build())
.toList();
given(posts.findByTitleContains(anyString(), isA(Pageable.class))).willReturn(Flux.fromIterable(data));
given(posts.countByTitleContains(anyString())).willReturn(Mono.just(30L));
client.get().uri(uriBuilder -> uriBuilder.path("/posts").queryParam("q", "5").build())
.exchange()
.expectStatus().isOk()
.expectBody()
.jsonPath("$.data.size()").isEqualTo(5)
.jsonPath("$.count").isEqualTo(30L);
verify(posts, times(1)).findByTitleContains(anyString(), any(Pageable.class));
verify(posts, times(1)).countByTitleContains(anyString());
verifyNoMoreInteractions(posts);
}
}
@Nested
@DisplayName("/posts/:id GET")
class GettingPostById {
@Test
@DisplayName("should return 200 when getting post by id")
void shouldBeOkWhenGettingPostById() {
given(posts.findById("1")).willReturn(
Mono.just(Post.builder().id("1").title("my first post").content("content of my first post").build()));
client.get()
.uri("/posts/1")
.exchange()
.expectStatus().isOk()
.expectBody()
.jsonPath("$.title").isEqualTo("my first post")
.jsonPath("$.id").isEqualTo("1")
.jsonPath("$.content").isEqualTo("content of my first post");
verify(posts, times(1)).findById(anyString());
verifyNoMoreInteractions(posts);
}
@Test
@DisplayName("should return 404 when getting post by a none existing id")
void shouldReturn404WhenGettingPostByNonExistedId() {
given(posts.findById("1")).willReturn(Mono.empty());
client.get().uri("/posts/1")
.exchange()
.expectStatus()
.isNotFound();
verify(posts, times(1)).findById(anyString());
verifyNoMoreInteractions(posts);
}
}
@Nested
@DisplayName("/posts POST")
class CreatingPost {
@Test
@DisplayName("should return 400 when creating post with invalid body")
void shouldReturn400WhenCreatingPostWithInvalidBody() {
CreatePostCommand formData = new CreatePostCommand(null, null);
client.post()
.uri("/posts")
.body(BodyInserters.fromValue(formData))
.exchange().expectStatus()
.isEqualTo(HttpStatus.BAD_REQUEST);
verifyNoInteractions(posts);
}
@Test
@DisplayName("should return 201 when creating post")
void shouldReturn201WhenCreatingPost() {
CreatePostCommand formData = new CreatePostCommand("my first post", "content of my first post");
given(posts.save(any(Post.class))).willReturn(
Mono.just(
Post.builder()
.id("1")
.title("my first post")
.content("content of my first post")
.createdDate(LocalDateTime.now())
.build()
)
);
client.post()
.uri("/posts")
.body(BodyInserters.fromValue(formData))
.exchange().expectHeader()
.value("Location", containsString("/posts/1"))
.expectStatus().isCreated()
.expectBody().isEmpty();
verify(posts, times(1)).save(any(Post.class));
verifyNoMoreInteractions(posts);
}
}
@Nested
@DisplayName("/posts/:id PUT")
class UpdatingPost {
@Test
@DisplayName("should return 204 when updating post")
void shouldBeOkWhenUpdatingPost() {
Post post = Post.builder()
.id("1")
.title("my first post")
.content("content of my first post")
.createdDate(LocalDateTime.now())
.build();
given(posts.findById("1")).willReturn(Mono.just(post));
post.setTitle("updated title");
post.setContent("updated content");
given(posts.save(post)).willReturn(
Mono.just(
Post.builder()
.id("1")
.title("updated title")
.content("updated content")
.createdDate(LocalDateTime.now())
.build()
)
);
client.put()
.uri("/posts/1")
.body(BodyInserters.fromValue(post))
.exchange()
.expectStatus()
.isNoContent()
.expectBody()
.isEmpty();
verify(posts, times(1)).findById(anyString());
verify(posts, times(1)).save(any(Post.class));
verifyNoMoreInteractions(posts);
}
}
@Nested
@DisplayName("/posts/:id/status PUT")
class UpdatingStatusOfPost {
@Test
@DisplayName("should return 204 when updating the status of a post")
void shouldBeOkWhenUpdatingPostStatus() {
Post post = Post.builder()
.id("1")
.title("my first post")
.content("content of my first post")
.createdDate(LocalDateTime.now())
.build();
given(posts.findById("1")).willReturn(Mono.just(post));
post.setStatus(Post.Status.PUBLISHED);
given(posts.save(post))
.willReturn(
Mono.just(
Post.builder()
.id("1")
.title("updated title")
.content("updated content")
.createdDate(LocalDateTime.now())
.build()
)
);
client.put()
.uri("/posts/1/status")
.body(BodyInserters.fromValue(new UpdateStatusRequest("PUBLISHED")))
.exchange()
.expectStatus()
.isNoContent();
verify(posts, times(1)).findById(anyString());
verify(posts, times(1)).save(any(Post.class));
verifyNoMoreInteractions(posts);
}
}
@Nested
@DisplayName("/posts/:id DELETE")
class DeletingPost {
@Test
@DisplayName("should return 204 when deleting post")
void shouldReturn204WhenDeletingPost() {
Post post = Post.builder()
.id("1")
.title("my first post")
.content("content of my first post")
.createdDate(LocalDateTime.now())
.build();
given(posts.findById("1")).willReturn(Mono.just(post));
Mono<Void> mono = Mono.empty();
given(posts.delete(post)).willReturn(mono);
client.delete().uri("/posts/1").exchange().expectStatus().isNoContent();
verify(posts, times(1)).findById(anyString());
verify(posts, times(1)).delete(any(Post.class));
verifyNoMoreInteractions(posts);
}
}
@Nested
@DisplayName("/posts/:id/comments GET")
class GettingCommentsByPostId {
@Test
@DisplayName("should return 200 when get comments of a post")
void shouldBeOkWhenGettingCommentsByPostId() {
given(comments.findByPost(any(PostId.class)))
.willReturn(
Flux.just(
Comment.builder()
.id("comment-id-1")
.post(new PostId("1"))
.content("comment of my first post")
.build()
)
);
client.get().uri("/posts/1/comments")
.exchange()
.expectStatus().isOk()
.expectBody()
.consumeWith(result -> log.debug("RESPONSE::" + new String(result.getResponseBody())))
.jsonPath("$.[0].id").isEqualTo("comment-id-1")
.jsonPath("$.[0].content").isEqualTo("comment of my first post");
verify(comments, times(1)).findByPost(any(PostId.class));
verifyNoMoreInteractions(comments);
}
}
@Nested
@DisplayName("/posts/:id/comments/count GET")
class CountingCommentsByPostId {
@Test
@DisplayName("should return 200 when get the count of comments of a post")
void shouldBeOkWhenGettingCommentsCountByPostId() {
given(comments.countByPost(any(PostId.class))).willReturn(Mono.just(1L));
client.get().uri("/posts/1/comments/count")
.exchange()
.expectStatus().isOk()
.expectBody()
.consumeWith(result -> log.debug("RESPONSE::" + new String(result.getResponseBody())))
.jsonPath("$.count").isEqualTo(1L);
verify(comments, times(1)).countByPost(any(PostId.class));
verifyNoMoreInteractions(comments);
}
}
@Nested
@DisplayName("/posts/:id/comments POST")
class CreatingCommentsOfPost {
@Test
@DisplayName("should return 201 when creating comment of a post")
void shouldBeOkWhenCreatingCommentOfPost() {
given(comments.save(any(Comment.class)))
.willReturn(
Mono.just(
Comment.builder()
.id("comment-id-1")
.post(PostId.builder().id("1").build())
.content("content of my first post")
.createdDate(LocalDateTime.now())
.build()
)
);
CommentForm form = new CommentForm("comment of my first post");
client.post()
.uri("/posts/1/comments")
.body(BodyInserters.fromValue(form))
.exchange()
.expectStatus().isCreated()
.expectBody().isEmpty();
verify(comments, times(1)).save(any(Comment.class));
verifyNoMoreInteractions(comments);
}
}
}