forked from solid-connection/solid-connect-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostCommandServiceTest.java
More file actions
349 lines (303 loc) · 13.8 KB
/
PostCommandServiceTest.java
File metadata and controls
349 lines (303 loc) · 13.8 KB
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
package com.example.solidconnection.community.post.service;
import static com.example.solidconnection.common.exception.ErrorCode.CAN_NOT_DELETE_OR_UPDATE_QUESTION;
import static com.example.solidconnection.common.exception.ErrorCode.CAN_NOT_UPLOAD_MORE_THAN_FIVE_IMAGES;
import static com.example.solidconnection.common.exception.ErrorCode.INVALID_POST_ACCESS;
import static com.example.solidconnection.common.exception.ErrorCode.INVALID_POST_CATEGORY;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.mockito.BDDMockito.any;
import static org.mockito.BDDMockito.eq;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.then;
import com.example.solidconnection.common.exception.CustomException;
import com.example.solidconnection.community.board.fixture.BoardFixture;
import com.example.solidconnection.community.post.domain.Post;
import com.example.solidconnection.community.post.domain.PostCategory;
import com.example.solidconnection.community.post.domain.PostImage;
import com.example.solidconnection.community.post.dto.PostCreateRequest;
import com.example.solidconnection.community.post.dto.PostCreateResponse;
import com.example.solidconnection.community.post.dto.PostDeleteResponse;
import com.example.solidconnection.community.post.dto.PostUpdateRequest;
import com.example.solidconnection.community.post.dto.PostUpdateResponse;
import com.example.solidconnection.community.post.fixture.PostFixture;
import com.example.solidconnection.community.post.fixture.PostImageFixture;
import com.example.solidconnection.community.post.repository.PostRepository;
import com.example.solidconnection.redis.RedisService;
import com.example.solidconnection.s3.domain.UploadPath;
import com.example.solidconnection.s3.dto.UploadedFileUrlResponse;
import com.example.solidconnection.s3.service.S3Service;
import com.example.solidconnection.siteuser.domain.SiteUser;
import com.example.solidconnection.siteuser.fixture.SiteUserFixture;
import com.example.solidconnection.support.TestContainerSpringBootTest;
import jakarta.transaction.Transactional;
import java.util.List;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;
@TestContainerSpringBootTest
@DisplayName("게시글 생성/수정/삭제 서비스 테스트")
class PostCommandServiceTest {
@Autowired
private PostCommandService postCommandService;
@MockBean
private S3Service s3Service;
@Autowired
private RedisService redisService;
@Autowired
private PostRedisManager postRedisManager;
@Autowired
private PostRepository postRepository;
@Autowired
private SiteUserFixture siteUserFixture;
@Autowired
private BoardFixture boardFixture;
@Autowired
private PostFixture postFixture;
@Autowired
private PostImageFixture postImageFixture;
private SiteUser user1;
private Post post;
private Post questionPost;
@BeforeEach
void setUp() {
user1 = siteUserFixture.사용자(1, "test1");
post = postFixture.게시글(
"제목",
"내용",
false,
PostCategory.자유,
boardFixture.자유게시판(),
user1
);
questionPost = postFixture.게시글(
"제목",
"내용",
true,
PostCategory.질문,
boardFixture.자유게시판(),
user1
);
}
@Nested
class 게시글_생성_테스트 {
@Test
@Transactional
void 게시글을_성공적으로_생성한다() {
// given
PostCreateRequest request = createPostCreateRequest(PostCategory.자유.name());
List<MultipartFile> imageFiles = List.of(createImageFile());
String expectedImageUrl = "test-image-url";
given(s3Service.uploadFiles(any(), eq(UploadPath.COMMUNITY)))
.willReturn(List.of(new UploadedFileUrlResponse(expectedImageUrl)));
// when
PostCreateResponse response = postCommandService.createPost(user1.getId(), request, imageFiles);
// then
Post savedPost = postRepository.findById(response.id()).orElseThrow();
assertAll(
() -> assertThat(response.id()).isEqualTo(savedPost.getId()),
() -> assertThat(savedPost.getPostImageList()).hasSize(imageFiles.size()),
() -> assertThat(savedPost.getPostImageList())
.extracting(PostImage::getUrl)
.containsExactly(expectedImageUrl)
);
}
@Test
void 전체_카테고리로_생성하면_예외가_발생한다() {
// given
PostCreateRequest request = createPostCreateRequest(PostCategory.전체.name());
List<MultipartFile> imageFiles = List.of();
// when & then
assertThatThrownBy(() ->
postCommandService.createPost(user1.getId(), request, imageFiles))
.isInstanceOf(CustomException.class)
.hasMessage(INVALID_POST_CATEGORY.getMessage());
}
@Test
void 존재하지_않는_카테고리로_생성하면_예외가_발생한다() {
// given
PostCreateRequest request = createPostCreateRequest("INVALID_CATEGORY");
List<MultipartFile> imageFiles = List.of();
// when & then
assertThatThrownBy(() ->
postCommandService.createPost(user1.getId(), request, imageFiles))
.isInstanceOf(CustomException.class)
.hasMessage(INVALID_POST_CATEGORY.getMessage());
}
@Test
void 이미지를_5개_초과하여_업로드하면_예외가_발생한다() {
// given
PostCreateRequest request = createPostCreateRequest(PostCategory.자유.name());
List<MultipartFile> imageFiles = createSixImageFiles();
// when & then
assertThatThrownBy(() ->
postCommandService.createPost(user1.getId(), request, imageFiles))
.isInstanceOf(CustomException.class)
.hasMessage(CAN_NOT_UPLOAD_MORE_THAN_FIVE_IMAGES.getMessage());
}
}
@Nested
class 게시글_수정_테스트 {
@Test
@Transactional
void 게시글을_성공적으로_수정한다() {
// given
String originImageUrl = "origin-image-url";
postImageFixture.게시글_이미지(originImageUrl, post);
String expectedImageUrl = "update-image-url";
PostUpdateRequest request = createPostUpdateRequest();
List<MultipartFile> imageFiles = List.of(createImageFile());
given(s3Service.uploadFiles(any(), eq(UploadPath.COMMUNITY)))
.willReturn(List.of(new UploadedFileUrlResponse(expectedImageUrl)));
// when
PostUpdateResponse response = postCommandService.updatePost(
user1.getId(),
post.getId(),
request,
imageFiles
);
// then
Post updatedPost = postRepository.findById(response.id()).orElseThrow();
assertAll(
() -> assertThat(response.id()).isEqualTo(updatedPost.getId()),
() -> assertThat(updatedPost.getPostImageList()).hasSize(imageFiles.size()),
() -> assertThat(updatedPost.getPostImageList())
.extracting(PostImage::getUrl)
.containsExactly(expectedImageUrl)
);
then(s3Service).should().deletePostImage(originImageUrl);
}
@Test
void 다른_사용자의_게시글을_수정하면_예외가_발생한다() {
// given
SiteUser user2 = siteUserFixture.사용자(2, "test2");
PostUpdateRequest request = createPostUpdateRequest();
List<MultipartFile> imageFiles = List.of();
// when & then
assertThatThrownBy(() ->
postCommandService.updatePost(
user2.getId(),
post.getId(),
request,
imageFiles
))
.isInstanceOf(CustomException.class)
.hasMessage(INVALID_POST_ACCESS.getMessage());
}
@Test
void 질문_게시글을_수정하면_예외가_발생한다() {
// given
PostUpdateRequest request = createPostUpdateRequest();
List<MultipartFile> imageFiles = List.of();
// when & then
assertThatThrownBy(() ->
postCommandService.updatePost(
user1.getId(),
questionPost.getId(),
request,
imageFiles
))
.isInstanceOf(CustomException.class)
.hasMessage(CAN_NOT_DELETE_OR_UPDATE_QUESTION.getMessage());
}
@Test
void 이미지를_5개_초과하여_수정하면_예외가_발생한다() {
// given
PostUpdateRequest request = createPostUpdateRequest();
List<MultipartFile> imageFiles = createSixImageFiles();
// when & then
assertThatThrownBy(() ->
postCommandService.updatePost(
user1.getId(),
post.getId(),
request,
imageFiles
))
.isInstanceOf(CustomException.class)
.hasMessage(CAN_NOT_UPLOAD_MORE_THAN_FIVE_IMAGES.getMessage());
}
}
@Nested
class 게시글_삭제_테스트 {
@Test
void 게시글을_성공적으로_삭제한다() {
// given
String originImageUrl = "origin-image-url";
postImageFixture.게시글_이미지(originImageUrl, post);
String viewCountKey = postRedisManager.getPostViewCountRedisKey(post.getId());
redisService.increaseViewCount(viewCountKey);
// when
PostDeleteResponse response = postCommandService.deletePostById(user1.getId(), post.getId());
// then
assertAll(
() -> assertThat(response.id()).isEqualTo(post.getId()),
() -> assertThat(postRepository.findById(post.getId())).isEmpty(),
() -> assertThat(redisService.isKeyExists(viewCountKey)).isFalse()
);
then(s3Service).should().deletePostImage(originImageUrl);
}
@Test
void 다른_사용자의_게시글을_삭제하면_예외가_발생한다() {
// given
SiteUser user2 = siteUserFixture.사용자(2, "test2");
// when & then
assertThatThrownBy(() ->
postCommandService.deletePostById(
user2.getId(),
post.getId()
))
.isInstanceOf(CustomException.class)
.hasMessage(INVALID_POST_ACCESS.getMessage());
}
@Test
void 질문_게시글을_삭제하면_예외가_발생한다() {
// when & then
assertThatThrownBy(() ->
postCommandService.deletePostById(
user1.getId(),
questionPost.getId()
))
.isInstanceOf(CustomException.class)
.hasMessage(CAN_NOT_DELETE_OR_UPDATE_QUESTION.getMessage());
}
}
private PostCreateRequest createPostCreateRequest(String category) {
return new PostCreateRequest(
boardFixture.자유게시판().getCode(),
category,
"테스트 제목",
"테스트 내용",
false
);
}
private MockMultipartFile createImageFile() {
return new MockMultipartFile(
"image",
"test.jpg",
"image/jpeg",
"test image content".getBytes()
);
}
private List<MultipartFile> createSixImageFiles() {
return List.of(
createImageFile(),
createImageFile(),
createImageFile(),
createImageFile(),
createImageFile(),
createImageFile()
);
}
private PostUpdateRequest createPostUpdateRequest() {
return new PostUpdateRequest(
PostCategory.자유.name(),
"수정된 제목",
"수정된 내용"
);
}
}