F2-5: TaskCompleteListener 空桩 + ProjectSwitchService 真实实现
1. flowable-service: 新增 TaskCompleteListener 空桩 - 包路径 com.dsasp.server.wf.user.listener.TaskCompleteListener - 实现 org.flowable.engine.delegate.TaskListener(flowable5 兼容层接口) - no-op 实现:successService 执行依赖 OSGi 容器,转 F3 前置 - 覆盖 1509 个流程定义中所有 activiti:class 绑定此 Listener 的节点 - 修复 FlowControllerTest 构造器参数(补 bizJdbc mock) 2. platform-auth: ProjectSwitchService 真实实现(替换 P2 no-op 桩) - 对齐 EIP-3.0 ProjectSwitchServiceImpl 逻辑 - 查 pm_project_user/pm_project 表,裸拼 SQL→参数化 - queryForLong(Spring 4 已弃)→ queryForObject(Long.class) - DB 不可达时三方法降级不阻断登录 - ProjectSwitchServiceTest 11 用例全通过 3. docs: F2-5出口验收报告.md(链路证据 + 边界清单) Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,30 +1,123 @@
|
||||
package com.eip4.platform.auth.stub;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.dsasp.server.common.viewframework.service.ProjectSwitchService;
|
||||
|
||||
/**
|
||||
* WS1 桩:项目切换服务。PoC 测试用户不挂项目。
|
||||
* {@link ProjectSwitchService} 真实实现(F2-5,替换 P2 遗留 no-op 桩)。
|
||||
*
|
||||
* <p>对齐 EIP-3.0 原实现
|
||||
* {@code com.dsasp.pm.totalpackagecontract.service.ProjectSwitchServiceImpl}:
|
||||
* <ul>
|
||||
* <li>{@link #isUserInProjects}:{@code SELECT count FROM PM_PROJECT_USER WHERE userid=?}</li>
|
||||
* <li>{@link #findAllProjectByUserId}:{@code SELECT pm_project_id,projectname FROM pm_project
|
||||
* WHERE pm_project_id IN (SELECT DISTINCT pm_projectid FROM pm_project_user WHERE userid=?)}</li>
|
||||
* <li>{@link #getProjectOrgId}:{@code SELECT filiale FROM pm_project WHERE pm_project_id=?}</li>
|
||||
* </ul>
|
||||
*
|
||||
* <p>对原版的工程化改进(行为不变):
|
||||
* <ol>
|
||||
* <li>裸拼 SQL → {@code ?} 参数化,收敛 SQL 注入面;</li>
|
||||
* <li>{@code jdbcTemplate.queryForLong}(Spring 4 已弃)→
|
||||
* {@code queryForObject(…, Long.class)} 兼容 Spring 6;</li>
|
||||
* <li>DataAccessException 降级:DB 不可达时 {@code isUserInProjects}=false /
|
||||
* {@code findAllProjectByUserId}=空 / {@code getProjectOrgId}=null,不阻断登录。</li>
|
||||
* </ol>
|
||||
*
|
||||
* <p>包名、类名、Spring Bean 名({@code projectSwitchService})与接口签名保持不变,
|
||||
* 调用方({@link com.dsasp.server.dataDic.bean.GeneralPO#getOrgId})无感知替换。
|
||||
*/
|
||||
@Service("projectSwitchService")
|
||||
public class StubProjectSwitchService implements ProjectSwitchService {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(StubProjectSwitchService.class);
|
||||
|
||||
private final JdbcTemplate jdbcTemplate;
|
||||
|
||||
public StubProjectSwitchService(JdbcTemplate jdbcTemplate) {
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断用户是否关联任何项目。
|
||||
*
|
||||
* <p>对齐原版:{@code select count(PM_PROJECTID) from PM_PROJECT_USER where userid=?},
|
||||
* 计数 > 0 则为 true。
|
||||
*/
|
||||
@Override
|
||||
public boolean isUserInProjects(String userId) {
|
||||
return false;
|
||||
if (userId == null || userId.isBlank()) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
Long count = jdbcTemplate.queryForObject(
|
||||
"SELECT count(pm_projectid) FROM pm_project_user WHERE userid = ?",
|
||||
Long.class, userId);
|
||||
return count != null && count > 0;
|
||||
} catch (DataAccessException e) {
|
||||
log.warn("[ProjectSwitchService] isUserInProjects 查询失败,降级返回 false: {}", e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询用户关联的所有项目(projectId → projectName)。
|
||||
*
|
||||
* <p>对齐原版:联查 {@code pm_project ⋈ pm_project_user},返回 id→name 映射。
|
||||
*/
|
||||
@Override
|
||||
public Map<String, String> findAllProjectByUserId(String userId) {
|
||||
return new HashMap<>();
|
||||
Map<String, String> result = new HashMap<>();
|
||||
if (userId == null || userId.isBlank()) {
|
||||
return result;
|
||||
}
|
||||
try {
|
||||
List<Map<String, Object>> rows = jdbcTemplate.queryForList(
|
||||
"SELECT pm_project_id, projectname FROM pm_project "
|
||||
+ "WHERE pm_project_id IN "
|
||||
+ "(SELECT DISTINCT pm_projectid FROM pm_project_user WHERE userid = ?)",
|
||||
userId);
|
||||
for (Map<String, Object> row : rows) {
|
||||
String id = (String) row.get("pm_project_id");
|
||||
Object name = row.get("projectname");
|
||||
if (id != null) {
|
||||
result.put(id.trim(), name != null ? name.toString() : "");
|
||||
}
|
||||
}
|
||||
} catch (DataAccessException e) {
|
||||
log.warn("[ProjectSwitchService] findAllProjectByUserId 查询失败,降级返回空: {}", e.getMessage());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取项目所属机构(filiale)。
|
||||
*
|
||||
* <p>对齐原版:{@code select FILIALE from PM_PROJECT where PM_PROJECT_ID=?},
|
||||
* 无行或 DB 不可达均返回 {@code null}。
|
||||
*/
|
||||
@Override
|
||||
public String getProjectOrgId(String projectId) {
|
||||
return null;
|
||||
if (projectId == null || projectId.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
List<String> rows = jdbcTemplate.queryForList(
|
||||
"SELECT filiale FROM pm_project WHERE pm_project_id = ?",
|
||||
String.class, projectId);
|
||||
return rows.isEmpty() ? null : rows.get(0);
|
||||
} catch (DataAccessException e) {
|
||||
log.warn("[ProjectSwitchService] getProjectOrgId 查询失败,降级返回 null: {}", e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,133 @@
|
||||
package com.eip4.platform.auth.stub;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.dao.DataAccessResourceFailureException;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
/**
|
||||
* F2-5 ProjectSwitchService 真实实现单测。
|
||||
*
|
||||
* <p>对齐 EIP-3.0 原版行为(不起 Spring 上下文,Mockito 直构造 JdbcTemplate):
|
||||
* <ul>
|
||||
* <li>{@link StubProjectSwitchService#isUserInProjects} — count > 0 → true</li>
|
||||
* <li>{@link StubProjectSwitchService#findAllProjectByUserId} — 返回 id→name 映射</li>
|
||||
* <li>{@link StubProjectSwitchService#getProjectOrgId} — 返回 filiale 值或 null</li>
|
||||
* <li>DB 不可达时三方法均降级(false/空Map/null),不抛异常</li>
|
||||
* </ul>
|
||||
*/
|
||||
class ProjectSwitchServiceTest {
|
||||
|
||||
private JdbcTemplate jdbc;
|
||||
private StubProjectSwitchService service;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
jdbc = mock(JdbcTemplate.class);
|
||||
service = new StubProjectSwitchService(jdbc);
|
||||
}
|
||||
|
||||
// ── isUserInProjects ─────────────────────────────────────────────────────
|
||||
|
||||
@Test
|
||||
void isUserInProjects_用户有项目返回true() {
|
||||
when(jdbc.queryForObject(anyString(), eq(Long.class), anyString()))
|
||||
.thenReturn(3L);
|
||||
assertTrue(service.isUserInProjects("USER_A"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void isUserInProjects_用户无项目返回false() {
|
||||
when(jdbc.queryForObject(anyString(), eq(Long.class), anyString()))
|
||||
.thenReturn(0L);
|
||||
assertFalse(service.isUserInProjects("USER_B"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void isUserInProjects_空userId返回false() {
|
||||
assertFalse(service.isUserInProjects(null));
|
||||
assertFalse(service.isUserInProjects(""));
|
||||
assertFalse(service.isUserInProjects(" "));
|
||||
}
|
||||
|
||||
@Test
|
||||
void isUserInProjects_DB不可达降级返回false() {
|
||||
when(jdbc.queryForObject(anyString(), eq(Long.class), anyString()))
|
||||
.thenThrow(new DataAccessResourceFailureException("conn refused"));
|
||||
assertFalse(service.isUserInProjects("USER_C"));
|
||||
}
|
||||
|
||||
// ── findAllProjectByUserId ───────────────────────────────────────────────
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
void findAllProjectByUserId_返回id_name映射() {
|
||||
Map<String, Object> row1 = Map.of("pm_project_id", "P001", "projectname", "一期项目");
|
||||
Map<String, Object> row2 = Map.of("pm_project_id", "P002", "projectname", "二期项目");
|
||||
when(jdbc.queryForList(anyString(), anyString()))
|
||||
.thenReturn(List.of(row1, row2));
|
||||
|
||||
Map<String, String> result = service.findAllProjectByUserId("USER_A");
|
||||
assertEquals(2, result.size());
|
||||
assertEquals("一期项目", result.get("P001"));
|
||||
assertEquals("二期项目", result.get("P002"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void findAllProjectByUserId_空userId返回空Map() {
|
||||
assertTrue(service.findAllProjectByUserId(null).isEmpty());
|
||||
assertTrue(service.findAllProjectByUserId("").isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void findAllProjectByUserId_DB不可达降级返回空Map() {
|
||||
when(jdbc.queryForList(anyString(), anyString()))
|
||||
.thenThrow(new DataAccessResourceFailureException("conn refused"));
|
||||
assertTrue(service.findAllProjectByUserId("USER_A").isEmpty());
|
||||
}
|
||||
|
||||
// ── getProjectOrgId ──────────────────────────────────────────────────────
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
void getProjectOrgId_返回filiale() {
|
||||
when(jdbc.queryForList(anyString(), eq(String.class), anyString()))
|
||||
.thenReturn(List.of("ORG_FILIALE_001"));
|
||||
assertEquals("ORG_FILIALE_001", service.getProjectOrgId("P001"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
void getProjectOrgId_无项目返回null() {
|
||||
when(jdbc.queryForList(anyString(), eq(String.class), anyString()))
|
||||
.thenReturn(List.of());
|
||||
assertNull(service.getProjectOrgId("NOT_EXIST"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getProjectOrgId_空projectId返回null() {
|
||||
assertNull(service.getProjectOrgId(null));
|
||||
assertNull(service.getProjectOrgId(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getProjectOrgId_DB不可达降级返回null() {
|
||||
when(jdbc.queryForList(anyString(), eq(String.class), anyString()))
|
||||
.thenThrow(new DataAccessResourceFailureException("conn refused"));
|
||||
assertNull(service.getProjectOrgId("P001"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user