|
|
@@ -0,0 +1,224 @@
|
|
|
+package com.train.service.impl;
|
|
|
+
|
|
|
+import com.train.common.Result;
|
|
|
+import com.train.entity.TrainEvalRecord;
|
|
|
+import com.train.entity.TrainServiceOrder;
|
|
|
+import com.train.mapper.TrainEvalRecordMapper;
|
|
|
+import com.train.mapper.TrainPractitionerProfileMapper;
|
|
|
+import com.train.mapper.TrainServiceOrderMapper;
|
|
|
+import com.train.service.CertService;
|
|
|
+import com.train.service.EvalService;
|
|
|
+import com.train.service.PractitionerService;
|
|
|
+import com.train.service.RoleGuard;
|
|
|
+import org.junit.jupiter.api.BeforeEach;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+import org.mockito.ArgumentCaptor;
|
|
|
+import org.springframework.test.util.ReflectionTestUtils;
|
|
|
+
|
|
|
+import java.lang.reflect.Method;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.Locale;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
|
+import static org.mockito.ArgumentMatchers.any;
|
|
|
+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;
|
|
|
+
|
|
|
+/**
|
|
|
+ * {@code train.eval.customer} 契约边界补强单测(TDD-RED,S-train-28 二次派发;TC-1028 / TC-3004 / TC-2009)。
|
|
|
+ *
|
|
|
+ * <p>与 {@link TrainEvalCustomerContractTest} 同目录、同被测面,但本类刻意补足三点既有测试未覆盖的缺口:</p>
|
|
|
+ * <ol>
|
|
|
+ * <li>C4092 由「结构上存在带 replay/key 的入口」补强为「同 order_id 幂等键、异评分/评语载荷真实返回 4092」;</li>
|
|
|
+ * <li>replay=true 的反射查找限定为「客户评价入口」(名称含 client/customer),避免被 org/coach 入口误满足;</li>
|
|
|
+ * <li>C422 补强客户评价落库实体须初始化 version(乐观锁版本),当前 submitClient 未设置。</li>
|
|
|
+ * </ol>
|
|
|
+ *
|
|
|
+ * <p>只写测试、不改任何 src/main 实现。纯 Mockito(不启 Spring、不连 DB)。契约错误码:
|
|
|
+ * 4092 同键异载荷、422 乐观锁冲突、404 工单不存在、403 非 worker 越权评价。</p>
|
|
|
+ */
|
|
|
+class TrainEvalCustomerBoundaryContractTest {
|
|
|
+
|
|
|
+ private static final int CODE_OK = 200;
|
|
|
+ private static final int CODE_SAME_KEY_DIFF_PAYLOAD = 4092;
|
|
|
+ private static final int CODE_VERSION_CONFLICT = 422;
|
|
|
+ private static final int CODE_NOT_FOUND = 404;
|
|
|
+ private static final int CODE_FORBIDDEN = 403;
|
|
|
+
|
|
|
+ private static final Long WORKER_ID = 3001L;
|
|
|
+ private static final Long OUTSIDER_ID = 999L;
|
|
|
+ private static final Long ORDER_ID = 6001L;
|
|
|
+
|
|
|
+ private EvalServiceImpl service;
|
|
|
+ private TrainEvalRecordMapper evalMapper;
|
|
|
+ private TrainServiceOrderMapper orderMapper;
|
|
|
+
|
|
|
+ @BeforeEach
|
|
|
+ void setUp() {
|
|
|
+ service = new EvalServiceImpl();
|
|
|
+ evalMapper = mock(TrainEvalRecordMapper.class);
|
|
|
+ orderMapper = mock(TrainServiceOrderMapper.class);
|
|
|
+
|
|
|
+ ReflectionTestUtils.setField(service, "trainEvalRecordMapper", evalMapper);
|
|
|
+ ReflectionTestUtils.setField(service, "trainServiceOrderMapper", orderMapper);
|
|
|
+ ReflectionTestUtils.setField(service, "trainPractitionerProfileMapper", mock(TrainPractitionerProfileMapper.class));
|
|
|
+ ReflectionTestUtils.setField(service, "practitionerService", mock(PractitionerService.class));
|
|
|
+ ReflectionTestUtils.setField(service, "certService", mock(CertService.class));
|
|
|
+ ReflectionTestUtils.setField(service, "roleGuard", mock(RoleGuard.class));
|
|
|
+ }
|
|
|
+
|
|
|
+ private static TrainServiceOrder confirmedOrder() {
|
|
|
+ TrainServiceOrder o = new TrainServiceOrder();
|
|
|
+ o.setId(ORDER_ID);
|
|
|
+ o.setOrderNo("HG2026120200000001");
|
|
|
+ o.setWorkerId(WORKER_ID);
|
|
|
+ o.setOrgId(55L);
|
|
|
+ o.setStatus("confirmed");
|
|
|
+ o.setCreatedAt(new Date(System.currentTimeMillis() - 86_400_000L));
|
|
|
+ o.setUpdatedAt(new Date(System.currentTimeMillis() - 86_400_000L));
|
|
|
+ return o;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static TrainEvalRecord existingEval() {
|
|
|
+ TrainEvalRecord r = new TrainEvalRecord();
|
|
|
+ r.setId(7002L);
|
|
|
+ r.setEvalType("client");
|
|
|
+ r.setOrderId(ORDER_ID);
|
|
|
+ r.setUserId(WORKER_ID);
|
|
|
+ r.setScorerId(WORKER_ID);
|
|
|
+ r.setScore(4);
|
|
|
+ r.setDimension("service");
|
|
|
+ r.setComment("服务很好");
|
|
|
+ r.setStatus("valid");
|
|
|
+ r.setDeleted(0);
|
|
|
+ r.setCreatedAt(new Date());
|
|
|
+ return r;
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 边界 0:幂等重放 replay=true ====================
|
|
|
+
|
|
|
+ /** 客户评价提交入口必须显式携带 replay/key,才能支撑重放去重与对账(org/coach 入口不算数)。 */
|
|
|
+ @Test
|
|
|
+ void customerReplayEntryRequiredForIdempotentReplay() {
|
|
|
+ Method entry = findCustomerReplayEntry();
|
|
|
+
|
|
|
+ assertNotNull(entry,
|
|
|
+ "train.eval.customer 幂等重放 replay=true 要求 EvalService 暴露「客户评价」提交入口:"
|
|
|
+ + "方法名含 client/customer、返回 Result、参数 ≥5 且含 Boolean replay / String 幂等键 / Long orderId;"
|
|
|
+ + "当前仅有 submitClient(Long,Long,Integer,String) 裸入口,org/coach 入口不能替代客户评价契约");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 边界 1:同键异载荷 C4092 ====================
|
|
|
+
|
|
|
+ /** 同一 order_id 幂等键重放但评分/评语改变,必须 C4092,禁止静默返回首次评价。 */
|
|
|
+ @Test
|
|
|
+ void sameOrderIdWithDifferentPayloadReturnsC4092() {
|
|
|
+ when(orderMapper.selectById(ORDER_ID)).thenReturn(confirmedOrder());
|
|
|
+ // 首次无历史评价 → 落库;第二次命中同 order_id 但载荷不同
|
|
|
+ when(evalMapper.selectOne(any())).thenReturn(null, existingEval());
|
|
|
+
|
|
|
+ Result<Map<String, Object>> first = service.submitClient(WORKER_ID, ORDER_ID, 5, "满意");
|
|
|
+ assertEquals(CODE_OK, first.getCode());
|
|
|
+
|
|
|
+ Result<Map<String, Object>> second = service.submitClient(WORKER_ID, ORDER_ID, 3, "不满意");
|
|
|
+
|
|
|
+ assertEquals(CODE_SAME_KEY_DIFF_PAYLOAD, second.getCode(),
|
|
|
+ "同一 order_id 幂等键携带不同评分/评语载荷必须返回 C4092(4092),"
|
|
|
+ + "不得静默返回首次评价(当前实现返回 200 + 原记录)");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 边界 2:乐观锁冲突 C422 ====================
|
|
|
+
|
|
|
+ /** 客户评价落库实体必须初始化 version(乐观锁版本),供并发冲突判定返回 C422。 */
|
|
|
+ @Test
|
|
|
+ void clientEvalPersistsWithVersionForC422() {
|
|
|
+ when(orderMapper.selectById(ORDER_ID)).thenReturn(confirmedOrder());
|
|
|
+
|
|
|
+ Result<Map<String, Object>> r = service.submitClient(WORKER_ID, ORDER_ID, 5, "评价");
|
|
|
+
|
|
|
+ assertEquals(CODE_OK, r.getCode());
|
|
|
+ ArgumentCaptor<TrainEvalRecord> cap = ArgumentCaptor.forClass(TrainEvalRecord.class);
|
|
|
+ verify(evalMapper, times(1)).insert(cap.capture());
|
|
|
+ assertNotNull(cap.getValue().getVersion(),
|
|
|
+ "客户评价落库须初始化 version=1 以支撑并发 C422(422) 判定;当前 submitClient 未设置 version");
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 落库影响 0 行(并发丢失更新)必须 C422,不得忽略 insert 返回行数按 200 成功。 */
|
|
|
+ @Test
|
|
|
+ void staleVersionInsertReturnsC422ForCustomer() {
|
|
|
+ when(orderMapper.selectById(ORDER_ID)).thenReturn(confirmedOrder());
|
|
|
+ when(evalMapper.insert(any(TrainEvalRecord.class))).thenReturn(0);
|
|
|
+
|
|
|
+ Result<Map<String, Object>> r = service.submitClient(WORKER_ID, ORDER_ID, 5, "评价");
|
|
|
+
|
|
|
+ assertEquals(CODE_VERSION_CONFLICT, r.getCode(),
|
|
|
+ "客户评价落库影响 0 行(并发丢失更新/陈旧版本)必须返回 C422(422),"
|
|
|
+ + "不得忽略 insert 返回行数按 200 成功");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 边界 3:不存在 C404 ====================
|
|
|
+
|
|
|
+ /** 工单查无 → C404,须可区分「不存在」与「系统错误」,不得笼统 500。 */
|
|
|
+ @Test
|
|
|
+ void missingOrderReturns404NotGeneric500() {
|
|
|
+ when(orderMapper.selectById(ORDER_ID)).thenReturn(null);
|
|
|
+
|
|
|
+ Result<Map<String, Object>> r = service.submitClient(WORKER_ID, ORDER_ID, 5, "评价");
|
|
|
+
|
|
|
+ assertEquals(CODE_NOT_FOUND, r.getCode(),
|
|
|
+ "train.eval.customer 契约要求工单不存在时返回 C404(404) 以区分系统错误;当前实现返回笼统 500");
|
|
|
+ verify(evalMapper, never()).insert(any(TrainEvalRecord.class));
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 边界 4:越权 C403 ====================
|
|
|
+
|
|
|
+ /** 非工单 worker 提交客户评价 → C403,不得降级为笼统 500。 */
|
|
|
+ @Test
|
|
|
+ void nonWorkerCustomerEvalReturns403() {
|
|
|
+ when(orderMapper.selectById(ORDER_ID)).thenReturn(confirmedOrder());
|
|
|
+
|
|
|
+ Result<Map<String, Object>> r = service.submitClient(OUTSIDER_ID, ORDER_ID, 5, "评价");
|
|
|
+
|
|
|
+ assertEquals(CODE_FORBIDDEN, r.getCode(),
|
|
|
+ "非工单 worker 越权评价必须返回 C403(403),不得降级为笼统 500");
|
|
|
+ verify(evalMapper, never()).insert(any(TrainEvalRecord.class));
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 反射辅助 ====================
|
|
|
+
|
|
|
+ /** 仅查找「客户评价」提交入口:名称含 client/customer、Result 返回、≥minParams 参数、Boolean replay + String key + Long orderId。 */
|
|
|
+ private static Method findCustomerReplayEntry() {
|
|
|
+ for (Method m : EvalService.class.getMethods()) {
|
|
|
+ String name = m.getName().toLowerCase(Locale.ROOT);
|
|
|
+ if (!name.contains("client") && !name.contains("customer")) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (!Result.class.isAssignableFrom(m.getReturnType()) || m.getParameterCount() < 5) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ boolean replay = false;
|
|
|
+ boolean key = false;
|
|
|
+ boolean order = false;
|
|
|
+ for (Class<?> p : m.getParameterTypes()) {
|
|
|
+ if (Boolean.class.equals(p) || Boolean.TYPE.equals(p)) {
|
|
|
+ replay = true;
|
|
|
+ }
|
|
|
+ if (String.class.equals(p)) {
|
|
|
+ key = true;
|
|
|
+ }
|
|
|
+ if (Long.class.equals(p) || Long.TYPE.equals(p)) {
|
|
|
+ order = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (replay && key && order) {
|
|
|
+ return m;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+}
|