|
|
@@ -0,0 +1,115 @@
|
|
|
+package com.etotem.cfc.service;
|
|
|
+
|
|
|
+import com.etotem.cfc.entity.Coupon;
|
|
|
+import com.etotem.cfc.entity.FamilyCoupon;
|
|
|
+import com.etotem.cfc.entity.User;
|
|
|
+import com.etotem.cfc.mapper.CouponMapper;
|
|
|
+import com.etotem.cfc.mapper.FamilyCouponGrantLogMapper;
|
|
|
+import com.etotem.cfc.mapper.FamilyCouponMapper;
|
|
|
+import com.etotem.cfc.mapper.UserMapper;
|
|
|
+import org.junit.jupiter.api.BeforeEach;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+import org.junit.jupiter.api.extension.ExtendWith;
|
|
|
+import org.mockito.InjectMocks;
|
|
|
+import org.mockito.Mock;
|
|
|
+import org.mockito.junit.jupiter.MockitoExtension;
|
|
|
+
|
|
|
+import java.util.Collections;
|
|
|
+
|
|
|
+import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertNull;
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 会员订单优惠券适用范围契约:购买会员只能使用会员优惠券(applicableTo=MEMBERSHIP),
|
|
|
+ * 全品类/商品/历史空值券一律不可用;非会员订单仍保持原有 ALL 通用逻辑。
|
|
|
+ */
|
|
|
+@ExtendWith(MockitoExtension.class)
|
|
|
+public class CouponServiceMembershipScopeTest {
|
|
|
+
|
|
|
+ private static final Long USER_ID = 1L;
|
|
|
+ private static final Long FAMILY_ID = 10L;
|
|
|
+ private static final Long USER_COUPON_ID = 500L;
|
|
|
+ private static final Long COUPON_ID = 100L;
|
|
|
+
|
|
|
+ @Mock
|
|
|
+ private CouponMapper couponMapper;
|
|
|
+
|
|
|
+ @Mock
|
|
|
+ private FamilyCouponMapper familyCouponMapper;
|
|
|
+
|
|
|
+ @Mock
|
|
|
+ private FamilyCouponGrantLogMapper familyCouponGrantLogMapper;
|
|
|
+
|
|
|
+ @Mock
|
|
|
+ private UserMapper userMapper;
|
|
|
+
|
|
|
+ @Mock
|
|
|
+ private MembershipService membershipService;
|
|
|
+
|
|
|
+ @Mock
|
|
|
+ private CouponGiftService couponGiftService;
|
|
|
+
|
|
|
+ @InjectMocks
|
|
|
+ private CouponService couponService;
|
|
|
+
|
|
|
+ @BeforeEach
|
|
|
+ void setUp() {
|
|
|
+ User user = new User();
|
|
|
+ user.setId(USER_ID);
|
|
|
+ user.setFamilyId(FAMILY_ID);
|
|
|
+ when(userMapper.selectById(USER_ID)).thenReturn(user);
|
|
|
+
|
|
|
+ FamilyCoupon fc = new FamilyCoupon();
|
|
|
+ fc.setId(USER_COUPON_ID);
|
|
|
+ fc.setFamilyId(FAMILY_ID);
|
|
|
+ fc.setCouponId(COUPON_ID);
|
|
|
+ fc.setStatus("AVAILABLE");
|
|
|
+ fc.setIsGift(0);
|
|
|
+ when(familyCouponMapper.selectById(USER_COUPON_ID)).thenReturn(fc);
|
|
|
+ }
|
|
|
+
|
|
|
+ private Coupon coupon(String applicableTo) {
|
|
|
+ Coupon c = new Coupon();
|
|
|
+ c.setId(COUPON_ID);
|
|
|
+ c.setStatus("ACTIVE");
|
|
|
+ c.setType("FIXED");
|
|
|
+ c.setValue(1000);
|
|
|
+ c.setApplicableTo(applicableTo);
|
|
|
+ return c;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void membershipOrder_rejectsAllScopeCoupon() {
|
|
|
+ when(couponMapper.selectById(COUPON_ID)).thenReturn(coupon("ALL"));
|
|
|
+ assertNull(couponService.apply(USER_ID, USER_COUPON_ID, "MEMBERSHIP", 9900));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void membershipOrder_rejectsNullScopeCoupon() {
|
|
|
+ when(couponMapper.selectById(COUPON_ID)).thenReturn(coupon(null));
|
|
|
+ assertNull(couponService.apply(USER_ID, USER_COUPON_ID, "MEMBERSHIP", 9900));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void membershipOrder_rejectsProductScopeCoupon() {
|
|
|
+ when(couponMapper.selectById(COUPON_ID)).thenReturn(coupon("PRODUCT"));
|
|
|
+ assertNull(couponService.apply(USER_ID, USER_COUPON_ID, "MEMBERSHIP", 9900));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void membershipOrder_acceptsMembershipCouponForMember() {
|
|
|
+ when(membershipService.getMemberLevel(USER_ID)).thenReturn("FAMILY");
|
|
|
+ when(couponMapper.selectById(COUPON_ID)).thenReturn(coupon("MEMBERSHIP"));
|
|
|
+ assertEquals(Integer.valueOf(1000),
|
|
|
+ couponService.apply(USER_ID, USER_COUPON_ID, "MEMBERSHIP", 9900));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void productOrder_stillAcceptsAllScopeCoupon() {
|
|
|
+ when(couponMapper.selectById(COUPON_ID)).thenReturn(coupon("ALL"));
|
|
|
+ assertEquals(Integer.valueOf(1000),
|
|
|
+ couponService.apply(USER_ID, USER_COUPON_ID, "PRODUCT", 9900,
|
|
|
+ Collections.singletonList(1L)));
|
|
|
+ }
|
|
|
+}
|