66import com .back .domain .board .post .enums .CategoryType ;
77import com .back .domain .board .post .repository .PostCategoryRepository ;
88import com .back .domain .board .post .repository .PostRepository ;
9+ import com .back .domain .file .entity .FileAttachment ;
10+ import com .back .domain .file .repository .FileAttachmentRepository ;
911import com .back .domain .user .common .entity .User ;
1012import com .back .domain .user .common .entity .UserProfile ;
1113import com .back .domain .user .common .enums .UserStatus ;
1820import org .springframework .boot .test .autoconfigure .web .servlet .AutoConfigureMockMvc ;
1921import org .springframework .boot .test .context .SpringBootTest ;
2022import org .springframework .http .MediaType ;
23+ import org .springframework .mock .web .MockMultipartFile ;
2124import org .springframework .security .crypto .password .PasswordEncoder ;
2225import org .springframework .test .context .ActiveProfiles ;
2326import org .springframework .test .web .servlet .MockMvc ;
@@ -49,6 +52,9 @@ class PostControllerTest {
4952 @ Autowired
5053 private PostCategoryRepository postCategoryRepository ;
5154
55+ @ Autowired
56+ private FileAttachmentRepository fileAttachmentRepository ;
57+
5258 @ Autowired
5359 private TestJwtTokenProvider testJwtTokenProvider ;
5460
@@ -82,7 +88,19 @@ void createPost_success() throws Exception {
8288 PostCategory c2 = new PostCategory ("자유게시판" , CategoryType .SUBJECT );
8389 postCategoryRepository .save (c2 );
8490
85- PostRequest request = new PostRequest ("첫 번째 게시글" , "안녕하세요, 첫 글입니다!" , null , List .of (c1 .getId (), c2 .getId ()));
91+ // 이미지 등록
92+ MockMultipartFile file = new MockMultipartFile ("file" , "thumb.png" , "image/png" , "dummy" .getBytes ());
93+ FileAttachment attachment = new FileAttachment ("stored_thumb.png" , file , user , "https://cdn.example.com/thumb.png" );
94+ fileAttachmentRepository .save (attachment );
95+
96+ // 요청 구성
97+ PostRequest request = new PostRequest (
98+ "첫 번째 게시글" ,
99+ "안녕하세요, 첫 글입니다!" ,
100+ null ,
101+ List .of (c1 .getId (), c2 .getId ()),
102+ List .of (attachment .getId ())
103+ );
86104
87105 // when
88106 ResultActions resultActions = mvc .perform (
@@ -99,7 +117,9 @@ void createPost_success() throws Exception {
99117 .andExpect (jsonPath ("$.code" ).value ("SUCCESS_200" ))
100118 .andExpect (jsonPath ("$.data.title" ).value ("첫 번째 게시글" ))
101119 .andExpect (jsonPath ("$.data.author.nickname" ).value ("홍길동" ))
102- .andExpect (jsonPath ("$.data.categories.length()" ).value (2 ));
120+ .andExpect (jsonPath ("$.data.categories.length()" ).value (2 ))
121+ .andExpect (jsonPath ("$.data.images.length()" ).value (1 ))
122+ .andExpect (jsonPath ("$.data.images[0].url" ).value ("https://cdn.example.com/thumb.png" ));
103123 }
104124
105125 @ Test
@@ -108,7 +128,7 @@ void createPost_userNotFound() throws Exception {
108128 // given: 토큰만 발급(실제 DB엔 없음)
109129 String fakeToken = testJwtTokenProvider .createAccessToken (999L , "ghost" , "USER" );
110130
111- PostRequest request = new PostRequest ("제목" , "내용" , null , null );
131+ PostRequest request = new PostRequest ("제목" , "내용" , null , List . of (), List . of () );
112132
113133 // when & then
114134 mvc .perform (post ("/api/posts" )
@@ -133,7 +153,7 @@ void createPost_categoryNotFound() throws Exception {
133153 String accessToken = generateAccessToken (user );
134154
135155 // 존재하지 않는 카테고리 ID
136- PostRequest request = new PostRequest ("제목" , "내용" , null , List .of (999L ));
156+ PostRequest request = new PostRequest ("제목" , "내용" , null , List .of (999L ), List . of () );
137157
138158 // when & then
139159 mvc .perform (post ("/api/posts" )
@@ -146,6 +166,35 @@ void createPost_categoryNotFound() throws Exception {
146166 .andExpect (jsonPath ("$.message" ).value ("존재하지 않는 카테고리입니다." ));
147167 }
148168
169+ @ Test
170+ @ DisplayName ("게시글 생성 실패 - 존재하지 않는 이미지 → 404 Not Found" )
171+ void createPost_fail_imageNotFound () throws Exception {
172+ // given
173+ User user =
User .
createUser (
"writer" ,
"[email protected] " ,
passwordEncoder .
encode (
"P@ssw0rd!" ));
174+ user .setUserProfile (new UserProfile (user , "홍길동" , null , "소개글" , LocalDate .of (2000 , 1 , 1 ), 1000 ));
175+ user .setUserStatus (UserStatus .ACTIVE );
176+ userRepository .save (user );
177+
178+ String accessToken = generateAccessToken (user );
179+
180+ PostCategory category = new PostCategory ("공지사항" , CategoryType .SUBJECT );
181+ postCategoryRepository .save (category );
182+
183+ // 존재하지 않는 이미지 ID
184+ PostRequest request = new PostRequest ("이미지 테스트" , "없는 이미지입니다" , null ,
185+ List .of (category .getId ()), List .of (999L ));
186+
187+ // when & then
188+ mvc .perform (post ("/api/posts" )
189+ .header ("Authorization" , "Bearer " + accessToken )
190+ .contentType (MediaType .APPLICATION_JSON )
191+ .content (objectMapper .writeValueAsString (request )))
192+ .andDo (print ())
193+ .andExpect (status ().isNotFound ())
194+ .andExpect (jsonPath ("$.code" ).value ("FILE_004" ))
195+ .andExpect (jsonPath ("$.message" ).value ("파일 정보를 찾을 수 없습니다." ));
196+ }
197+
149198 @ Test
150199 @ DisplayName ("게시글 생성 실패 - 잘못된 요청(필드 누락) → 400 Bad Request" )
151200 void createPost_badRequest () throws Exception {
@@ -179,7 +228,7 @@ void createPost_badRequest() throws Exception {
179228 @ DisplayName ("게시글 생성 실패 - 토큰 없음 → 401 Unauthorized" )
180229 void createPost_noToken () throws Exception {
181230 // given
182- PostRequest request = new PostRequest ("제목" , "내용" , null , null );
231+ PostRequest request = new PostRequest ("제목" , "내용" , null , List . of (), List . of () );
183232
184233 // when & then
185234 mvc .perform (post ("/api/posts" )
@@ -292,7 +341,17 @@ void updatePost_success() throws Exception {
292341 PostCategory c2 = new PostCategory ("자유게시판" , CategoryType .SUBJECT );
293342 postCategoryRepository .save (c2 );
294343
295- PostRequest request = new PostRequest ("수정된 게시글" , "안녕하세요, 수정했습니다!" , null , List .of (c1 .getId (), c2 .getId ()));
344+ MockMultipartFile file = new MockMultipartFile ("file" , "thumb.png" , "image/png" , "dummy" .getBytes ());
345+ FileAttachment attachment = new FileAttachment ("stored_thumb.png" , file , user , "https://cdn.example.com/thumb.png" );
346+ fileAttachmentRepository .save (attachment );
347+
348+ PostRequest request = new PostRequest (
349+ "수정된 게시글" ,
350+ "안녕하세요, 수정했습니다!" ,
351+ null ,
352+ List .of (c1 .getId (), c2 .getId ()),
353+ List .of (attachment .getId ())
354+ );
296355
297356 // when & then
298357 mvc .perform (put ("/api/posts/{postId}" , post .getId ())
@@ -304,7 +363,9 @@ void updatePost_success() throws Exception {
304363 .andExpect (jsonPath ("$.success" ).value (true ))
305364 .andExpect (jsonPath ("$.code" ).value ("SUCCESS_200" ))
306365 .andExpect (jsonPath ("$.data.title" ).value ("수정된 게시글" ))
307- .andExpect (jsonPath ("$.data.categories.length()" ).value (2 ));
366+ .andExpect (jsonPath ("$.data.categories.length()" ).value (2 ))
367+ .andExpect (jsonPath ("$.data.images.length()" ).value (1 ))
368+ .andExpect (jsonPath ("$.data.images[0].url" ).value ("https://cdn.example.com/thumb.png" ));
308369 }
309370
310371 @ Test
@@ -318,7 +379,7 @@ void updatePost_fail_notFound() throws Exception {
318379
319380 String accessToken = generateAccessToken (user );
320381
321- PostRequest request = new PostRequest ("수정된 제목" , "내용" , null , List .of ());
382+ PostRequest request = new PostRequest ("수정된 제목" , "내용" , null , List .of (), List . of () );
322383
323384 // when & then
324385 mvc .perform (put ("/api/posts/{postId}" , 999L )
@@ -354,7 +415,7 @@ void updatePost_fail_noPermission() throws Exception {
354415
355416 String accessToken = generateAccessToken (another );
356417
357- PostRequest request = new PostRequest ("수정된 제목" , "수정된 내용" , null , List .of (c1 .getId ()));
418+ PostRequest request = new PostRequest ("수정된 제목" , "수정된 내용" , null , List .of (c1 .getId ()), List . of () );
358419
359420 // when & then
360421 mvc .perform (put ("/api/posts/{postId}" , post .getId ())
@@ -386,7 +447,7 @@ void updatePost_fail_categoryNotFound() throws Exception {
386447 String accessToken = generateAccessToken (user );
387448
388449 // 존재하지 않는 카테고리 ID
389- PostRequest request = new PostRequest ("수정된 제목" , "수정된 내용" , null , List .of (999L ));
450+ PostRequest request = new PostRequest ("수정된 제목" , "수정된 내용" , null , List .of (999L ), List . of () );
390451
391452 // when & then
392453 mvc .perform (put ("/api/posts/{postId}" , post .getId ())
@@ -399,6 +460,49 @@ void updatePost_fail_categoryNotFound() throws Exception {
399460 .andExpect (jsonPath ("$.message" ).value ("존재하지 않는 카테고리입니다." ));
400461 }
401462
463+ @ Test
464+ @ DisplayName ("게시글 수정 실패 - 존재하지 않는 이미지 → 404 Not Found" )
465+ void updatePost_fail_imageNotFound () throws Exception {
466+ // given
467+ User user =
User .
createUser (
"writer" ,
"[email protected] " ,
passwordEncoder .
encode (
"P@ssw0rd!" ));
468+ user .setUserProfile (new UserProfile (user , "홍길동" , null , null , null , 0 ));
469+ user .setUserStatus (UserStatus .ACTIVE );
470+ userRepository .save (user );
471+
472+ String accessToken = generateAccessToken (user );
473+
474+ // 카테고리 등록
475+ PostCategory c1 = new PostCategory ("공지사항" , CategoryType .SUBJECT );
476+ postCategoryRepository .save (c1 );
477+
478+ // 게시글 생성
479+ Post post = new Post (user , "수정 테스트 제목" , "수정 테스트 내용" , null );
480+ postRepository .save (post );
481+
482+ // 존재하지 않는 이미지 ID
483+ Long invalidImageId = 999L ;
484+
485+ // 수정 요청
486+ PostRequest request = new PostRequest (
487+ "수정된 제목" ,
488+ "수정된 내용" ,
489+ null ,
490+ List .of (c1 .getId ()),
491+ List .of (invalidImageId )
492+ );
493+
494+ // when & then
495+ mvc .perform (put ("/api/posts/{postId}" , post .getId ())
496+ .header ("Authorization" , "Bearer " + accessToken )
497+ .contentType (MediaType .APPLICATION_JSON )
498+ .content (objectMapper .writeValueAsString (request )))
499+ .andDo (print ())
500+ .andExpect (status ().isNotFound ())
501+ .andExpect (jsonPath ("$.success" ).value (false ))
502+ .andExpect (jsonPath ("$.code" ).value ("FILE_004" ))
503+ .andExpect (jsonPath ("$.message" ).value ("파일 정보를 찾을 수 없습니다." ));
504+ }
505+
402506 @ Test
403507 @ DisplayName ("게시글 수정 실패 - 잘못된 요청(필드 누락) → 400 Bad Request" )
404508 void updatePost_fail_badRequest () throws Exception {
@@ -431,7 +535,7 @@ void updatePost_fail_badRequest() throws Exception {
431535 @ DisplayName ("게시글 수정 실패 - 인증 없음 → 401 Unauthorized" )
432536 void updatePost_fail_unauthorized () throws Exception {
433537 // given
434- PostRequest request = new PostRequest ("제목" , "내용" , null , List .of ());
538+ PostRequest request = new PostRequest ("제목" , "내용" , null , List .of (), List . of () );
435539
436540 // when & then
437541 mvc .perform (put ("/api/posts/{postId}" , 1L )
0 commit comments