|
|
@@ -0,0 +1,271 @@
|
|
|
+package com.etotem.cfc.unit;
|
|
|
+
|
|
|
+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.RequestAttribute;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+
|
|
|
+import java.lang.reflect.Field;
|
|
|
+import java.lang.reflect.Method;
|
|
|
+import java.lang.reflect.Parameter;
|
|
|
+import java.util.Arrays;
|
|
|
+
|
|
|
+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.detail},任务 S-be-2,
|
|
|
+ * TDD-RED 阶段)。
|
|
|
+ *
|
|
|
+ * <p><b>契约溯源</b></p>
|
|
|
+ * <ul>
|
|
|
+ * <li>需求:{@code docs/superpowers/specs/2026-11-14-product-order-gap-prd.md} §3 P0-A
|
|
|
+ * (completed 的普通商品订单可评价、商品详情页可展示评价列表;订单详情页「去评价」按钮态——
|
|
|
+ * completed 且未评价时;已合入主线 85a64e02b)</li>
|
|
|
+ * <li>设计:{@code docs/superpowers/specs/2026-11-14-product-order-gap-arch-design.md} §3.1 R2
|
|
|
+ * ({@code POST /api/product/review/detail},入参 {@code {orderId}},出参
|
|
|
+ * {@code Result<ProductReviewResponse>}(含 {@code review} / {@code canReview} /
|
|
|
+ * {@code reviewHint});已合入主线 85a64e02b)</li>
|
|
|
+ * </ul>
|
|
|
+ *
|
|
|
+ * <p><b>R2 契约(本测试断言锚点)</b></p>
|
|
|
+ * <ul>
|
|
|
+ * <li>路由:{@code POST /api/product/review/detail},鉴权=登录(下单人/商家)——Controller 方法必须
|
|
|
+ * 携带 {@code @RequestAttribute("userId")}(对齐既有 {@code ProductOrderController} 同点位写法)</li>
|
|
|
+ * <li>入参:{@code {orderId}}(订单详情页上下文,订单主键)</li>
|
|
|
+ * <li>出参:{@code Result<ProductReviewResponse>},DTO 含 {@code review}(已评记录,未评为 null)、
|
|
|
+ * {@code canReview}(Boolean:completed 且 90 天窗口内且未评时 true)、
|
|
|
+ * {@code reviewHint}(String:不可评/已评提示文案,供前端按钮态兜底)</li>
|
|
|
+ * <li>服务契约:{@code ProductReviewService.detail(Long orderId, Long userId)}(§5.3 类图;
|
|
|
+ * 实现依赖 {@code ProductReviewMapper.findByOrderId} 读已评 + {@code ProductOrderMapper} 读订单,
|
|
|
+ * 判定「去评价」按钮态)</li>
|
|
|
+ * <li>行为判定(GREEN 后由实现承担,本测试以结构契约先钉死宿主):{@code review==null 且可评时
|
|
|
+ * {@code canReview=true}};已评时 {@code review!=null};非 completed/超 90 天窗口 → {@code canReview=false}
|
|
|
+ * 且 {@code reviewHint} 非空提示(PRD §3 P0-A 验收3 + §5 UI 订单详情「去评价」按钮)</li>
|
|
|
+ * </ul>
|
|
|
+ *
|
|
|
+ * <p><b>实现现状</b>:本分支 src/main 中 {@code ProductReviewController} /
|
|
|
+ * {@code ProductReviewService} / {@code ProductReviewServiceImpl} / {@code ProductReviewResponse} DTO
|
|
|
+ * 均 <b>未落地</b>(已落地仅 {@code ProductReview} 实体 + {@code ProductReviewMapper},来自 T01 数据层
|
|
|
+ * 24fb6e22d)。因此本测试以「反射结构契约」探测实现缺失:能编译通过、能运行、断言失败(红),
|
|
|
+ * 失败断言原文清晰可贴(对齐同批 {@code ProductReviewCreateContractTest}(S-be-1)与
|
|
|
+ * {@code TeamProfitHookContractTest} 风格)。</p>
|
|
|
+ *
|
|
|
+ * <p><b>RED 预期</b>:全部断言在宿主类/DTO 缺失处失败(红);GREEN 阶段实现方按上述契约清单
|
|
|
+ * 落地 {@code ProductReviewController.detail} / {@code ProductReviewService.detail} /
|
|
|
+ * {@code ProductReviewResponse} 后转绿。</p>
|
|
|
+ *
|
|
|
+ * <p><b>测试范围</b>:纯契约级,无 Spring / 无 DB / 无外部服务。</p>
|
|
|
+ */
|
|
|
+class ProductReviewDetailContractTest {
|
|
|
+
|
|
|
+ /** R2 控制器(设计 §3.1,前缀 /api/product/review)。 */
|
|
|
+ private static final String CONTROLLER = "com.etotem.cfc.controller.product.ProductReviewController";
|
|
|
+ private static final String CONTROLLER_PREFIX = "/api/product/review";
|
|
|
+
|
|
|
+ /** R2 服务接口 + 实现(设计 §5.3 类图)。 */
|
|
|
+ private static final String SERVICE = "com.etotem.cfc.service.ProductReviewService";
|
|
|
+ private static final String SERVICE_IMPL = "com.etotem.cfc.service.ProductReviewServiceImpl";
|
|
|
+
|
|
|
+ /** R2 出参 DTO(设计 §3.1 R2:review / canReview / reviewHint)。 */
|
|
|
+ private static final String RESPONSE_DTO = "com.etotem.cfc.dto.ProductReviewResponse";
|
|
|
+
|
|
|
+ /** §3.1 R2 出参字段(已评记录 + 去评价按钮态 + 提示文案)。 */
|
|
|
+ private static final String[] RESPONSE_FIELDS = {"review", "canReview", "reviewHint"};
|
|
|
+
|
|
|
+ /** §5.3 类图:ProductReviewServiceImpl 依赖(读已评 + 读订单)。 */
|
|
|
+ private static final String[] SERVICE_IMPL_DEP_FIELDS = {"reviewMapper", "orderMapper"};
|
|
|
+
|
|
|
+ // ==================== 正常流:Controller 契约(§3.1 R2 路由) ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流-1:ProductReviewController 类应存在")
|
|
|
+ void controller_classExists() {
|
|
|
+ assertNotNull(loadClass(CONTROLLER),
|
|
|
+ "[shop.review.detail] ProductReviewController 类不存在:契约要求新增独立控制器,"
|
|
|
+ + "路由前缀 /api/product/review(设计 §3.1 R2,PRD §3 P0-A)。实现未落地。");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流-2:控制器应映射到 /api/product/review 前缀")
|
|
|
+ void controller_classMappingPrefix() {
|
|
|
+ Class<?> clazz = loadClass(CONTROLLER);
|
|
|
+ if (clazz == null) {
|
|
|
+ fail("[shop.review.detail] ProductReviewController 类不存在,无法验证前缀映射(§3.1 R2)。");
|
|
|
+ }
|
|
|
+ boolean mapped = false;
|
|
|
+ for (java.lang.annotation.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.detail] ProductReviewController 类级映射应含 " + CONTROLLER_PREFIX
|
|
|
+ + "(§3.1 R2 前缀 /api/product/review)。");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流-3:detail 方法应标注 POST /detail(完整路由 /api/product/review/detail)")
|
|
|
+ void controller_detailPostMapping() {
|
|
|
+ Class<?> clazz = loadClass(CONTROLLER);
|
|
|
+ if (clazz == null) {
|
|
|
+ fail("[shop.review.detail] ProductReviewController 类不存在,无法验证 detail 路由(§3.1 R2)。");
|
|
|
+ }
|
|
|
+ Method detail = findMethod(clazz, "detail");
|
|
|
+ assertNotNull(detail,
|
|
|
+ "[shop.review.detail] ProductReviewController 应提供 detail 方法"
|
|
|
+ + "(POST /api/product/review/detail,§3.1 R2)。");
|
|
|
+ PostMapping pm = detail.getAnnotation(PostMapping.class);
|
|
|
+ assertNotNull(pm,
|
|
|
+ "[shop.review.detail] detail 方法应标注 @PostMapping(统一 @PostMapping 规约,§3.1 R2)。");
|
|
|
+ boolean routeOk = Arrays.asList(pm.value()).contains("/detail")
|
|
|
+ || Arrays.asList(pm.path()).contains("/detail");
|
|
|
+ assertTrue(routeOk,
|
|
|
+ "[shop.review.detail] detail 应映射 POST /detail(完整路由 /api/product/review/detail,§3.1 R2)。");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流-4:detail 应携带 @RequestAttribute(\"userId\")(登录下单人/商家)")
|
|
|
+ void controller_detailCarriesBuyerIdentity() {
|
|
|
+ Class<?> clazz = loadClass(CONTROLLER);
|
|
|
+ if (clazz == null) {
|
|
|
+ fail("[shop.review.detail] ProductReviewController 类不存在,无法验证鉴权参数(§3.1 R2)。");
|
|
|
+ }
|
|
|
+ Method detail = findMethod(clazz, "detail");
|
|
|
+ assertNotNull(detail, "[shop.review.detail] detail 方法不存在(§3.1 R2)。");
|
|
|
+ boolean hasUserId = false;
|
|
|
+ for (Parameter p : detail.getParameters()) {
|
|
|
+ RequestAttribute ra = p.getAnnotation(RequestAttribute.class);
|
|
|
+ if (ra != null && "userId".equals(ra.value())) {
|
|
|
+ hasUserId = true;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ assertTrue(hasUserId,
|
|
|
+ "[shop.review.detail] detail 应携带 @RequestAttribute(\"userId\") 读取登录身份"
|
|
|
+ + "(鉴权=登录下单人/商家,对齐 ProductOrderController 同点位写法,§3.1 R2 鉴权列)。");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 正常流:Service 契约(§3.1 R2 + §5.3 类图) ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流-5:ProductReviewService 接口应存在并提供 detail 契约方法")
|
|
|
+ void service_interfaceHasDetail() {
|
|
|
+ Class<?> clazz = loadClass(SERVICE);
|
|
|
+ assertNotNull(clazz,
|
|
|
+ "[shop.review.detail] ProductReviewService 接口不存在:独立 ProductReview* 服务契约(§5.3 类图)。"
|
|
|
+ + "实现未落地。");
|
|
|
+ assertTrue(findMethod(clazz, "detail") != null,
|
|
|
+ "[shop.review.detail] ProductReviewService 应提供 detail(...) 方法"
|
|
|
+ + "(R2 详情入口,合同签名 detail(Long orderId, Long userId),§5.3)。");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流-6:ProductReviewServiceImpl 应落地并实现 ProductReviewService")
|
|
|
+ void serviceImpl_implementsInterface() {
|
|
|
+ Class<?> impl = loadClass(SERVICE_IMPL);
|
|
|
+ assertNotNull(impl,
|
|
|
+ "[shop.review.detail] ProductReviewServiceImpl 不存在:契约要求独立 ProductReview* 服务实现"
|
|
|
+ + "(PRD §3 P0-A:活动 ReviewOrderServiceImpl 零改动,不走复用)。实现未落地。");
|
|
|
+ assertTrue(SERVICE_IMPL.equals(impl.getName()),
|
|
|
+ "[shop.review.detail] ProductReviewServiceImpl 应实现 ProductReviewService(§5.3 类图)。");
|
|
|
+ boolean anyImpl = false;
|
|
|
+ for (Class<?> itf : impl.getInterfaces()) {
|
|
|
+ if (SERVICE.equals(itf.getName())) {
|
|
|
+ anyImpl = true;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ assertTrue(anyImpl,
|
|
|
+ "[shop.review.detail] ProductReviewServiceImpl 应实现 ProductReviewService 接口(§5.3 类图)。");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流-7:ProductReviewServiceImpl 应注入 reviewMapper/orderMapper(读已评+读订单)")
|
|
|
+ void serviceImpl_wiresReviewAndOrderMappers() {
|
|
|
+ Class<?> impl = loadClass(SERVICE_IMPL);
|
|
|
+ if (impl == null) {
|
|
|
+ fail("[shop.review.detail] ProductReviewServiceImpl 不存在,无法验证依赖注入(§5.3 类图)。");
|
|
|
+ }
|
|
|
+ for (String field : SERVICE_IMPL_DEP_FIELDS) {
|
|
|
+ boolean present = false;
|
|
|
+ for (Field f : impl.getDeclaredFields()) {
|
|
|
+ if (field.equals(f.getName())) {
|
|
|
+ present = true;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ assertTrue(present,
|
|
|
+ "[shop.review.detail] ProductReviewServiceImpl 应注入 " + field
|
|
|
+ + "(§5.3 类图:reviewMapper 读已评 + orderMapper 读订单,判定「去评价」按钮态)。");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 边界流:出参 DTO 契约(§3.1 R2:review/canReview/reviewHint) ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("边界流-1:ProductReviewResponse 出参 DTO 应含 review/canReview/reviewHint")
|
|
|
+ void responseDto_carriesR2OutputFields() {
|
|
|
+ Class<?> clazz = loadClass(RESPONSE_DTO);
|
|
|
+ if (clazz == null) {
|
|
|
+ fail("[shop.review.detail] ProductReviewResponse DTO 不存在:R2 出参"
|
|
|
+ + " {review, canReview, reviewHint}(§3.1 R2,订单详情「去评价」按钮态载体)。实现未落地。");
|
|
|
+ }
|
|
|
+ for (String field : RESPONSE_FIELDS) {
|
|
|
+ boolean present = false;
|
|
|
+ for (Field f : clazz.getDeclaredFields()) {
|
|
|
+ if (field.equals(f.getName())) {
|
|
|
+ present = true;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ assertTrue(present,
|
|
|
+ "[shop.review.detail] ProductReviewResponse 应含 " + field
|
|
|
+ + " 出参(§3.1 R2:review 已评记录 / canReview 可评态 / reviewHint 提示文案)。");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("边界流-2:canReview 应为布尔、reviewHint 应为字符串(前端按钮态判定载体)")
|
|
|
+ void responseDto_canReviewBooleanAndHintString() {
|
|
|
+ Class<?> clazz = loadClass(RESPONSE_DTO);
|
|
|
+ if (clazz == null) {
|
|
|
+ fail("[shop.review.detail] ProductReviewResponse DTO 不存在,无法验证字段类型(§3.1 R2)。");
|
|
|
+ }
|
|
|
+ for (Field f : clazz.getDeclaredFields()) {
|
|
|
+ if ("canReview".equals(f.getName())) {
|
|
|
+ assertTrue(f.getType() == Boolean.class || f.getType() == boolean.class,
|
|
|
+ "[shop.review.detail] ProductReviewResponse.canReview 应为 Boolean"
|
|
|
+ + "(§3.1 R2:completed 且 90 天窗口内且未评时 true)。");
|
|
|
+ }
|
|
|
+ if ("reviewHint".equals(f.getName())) {
|
|
|
+ assertTrue(f.getType() == String.class,
|
|
|
+ "[shop.review.detail] ProductReviewResponse.reviewHint 应为 String"
|
|
|
+ + "(§3.1 R2:不可评/已评提示文案,供按钮态兜底)。");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 反射辅助(对齐 ProductReviewCreateContractTest 风格) ====================
|
|
|
+
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+}
|