|
| 1 | +package com.roome.domain.payment.repository; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | + |
| 5 | +import com.roome.domain.config.TestQueryDslConfig; |
| 6 | +import com.roome.domain.payment.entity.Payment; |
| 7 | +import com.roome.domain.payment.entity.PaymentLog; |
| 8 | +import com.roome.domain.payment.entity.PaymentStatus; |
| 9 | +import com.roome.domain.user.entity.Provider; |
| 10 | +import com.roome.domain.user.entity.Status; |
| 11 | +import com.roome.domain.user.entity.User; |
| 12 | +import com.roome.domain.user.repository.UserRepository; |
| 13 | +import com.roome.global.config.JpaConfig; |
| 14 | +import jakarta.persistence.EntityManager; |
| 15 | +import java.time.LocalDateTime; |
| 16 | +import java.util.List; |
| 17 | +import org.junit.jupiter.api.DisplayName; |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | +import org.springframework.beans.factory.annotation.Autowired; |
| 20 | +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; |
| 21 | +import org.springframework.context.annotation.Import; |
| 22 | +import org.springframework.data.domain.Page; |
| 23 | +import org.springframework.data.domain.PageRequest; |
| 24 | +import org.springframework.data.domain.Sort; |
| 25 | +import org.springframework.test.context.ActiveProfiles; |
| 26 | + |
| 27 | +@ActiveProfiles("test") |
| 28 | +@Import({TestQueryDslConfig.class, JpaConfig.class}) |
| 29 | +@DataJpaTest |
| 30 | +class PaymentLogRepositoryTest { |
| 31 | + |
| 32 | + @Autowired |
| 33 | + private PaymentLogRepository paymentLogRepository; |
| 34 | + |
| 35 | + @Autowired |
| 36 | + private PaymentRepository paymentRepository; |
| 37 | + |
| 38 | + @Autowired |
| 39 | + private UserRepository userRepository; |
| 40 | + |
| 41 | + @Autowired |
| 42 | + private EntityManager entityManager; |
| 43 | + |
| 44 | + @Test |
| 45 | + @DisplayName("결제 로그는 연결된 Payment를 통해 주문 건과 매칭할 수 있다.") |
| 46 | + void paymentLog_LinkedToPayment() { |
| 47 | + // given |
| 48 | + User user = userRepository.save(createUser("user@gmail.com", "provId-1")); |
| 49 | + Payment payment = paymentRepository.save(payment(user, "order-1")); |
| 50 | + paymentLogRepository.save(PaymentLog.builder() |
| 51 | + .user(user).payment(payment) |
| 52 | + .amount(5_000).earnedPoints(550).paymentKey("pk1") |
| 53 | + .build()); |
| 54 | + entityManager.flush(); |
| 55 | + entityManager.clear(); |
| 56 | + |
| 57 | + // when |
| 58 | + Page<PaymentLog> result = paymentLogRepository.findByUserWithPayment( |
| 59 | + user, PageRequest.of(0, 10, Sort.by(Sort.Direction.DESC, "createdAt"))); |
| 60 | + |
| 61 | + // then: 로그에서 연결된 결제 건의 orderId를 추적할 수 있다 |
| 62 | + List<PaymentLog> logs = result.getContent(); |
| 63 | + assertThat(logs).hasSize(1); |
| 64 | + assertThat(logs.get(0).getPayment()).isNotNull(); |
| 65 | + assertThat(logs.get(0).getPayment().getOrderId()).isEqualTo("order-1"); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + @DisplayName("payment가 연결되지 않은 기존 로그도 조회에서 누락되지 않는다 (LEFT JOIN).") |
| 70 | + void legacyLog_WithoutPayment_StillReturned() { |
| 71 | + // given: payment 연결이 없는 레거시 로그 |
| 72 | + User user = userRepository.save(createUser("legacy@gmail.com", "provId-2")); |
| 73 | + paymentLogRepository.save(PaymentLog.builder() |
| 74 | + .user(user).payment(null) |
| 75 | + .amount(1_000).earnedPoints(100).paymentKey("pk-legacy") |
| 76 | + .build()); |
| 77 | + entityManager.flush(); |
| 78 | + entityManager.clear(); |
| 79 | + |
| 80 | + // when |
| 81 | + Page<PaymentLog> result = paymentLogRepository.findByUserWithPayment( |
| 82 | + user, PageRequest.of(0, 10, Sort.by(Sort.Direction.DESC, "createdAt"))); |
| 83 | + |
| 84 | + // then |
| 85 | + assertThat(result.getContent()).hasSize(1); |
| 86 | + assertThat(result.getContent().get(0).getPayment()).isNull(); |
| 87 | + } |
| 88 | + |
| 89 | + private Payment payment(User user, String orderId) { |
| 90 | + return Payment.builder() |
| 91 | + .user(user) |
| 92 | + .orderId(orderId) |
| 93 | + .amount(5_000) |
| 94 | + .purchasedPoints(550) |
| 95 | + .status(PaymentStatus.SUCCESS) |
| 96 | + .build(); |
| 97 | + } |
| 98 | + |
| 99 | + private User createUser(String email, String providerId) { |
| 100 | + return User.builder() |
| 101 | + .email(email) |
| 102 | + .name("user") |
| 103 | + .nickname("nickname") |
| 104 | + .profileImage("profile") |
| 105 | + .provider(Provider.GOOGLE) |
| 106 | + .providerId(providerId) |
| 107 | + .status(Status.ONLINE) |
| 108 | + .lastLogin(LocalDateTime.of(2025, 1, 1, 1, 1)) |
| 109 | + .refreshToken("refToken") |
| 110 | + .build(); |
| 111 | + } |
| 112 | +} |
0 commit comments