|
|
@@ -0,0 +1,186 @@
|
|
|
+package com.train.service.impl;
|
|
|
+
|
|
|
+import com.train.common.Result;
|
|
|
+import com.train.entity.TrainPractitionerProfile;
|
|
|
+import com.train.mapper.TrainCreditAccountMapper;
|
|
|
+import com.train.mapper.TrainCreditLedgerMapper;
|
|
|
+import com.train.mapper.TrainOrgMemberMapper;
|
|
|
+import com.train.mapper.TrainPractitionerLicenseMapper;
|
|
|
+import com.train.mapper.TrainPractitionerProfileMapper;
|
|
|
+import com.train.mapper.TrainUserMapper;
|
|
|
+import com.train.service.RoleGuard;
|
|
|
+import org.junit.jupiter.api.BeforeEach;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+import org.springframework.test.util.ReflectionTestUtils;
|
|
|
+
|
|
|
+import java.util.Date;
|
|
|
+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.times;
|
|
|
+import static org.mockito.Mockito.verify;
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
+
|
|
|
+/**
|
|
|
+ * {@code train.cert.approve} 契约级边界单测(TDD-RED,S-train-13;TC-1013,第二轮补强)。
|
|
|
+ *
|
|
|
+ * <p>需求锚点:docs/tdd/SRS.md「场景定义 §2 域二 C1-S2(Must)」——机构审批通过后
|
|
|
+ * {@code practitioner_profile.status} 生效且 {@code approved_by} 记录;未审批不可被派单。</p>
|
|
|
+ *
|
|
|
+ * <p>与同目录 {@link TrainCertApproveContractTest} / {@link TrainCertApproveBoundaryContractTest}
|
|
|
+ * 形成互补:本类聚焦「同一审批契约在重放与并发下的可观测性」,覆盖 replay=true 标识、同键异载荷
|
|
|
+ * C4092、乐观锁冲突 C422、不存在 C404、越权 C403 五类边界,并补 null isAdmin / null operatorId
|
|
|
+ * 两个越权形态。当前 {@code PractitionerServiceImpl#approveProfile} 把全部错误统一走
|
|
|
+ * {@code Result.error}(code=500)、Result.data 不回带 approvedBy、无 replay 标识、忽略 updateById
|
|
|
+ * 影响行数,故本类预期为 RED(至少 1 条断言失败)。</p>
|
|
|
+ *
|
|
|
+ * <p>只写测试、不改 src/main 一行。被测面为纯 Mockito(不启 Spring、不连 DB)。</p>
|
|
|
+ */
|
|
|
+class TrainCertApproveEdgeContractTest {
|
|
|
+
|
|
|
+ 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_A = 1L;
|
|
|
+ private static final Long ADMIN_B = 2L;
|
|
|
+ private static final Long TARGET_USER_ID = 3001L;
|
|
|
+
|
|
|
+ private PractitionerServiceImpl service;
|
|
|
+ private TrainPractitionerProfileMapper profileMapper;
|
|
|
+
|
|
|
+ @BeforeEach
|
|
|
+ void setUp() {
|
|
|
+ service = new PractitionerServiceImpl();
|
|
|
+ profileMapper = mock(TrainPractitionerProfileMapper.class);
|
|
|
+
|
|
|
+ ReflectionTestUtils.setField(service, "trainPractitionerProfileMapper", profileMapper);
|
|
|
+ ReflectionTestUtils.setField(service, "trainUserMapper", mock(TrainUserMapper.class));
|
|
|
+ ReflectionTestUtils.setField(service, "trainOrgMemberMapper", mock(TrainOrgMemberMapper.class));
|
|
|
+ ReflectionTestUtils.setField(service, "trainPractitionerLicenseMapper", mock(TrainPractitionerLicenseMapper.class));
|
|
|
+ ReflectionTestUtils.setField(service, "trainCreditAccountMapper", mock(TrainCreditAccountMapper.class));
|
|
|
+ ReflectionTestUtils.setField(service, "trainCreditLedgerMapper", mock(TrainCreditLedgerMapper.class));
|
|
|
+ ReflectionTestUtils.setField(service, "roleGuard", mock(RoleGuard.class));
|
|
|
+ }
|
|
|
+
|
|
|
+ private static TrainPractitionerProfile pendingProfile() {
|
|
|
+ TrainPractitionerProfile p = new TrainPractitionerProfile();
|
|
|
+ p.setId(7001L);
|
|
|
+ p.setUserId(TARGET_USER_ID);
|
|
|
+ p.setDisplayName("张*三");
|
|
|
+ p.setLifecycleStage("internship");
|
|
|
+ p.setAuthLevel("T2");
|
|
|
+ p.setSpecialties("[\"保洁\",\"照护\"]");
|
|
|
+ p.setPracticeYears(3);
|
|
|
+ p.setServiceStatus("off");
|
|
|
+ p.setStatus("pending");
|
|
|
+ p.setDeleted(0);
|
|
|
+ p.setCreatedAt(new Date(System.currentTimeMillis() - 86_400_000L));
|
|
|
+ p.setUpdatedAt(new Date(System.currentTimeMillis() - 86_400_000L));
|
|
|
+ return p;
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 越权 C403 ====================
|
|
|
+
|
|
|
+ /** isAdmin=null(未显式授权)不得按管理员处理,必须返回 C403 而非笼统 500。 */
|
|
|
+ @Test
|
|
|
+ void nullIsAdminReturns403() {
|
|
|
+ Result<Map<String, Object>> r = service.approveProfile(ADMIN_A, null, TARGET_USER_ID);
|
|
|
+
|
|
|
+ assertEquals(CODE_FORBIDDEN, r.getCode(),
|
|
|
+ "isAdmin=null 时不得视为管理员审批,必须返回 C403(403);当前 code=" + r.getCode());
|
|
|
+ }
|
|
|
+
|
|
|
+ /** operatorId=null 无法归因 approved_by,即便 isAdmin=true 也必须返回 C403。 */
|
|
|
+ @Test
|
|
|
+ void nullOperatorIdReturns403() {
|
|
|
+ when(profileMapper.selectOne(any())).thenReturn(pendingProfile());
|
|
|
+
|
|
|
+ Result<Map<String, Object>> r = service.approveProfile(null, Boolean.TRUE, TARGET_USER_ID);
|
|
|
+
|
|
|
+ assertEquals(CODE_FORBIDDEN, r.getCode(),
|
|
|
+ "operatorId 为空不得把 approved_by 写成 null 并按 200 成功,必须返回 C403(403);"
|
|
|
+ + "当前 code=" + r.getCode());
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 不存在 C404 ====================
|
|
|
+
|
|
|
+ /** 目标档案不存在必须返回 C404,不得降级为笼统 500。 */
|
|
|
+ @Test
|
|
|
+ void missingProfileReturns404() {
|
|
|
+ when(profileMapper.selectOne(any())).thenReturn(null);
|
|
|
+
|
|
|
+ Result<Map<String, Object>> r = service.approveProfile(ADMIN_A, Boolean.TRUE, TARGET_USER_ID);
|
|
|
+
|
|
|
+ assertEquals(CODE_NOT_FOUND, r.getCode(),
|
|
|
+ "待审批档案不存在必须返回 C404(404);当前 code=" + r.getCode());
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 乐观锁冲突 C422 ====================
|
|
|
+
|
|
|
+ /** updateById 影响 0 行(并发丢失更新)必须返回 C422,不得忽略行数按 200 成功。 */
|
|
|
+ @Test
|
|
|
+ void zeroRowUpdateReturns422() {
|
|
|
+ when(profileMapper.selectOne(any())).thenReturn(pendingProfile());
|
|
|
+ when(profileMapper.updateById(any(TrainPractitionerProfile.class))).thenReturn(0);
|
|
|
+
|
|
|
+ Result<Map<String, Object>> r = service.approveProfile(ADMIN_A, Boolean.TRUE, TARGET_USER_ID);
|
|
|
+
|
|
|
+ assertEquals(CODE_VERSION_CONFLICT, r.getCode(),
|
|
|
+ "审批更新影响 0 行(乐观锁冲突/并发丢失更新)必须返回 C422(422);当前 code=" + r.getCode());
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 同键异载荷 C4092 ====================
|
|
|
+
|
|
|
+ /** 同幂等键换审批人(ADMIN_A→ADMIN_B)必须 C4092,禁止静默复用既有审批。 */
|
|
|
+ @Test
|
|
|
+ void differentApproverReplayReturns4092() {
|
|
|
+ when(profileMapper.selectOne(any())).thenReturn(pendingProfile());
|
|
|
+
|
|
|
+ Result<Map<String, Object>> first = service.approveProfile(ADMIN_A, Boolean.TRUE, TARGET_USER_ID);
|
|
|
+ Result<Map<String, Object>> replay = service.approveProfile(ADMIN_B, Boolean.TRUE, TARGET_USER_ID);
|
|
|
+
|
|
|
+ assertEquals(CODE_OK, first.getCode());
|
|
|
+ assertEquals(CODE_SAME_KEY_DIFF_PAYLOAD, replay.getCode(),
|
|
|
+ "同键换审批人载荷必须返回 C4092(4092) 冲突;当前 code=" + replay.getCode());
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 同幂等键审批决策翻转(TRUE→FALSE)必须 C4092。 */
|
|
|
+ @Test
|
|
|
+ void flippedDecisionReplayReturns4092() {
|
|
|
+ when(profileMapper.selectOne(any())).thenReturn(pendingProfile());
|
|
|
+
|
|
|
+ Result<Map<String, Object>> first = service.approveProfile(ADMIN_A, Boolean.TRUE, TARGET_USER_ID);
|
|
|
+ Result<Map<String, Object>> replay = service.approveProfile(ADMIN_A, Boolean.FALSE, TARGET_USER_ID);
|
|
|
+
|
|
|
+ assertEquals(CODE_OK, first.getCode());
|
|
|
+ assertEquals(CODE_SAME_KEY_DIFF_PAYLOAD, replay.getCode(),
|
|
|
+ "同键审批决策翻转必须返回 C4092(4092) 冲突;当前 code=" + replay.getCode());
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 幂等重放 replay=true ====================
|
|
|
+
|
|
|
+ /** 同键同载荷重放须显式标识 replay=true、回带首次审批人,且不得二次写库。 */
|
|
|
+ @Test
|
|
|
+ void replayMustFlagReplayAndExposeFirstApprover() {
|
|
|
+ when(profileMapper.selectOne(any())).thenReturn(pendingProfile());
|
|
|
+
|
|
|
+ Result<Map<String, Object>> first = service.approveProfile(ADMIN_A, Boolean.TRUE, TARGET_USER_ID);
|
|
|
+ Result<Map<String, Object>> replay = service.approveProfile(ADMIN_A, Boolean.TRUE, TARGET_USER_ID);
|
|
|
+
|
|
|
+ assertEquals(CODE_OK, first.getCode());
|
|
|
+ assertEquals(CODE_OK, replay.getCode());
|
|
|
+ assertNotNull(first.getData(), "首次审批返回 data 不得为空");
|
|
|
+ assertNotNull(replay.getData(), "重放返回 data 不得为空");
|
|
|
+ assertEquals(Boolean.TRUE, replay.getData().get("replay"),
|
|
|
+ "同键同载荷重放须在 data 中显式标识 replay=true,供调用方区分首次审批与幂等回放");
|
|
|
+ assertEquals(ADMIN_A, replay.getData().get("approvedBy"),
|
|
|
+ "重放结果须回带首次审批人 approved_by,不得丢失归因");
|
|
|
+ verify(profileMapper, times(1)).updateById(any(TrainPractitionerProfile.class));
|
|
|
+ }
|
|
|
+}
|