From c308401f3fe3adb633ae8aa61b59c1b4cb15d059 Mon Sep 17 00:00:00 2001 From: pricelees Date: Wed, 16 Jul 2025 18:07:30 +0900 Subject: [PATCH] =?UTF-8?q?test:=20Payment=20/=20CanceledPaymentRepository?= =?UTF-8?q?=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CanceledPaymentRepositoryTest.kt | 37 +++++++ .../persistence/PaymentRepositoryTest.kt | 103 ++++++++++++++++++ src/test/resources/application.yaml | 16 +-- 3 files changed, 146 insertions(+), 10 deletions(-) create mode 100644 src/test/java/roomescape/payment/infrastructure/persistence/CanceledPaymentRepositoryTest.kt create mode 100644 src/test/java/roomescape/payment/infrastructure/persistence/PaymentRepositoryTest.kt diff --git a/src/test/java/roomescape/payment/infrastructure/persistence/CanceledPaymentRepositoryTest.kt b/src/test/java/roomescape/payment/infrastructure/persistence/CanceledPaymentRepositoryTest.kt new file mode 100644 index 00000000..1043a737 --- /dev/null +++ b/src/test/java/roomescape/payment/infrastructure/persistence/CanceledPaymentRepositoryTest.kt @@ -0,0 +1,37 @@ +package roomescape.payment.infrastructure.persistence + +import io.kotest.assertions.assertSoftly +import io.kotest.core.spec.style.FunSpec +import io.kotest.matchers.shouldBe +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest +import roomescape.util.PaymentFixture +import java.util.* + +@DataJpaTest +class CanceledPaymentRepositoryTest( + @Autowired val canceledPaymentRepository: CanceledPaymentRepository, +): FunSpec() { + init { + context("paymentKey로 CanceledPaymentEntity 조회") { + val paymentKey = "test-payment-key" + beforeTest { + PaymentFixture.createCanceled(paymentKey = paymentKey) + .also { canceledPaymentRepository.save(it) } + } + + test("정상 반환") { + canceledPaymentRepository.findByPaymentKey(paymentKey)?.let { + assertSoftly(it) { + this.paymentKey shouldBe paymentKey + } + } ?: throw AssertionError("Unexpected null value") + } + + test("null 반환") { + canceledPaymentRepository.findByPaymentKey(UUID.randomUUID().toString()) + .also { it shouldBe null } + } + } + } +} diff --git a/src/test/java/roomescape/payment/infrastructure/persistence/PaymentRepositoryTest.kt b/src/test/java/roomescape/payment/infrastructure/persistence/PaymentRepositoryTest.kt new file mode 100644 index 00000000..ab3217d0 --- /dev/null +++ b/src/test/java/roomescape/payment/infrastructure/persistence/PaymentRepositoryTest.kt @@ -0,0 +1,103 @@ +package roomescape.payment.infrastructure.persistence + +import io.kotest.assertions.assertSoftly +import io.kotest.core.spec.style.FunSpec +import io.kotest.matchers.shouldBe +import jakarta.persistence.EntityManager +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest +import roomescape.util.PaymentFixture +import roomescape.util.ReservationFixture + +@DataJpaTest +class PaymentRepositoryTest( + @Autowired val paymentRepository: PaymentRepository, + @Autowired val entityManager: EntityManager +) : FunSpec() { + + var reservationId: Long = 0L + + init { + context("existsByReservationId") { + beforeTest { + reservationId = setupReservation() + PaymentFixture.create(reservationId = reservationId) + .also { paymentRepository.save(it) } + } + + test("true") { + paymentRepository.existsByReservationId(reservationId) + .also { it shouldBe true } + } + + test("false") { + paymentRepository.existsByReservationId(reservationId + 1) + .also { it shouldBe false } + } + } + + context("findPaymentKeyByReservationId") { + lateinit var paymentKey: String + + beforeTest { + reservationId = setupReservation() + paymentKey = PaymentFixture.create(reservationId = reservationId) + .also { paymentRepository.save(it) } + .paymentKey + } + + test("정상 반환") { + paymentRepository.findPaymentKeyByReservationId(reservationId) + ?.let { it shouldBe paymentKey } + ?: throw AssertionError("Unexpected null value") + } + + test("null 반환") { + paymentRepository.findPaymentKeyByReservationId(reservationId + 1) + .also { it shouldBe null } + } + } + + context("findByPaymentKey") { + lateinit var payment: PaymentEntity + + beforeTest { + reservationId = setupReservation() + payment = PaymentFixture.create(reservationId = reservationId) + .also { paymentRepository.save(it) } + } + + test("정상 반환") { + paymentRepository.findByPaymentKey(payment.paymentKey) + ?.also { + assertSoftly(it) { + this.id shouldBe payment.id + this.orderId shouldBe payment.orderId + this.paymentKey shouldBe payment.paymentKey + this.totalAmount shouldBe payment.totalAmount + this.reservation.id shouldBe payment.reservation.id + this.approvedAt shouldBe payment.approvedAt + } + } + ?: throw AssertionError("Unexpected null value") + } + + test("null 반환") { + paymentRepository.findByPaymentKey("non-existent-key") + .also { it shouldBe null } + } + } + } + + private fun setupReservation(): Long { + return ReservationFixture.create().also { + entityManager.persist(it.member) + entityManager.persist(it.theme) + entityManager.persist(it.reservationTime) + entityManager.persist(it) + + entityManager.flush() + entityManager.clear() + }.id + } +} diff --git a/src/test/resources/application.yaml b/src/test/resources/application.yaml index 350ecfc3..9bc65da2 100644 --- a/src/test/resources/application.yaml +++ b/src/test/resources/application.yaml @@ -9,15 +9,6 @@ spring: sql: init: data-locations: - h2: - console: - enabled: true - path: /h2-console - datasource: - driver-class-name: org.h2.Driver - url: jdbc:h2:mem:database-test - username: sa - password: security: jwt: @@ -30,4 +21,9 @@ payment: api-base-url: https://api.tosspayments.com confirm-secret-key: test_gsk_docs_OaPz8L5KdmQXkzRz3y47BMw6 read-timeout: 3 - connect-timeout: 30 \ No newline at end of file + connect-timeout: 30 + +logging: + level: + org.springframework.orm.jpa: DEBUG + org.springframework.transaction: DEBUG