22
33import com .fasterxml .jackson .core .JsonProcessingException ;
44import com .somemore .domains .review .dto .request .ReviewCreateRequestDto ;
5+ import com .somemore .domains .review .dto .request .ReviewUpdateRequestDto ;
56import com .somemore .domains .review .usecase .CreateReviewUseCase ;
67import com .somemore .domains .review .usecase .DeleteReviewUseCase ;
8+ import com .somemore .domains .review .usecase .UpdateReviewUseCase ;
79import com .somemore .global .imageupload .usecase .ImageUploadUseCase ;
810import com .somemore .support .ControllerTestSupport ;
911import com .somemore .support .annotation .WithMockCustomUser ;
1214import org .springframework .boot .test .mock .mockito .MockBean ;
1315import org .springframework .http .MediaType ;
1416import org .springframework .mock .web .MockMultipartFile ;
17+ import org .springframework .test .web .servlet .request .MockMultipartHttpServletRequestBuilder ;
1518
1619import java .util .UUID ;
1720
1821import static org .mockito .ArgumentMatchers .any ;
1922import static org .mockito .ArgumentMatchers .anyString ;
2023import static org .mockito .BDDMockito .given ;
2124import static org .mockito .BDDMockito .willDoNothing ;
25+ import static org .springframework .http .MediaType .APPLICATION_JSON ;
2226import static org .springframework .http .MediaType .MULTIPART_FORM_DATA ;
23- import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .delete ;
24- import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .multipart ;
27+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .*;
2528import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .jsonPath ;
2629import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
2730
@@ -34,7 +37,10 @@ class ReviewCommandApiControllerTest extends ControllerTestSupport {
3437 private CreateReviewUseCase createReviewUseCase ;
3538
3639 @ MockBean
37- DeleteReviewUseCase deleteReviewUseCase ;
40+ private UpdateReviewUseCase updateReviewUseCase ;
41+
42+ @ MockBean
43+ private DeleteReviewUseCase deleteReviewUseCase ;
3844
3945 @ DisplayName ("리뷰 생성 성공" )
4046 @ Test
@@ -154,6 +160,111 @@ void createReviewValidateTestContent() throws Exception {
154160 .andExpect (jsonPath ("$.detail" ).value ("리뷰 내용은 필수 값입니다." ));
155161 }
156162
163+ @ DisplayName ("리뷰 수정 성공" )
164+ @ Test
165+ @ WithMockCustomUser ()
166+ void updateReview () throws Exception {
167+ // given
168+ ReviewUpdateRequestDto requestDto = ReviewUpdateRequestDto .builder ()
169+ .title ("리뷰 수정 제목" )
170+ .content ("리뷰 수정 내용" )
171+ .build ();
172+
173+ Long reviewId = 1L ;
174+
175+ willDoNothing ().given (updateReviewUseCase ).updateReview (any (), any (UUID .class ), any ());
176+ String requestBody = objectMapper .writeValueAsString (requestDto );
177+
178+ // when
179+ mockMvc .perform (put ("/api/review/{id}" , reviewId )
180+ .content (requestBody )
181+ .contentType (APPLICATION_JSON )
182+ .header ("Authorization" , "Bearer access-token" ))
183+ // then
184+ .andExpect (status ().isOk ())
185+ .andExpect (jsonPath ("$.code" ).value (200 ))
186+ .andExpect (jsonPath ("$.message" ).value ("리뷰 수정 성공" ));
187+ }
188+
189+ @ DisplayName ("리뷰 수정 유효성 테스트 - 제목" )
190+ @ Test
191+ @ WithMockCustomUser ()
192+ void updateReviewValidateTitle () throws Exception {
193+ // given
194+ ReviewUpdateRequestDto requestDto = ReviewUpdateRequestDto .builder ()
195+ .content ("업데이트 내용" )
196+ .build ();
197+
198+ Long reviewId = 1L ;
199+ String requestBody = objectMapper .writeValueAsString (requestDto );
200+
201+ // when
202+ mockMvc .perform (put ("/api/review/{id}" , reviewId )
203+ .content (requestBody )
204+ .contentType (APPLICATION_JSON )
205+ .header ("Authorization" , "Bearer access-token" ))
206+ // then
207+ .andExpect (status ().isBadRequest ())
208+ .andExpect (jsonPath ("$.status" ).value (400 ))
209+ .andExpect (jsonPath ("$.title" ).value ("유효성 예외" ))
210+ .andExpect (jsonPath ("$.detail" ).value ("리뷰 제목은 필수 값입니다." ));
211+ }
212+
213+ @ DisplayName ("리뷰 수정 유효성 테스트 - 내용" )
214+ @ Test
215+ @ WithMockCustomUser ()
216+ void updateReviewValidateContent () throws Exception {
217+ // given
218+ ReviewUpdateRequestDto requestDto = ReviewUpdateRequestDto .builder ()
219+ .title ("업데이트 제목" )
220+ .build ();
221+
222+ Long reviewId = 1L ;
223+ String requestBody = objectMapper .writeValueAsString (requestDto );
224+
225+ // when
226+ mockMvc .perform (put ("/api/review/{id}" , reviewId )
227+ .content (requestBody )
228+ .contentType (APPLICATION_JSON )
229+ .header ("Authorization" , "Bearer access-token" ))
230+ // then
231+ .andExpect (status ().isBadRequest ())
232+ .andExpect (jsonPath ("$.status" ).value (400 ))
233+ .andExpect (jsonPath ("$.title" ).value ("유효성 예외" ))
234+ .andExpect (jsonPath ("$.detail" ).value ("리뷰 내용은 필수 값입니다." ));
235+ }
236+
237+ @ DisplayName ("리뷰 이미지 수정 성공" )
238+ @ Test
239+ @ WithMockCustomUser ()
240+ void updateReviewImage () throws Exception {
241+ // given
242+ Long reviewId = 1L ;
243+
244+ MockMultipartFile imageFile = new MockMultipartFile (
245+ "img_file" ,
246+ "test-image.jpg" ,
247+ MediaType .IMAGE_JPEG_VALUE ,
248+ "test image content" .getBytes ()
249+ );
250+
251+ String imgUrl = "https://example.com/image/test-image.jpg" ;
252+
253+ given (imageUploadUseCase .uploadImage (any ())).willReturn (imgUrl );
254+
255+ willDoNothing ().given (updateReviewUseCase ).updateReviewImageUrl (any (), any (UUID .class ), anyString ());
256+
257+ // when
258+ mockMvc .perform (createMultipartPutRequest ("/api/review/{id}" , reviewId )
259+ .file (imageFile )
260+ .contentType (MULTIPART_FORM_DATA )
261+ .header ("Authorization" , "Bearer access-token" ))
262+ // then
263+ .andExpect (status ().isOk ())
264+ .andExpect (jsonPath ("$.code" ).value (200 ))
265+ .andExpect (jsonPath ("$.message" ).value ("리뷰 이미지 수정 성공" ));
266+ }
267+
157268 @ DisplayName ("리뷰 삭제 성공 테스트" )
158269 @ Test
159270 @ WithMockCustomUser ()
@@ -182,4 +293,14 @@ private MockMultipartFile getRequestData(ReviewCreateRequestDto requestDto)
182293 objectMapper .writeValueAsBytes (requestDto )
183294 );
184295 }
296+
297+ private MockMultipartHttpServletRequestBuilder createMultipartPutRequest (String url , Long id ) {
298+ MockMultipartHttpServletRequestBuilder builder = multipart (url , id );
299+ builder .with (request -> {
300+ request .setMethod ("PUT" );
301+ return request ;
302+ });
303+ return builder ;
304+ }
305+
185306}
0 commit comments