Skip to content

Commit

Permalink
test: 테스트 코드 유지보수
Browse files Browse the repository at this point in the history
  • Loading branch information
nove1080 committed Nov 6, 2024
1 parent f922e67 commit 9b45b76
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,4 @@ public interface PaymentDatabaseHelper {
void clean();

void saveTestData();

void setOrderId(String orderId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import com.ordertogether.team14_be.payment.persistence.repository.ProductRepository;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -27,8 +26,6 @@ public class JpaPaymentDatabaseHelper implements PaymentDatabaseHelper {
private final ProductRepository productRepository;
private final MemberRepository memberRepository;

private String orderId;

@Override
public void clean() {
jpaDatabaseCleanup.execute();
Expand All @@ -37,56 +34,158 @@ public void clean() {
@Override
@Transactional
public void saveTestData() {
if (Objects.isNull(orderId)) {
throw new IllegalStateException("orderId is not set");
}
memberRepository.save(
new Member(1L, "member1@example.com", 100000, "010-1234-5678", "member1", "Kakao"));
// Save members
memberRepository.saveAll(
List.of(
new Member(1L, "[email protected]", 1000000, "010-0000-0001", "Member01", "Kakao"),
new Member(2L, "member2@example.com", 1000000, "010-0000-0002", "Member02", "Kakao")));

// Save products
productRepository.saveAll(
List.of(
Product.builder().id(1L).name("Product 1").price(BigDecimal.valueOf(10000)).build(),
Product.builder().id(2L).name("Product 2").price(BigDecimal.valueOf(20000)).build(),
Product.builder().id(3L).name("Product 3").price(BigDecimal.valueOf(30000)).build()));

List<PaymentOrder> paymentOrders =
// Save payment orders and events
List<PaymentOrder> paymentOrders1 =
paymentOrderRepository.saveAll(
List.of(
PaymentOrder.builder()
.id(1L)
.productId(1L)
.orderId(orderId)
.orderId("test-order-id-1")
.orderName("Product 1")
.amount(BigDecimal.valueOf(10000L))
.amount(BigDecimal.valueOf(10000))
.build(),
PaymentOrder.builder()
.id(2L)
.productId(2L)
.orderId(orderId)
.orderId("test-order-id-1")
.orderName("Product 2")
.amount(BigDecimal.valueOf(20000L))
.amount(BigDecimal.valueOf(20000))
.build(),
PaymentOrder.builder()
.id(3L)
.productId(3L)
.orderId(orderId)
.orderId("test-order-id-1")
.orderName("Product 3")
.amount(BigDecimal.valueOf(30000L))
.amount(BigDecimal.valueOf(30000))
.build()));

paymentEventRepository.save(
PaymentEvent.builder()
.id(1L)
.buyerId(1L)
.orderId(orderId)
.paymentOrders(paymentOrders)
.orderId("test-order-id-1")
.paymentOrders(paymentOrders1)
.orderName("Product 1, Product 2, Product 3")
.paymentStatus(PaymentStatus.READY)
.build());
}

@Override
public void setOrderId(String orderId) {
this.orderId = orderId;
List<PaymentOrder> paymentOrders2 =
paymentOrderRepository.saveAll(
List.of(
PaymentOrder.builder()
.id(4L)
.productId(1L)
.orderId("test-order-id-2")
.orderName("Product 1")
.amount(BigDecimal.valueOf(10000))
.build(),
PaymentOrder.builder()
.id(5L)
.productId(2L)
.orderId("test-order-id-2")
.orderName("Product 2")
.amount(BigDecimal.valueOf(20000))
.build()));

paymentEventRepository.save(
PaymentEvent.builder()
.id(2L)
.buyerId(1L)
.orderId("test-order-id-2")
.paymentOrders(paymentOrders2)
.orderName("Product 1, Product 2")
.paymentStatus(PaymentStatus.SUCCESS)
.build());

List<PaymentOrder> paymentOrders3 =
paymentOrderRepository.saveAll(
List.of(
PaymentOrder.builder()
.id(6L)
.productId(1L)
.orderId("test-order-id-3")
.orderName("Product 1")
.amount(BigDecimal.valueOf(10000))
.build(),
PaymentOrder.builder()
.id(7L)
.productId(3L)
.orderId("test-order-id-3")
.orderName("Product 3")
.amount(BigDecimal.valueOf(30000))
.build()));

paymentEventRepository.save(
PaymentEvent.builder()
.id(3L)
.buyerId(1L)
.orderId("test-order-id-3")
.paymentOrders(paymentOrders3)
.orderName("Product 1, Product 3")
.paymentStatus(PaymentStatus.FAIL)
.build());

List<PaymentOrder> paymentOrders4 =
paymentOrderRepository.saveAll(
List.of(
PaymentOrder.builder()
.id(8L)
.productId(1L)
.orderId("test-order-id-4")
.orderName("Product 1")
.amount(BigDecimal.valueOf(10000))
.build()));

paymentEventRepository.save(
PaymentEvent.builder()
.id(4L)
.buyerId(2L)
.orderId("test-order-id-4")
.paymentOrders(paymentOrders4)
.orderName("Product 1")
.paymentStatus(PaymentStatus.SUCCESS)
.build());

List<PaymentOrder> paymentOrders5 =
paymentOrderRepository.saveAll(
List.of(
PaymentOrder.builder()
.id(9L)
.productId(1L)
.orderId("test-order-id-5")
.orderName("Product 1")
.amount(BigDecimal.valueOf(10000))
.build(),
PaymentOrder.builder()
.id(10L)
.productId(2L)
.orderId("test-order-id-5")
.orderName("Product 2")
.amount(BigDecimal.valueOf(20000))
.build()));

paymentEventRepository.save(
PaymentEvent.builder()
.id(5L)
.buyerId(2L)
.orderId("test-order-id-5")
.paymentOrders(paymentOrders5)
.orderName("Product 1, Product 2")
.paymentStatus(PaymentStatus.SUCCESS)
.build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ void setUp() {
pointManagementService);

paymentDatabaseHelper.clean();

paymentDatabaseHelper.setOrderId("test-order-id");
paymentDatabaseHelper.saveTestData();
}

Expand All @@ -75,7 +73,7 @@ void shouldSaveSuccessStatusWhenNormallyRequest() {
long chargeAmount = 60000L;
PaymentConfirmRequest request =
PaymentConfirmRequest.builder()
.orderId("test-order-id")
.orderId("test-order-id-1")
.paymentKey(UUID.randomUUID().toString())
.amount(chargeAmount)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ class PointManagementServiceTest {
@BeforeEach
void setUp() {
paymentDatabaseHelper.clean();

paymentDatabaseHelper.setOrderId("test-order-id");
paymentDatabaseHelper.saveTestData();
}

Expand All @@ -43,12 +41,12 @@ void shouldIncreaseSuccessWhenNormallyRequest() {
.getPoint();
Long chargeAmount =
paymentEventRepository
.findByOrderId("test-order-id")
.findByOrderId("test-order-id-1")
.orElseThrow(() -> new NoSuchElementException("PaymentEvent not found"))
.totalAmount();

// when
pointManagementService.increasePoint("test-order-id");
pointManagementService.increasePoint("test-order-id-1");

// then
int afterPoint =
Expand Down

0 comments on commit 9b45b76

Please sign in to comment.