|
|
@@ -0,0 +1,469 @@
|
|
|
+package com.etotem.cfc.unit;
|
|
|
+
|
|
|
+import com.etotem.cfc.common.Result;
|
|
|
+import com.etotem.cfc.controller.wish.WishController;
|
|
|
+import com.etotem.cfc.dto.CreateWishDTO;
|
|
|
+import com.etotem.cfc.dto.WishDTO;
|
|
|
+import com.etotem.cfc.entity.FamilyMember;
|
|
|
+import com.etotem.cfc.entity.User;
|
|
|
+import com.etotem.cfc.entity.Wish;
|
|
|
+import com.etotem.cfc.mapper.FamilyMemberMapper;
|
|
|
+import com.etotem.cfc.mapper.UserMapper;
|
|
|
+import com.etotem.cfc.mapper.WishMapper;
|
|
|
+import com.etotem.cfc.service.CfSpendDispatchService;
|
|
|
+import com.etotem.cfc.service.WishService;
|
|
|
+import org.junit.jupiter.api.BeforeEach;
|
|
|
+import org.junit.jupiter.api.DisplayName;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestBody;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+
|
|
|
+import java.lang.annotation.Annotation;
|
|
|
+import java.lang.reflect.Field;
|
|
|
+import java.lang.reflect.Method;
|
|
|
+import java.lang.reflect.Parameter;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.HashSet;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Set;
|
|
|
+
|
|
|
+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.assertThrows;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
+import static org.junit.jupiter.api.Assertions.fail;
|
|
|
+import static org.mockito.ArgumentMatchers.any;
|
|
|
+import static org.mockito.Mockito.mock;
|
|
|
+import static org.mockito.Mockito.never;
|
|
|
+import static org.mockito.Mockito.verify;
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 契约级单测:能力键 {@code wish.cycle.manage}(SC-P-13 心愿创建-审批-兑换闭环 / SC-P-14 心愿驳回/失效处理)。
|
|
|
+ *
|
|
|
+ * <p><b>溯源·需求</b>:{@code tests/user-scenarios/应用场景总览.md}
|
|
|
+ * <ul>
|
|
|
+ * <li>SC-P-13:B 建心愿 → 家长待审批 → 通过扣积分兑换 / 驳回失效;边界:积分不足拦截;驳回后不可再兑。</li>
|
|
|
+ * <li>SC-P-14:驳回 → 心愿失效 → 可重新提;边界:已兑换不可驳回。</li>
|
|
|
+ * </ul></p>
|
|
|
+ *
|
|
|
+ * <p><b>溯源·设计</b>:{@code API_REFERENCE.md} §4.4 心愿({@code /api/wishes})create / set-price /
|
|
|
+ * reject / exchange / approve-exchange / fulfill / confirm / cancel / list / pending / {id};
|
|
|
+ * §4.61.6 商城/心愿/积分(SC-C-04/C-22)list 请求体契约 {@code {memberId?, status?, page?, size?}} → {@code Wish[]}。
|
|
|
+ * 模块:{@link WishController} + 奖励域 {@code RewardController}(奖励模板/兑换,本键聚焦 wish 域闭环)。</p>
|
|
|
+ *
|
|
|
+ * <p><b>编排溯源</b>:{@code docs/tdd/orchestration/sc-key-map.md} SC-P-13 / SC-P-14 → 键
|
|
|
+ * {@code wish.cycle.manage} → 任务 <b>S-cfc-261</b>(RED 阶段)。</p>
|
|
|
+ *
|
|
|
+ * <p><b>测试范围与 RED 判定</b>:纯 Mockito / 反射,无 Spring / 无 DB / 无外部服务(同批
|
|
|
+ * {@code WishFlowScP13ContractTest} / {@code ChildWishManageContractTest} 风格)。</p>
|
|
|
+ * <ul>
|
|
|
+ * <li><b>核心闭环(A–E 段)</b>:心愿 create→定价→申请兑换→批准扣积分→兑现→确认 的状态机、
|
|
|
+ * 驳回/失效/重新提、积分不足拦截、已兑换不可驳回、越权拒绝——这些为 SC-P-13/14 契约本体。</li>
|
|
|
+ * <li><b>契约缺口(F 段,预期红)</b>:{@code /api/wishes/list} 的 §4.61.6 契约
|
|
|
+ * (请求体 body 传参 {@code {memberId, status}} + 分页 {@code page}/{@code size} + {@code status} 过滤生效)
|
|
|
+ * 未落地——现有 {@code WishController.getWishes} 用 {@code @RequestParam} 读参、
|
|
|
+ * 无 page/size 分页、且 status 参数被完全忽略(按 notIn(cancelled) 硬编码返回全部非取消心愿)。
|
|
|
+ * F 段断言契约所需行为缺失,预期失败(红),GREEN 阶段补齐 list 契约后转绿。</li>
|
|
|
+ * </ul>
|
|
|
+ */
|
|
|
+class WishCycleManageContractTest {
|
|
|
+
|
|
|
+ // ==================== 契约锚点常量(API_REFERENCE §4.4) ====================
|
|
|
+
|
|
|
+ /** §4.4 心愿契约端点(11 条) */
|
|
|
+ private static final String[] WISH_CONTRACT_PATHS = {
|
|
|
+ "/create", "/{id}/set-price", "/{id}/reject", "/{id}/exchange",
|
|
|
+ "/{id}/approve-exchange", "/{id}/fulfill", "/{id}/confirm",
|
|
|
+ "/{id}/cancel", "/list", "/pending", "/{id}"
|
|
|
+ };
|
|
|
+
|
|
|
+ /** 心愿兑换扣账 refType(cf_balance 统一账本口径,§10.25 cf 单一真源) */
|
|
|
+ private static final String WISH_EXCHANGE_REF_TYPE = "wish_exchange";
|
|
|
+
|
|
|
+ private WishService service;
|
|
|
+ private WishMapper wishMapper;
|
|
|
+ private FamilyMemberMapper familyMemberMapper;
|
|
|
+ private UserMapper userMapper;
|
|
|
+ private CfSpendDispatchService cfSpendDispatchService;
|
|
|
+
|
|
|
+ /** 捕获 DB 中已插入/更新的心愿(模拟 wish 表) */
|
|
|
+ private final List<Wish> wishStore = new ArrayList<>();
|
|
|
+ private long idSeq = 5000L;
|
|
|
+
|
|
|
+ @BeforeEach
|
|
|
+ void setUp() throws Exception {
|
|
|
+ service = new WishService();
|
|
|
+ wishMapper = mock(WishMapper.class);
|
|
|
+ familyMemberMapper = mock(FamilyMemberMapper.class);
|
|
|
+ userMapper = mock(UserMapper.class);
|
|
|
+ cfSpendDispatchService = mock(CfSpendDispatchService.class);
|
|
|
+
|
|
|
+ inject(service, "wishMapper", wishMapper);
|
|
|
+ inject(service, "familyMemberMapper", familyMemberMapper);
|
|
|
+ inject(service, "userMapper", userMapper);
|
|
|
+ inject(service, "cfSpendDispatchService", cfSpendDispatchService);
|
|
|
+
|
|
|
+ // insert 捕获并回填主键
|
|
|
+ when(wishMapper.insert(any(Wish.class))).thenAnswer(inv -> {
|
|
|
+ Wish w = inv.getArgument(0);
|
|
|
+ if (w.getId() == null) w.setId(++idSeq);
|
|
|
+ wishStore.add(w);
|
|
|
+ return 1;
|
|
|
+ });
|
|
|
+ // selectById 主键查询
|
|
|
+ when(wishMapper.selectById(any(Long.class))).thenAnswer(inv -> {
|
|
|
+ Long id = inv.getArgument(0);
|
|
|
+ return wishStore.stream().filter(w -> w.getId().equals(id)).findFirst().orElse(null);
|
|
|
+ });
|
|
|
+ // updateById 覆盖 store 中同 id
|
|
|
+ when(wishMapper.updateById(any(Wish.class))).thenAnswer(inv -> {
|
|
|
+ Wish w = inv.getArgument(0);
|
|
|
+ for (int i = 0; i < wishStore.size(); i++) {
|
|
|
+ if (wishStore.get(i).getId().equals(w.getId())) wishStore.set(i, w);
|
|
|
+ }
|
|
|
+ return 1;
|
|
|
+ });
|
|
|
+ // selectList 返回 store 快照(供 list 契约 F 段验证 status 过滤)
|
|
|
+ when(wishMapper.selectList(any())).thenAnswer(inv -> new ArrayList<>(wishStore));
|
|
|
+ when(wishMapper.selectCount(any())).thenReturn(0L);
|
|
|
+
|
|
|
+ // 默认:家庭1、家长 user(1,1)、成员B member(100,1,1)、CF 余额 1000
|
|
|
+ when(userMapper.selectById(1L)).thenReturn(user(1L, 1L));
|
|
|
+ when(familyMemberMapper.selectById(100L)).thenReturn(member(100L, 1L, 1L, "小明"));
|
|
|
+ when(cfSpendDispatchService.getAvailableForMember(100L)).thenReturn(1000);
|
|
|
+ }
|
|
|
+
|
|
|
+ // =====================================================================
|
|
|
+ // A. 端点路由契约(反射):§4.4 /api/wishes 11 条端点
|
|
|
+ // =====================================================================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("端点契约:WishController /api/wishes 暴露 §4.4 全部 11 条契约端点")
|
|
|
+ void wishController_exposesContractEndpoints() {
|
|
|
+ Class<?> clazz = WishController.class;
|
|
|
+ RequestMapping classMapping = clazz.getAnnotation(RequestMapping.class);
|
|
|
+ assertNotNull(classMapping, "WishController 应声明类级 @RequestMapping");
|
|
|
+ assertTrue(Arrays.asList(classMapping.value()).contains("/api/wishes"),
|
|
|
+ "WishController 类级路由应为 /api/wishes,实际: " + Arrays.toString(classMapping.value()));
|
|
|
+
|
|
|
+ Set<String> postPaths = postMappingPaths(clazz);
|
|
|
+ for (String path : WISH_CONTRACT_PATHS) {
|
|
|
+ assertTrue(postPaths.contains(path),
|
|
|
+ "契约端点 POST /api/wishes" + path + " 应已注册(API_REFERENCE §4.4),实际已注册: " + postPaths);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // =====================================================================
|
|
|
+ // B. SC-P-13 心愿创建-审批-兑换闭环(正常流)
|
|
|
+ // =====================================================================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("SC-P-13 正常流:孩子建心愿→家长定价→孩子申请兑换→家长批准扣积分→兑现→确认收货")
|
|
|
+ void p13_闭环_创建_定价_兑换_批准扣积分_兑现_确认() {
|
|
|
+ // 1) B 建心愿 → draft(进家长待审批)
|
|
|
+ Long id = service.createByChild(100L, childWish("买乐高"));
|
|
|
+ assertEquals("draft", current(id).getStatus(), "孩子建心愿初始为 draft(待家长定价审批)");
|
|
|
+ assertEquals("child", current(id).getCreatorType());
|
|
|
+
|
|
|
+ // 2) 家长定价 draft→active
|
|
|
+ assertTrue(service.setPrice(id, 1L, 300), "家长定价应成功");
|
|
|
+ assertEquals("active", current(id).getStatus(), "定价后 active");
|
|
|
+ assertEquals(300, current(id).getPointsRequired().intValue());
|
|
|
+
|
|
|
+ // 3) B 申请兑换 active→pending_exchange
|
|
|
+ assertTrue(service.requestExchange(id, 100L), "申请兑换应成功");
|
|
|
+ assertEquals("pending_exchange", current(id).getStatus());
|
|
|
+
|
|
|
+ // 4) 家长批准兑换 pending_exchange→approved,扣 cf_balance
|
|
|
+ assertTrue(service.approveExchange(id, 1L, true), "家长批准兑换应成功");
|
|
|
+ assertEquals("approved", current(id).getStatus(), "批准后 approved");
|
|
|
+ verify(cfSpendDispatchService).spendForMember(100L, 300, WISH_EXCHANGE_REF_TYPE, id, "兑换心愿: 买乐高");
|
|
|
+
|
|
|
+ // 5) 家长标记已购买 approved→exchanging
|
|
|
+ assertTrue(service.markFulfilled(id, 1L), "标记已购买应成功");
|
|
|
+ assertEquals("exchanging", current(id).getStatus());
|
|
|
+
|
|
|
+ // 6) B 确认收货 exchanging→fulfilled
|
|
|
+ assertTrue(service.confirmReceived(id, 100L), "确认收货应成功");
|
|
|
+ assertEquals("fulfilled", current(id).getStatus(), "闭环终点 fulfilled");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("SC-P-13 正常流:家长可直接建 active 心愿并进入兑换链")
|
|
|
+ void p13_闭环_家长直接建active心愿_孩子兑换_批准() {
|
|
|
+ Long id = service.createByParent(1L, parentWish(100L, 300, "乐高"));
|
|
|
+ assertEquals("active", current(id).getStatus(), "家长建心愿应直接 active(已定价)");
|
|
|
+
|
|
|
+ assertTrue(service.requestExchange(id, 100L));
|
|
|
+ assertEquals("pending_exchange", current(id).getStatus());
|
|
|
+
|
|
|
+ assertTrue(service.approveExchange(id, 1L, true));
|
|
|
+ assertEquals("approved", current(id).getStatus());
|
|
|
+ verify(cfSpendDispatchService).spendForMember(100L, 300, WISH_EXCHANGE_REF_TYPE, id, "兑换心愿: 乐高");
|
|
|
+ }
|
|
|
+
|
|
|
+ // =====================================================================
|
|
|
+ // C. SC-P-13 异常流:积分不足拦截 / 非 active 不可兑 / 重复兑拦截
|
|
|
+ // =====================================================================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("SC-P-13 异常流:积分不足申请兑换被拦截,状态保持 active")
|
|
|
+ void p13_异常_积分不足_拦截_不改状态() {
|
|
|
+ when(cfSpendDispatchService.getAvailableForMember(100L)).thenReturn(10); // 余额 < 价
|
|
|
+ Long id = service.createByParent(1L, parentWish(100L, 300, "高价心愿"));
|
|
|
+
|
|
|
+ RuntimeException e = assertThrows(RuntimeException.class, () -> service.requestExchange(id, 100L));
|
|
|
+ assertTrue(e.getMessage().contains("积分不足"), "实际: " + e.getMessage());
|
|
|
+ assertEquals("active", current(id).getStatus(), "积分不足不应改状态");
|
|
|
+ verify(cfSpendDispatchService, never()).spendForMember(any(), any(Integer.class), any(), any(), any());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("SC-P-13 异常流:draft 状态申请兑换被拒(未定价不可兑)")
|
|
|
+ void p13_异常_draft不可申请兑换() {
|
|
|
+ Long id = service.createByChild(100L, childWish("draft心愿"));
|
|
|
+ assertFalse(service.requestExchange(id, 100L), "draft 未定价不可申请兑换");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("SC-P-13 异常流:已兑换(approved)不可再次申请兑换")
|
|
|
+ void p13_异常_已兑换不可重复申请() {
|
|
|
+ Long id = service.createByParent(1L, parentWish(100L, 20, "再兑心愿"));
|
|
|
+ service.requestExchange(id, 100L);
|
|
|
+ service.approveExchange(id, 1L, true); // approved
|
|
|
+
|
|
|
+ assertFalse(service.requestExchange(id, 100L), "approved 非 active,不可再次申请兑换");
|
|
|
+ }
|
|
|
+
|
|
|
+ // =====================================================================
|
|
|
+ // D. SC-P-14 心愿驳回/失效处理 + 已兑换不可驳回
|
|
|
+ // =====================================================================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("SC-P-14 正常流:家长驳回 draft 心愿→rejected(失效),记 rejectCount")
|
|
|
+ void p14_正常_驳回draft_失效() {
|
|
|
+ Long id = service.createByChild(100L, childWish("异常心愿"));
|
|
|
+ assertTrue(service.rejectWish(id, 1L, "积分超额"), "驳回应成功");
|
|
|
+ assertEquals("rejected", current(id).getStatus(), "驳回后失效 rejected");
|
|
|
+ assertEquals(1, current(id).getRejectCount().intValue(), "rejectCount 应 +1");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("SC-P-14 正常流:驳回后不可再兑(rejected 非 active)")
|
|
|
+ void p14_正常_驳回后不可兑() {
|
|
|
+ Long id = service.createByChild(100L, childWish("被驳回心愿"));
|
|
|
+ service.rejectWish(id, 1L, "积分超额");
|
|
|
+
|
|
|
+ assertFalse(service.requestExchange(id, 100L), "被驳回心愿不可再申请兑换(驳回后不可再兑)");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("SC-P-14 正常流:驳回→失效→可重新提(member 重新建新心愿为 draft)")
|
|
|
+ void p14_正常_驳回后可重新提_新心愿draft() {
|
|
|
+ Long first = service.createByChild(100L, childWish("被驳回心愿"));
|
|
|
+ service.rejectWish(first, 1L, "积分超额");
|
|
|
+ assertEquals("rejected", current(first).getStatus());
|
|
|
+
|
|
|
+ // 可重新提:member 重新建新心愿,仍走 draft 待审批
|
|
|
+ Long second = service.createByChild(100L, childWish("重新提的心愿"));
|
|
|
+ assertNotNull(second, "重新提应成功(新建心愿)");
|
|
|
+ assertEquals("draft", current(second).getStatus(), "重新提的心愿应为 draft(待审批)");
|
|
|
+ assertEquals("child", current(second).getCreatorType());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("SC-P-14 正常流:取消 active 心愿→cancelled(失效)")
|
|
|
+ void p14_正常_取消active_失效() {
|
|
|
+ Long id = service.createByParent(1L, parentWish(100L, 10, "取消心愿"));
|
|
|
+ assertTrue(service.cancelWish(id, 1L, "孩子不要了"), "取消应成功");
|
|
|
+ assertEquals("cancelled", current(id).getStatus(), "取消后失效 cancelled");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("SC-P-14 异常流:已兑换(approved)不可驳回")
|
|
|
+ void p14_异常_已兑换不可驳回() {
|
|
|
+ Long id = service.createByParent(1L, parentWish(100L, 20, "已兑换心愿"));
|
|
|
+ service.requestExchange(id, 100L);
|
|
|
+ service.approveExchange(id, 1L, true); // approved
|
|
|
+
|
|
|
+ assertFalse(service.rejectWish(id, 1L, "驳回"), "已兑换心愿不可驳回(rejectWish 仅允许 draft)");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("SC-P-14 异常流:已确认收货(fulfilled)不可驳回")
|
|
|
+ void p14_异常_已兑现不可驳回() {
|
|
|
+ Long id = service.createByParent(1L, parentWish(100L, 20, "已兑现心愿"));
|
|
|
+ service.requestExchange(id, 100L);
|
|
|
+ service.approveExchange(id, 1L, true);
|
|
|
+ service.markFulfilled(id, 1L);
|
|
|
+ service.confirmReceived(id, 100L); // fulfilled
|
|
|
+
|
|
|
+ assertFalse(service.rejectWish(id, 1L, "驳回"), "已兑现心愿不可驳回");
|
|
|
+ }
|
|
|
+
|
|
|
+ // =====================================================================
|
|
|
+ // E. 权限流:跨家庭家长定价/驳回/审批被拒;非归属成员兑换被拒
|
|
|
+ // =====================================================================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("权限流:跨家庭家长为孩子定价被拒")
|
|
|
+ void 权限_跨家庭家长定价_拒绝() {
|
|
|
+ Long id = service.createByChild(100L, childWish("本家心愿"));
|
|
|
+ when(userMapper.selectById(99L)).thenReturn(user(99L, 999L));
|
|
|
+
|
|
|
+ assertFalse(service.setPrice(id, 99L, 100), "非本家庭家长定价应失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("权限流:跨家庭家长为孩子建 active 心愿被拒(成员不属于该家庭)")
|
|
|
+ void 权限_跨家庭家长建心愿_拒绝() {
|
|
|
+ when(userMapper.selectById(99L)).thenReturn(user(99L, 999L));
|
|
|
+
|
|
|
+ RuntimeException e = assertThrows(RuntimeException.class,
|
|
|
+ () -> service.createByParent(99L, parentWish(100L, 100, "越权心愿")));
|
|
|
+ assertTrue(e.getMessage().contains("不属于该家庭"), "实际: " + e.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("权限流:非心愿归属成员申请兑换被拒")
|
|
|
+ void 权限_非归属成员兑换_拒绝() {
|
|
|
+ Long id = service.createByParent(1L, parentWish(100L, 10, "他人心愿"));
|
|
|
+
|
|
|
+ assertFalse(service.requestExchange(id, 200L), "非归属成员申请兑换应被拒");
|
|
|
+ verify(cfSpendDispatchService, never()).spendForMember(any(), any(Integer.class), any(), any(), any());
|
|
|
+ }
|
|
|
+
|
|
|
+ // =====================================================================
|
|
|
+ // F. 契约缺口(预期红):/api/wishes/list §4.61.6 契约未落地
|
|
|
+ // =====================================================================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("F1 契约缺口:list 应按契约从请求体 body 读 {memberId, status, page, size}")
|
|
|
+ void f1_list_应支持body传参_契约缺口_预期红() {
|
|
|
+ // §4.61.6:POST /api/wishes/list 请求体 {memberId?, status?, page?, size?}
|
|
|
+ // 现状 getWishes 用 @RequestParam(query 参数)读 memberId/familyId/status,
|
|
|
+ // 与契约「请求体 body」不符 → 无任何 @RequestBody 参数 → 断言失败(红)
|
|
|
+ Method m = findMethod(WishController.class, "getWishes");
|
|
|
+ boolean hasRequestBody = false;
|
|
|
+ for (Parameter p : m.getParameters()) {
|
|
|
+ for (Annotation a : p.getAnnotations()) {
|
|
|
+ if (a.annotationType() == RequestBody.class) {
|
|
|
+ hasRequestBody = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ assertTrue(hasRequestBody,
|
|
|
+ "[wish.cycle.manage] POST /api/wishes/list 契约要求请求体 body {memberId?, status?, page?, size?}(§4.61.6),"
|
|
|
+ + "但 WishController.getWishes 未声明 @RequestBody 参数(现状用 @RequestParam 读参)。契约缺口,预期红。");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("F2 契约缺口:list 应支持 page/size 分页(契约 body.page/size)")
|
|
|
+ void f2_list_应支持分页_契约缺口_预期红() {
|
|
|
+ // 契约含 page?/size? 分页;现状 getWishes 仅 (userId, role, memberId, familyId, status) 5 参,无分页槽位
|
|
|
+ // → 参数总数 5 < 7(userId/role/memberId/familyId/status + page/size)→ 断言失败(红)
|
|
|
+ Method m = findMethod(WishController.class, "getWishes");
|
|
|
+ int paramCount = m.getParameterCount();
|
|
|
+ assertTrue(paramCount >= 7,
|
|
|
+ "[wish.cycle.manage] POST /api/wishes/list 契约要求支持 page/size 分页(§4.61.6 body.page/size),"
|
|
|
+ + "但 WishController.getWishes 参数数=" + paramCount
|
|
|
+ + "(仅 userId/role/memberId/familyId/status),无分页槽位。契约缺口,预期红。");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("F3 契约缺口:list 应按 status 过滤(契约 body.status)")
|
|
|
+ void f3_list_应按status过滤_契约缺口_预期红() throws Exception {
|
|
|
+ // 成员 B 有 2 条心愿:一条 active、一条 fulfilled(已确认收货)
|
|
|
+ service.createByParent(1L, parentWish(100L, 300, "A_active"));
|
|
|
+ Long doneId = service.createByParent(1L, parentWish(100L, 20, "B_fulfilled"));
|
|
|
+ service.requestExchange(doneId, 100L);
|
|
|
+ service.approveExchange(doneId, 1L, true);
|
|
|
+ service.markFulfilled(doneId, 1L);
|
|
|
+ service.confirmReceived(doneId, 100L); // fulfilled
|
|
|
+
|
|
|
+ WishController controller = new WishController();
|
|
|
+ inject(controller, "wishService", service);
|
|
|
+
|
|
|
+ // 契约:body.status=active → 仅返回 active(1 条)
|
|
|
+ Result<List<WishDTO>> result = controller.getWishes(1L, "parent", 100L, null, "active");
|
|
|
+ List<WishDTO> data = result.getData();
|
|
|
+ assertNotNull(data);
|
|
|
+ // 现状:status 参数被完全忽略(getChildWishes 仅 notIn(cancelled)),active+fulfilled 全返回(2 条)
|
|
|
+ assertEquals(1, data.size(),
|
|
|
+ "[wish.cycle.manage] POST /api/wishes/list 契约要求按 body.status 过滤(§4.61.6),"
|
|
|
+ + "status=active 应仅返回 active 心愿;现状忽略 status,返回 " + data.size()
|
|
|
+ + " 条(active+fulfilled 全部)。契约缺口,预期红。");
|
|
|
+ }
|
|
|
+
|
|
|
+ // =====================================================================
|
|
|
+ // 辅助构造
|
|
|
+ // =====================================================================
|
|
|
+
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static User user(Long id, Long familyId) {
|
|
|
+ User u = new User();
|
|
|
+ u.setId(id);
|
|
|
+ u.setFamilyId(familyId);
|
|
|
+ return u;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static FamilyMember member(Long id, Long familyId, Long userId, String nickname) {
|
|
|
+ FamilyMember m = new FamilyMember();
|
|
|
+ m.setId(id);
|
|
|
+ m.setFamilyId(familyId);
|
|
|
+ m.setUserId(userId);
|
|
|
+ m.setNickname(nickname);
|
|
|
+ return m;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static CreateWishDTO childWish(String title) {
|
|
|
+ CreateWishDTO dto = new CreateWishDTO();
|
|
|
+ dto.setTitle(title);
|
|
|
+ return dto;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static CreateWishDTO parentWish(Long memberId, Integer points, String title) {
|
|
|
+ CreateWishDTO dto = new CreateWishDTO();
|
|
|
+ dto.setTitle(title);
|
|
|
+ dto.setMemberId(memberId);
|
|
|
+ dto.setPointsRequired(points);
|
|
|
+ return dto;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 在 store 中检索指定 id 的当前心愿对象 */
|
|
|
+ private Wish current(Long id) {
|
|
|
+ return wishStore.stream().filter(w -> w.getId().equals(id)).findFirst().orElse(null);
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 收集类上所有 @PostMapping 的 value 路径 */
|
|
|
+ private static Set<String> postMappingPaths(Class<?> clazz) {
|
|
|
+ Set<String> paths = new HashSet<>();
|
|
|
+ for (Method m : clazz.getDeclaredMethods()) {
|
|
|
+ PostMapping pm = m.getAnnotation(PostMapping.class);
|
|
|
+ if (pm != null) {
|
|
|
+ paths.addAll(Arrays.asList(pm.value()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return paths;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 按名称查找无参/任意参方法(用于反射断言) */
|
|
|
+ private static Method findMethod(Class<?> clazz, String name) {
|
|
|
+ for (Method m : clazz.getDeclaredMethods()) {
|
|
|
+ if (m.getName().equals(name)) {
|
|
|
+ return m;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ fail("方法 " + clazz.getSimpleName() + "." + name + " 不存在");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+}
|