-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 결제 실패 시 결제 상태를
ABORTED
로 변경한다. (#442)
* feat: 결제 실패 시 결제 상태를 변경한다. * style: 개행을 추가한다. * docs: README.md 에 이슈해결을 추가한다.
- Loading branch information
1 parent
76ae039
commit cf33e79
Showing
4 changed files
with
143 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
src/test/java/com/clova/anifriends/domain/payment/service/PaymentIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package com.clova.anifriends.domain.payment.service; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.catchException; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.clova.anifriends.base.BaseIntegrationTest; | ||
import com.clova.anifriends.domain.donation.Donation; | ||
import com.clova.anifriends.domain.donation.support.fixture.DonationFixture; | ||
import com.clova.anifriends.domain.payment.Payment; | ||
import com.clova.anifriends.domain.payment.dto.response.TossPaymentApiResponse; | ||
import com.clova.anifriends.domain.payment.exception.PaymentFailException; | ||
import com.clova.anifriends.domain.payment.vo.PaymentStatus; | ||
import com.clova.anifriends.domain.shelter.Shelter; | ||
import com.clova.anifriends.domain.shelter.support.ShelterFixture; | ||
import com.clova.anifriends.domain.volunteer.Volunteer; | ||
import com.clova.anifriends.domain.volunteer.support.VolunteerFixture; | ||
import com.clova.anifriends.global.exception.ExternalApiException; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
class PaymentIntegrationTest extends BaseIntegrationTest { | ||
|
||
@Autowired | ||
private PaymentService paymentService; | ||
|
||
@Nested | ||
@DisplayName("paySuccess 실행 시") | ||
class PaySuccessTest { | ||
|
||
@Test | ||
@DisplayName("성공: 결제 상태가 DONE 으로 변경") | ||
void paySuccess() { | ||
// given | ||
Shelter shelter = ShelterFixture.shelter(); | ||
Volunteer volunteer = VolunteerFixture.volunteer(); | ||
Donation donation = DonationFixture.donation(shelter, volunteer); | ||
String mockPaymentKey = "mockPaymentKey"; | ||
Payment payment = new Payment(donation); | ||
|
||
shelterRepository.save(shelter); | ||
volunteerRepository.save(volunteer); | ||
paymentRepository.save(payment); | ||
|
||
when(apiService.post(any(), any(), any())).thenReturn( | ||
new TossPaymentApiResponse("DONE")); | ||
|
||
// when | ||
paymentService.paySuccess(payment.getOrderId(), mockPaymentKey, donation.getAmount()); | ||
|
||
// then | ||
Payment updatedPayment = paymentRepository.findById(payment.getPaymentId()).get(); | ||
assertThat(updatedPayment.getStatus()).isEqualTo(PaymentStatus.DONE); | ||
} | ||
|
||
@Test | ||
@DisplayName("예외(ExternalApiException): PG Api 예외 발생 시 Payment 상태 ABORTED 로 변경") | ||
void exceptionWhenPGApiException() { | ||
// given | ||
Shelter shelter = ShelterFixture.shelter(); | ||
Volunteer volunteer = VolunteerFixture.volunteer(); | ||
Donation donation = DonationFixture.donation(shelter, volunteer); | ||
String mockPaymentKey = "mockPaymentKey"; | ||
Payment payment = new Payment(donation); | ||
|
||
shelterRepository.save(shelter); | ||
volunteerRepository.save(volunteer); | ||
paymentRepository.save(payment); | ||
|
||
when(apiService.post(any(), any(), any())).thenThrow( | ||
new ExternalApiException("errorMessage")); | ||
|
||
// when | ||
Exception exception = catchException( | ||
() -> paymentService.paySuccess(payment.getOrderId(), mockPaymentKey, | ||
donation.getAmount())); | ||
|
||
// then | ||
Payment updatedPayment = paymentRepository.findById(payment.getPaymentId()).get(); | ||
assertThat(updatedPayment.getStatus()).isEqualTo(PaymentStatus.ABORTED); | ||
assertThat(exception).isInstanceOf(ExternalApiException.class); | ||
} | ||
|
||
@Test | ||
@DisplayName("예외(PaymentFailException): 결제 실패 시 Payment 상태 ABORTED 로 변경") | ||
void exceptionWhenPaymentFail() { | ||
// given | ||
Shelter shelter = ShelterFixture.shelter(); | ||
Volunteer volunteer = VolunteerFixture.volunteer(); | ||
Donation donation = DonationFixture.donation(shelter, volunteer); | ||
String mockPaymentKey = "mockPaymentKey"; | ||
Payment payment = new Payment(donation); | ||
|
||
shelterRepository.save(shelter); | ||
volunteerRepository.save(volunteer); | ||
paymentRepository.save(payment); | ||
|
||
when(apiService.post(any(), any(), any())).thenReturn( | ||
new TossPaymentApiResponse("FAIL")); | ||
|
||
// when | ||
Exception exception = catchException( | ||
() -> paymentService.paySuccess(payment.getOrderId(), mockPaymentKey, | ||
donation.getAmount())); | ||
|
||
// then | ||
Payment updatedPayment = paymentRepository.findById(payment.getPaymentId()).get(); | ||
assertThat(updatedPayment.getStatus()).isEqualTo(PaymentStatus.ABORTED); | ||
assertThat(exception).isInstanceOf(PaymentFailException.class); | ||
} | ||
} | ||
|
||
} |