|
@@ -0,0 +1,478 @@
|
|
|
|
|
+package com.etotem.cfc.unit;
|
|
|
|
|
+
|
|
|
|
|
+import com.etotem.cfc.entity.ProductReview;
|
|
|
|
|
+import com.etotem.cfc.mapper.ProductReviewMapper;
|
|
|
|
|
+import com.etotem.cfc.service.ProductReviewService;
|
|
|
|
|
+import org.junit.jupiter.api.DisplayName;
|
|
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
|
|
+import org.mockito.Mockito;
|
|
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
|
|
+import org.springframework.web.bind.annotation.RequestAttribute;
|
|
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
|
+
|
|
|
|
|
+import java.io.InputStream;
|
|
|
|
|
+import java.lang.annotation.Annotation;
|
|
|
|
|
+import java.lang.reflect.Field;
|
|
|
|
|
+import java.lang.reflect.Method;
|
|
|
|
|
+import java.lang.reflect.Parameter;
|
|
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
|
|
+import java.util.Arrays;
|
|
|
|
|
+import java.util.Collections;
|
|
|
|
|
+import java.util.HashMap;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+import java.util.Scanner;
|
|
|
|
|
+
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.fail;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 契约级单测:商品详情页评价列表(能力键 {@code shop.review.list},任务 S-be-3,TDD-RED 阶段)。
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p><b>契约溯源</b></p>
|
|
|
|
|
+ * <ul>
|
|
|
|
|
+ * <li>需求:{@code docs/superpowers/specs/2026-11-14-product-order-gap-prd.md} §3 P0-A
|
|
|
|
|
+ * —— 验收3「商品详情页可展示该商品评价列表(匿名可读 + 星级聚合)」
|
|
|
|
|
+ * (已合入主线 85a64e02b)</li>
|
|
|
|
|
+ * <li>设计:{@code docs/superpowers/specs/2026-11-14-product-order-gap-arch-design.md} §3.1 R3
|
|
|
|
|
+ * ({@code POST /api/product/review/list},鉴权=<b>公开 / 加 PUBLIC_PATHS</b>,入参
|
|
|
|
|
+ * {@code {productId, page, size}} → {@code Result<Map>}:{@code {total, avgRating, records[]}},
|
|
|
|
|
+ * 商品详情页评价列表含星级聚合、只读公开、匿名可看;已合入主线 85a64e02b)
|
|
|
|
|
+ * + §5.3 类图({@code ProductReviewService.listByProduct(Long, int, int): Map}、
|
|
|
|
|
+ * {@code ProductReviewMapper.listByProduct(pid, offset, size)} /
|
|
|
|
|
+ * {@code statsByProduct(pid)})</li>
|
|
|
|
|
+ * </ul>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p><b>R3 契约(本测试断言锚点)</b></p>
|
|
|
|
|
+ * <ul>
|
|
|
|
|
+ * <li>路由:{@code POST /api/product/review/list}(统一 @PostMapping 规约)</li>
|
|
|
|
|
+ * <li>鉴权:<b>公开只读</b>——Controller 的 {@code list} 方法<b>不得</b>携带
|
|
|
|
|
+ * {@code @RequestAttribute("userId")}(与同控制器 create/detail/my 的登录鉴权相反),
|
|
|
|
|
+ * 且 {@code /api/product/review/list} 必须登记进
|
|
|
|
|
+ * {@code JwtInterceptor.PUBLIC_PATHS},否则匿名访问被 401 拦截</li>
|
|
|
|
|
+ * <li>入参:{@code {productId, page, size}},分页默认值按 §9 共享约定 = {@code page=1 / size=20}</li>
|
|
|
|
|
+ * <li>出参:{@code Result<Map>},Map 必须含 {@code total}(评价总数)、
|
|
|
|
|
+ * {@code avgRating}(星级均值)、{@code records[]}(评价记录,含商家回复)</li>
|
|
|
|
|
+ * <li>数据/隔离:独立表 {@code product_reviews}(不复用活动评价 {@code review_orders}),
|
|
|
|
|
+ * 列表与统计仅取 {@code status=1} 展示态评价,按 {@code review_at DESC} 倒序分页,
|
|
|
|
|
+ * {@code avgRating} 由 SQL {@code AVG(rating)} 聚合</li>
|
|
|
|
|
+ * </ul>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p><b>实现现状</b>(本 worktree 基于 origin/cfclub 核实):数据底座与同域兄弟能力已落地——
|
|
|
|
|
+ * {@code ProductReviewController} / {@code ProductReviewService(Impl)} 的 create/detail/my、
|
|
|
|
|
+ * {@code ProductReviewMapper.listByProduct + statsByProduct}、{@code product_reviews} 建表与
|
|
|
|
|
+ * {@code idx_product_id} 索引<b>均已存在</b>(作为绿锚点参与断言,非 RED 目标);
|
|
|
|
|
+ * <b>但 R3 的 list 能力未落地</b>:控制器无 {@code list} 方法、服务接口/实现无
|
|
|
|
|
+ * {@code listByProduct}、{@code JwtInterceptor.PUBLIC_PATHS} 未登记 {@code /api/product/review/list}。
|
|
|
|
|
+ * 因此本测试以「反射结构契约 + Mockito 行为契约」探测实现缺失:能编译通过、能运行,
|
|
|
|
|
+ * list 相关断言失败(红),失败断言原文清晰可贴(对齐同批
|
|
|
|
|
+ * {@code ProductReviewMyContractTest} / {@code TeamProfitHookContractTest} 风格)。</p>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p><b>RED 预期</b>:Controller list / Service listByProduct / PUBLIC_PATHS 相关断言失败(红);
|
|
|
|
|
+ * Mapper/XML/实体/建表 4 类数据层锚点为正常通过项(不算 RED 目标)。GREEN 阶段实现方按上述契约
|
|
|
|
|
+ * 清单补齐 {@code ProductReviewController.list} + {@code ProductReviewService.listByProduct} /
|
|
|
|
|
+ * {@code Impl} 覆盖 + {@code JwtInterceptor.PUBLIC_PATHS} 登记即可整类转绿。</p>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p><b>测试范围</b>:纯契约级,无 Spring / 无 DB / 无外部服务。</p>
|
|
|
|
|
+ */
|
|
|
|
|
+class ProductReviewListContractTest {
|
|
|
|
|
+
|
|
|
|
|
+ /** R3 控制器(设计 §3.1,前缀 /api/product/review;与 create/detail/my 同宿主)。 */
|
|
|
|
|
+ private static final String CONTROLLER = "com.etotem.cfc.controller.product.ProductReviewController";
|
|
|
|
|
+ private static final String CONTROLLER_PREFIX = "/api/product/review";
|
|
|
|
|
+
|
|
|
|
|
+ /** R3 服务接口 + 实现(设计 §5.3 类图:listByProduct(Long, int, int): Map)。 */
|
|
|
|
|
+ private static final String SERVICE = "com.etotem.cfc.service.ProductReviewService";
|
|
|
|
|
+ private static final String SERVICE_IMPL = "com.etotem.cfc.service.ProductReviewServiceImpl";
|
|
|
|
|
+
|
|
|
|
|
+ /** R3 数据层锚点(设计 §5.3 类图:listByProduct + statsByProduct 已落地)。 */
|
|
|
|
|
+ private static final String MAPPER = "com.etotem.cfc.mapper.ProductReviewMapper";
|
|
|
|
|
+
|
|
|
|
|
+ /** R3 落库实体(设计 §2.1 product_reviews 建表)。 */
|
|
|
|
|
+ private static final String ENTITY = "com.etotem.cfc.entity.ProductReview";
|
|
|
|
|
+
|
|
|
|
|
+ /** 公开只读鉴权锚点(设计 §3.1 R3:PUBLIC_PATHS 登记)。 */
|
|
|
|
|
+ private static final String JWT_INTERCEPTOR = "com.etotem.cfc.config.JwtInterceptor";
|
|
|
|
|
+ private static final String PUBLIC_PATHS_FIELD = "PUBLIC_PATHS";
|
|
|
|
|
+ private static final String PUBLIC_PATH = "/api/product/review/list";
|
|
|
|
|
+
|
|
|
|
|
+ /** §2.1 独立表名 + 商品维度索引(列表按 product_id 查询)。 */
|
|
|
|
|
+ private static final String TABLE_NAME = "product_reviews";
|
|
|
|
|
+ private static final String PRODUCT_ID_INDEX = "idx_product_id";
|
|
|
|
|
+
|
|
|
|
|
+ /** 商品详情页评价列表渲染所需实体字段(设计 §2.1 + §4.2 商品详情页评价浮层)。 */
|
|
|
|
|
+ private static final String[] LIST_DISPLAY_FIELDS = {
|
|
|
|
|
+ "id", "productId", "reviewerId", "rating", "reviewText", "images",
|
|
|
|
|
+ "replyText", "repliedAt", "status", "reviewAt"
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 正常流:Controller 契约(§3.1 R3 路由 + 公开只读) ====================
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("正常流-1(锚点):ProductReviewController 类应存在(create/detail/my 已落地)")
|
|
|
|
|
+ void controller_classExists() {
|
|
|
|
|
+ assertNotNull(loadClass(CONTROLLER),
|
|
|
|
|
+ "[shop.review.list] ProductReviewController 类不存在:契约要求独立控制器,"
|
|
|
|
|
+ + "路由前缀 /api/product/review(设计 §3.1 R3)。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("正常流-2(锚点):控制器应映射到 /api/product/review 前缀")
|
|
|
|
|
+ void controller_classMappingPrefix() {
|
|
|
|
|
+ Class<?> clazz = loadClass(CONTROLLER);
|
|
|
|
|
+ if (clazz == null) {
|
|
|
|
|
+ fail("[shop.review.list] ProductReviewController 类不存在,无法验证前缀映射(§3.1 R3)。");
|
|
|
|
|
+ }
|
|
|
|
|
+ boolean mapped = false;
|
|
|
|
|
+ for (Annotation a : clazz.getAnnotations()) {
|
|
|
|
|
+ if (a instanceof RequestMapping) {
|
|
|
|
|
+ RequestMapping rm = (RequestMapping) a;
|
|
|
|
|
+ mapped = Arrays.asList(rm.value()).contains(CONTROLLER_PREFIX)
|
|
|
|
|
+ || Arrays.asList(rm.path()).contains(CONTROLLER_PREFIX);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ assertTrue(mapped,
|
|
|
|
|
+ "[shop.review.list] ProductReviewController 类级映射应含 " + CONTROLLER_PREFIX
|
|
|
|
|
+ + "(§3.1 R3 前缀 /api/product/review)。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("正常流-3:list 方法应标注 POST /list(完整路由 /api/product/review/list)")
|
|
|
|
|
+ void controller_listPostMapping() {
|
|
|
|
|
+ Class<?> clazz = loadClass(CONTROLLER);
|
|
|
|
|
+ if (clazz == null) {
|
|
|
|
|
+ fail("[shop.review.list] ProductReviewController 类不存在,无法验证 list 路由(§3.1 R3)。");
|
|
|
|
|
+ }
|
|
|
|
|
+ Method list = findMethod(clazz, "list");
|
|
|
|
|
+ assertNotNull(list,
|
|
|
|
|
+ "[shop.review.list] ProductReviewController 应提供 list 方法"
|
|
|
|
|
+ + "(POST /api/product/review/list,商品详情页评价列表,设计 §3.1 R3 / PRD §3 P0-A 验收3)。"
|
|
|
|
|
+ + "实现未落地。");
|
|
|
|
|
+ PostMapping pm = list.getAnnotation(PostMapping.class);
|
|
|
|
|
+ assertNotNull(pm,
|
|
|
|
|
+ "[shop.review.list] list 方法应标注 @PostMapping(统一 @PostMapping 规约,§3.1 R3)。");
|
|
|
|
|
+ boolean routeOk = Arrays.asList(pm.value()).contains("/list")
|
|
|
|
|
+ || Arrays.asList(pm.path()).contains("/list");
|
|
|
|
|
+ assertTrue(routeOk,
|
|
|
|
|
+ "[shop.review.list] list 应映射 POST /list(完整路由 /api/product/review/list,§3.1 R3)。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("正常流-4:list 返回类型应为 Result<Map>({total, avgRating, records})")
|
|
|
|
|
+ void controller_listReturnsResultMap() {
|
|
|
|
|
+ Class<?> clazz = loadClass(CONTROLLER);
|
|
|
|
|
+ if (clazz == null) {
|
|
|
|
|
+ fail("[shop.review.list] ProductReviewController 类不存在,无法验证 list 出参(§3.1 R3)。");
|
|
|
|
|
+ }
|
|
|
|
|
+ Method list = findMethod(clazz, "list");
|
|
|
|
|
+ assertNotNull(list, "[shop.review.list] list 方法不存在,无法验证出参 Result<Map>(§3.1 R3)。实现未落地。");
|
|
|
|
|
+ assertEqualsSimpleName("Result", list.getReturnType(),
|
|
|
|
|
+ "[shop.review.list] list 应以 Result<T> 包装出参(§3.1 R3:Result<Map>)。");
|
|
|
|
|
+ assertTrue(list.getGenericReturnType().getTypeName().contains("Map"),
|
|
|
|
|
+ "[shop.review.list] list 出参泛型应为 Map 承载 {total, avgRating, records[]}(§3.1 R3)。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("正常流-5:list 为公开只读,不得携带 @RequestAttribute(\"userId\")")
|
|
|
|
|
+ void controller_listIsPublic_noUserIdAttribute() {
|
|
|
|
|
+ Class<?> clazz = loadClass(CONTROLLER);
|
|
|
|
|
+ if (clazz == null) {
|
|
|
|
|
+ fail("[shop.review.list] ProductReviewController 类不存在,无法验证 list 公开鉴权(§3.1 R3)。");
|
|
|
|
|
+ }
|
|
|
|
|
+ Method list = findMethod(clazz, "list");
|
|
|
|
|
+ assertNotNull(list, "[shop.review.list] list 方法不存在,无法验证公开鉴权(§3.1 R3)。实现未落地。");
|
|
|
|
|
+ boolean hasUserId = false;
|
|
|
|
|
+ for (Parameter p : list.getParameters()) {
|
|
|
|
|
+ RequestAttribute ra = p.getAnnotation(RequestAttribute.class);
|
|
|
|
|
+ if (ra != null && "userId".equals(ra.value())) {
|
|
|
|
|
+ hasUserId = true;
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ assertTrue(!hasUserId,
|
|
|
|
|
+ "[shop.review.list] list 是公开只读接口(鉴权=公开 + PUBLIC_PATHS),"
|
|
|
|
|
+ + "不得携带 @RequestAttribute(\"userId\");匿名可看(§3.1 R3,"
|
|
|
|
|
+ + "与同控制器 create/detail/my 的登录鉴权相反)。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("正常流-6:ProductReviewService 应提供 listByProduct(Long,int,int): Map")
|
|
|
|
|
+ void service_interfaceHasListByProduct() {
|
|
|
|
|
+ Class<?> clazz = loadClass(SERVICE);
|
|
|
|
|
+ assertNotNull(clazz,
|
|
|
|
|
+ "[shop.review.list] ProductReviewService 接口不存在(§5.3 类图)。");
|
|
|
|
|
+ Method method = findMethod(clazz, "listByProduct");
|
|
|
|
|
+ assertNotNull(method,
|
|
|
|
|
+ "[shop.review.list] ProductReviewService 应提供 listByProduct(...) 方法"
|
|
|
|
|
+ + "(R3 商品评价列表入口,合同签名 listByProduct(Long productId, int page, int size): Map,§5.3 类图)。"
|
|
|
|
|
+ + "实现未落地。");
|
|
|
|
|
+ assertTrue(method.getParameterCount() == 3,
|
|
|
|
|
+ "[shop.review.list] listByProduct 应接收 3 参(productId/page/size)(§5.3 类图)。");
|
|
|
|
|
+ assertTrue(Map.class.isAssignableFrom(method.getReturnType()),
|
|
|
|
|
+ "[shop.review.list] listByProduct 应返回 Map 承载 {total, avgRating, records[]}"
|
|
|
|
|
+ + "(§3.1 R3 出参 + §5.3 类图 listByProduct(...): Map)。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("正常流-7:ProductReviewServiceImpl 应覆盖 listByProduct")
|
|
|
|
|
+ void serviceImpl_overridesListByProduct() {
|
|
|
|
|
+ Class<?> impl = loadClass(SERVICE_IMPL);
|
|
|
|
|
+ assertNotNull(impl,
|
|
|
|
|
+ "[shop.review.list] ProductReviewServiceImpl 不存在:契约要求独立 ProductReview* 服务实现"
|
|
|
|
|
+ + "(§6 T02,活动 ReviewOrderServiceImpl 零改动)。");
|
|
|
|
|
+ Method method = findMethod(impl, "listByProduct");
|
|
|
|
|
+ assertNotNull(method,
|
|
|
|
|
+ "[shop.review.list] ProductReviewServiceImpl 应实现 listByProduct(...)"
|
|
|
|
|
+ + "(读 Mapper.statsByProduct 聚合 + Mapper.listByProduct 分页,返回 {total, avgRating, records})。"
|
|
|
|
|
+ + "实现未落地。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 权限流:公开只读必须登记 PUBLIC_PATHS(§3.1 R3 鉴权列) ====================
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("权限流:JwtInterceptor.PUBLIC_PATHS 应登记 /api/product/review/list(匿名可读)")
|
|
|
|
|
+ void jwtInterceptor_publicPathsIncludeReviewList() {
|
|
|
|
|
+ Class<?> clazz = loadClass(JWT_INTERCEPTOR);
|
|
|
|
|
+ if (clazz == null) {
|
|
|
|
|
+ fail("[shop.review.list] JwtInterceptor 类不存在,无法验证公开路径登记(§3.1 R3)。");
|
|
|
|
|
+ }
|
|
|
|
|
+ String[] publicPaths;
|
|
|
|
|
+ try {
|
|
|
|
|
+ Field field = clazz.getDeclaredField(PUBLIC_PATHS_FIELD);
|
|
|
|
|
+ field.setAccessible(true);
|
|
|
|
|
+ publicPaths = (String[]) field.get(null);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ fail("[shop.review.list] 无法反射读取 JwtInterceptor.PUBLIC_PATHS:" + e.getMessage());
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ assertTrue(publicPaths != null && Arrays.asList(publicPaths).contains(PUBLIC_PATH),
|
|
|
|
|
+ "[shop.review.list] JwtInterceptor.PUBLIC_PATHS 应登记 " + PUBLIC_PATH
|
|
|
|
|
+ + "(§3.1 R3 鉴权=公开 / 加 PUBLIC_PATHS,商品详情页匿名可读;"
|
|
|
|
|
+ + "未登记则匿名访问被 401 拦截)。实现未落地。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 边界流:数据层契约(§2.1 + §5.3,已落地锚点) ====================
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("边界流-1(锚点):ProductReviewMapper 应提供 listByProduct + statsByProduct")
|
|
|
|
|
+ void mapper_hasListAndStatsMethods() {
|
|
|
|
|
+ Class<?> clazz = loadClass(MAPPER);
|
|
|
|
|
+ assertNotNull(clazz, "[shop.review.list] ProductReviewMapper 不存在(§5.3 类图)。");
|
|
|
|
|
+ Method list = findMethod(clazz, "listByProduct");
|
|
|
|
|
+ assertNotNull(list,
|
|
|
|
|
+ "[shop.review.list] ProductReviewMapper 应提供 listByProduct(productId, offset, size)"
|
|
|
|
|
+ + "(§5.3 类图,商品详情页列表数据源)。");
|
|
|
|
|
+ assertTrue(list.getParameterCount() == 3,
|
|
|
|
|
+ "[shop.review.list] listByProduct 应接收 productId/offset/size 3 参(§5.3 类图)。");
|
|
|
|
|
+ Method stats = findMethod(clazz, "statsByProduct");
|
|
|
|
|
+ assertNotNull(stats,
|
|
|
|
|
+ "[shop.review.list] ProductReviewMapper 应提供 statsByProduct(productId)"
|
|
|
|
|
+ + "(§5.3 类图,星级聚合 total + avgRating 数据源)。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("边界流-2(锚点):Mapper XML 列表/统计仅取 status=1 且 AVG(rating) 聚合")
|
|
|
|
|
+ void mapperXml_displayOnlyAndAggregation() {
|
|
|
|
|
+ String xml = readResource("mapper/ProductReviewMapper.xml");
|
|
|
|
|
+ assertTrue(xml.contains("id=\"listByProduct\""),
|
|
|
|
|
+ "[shop.review.list] ProductReviewMapper.xml 缺少 listByProduct select(§3.1 R3 列表数据源)。");
|
|
|
|
|
+ assertTrue(xml.contains("status = 1"),
|
|
|
|
|
+ "[shop.review.list] 评价列表/统计应仅取展示态 status = 1(P2 治理隐藏评价不外显,§2.1 + §3.1 R3)。");
|
|
|
|
|
+ assertTrue(xml.contains("AVG(rating)"),
|
|
|
|
|
+ "[shop.review.list] 星级聚合应由 SQL AVG(rating) 计算(§3.1 R3:avgRating)。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("边界流-3(锚点):ProductReview 实体应承载列表渲染字段")
|
|
|
|
|
+ void entity_carriesListDisplayFields() {
|
|
|
|
|
+ Class<?> clazz = loadClass(ENTITY);
|
|
|
|
|
+ if (clazz == null) {
|
|
|
|
|
+ fail("[shop.review.list] ProductReview 实体不存在:product_reviews 落库载体(§2.1)。");
|
|
|
|
|
+ }
|
|
|
|
|
+ for (String field : LIST_DISPLAY_FIELDS) {
|
|
|
|
|
+ boolean present = false;
|
|
|
|
|
+ for (Field f : clazz.getDeclaredFields()) {
|
|
|
|
|
+ if (field.equals(f.getName())) {
|
|
|
|
|
+ present = true;
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ assertTrue(present,
|
|
|
|
|
+ "[shop.review.list] ProductReview 实体应含 " + field
|
|
|
|
|
+ + " 字段(商品详情页评价列表渲染所需,对应 " + camelToSnake(field) + ",§2.1)。");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("边界流-4(锚点):product_reviews 独立表应落地 schema.sql(含 idx_product_id)")
|
|
|
|
|
+ void schemaSql_hasProductReviewsTableWithProductIndex() {
|
|
|
|
|
+ String schema = readResource("schema.sql");
|
|
|
|
|
+ assertTrue(schema.contains(TABLE_NAME),
|
|
|
|
|
+ "[shop.review.list] schema.sql 缺少 product_reviews 建表"
|
|
|
|
|
+ + "(§2.1,Q1 拍板=独立表,不复用 review_orders 活动评价表)。");
|
|
|
|
|
+ int idx = schema.indexOf(TABLE_NAME);
|
|
|
|
|
+ String tableBlock = idx >= 0 ? schema.substring(idx, Math.min(idx + 1600, schema.length())) : "";
|
|
|
|
|
+ assertTrue(tableBlock.contains(PRODUCT_ID_INDEX),
|
|
|
|
|
+ "[shop.review.list] product_reviews 应含 " + PRODUCT_ID_INDEX
|
|
|
|
|
+ + " 索引(评价列表按 product_id 查询,§2.1 + §3.1 R3)。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 边界流:行为契约(Mockito,RED 后 GREEN 阶段生效) ====================
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("边界流-5:list 缺省 page/size 应按 §9 默认 1/20 透传服务")
|
|
|
|
|
+ void controller_listDefaultPagination() {
|
|
|
|
|
+ Class<?> clazz = loadClass(CONTROLLER);
|
|
|
|
|
+ if (clazz == null) {
|
|
|
|
|
+ fail("[shop.review.list] ProductReviewController 类不存在,无法验证分页默认值(§9)。");
|
|
|
|
|
+ }
|
|
|
|
|
+ Method list = findMethod(clazz, "list");
|
|
|
|
|
+ assertNotNull(list,
|
|
|
|
|
+ "[shop.review.list] list 方法不存在,无法验证分页默认值 page=1/size=20(§9 共享约定)。实现未落地。");
|
|
|
|
|
+ Class<?> serviceClass = loadClass(SERVICE);
|
|
|
|
|
+ assertNotNull(serviceClass, "[shop.review.list] ProductReviewService 接口不存在(§5.3)。");
|
|
|
|
|
+ try {
|
|
|
|
|
+ Object controller = clazz.getDeclaredConstructor().newInstance();
|
|
|
|
|
+ ProductReviewService serviceMock = Mockito.mock(ProductReviewService.class);
|
|
|
|
|
+ Field serviceField = findFieldByName(clazz, "reviewService");
|
|
|
|
|
+ assertNotNull(serviceField,
|
|
|
|
|
+ "[shop.review.list] ProductReviewController 应注入 reviewService(对齐 create/detail/my 字段名)。");
|
|
|
|
|
+ serviceField.setAccessible(true);
|
|
|
|
|
+ serviceField.set(controller, serviceMock);
|
|
|
|
|
+
|
|
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
|
|
+ params.put("productId", 100L); // 仅给 productId,page/size 缺省
|
|
|
|
|
+ list.invoke(controller, params);
|
|
|
|
|
+
|
|
|
|
|
+ // 服务接口当前尚无 listByProduct(RED),故用 Mockito 调用记录做反射式校验,
|
|
|
|
|
+ // 避免在测试源码中直接引用不存在的方法导致编译失败。
|
|
|
|
|
+ boolean passedDefaults = false;
|
|
|
|
|
+ for (org.mockito.invocation.Invocation inv : Mockito.mockingDetails(serviceMock).getInvocations()) {
|
|
|
|
|
+ if ("listByProduct".equals(inv.getMethod().getName())
|
|
|
|
|
+ && Arrays.equals(new Object[]{100L, 1, 20}, inv.getArguments())) {
|
|
|
|
|
+ passedDefaults = true;
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ assertTrue(passedDefaults,
|
|
|
|
|
+ "[shop.review.list] list 缺省 page/size 应以默认 1/20 调用"
|
|
|
|
|
+ + " ProductReviewService.listByProduct(productId, page, size)(§9 共享约定)。");
|
|
|
|
|
+ } catch (AssertionError e) {
|
|
|
|
|
+ throw e;
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ fail("[shop.review.list] 反射调用 list 验证分页默认值失败:" + e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("边界流-6:服务 listByProduct 应聚合 {total, avgRating, records} 只读视图")
|
|
|
|
|
+ void serviceImpl_listReturnsAggregatedMap() {
|
|
|
|
|
+ Class<?> implClass = loadClass(SERVICE_IMPL);
|
|
|
|
|
+ if (implClass == null) {
|
|
|
|
|
+ fail("[shop.review.list] ProductReviewServiceImpl 不存在,无法验证出参聚合(§3.1 R3)。");
|
|
|
|
|
+ }
|
|
|
|
|
+ Method method = findMethod(implClass, "listByProduct");
|
|
|
|
|
+ assertNotNull(method,
|
|
|
|
|
+ "[shop.review.list] ProductReviewServiceImpl 应实现 listByProduct(...),出参含"
|
|
|
|
|
+ + " {total, avgRating, records}(§3.1 R3)。实现未落地。");
|
|
|
|
|
+ try {
|
|
|
|
|
+ Object impl = implClass.getDeclaredConstructor().newInstance();
|
|
|
|
|
+ ProductReviewMapper mapperMock = Mockito.mock(ProductReviewMapper.class);
|
|
|
|
|
+ Map<String, Object> stats = new HashMap<>();
|
|
|
|
|
+ stats.put("total", 2L);
|
|
|
|
|
+ stats.put("avgRating", 4.5);
|
|
|
|
|
+ Mockito.when(mapperMock.statsByProduct(100L)).thenReturn(stats);
|
|
|
|
|
+ Mockito.when(mapperMock.listByProduct(Mockito.eq(100L), Mockito.anyInt(), Mockito.anyInt()))
|
|
|
|
|
+ .thenReturn(Collections.<ProductReview>emptyList());
|
|
|
|
|
+ setMapper(implClass, impl, mapperMock);
|
|
|
|
|
+
|
|
|
|
|
+ Object out = method.invoke(impl, 100L, 1, 20);
|
|
|
|
|
+ assertTrue(out instanceof Map,
|
|
|
|
|
+ "[shop.review.list] listByProduct 应返回 Map(§3.1 R3 出参)。");
|
|
|
|
|
+ Map<?, ?> result = (Map<?, ?>) out;
|
|
|
|
|
+ assertTrue(result.containsKey("total"),
|
|
|
|
|
+ "[shop.review.list] 出参应含 total(评价总数,§3.1 R3)。");
|
|
|
|
|
+ assertTrue(result.containsKey("avgRating"),
|
|
|
|
|
+ "[shop.review.list] 出参应含 avgRating(星级均值,§3.1 R3)。");
|
|
|
|
|
+ assertTrue(result.containsKey("records"),
|
|
|
|
|
+ "[shop.review.list] 出参应含 records[](评价记录,§3.1 R3)。");
|
|
|
|
|
+ } catch (AssertionError e) {
|
|
|
|
|
+ throw e;
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ fail("[shop.review.list] 反射调用 listByProduct 验证出参失败:" + e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 反射辅助(对齐同域 ProductReviewMyContractTest 风格) ====================
|
|
|
|
|
+
|
|
|
|
|
+ private Class<?> loadClass(String name) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ return Class.forName(name, false, Thread.currentThread().getContextClassLoader());
|
|
|
|
|
+ } catch (ClassNotFoundException e) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Method findMethod(Class<?> clazz, String name) {
|
|
|
|
|
+ for (Method m : clazz.getDeclaredMethods()) {
|
|
|
|
|
+ if (m.getName().equals(name)) {
|
|
|
|
|
+ return m;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Field findFieldByName(Class<?> clazz, String name) {
|
|
|
|
|
+ Class<?> c = clazz;
|
|
|
|
|
+ while (c != null) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ return c.getDeclaredField(name);
|
|
|
|
|
+ } catch (NoSuchFieldException e) {
|
|
|
|
|
+ c = c.getSuperclass();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void setMapper(Class<?> clazz, Object target, ProductReviewMapper mapper) throws Exception {
|
|
|
|
|
+ Field field = findFieldByName(clazz, "reviewMapper");
|
|
|
|
|
+ if (field == null) {
|
|
|
|
|
+ field = findFieldByName(clazz, "baseMapper");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (field == null) {
|
|
|
|
|
+ throw new NoSuchFieldException("reviewMapper / baseMapper 均不存在");
|
|
|
|
|
+ }
|
|
|
|
|
+ field.setAccessible(true);
|
|
|
|
|
+ field.set(target, mapper);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void assertEqualsSimpleName(String expected, Class<?> actual, String message) {
|
|
|
|
|
+ assertTrue(actual != null && expected.equals(actual.getSimpleName()), message);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private String camelToSnake(String camel) {
|
|
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
|
|
+ for (char c : camel.toCharArray()) {
|
|
|
|
|
+ if (Character.isUpperCase(c)) {
|
|
|
|
|
+ sb.append('_').append(Character.toLowerCase(c));
|
|
|
|
|
+ } else {
|
|
|
|
|
+ sb.append(c);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return sb.toString();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private String readResource(String path) {
|
|
|
|
|
+ try (InputStream is = getClass().getClassLoader().getResourceAsStream(path)) {
|
|
|
|
|
+ if (is == null) {
|
|
|
|
|
+ return "";
|
|
|
|
|
+ }
|
|
|
|
|
+ try (Scanner scanner = new Scanner(is, StandardCharsets.UTF_8.name())) {
|
|
|
|
|
+ scanner.useDelimiter("\\A");
|
|
|
|
|
+ return scanner.hasNext() ? scanner.next() : "";
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ return "";
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|