[#16] Reservation 도메인 코드 코틀린 마이그레이션 #17

Merged
pricelees merged 40 commits from refactor/#16 into main 2025-07-21 12:08:56 +00:00
2 changed files with 107 additions and 263 deletions
Showing only changes of commit ba0dc44e04 - Show all commits

View File

@ -1,129 +0,0 @@
package roomescape.payment.business
import io.kotest.assertions.assertSoftly
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import io.mockk.every
import io.mockk.just
import io.mockk.mockk
import io.mockk.runs
import org.springframework.http.HttpStatus
import roomescape.common.exception.ErrorType
import roomescape.common.exception.RoomescapeException
import roomescape.payment.infrastructure.persistence.CanceledPaymentRepository
import roomescape.payment.infrastructure.persistence.PaymentRepository
import roomescape.payment.web.PaymentCancel
import roomescape.util.PaymentFixture
import java.time.OffsetDateTime
class PaymentServiceKTest : FunSpec({
val paymentRepository: PaymentRepository = mockk()
val canceledPaymentRepository: CanceledPaymentRepository = mockk()
val paymentService = PaymentService(paymentRepository, canceledPaymentRepository)
context("cancelPaymentByAdmin") {
val reservationId = 1L
test("reservationId로 paymentKey를 찾을 수 없으면 예외를 던진다.") {
every { paymentRepository.findPaymentKeyByReservationId(reservationId) } returns null
val exception = shouldThrow<RoomescapeException> {
paymentService.cancelPaymentByAdmin(reservationId)
}
assertSoftly(exception) {
this.errorType shouldBe ErrorType.PAYMENT_NOT_FOUND
this.httpStatus shouldBe HttpStatus.NOT_FOUND
}
}
context("reservationId로 paymentKey를 찾고난 후") {
val paymentKey = "test-payment-key"
every {
paymentRepository.findPaymentKeyByReservationId(reservationId)
} returns paymentKey
test("해당 paymentKey로 paymentEntity를 찾을 수 없으면 예외를 던진다.") {
every {
paymentRepository.findByPaymentKey(paymentKey)
} returns null
val exception = shouldThrow<RoomescapeException> {
paymentService.cancelPaymentByAdmin(reservationId)
}
assertSoftly(exception) {
this.errorType shouldBe ErrorType.PAYMENT_NOT_FOUND
this.httpStatus shouldBe HttpStatus.NOT_FOUND
}
}
test("해당 paymentKey로 paymentEntity를 찾고, cancelPaymentEntity를 저장한다.") {
val paymentEntity = PaymentFixture.create(paymentKey = paymentKey)
every {
paymentRepository.findByPaymentKey(paymentKey)
} returns paymentEntity.also {
every {
paymentRepository.delete(it)
} just runs
}
every {
canceledPaymentRepository.save(any())
} returns PaymentFixture.createCanceled(
id = 1L,
paymentKey = paymentKey,
cancelAmount = paymentEntity.totalAmount,
)
val result: PaymentCancel.Request = paymentService.cancelPaymentByAdmin(reservationId)
assertSoftly(result) {
this.paymentKey shouldBe paymentKey
this.amount shouldBe paymentEntity.totalAmount
this.cancelReason shouldBe "고객 요청"
}
}
}
}
context("updateCanceledTime") {
val paymentKey = "test-payment-key"
val canceledAt = OffsetDateTime.now()
test("paymentKey로 canceledPaymentEntity를 찾을 수 없으면 예외를 던진다.") {
every {
canceledPaymentRepository.findByPaymentKey(paymentKey)
} returns null
val exception = shouldThrow<RoomescapeException> {
paymentService.updateCanceledTime(paymentKey, canceledAt)
}
assertSoftly(exception) {
this.errorType shouldBe ErrorType.PAYMENT_NOT_FOUND
this.httpStatus shouldBe HttpStatus.NOT_FOUND
}
}
test("paymentKey로 canceledPaymentEntity를 찾고, canceledAt을 업데이트한다.") {
val canceledPaymentEntity = PaymentFixture.createCanceled(
paymentKey = paymentKey,
canceledAt = canceledAt.minusMinutes(1)
)
every {
canceledPaymentRepository.findByPaymentKey(paymentKey)
} returns canceledPaymentEntity
paymentService.updateCanceledTime(paymentKey, canceledAt)
assertSoftly(canceledPaymentEntity) {
this.canceledAt shouldBe canceledAt
}
}
}
})

View File

@ -1,156 +1,129 @@
package roomescape.payment.business; package roomescape.payment.business
import static org.assertj.core.api.Assertions.*; import io.kotest.assertions.assertSoftly
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import io.mockk.every
import io.mockk.just
import io.mockk.mockk
import io.mockk.runs
import org.springframework.http.HttpStatus
import roomescape.common.exception.ErrorType
import roomescape.common.exception.RoomescapeException
import roomescape.payment.infrastructure.persistence.CanceledPaymentRepository
import roomescape.payment.infrastructure.persistence.PaymentRepository
import roomescape.payment.web.PaymentCancel
import roomescape.util.PaymentFixture
import java.time.OffsetDateTime
import java.time.LocalDate; class PaymentServiceTest : FunSpec({
import java.time.LocalDateTime; val paymentRepository: PaymentRepository = mockk()
import java.time.OffsetDateTime; val canceledPaymentRepository: CanceledPaymentRepository = mockk()
import org.junit.jupiter.api.DisplayName; val paymentService = PaymentService(paymentRepository, canceledPaymentRepository)
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.jdbc.Sql.ExecutionPhase;
import roomescape.common.exception.RoomescapeException; context("cancelPaymentByAdmin") {
import roomescape.member.infrastructure.persistence.MemberEntity; val reservationId = 1L
import roomescape.member.infrastructure.persistence.MemberRepository; test("reservationId로 paymentKey를 찾을 수 없으면 예외를 던진다.") {
import roomescape.member.infrastructure.persistence.Role; every { paymentRepository.findPaymentKeyByReservationId(reservationId) } returns null
import roomescape.payment.infrastructure.persistence.CanceledPaymentEntity;
import roomescape.payment.infrastructure.persistence.CanceledPaymentRepository;
import roomescape.payment.web.PaymentApprove;
import roomescape.payment.web.PaymentCancel;
import roomescape.payment.web.ReservationPaymentResponse;
import roomescape.reservation.infrastructure.persistence.ReservationEntity;
import roomescape.reservation.infrastructure.persistence.ReservationRepository;
import roomescape.reservation.infrastructure.persistence.ReservationStatus;
import roomescape.reservation.infrastructure.persistence.ReservationTimeEntity;
import roomescape.reservation.infrastructure.persistence.ReservationTimeRepository;
import roomescape.theme.infrastructure.persistence.ThemeEntity;
import roomescape.theme.infrastructure.persistence.ThemeRepository;
@SpringBootTest val exception = shouldThrow<RoomescapeException> {
@Sql(scripts = "/truncate.sql", executionPhase = ExecutionPhase.BEFORE_TEST_METHOD) paymentService.cancelPaymentByAdmin(reservationId)
class PaymentServiceTest {
@Autowired
private PaymentService paymentService;
@Autowired
private ReservationRepository reservationRepository;
@Autowired
private MemberRepository memberRepository;
@Autowired
private ReservationTimeRepository reservationTimeRepository;
@Autowired
private ThemeRepository themeRepository;
@Autowired
private CanceledPaymentRepository canceledPaymentRepository;
@Test
@DisplayName("결제 정보를 저장한다.")
void savePayment() {
// given
PaymentApprove.Response paymentInfo = new PaymentApprove.Response("payment-key", "order-id",
OffsetDateTime.now(), 10000L);
LocalDateTime localDateTime = LocalDateTime.now().plusHours(1L);
LocalDate date = localDateTime.toLocalDate();
ReservationTimeEntity time = reservationTimeRepository.save(
new ReservationTimeEntity(null, localDateTime.toLocalTime()));
MemberEntity member = memberRepository.save(
new MemberEntity(null, "member", "email@email.com", "password", Role.MEMBER));
ThemeEntity theme = themeRepository.save(new ThemeEntity(null, "name", "desc", "thumbnail"));
ReservationEntity reservation = reservationRepository.save(
new ReservationEntity(null, date, time, theme, member,
ReservationStatus.CONFIRMED));
// when
ReservationPaymentResponse reservationPaymentResponse = paymentService.savePayment(paymentInfo, reservation);
// then
assertThat(reservationPaymentResponse.reservation().id).isEqualTo(reservation.getId());
assertThat(reservationPaymentResponse.paymentKey()).isEqualTo(paymentInfo.paymentKey);
} }
@Test assertSoftly(exception) {
@DisplayName("예약 ID로 결제 정보를 제거하고, 결제 취소 테이블에 취소 정보를 저장한다.") this.errorType shouldBe ErrorType.PAYMENT_NOT_FOUND
void cancelPaymentByAdmin() { this.httpStatus shouldBe HttpStatus.NOT_FOUND
// given
PaymentApprove.Response paymentInfo = new PaymentApprove.Response("payment-key", "order-id",
OffsetDateTime.now(), 10000L);
LocalDateTime localDateTime = LocalDateTime.now().plusHours(1L);
LocalDate date = localDateTime.toLocalDate();
ReservationTimeEntity time = reservationTimeRepository.save(
new ReservationTimeEntity(null, localDateTime.toLocalTime()));
MemberEntity member = memberRepository.save(
new MemberEntity(null, "member", "email@email.com", "password", Role.MEMBER));
ThemeEntity theme = themeRepository.save(new ThemeEntity(null, "name", "desc", "thumbnail"));
ReservationEntity reservation = reservationRepository.save(
new ReservationEntity(null, date, time, theme, member,
ReservationStatus.CONFIRMED));
paymentService.savePayment(paymentInfo, reservation);
// when
PaymentCancel.Request paymentCancelRequest = paymentService.cancelPaymentByAdmin(reservation.getId());
// then
assertThat(canceledPaymentRepository.findByPaymentKey("payment-key")).isNotNull();
assertThat(paymentCancelRequest.paymentKey).isEqualTo(paymentInfo.paymentKey);
assertThat(paymentCancelRequest.cancelReason).isEqualTo("고객 요청");
assertThat(paymentCancelRequest.amount).isEqualTo(10000L);
}
@Test
@DisplayName("입력된 예약 ID에 대한 결제 정보가 없으면 예외가 발생한다.")
void cancelPaymentByAdminWithNonExistentReservationId() {
// given
Long nonExistentReservationId = 1L;
// when
assertThatThrownBy(() -> paymentService.cancelPaymentByAdmin(nonExistentReservationId))
.isInstanceOf(RoomescapeException.class);
}
@Test
@DisplayName("결제 취소 정보에 있는 취소 시간을 업데이트한다.")
void updateCanceledTime() {
// given
PaymentApprove.Response paymentInfo = new PaymentApprove.Response("payment-key", "order-id",
OffsetDateTime.now(), 10000L);
LocalDateTime localDateTime = LocalDateTime.now().plusHours(1L);
LocalDate date = localDateTime.toLocalDate();
ReservationTimeEntity time = reservationTimeRepository.save(
new ReservationTimeEntity(null, localDateTime.toLocalTime()));
MemberEntity member = memberRepository.save(
new MemberEntity(null, "member", "email@email.com", "password", Role.MEMBER));
ThemeEntity theme = themeRepository.save(new ThemeEntity(null, "name", "desc", "thumbnail"));
ReservationEntity reservation = reservationRepository.save(
new ReservationEntity(null, date, time, theme, member,
ReservationStatus.CONFIRMED));
paymentService.savePayment(paymentInfo, reservation);
paymentService.cancelPaymentByAdmin(reservation.getId());
// when
OffsetDateTime canceledAt = OffsetDateTime.now().plusHours(2L);
paymentService.updateCanceledTime(paymentInfo.paymentKey, canceledAt);
// then
CanceledPaymentEntity canceledPayment = canceledPaymentRepository.findByPaymentKey(paymentInfo.paymentKey);
assertThat(canceledPayment).isNotNull();
assertThat(canceledPayment.getCanceledAt()).isEqualTo(canceledAt);
}
@Test
@DisplayName("결제 취소 시간을 업데이트 할 때, 입력한 paymentKey가 존재하지 않으면 예외가 발생한다.")
void updateCanceledTimeWithNonExistentPaymentKey() {
// given
OffsetDateTime canceledAt = OffsetDateTime.now().plusHours(2L);
// when
assertThatThrownBy(() -> paymentService.updateCanceledTime("non-existent-payment-key", canceledAt))
.isInstanceOf(RoomescapeException.class);
} }
} }
context("reservationId로 paymentKey를 찾고난 후") {
val paymentKey = "test-payment-key"
every {
paymentRepository.findPaymentKeyByReservationId(reservationId)
} returns paymentKey
test("해당 paymentKey로 paymentEntity를 찾을 수 없으면 예외를 던진다.") {
every {
paymentRepository.findByPaymentKey(paymentKey)
} returns null
val exception = shouldThrow<RoomescapeException> {
paymentService.cancelPaymentByAdmin(reservationId)
}
assertSoftly(exception) {
this.errorType shouldBe ErrorType.PAYMENT_NOT_FOUND
this.httpStatus shouldBe HttpStatus.NOT_FOUND
}
}
test("해당 paymentKey로 paymentEntity를 찾고, cancelPaymentEntity를 저장한다.") {
val paymentEntity = PaymentFixture.create(paymentKey = paymentKey)
every {
paymentRepository.findByPaymentKey(paymentKey)
} returns paymentEntity.also {
every {
paymentRepository.delete(it)
} just runs
}
every {
canceledPaymentRepository.save(any())
} returns PaymentFixture.createCanceled(
id = 1L,
paymentKey = paymentKey,
cancelAmount = paymentEntity.totalAmount,
)
val result: PaymentCancel.Request = paymentService.cancelPaymentByAdmin(reservationId)
assertSoftly(result) {
this.paymentKey shouldBe paymentKey
this.amount shouldBe paymentEntity.totalAmount
this.cancelReason shouldBe "고객 요청"
}
}
}
}
context("updateCanceledTime") {
val paymentKey = "test-payment-key"
val canceledAt = OffsetDateTime.now()
test("paymentKey로 canceledPaymentEntity를 찾을 수 없으면 예외를 던진다.") {
every {
canceledPaymentRepository.findByPaymentKey(paymentKey)
} returns null
val exception = shouldThrow<RoomescapeException> {
paymentService.updateCanceledTime(paymentKey, canceledAt)
}
assertSoftly(exception) {
this.errorType shouldBe ErrorType.PAYMENT_NOT_FOUND
this.httpStatus shouldBe HttpStatus.NOT_FOUND
}
}
test("paymentKey로 canceledPaymentEntity를 찾고, canceledAt을 업데이트한다.") {
val canceledPaymentEntity = PaymentFixture.createCanceled(
paymentKey = paymentKey,
canceledAt = canceledAt.minusMinutes(1)
)
every {
canceledPaymentRepository.findByPaymentKey(paymentKey)
} returns canceledPaymentEntity
paymentService.updateCanceledTime(paymentKey, canceledAt)
assertSoftly(canceledPaymentEntity) {
this.canceledAt shouldBe canceledAt
}
}
}
})