|
29 | 29 | import org.mockito.InjectMocks; |
30 | 30 | import org.mockito.Mock; |
31 | 31 | import org.mockito.junit.jupiter.MockitoExtension; |
| 32 | +import org.springframework.test.util.ReflectionTestUtils; |
32 | 33 |
|
33 | 34 | import java.time.LocalDateTime; |
| 35 | +import java.time.temporal.ChronoUnit; |
34 | 36 | import java.util.List; |
35 | 37 | import java.util.Optional; |
36 | 38 |
|
@@ -287,4 +289,124 @@ void throwExceptionOnConcurrentApproval() { |
287 | 289 | .hasFieldOrPropertyWithValue("resultCode", ReservationErrorCode.CONCURRENT_APPROVAL_CONFLICT.getCode()); |
288 | 290 | } |
289 | 291 | } |
| 292 | + |
| 293 | + @Nested |
| 294 | + @DisplayName("예약 거절") |
| 295 | + class Describe_rejectReservation { |
| 296 | + |
| 297 | + @Test |
| 298 | + @DisplayName("예약 거절 성공") |
| 299 | + void rejectReservation() { |
| 300 | + // given |
| 301 | + when(mentoringStorage.findReservation(reservation.getId())) |
| 302 | + .thenReturn(reservation); |
| 303 | + |
| 304 | + // when |
| 305 | + ReservationResponse result = reservationService.rejectReservation(mentor, reservation.getId()); |
| 306 | + |
| 307 | + // then |
| 308 | + assertThat(result.reservation().status()).isEqualTo(ReservationStatus.REJECTED); |
| 309 | + assertThat(result.reservation().mentorSlotId()).isEqualTo(mentorSlot2.getId()); |
| 310 | + assertThat(result.mentor().mentorId()).isEqualTo(mentor.getId()); |
| 311 | + assertThat(result.mentee().menteeId()).isEqualTo(mentee.getId()); |
| 312 | + } |
| 313 | + |
| 314 | + @Test |
| 315 | + @DisplayName("PENDING 상태가 아니면 거절 불가") |
| 316 | + void throwExceptionWhenNotPending() { |
| 317 | + // given |
| 318 | + reservation.approve(mentor); |
| 319 | + |
| 320 | + when(mentoringStorage.findReservation(reservation.getId())) |
| 321 | + .thenReturn(reservation); |
| 322 | + |
| 323 | + // when & then |
| 324 | + assertThatThrownBy(() -> reservationService.rejectReservation(mentor, reservation.getId())) |
| 325 | + .isInstanceOf(ServiceException.class) |
| 326 | + .hasFieldOrPropertyWithValue("resultCode", ReservationErrorCode.CANNOT_REJECT.getCode()); |
| 327 | + } |
| 328 | + } |
| 329 | + |
| 330 | + @Nested |
| 331 | + @DisplayName("예약 취소") |
| 332 | + class Describe_cancelReservation { |
| 333 | + |
| 334 | + @Test |
| 335 | + @DisplayName("멘토가 예약 취소 성공") |
| 336 | + void cancelReservationByMentor() { |
| 337 | + // given |
| 338 | + when(mentoringStorage.findReservation(reservation.getId())) |
| 339 | + .thenReturn(reservation); |
| 340 | + |
| 341 | + // when |
| 342 | + ReservationResponse result = reservationService.cancelReservation(mentor, reservation.getId()); |
| 343 | + |
| 344 | + // then |
| 345 | + assertThat(result.reservation().status()).isEqualTo(ReservationStatus.CANCELED); |
| 346 | + } |
| 347 | + |
| 348 | + @Test |
| 349 | + @DisplayName("멘티가 예약 취소 성공") |
| 350 | + void cancelReservationByMentee() { |
| 351 | + // given |
| 352 | + when(mentoringStorage.findReservation(reservation.getId())) |
| 353 | + .thenReturn(reservation); |
| 354 | + |
| 355 | + // when |
| 356 | + ReservationResponse result = reservationService.cancelReservation(mentee, reservation.getId()); |
| 357 | + |
| 358 | + // then |
| 359 | + assertThat(result.reservation().status()).isEqualTo(ReservationStatus.CANCELED); |
| 360 | + } |
| 361 | + |
| 362 | + @Test |
| 363 | + @DisplayName("COMPLETED 상태는 취소 불가") |
| 364 | + void throwExceptionWhenCompleted() { |
| 365 | + // given |
| 366 | + // 완료 상태의 과거 슬롯 |
| 367 | + LocalDateTime pastTime = LocalDateTime.now().minusDays(1).truncatedTo(ChronoUnit.SECONDS); |
| 368 | + MentorSlot pastSlot = MentorSlotFixture.create(3L, mentor, pastTime, pastTime.plusHours(1)); |
| 369 | + Reservation completedReservation = ReservationFixture.create(2L, mentoring, mentee, pastSlot); |
| 370 | + |
| 371 | + // PENDING -> APPROVED는 미래 시간에 해야 하므로 리플렉션으로 직접 상태 변경 |
| 372 | + ReflectionTestUtils.setField(completedReservation, "status", ReservationStatus.APPROVED); |
| 373 | + completedReservation.complete(); |
| 374 | + |
| 375 | + when(mentoringStorage.findReservation(completedReservation.getId())) |
| 376 | + .thenReturn(completedReservation); |
| 377 | + |
| 378 | + // when & then |
| 379 | + assertThatThrownBy(() -> reservationService.cancelReservation(mentor, completedReservation.getId())) |
| 380 | + .isInstanceOf(ServiceException.class) |
| 381 | + .hasFieldOrPropertyWithValue("resultCode", ReservationErrorCode.CANNOT_CANCEL.getCode()); |
| 382 | + } |
| 383 | + |
| 384 | + @Test |
| 385 | + @DisplayName("다른 멘토는 취소 불가") |
| 386 | + void throwExceptionWhenNotMentor() { |
| 387 | + // given |
| 388 | + Member anotherMentorMember = MemberFixture. create( "[email protected]", "Another", "pass123"); |
| 389 | + Mentor anotherMentor = MentorFixture.create(2L, anotherMentorMember); |
| 390 | + when(mentoringStorage.findReservation(reservation.getId())) |
| 391 | + .thenReturn(reservation); |
| 392 | + |
| 393 | + // when & then |
| 394 | + assertThatThrownBy(() -> reservationService.cancelReservation(anotherMentor, reservation.getId())) |
| 395 | + .isInstanceOf(ServiceException.class) |
| 396 | + .hasFieldOrPropertyWithValue("resultCode", ReservationErrorCode.FORBIDDEN_NOT_MENTOR.getCode()); |
| 397 | + } |
| 398 | + |
| 399 | + @Test |
| 400 | + @DisplayName("다른 멘티는 취소 불가") |
| 401 | + void throwExceptionWhenNotMentee() { |
| 402 | + // given |
| 403 | + when(mentoringStorage.findReservation(reservation.getId())) |
| 404 | + .thenReturn(reservation); |
| 405 | + |
| 406 | + // when & then |
| 407 | + assertThatThrownBy(() -> reservationService.cancelReservation(mentee2, reservation.getId())) |
| 408 | + .isInstanceOf(ServiceException.class) |
| 409 | + .hasFieldOrPropertyWithValue("resultCode", ReservationErrorCode.FORBIDDEN_NOT_MENTEE.getCode()); |
| 410 | + } |
| 411 | + } |
290 | 412 | } |
0 commit comments