|
|
@@ -0,0 +1,121 @@
|
|
|
+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.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>与同目录 {@link TrainCertApproveContractTest} 形成互补:后者覆盖「审批人变化」形态的
|
|
|
+ * C4092、version 字段存在性、isAdmin=false 越权等;本类补充三种尚未覆盖的边界:</p>
|
|
|
+ * <ol>
|
|
|
+ * <li>同幂等键但<b>审批决策翻转</b>(approve → reject)必须 C4092;</li>
|
|
|
+ * <li>审批人 operatorId 为空(无法归因 approved_by)时即便 isAdmin=true 也必须 C403;</li>
|
|
|
+ * <li>replay=true 同键同载荷重放须在 Result.data 中显式标识 replay,且不得二次写库。</li>
|
|
|
+ * </ol>
|
|
|
+ *
|
|
|
+ * <p>只写测试、不改 src/main。当前实现把上述错误一律走 {@code Result.error}(code=500)、
|
|
|
+ * 无 replay 入参/标记,故本类为 RED。</p>
|
|
|
+ */
|
|
|
+class TrainCertApproveBoundaryContractTest {
|
|
|
+
|
|
|
+ private static final int CODE_OK = 200;
|
|
|
+ private static final int CODE_SAME_KEY_DIFF_PAYLOAD = 4092;
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** C4092:同键审批决策翻转(TRUE→FALSE)必须报冲突,禁止静默按 200 复用既有审批。 */
|
|
|
+ @Test
|
|
|
+ void flippedDecisionSameKeyReturnsC4092() {
|
|
|
+ when(profileMapper.selectOne(any())).thenReturn(pendingProfile());
|
|
|
+
|
|
|
+ Result<Map<String, Object>> first = service.approveProfile(ADMIN_A, Boolean.TRUE, TARGET_USER_ID);
|
|
|
+ Result<Map<String, Object>> second = service.approveProfile(ADMIN_A, Boolean.FALSE, TARGET_USER_ID);
|
|
|
+
|
|
|
+ assertEquals(CODE_OK, first.getCode());
|
|
|
+ assertEquals(CODE_SAME_KEY_DIFF_PAYLOAD, second.getCode(),
|
|
|
+ "同幂等键 targetUserId 的审批决策由通过翻转为驳回,必须返回 C4092(4092) 冲突");
|
|
|
+ }
|
|
|
+
|
|
|
+ /** C403:operatorId 为空时无法归因 approved_by,即便 isAdmin=true 也必须拒绝。 */
|
|
|
+ @Test
|
|
|
+ void nullOperatorIdApproveReturns403() {
|
|
|
+ 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)");
|
|
|
+ }
|
|
|
+
|
|
|
+ /** replay=true:同键同载荷重放须在 data 中标识 replay,且不得二次写库。 */
|
|
|
+ @Test
|
|
|
+ void replayResultMustFlagReplayAndSkipSecondUpdate() {
|
|
|
+ when(profileMapper.selectOne(any())).thenReturn(pendingProfile());
|
|
|
+
|
|
|
+ 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, replay.getCode());
|
|
|
+ assertEquals(Boolean.TRUE, replay.getData().get("replay"),
|
|
|
+ "同键同载荷重放须显式标识 replay=true,供调用方区分首次审批与幂等回放");
|
|
|
+ verify(profileMapper, times(1)).updateById(any(TrainPractitionerProfile.class));
|
|
|
+ }
|
|
|
+}
|