|
@@ -0,0 +1,231 @@
|
|
|
|
|
+package com.etotem.cfc.unit;
|
|
|
|
|
+
|
|
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
|
|
+import com.etotem.cfc.common.Result;
|
|
|
|
|
+import com.etotem.cfc.controller.family.PointsController;
|
|
|
|
|
+import com.etotem.cfc.entity.FamilyMember;
|
|
|
|
|
+import com.etotem.cfc.entity.PointsLog;
|
|
|
|
|
+import com.etotem.cfc.mapper.FamilyMemberMapper;
|
|
|
|
|
+import com.etotem.cfc.mapper.PointsLogMapper;
|
|
|
|
|
+import com.etotem.cfc.service.PointsService;
|
|
|
|
|
+import org.junit.jupiter.api.BeforeEach;
|
|
|
|
|
+import org.junit.jupiter.api.DisplayName;
|
|
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
|
|
+
|
|
|
|
|
+import java.lang.reflect.Field;
|
|
|
|
|
+import java.lang.reflect.Method;
|
|
|
|
|
+import java.text.ParseException;
|
|
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
|
+import java.util.Arrays;
|
|
|
|
|
+import java.util.Date;
|
|
|
|
|
+import java.util.HashMap;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
|
|
|
+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.when;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 契约级单测(TDD-RED):能力键 {@code points.ledger.view} 的<b>边界与误报守卫</b>。
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>契约溯源:</p>
|
|
|
|
|
+ * <ul>
|
|
|
|
|
+ * <li>需求 {@code tests/user-scenarios/应用场景总览.md} §71-72:SC-P-15 积分明细查看(来源/变更/余额,分页 + 时间筛选);
|
|
|
|
|
+ * SC-P-16 积分流水对账(总额与流水合计一致,删除/回退有负流水,不一致报警)。</li>
|
|
|
|
|
+ * <li>设计 {@code docs/superpowers/api/API_REFERENCE.md} §4.5:{@code POST /api/points/logs}、
|
|
|
|
|
+ * {@code /api/points/balance};模块 {@code PointsController} / {@code PointsService}。</li>
|
|
|
|
|
+ * </ul>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p><b>与既有测试的职责边界</b>:{@code PointsLedgerViewContractTest}(S-cfc-263 已合入)覆盖来源可读名、
|
|
|
|
|
+ * 时间筛选基本命中、不一致报警、ISO 8601;{@code PointsLedgerScP15ContractTest} 覆盖余额/分页/分类流水数据口径。
|
|
|
|
|
+ * 本类<b>不重复</b>上述断言,只补四个会让「半吊子 GREEN 实现」漏过的边界:</p>
|
|
|
|
|
+ * <ol>
|
|
|
|
|
+ * <li>账目一致时不得误报警(防止 reconcile 无条件 alert=true);</li>
|
|
|
|
|
+ * <li>负流水计数排除零额与正数(防止把 amount==0 计成删除/回退);</li>
|
|
|
|
|
+ * <li>{@code endTime=yyyy-MM-dd} 应包含当日整日(防止按 00:00:00 截断漏掉当天流水);</li>
|
|
|
|
|
+ * <li>分页 + 时间筛选同时使用时 {@code Page.total} 必须是筛选后的条数(防止先分页后筛选)。</li>
|
|
|
|
|
+ * </ol>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p><b>RED 机制</b>:{@code PointsService#reconcile(Long)} 与时间筛选参数在 src/main 尚未落地,
|
|
|
|
|
+ * 测试以反射调用缺失方法 / 走 controller 真实入口断言行为,失败发生在运行期(红)而非编译期。
|
|
|
|
|
+ * 纯 Mockito + 反射,无 Spring / DB / 外部服务。GREEN 阶段实现落地后本类应自动转绿。</p>
|
|
|
|
|
+ */
|
|
|
|
|
+class PointsLedgerViewBoundaryContractTest {
|
|
|
|
|
+
|
|
|
|
|
+ private static final Long MEMBER_ID = 100L;
|
|
|
|
|
+
|
|
|
|
|
+ private PointsService service;
|
|
|
|
|
+ private PointsController controller;
|
|
|
|
|
+ private FamilyMemberMapper familyMemberMapper;
|
|
|
|
|
+ private PointsLogMapper pointsLogMapper;
|
|
|
|
|
+
|
|
|
|
|
+ @BeforeEach
|
|
|
|
|
+ void setUp() throws Exception {
|
|
|
|
|
+ service = new PointsService();
|
|
|
|
|
+ familyMemberMapper = mock(FamilyMemberMapper.class);
|
|
|
|
|
+ pointsLogMapper = mock(PointsLogMapper.class);
|
|
|
|
|
+ inject(service, "familyMemberMapper", familyMemberMapper);
|
|
|
|
|
+ inject(service, "pointsLogMapper", pointsLogMapper);
|
|
|
|
|
+ controller = new PointsController();
|
|
|
|
|
+ inject(controller, "pointsService", service);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ 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 FamilyMember member(Long id, Integer totalPoints) {
|
|
|
|
|
+ FamilyMember m = new FamilyMember();
|
|
|
|
|
+ m.setId(id);
|
|
|
|
|
+ m.setTotalPoints(totalPoints);
|
|
|
|
|
+ return m;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static PointsLog log(Long id, Long memberId, Integer amount, String type,
|
|
|
|
|
+ String category, String createdAt) {
|
|
|
|
|
+ PointsLog l = new PointsLog();
|
|
|
|
|
+ l.setId(id);
|
|
|
|
|
+ l.setMemberId(memberId);
|
|
|
|
|
+ l.setAmount(amount);
|
|
|
|
|
+ l.setType(type);
|
|
|
|
|
+ l.setCategory(category);
|
|
|
|
|
+ l.setDescription(type);
|
|
|
|
|
+ l.setCreatedAt(date(createdAt));
|
|
|
|
|
+ return l;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static Date date(String yyyyMMddHHmmss) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
+ sdf.setLenient(false);
|
|
|
|
|
+ return sdf.parse(yyyyMMddHHmmss);
|
|
|
|
|
+ } catch (ParseException e) {
|
|
|
|
|
+ throw new IllegalArgumentException("非法测试时间: " + yyyyMMddHHmmss, e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
|
|
+ private void seedPage(PointsLog... logs) {
|
|
|
|
|
+ when(pointsLogMapper.selectPage(any(Page.class), any())).thenAnswer(inv -> {
|
|
|
|
|
+ Page<PointsLog> p = inv.getArgument(0);
|
|
|
|
|
+ p.setRecords(new ArrayList<>(Arrays.asList(logs)));
|
|
|
|
|
+ p.setTotal(logs.length);
|
|
|
|
|
+ return p;
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static Map<String, Object> logsParams(String startTime, String endTime, Integer page, Integer size) {
|
|
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
|
|
+ params.put("memberId", MEMBER_ID);
|
|
|
|
|
+ params.put("page", page);
|
|
|
|
|
+ params.put("size", size);
|
|
|
|
|
+ if (startTime != null) {
|
|
|
|
|
+ params.put("startTime", startTime);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (endTime != null) {
|
|
|
|
|
+ params.put("endTime", endTime);
|
|
|
|
|
+ }
|
|
|
|
|
+ return params;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Page<PointsLog> invokeLogs(Map<String, Object> params) {
|
|
|
|
|
+ Result<Page<PointsLog>> r = controller.getPointsLogs(params, MEMBER_ID);
|
|
|
|
|
+ assertEquals(200, r.getCode(), "流水查询入口应返回 200");
|
|
|
|
|
+ assertNotNull(r.getData(), "流水查询应返回 Page");
|
|
|
|
|
+ return r.getData();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 反射调用契约方法;方法缺失即 fail(RED),保证测试可与尚未落地的实现一同编译。 */
|
|
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
|
|
+ private Map<String, Object> reconcile(Long memberId) {
|
|
|
|
|
+ for (Method m : service.getClass().getMethods()) {
|
|
|
|
|
+ if (m.getName().equals("reconcile") && m.getParameterCount() == 1) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ Object result = m.invoke(service, memberId);
|
|
|
|
|
+ assertNotNull(result, "reconcile 应返回对账结果");
|
|
|
|
|
+ if (result instanceof Map) {
|
|
|
|
|
+ return (Map<String, Object>) result;
|
|
|
|
|
+ }
|
|
|
|
|
+ fail("RED[points.ledger.view] reconcile 应返回包含 balance/logSum/consistent/alert/negativeCount 的结果 Map");
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ fail("RED[points.ledger.view] reconcile 调用失败:" + e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ fail("RED[points.ledger.view] PointsService#reconcile(Long) 未实现(SC-P-16 对账入口未落地)");
|
|
|
|
|
+ throw new IllegalStateException("unreachable");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static int num(Object v) {
|
|
|
|
|
+ return v instanceof Number ? ((Number) v).intValue() : Integer.MIN_VALUE;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== AC:账目一致不得误报(SC-P-16) ====================
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("SC-P-16 边界:总额与流水合计相等时 consistent=true 且 alert=false(不得误报警)")
|
|
|
|
|
+ void 契约_对账_一致时不误报警() {
|
|
|
|
|
+ when(familyMemberMapper.selectById(MEMBER_ID)).thenReturn(member(MEMBER_ID, 520));
|
|
|
|
|
+ when(pointsLogMapper.selectSum(any())).thenReturn(520);
|
|
|
|
|
+ seedPage(log(1L, MEMBER_ID, 520, "earn", "task", "2026-06-01 10:00:00"));
|
|
|
|
|
+
|
|
|
|
|
+ Map<String, Object> r = reconcile(MEMBER_ID);
|
|
|
|
|
+ assertEquals(520, num(r.get("balance")), "应返回账面总额");
|
|
|
|
|
+ assertEquals(520, num(r.get("logSum")), "应返回流水合计");
|
|
|
|
|
+ assertEquals(Boolean.TRUE, r.get("consistent"), "520 == 520 应判为账目一致");
|
|
|
|
|
+ assertEquals(Boolean.FALSE, r.get("alert"), "账目一致时不得误报警(防止实现无条件 alert=true)");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== AC:负流水计数口径(SC-P-16 删除/回退有负流水) ====================
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("SC-P-16 边界:negativeCount 只统计负数流水,零额与正数不得计入")
|
|
|
|
|
+ void 契约_对账_负流水计数排除零额() {
|
|
|
|
|
+ when(familyMemberMapper.selectById(MEMBER_ID)).thenReturn(member(MEMBER_ID, 520));
|
|
|
|
|
+ when(pointsLogMapper.selectSum(any())).thenReturn(520);
|
|
|
|
|
+ seedPage(
|
|
|
|
|
+ log(2L, MEMBER_ID, 530, "earn", "task", "2026-06-01 10:00:00"),
|
|
|
|
|
+ log(3L, MEMBER_ID, 0, "system", "system", "2026-06-02 10:00:00"),
|
|
|
|
|
+ log(4L, MEMBER_ID, -10, "refund", "system", "2026-06-03 10:00:00"));
|
|
|
|
|
+
|
|
|
|
|
+ Map<String, Object> r = reconcile(MEMBER_ID);
|
|
|
|
|
+ assertEquals(1, num(r.get("negativeCount")),
|
|
|
|
|
+ "仅 -10 是删除/回退负流水,0 与 +530 不得计入(防止零额误判为回退)");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== AC:时间筛选边界(SC-P-15 分页+时间筛选) ====================
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("SC-P-15 边界:endTime=yyyy-MM-dd 应包含当日 23:59:59 的流水")
|
|
|
|
|
+ void 契约_时间筛选_endTime包含当日整日() {
|
|
|
|
|
+ PointsLog endOfDay = log(5L, MEMBER_ID, 10, "task", "task", "2026-06-30 23:30:00");
|
|
|
|
|
+ PointsLog nextDay = log(6L, MEMBER_ID, 5, "task", "task", "2026-07-01 00:30:00");
|
|
|
|
|
+ seedPage(endOfDay, nextDay);
|
|
|
|
|
+
|
|
|
|
|
+ List<PointsLog> records = invokeLogs(logsParams("2026-06-01", "2026-06-30", 1, 10)).getRecords();
|
|
|
|
|
+ assertEquals(1, records.size(),
|
|
|
|
|
+ "endTime=2026-06-30 必须包含当天 23:30 的流水,且排除 7 月 1 日的流水");
|
|
|
|
|
+ assertEquals(Long.valueOf(5L), records.get(0).getId(), "应保留当日末尾那条流水");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("SC-P-15 边界:分页与时间筛选同时生效时 total 为筛选后条数")
|
|
|
|
|
+ void 契约_分页加时间筛选_total为筛选后条数() {
|
|
|
|
|
+ seedPage(
|
|
|
|
|
+ log(7L, MEMBER_ID, 10, "task", "task", "2026-06-10 10:00:00"),
|
|
|
|
|
+ log(8L, MEMBER_ID, 5, "task", "task", "2026-06-20 10:00:00"),
|
|
|
|
|
+ log(9L, MEMBER_ID, 1, "task", "task", "2026-07-05 10:00:00"));
|
|
|
|
|
+
|
|
|
|
|
+ Page<PointsLog> page = invokeLogs(logsParams("2026-06-01", "2026-06-30", 1, 1));
|
|
|
|
|
+ assertEquals(2, page.getTotal(),
|
|
|
|
|
+ "total 必须是筛选后 6 月流水的条数(2),不能是全部流水条数(3)");
|
|
|
|
|
+ assertEquals(1, page.getRecords().size(), "size=1 时每页只返回一条流水");
|
|
|
|
|
+ }
|
|
|
|
|
+}
|