feat(jd): 京东数据库公共化——数据不区分公司、分组全平台一份(2026-11-15 用户定案)

This commit is contained in:
2026-11-15 20:26:55 +08:00
parent b9b66bcb0b
commit 91cf5dfaf7
20 changed files with 508 additions and 415 deletions
@@ -48,14 +48,16 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;
/**
* v1.4 Phase 5:后管「京东数据库」只读接口(列表/详情)与分组管理入口。
*
* <p>结构判据(照 {@code AdminAppearancePatentCleanItemsControllerTest} 双形态):
* 端点只读、鉴权 seam、公司隔离正门、snake 参数带默认值、SQL 只落 XML;
* 行为样本:mock HttpServletRequest 把 company 参数交给 readCompanyIdOrOwn 并装进 200 信封、
* 端点只读、鉴权 seam、公司口径(读侧只剩超管筛选 superAdminScopedCompanyId,公共库不走
* readCompanyIdOrOwn)、snake 参数带默认值、SQL 只落 XML;
* 行为样本:mock HttpServletRequest 把 company 参数交给 superAdminScopedCompanyId 并装进 200 信封、
* 分页钳制 offset/limit(page=Long.MAX_VALUE 不得负,铁律 15)、详情 attr_json
* tabs→attrGroups 解析与非法形状兜底(AC-7)。
* 导出端点与 sale_status 筛选参数已随 2026-11-15 客户口径整段下线(反向钉住防回潜)。</p>
@@ -109,13 +111,14 @@ class AdminJdProductControllerTest {
}
@Test
void 公司隔离走readCompanyIdOrOwn() throws IOException {
void 公司口径只剩超管筛选_公共库不走readCompanyIdOrOwn() throws IOException {
String source = stripComments(Files.readString(CONTROLLER));
assertTrue(source.contains("DataReadScope"), "公司隔离唯一出口是 DataReadScope");
assertTrue(source.contains("readCompanyIdOrOwn"),
"读侧必须走 readCompanyIdOrOwn:超管显式传才收窄,非超管强制本公司");
assertFalse(source.contains("superAdminScopedCompanyId"),
"不得走 superAdminScopedCompanyId:它对非超管返回 null=按行 company_id 查询时把整张表交给每一家");
assertTrue(source.contains("DataReadScope"), "公司维度唯一出口是 DataReadScope");
assertTrue(source.contains("superAdminScopedCompanyId"),
"读侧(列表/类目)必须走 superAdminScopedCompanyId:只有站点超管显式传 company_id 才收窄"
+ "(公司 tab 筛选管理员),其余请求全量(2026-11-15 用户定案「京东数据库不区分公司、属于公共的」)");
assertFalse(source.contains("readCompanyIdOrOwn"),
"不得走 readCompanyIdOrOwn:它对非超管强制本公司,公共库对每一家管理员都是同一份全量");
}
@Test
@@ -169,7 +172,7 @@ class AdminJdProductControllerTest {
// ===================== 行为样本 =====================
@Test
void 控制器把公司参数交给readCompanyIdOrOwn并把结果装进200信封() {
void 控制器把公司参数交给superAdminScopedCompanyId并把结果装进200信封() {
JdProductQueryService queryService = mock(JdProductQueryService.class);
AdminAuthSupport authSupport = mock(AdminAuthSupport.class);
PermissionMenuService permissionMenuService = mock(PermissionMenuService.class);
@@ -183,8 +186,8 @@ class AdminJdProductControllerTest {
HttpServletRequest request = mock(HttpServletRequest.class);
when(authSupport.requireUser(request)).thenReturn(operator);
when(authSupport.currentRole(operator)).thenReturn("super_admin");
// 超管显式传 company_id=5 ⇒ 正门把 5 交出来(不传时正门给 null=全量,同一行为样本家族)。
when(dataReadScope.readCompanyIdOrOwn(operator, 5L)).thenReturn(5L);
// 超管显式传 company_id=5 ⇒ 超管筛选把 5 交出来(不传时给 null=全量,同一行为样本家族)。
when(dataReadScope.superAdminScopedCompanyId(operator, 5L)).thenReturn(5L);
JdProductPageVo pageVo = new JdProductPageVo();
pageVo.setItems(List.of());
pageVo.setTotal(0L);
@@ -195,14 +198,14 @@ class AdminJdProductControllerTest {
ApiResponse<JdProductPageVo> response = controller.page(1L, 20L, null, null, null, null, null,
null, null, 5L, request);
verify(dataReadScope).readCompanyIdOrOwn(operator, 5L);
verify(dataReadScope).superAdminScopedCompanyId(operator, 5L);
verify(queryService).page(1L, 20L, 5L, null, null, null, null, null, null, null);
assertEquals(Boolean.TRUE, response.isSuccess(), "拒绝走 403 业务码,放行必须是 success 信封(B80-03)");
assertEquals(pageVo, response.getData());
}
@Test
void 类目下拉端点走同一正门并回选项集() {
void 类目下拉端点走同一超管筛选口径并回选项集() {
JdProductQueryService queryService = mock(JdProductQueryService.class);
AdminAuthSupport authSupport = mock(AdminAuthSupport.class);
DataReadScope dataReadScope = mock(DataReadScope.class);
@@ -215,8 +218,7 @@ class AdminJdProductControllerTest {
HttpServletRequest request = mock(HttpServletRequest.class);
when(authSupport.requireUser(request)).thenReturn(operator);
when(authSupport.currentRole(operator)).thenReturn("super_admin");
// 非超管正门强制本公司:即使请求带别的 company_id 也按返回值收口(同一行为样本家族)。
when(dataReadScope.readCompanyIdOrOwn(operator, 5L)).thenReturn(5L);
when(dataReadScope.superAdminScopedCompanyId(operator, 5L)).thenReturn(5L);
JdProductCategoryVo row = new JdProductCategoryVo();
row.setName("艺考形体服");
row.setProductCount(85L);
@@ -224,7 +226,7 @@ class AdminJdProductControllerTest {
ApiResponse<List<JdProductCategoryVo>> response = controller.categories(5L, request);
verify(dataReadScope).readCompanyIdOrOwn(operator, 5L);
verify(dataReadScope).superAdminScopedCompanyId(operator, 5L);
verify(queryService).categories(5L);
assertEquals(Boolean.TRUE, response.isSuccess(), "类目下拉与列表同一鉴权正门(200 信封)");
assertEquals("艺考形体服", response.getData().get(0).getName(), "选项=已落库行的末级类目名");
@@ -232,7 +234,7 @@ class AdminJdProductControllerTest {
}
@Test
void 控制器详情走同一鉴权正门并回详情() {
void 控制器详情走同一鉴权正门并回详情且不问公司() {
JdProductQueryService queryService = mock(JdProductQueryService.class);
AdminAuthSupport authSupport = mock(AdminAuthSupport.class);
DataReadScope dataReadScope = mock(DataReadScope.class);
@@ -245,22 +247,21 @@ class AdminJdProductControllerTest {
HttpServletRequest request = mock(HttpServletRequest.class);
when(authSupport.requireUser(request)).thenReturn(operator);
when(authSupport.currentRole(operator)).thenReturn("super_admin");
// 详情不收 company 参数:超管不传=全量可见;非超管由正门强制本公司(服务按返回值收口)。
when(dataReadScope.readCompanyIdOrOwn(operator, null)).thenReturn(null);
JdProductDetailVo detailVo = new JdProductDetailVo();
detailVo.setId(11L);
when(queryService.detail(11L, null)).thenReturn(detailVo);
when(queryService.detail(11L)).thenReturn(detailVo);
ApiResponse<JdProductDetailVo> response = controller.detail(11L, request);
verify(dataReadScope).readCompanyIdOrOwn(operator, null);
verify(queryService).detail(11L, null);
// 公共库详情没有公司维度:DataReadScope 不参与(防有人把公司收口接回来)。
verifyNoInteractions(dataReadScope);
verify(queryService).detail(11L);
assertEquals(Boolean.TRUE, response.isSuccess());
assertEquals(detailVo, response.getData());
}
@Test
void 删除端点走同一正门并把服务回执装进200信封() {
void 删除端点按公共库直判存在并把服务回执装进200信封() {
JdProductQueryService queryService = mock(JdProductQueryService.class);
JdProductDeleteService deleteService = mock(JdProductDeleteService.class);
AdminAuthSupport authSupport = mock(AdminAuthSupport.class);
@@ -274,17 +275,17 @@ class AdminJdProductControllerTest {
HttpServletRequest request = mock(HttpServletRequest.class);
when(authSupport.requireUser(request)).thenReturn(operator);
when(authSupport.currentRole(operator)).thenReturn("super_admin");
when(dataReadScope.readCompanyIdOrOwn(operator, 5L)).thenReturn(5L);
JdProductDeleteVo receipt = new JdProductDeleteVo();
receipt.setDeleted(1);
receipt.setObjectsDeleted(9);
receipt.setObjectsKept(2);
when(deleteService.delete(List.of(11L), 5L)).thenReturn(receipt);
when(deleteService.delete(List.of(11L))).thenReturn(receipt);
ApiResponse<JdProductDeleteVo> response = controller.delete(11L, 5L, request);
ApiResponse<JdProductDeleteVo> response = controller.delete(11L, request);
verify(dataReadScope).readCompanyIdOrOwn(operator, 5L);
verify(deleteService).delete(List.of(11L), 5L);
// 公共库写侧没有公司维度:不收 company_id、DataReadScope 不参与。
verifyNoInteractions(dataReadScope);
verify(deleteService).delete(List.of(11L));
assertEquals(Boolean.TRUE, response.isSuccess());
assertEquals(9, response.getData().getObjectsDeleted());
}
@@ -304,14 +305,13 @@ class AdminJdProductControllerTest {
HttpServletRequest request = mock(HttpServletRequest.class);
when(authSupport.requireUser(request)).thenReturn(operator);
when(authSupport.currentRole(operator)).thenReturn("super_admin");
when(dataReadScope.readCompanyIdOrOwn(operator, null)).thenReturn(5L);
when(deleteService.delete(List.of(11L, 12L), 5L))
.thenThrow(new BusinessException("有 1 个商品不存在或不属于当前公司,请刷新后重试(本次未删除任何数据)"));
when(deleteService.delete(List.of(11L, 12L)))
.thenThrow(new BusinessException("有 1 个商品不存在,请刷新后重试(本次未删除任何数据)"));
JdProductBatchDeleteRequest body = new JdProductBatchDeleteRequest();
body.setIds(List.of(11L, 12L));
BusinessException ex = assertThrows(BusinessException.class,
() -> controller.batchDelete(body, null, request));
() -> controller.batchDelete(body, request));
assertTrue(ex.getMessage().contains("本次未删除任何数据"), "整批拒绝的语义必须到界面");
}
@@ -402,7 +402,7 @@ class AdminJdProductControllerTest {
image.setHeight(800);
when(imageMapper.selectList(any())).thenReturn(List.of(image));
JdProductDetailVo vo = service.detail(11L, 5L);
JdProductDetailVo vo = service.detail(11L);
assertEquals(2, vo.getAttrGroups().size(), "tabs 数组逐个映射成 attrGroups(groupName=tab 字段)");
assertEquals("商品基本信息", vo.getAttrGroups().get(0).getGroupName());
@@ -433,7 +433,7 @@ class AdminJdProductControllerTest {
JdProductEntity broken = productEntity(11L);
broken.setAttrJson("{\"tabs\": [broken");
when(mapper.selectById(11L)).thenReturn(broken);
JdProductDetailVo brokenVo = service.detail(11L, 5L);
JdProductDetailVo brokenVo = service.detail(11L);
assertTrue(brokenVo.getAttrGroups().isEmpty(), "解析异常落空 attrGroups,不抛 500(AC-7)");
assertEquals("{\"tabs\": [broken", brokenVo.getAttrJsonRaw(), "原文进 attrJsonRaw 兜底展示");
@@ -441,7 +441,7 @@ class AdminJdProductControllerTest {
JdProductEntity foreign = productEntity(11L);
foreign.setAttrJson("{\"attrs\":[{\"key\":\"x\",\"value\":\"y\"}]}");
when(mapper.selectById(11L)).thenReturn(foreign);
JdProductDetailVo foreignVo = service.detail(11L, 5L);
JdProductDetailVo foreignVo = service.detail(11L);
assertTrue(foreignVo.getAttrGroups().isEmpty(), "非契约形状落空 attrGroups");
assertEquals("{\"attrs\":[{\"key\":\"x\",\"value\":\"y\"}]}", foreignVo.getAttrJsonRaw(),
"未识别属性不丢弃:原文兜底透出(AC-7)");
@@ -450,7 +450,7 @@ class AdminJdProductControllerTest {
JdProductEntity blank = productEntity(11L);
blank.setAttrJson(null);
when(mapper.selectById(11L)).thenReturn(blank);
JdProductDetailVo blankVo = service.detail(11L, 5L);
JdProductDetailVo blankVo = service.detail(11L);
assertTrue(blankVo.getAttrGroups().isEmpty());
assertNull(blankVo.getAttrJsonRaw());
}
@@ -482,7 +482,7 @@ class AdminJdProductControllerTest {
brokenJson.setSaleAttrsJson("[{\"name\":");
when(skuMapper.selectList(any())).thenReturn(List.of(first, brokenJson));
JdProductDetailVo vo = service.detail(11L, 5L);
JdProductDetailVo vo = service.detail(11L);
assertEquals(2, vo.getSkus().size(), "京麦那 12 行矩阵按入库行下发(抽屉渲染的唯一来源)");
JdProductDetailVo.SkuVo sku = vo.getSkus().get(0);
@@ -499,23 +499,24 @@ class AdminJdProductControllerTest {
}
@Test
void 详情公司越界按不存在拒绝() {
void 详情按公共库直取_缺失按不存在拒绝() {
JdProductMapper mapper = mock(JdProductMapper.class);
JdProductQueryService service = new JdProductQueryService(mapper, mock(JdProductImageMapper.class),
mock(com.nanri.aiimage.modules.jd.mapper.JdProductSkuMapper.class),
mock(com.nanri.aiimage.modules.jd.mapper.JdProductGroupMapper.class));
// 公共库(2026-11-15 用户定案):详情没有公司维度,别家公司时代的行照样可读。
JdProductEntity row = productEntity(11L);
row.setCompanyId(6L);
when(mapper.selectById(11L)).thenReturn(row);
assertNotNull(service.detail(11L));
// 非超管(scopedCompanyId=5):行属公司 6 ⇒ 按「商品不存在」拒绝,不泄露跨公司行的存在性。
BusinessException ex = assertThrows(BusinessException.class, () -> service.detail(11L, 5L));
// 行不存在 ⇒ 按「商品不存在」拒绝。
when(mapper.selectById(11L)).thenReturn(null);
BusinessException ex = assertThrows(BusinessException.class, () -> service.detail(11L));
assertTrue(ex.getMessage().contains("商品不存在"));
// 超管不选公司(null)=全量可见。
assertNotNull(service.detail(11L, null));
// id 非法同样拒绝。
assertThrows(BusinessException.class, () -> service.detail(0L, null));
assertThrows(BusinessException.class, () -> service.detail(0L));
}
@Test
@@ -37,9 +37,10 @@ import static org.mockito.Mockito.when;
* 京东商品删除编排判据(用户口径「后台删除数据的时候也要把桶对应的文件删除」)。
*
* <p>钉四件事:①<b>先删桶、全成功才删库行</b>——桶失败时库行删除出口必须零调用
* (库行先消失就再没人知道该删哪些 key,对象成永久孤儿);②共用对象不删(同公司
* 同内容同 key,删一个商品的图不能把别的商品搞裂图);③归属先判、越界整批拒绝
* (不做部分删除);④前缀外的 key 不动手(只留 WARN),防脏数据把删除扩到别的桶前缀。</p>
* (库行先消失就再没人知道该删哪些 key,对象成永久孤儿);②共用对象不删(同内容同 key,
* 删一个商品的图不能把别的商品搞裂图);③存在先判、缺失整批拒绝(不做部分删除);
* ④前缀外的 key 不动手(只留 WARN),防脏数据把删除扩到别的桶前缀。
* 公共库无公司维度(2026-11-15 用户定案):删除按 id 直判存在性,不再按 company_id 收口。</p>
*/
@ExtendWith(MockitoExtension.class)
class JdProductDeleteServiceTest {
@@ -93,7 +94,7 @@ class JdProductDeleteServiceTest {
@Test
void 先删桶_全成功才删库行() {
givenOwnedRows(List.of(11L));
// selectList 依次被调:①商品归属闸 ②图片行取数 ③共用性回查(mock 不看 wrapper 条件,
// selectList 依次被调:①商品存在闸 ②图片行取数 ③共用性回查(mock 不看 wrapper 条件,
// 所以回查那次显式给空——没人引用这张图,才能真的删)。
when(jdProductMapper.selectList(any())).thenReturn(List.of(productRow(11L)));
when(jdProductImageMapper.selectList(any()))
@@ -101,7 +102,7 @@ class JdProductDeleteServiceTest {
.thenReturn(List.of());
when(jdProductRowDeleteService.deleteRows(List.of(11L))).thenReturn(1);
JdProductDeleteVo vo = service.delete(List.of(11L), COMPANY_ID);
JdProductDeleteVo vo = service.delete(List.of(11L));
// 顺序:removeObject 先于 deleteRows。
var inOrder = org.mockito.Mockito.inOrder(ossStorageService, jdProductRowDeleteService);
@@ -120,7 +121,7 @@ class JdProductDeleteServiceTest {
doThrow(new RuntimeException("oss 5xx"))
.when(ossStorageService).removeObject(anyString(), anyString());
assertThatThrownBy(() -> service.delete(List.of(11L), COMPANY_ID))
assertThatThrownBy(() -> service.delete(List.of(11L)))
.isInstanceOf(BusinessException.class)
.hasMessageContaining("本次未删除任何数据");
@@ -130,14 +131,14 @@ class JdProductDeleteServiceTest {
@Test
void 被其他商品共用的对象保留不删() {
givenOwnedRows(List.of(11L));
// selectList 依次被调:①商品归属闸 ②图片行取数 ③按 key 反查别的商品的共用性回查
// selectList 依次被调:①商品存在闸 ②图片行取数 ③按 key 反查别的商品的共用性回查
//(回查返回 99 号商品也引用同一 key ⇒ 保留)。
when(jdProductImageMapper.selectList(any()))
.thenReturn(List.of(image(11L, "jd-products/5/shared.jpg")))
.thenReturn(List.of(image(99L, "jd-products/5/shared.jpg")));
when(jdProductRowDeleteService.deleteRows(List.of(11L))).thenReturn(1);
JdProductDeleteVo vo = service.delete(List.of(11L), COMPANY_ID);
JdProductDeleteVo vo = service.delete(List.of(11L));
verify(ossStorageService, never()).removeObject(anyString(), anyString());
assertThat(vo.getObjectsDeleted()).isZero();
@@ -147,17 +148,22 @@ class JdProductDeleteServiceTest {
@Test
@SuppressWarnings("unchecked")
void 越界或缺失的行整批拒绝_不做部分删除() {
// 请求 2 行,归属闸只找到 1 行(另一行不存在/不属于本公司)。
void 存在闸不分公司_缺失的行整批拒绝_不做部分删除() {
// 公共库(2026-11-15 用户定案):存在闸 wrapper 不带 company_id 谓词,任何公司的行都可删。
// 请求 2 行,存在闸只找到 1 行(另一行不存在)⇒ 整批拒绝。
JdProductEntity row = new JdProductEntity();
row.setId(11L);
row.setCompanyId(COMPANY_ID);
when(jdProductMapper.selectList(any(Wrapper.class))).thenReturn(List.of(row));
assertThatThrownBy(() -> service.delete(List.of(11L, 12L), COMPANY_ID))
assertThatThrownBy(() -> service.delete(List.of(11L, 12L)))
.isInstanceOf(BusinessException.class)
.hasMessageContaining("本次未删除任何数据");
ArgumentCaptor<Wrapper<JdProductEntity>> captor = ArgumentCaptor.forClass(Wrapper.class);
verify(jdProductMapper).selectList(captor.capture());
assertThat(captor.getValue().getSqlSegment()).doesNotContain("company_id");
verify(ossStorageService, never()).removeObject(anyString(), anyString());
verify(jdProductRowDeleteService, never()).deleteRows(any());
}
@@ -169,7 +175,7 @@ class JdProductDeleteServiceTest {
image(11L, "nanri-ai-images/legacy.jpg")));
when(jdProductRowDeleteService.deleteRows(List.of(11L))).thenReturn(1);
JdProductDeleteVo vo = service.delete(List.of(11L), COMPANY_ID);
JdProductDeleteVo vo = service.delete(List.of(11L));
verify(ossStorageService, never()).removeObject(anyString(), anyString());
assertThat(vo.getObjectsSkipped()).isEqualTo(1);
@@ -178,10 +184,10 @@ class JdProductDeleteServiceTest {
@Test
void 空清单与超上限都在入口拒绝() {
assertThatThrownBy(() -> service.delete(List.of(), COMPANY_ID))
assertThatThrownBy(() -> service.delete(List.of()))
.isInstanceOf(BusinessException.class)
.hasMessageContaining("请先选择");
assertThatThrownBy(() -> service.delete(null, COMPANY_ID))
assertThatThrownBy(() -> service.delete(null))
.isInstanceOf(BusinessException.class)
.hasMessageContaining("请先选择");
@@ -189,28 +195,11 @@ class JdProductDeleteServiceTest {
for (long id = 1; id <= JdProductDeleteService.MAX_BATCH + 1; id++) {
tooMany.add(id);
}
assertThatThrownBy(() -> service.delete(tooMany, COMPANY_ID))
assertThatThrownBy(() -> service.delete(tooMany))
.isInstanceOf(BusinessException.class)
.hasMessageContaining("200");
verify(jdProductMapper, times(0)).selectList(any());
verify(jdProductRowDeleteService, never()).deleteRows(any());
}
@Test
@SuppressWarnings("unchecked")
void 非超管按本公司收口_越公司行不可见() {
// scopedCompanyId=5:归属闸 wrapper 带 company_id=5 ⇒ 别家行查不到 ⇒ 整批拒绝。
JdProductEntity row = new JdProductEntity();
row.setId(11L);
row.setCompanyId(6L);
when(jdProductMapper.selectList(any(Wrapper.class))).thenReturn(List.of());
assertThatThrownBy(() -> service.delete(List.of(11L), COMPANY_ID))
.isInstanceOf(BusinessException.class);
ArgumentCaptor<Wrapper<JdProductEntity>> captor = ArgumentCaptor.forClass(Wrapper.class);
verify(jdProductMapper).selectList(captor.capture());
assertThat(captor.getValue().getSqlSegment()).contains("company_id");
}
}
@@ -11,7 +11,7 @@ import com.nanri.aiimage.modules.jd.model.entity.JdProductGroupEntity;
import com.nanri.aiimage.modules.jd.model.vo.JdProductGroupVo;
import com.nanri.aiimage.testutil.UpdateWrapperProbe;
import org.apache.ibatis.builder.MapperBuilderAssistant;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
@@ -35,9 +35,10 @@ import static org.mockito.Mockito.when;
/**
* v1.4 Phase 5(R4/AC-8/AC-9):京东商品自定义分组业务规则。
*
* <p>判据:同公司重名拒绝不落库(409)、删除先把商品 group_id 置 NULL 再删分组行
* (事务两步,AC-8)、越公司 id 操作按不存在拒绝(不泄露存在性)、productCount
* 读侧现算统计(分组下拉展示用)。</p>
* <p>判据(2026-11-15 用户定案「分组和亚马逊功能一样」=全平台一份,V200 退键后的服务形状):
* 全局重名拒绝不落库(409)、删除先把商品 group_id 置 NULL 再删分组行(事务两步,AC-8,
* 且脱组谓词不带 company_id)、越界 id 操作按不存在拒绝、company_id 列读写两侧都不再引用
* (新行不带公司,DB DEFAULT 0 兜底)、productCount 读侧现算统计(分组下拉展示用)。</p>
*/
class JdProductGroupServiceTest {
@@ -45,29 +46,30 @@ class JdProductGroupServiceTest {
private final JdProductMapper productMapper = mock(JdProductMapper.class);
private final JdProductGroupService service = new JdProductGroupService(groupMapper, productMapper);
@BeforeEach
void initTableInfo() {
@BeforeAll
static void initTableInfo() {
// 探针要读 LambdaUpdateWrapper 的 SET/WHERE 段:裸单测 JVM 里 lambda 列缓存不初始化就解析不了,
// AccountExpiryServiceTest 同款(无 GlobalConfig 下 underCamel=false,列名归一由探针兜)。
TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""),
JdProductEntity.class);
MapperBuilderAssistant assistant = new MapperBuilderAssistant(new MybatisConfiguration(), "");
TableInfoHelper.initTableInfo(assistant, JdProductEntity.class);
TableInfoHelper.initTableInfo(assistant, JdProductGroupEntity.class);
}
// ===================== 新建 =====================
@Test
void 新建同公司重名拒绝且不落库() {
void 新建重名拒绝且不落库() {
when(groupMapper.selectCount(any())).thenReturn(1L);
BusinessException ex = assertThrows(BusinessException.class,
() -> service.create(5L, " 秋冬款 ", 2));
() -> service.create(" 秋冬款 ", 2));
assertEquals(409, ex.getCode(), "重名冲突=409(语义正确的资源冲突码,仓内 DeviceLogService 有先例)");
assertTrue(ex.getMessage().contains("同名分组"), "重名必须给可读文案,不拿 DB 唯一键异常撞 500");
verify(groupMapper, never()).insert(any(JdProductGroupEntity.class));
}
@Test
void 新建成功落公司归属与归一名() {
void 新建成功落归一名与排序且不带公司() {
when(groupMapper.selectCount(any())).thenReturn(0L);
when(groupMapper.insert(any(JdProductGroupEntity.class))).thenAnswer(invocation -> {
JdProductGroupEntity group = invocation.getArgument(0);
@@ -75,25 +77,21 @@ class JdProductGroupServiceTest {
return 1;
});
Long id = service.create(5L, " 秋冬款 ", 2);
Long id = service.create(" 秋冬款 ", 2);
assertEquals(9L, id);
ArgumentCaptor<JdProductGroupEntity> captor = ArgumentCaptor.forClass(JdProductGroupEntity.class);
verify(groupMapper).insert(captor.capture());
JdProductGroupEntity saved = captor.getValue();
assertEquals(5L, saved.getCompanyId(), "归属公司来自隔离正门的 scopedCompanyId,不是请求体");
assertNull(saved.getCompanyId(), "公共库新行不带公司(实体字段不赋值,DB DEFAULT 0=平台公共)");
assertEquals("秋冬款", saved.getName(), "名字 trim 后落库");
assertEquals(2, saved.getSortOrder());
}
@Test
void 新建缺公司或空名直接拒绝() {
// 超管没选公司(scopedCompanyId=null)=分组没有归属公司,直接拒绝。
BusinessException noCompany = assertThrows(BusinessException.class,
() -> service.create(null, "秋冬款", null));
assertEquals(400, noCompany.getCode());
void 新建空名直接拒绝() {
BusinessException blankName = assertThrows(BusinessException.class,
() -> service.create(5L, " ", null));
() -> service.create(" ", null));
assertEquals(400, blankName.getCode());
verifyNoInteractions(groupMapper);
}
@@ -102,7 +100,7 @@ class JdProductGroupServiceTest {
void 新建排序缺省归零() {
when(groupMapper.selectCount(any())).thenReturn(0L);
service.create(5L, "默认排序", null);
service.create("默认排序", null);
ArgumentCaptor<JdProductGroupEntity> captor = ArgumentCaptor.forClass(JdProductGroupEntity.class);
verify(groupMapper).insert(captor.capture());
@@ -112,34 +110,32 @@ class JdProductGroupServiceTest {
// ===================== 改名 =====================
@Test
void 改名越公司id按不存在拒绝() {
JdProductGroupEntity group = groupEntity(1L, 6L, "别家的组");
when(groupMapper.selectById(1L)).thenReturn(group);
void 改名不存在按不存在拒绝() {
when(groupMapper.selectById(1L)).thenReturn(null);
BusinessException ex = assertThrows(BusinessException.class,
() -> service.rename(1L, 5L, "新名字", null));
assertTrue(ex.getMessage().contains("分组不存在"),
"越界 id 与 id 不存在是同一种响应,不泄露跨公司行的存在性");
() -> service.rename(1L, "新名字", null));
assertTrue(ex.getMessage().contains("分组不存在"));
verify(groupMapper, never()).updateById(any(JdProductGroupEntity.class));
}
@Test
void 改名重名拒绝且不落库() {
when(groupMapper.selectById(1L)).thenReturn(groupEntity(1L, 5L, "旧名"));
when(groupMapper.selectById(1L)).thenReturn(groupEntity(1L, "旧名"));
when(groupMapper.selectCount(any())).thenReturn(1L);
BusinessException ex = assertThrows(BusinessException.class,
() -> service.rename(1L, 5L, "已有名", 3));
() -> service.rename(1L, "已有名", 3));
assertEquals(409, ex.getCode());
verify(groupMapper, never()).updateById(any(JdProductGroupEntity.class));
}
@Test
void 改名成功带归一名与排序() {
when(groupMapper.selectById(1L)).thenReturn(groupEntity(1L, 5L, "旧名"));
when(groupMapper.selectById(1L)).thenReturn(groupEntity(1L, "旧名"));
when(groupMapper.selectCount(any())).thenReturn(0L);
service.rename(1L, 5L, " 新名 ", 7);
service.rename(1L, " 新名 ", 7);
ArgumentCaptor<JdProductGroupEntity> captor = ArgumentCaptor.forClass(JdProductGroupEntity.class);
verify(groupMapper).updateById(captor.capture());
@@ -149,10 +145,10 @@ class JdProductGroupServiceTest {
@Test
void 改名排序不传保持原值() {
when(groupMapper.selectById(1L)).thenReturn(groupEntity(1L, 5L, "旧名"));
when(groupMapper.selectById(1L)).thenReturn(groupEntity(1L, "旧名"));
when(groupMapper.selectCount(any())).thenReturn(0L);
service.rename(1L, 5L, "只改名", null);
service.rename(1L, "只改名", null);
ArgumentCaptor<JdProductGroupEntity> captor = ArgumentCaptor.forClass(JdProductGroupEntity.class);
verify(groupMapper).updateById(captor.capture());
@@ -163,29 +159,31 @@ class JdProductGroupServiceTest {
@Test
void 删除先把商品组置NULL再删分组行() {
when(groupMapper.selectById(1L)).thenReturn(groupEntity(1L, 5L, "秋冬款"));
when(groupMapper.selectById(1L)).thenReturn(groupEntity(1L, "秋冬款"));
when(productMapper.update(any(), any(Wrapper.class))).thenReturn(3);
int detached = service.delete(1L, 5L);
int detached = service.delete(1L);
assertEquals(3, detached, "返回实际脱组的商品行数(前端拿它做反馈,不猜)");
ArgumentCaptor<Wrapper<JdProductEntity>> wrapperCaptor = ArgumentCaptor.forClass(Wrapper.class);
verify(productMapper).update(any(), wrapperCaptor.capture());
// 置 NULL 语义必须落在 SET group_id=NULL(商品保留,脱组即「未分组」),且限定本公司该组。
// 置 NULL 语义必须落在 SET group_id=NULL(商品保留,脱组即「未分组」)。
var setValues = UpdateWrapperProbe.setValues(wrapperCaptor.getValue());
assertTrue(setValues.containsKey("group_id") && setValues.get("group_id") == null,
"删除分组必须把商品 group_id 置 NULL(SET group_id=NULL),不是删商品行:实得 " + setValues);
// 公共库:脱组谓词只限定该组,不带 company_id。
var whereEq = UpdateWrapperProbe.whereEq(wrapperCaptor.getValue());
assertEquals(5L, ((Number) whereEq.get("company_id")).longValue(), "脱组必须限定本公司");
assertEquals(1L, ((Number) whereEq.get("group_id")).longValue(), "脱组必须限定该组");
assertTrue(!whereEq.containsKey("company_id"),
"分组公共化后脱组谓词不得再带 company_id:实得 " + whereEq);
verify(groupMapper).deleteById(1L);
}
@Test
void 删除越公司id拒绝且一个字节都不写() {
when(groupMapper.selectById(1L)).thenReturn(groupEntity(1L, 6L, "别家的组"));
void 删除不存在拒绝且一个字节都不写() {
when(groupMapper.selectById(1L)).thenReturn(null);
assertThrows(BusinessException.class, () -> service.delete(1L, 5L));
assertThrows(BusinessException.class, () -> service.delete(1L));
verifyNoInteractions(productMapper);
verify(groupMapper, never()).deleteById(anyLong());
}
@@ -194,22 +192,23 @@ class JdProductGroupServiceTest {
void 删除分组不存在直接拒绝() {
when(groupMapper.selectById(404L)).thenReturn(null);
assertThrows(BusinessException.class, () -> service.delete(404L, null));
assertThrows(BusinessException.class, () -> service.delete(404L));
verifyNoInteractions(productMapper);
}
// ===================== 列表(productCount 统计) =====================
@Test
void 列表按排序返回且productCount现算() {
JdProductGroupEntity first = groupEntity(2L, 5L, "晚出厂");
@SuppressWarnings("unchecked")
void 列表全平台一份不带公司谓词且productCount现算() {
JdProductGroupEntity first = groupEntity(2L, "晚出厂");
first.setSortOrder(0);
JdProductGroupEntity second = groupEntity(1L, 5L, "早出厂");
JdProductGroupEntity second = groupEntity(1L, "早出厂");
second.setSortOrder(1);
when(groupMapper.selectList(any())).thenReturn(List.of(first, second));
when(productMapper.selectCount(any())).thenReturn(12L, 0L);
List<JdProductGroupVo> items = service.list(5L);
List<JdProductGroupVo> items = service.list();
assertEquals(2, items.size());
assertEquals("晚出厂", items.get(0).getName(), "selectList 的排序由 wrapper 决定,服务不重排");
@@ -218,24 +217,20 @@ class JdProductGroupServiceTest {
assertEquals(1, items.get(1).getSortOrder());
verify(productMapper, times(2)).selectCount(any());
assertNotNull(items.get(0).getCreatedAt());
}
@Test
void 列表超管不选公司查全量() {
when(groupMapper.selectList(any())).thenReturn(List.of());
List<JdProductGroupVo> items = service.list(null);
assertTrue(items.isEmpty());
verify(groupMapper).selectList(any());
// 公共库:列表 wrapper 不带 company_id 谓词(历史行的 company_id 只是溯源标记)。
ArgumentCaptor<Wrapper<JdProductGroupEntity>> captor = ArgumentCaptor.forClass(Wrapper.class);
verify(groupMapper).selectList(captor.capture());
assertTrue(!captor.getValue().getSqlSegment().contains("company_id"),
"分组列表不得再按公司过滤(2026-11-15 公共化):实得 " + captor.getValue().getSqlSegment());
}
// ---------------------------------------------------------------- 助手
private static JdProductGroupEntity groupEntity(long id, long companyId, String name) {
private static JdProductGroupEntity groupEntity(long id, String name) {
JdProductGroupEntity group = new JdProductGroupEntity();
group.setId(id);
group.setCompanyId(companyId);
group.setCompanyId(5L);
group.setName(name);
group.setSortOrder(3);
group.setCreatedAt(LocalDateTime.of(2026, 11, 14, 10, 0));
@@ -42,9 +42,9 @@ import static org.mockito.Mockito.when;
/**
* 后管「编辑京东商品」编排判据(PUT /api/admin/jd-products/{id},2026-11-15 用户口径)。
*
* <p>钉六件事:①归属闸(越界 id 按不存在拒绝,零写);②主字段全量保存即置人工编辑锁
* <p>钉六件事:①存在闸(行不存在按不存在拒绝,零写;公共库无公司维度);②主字段全量保存即置人工编辑锁
* {@code manual_locked=1}(V199——没有这个锁,编辑会被下一次采集 upsert 覆盖回去);
* ③分组归属校验(跨公司挂组拒绝);④属性重建只覆写 attr_json 的 {@code tabs} 段、
* ③分组存在校验(分组 2026-11-15 起全平台一份,挂组只认存在性);④属性重建只覆写 attr_json 的 {@code tabs} 段、
* images/productCode 等其它顶层键原样保留(铁律 30 形状),原文形状不识别时拒绝而不是静默重写;
* ⑤SKU 按 (productId, skuId) 匹配改价改库存,缺失 skuId 拒绝;⑥删图=先删桶、共用对象保留、
* image_count 按表实数刷新。</p>
@@ -124,18 +124,19 @@ class JdProductUpdateServiceTest {
// ---------- 判据 ----------
@Test
void 归属闸_越界或不存在按不存在拒绝且零写() {
void 行不存在按不存在拒绝_公共库跨公司行可编辑() {
// 公共库(2026-11-15 用户定案):行存在即可编辑,company_id 只是同步来源标记。
JdProductEntity otherCompany = productRow();
otherCompany.setCompanyId(99L);
when(jdProductMapper.selectById(PRODUCT_ID)).thenReturn(otherCompany);
when(jdProductMapper.update(isNull(), any(Wrapper.class))).thenReturn(1);
assertThat(service.update(PRODUCT_ID, mainFieldsOnly())).isEqualTo(PRODUCT_ID);
assertThatThrownBy(() -> service.update(PRODUCT_ID, mainFieldsOnly(), COMPANY_ID))
// 行不存在 ⇒ 按不存在拒绝,零写。
when(jdProductMapper.selectById(PRODUCT_ID)).thenReturn(null);
assertThatThrownBy(() -> service.update(PRODUCT_ID, mainFieldsOnly()))
.isInstanceOf(BusinessException.class)
.hasMessageContaining("商品不存在");
verify(jdProductMapper, never()).update(isNull(), any(Wrapper.class));
assertThatThrownBy(() -> service.update(PRODUCT_ID, mainFieldsOnly(), COMPANY_ID))
.isInstanceOf(BusinessException.class);
}
@Test
@@ -143,7 +144,7 @@ class JdProductUpdateServiceTest {
givenOwnedRow();
when(jdProductMapper.update(isNull(), any(Wrapper.class))).thenReturn(1);
Long id = service.update(PRODUCT_ID, mainFieldsOnly(), COMPANY_ID);
Long id = service.update(PRODUCT_ID, mainFieldsOnly());
assertThat(id).isEqualTo(PRODUCT_ID);
Map<String, Object> values = capturedSetValues();
@@ -159,19 +160,27 @@ class JdProductUpdateServiceTest {
}
@Test
void 分组必须属于本公司() {
void 分组必须存在_公共组无公司维度() {
givenOwnedRow();
JdProductUpdateRequest body = mainFieldsOnly();
body.setGroupId(7L);
// 分组 2026-11-15 起全平台一份:别家时代建的组(company_id=99)照样能挂。
JdProductGroupEntity group = new JdProductGroupEntity();
group.setId(7L);
group.setCompanyId(99L);
when(jdProductGroupMapper.selectById(7L)).thenReturn(group);
when(jdProductMapper.update(isNull(), any(Wrapper.class))).thenReturn(1);
assertThat(service.update(PRODUCT_ID, body)).isEqualTo(PRODUCT_ID);
assertThatThrownBy(() -> service.update(PRODUCT_ID, body, COMPANY_ID))
// 分组不存在 ⇒ 拒绝且零写。
JdProductUpdateRequest ghost = mainFieldsOnly();
ghost.setGroupId(404L);
when(jdProductGroupMapper.selectById(404L)).thenReturn(null);
assertThatThrownBy(() -> service.update(PRODUCT_ID, ghost))
.isInstanceOf(BusinessException.class)
.hasMessageContaining("分组不存在或不属于当前公司");
verify(jdProductMapper, never()).update(isNull(), any(Wrapper.class));
.hasMessageContaining("分组不存在");
// update 只剩成功挂组那 1 次:ghost 分支零写点(同用例前半段已成功写过一次,不能按 never 数)。
verify(jdProductMapper, org.mockito.Mockito.times(1)).update(isNull(), any(Wrapper.class));
}
@Test
@@ -188,7 +197,7 @@ class JdProductUpdateServiceTest {
group.setAttrs(List.of(attr));
body.setAttrGroups(List.of(group));
service.update(PRODUCT_ID, body, COMPANY_ID);
service.update(PRODUCT_ID, body);
Map<String, Object> values = capturedSetValues();
// 新 JSON 里有重建后的 tabs(新属性值落进去了),且 images/productCode 原样保留(铁律 30)
@@ -206,7 +215,7 @@ class JdProductUpdateServiceTest {
JdProductUpdateRequest body = mainFieldsOnly();
body.setAttrGroups(List.of(new JdProductUpdateRequest.AttrGroupDto()));
assertThatThrownBy(() -> service.update(PRODUCT_ID, body, COMPANY_ID))
assertThatThrownBy(() -> service.update(PRODUCT_ID, body))
.isInstanceOf(BusinessException.class)
.hasMessageContaining("属性原文形状不识别");
verify(jdProductMapper, never()).update(isNull(), any(Wrapper.class));
@@ -228,7 +237,7 @@ class JdProductUpdateServiceTest {
edit.setPrice(new BigDecimal("199.00"));
edit.setStock(88);
body.setSkus(List.of(edit));
service.update(PRODUCT_ID, body, COMPANY_ID);
service.update(PRODUCT_ID, body);
ArgumentCaptor<Wrapper<JdProductSkuEntity>> captor = ArgumentCaptor.forClass(Wrapper.class);
verify(jdProductSkuMapper).update(isNull(), captor.capture());
@@ -242,7 +251,7 @@ class JdProductUpdateServiceTest {
ghost.setSkuId("ghost-sku");
ghost.setPrice(BigDecimal.ONE);
bad.setSkus(List.of(ghost));
assertThatThrownBy(() -> service.update(PRODUCT_ID, bad, COMPANY_ID))
assertThatThrownBy(() -> service.update(PRODUCT_ID, bad))
.isInstanceOf(BusinessException.class)
.hasMessageContaining("SKU 行不存在");
}
@@ -266,14 +275,14 @@ class JdProductUpdateServiceTest {
JdProductUpdateRequest body = mainFieldsOnly();
body.setDeleteImageIds(List.of(1L, 2L));
assertThatThrownBy(() -> service.update(PRODUCT_ID, body, COMPANY_ID))
assertThatThrownBy(() -> service.update(PRODUCT_ID, body))
.isInstanceOf(BusinessException.class)
.hasMessageContaining("本次未删除任何数据");
// 修正归属:只删 1L;同 key 被别的商品引用 ⇒ 对象保留、只删登记行、计数刷新
JdProductUpdateRequest fixed = mainFieldsOnly();
fixed.setDeleteImageIds(List.of(1L));
service.update(PRODUCT_ID, fixed, COMPANY_ID);
service.update(PRODUCT_ID, fixed);
verify(ossStorageService, never()).removeObject(eq("jd-products"), eq("jd-products/5/aaa.jpg"));
verify(jdProductImageMapper).delete(any(Wrapper.class));
@@ -288,13 +297,13 @@ class JdProductUpdateServiceTest {
JdProductUpdateRequest badPrice = mainFieldsOnly();
badPrice.setPriceMin(new BigDecimal("100"));
badPrice.setPriceMax(new BigDecimal("50"));
assertThatThrownBy(() -> service.update(PRODUCT_ID, badPrice, COMPANY_ID))
assertThatThrownBy(() -> service.update(PRODUCT_ID, badPrice))
.isInstanceOf(BusinessException.class)
.hasMessageContaining("价格区间下限不能大于上限");
JdProductUpdateRequest badStock = mainFieldsOnly();
badStock.setStockTotal(-1);
assertThatThrownBy(() -> service.update(PRODUCT_ID, badStock, COMPANY_ID))
assertThatThrownBy(() -> service.update(PRODUCT_ID, badStock))
.isInstanceOf(BusinessException.class)
.hasMessageContaining("总库存不能为负数");
verify(jdProductMapper, never()).update(isNull(), any(Wrapper.class));