|
@@ -0,0 +1,222 @@
|
|
|
|
|
+package com.train.service.impl;
|
|
|
|
|
+
|
|
|
|
|
+import com.train.common.Result;
|
|
|
|
|
+import com.train.entity.TrainServiceOrder;
|
|
|
|
|
+import com.train.mapper.TrainOrgMemberMapper;
|
|
|
|
|
+import com.train.mapper.TrainOrderDispatchMapper;
|
|
|
|
|
+import com.train.mapper.TrainPractitionerProfileMapper;
|
|
|
|
|
+import com.train.mapper.TrainServiceOrderMapper;
|
|
|
|
|
+import com.train.mapper.TrainUserMapper;
|
|
|
|
|
+import com.train.service.DispatchService;
|
|
|
|
|
+import com.train.service.PromotionService;
|
|
|
|
|
+import com.train.service.RoleGuard;
|
|
|
|
|
+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.lang.reflect.Method;
|
|
|
|
|
+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.junit.jupiter.api.Assertions.assertTrue;
|
|
|
|
|
+import static org.mockito.ArgumentMatchers.any;
|
|
|
|
|
+import static org.mockito.ArgumentMatchers.anyBoolean;
|
|
|
|
|
+import static org.mockito.ArgumentMatchers.anyLong;
|
|
|
|
|
+import static org.mockito.Mockito.mock;
|
|
|
|
|
+import static org.mockito.Mockito.times;
|
|
|
|
|
+import static org.mockito.Mockito.verify;
|
|
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * {@code train.order.splitAmount} 契约级单测(TDD-RED,S-train-21;TC-1021 / TC-3004)。
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>需求锚点:docs/tdd/SRS.md「场景定义 §2 域三 O1-S4(Must)」——整分存储,
|
|
|
|
|
+ * {@code amount = platform_fee + staff_income} 恒等;禁浮点;分成比例配置权归属未拍板(本轮不推断口径)。
|
|
|
|
|
+ * TC-3004 要求全库金额 Integer 整分无浮点、恒等校验通过。</p>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>本轮只写测试、不改任何 src/main 实现。纯 Mockito(不启 Spring、不连 DB),被测面为
|
|
|
|
|
+ * {@link DispatchServiceImpl#create(Long, Boolean, Map)}(金额入参落库与派生 staffIncome)及金额明细查询
|
|
|
|
|
+ * {@link ServiceOrderService#detail(Long, Long)}。覆盖幂等重放 replay=true、同键异载荷 C4092、
|
|
|
|
|
+ * 乐观锁冲突 C422、不存在 C404、越权 C403 五类边界。当前实现:错误一律 {@code Result.error}(500)、
|
|
|
|
|
+ * create 无 replay/key/payload 入参(重复建单)、忽略幂等键异载荷、工单无 version 字段、明细不存在返回 500、
|
|
|
|
|
+ * 非机构管理者建单返回 500,故本类预期为 RED。</p>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>契约错误码({@code Result.code}):C4092=4092 同键异载荷;C422=422 乐观锁冲突;
|
|
|
|
|
+ * C404=404 工单不存在;C403=403 越权。金额一律单位「分」整数。</p>
|
|
|
|
|
+ */
|
|
|
|
|
+class TrainOrderSplitamountContractTest {
|
|
|
|
|
+
|
|
|
|
|
+ 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 ADMIN_ID = 1L;
|
|
|
|
|
+ private static final Long OUTSIDER_ID = 999L;
|
|
|
|
|
+ private static final Long ORDER_ID = 5001L;
|
|
|
|
|
+ private static final Long WORKER_ID = 3001L;
|
|
|
|
|
+ private static final Long ORG_ID = 55L;
|
|
|
|
|
+
|
|
|
|
|
+ private static final int AMOUNT = 100000; // 1000.00 元(分)
|
|
|
|
|
+ private static final int PLATFORM_FEE = 20000; // 200.00 元(分)
|
|
|
|
|
+ private static final int STAFF_INCOME = 80000; // 800.00 元(分)
|
|
|
|
|
+
|
|
|
|
|
+ private DispatchServiceImpl service;
|
|
|
|
|
+ private ServiceOrderService detailService;
|
|
|
|
|
+ private TrainServiceOrderMapper orderMapper;
|
|
|
|
|
+ private TrainOrderDispatchMapper dispatchMapper;
|
|
|
|
|
+ private RoleGuard roleGuard;
|
|
|
|
|
+
|
|
|
|
|
+ @BeforeEach
|
|
|
|
|
+ void setUp() {
|
|
|
|
|
+ service = new DispatchServiceImpl();
|
|
|
|
|
+ detailService = new ServiceOrderService();
|
|
|
|
|
+ orderMapper = mock(TrainServiceOrderMapper.class);
|
|
|
|
|
+ dispatchMapper = mock(TrainOrderDispatchMapper.class);
|
|
|
|
|
+ roleGuard = mock(RoleGuard.class);
|
|
|
|
|
+
|
|
|
|
|
+ ReflectionTestUtils.setField(service, "trainServiceOrderMapper", orderMapper);
|
|
|
|
|
+ ReflectionTestUtils.setField(service, "trainOrderDispatchMapper", dispatchMapper);
|
|
|
|
|
+ ReflectionTestUtils.setField(service, "trainOrgMemberMapper", mock(TrainOrgMemberMapper.class));
|
|
|
|
|
+ ReflectionTestUtils.setField(service, "trainPractitionerProfileMapper", mock(TrainPractitionerProfileMapper.class));
|
|
|
|
|
+ ReflectionTestUtils.setField(service, "trainUserMapper", mock(TrainUserMapper.class));
|
|
|
|
|
+ ReflectionTestUtils.setField(service, "roleGuard", roleGuard);
|
|
|
|
|
+ ReflectionTestUtils.setField(service, "promotionService", mock(PromotionService.class));
|
|
|
|
|
+
|
|
|
|
|
+ ReflectionTestUtils.setField(detailService, "orderMapper", orderMapper);
|
|
|
|
|
+ ReflectionTestUtils.setField(detailService, "dispatchMapper", dispatchMapper);
|
|
|
|
|
+ ReflectionTestUtils.setField(detailService, "userMapper", mock(TrainUserMapper.class));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static Map<String, Object> body(int amount, int fee, boolean replay, String key) {
|
|
|
|
|
+ Map<String, Object> b = new HashMap<>();
|
|
|
|
|
+ b.put("orgId", ORG_ID);
|
|
|
|
|
+ b.put("familyName", "王阿姨家");
|
|
|
|
|
+ b.put("serviceType", "cleaning");
|
|
|
|
|
+ b.put("amount", amount);
|
|
|
|
|
+ b.put("platformFee", fee);
|
|
|
|
|
+ b.put("replay", replay);
|
|
|
|
|
+ b.put("key", key);
|
|
|
|
|
+ return b;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 对照基线:整分恒等 ====================
|
|
|
|
|
+
|
|
|
|
|
+ /** TC-3004:合法金额按整数「分」落库,staffIncome = amount − platformFee,无浮点。 */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void createStoresIntegerFenAndKeepsIdentity() {
|
|
|
|
|
+ Result<Map<String, Object>> r = service.create(ADMIN_ID, Boolean.TRUE,
|
|
|
|
|
+ body(AMOUNT, PLATFORM_FEE, false, null));
|
|
|
|
|
+
|
|
|
|
|
+ assertEquals(CODE_OK, r.getCode());
|
|
|
|
|
+ ArgumentCaptor<TrainServiceOrder> cap = ArgumentCaptor.forClass(TrainServiceOrder.class);
|
|
|
|
|
+ verify(orderMapper, times(1)).insert(cap.capture());
|
|
|
|
|
+ TrainServiceOrder o = cap.getValue();
|
|
|
|
|
+ assertEquals(AMOUNT, o.getAmount().intValue(), "amount 须整分存储");
|
|
|
|
|
+ assertEquals(PLATFORM_FEE, o.getPlatformFee().intValue(), "platformFee 须整分存储");
|
|
|
|
|
+ assertEquals(STAFF_INCOME, o.getStaffIncome().intValue(),
|
|
|
|
|
+ "TC-3004:staffIncome 必须等于 amount − platformFee(分)");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 边界 0:幂等重放 replay=true ====================
|
|
|
|
|
+
|
|
|
|
|
+ /** 同幂等键 replay=true 重放建单:须复用原单,不得重复 insert。 */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void replayTrueSameKeySkipsSecondOrderInsert() {
|
|
|
|
|
+ service.create(ADMIN_ID, Boolean.TRUE, body(AMOUNT, PLATFORM_FEE, true, "SPLIT-KEY-1"));
|
|
|
|
|
+ service.create(ADMIN_ID, Boolean.TRUE, body(AMOUNT, PLATFORM_FEE, true, "SPLIT-KEY-1"));
|
|
|
|
|
+
|
|
|
|
|
+ verify(orderMapper, times(1)).insert(any(TrainServiceOrder.class));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 契约入口须支持显式 replay/key/payload(Result 返回且至少 4 入参)。 */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void replayEntryMustAcceptIdempotencyKeyAndPayload() {
|
|
|
|
|
+ boolean hasReplayEntry = false;
|
|
|
|
|
+ for (Method m : DispatchService.class.getMethods()) {
|
|
|
|
|
+ String name = m.getName().toLowerCase();
|
|
|
|
|
+ if (name.contains("create") && Result.class.isAssignableFrom(m.getReturnType())
|
|
|
|
|
+ && m.getParameterCount() >= 4) {
|
|
|
|
|
+ hasReplayEntry = true;
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ assertTrue(hasReplayEntry,
|
|
|
|
|
+ "train.order.splitAmount 契约要求 DispatchService 暴露带 replay/key/payload 的建单入口"
|
|
|
|
|
+ + "(Result 返回且至少 4 入参)以支持 replay=true;"
|
|
|
|
|
+ + "当前 create(Long,Boolean,Map) 仅 3 入参,无法表达幂等重放");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 边界 1:同键异载荷 C4092 ====================
|
|
|
|
|
+
|
|
|
|
|
+ /** 同一幂等键却改金额载荷(100000/20000 → 90000/10000):必须 C4092,禁止静默建第二张单。 */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void sameKeyDifferentPayloadReturnsC4092() {
|
|
|
|
|
+ Result<Map<String, Object>> r1 = service.create(ADMIN_ID, Boolean.TRUE,
|
|
|
|
|
+ body(AMOUNT, PLATFORM_FEE, true, "SPLIT-KEY-1"));
|
|
|
|
|
+ Result<Map<String, Object>> r2 = service.create(ADMIN_ID, Boolean.TRUE,
|
|
|
|
|
+ body(90000, 10000, true, "SPLIT-KEY-1"));
|
|
|
|
|
+
|
|
|
|
|
+ assertEquals(CODE_OK, r1.getCode());
|
|
|
|
|
+ assertEquals(CODE_SAME_KEY_DIFF_PAYLOAD, r2.getCode(),
|
|
|
|
|
+ "同一幂等键携带不同 amount/platformFee 载荷必须返回 C4092(4092) 冲突,禁止静默重复建单");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 边界 2:乐观锁冲突 C422 ====================
|
|
|
|
|
+
|
|
|
|
|
+ /** 工单实体须有 version 字段支撑并发金额写入的乐观锁 C422。 */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void orderEntityMustHaveVersionField() {
|
|
|
|
|
+ Field versionField = findField(TrainServiceOrder.class, "version");
|
|
|
|
|
+ assertNotNull(versionField,
|
|
|
|
|
+ "train_service_order 实体必须声明 version 字段以支撑并发金额写入冲突 C422("
|
|
|
|
|
+ + CODE_VERSION_CONFLICT + ");当前实体无 version");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 边界 3:不存在 C404 ====================
|
|
|
|
|
+
|
|
|
|
|
+ /** 查询不存在工单的金额明细 → C404,而非笼统的 500。 */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void nonExistentOrderAmountDetailReturns404() {
|
|
|
|
|
+ when(orderMapper.selectOne(any())).thenReturn(null);
|
|
|
|
|
+
|
|
|
|
|
+ Result<Map<String, Object>> r = detailService.detail(WORKER_ID, ORDER_ID);
|
|
|
|
|
+
|
|
|
|
|
+ assertEquals(CODE_NOT_FOUND, r.getCode(),
|
|
|
|
|
+ "查询不存在工单的金额明细(amount/platformFee/staffIncome)必须返回 C404(404),不得返回 500");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 边界 4:越权 C403 ====================
|
|
|
|
|
+
|
|
|
|
|
+ /** 非管理员、非该机构 active 管理者建单(金额写入入口)→ C403,而非笼统的 500。 */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void nonOrgManagerCreateReturns403() {
|
|
|
|
|
+ when(roleGuard.requireOrgManagerAt(anyLong(), anyLong())).thenReturn("无该机构派单权限");
|
|
|
|
|
+
|
|
|
|
|
+ Result<Map<String, Object>> r = service.create(OUTSIDER_ID, Boolean.FALSE,
|
|
|
|
|
+ body(AMOUNT, PLATFORM_FEE, false, null));
|
|
|
|
|
+
|
|
|
|
|
+ assertEquals(CODE_FORBIDDEN, r.getCode(),
|
|
|
|
|
+ "非管理员/非该机构管理者建单(金额拆分入口)必须返回 C403(403),不得降级为笼统的 500");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 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;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|