|
|
@@ -0,0 +1,592 @@
|
|
|
+package com.etotem.cfc.unit;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.MybatisConfiguration;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
|
|
+import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
|
|
+import com.etotem.cfc.entity.Coupon;
|
|
|
+import com.etotem.cfc.entity.CouponGiftTransfer;
|
|
|
+import com.etotem.cfc.entity.CouponShare;
|
|
|
+import com.etotem.cfc.entity.FamilyCoupon;
|
|
|
+import com.etotem.cfc.entity.FamilyCouponGrantLog;
|
|
|
+import com.etotem.cfc.entity.User;
|
|
|
+import com.etotem.cfc.mapper.CouponMapper;
|
|
|
+import com.etotem.cfc.mapper.CouponShareMapper;
|
|
|
+import com.etotem.cfc.mapper.FamilyCouponGrantLogMapper;
|
|
|
+import com.etotem.cfc.mapper.FamilyCouponMapper;
|
|
|
+import com.etotem.cfc.mapper.UserMapper;
|
|
|
+import com.etotem.cfc.service.CouponGiftService;
|
|
|
+import com.etotem.cfc.service.CouponShareService;
|
|
|
+import com.etotem.cfc.service.NotificationDispatchService;
|
|
|
+import org.apache.ibatis.builder.MapperBuilderAssistant;
|
|
|
+import org.junit.jupiter.api.BeforeAll;
|
|
|
+import org.junit.jupiter.api.BeforeEach;
|
|
|
+import org.junit.jupiter.api.DisplayName;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+import org.mockito.ArgumentCaptor;
|
|
|
+
|
|
|
+import java.lang.reflect.Field;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertFalse;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertNull;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
+import static org.mockito.ArgumentMatchers.any;
|
|
|
+import static org.mockito.ArgumentMatchers.anyLong;
|
|
|
+import static org.mockito.ArgumentMatchers.anyMap;
|
|
|
+import static org.mockito.ArgumentMatchers.anyString;
|
|
|
+import static org.mockito.ArgumentMatchers.eq;
|
|
|
+import static org.mockito.ArgumentMatchers.isNull;
|
|
|
+import static org.mockito.Mockito.mock;
|
|
|
+import static org.mockito.Mockito.never;
|
|
|
+import static org.mockito.Mockito.times;
|
|
|
+import static org.mockito.Mockito.verify;
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 契约级单测:会员券「分享转赠」后端(ARCH-CFC-COUPON-SHARE-GIFT-001 §10.1 十场景)。
|
|
|
+ *
|
|
|
+ * <p>契约溯源:{@code docs/superpowers/api/API_REFERENCE.md} 会员券分享转赠
|
|
|
+ * {@code /api/coupon/gift/share/*}(§5 create/detail/claim/revoke/list);
|
|
|
+ * 实现承载:{@link CouponShareService}(create 幂等 + claim 条件更新防重领 + 复用
|
|
|
+ * {@link CouponGiftService#commitTransfer})。纯 Mockito 无 Spring 上下文 / 无 DB /
|
|
|
+ * 无外部服务,风格参考 {@code ShopCouponClaimContractTest}。</p>
|
|
|
+ *
|
|
|
+ * <p><b>覆盖流</b>:</p>
|
|
|
+ * <ul>
|
|
|
+ * <li><b>create</b>:正常(token/path/expireAt,insert 1 行 PENDING,expireAt=now+7 天);
|
|
|
+ * 幂等(已有 PENDING → 返回既有 token 不新增);校验(券不属我 / 非 MEMBERSHIP /
|
|
|
+ * 非 ACTIVE / 已过期 → 对应文案);</li>
|
|
|
+ * <li><b>detail</b>:正常(PENDING → 券信息 + claimable=true,匿名可调);失效态
|
|
|
+ * (CLAIMED/REVOKED/EXPIRED 懒判定 / token 不存在 → claimable=false + 对应状态);</li>
|
|
|
+ * <li><b>claim</b>:成功(条件更新 1 行 → commitTransfer 委托参数 giftLevel=0/claimMethod=SHARE/
|
|
|
+ * shareToken 回写,通知赠送人与 grant log 各 1 次);并发防重领(条件更新 0 行 →
|
|
|
+ * 「该分享已被领取」,transfer 不重复写);校验(自己分享/同家庭/已撤回/已过期/
|
|
|
+ * 券失效/领取人无家庭 → 各自文案);</li>
|
|
|
+ * <li><b>revoke</b>:PENDING→REVOKED(不动券实例);CLAIMED 终态不可撤回;非赠送人无权;</li>
|
|
|
+ * <li><b>list</b>:仅本人记录,created_at 倒序。</li>
|
|
|
+ * </ul>
|
|
|
+ */
|
|
|
+class CouponShareClaimContractTest {
|
|
|
+
|
|
|
+ private static final String TOKEN = "CS-CONTRACT001";
|
|
|
+ private static final String AVATAR = "http://avatar/";
|
|
|
+
|
|
|
+ /** 纯 Mockito 无 Spring:为 Lambda 包装器预注册 TableInfo(否则 set()/getSqlSegment() 报 lambda cache 异常) */
|
|
|
+ @BeforeAll
|
|
|
+ static void initMpTableInfo() {
|
|
|
+ MapperBuilderAssistant assistant = new MapperBuilderAssistant(new MybatisConfiguration(), "");
|
|
|
+ TableInfoHelper.initTableInfo(assistant, CouponShare.class);
|
|
|
+ TableInfoHelper.initTableInfo(assistant, Coupon.class);
|
|
|
+ TableInfoHelper.initTableInfo(assistant, CouponGiftTransfer.class);
|
|
|
+ TableInfoHelper.initTableInfo(assistant, FamilyCoupon.class);
|
|
|
+ TableInfoHelper.initTableInfo(assistant, FamilyCouponGrantLog.class);
|
|
|
+ TableInfoHelper.initTableInfo(assistant, User.class);
|
|
|
+ }
|
|
|
+
|
|
|
+ private CouponShareService service;
|
|
|
+ private CouponShareMapper couponShareMapper;
|
|
|
+ private FamilyCouponMapper familyCouponMapper;
|
|
|
+ private CouponMapper couponMapper;
|
|
|
+ private UserMapper userMapper;
|
|
|
+ private CouponGiftService couponGiftService;
|
|
|
+ private NotificationDispatchService notificationDispatchService;
|
|
|
+ private FamilyCouponGrantLogMapper familyCouponGrantLogMapper;
|
|
|
+
|
|
|
+ @BeforeEach
|
|
|
+ void setUp() throws Exception {
|
|
|
+ service = new CouponShareService();
|
|
|
+ couponShareMapper = mock(CouponShareMapper.class);
|
|
|
+ familyCouponMapper = mock(FamilyCouponMapper.class);
|
|
|
+ couponMapper = mock(CouponMapper.class);
|
|
|
+ userMapper = mock(UserMapper.class);
|
|
|
+ couponGiftService = mock(CouponGiftService.class);
|
|
|
+ notificationDispatchService = mock(NotificationDispatchService.class);
|
|
|
+ familyCouponGrantLogMapper = mock(FamilyCouponGrantLogMapper.class);
|
|
|
+
|
|
|
+ inject(service, "couponShareMapper", couponShareMapper);
|
|
|
+ inject(service, "familyCouponMapper", familyCouponMapper);
|
|
|
+ inject(service, "couponMapper", couponMapper);
|
|
|
+ inject(service, "userMapper", userMapper);
|
|
|
+ inject(service, "couponGiftService", couponGiftService);
|
|
|
+ inject(service, "notificationDispatchService", notificationDispatchService);
|
|
|
+ inject(service, "familyCouponGrantLogMapper", familyCouponGrantLogMapper);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void inject(Object target, String field, Object value) throws Exception {
|
|
|
+ Field f = target.getClass().getDeclaredField(field);
|
|
|
+ f.setAccessible(true);
|
|
|
+ f.set(target, value);
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 桩数据 ====================
|
|
|
+
|
|
|
+ /** 用户:id=1 赠送人(家庭5)/ 其他 id 领取人(familyId 可指定) */
|
|
|
+ private User user(Long id, Long familyId) {
|
|
|
+ User u = new User();
|
|
|
+ u.setId(id);
|
|
|
+ u.setFamilyId(familyId);
|
|
|
+ u.setNickname(id == 1L ? "赠送人" : "领取人" + id);
|
|
|
+ u.setAvatar(AVATAR + id);
|
|
|
+ return u;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 有效期内、ACTIVE、MEMBERSHIP 的会员券模板 */
|
|
|
+ private Coupon membershipCoupon() {
|
|
|
+ Coupon c = new Coupon();
|
|
|
+ c.setId(100L);
|
|
|
+ c.setName("会员升级券");
|
|
|
+ c.setType("CASH");
|
|
|
+ c.setStatus("ACTIVE");
|
|
|
+ c.setApplicableTo("MEMBERSHIP");
|
|
|
+ c.setValue(2000);
|
|
|
+ c.setMinSpend(0);
|
|
|
+ c.setValidFrom(new Date(System.currentTimeMillis() - 86400000L * 7));
|
|
|
+ c.setValidUntil(new Date(System.currentTimeMillis() + 86400000L * 30));
|
|
|
+ return c;
|
|
|
+ }
|
|
|
+
|
|
|
+ private FamilyCoupon familyCoupon(Long id, Long familyId, Long couponId) {
|
|
|
+ FamilyCoupon fc = new FamilyCoupon();
|
|
|
+ fc.setId(id);
|
|
|
+ fc.setFamilyId(familyId);
|
|
|
+ fc.setCouponId(couponId);
|
|
|
+ fc.setStatus("AVAILABLE");
|
|
|
+ fc.setIsGift(0);
|
|
|
+ return fc;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 分享行:赠送人=1(家庭5),familyCoupon=500,coupon=100,expireAt=7 天后 */
|
|
|
+ private CouponShare pendingShare(String token) {
|
|
|
+ CouponShare s = new CouponShare();
|
|
|
+ s.setId(10L);
|
|
|
+ s.setShareToken(token);
|
|
|
+ s.setGiverUserId(1L);
|
|
|
+ s.setGiverFamilyId(5L);
|
|
|
+ s.setFamilyCouponId(500L);
|
|
|
+ s.setCouponId(100L);
|
|
|
+ s.setStatus("PENDING");
|
|
|
+ s.setExpireAt(new Date(System.currentTimeMillis() + 86400000L * 7));
|
|
|
+ s.setCreatedAt(new Date());
|
|
|
+ return s;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String createError(Long giverUserId, Long familyCouponId) {
|
|
|
+ try {
|
|
|
+ service.create(giverUserId, familyCouponId);
|
|
|
+ return null;
|
|
|
+ } catch (IllegalArgumentException e) {
|
|
|
+ return e.getMessage();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private String detailError(String token) {
|
|
|
+ try {
|
|
|
+ service.detail(token, null);
|
|
|
+ return null;
|
|
|
+ } catch (IllegalArgumentException e) {
|
|
|
+ return e.getMessage();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private String claimError(Long userId, String token) {
|
|
|
+ try {
|
|
|
+ service.claim(userId, token);
|
|
|
+ return null;
|
|
|
+ } catch (IllegalArgumentException e) {
|
|
|
+ return e.getMessage();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private String revokeError(Long userId, String token) {
|
|
|
+ try {
|
|
|
+ service.revoke(userId, token);
|
|
|
+ return null;
|
|
|
+ } catch (IllegalArgumentException e) {
|
|
|
+ return e.getMessage();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 场景1-3:create ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("create正常:返回 token/sharePath/expireAt,insert 1 行 PENDING,expireAt=now+7天")
|
|
|
+ void create_正常() {
|
|
|
+ when(userMapper.selectById(1L)).thenReturn(user(1L, 5L));
|
|
|
+ when(couponShareMapper.selectOne(any(Wrapper.class))).thenReturn(null);
|
|
|
+ when(familyCouponMapper.selectById(500L)).thenReturn(familyCoupon(500L, 5L, 100L));
|
|
|
+ when(couponMapper.selectById(100L)).thenReturn(membershipCoupon());
|
|
|
+ when(couponShareMapper.insert(any(CouponShare.class))).thenAnswer(inv -> {
|
|
|
+ CouponShare s = inv.getArgument(0);
|
|
|
+ s.setId(10L);
|
|
|
+ return 1;
|
|
|
+ });
|
|
|
+
|
|
|
+ Map<String, Object> data = service.create(1L, 500L);
|
|
|
+
|
|
|
+ String token = (String) data.get("shareToken");
|
|
|
+ assertNotNull(token, "应生成 shareToken");
|
|
|
+ assertTrue(((String) data.get("sharePath")).startsWith("/pages/coupon/share-claim?shareToken="),
|
|
|
+ "sharePath 应指向落地领取页并携带 token");
|
|
|
+ assertNotNull(data.get("expireAt"), "应返回失效时间");
|
|
|
+
|
|
|
+ ArgumentCaptor<CouponShare> captor = ArgumentCaptor.forClass(CouponShare.class);
|
|
|
+ verify(couponShareMapper).insert(captor.capture());
|
|
|
+ CouponShare inserted = captor.getValue();
|
|
|
+ assertEquals("PENDING", inserted.getStatus(), "新分享行状态应为 PENDING");
|
|
|
+ assertEquals(Long.valueOf(1L), inserted.getGiverUserId());
|
|
|
+ assertEquals(Long.valueOf(5L), inserted.getGiverFamilyId());
|
|
|
+ assertEquals(Long.valueOf(500L), inserted.getFamilyCouponId());
|
|
|
+ assertEquals(Long.valueOf(100L), inserted.getCouponId());
|
|
|
+ long diff = inserted.getExpireAt().getTime() - System.currentTimeMillis();
|
|
|
+ long expected = CouponShareService.DEFAULT_EXPIRE_DAYS * 86400000L;
|
|
|
+ assertTrue(diff > expected - 60000L && diff < expected + 60000L, "expireAt 应为 now + 7 天");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("create幂等:同 giver+familyCouponId 已有 PENDING → 返回既有 token,不新增行")
|
|
|
+ void create_幂等() {
|
|
|
+ when(userMapper.selectById(1L)).thenReturn(user(1L, 5L));
|
|
|
+ CouponShare existing = pendingShare("CS-EXISTING001");
|
|
|
+ when(couponShareMapper.selectOne(any(Wrapper.class))).thenReturn(existing);
|
|
|
+
|
|
|
+ Map<String, Object> data = service.create(1L, 500L);
|
|
|
+
|
|
|
+ assertEquals("CS-EXISTING001", data.get("shareToken"), "幂等命中应返回既有 token");
|
|
|
+ verify(couponShareMapper, never()).insert(any(CouponShare.class));
|
|
|
+ verify(familyCouponMapper, never()).selectById(anyLong());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("create校验-1:券不属于我(familyId 不符)→ 该优惠券不可用或不属于你")
|
|
|
+ void create_校验_券不属于我() {
|
|
|
+ when(userMapper.selectById(1L)).thenReturn(user(1L, 5L));
|
|
|
+ when(couponShareMapper.selectOne(any(Wrapper.class))).thenReturn(null);
|
|
|
+ when(familyCouponMapper.selectById(500L)).thenReturn(familyCoupon(500L, 99L, 100L));
|
|
|
+
|
|
|
+ assertEquals(CouponShareService.MSG_COUPON_UNAVAILABLE, createError(1L, 500L));
|
|
|
+ verify(couponShareMapper, never()).insert(any(CouponShare.class));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("create校验-2:非 MEMBERSHIP → 仅会员专属券可分享")
|
|
|
+ void create_校验_非会员券() {
|
|
|
+ when(userMapper.selectById(1L)).thenReturn(user(1L, 5L));
|
|
|
+ when(couponShareMapper.selectOne(any(Wrapper.class))).thenReturn(null);
|
|
|
+ when(familyCouponMapper.selectById(500L)).thenReturn(familyCoupon(500L, 5L, 100L));
|
|
|
+ Coupon coupon = membershipCoupon();
|
|
|
+ coupon.setApplicableTo("ALL");
|
|
|
+ when(couponMapper.selectById(100L)).thenReturn(coupon);
|
|
|
+
|
|
|
+ assertEquals(CouponShareService.MSG_ONLY_MEMBERSHIP, createError(1L, 500L));
|
|
|
+ verify(couponShareMapper, never()).insert(any(CouponShare.class));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("create校验-3:非 ACTIVE → 优惠券已失效")
|
|
|
+ void create_校验_券已失效() {
|
|
|
+ when(userMapper.selectById(1L)).thenReturn(user(1L, 5L));
|
|
|
+ when(couponShareMapper.selectOne(any(Wrapper.class))).thenReturn(null);
|
|
|
+ when(familyCouponMapper.selectById(500L)).thenReturn(familyCoupon(500L, 5L, 100L));
|
|
|
+ Coupon coupon = membershipCoupon();
|
|
|
+ coupon.setStatus("DISABLED");
|
|
|
+ when(couponMapper.selectById(100L)).thenReturn(coupon);
|
|
|
+
|
|
|
+ assertEquals(CouponShareService.MSG_COUPON_INVALID, createError(1L, 500L));
|
|
|
+ verify(couponShareMapper, never()).insert(any(CouponShare.class));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("create校验-4:已过券有效期 → 优惠券已过期")
|
|
|
+ void create_校验_券已过期() {
|
|
|
+ when(userMapper.selectById(1L)).thenReturn(user(1L, 5L));
|
|
|
+ when(couponShareMapper.selectOne(any(Wrapper.class))).thenReturn(null);
|
|
|
+ when(familyCouponMapper.selectById(500L)).thenReturn(familyCoupon(500L, 5L, 100L));
|
|
|
+ Coupon coupon = membershipCoupon();
|
|
|
+ coupon.setValidUntil(new Date(System.currentTimeMillis() - 86400000L));
|
|
|
+ when(couponMapper.selectById(100L)).thenReturn(coupon);
|
|
|
+
|
|
|
+ assertEquals(CouponShareService.MSG_COUPON_EXPIRED, createError(1L, 500L));
|
|
|
+ verify(couponShareMapper, never()).insert(any(CouponShare.class));
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 场景4-5:detail ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("detail正常:PENDING → 返回券信息 + claimable=true(匿名可调 viewerUserId=null)")
|
|
|
+ void detail_正常() {
|
|
|
+ CouponShare s = pendingShare(TOKEN);
|
|
|
+ when(couponShareMapper.selectOne(any(Wrapper.class))).thenReturn(s);
|
|
|
+ when(couponMapper.selectById(100L)).thenReturn(membershipCoupon());
|
|
|
+ when(userMapper.selectById(1L)).thenReturn(user(1L, 5L));
|
|
|
+
|
|
|
+ Map<String, Object> d = service.detail(TOKEN, null);
|
|
|
+
|
|
|
+ assertEquals(TOKEN, d.get("shareToken"));
|
|
|
+ assertEquals("PENDING", d.get("status"));
|
|
|
+ assertEquals(Boolean.TRUE, d.get("claimable"), "PENDING 且未过期且券可用 → 可领取");
|
|
|
+ assertEquals("会员升级券", d.get("couponName"));
|
|
|
+ assertEquals(Integer.valueOf(2000), d.get("couponValue"));
|
|
|
+ assertEquals("CASH", d.get("couponType"));
|
|
|
+ assertEquals(Integer.valueOf(0), d.get("minSpend"));
|
|
|
+ assertEquals("赠送人", d.get("giverNickname"));
|
|
|
+ assertEquals(AVATAR + "1", d.get("giverAvatar"));
|
|
|
+ assertNotNull(d.get("validUntil"));
|
|
|
+ assertNotNull(d.get("expireAt"));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("detail失效态-1:CLAIMED → claimable=false,status=CLAIMED")
|
|
|
+ void detail_失效态_已领取() {
|
|
|
+ CouponShare s = pendingShare(TOKEN);
|
|
|
+ s.setStatus("CLAIMED");
|
|
|
+ when(couponShareMapper.selectOne(any(Wrapper.class))).thenReturn(s);
|
|
|
+ when(couponMapper.selectById(100L)).thenReturn(membershipCoupon());
|
|
|
+ when(userMapper.selectById(1L)).thenReturn(user(1L, 5L));
|
|
|
+
|
|
|
+ Map<String, Object> d = service.detail(TOKEN, null);
|
|
|
+ assertEquals("CLAIMED", d.get("status"));
|
|
|
+ assertEquals(Boolean.FALSE, d.get("claimable"));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("detail失效态-2:REVOKED → claimable=false,status=REVOKED")
|
|
|
+ void detail_失效态_已撤回() {
|
|
|
+ CouponShare s = pendingShare(TOKEN);
|
|
|
+ s.setStatus("REVOKED");
|
|
|
+ when(couponShareMapper.selectOne(any(Wrapper.class))).thenReturn(s);
|
|
|
+ when(couponMapper.selectById(100L)).thenReturn(membershipCoupon());
|
|
|
+ when(userMapper.selectById(1L)).thenReturn(user(1L, 5L));
|
|
|
+
|
|
|
+ Map<String, Object> d = service.detail(TOKEN, null);
|
|
|
+ assertEquals("REVOKED", d.get("status"));
|
|
|
+ assertEquals(Boolean.FALSE, d.get("claimable"));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("detail失效态-3:PENDING 但已过 expireAt → 懒判定 EXPIRED,claimable=false")
|
|
|
+ void detail_失效态_过期懒判定() {
|
|
|
+ CouponShare s = pendingShare(TOKEN);
|
|
|
+ s.setExpireAt(new Date(System.currentTimeMillis() - 60000L)); // 1 分钟前过期
|
|
|
+ when(couponShareMapper.selectOne(any(Wrapper.class))).thenReturn(s);
|
|
|
+ when(couponMapper.selectById(100L)).thenReturn(membershipCoupon());
|
|
|
+ when(userMapper.selectById(1L)).thenReturn(user(1L, 5L));
|
|
|
+
|
|
|
+ Map<String, Object> d = service.detail(TOKEN, null);
|
|
|
+ assertEquals("EXPIRED", d.get("status"), "PENDING 且过期 → 展示 EXPIRED(懒判定)");
|
|
|
+ assertEquals(Boolean.FALSE, d.get("claimable"));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("detail失效态-4:token 无记录 → 分享不存在或已失效")
|
|
|
+ void detail_失效态_不存在() {
|
|
|
+ when(couponShareMapper.selectOne(any(Wrapper.class))).thenReturn(null);
|
|
|
+ assertEquals(CouponShareService.MSG_SHARE_NOT_FOUND, detailError("CS-NOT-FOUND"));
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 场景6-8:claim ====================
|
|
|
+
|
|
|
+ /** claim 成功前置:分享可领取(赠送人1/家庭5,领取人2/家庭6,券在赠送人名下且可用) */
|
|
|
+ private void stubClaimable() {
|
|
|
+ when(couponShareMapper.selectOne(any(Wrapper.class))).thenReturn(pendingShare(TOKEN));
|
|
|
+ when(userMapper.selectById(2L)).thenReturn(user(2L, 6L));
|
|
|
+ when(couponMapper.selectById(100L)).thenReturn(membershipCoupon());
|
|
|
+ when(familyCouponMapper.selectById(500L)).thenReturn(familyCoupon(500L, 5L, 100L));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("claim成功:条件更新 1 行 → 委托 commitTransfer(giftLevel=0,SHARE,token) + 通知赠送人 + grant log 各 1 次")
|
|
|
+ void claim_成功() {
|
|
|
+ stubClaimable();
|
|
|
+ when(couponShareMapper.update(isNull(), any(Wrapper.class))).thenReturn(1);
|
|
|
+ when(couponGiftService.commitTransfer(1L, 5L, 2L, 6L, 100L, 500L,
|
|
|
+ CouponShareService.SHARE_GIFT_LEVEL, "SHARE", TOKEN, null)).thenReturn(300L);
|
|
|
+
|
|
|
+ Long transferId = service.claim(2L, TOKEN);
|
|
|
+
|
|
|
+ assertEquals(Long.valueOf(300L), transferId, "应返回 commitTransfer 生成的 transferId");
|
|
|
+ // 条件更新恰好执行 1 次(防重领原子门)
|
|
|
+ verify(couponShareMapper).update(isNull(), any(Wrapper.class));
|
|
|
+ // 委托参数:giftLevel=0、claimMethod=SHARE、shareToken 回写、receiver=领取人家庭
|
|
|
+ verify(couponGiftService).commitTransfer(1L, 5L, 2L, 6L, 100L, 500L,
|
|
|
+ CouponShareService.SHARE_GIFT_LEVEL, "SHARE", TOKEN, null);
|
|
|
+ // 通知赠送人 1 次(模板 coupon_gift,目标 share.giverUserId=1)
|
|
|
+ verify(notificationDispatchService, times(1)).sendByTemplate(anyString(), eq(1L),
|
|
|
+ anyMap(), anyString(), anyLong(), anyString(), anyString());
|
|
|
+ // grant log 1 次(GIFT_TRANSFER, source=share:shareId)
|
|
|
+ ArgumentCaptor<FamilyCouponGrantLog> logCaptor = ArgumentCaptor.forClass(FamilyCouponGrantLog.class);
|
|
|
+ verify(familyCouponGrantLogMapper).insert(logCaptor.capture());
|
|
|
+ assertEquals("GIFT_TRANSFER", logCaptor.getValue().getGrantType());
|
|
|
+ assertEquals("share:10", logCaptor.getValue().getSource(), "分享链路审计 source=share:shareId");
|
|
|
+ assertEquals(Long.valueOf(6L), logCaptor.getValue().getFamilyId(), "流水落接收方家庭");
|
|
|
+ assertEquals(Long.valueOf(100L), logCaptor.getValue().getCouponId());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("claim并发防重领:条件更新影响 0 行(第二次/已被领走)→ 该分享已被领取,不重复写 transfer")
|
|
|
+ void claim_并发防重领() {
|
|
|
+ stubClaimable();
|
|
|
+ when(couponShareMapper.update(isNull(), any(Wrapper.class))).thenReturn(0); // 并发第二个请求
|
|
|
+
|
|
|
+ assertEquals(CouponShareService.MSG_ALREADY_CLAIMED, claimError(2L, TOKEN));
|
|
|
+ verify(couponGiftService, never()).commitTransfer(any(), any(), any(), any(),
|
|
|
+ any(), any(), any(), any(), any(), any());
|
|
|
+ verify(familyCouponGrantLogMapper, never()).insert(any(FamilyCouponGrantLog.class));
|
|
|
+ verify(notificationDispatchService, never()).sendByTemplate(anyString(), anyLong(),
|
|
|
+ anyMap(), anyString(), anyLong(), anyString(), anyString());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("claim校验-1:领取人=赠送人 → 不能领取自己分享的券")
|
|
|
+ void claim_校验_自己分享() {
|
|
|
+ CouponShare s = pendingShare(TOKEN);
|
|
|
+ when(couponShareMapper.selectOne(any(Wrapper.class))).thenReturn(s);
|
|
|
+ when(userMapper.selectById(1L)).thenReturn(user(1L, 5L));
|
|
|
+
|
|
|
+ assertEquals(CouponShareService.MSG_SELF_CLAIM, claimError(1L, TOKEN));
|
|
|
+ verify(couponShareMapper, never()).update(isNull(), any(Wrapper.class));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("claim校验-2:同家庭(领取人家庭=赠送人家庭)→ 不能领取同家庭成员的券")
|
|
|
+ void claim_校验_同家庭() {
|
|
|
+ CouponShare s = pendingShare(TOKEN);
|
|
|
+ when(couponShareMapper.selectOne(any(Wrapper.class))).thenReturn(s);
|
|
|
+ when(userMapper.selectById(2L)).thenReturn(user(2L, 5L)); // 与赠送人同家庭
|
|
|
+
|
|
|
+ assertEquals(CouponShareService.MSG_SAME_FAMILY, claimError(2L, TOKEN));
|
|
|
+ verify(couponShareMapper, never()).update(isNull(), any(Wrapper.class));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("claim校验-3:已撤回 → 该分享已撤回")
|
|
|
+ void claim_校验_已撤回() {
|
|
|
+ CouponShare s = pendingShare(TOKEN);
|
|
|
+ s.setStatus("REVOKED");
|
|
|
+ when(couponShareMapper.selectOne(any(Wrapper.class))).thenReturn(s);
|
|
|
+ when(userMapper.selectById(2L)).thenReturn(user(2L, 6L));
|
|
|
+
|
|
|
+ assertEquals(CouponShareService.MSG_ALREADY_REVOKED, claimError(2L, TOKEN));
|
|
|
+ verify(couponShareMapper, never()).update(isNull(), any(Wrapper.class));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("claim校验-4:已过期(PENDING 且过 expireAt)→ 该分享已过期")
|
|
|
+ void claim_校验_已过期() {
|
|
|
+ CouponShare s = pendingShare(TOKEN);
|
|
|
+ s.setExpireAt(new Date(System.currentTimeMillis() - 60000L));
|
|
|
+ when(couponShareMapper.selectOne(any(Wrapper.class))).thenReturn(s);
|
|
|
+ when(userMapper.selectById(2L)).thenReturn(user(2L, 6L));
|
|
|
+
|
|
|
+ assertEquals(CouponShareService.MSG_SHARE_EXPIRED, claimError(2L, TOKEN));
|
|
|
+ verify(couponShareMapper, never()).update(isNull(), any(Wrapper.class));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("claim校验-5:券模板失效 → 优惠券已失效")
|
|
|
+ void claim_校验_券失效() {
|
|
|
+ stubClaimable();
|
|
|
+ Coupon coupon = membershipCoupon();
|
|
|
+ coupon.setStatus("DISABLED");
|
|
|
+ when(couponMapper.selectById(100L)).thenReturn(coupon);
|
|
|
+
|
|
|
+ assertEquals(CouponShareService.MSG_COUPON_INVALID, claimError(2L, TOKEN));
|
|
|
+ verify(couponShareMapper, never()).update(isNull(), any(Wrapper.class));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("claim校验-6:领取人无家庭(familyId 空)→ 领取失败,请联系客服")
|
|
|
+ void claim_校验_无家庭() {
|
|
|
+ CouponShare s = pendingShare(TOKEN);
|
|
|
+ when(couponShareMapper.selectOne(any(Wrapper.class))).thenReturn(s);
|
|
|
+ when(userMapper.selectById(2L)).thenReturn(user(2L, null)); // 理论兜底:登录用户必有家庭
|
|
|
+
|
|
|
+ assertEquals(CouponShareService.MSG_CLAIM_FAILED, claimError(2L, TOKEN));
|
|
|
+ verify(couponShareMapper, never()).update(isNull(), any(Wrapper.class));
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 场景9:revoke ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("revoke:PENDING → REVOKED(条件更新 1 行),不动券实例")
|
|
|
+ void revoke_正常() {
|
|
|
+ when(couponShareMapper.selectOne(any(Wrapper.class))).thenReturn(pendingShare(TOKEN));
|
|
|
+ when(couponShareMapper.update(any(CouponShare.class), any(Wrapper.class))).thenReturn(1);
|
|
|
+
|
|
|
+ service.revoke(1L, TOKEN);
|
|
|
+
|
|
|
+ ArgumentCaptor<CouponShare> captor = ArgumentCaptor.forClass(CouponShare.class);
|
|
|
+ verify(couponShareMapper).update(captor.capture(), any(Wrapper.class));
|
|
|
+ assertEquals("REVOKED", captor.getValue().getStatus(), "仅置 status=REVOKED");
|
|
|
+ verify(familyCouponMapper, never()).updateById(any(FamilyCoupon.class));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("revoke:CLAIMED 终态 → 该分享已被领取,无法撤回")
|
|
|
+ void revoke_已领取不可撤回() {
|
|
|
+ CouponShare s = pendingShare(TOKEN);
|
|
|
+ s.setStatus("CLAIMED");
|
|
|
+ when(couponShareMapper.selectOne(any(Wrapper.class))).thenReturn(s);
|
|
|
+
|
|
|
+ assertEquals(CouponShareService.MSG_CLAIMED_CANNOT_REVOKE, revokeError(1L, TOKEN));
|
|
|
+ verify(couponShareMapper, never()).update(any(CouponShare.class), any(Wrapper.class));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("revoke:非赠送人 → 无权操作该分享")
|
|
|
+ void revoke_非赠送人() {
|
|
|
+ when(couponShareMapper.selectOne(any(Wrapper.class))).thenReturn(pendingShare(TOKEN));
|
|
|
+
|
|
|
+ assertEquals(CouponShareService.MSG_NO_PERMISSION, revokeError(2L, TOKEN));
|
|
|
+ verify(couponShareMapper, never()).update(any(CouponShare.class), any(Wrapper.class));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("revoke:已 REVOKED → 该分享已撤回(幂等兜底)")
|
|
|
+ void revoke_已撤回() {
|
|
|
+ CouponShare s = pendingShare(TOKEN);
|
|
|
+ s.setStatus("REVOKED");
|
|
|
+ when(couponShareMapper.selectOne(any(Wrapper.class))).thenReturn(s);
|
|
|
+
|
|
|
+ assertEquals(CouponShareService.MSG_ALREADY_REVOKED, revokeError(1L, TOKEN));
|
|
|
+ verify(couponShareMapper, never()).update(any(CouponShare.class), any(Wrapper.class));
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 场景10:list ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("list:仅返回本人分享记录,created_at 倒序,含领取人昵称")
|
|
|
+ void list_倒序() {
|
|
|
+ CouponShare newer = pendingShare("CS-LIST-A"); // 10s 前
|
|
|
+ newer.setCreatedAt(new Date(System.currentTimeMillis() - 10000L));
|
|
|
+ CouponShare older = pendingShare("CS-LIST-B"); // 60s 前
|
|
|
+ older.setCreatedAt(new Date(System.currentTimeMillis() - 60000L));
|
|
|
+ older.setClaimUserId(2L);
|
|
|
+ when(couponShareMapper.selectList(any(Wrapper.class))).thenReturn(Arrays.asList(newer, older));
|
|
|
+ when(couponMapper.selectById(100L)).thenReturn(membershipCoupon());
|
|
|
+ when(userMapper.selectById(2L)).thenReturn(user(2L, 6L));
|
|
|
+
|
|
|
+ List<Map<String, Object>> list = service.list(1L);
|
|
|
+
|
|
|
+ assertEquals(2, list.size());
|
|
|
+ assertEquals("CS-LIST-A", list.get(0).get("shareToken"), "列表应按 created_at 倒序");
|
|
|
+ assertEquals("CS-LIST-B", list.get(1).get("shareToken"));
|
|
|
+ assertEquals("会员升级券", list.get(0).get("couponName"));
|
|
|
+ assertEquals(Integer.valueOf(2000), list.get(0).get("couponValue"));
|
|
|
+ assertNotNull(list.get(0).get("expireAt"));
|
|
|
+ assertNotNull(list.get(0).get("createdAt"));
|
|
|
+ assertNull(list.get(0).get("claimNickname"), "未领取行 claimNickname 为 null");
|
|
|
+ assertEquals("领取人2", list.get(1).get("claimNickname"));
|
|
|
+
|
|
|
+ // 查询条件:giver_user_id=本人 + orderByDesc(createdAt)
|
|
|
+ ArgumentCaptor<Wrapper<CouponShare>> wrapperCaptor = ArgumentCaptor.forClass(Wrapper.class);
|
|
|
+ verify(couponShareMapper).selectList(wrapperCaptor.capture());
|
|
|
+ String sql = wrapperCaptor.getValue().getSqlSegment().toLowerCase();
|
|
|
+ assertTrue(sql.contains("giver_user_id"), "list 应按赠送人过滤");
|
|
|
+ assertTrue(sql.contains("desc"), "list 应按 created_at 倒序(ORDER BY ... DESC)");
|
|
|
+ }
|
|
|
+}
|