|
@@ -0,0 +1,234 @@
|
|
|
|
|
+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.PractitionerService;
|
|
|
|
|
+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.lang.reflect.Field;
|
|
|
|
|
+import java.lang.reflect.Method;
|
|
|
|
|
+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.assertSame;
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
|
|
+import static org.mockito.ArgumentMatchers.any;
|
|
|
|
|
+import static org.mockito.Mockito.mock;
|
|
|
|
|
+import static org.mockito.Mockito.never;
|
|
|
|
|
+import static org.mockito.Mockito.verify;
|
|
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * {@code train.profile.dual} 契约级单测(TDD-RED,S-train-14;TC-1014)。
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>需求锚点:docs/tdd/SRS.md「场景定义 §2 域二 C1-S3(Must·待确认)」——Given 一个 train_user 账号;
|
|
|
|
|
+ * When 切换角色视图;Then 训练营学员/讲师档案与健康服务人员档案独立读写、互不覆盖、按角色分流。
|
|
|
|
|
+ * ⚠️ 需求纪要 §7.2-1「身份关系默认」尚未拍板,故本测试只固化「双档案隔离 + 幂等/冲突错误语义」
|
|
|
|
|
+ * 的最小可验证契约,不臆造未拍板形态。</p>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>本轮只写测试、不改任何 src/main 实现。纯 Mockito(不启 Spring、不连 DB)。被测面为
|
|
|
|
|
+ * {@link PractitionerServiceImpl} 的档案读写入口(mine/detail/ensureProfile/approveProfile)
|
|
|
|
|
+ * 与 {@link PractitionerService} 契约面,外加 {@link TrainPractitionerProfile} 实体的乐观锁字段。</p>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>当前实现缺陷(RED 依据):① PractitionerService 无 dual/角色分流写入口,无法表达 replay 与
|
|
|
|
|
+ * 同键异载荷;② TrainPractitionerProfile 无 version 字段,无法支撑 C422;③ approveProfile 忽略
|
|
|
|
|
+ * updateById 返回值,影响 0 行仍返回 200;④ detail 对「档案不存在」与「越权」统一走 Result.error
|
|
|
|
|
+ * 的 500,无法区分 C404 / C403。</p>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>契约错误码(Result.code):C4092=4092 同幂等键异载荷;C422=422 乐观锁冲突;C404=404 不存在;
|
|
|
|
|
+ * C403=403 越权。</p>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>说明:本仓后端模块为 train-backend(无字面量 train-impl 模块),测试按既有契约单测惯例落在
|
|
|
|
|
+ * train-backend/src/test/java/com/train/service/impl/,与 S-train-5/7/8/9 的契约测试同目录。</p>
|
|
|
|
|
+ */
|
|
|
|
|
+class TrainProfileDualContractTest {
|
|
|
|
|
+
|
|
|
|
|
+ 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 USER = 990014L;
|
|
|
|
|
+ private static final Long OTHER_USER = 990015L;
|
|
|
|
|
+ private static final Long ADMIN = 1L;
|
|
|
|
|
+
|
|
|
|
|
+ private PractitionerServiceImpl service;
|
|
|
|
|
+ private TrainPractitionerProfileMapper profileMapper;
|
|
|
|
|
+ private TrainUserMapper userMapper;
|
|
|
|
|
+ private TrainOrgMemberMapper orgMemberMapper;
|
|
|
|
|
+ private RoleGuard roleGuard;
|
|
|
|
|
+
|
|
|
|
|
+ @BeforeEach
|
|
|
|
|
+ void setUp() {
|
|
|
|
|
+ service = new PractitionerServiceImpl();
|
|
|
|
|
+ profileMapper = mock(TrainPractitionerProfileMapper.class);
|
|
|
|
|
+ userMapper = mock(TrainUserMapper.class);
|
|
|
|
|
+ orgMemberMapper = mock(TrainOrgMemberMapper.class);
|
|
|
|
|
+ roleGuard = mock(RoleGuard.class);
|
|
|
|
|
+
|
|
|
|
|
+ ReflectionTestUtils.setField(service, "trainPractitionerProfileMapper", profileMapper);
|
|
|
|
|
+ ReflectionTestUtils.setField(service, "trainUserMapper", userMapper);
|
|
|
|
|
+ ReflectionTestUtils.setField(service, "trainOrgMemberMapper", orgMemberMapper);
|
|
|
|
|
+ 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", roleGuard);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static TrainPractitionerProfile profile(String status) {
|
|
|
|
|
+ TrainPractitionerProfile p = new TrainPractitionerProfile();
|
|
|
|
|
+ p.setId(70014L);
|
|
|
|
|
+ p.setUserId(USER);
|
|
|
|
|
+ p.setDisplayName("张*三");
|
|
|
|
|
+ p.setLifecycleStage("training");
|
|
|
|
|
+ p.setAuthLevel("none");
|
|
|
|
|
+ p.setStatus(status);
|
|
|
|
|
+ p.setDeleted(0);
|
|
|
|
|
+ return p;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ 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;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 双档案写入口:名称含 dual、返回 Result、至少 3 入参(replay/key/payload)。 */
|
|
|
|
|
+ private static Method findDualWriteMethod() {
|
|
|
|
|
+ for (Method m : PractitionerService.class.getMethods()) {
|
|
|
|
|
+ String n = m.getName().toLowerCase();
|
|
|
|
|
+ if (n.contains("dual") && Result.class.isAssignableFrom(m.getReturnType())
|
|
|
|
|
+ && m.getParameterCount() >= 3) {
|
|
|
|
|
+ return m;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 边界 0:幂等重放 replay=true ====================
|
|
|
|
|
+
|
|
|
|
|
+ /** 基线(当前已覆盖):同一 user 重复 ensureProfile 不重复建档。 */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void replayDoesNotRecreateProfile() {
|
|
|
|
|
+ TrainPractitionerProfile existing = profile("pending");
|
|
|
|
|
+ when(profileMapper.selectOne(any())).thenReturn(existing);
|
|
|
|
|
+
|
|
|
|
|
+ TrainPractitionerProfile p1 = service.ensureProfile(USER);
|
|
|
|
|
+ TrainPractitionerProfile p2 = service.ensureProfile(USER);
|
|
|
|
|
+
|
|
|
|
|
+ assertSame(existing, p1);
|
|
|
|
|
+ assertSame(existing, p2);
|
|
|
|
|
+ verify(profileMapper, never()).insert(any(TrainPractitionerProfile.class));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 契约:replay=true 必须经角色分流的双档案幂等写入口;当前无此入口。 */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void replayTrueRequiresDualWriteEntry() {
|
|
|
|
|
+ Method dualWrite = findDualWriteMethod();
|
|
|
|
|
+ assertNotNull(dualWrite,
|
|
|
|
|
+ "train.profile.dual 契约要求存在 replay=true 的双档案幂等写入口(返回 Result,"
|
|
|
|
|
+ + "含 replay/key/payload 至少 3 入参):重放不得覆盖训练营学员/讲师档案,"
|
|
|
|
|
+ + "当前 PractitionerService 无 dual 入口");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 核心:按角色分流的双档案独立读写 ====================
|
|
|
|
|
+
|
|
|
|
|
+ /** TC-1014:同一 train_user 按角色(student/teacher vs worker)分流到两份独立档案,互不覆盖。 */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void roleBasedRoutingToTwoIndependentProfiles() {
|
|
|
|
|
+ boolean hasRoleRouting = false;
|
|
|
|
|
+ for (Method m : PractitionerService.class.getMethods()) {
|
|
|
|
|
+ String n = m.getName().toLowerCase();
|
|
|
|
|
+ if ((n.contains("dual") || n.contains("role") || n.contains("view"))
|
|
|
|
|
+ && m.getParameterCount() >= 2) {
|
|
|
|
|
+ hasRoleRouting = true;
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ assertTrue(hasRoleRouting,
|
|
|
|
|
+ "TC-1014:同一 train_user 须按角色分流(训练营学员/讲师档案 与 健康服务人员档案),"
|
|
|
|
|
+ + "两档案独立读写互不覆盖;当前 PractitionerService 无角色分流入口");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 边界 1:同键异载荷 C4092 ====================
|
|
|
|
|
+
|
|
|
|
|
+ /** 同一幂等键 (userId, role) 携带不同载荷:契约要求返回 C4092,禁止静默复用旧快照。 */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void sameKeyDifferentPayloadReturnsC4092() {
|
|
|
|
|
+ Method dualWrite = findDualWriteMethod();
|
|
|
|
|
+ assertNotNull(dualWrite,
|
|
|
|
|
+ "train.profile.dual 契约要求同幂等键 (userId, role) 携带不同载荷(role/profile 数据冲突)"
|
|
|
|
|
+ + "返回 C4092(" + CODE_SAME_KEY_DIFF_PAYLOAD + ");当前 PractitionerService 无 dual 写入口,"
|
|
|
|
|
+ + "无法表达同键异载荷冲突");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 边界 2:乐观锁冲突 C422 ====================
|
|
|
|
|
+
|
|
|
|
|
+ /** 健康服务人员档案实体须有 version 字段支撑乐观锁;陈旧 version 写入须 C422。 */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void staleVersionReturnsC422() {
|
|
|
|
|
+ Field version = findField(TrainPractitionerProfile.class, "version");
|
|
|
|
|
+ assertNotNull(version,
|
|
|
|
|
+ "train_practitioner_profile 实体必须声明 version 字段支撑并发读写冲突 C422("
|
|
|
|
|
+ + CODE_VERSION_CONFLICT + ");当前 TrainPractitionerProfile 无 version");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 档案更新影响 0 行(并发丢失更新)必须 C422,不得按成功返回。 */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void zeroRowUpdateReturnsC422() {
|
|
|
|
|
+ when(profileMapper.selectOne(any())).thenReturn(profile("pending"));
|
|
|
|
|
+ when(profileMapper.updateById(any(TrainPractitionerProfile.class))).thenReturn(0);
|
|
|
|
|
+
|
|
|
|
|
+ Result<Map<String, Object>> r = service.approveProfile(ADMIN, Boolean.TRUE, USER);
|
|
|
|
|
+
|
|
|
|
|
+ assertEquals(CODE_VERSION_CONFLICT, r.getCode(),
|
|
|
|
|
+ "档案更新影响 0 行(乐观锁冲突)必须返回 C422(422),不得静默按成功返回;"
|
|
|
|
|
+ + "当前 approveProfile 忽略 updateById 返回值并返回 200");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 边界 3:不存在 C404 ====================
|
|
|
|
|
+
|
|
|
|
|
+ /** 查询不存在的健康服务人员档案:必须 C404,而非 Result.error 的 500。 */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void nonExistentProfileReturnsC404() {
|
|
|
|
|
+ when(profileMapper.selectOne(any())).thenReturn(null);
|
|
|
|
|
+
|
|
|
|
|
+ Result<Map<String, Object>> r = service.detail(USER, Boolean.FALSE, USER);
|
|
|
|
|
+
|
|
|
|
|
+ assertEquals(CODE_NOT_FOUND, r.getCode(),
|
|
|
|
|
+ "查询不存在的健康服务人员档案必须返回 C404(404);当前 detail() 走 Result.error 返回 500");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 边界 4:越权 C403 ====================
|
|
|
|
|
+
|
|
|
|
|
+ /** 非本人/非 admin/非机构成员查看他人档案:必须 C403,而非笼统的 500。 */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void crossUserProfileDetailReturnsC403() {
|
|
|
|
|
+ when(orgMemberMapper.selectCount(any())).thenReturn(0L);
|
|
|
|
|
+
|
|
|
|
|
+ Result<Map<String, Object>> r = service.detail(USER, Boolean.FALSE, OTHER_USER);
|
|
|
|
|
+
|
|
|
|
|
+ assertEquals(CODE_FORBIDDEN, r.getCode(),
|
|
|
|
|
+ "非本人/非 admin/非机构成员查看他人双档案必须返回 C403(403);"
|
|
|
|
|
+ + "当前 detail() 走 Result.error 返回 500");
|
|
|
|
|
+ }
|
|
|
|
|
+}
|