|
|
@@ -0,0 +1,224 @@
|
|
|
+package com.train.service.impl;
|
|
|
+
|
|
|
+import com.train.common.Result;
|
|
|
+import com.train.entity.TrainServiceOrder;
|
|
|
+import com.train.mapper.TrainOrderDispatchMapper;
|
|
|
+import com.train.mapper.TrainServiceOrderMapper;
|
|
|
+import com.train.mapper.TrainUserMapper;
|
|
|
+import com.train.service.ServiceOrderService;
|
|
|
+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.Field;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+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.mockingDetails;
|
|
|
+import static org.mockito.Mockito.times;
|
|
|
+import static org.mockito.Mockito.verify;
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
+
|
|
|
+/**
|
|
|
+ * {@code train.order.arrive} 契约边界单测(TDD-RED · task/S-train-24)。
|
|
|
+ *
|
|
|
+ * <p>需求锚点:{@code docs/tdd/SRS.md} 场景定义 §2 域四 S1-S1(Must·V1 复用),
|
|
|
+ * 用例 TC-1024 / TC-3004 / TC-2006 / TC-2008:经纬度+照片 → arrived,复用 /api/order/arrive,
|
|
|
+ * 到岗执行率可统计。设计侧本期 ⏳ 未契约化,故本类只声明契约判据,不改任何 {@code src/main} 代码。</p>
|
|
|
+ *
|
|
|
+ * <p>契约错误码({@code Result.code}):C4092=4092 同幂等键异载荷;C422=422 乐观锁冲突;
|
|
|
+ * C404=404 工单不存在;C403=403 越权打卡。幂等重放以 {@code replay=true} + 稳定键表达。</p>
|
|
|
+ *
|
|
|
+ * <p>RED 预期:当前 {@code ServiceOrderService#arrive} 仅 3 入参、错误一律 {@code Result.error}(500)、
|
|
|
+ * arrived→arrived 每次都重写 arriveAt、实体无 version 字段、updateById 返回行数被忽略、
|
|
|
+ * 越权仅靠 SQL 过滤折叠为普通成功/失败。故本类除基线用例外应全部失败。</p>
|
|
|
+ */
|
|
|
+class TrainOrderArriveBoundaryContractTest {
|
|
|
+
|
|
|
+ private static final int CODE_OK = 200;
|
|
|
+ private static final int CODE_CONFLICT_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 = 4101L;
|
|
|
+ private static final Long OUTSIDER_ID = 9999L;
|
|
|
+ private static final Long ORDER_ID = 6103L;
|
|
|
+ private static final Double LAT = 31.2304D;
|
|
|
+ private static final Double LNG = 121.4737D;
|
|
|
+ private static final String PHOTO_A = "https://aimate.s3.cn-north-1.jdcloud-oss.com/uploads/arrive-a.png";
|
|
|
+ private static final String PHOTO_B = "https://aimate.s3.cn-north-1.jdcloud-oss.com/uploads/arrive-b.png";
|
|
|
+ private static final String IDEMPOTENCY_KEY = "ARRIVE-6103-20261202";
|
|
|
+
|
|
|
+ private ServiceOrderService service;
|
|
|
+ private TrainServiceOrderMapper orderMapper;
|
|
|
+
|
|
|
+ @BeforeEach
|
|
|
+ void setUp() {
|
|
|
+ service = new ServiceOrderService();
|
|
|
+ orderMapper = mock(TrainServiceOrderMapper.class);
|
|
|
+ ReflectionTestUtils.setField(service, "orderMapper", orderMapper);
|
|
|
+ ReflectionTestUtils.setField(service, "dispatchMapper", mock(TrainOrderDispatchMapper.class));
|
|
|
+ ReflectionTestUtils.setField(service, "userMapper", mock(TrainUserMapper.class));
|
|
|
+ }
|
|
|
+
|
|
|
+ private static TrainServiceOrder order(String status) {
|
|
|
+ TrainServiceOrder o = new TrainServiceOrder();
|
|
|
+ o.setId(ORDER_ID);
|
|
|
+ o.setOrderNo("HG2026120200000003");
|
|
|
+ o.setWorkerId(WORKER_ID);
|
|
|
+ o.setStatus(status);
|
|
|
+ o.setArriveAt(new Date(1_700_000_000_000L));
|
|
|
+ o.setArriveLat(LAT);
|
|
|
+ o.setArriveLng(LNG);
|
|
|
+ o.setArrivePhoto(PHOTO_A);
|
|
|
+ return o;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Map<String, Object> arrival(boolean replay, String key, String photo) {
|
|
|
+ Map<String, Object> body = new HashMap<String, Object>();
|
|
|
+ body.put("replay", replay);
|
|
|
+ body.put("key", key);
|
|
|
+ body.put("lat", LAT);
|
|
|
+ body.put("lng", LNG);
|
|
|
+ body.put("photo", photo);
|
|
|
+ return body;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static long arrivalsWritten(TrainServiceOrderMapper mapper) {
|
|
|
+ return mockingDetails(mapper).getInvocations().stream()
|
|
|
+ .filter(i -> "updateById".equals(i.getMethod().getName()))
|
|
|
+ .count();
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 基线:合法到岗留痕(应对照通过) ====================
|
|
|
+
|
|
|
+ /** accepted → arrived:经纬度 + 门牌照 + 服务端时间戳一并落库。 */
|
|
|
+ @Test
|
|
|
+ void acceptedArrivePersistsEvidenceAndServerTimestamp() {
|
|
|
+ when(orderMapper.selectOne(any())).thenReturn(order("accepted"));
|
|
|
+ when(orderMapper.updateById(any(TrainServiceOrder.class))).thenReturn(1);
|
|
|
+
|
|
|
+ Result<Boolean> r = service.arrive(WORKER_ID, ORDER_ID, arrival(false, null, PHOTO_A));
|
|
|
+
|
|
|
+ assertEquals(CODE_OK, r.getCode());
|
|
|
+ assertEquals(Boolean.TRUE, r.getData());
|
|
|
+ ArgumentCaptor<TrainServiceOrder> cap = ArgumentCaptor.forClass(TrainServiceOrder.class);
|
|
|
+ verify(orderMapper, times(1)).updateById(cap.capture());
|
|
|
+ assertEquals("arrived", cap.getValue().getStatus());
|
|
|
+ assertEquals(LAT, cap.getValue().getArriveLat());
|
|
|
+ assertEquals(LNG, cap.getValue().getArriveLng());
|
|
|
+ assertEquals(PHOTO_A, cap.getValue().getArrivePhoto());
|
|
|
+ assertNotNull(cap.getValue().getArriveAt(), "到岗须写服务端时间戳");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 边界 0:幂等重放 replay=true ====================
|
|
|
+
|
|
|
+ /** 同一键以 replay=true 重放:命中既有到岗事件,不得再次写 arriveAt/定位/门牌照。 */
|
|
|
+ @Test
|
|
|
+ void replayTrueArrivalMustBeIdempotent() {
|
|
|
+ when(orderMapper.selectOne(any())).thenReturn(order("arrived"));
|
|
|
+ when(orderMapper.updateById(any(TrainServiceOrder.class))).thenReturn(1);
|
|
|
+
|
|
|
+ Result<Boolean> r = service.arrive(WORKER_ID, ORDER_ID, arrival(true, IDEMPOTENCY_KEY, PHOTO_A));
|
|
|
+ Result<Boolean> replay = service.arrive(WORKER_ID, ORDER_ID, arrival(true, IDEMPOTENCY_KEY, PHOTO_A));
|
|
|
+
|
|
|
+ assertEquals(CODE_OK, r.getCode());
|
|
|
+ assertEquals(CODE_OK, replay.getCode());
|
|
|
+ assertEquals(0L, arrivalsWritten(orderMapper),
|
|
|
+ "replay=true 同一幂等键重放到岗打卡必须幂等短路,不得再次 updateById 刷新 arriveAt/定位/门牌照;"
|
|
|
+ + "当前 arrive(Long,Long,Map) 无 replay 入参且 arrived→arrived 每次都重写留痕");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 边界 1:同键异载荷 C4092 ====================
|
|
|
+
|
|
|
+ /** 同一幂等键换照片/定位载荷:须显式报 C4092,不得按新事件静默覆盖。 */
|
|
|
+ @Test
|
|
|
+ void sameKeyWithDifferentPayloadMustReturn4092() {
|
|
|
+ when(orderMapper.selectOne(any())).thenReturn(order("accepted"));
|
|
|
+ when(orderMapper.updateById(any(TrainServiceOrder.class))).thenReturn(1);
|
|
|
+
|
|
|
+ Result<Boolean> first = service.arrive(WORKER_ID, ORDER_ID, arrival(false, IDEMPOTENCY_KEY, PHOTO_A));
|
|
|
+ Result<Boolean> conflict = service.arrive(WORKER_ID, ORDER_ID, arrival(false, IDEMPOTENCY_KEY, PHOTO_B));
|
|
|
+
|
|
|
+ assertEquals(CODE_OK, first.getCode());
|
|
|
+ assertEquals(CODE_CONFLICT_PAYLOAD, conflict.getCode(),
|
|
|
+ "同一幂等键携带不同照片/定位载荷必须返回 C4092(" + CODE_CONFLICT_PAYLOAD
|
|
|
+ + "),不得静默覆盖原留痕;当前第二次调用 code=" + conflict.getCode());
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 边界 2:乐观锁冲突 C422 ====================
|
|
|
+
|
|
|
+ /** 并发到岗:updateById 影响 0 行须报 C422,不得忽略行数按成功返回。 */
|
|
|
+ @Test
|
|
|
+ void optimisticLockZeroRowUpdateMustReturn422() {
|
|
|
+ when(orderMapper.selectOne(any())).thenReturn(order("accepted"));
|
|
|
+ when(orderMapper.updateById(any(TrainServiceOrder.class))).thenReturn(0);
|
|
|
+
|
|
|
+ Result<Boolean> r = service.arrive(WORKER_ID, ORDER_ID, arrival(false, null, PHOTO_A));
|
|
|
+
|
|
|
+ assertEquals(CODE_VERSION_CONFLICT, r.getCode(),
|
|
|
+ "到岗更新影响 0 行(乐观锁冲突/并发丢失更新)必须返回 C422(" + CODE_VERSION_CONFLICT
|
|
|
+ + "),当前忽略 updateById 返回行数,code=" + r.getCode());
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 实体须声明 version 字段,才能支撑到岗并发冲突的可感知与重试。 */
|
|
|
+ @Test
|
|
|
+ void orderEntityMustDeclareVersionForArriveConcurrency() {
|
|
|
+ Field version = findField(TrainServiceOrder.class, "version");
|
|
|
+ assertNotNull(version,
|
|
|
+ "train_service_order 必须声明 version 字段以支撑并发到岗打卡 C422("
|
|
|
+ + CODE_VERSION_CONFLICT + "),当前实体无 version");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 边界 3:不存在 C404 ====================
|
|
|
+
|
|
|
+ /** 工单不存在(查询为空)→ C404,不得折叠为笼统 500。 */
|
|
|
+ @Test
|
|
|
+ void nonExistentOrderMustReturn404() {
|
|
|
+ when(orderMapper.selectOne(any())).thenReturn(null);
|
|
|
+
|
|
|
+ Result<Boolean> r = service.arrive(WORKER_ID, ORDER_ID, arrival(false, null, PHOTO_A));
|
|
|
+
|
|
|
+ assertEquals(CODE_NOT_FOUND, r.getCode(),
|
|
|
+ "工单不存在必须返回 C404(" + CODE_NOT_FOUND + "),不得返回笼统 500;当前 code=" + r.getCode());
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 边界 4:越权 C403 ====================
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 非本单服务人员到岗打卡 → C403。当前 mustOwn 仅把 workerId 下推到 SQL WHERE,服务层无显式判定;
|
|
|
+ * 以不感知该过滤的 mapper 桩模拟「调用方身份与被查询工单归属不一致」,契约要求显式折叠为 C403。
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ void outsiderArriveMustReturn403() {
|
|
|
+ when(orderMapper.selectOne(any())).thenReturn(order("accepted"));
|
|
|
+ when(orderMapper.updateById(any(TrainServiceOrder.class))).thenReturn(1);
|
|
|
+
|
|
|
+ Result<Boolean> r = service.arrive(OUTSIDER_ID, ORDER_ID, arrival(false, null, PHOTO_A));
|
|
|
+
|
|
|
+ assertEquals(CODE_FORBIDDEN, r.getCode(),
|
|
|
+ "非工单服务人员越权到岗打卡必须返回 C403(" + CODE_FORBIDDEN + "),"
|
|
|
+ + "当前无显式权限判定,code=" + r.getCode());
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== helpers ====================
|
|
|
+
|
|
|
+ private static Field findField(Class<?> type, String name) {
|
|
|
+ Class<?> c = type;
|
|
|
+ while (c != null) {
|
|
|
+ try {
|
|
|
+ return c.getDeclaredField(name);
|
|
|
+ } catch (NoSuchFieldException ignored) {
|
|
|
+ c = c.getSuperclass();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+}
|