feat(jd): 商品上架数量上限配置(默认 5000,管理员+名下员工合计口径,2026-11-15 用户口径)——aiimage.jd.max-products-per-admin(env AIIMAGE_JD_MAX_PRODUCTS_PER_ADMIN,0=不限,yml 与 @Value 同默认、application-local.example.yml 同步)+ 新增 JdPublishQuotaService 配额原语:计数源=biz_jd_product 按 owner uid 集计行数(JdProductMapper.countByUserIds,SQL 收口 XML,空集服务侧按 0 不进 SQL);uid 集复用 UserDataScopeSupport.resolveVisibleUserIds(管理员=本人+名下员工/分组成员+created_by_id 兜底,公司超管=全公司),普通账号先锚定 created_by_id 指向的管理员(读不到回退自己);站点超管豁免(走 DeviceSessionPolicy.isSuperAdmin 单出口)。出口两个:quotaOf 只读快照(供上架界面展示余量)+ assertWithinQuota 建上架单前的配额闸(超限抛 BusinessException,文案含占用/上限数字)。⚠ 本笔无生产调用方——商品上架功能目前是用户端占位卡(建设中、后端零代码),本笔只落配置与可测配额服务,不改任何现有链路行为;上架功能落地时在其建单入口调用 assertWithinQuota。判据:JdPublishQuotaServiceTest 7 例全绿(默认 5000/0=不限不查库/锚定到管理员/边界恰好等于上限放行/站点超管豁免/空集 used=0/读不到主管回退);后端 mvn -o test 编译零错,Jd*Test 108 例 107 绿(唯一红=另一路会话在飞件 JdProductUpdateServiceTest:182 与其 Service:130 的 never() 冲突,两文件均属在飞改动、与本笔零交集,未代改);ops 配置棘轮 test_no_implicit_config_switches 7 passed

This commit is contained in:
2026-11-15 20:18:27 +08:00
parent 84a4bc38b5
commit b9b66bcb0b
6 changed files with 280 additions and 0 deletions
@@ -0,0 +1,152 @@
package com.nanri.aiimage.modules.jd.service;
import com.nanri.aiimage.common.exception.BusinessException;
import com.nanri.aiimage.common.mapper.AdminUserMapper;
import com.nanri.aiimage.common.model.entity.AdminUserEntity;
import com.nanri.aiimage.modules.jd.mapper.JdProductMapper;
import com.nanri.aiimage.modules.permission.support.UserDataScopeSupport;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;
/**
* 商品上架配额(JdPublishQuotaService)行为测试:默认 5000、0=不限、普通账号锚定到
* created_by_id 管理员并按其团队 uid 集计数、站点超管豁免、恰好等于上限的边界放行、
* 空集按 used=0 不进 SQL。判限口径=「管理员+名下员工合计」(2026-11-15 用户口径)。
*/
@ExtendWith(MockitoExtension.class)
class JdPublishQuotaServiceTest {
@Mock
private AdminUserMapper adminUserMapper;
@Mock
private UserDataScopeSupport userDataScopeSupport;
@Mock
private JdProductMapper jdProductMapper;
private JdPublishQuotaService newService() {
return new JdPublishQuotaService(adminUserMapper, userDataScopeSupport, jdProductMapper);
}
private AdminUserEntity admin(long id) {
AdminUserEntity user = new AdminUserEntity();
user.setId(id);
user.setRole("admin");
return user;
}
/** 普通账号:role 为空(resolveRole 返回 null),created_by_id 指向所属管理员。 */
private AdminUserEntity employee(long id, long adminId) {
AdminUserEntity user = new AdminUserEntity();
user.setId(id);
user.setCreatedById(adminId);
return user;
}
/** 双重载(Long/实体)消歧:lambda 参数显式标注实体形状(argThat(...))。 */
@Test
void 默认上限5000_超限拒绝且文案含占用与上限() {
JdPublishQuotaService service = newService();
AdminUserEntity employee = employee(9L, 5L);
when(adminUserMapper.selectById(5L)).thenReturn(admin(5L));
when(userDataScopeSupport.resolveVisibleUserIds(argThat((AdminUserEntity u) -> u != null && u.getId() == 5L)))
.thenReturn(List.of(5L, 9L, 10L));
when(jdProductMapper.countByUserIds(List.of(5L, 9L, 10L))).thenReturn(4990L);
assertThatThrownBy(() -> service.assertWithinQuota(employee, 20))
.isInstanceOf(BusinessException.class)
.hasMessageContaining("4990")
.hasMessageContaining("5000")
.hasMessageContaining("管理员");
// 普通账号必须锚定到所属管理员(uid=5)的团队集,而不是只数自己
verify(userDataScopeSupport).resolveVisibleUserIds(argThat((AdminUserEntity u) -> u != null && u.getId() == 5L));
}
@Test
void 恰好等于上限_放行() {
JdPublishQuotaService service = newService();
AdminUserEntity operator = admin(5L);
when(userDataScopeSupport.resolveVisibleUserIds(argThat((AdminUserEntity u) -> u != null && u.getId() == 5L)))
.thenReturn(List.of(5L, 9L));
when(jdProductMapper.countByUserIds(List.of(5L, 9L))).thenReturn(4990L);
assertThatCode(() -> service.assertWithinQuota(operator, 10)).doesNotThrowAnyException();
}
@Test
void 上限为0_整体放行且不查库() {
JdPublishQuotaService service = newService();
service.maxProductsPerAdmin = 0L;
assertThatCode(() -> service.assertWithinQuota(admin(5L), 999_999)).doesNotThrowAnyException();
verifyNoInteractions(jdProductMapper, userDataScopeSupport, adminUserMapper);
}
@Test
void 站点超管_豁免不限额() {
JdPublishQuotaService service = newService();
AdminUserEntity siteSuperAdmin = new AdminUserEntity();
siteSuperAdmin.setId(1L);
siteSuperAdmin.setRole("super_admin");
assertThatCode(() -> service.assertWithinQuota(siteSuperAdmin, 999_999)).doesNotThrowAnyException();
verifyNoInteractions(jdProductMapper, userDataScopeSupport, adminUserMapper);
}
@Test
void 团队uid集为空_按used0处理且不进SQL() {
JdPublishQuotaService service = newService();
AdminUserEntity operator = admin(5L);
when(userDataScopeSupport.resolveVisibleUserIds(argThat((AdminUserEntity u) -> u != null && u.getId() == 5L)))
.thenReturn(List.of());
// used=0:additional=5000 恰好等于上限,放行
assertThatCode(() -> service.assertWithinQuota(operator, 5000)).doesNotThrowAnyException();
// additional=5001 超限,但空集不查库
assertThatThrownBy(() -> service.assertWithinQuota(operator, 5001))
.isInstanceOf(BusinessException.class)
.hasMessageContaining("0");
verifyNoInteractions(jdProductMapper);
}
@Test
void 快照返回占用与上限() {
JdPublishQuotaService service = newService();
AdminUserEntity operator = admin(5L);
when(userDataScopeSupport.resolveVisibleUserIds(argThat((AdminUserEntity u) -> u != null && u.getId() == 5L)))
.thenReturn(List.of(5L, 9L));
when(jdProductMapper.countByUserIds(List.of(5L, 9L))).thenReturn(1234L);
JdPublishQuotaService.QuotaSnapshot snapshot = service.quotaOf(operator);
assertThat(snapshot.used()).isEqualTo(1234L);
assertThat(snapshot.limit()).isEqualTo(5000L);
assertThat(snapshot.unlimited()).isFalse();
}
@Test
void 普通账号读不到主管_回退按自己计() {
JdPublishQuotaService service = newService();
AdminUserEntity employee = employee(9L, 5L);
when(adminUserMapper.selectById(5L)).thenReturn(null);
when(userDataScopeSupport.resolveVisibleUserIds(argThat((AdminUserEntity u) -> u != null && u.getId() == 9L)))
.thenReturn(List.of(9L));
when(jdProductMapper.countByUserIds(List.of(9L))).thenReturn(5000L);
assertThatThrownBy(() -> service.assertWithinQuota(employee, 1))
.isInstanceOf(BusinessException.class)
.hasMessageContaining("5000");
verify(userDataScopeSupport).resolveVisibleUserIds(argThat((AdminUserEntity u) -> u != null && u.getId() == 9L));
}
}