longz
2026-07-17 da9b7a52dbc6fcd2173efef90528d8fccc322554
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.ruoyi.order.service.impl;
 
import com.ruoyi.other.api.domain.TCoupon;
import org.junit.jupiter.api.Test;
 
import java.math.BigDecimal;
 
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
 
class ChargingCouponSupportTest {
 
    @Test
    void fullReductionDiscountsThePrepaymentBeforeThirdPartyPay() {
        TCoupon coupon = chargingCoupon(1);
        coupon.setMeetTheConditions(new BigDecimal("3"));
        coupon.setDiscountAmount(new BigDecimal("1"));
 
        BigDecimal discount = ChargingCouponSupport.calculateUpfrontDiscount(coupon, new BigDecimal("3"));
 
        assertEquals(new BigDecimal("1.00"), discount);
        assertEquals(new BigDecimal("2.00"), new BigDecimal("3").subtract(discount));
    }
 
    @Test
    void percentageDiscountHonorsMaximumDiscount() {
        TCoupon coupon = chargingCoupon(2);
        coupon.setMeetTheConditions(new BigDecimal("10"));
        coupon.setDiscount(new BigDecimal("8"));
        coupon.setMaximumDiscountAmount(new BigDecimal("1.50"));
 
        BigDecimal discount = ChargingCouponSupport.calculateUpfrontDiscount(coupon, new BigDecimal("10"));
 
        assertEquals(new BigDecimal("1.50"), discount);
    }
 
    @Test
    void rejectsPrepaymentBelowCouponThreshold() {
        TCoupon coupon = chargingCoupon(1);
        coupon.setMeetTheConditions(new BigDecimal("3"));
        coupon.setDiscountAmount(new BigDecimal("1"));
 
        IllegalArgumentException error = assertThrows(IllegalArgumentException.class,
                () -> ChargingCouponSupport.calculateUpfrontDiscount(coupon, new BigDecimal("2.99")));
 
        assertEquals("预付金额未达到优惠券使用门槛", error.getMessage());
    }
 
    private TCoupon chargingCoupon(int preferentialMode) {
        TCoupon coupon = new TCoupon();
        coupon.setType(1);
        coupon.setPreferentialMode(preferentialMode);
        return coupon;
    }
}