diff --git a/src/test/kotlin/roomescape/reservation/business/ReservationServiceTest.kt b/src/test/kotlin/roomescape/reservation/business/ReservationServiceTest.kt index 32efd17d..cef5672c 100644 --- a/src/test/kotlin/roomescape/reservation/business/ReservationServiceTest.kt +++ b/src/test/kotlin/roomescape/reservation/business/ReservationServiceTest.kt @@ -5,11 +5,13 @@ import io.kotest.core.spec.style.FunSpec import io.kotest.matchers.shouldBe import io.mockk.every import io.mockk.mockk +import org.springframework.data.repository.findByIdOrNull import roomescape.member.business.MemberService import roomescape.member.infrastructure.persistence.Role import roomescape.reservation.exception.ReservationErrorCode import roomescape.reservation.exception.ReservationException import roomescape.reservation.infrastructure.persistence.ReservationRepository +import roomescape.reservation.infrastructure.persistence.ReservationStatus import roomescape.theme.business.ThemeService import roomescape.time.business.TimeService import roomescape.util.MemberFixture @@ -121,6 +123,56 @@ class ReservationServiceTest : FunSpec({ } } + context("예약 대기를 취소할 때") { + val reservationId = 1L + val member = MemberFixture.create(id = 1L, role = Role.MEMBER) + test("예약을 찾을 수 없으면 예외를 던진다.") { + every { + reservationRepository.findByIdOrNull(reservationId) + } returns null + + shouldThrow { + reservationService.deleteWaiting(reservationId, member.id!!) + }.also { + it.errorCode shouldBe ReservationErrorCode.RESERVATION_NOT_FOUND + } + } + + test("대기중인 해당 예약이 이미 확정된 상태라면 예외를 던진다.") { + val alreadyConfirmed = ReservationFixture.create( + id = reservationId, + status = ReservationStatus.CONFIRMED + ) + every { + reservationRepository.findByIdOrNull(reservationId) + } returns alreadyConfirmed + + shouldThrow { + reservationService.deleteWaiting(reservationId, member.id!!) + }.also { + it.errorCode shouldBe ReservationErrorCode.ALREADY_CONFIRMED + } + } + + test("타인의 대기를 취소하려고 하면 예외를 던진다.") { + val otherMembersWaiting = ReservationFixture.create( + id = reservationId, + member = MemberFixture.create(id = member.id!! + 1L), + status = ReservationStatus.WAITING + ) + + every { + reservationRepository.findByIdOrNull(reservationId) + } returns otherMembersWaiting + + shouldThrow { + reservationService.deleteWaiting(reservationId, member.id!!) + }.also { + it.errorCode shouldBe ReservationErrorCode.NOT_RESERVATION_OWNER + } + } + } + context("예약을 조회할 때") { test("종료 날짜가 시작 날짜보다 이전이면 예외를 던진다.") { val startFrom = LocalDate.now() @@ -173,4 +225,61 @@ class ReservationServiceTest : FunSpec({ } } } + + context("대기중인 예약을 거절할 때") { + test("관리자가 아니면 예외를 던진다.") { + val member = MemberFixture.create(id = 1L, role = Role.MEMBER) + + every { + memberService.findById(any()) + } returns member + + shouldThrow { + reservationService.rejectWaiting(1L, member.id!!) + }.also { + it.errorCode shouldBe ReservationErrorCode.NO_PERMISSION + } + } + + test("예약을 찾을 수 없으면 예외를 던진다.") { + val member = MemberFixture.create(id = 1L, role = Role.ADMIN) + val reservationId = 1L + + every { + memberService.findById(member.id!!) + } returns member + + every { + reservationRepository.findByIdOrNull(reservationId) + } returns null + + shouldThrow { + reservationService.rejectWaiting(reservationId, member.id!!) + }.also { + it.errorCode shouldBe ReservationErrorCode.RESERVATION_NOT_FOUND + } + } + + test("이미 확정된 예약이면 예외를 던진다.") { + val member = MemberFixture.create(id = 1L, role = Role.ADMIN) + val reservation = ReservationFixture.create( + id = 1L, + status = ReservationStatus.CONFIRMED + ) + + every { + memberService.findById(member.id!!) + } returns member + + every { + reservationRepository.findByIdOrNull(reservation.id!!) + } returns reservation + + shouldThrow { + reservationService.rejectWaiting(reservation.id!!, member.id!!) + }.also { + it.errorCode shouldBe ReservationErrorCode.ALREADY_CONFIRMED + } + } + } }) diff --git a/src/test/kotlin/roomescape/time/business/TimeServiceTest.kt b/src/test/kotlin/roomescape/time/business/TimeServiceTest.kt index ba12249e..a3d9801a 100644 --- a/src/test/kotlin/roomescape/time/business/TimeServiceTest.kt +++ b/src/test/kotlin/roomescape/time/business/TimeServiceTest.kt @@ -1,9 +1,12 @@ package roomescape.time.business +import io.kotest.assertions.throwables.shouldNotThrow import io.kotest.assertions.throwables.shouldThrow import io.kotest.core.spec.style.FunSpec import io.kotest.matchers.shouldBe +import io.mockk.Runs import io.mockk.every +import io.mockk.just import io.mockk.mockk import org.springframework.data.repository.findByIdOrNull import roomescape.reservation.infrastructure.persistence.ReservationRepository @@ -63,6 +66,19 @@ class TimeServiceTest : FunSpec({ } context("removeTimeById") { + test("정상 제거 및 응답") { + val id = 1L + val time = TimeFixture.create(id = id) + + every { timeRepository.findByIdOrNull(id) } returns time + every { reservationRepository.findAllByTime(time) } returns emptyList() + every { timeRepository.delete(time) } just Runs + + shouldNotThrow { + timeService.deleteTime(id) + } + } + test("시간을 찾을 수 없으면 예외 응답") { val id = 1L