task-116: 后台管理新增/导入表单弹窗化 + 分组管理独立菜单与UI优化 + 轻量进度端点
- 后台管理页(admin)所有面板的新增/导入表单改为弹窗操作,原有字段 ID 全部保留、提交逻辑不变;导入删除入口不再触发二次确认拦截 - 分组管理升级为独立菜单(V102 + schema initializer),移除 5 个面板内的管理分组按钮;分组列表改为蓝白主题、增加权限分组横幅 - Python 侧 group-manage 权限守卫(_ensure_backend_menu_access 补充 group-manage) - 引入 V101(biz_task_file_job 复合索引)+ 新增 TaskProgressLight/TaskFileJob 轻量端点与进度聚合作 - 前端 progress-light / page-separated-loads / dispatch-guard 共享模块及单元测试
This commit is contained in:
+449
@@ -0,0 +1,449 @@
|
||||
package com.nanri.aiimage.modules.appearancepatent.service;
|
||||
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.common.service.DistributedJobLockService;
|
||||
import com.nanri.aiimage.config.AppearancePatentProperties;
|
||||
import com.nanri.aiimage.config.InstanceMetadata;
|
||||
import com.nanri.aiimage.config.StorageProperties;
|
||||
import com.nanri.aiimage.modules.appearancepatent.client.AppearancePatentLlmClient;
|
||||
import com.nanri.aiimage.modules.appearancepatent.model.dto.AppearancePatentParsedPayloadDto;
|
||||
import com.nanri.aiimage.modules.appearancepatent.model.dto.AppearancePatentResultRowDto;
|
||||
import com.nanri.aiimage.modules.appearancepatent.model.dto.AppearancePatentSubmitResultRequest;
|
||||
import com.nanri.aiimage.modules.appearancepatent.model.vo.AppearancePatentParsedRowVo;
|
||||
import com.nanri.aiimage.modules.appearancepatent.service.AppearancePatentTaskCacheService;
|
||||
import com.nanri.aiimage.modules.appearancepatent.service.support.AppearancePatentSheetBuilder;
|
||||
import com.nanri.aiimage.modules.appearancepatent.service.support.AppearancePatentRowNormalizer;
|
||||
import com.nanri.aiimage.modules.file.service.LocalFileStorageService;
|
||||
import com.nanri.aiimage.modules.task.mapper.FileResultMapper;
|
||||
import com.nanri.aiimage.modules.task.mapper.FileTaskMapper;
|
||||
import com.nanri.aiimage.modules.task.mapper.TaskChunkMapper;
|
||||
import com.nanri.aiimage.modules.task.mapper.TaskScopeStateMapper;
|
||||
import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity;
|
||||
import com.nanri.aiimage.modules.task.model.entity.TaskChunkEntity;
|
||||
import com.nanri.aiimage.modules.task.model.entity.TaskScopeStateEntity;
|
||||
import com.nanri.aiimage.modules.task.service.TaskDistributedLockService;
|
||||
import com.nanri.aiimage.modules.task.service.TaskFileJobService;
|
||||
import com.nanri.aiimage.modules.task.service.TaskProgressLightAssembler;
|
||||
import com.nanri.aiimage.modules.task.service.TaskProgressSnapshotService;
|
||||
import com.nanri.aiimage.modules.task.service.TransientPayloadStorageService;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.apache.poi.ss.usermodel.Row;
|
||||
import org.apache.poi.ss.usermodel.Sheet;
|
||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
|
||||
import java.io.File;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.lenient;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* 任务 95:AppearancePatent 门面改委托。
|
||||
* writeResultWorkbook/writeReasonSheet 改为委托 AppearancePatentSheetBuilder.buildResultSheet
|
||||
* (门面保留 SXSSF 写出、异常包装与 rowToken 优先的 findResultRow 语义);
|
||||
* submitResult 双事务路径与 owner 检查不动;Sheet 链专属私有方法从门面移除。
|
||||
* 私有门面方法经反射直接验证,不经过完整 job 链路。
|
||||
*/
|
||||
class AppearancePatentTaskServiceDelegationTest {
|
||||
|
||||
private FileTaskMapper fileTaskMapper = mock(FileTaskMapper.class);
|
||||
private FileResultMapper fileResultMapper = mock(FileResultMapper.class);
|
||||
private TaskScopeStateMapper taskScopeStateMapper = mock(TaskScopeStateMapper.class);
|
||||
private TaskChunkMapper taskChunkMapper = mock(TaskChunkMapper.class);
|
||||
private LocalFileStorageService localFileStorageService = mock(LocalFileStorageService.class);
|
||||
private AppearancePatentLlmClient llmClient = mock(AppearancePatentLlmClient.class);
|
||||
private AppearancePatentTaskCacheService taskCacheService = mock(AppearancePatentTaskCacheService.class); private AppearancePatentProperties properties = mock(AppearancePatentProperties.class);
|
||||
private StorageProperties storageProperties = mock(StorageProperties.class);
|
||||
private TaskFileJobService taskFileJobService = mock(TaskFileJobService.class);
|
||||
private TaskProgressSnapshotService taskProgressSnapshotService = mock(TaskProgressSnapshotService.class);
|
||||
private TransientPayloadStorageService transientPayloadStorageService = mock(TransientPayloadStorageService.class);
|
||||
private PlatformTransactionManager transactionManager = mock(PlatformTransactionManager.class);
|
||||
private DistributedJobLockService distributedJobLockService = mock(DistributedJobLockService.class);
|
||||
private TaskDistributedLockService taskDistributedLockService = mock(TaskDistributedLockService.class);
|
||||
private InstanceMetadata instanceMetadata = mock(InstanceMetadata.class);
|
||||
|
||||
private AppearancePatentTaskService service;
|
||||
private final AtomicInteger txCount = new AtomicInteger();
|
||||
|
||||
@BeforeEach
|
||||
void setUp() throws Exception {
|
||||
lenient().when(properties.getLlmBatchSize()).thenReturn(50);
|
||||
// TransactionTemplate.execute 需要 getTransaction 返回非 null status;commit/rollback 默认为 no-op
|
||||
lenient().when(transactionManager.getTransaction(any()))
|
||||
.thenReturn(mock(org.springframework.transaction.TransactionStatus.class));
|
||||
lenient().when(transientPayloadStorageService.isSharedWriteEnabled()).thenReturn(true);
|
||||
service = newService();
|
||||
}
|
||||
|
||||
private AppearancePatentTaskService newService() {
|
||||
return new AppearancePatentTaskService(
|
||||
localFileStorageService, null, storageProperties, fileTaskMapper, fileResultMapper,
|
||||
taskScopeStateMapper, taskChunkMapper, new ObjectMapper(), llmClient, taskCacheService,
|
||||
properties, taskFileJobService, taskProgressSnapshotService,
|
||||
transientPayloadStorageService, transactionManager, distributedJobLockService,
|
||||
taskDistributedLockService, instanceMetadata,
|
||||
mock(TaskProgressLightAssembler.class));
|
||||
}
|
||||
|
||||
private AppearancePatentTaskService serviceWithoutTransactionManager() {
|
||||
return new AppearancePatentTaskService(
|
||||
localFileStorageService, null, storageProperties, fileTaskMapper, fileResultMapper,
|
||||
taskScopeStateMapper, taskChunkMapper, new ObjectMapper(), llmClient, taskCacheService,
|
||||
properties, taskFileJobService, taskProgressSnapshotService,
|
||||
transientPayloadStorageService, null, distributedJobLockService,
|
||||
taskDistributedLockService, instanceMetadata,
|
||||
mock(TaskProgressLightAssembler.class));
|
||||
}
|
||||
|
||||
// ---------- 1 签名不变 ----------
|
||||
|
||||
@Test
|
||||
void test_write_result_workbook_signature_unchanged() throws Exception {
|
||||
Method method = AppearancePatentTaskService.class.getDeclaredMethod(
|
||||
"writeResultWorkbook",
|
||||
File.class,
|
||||
AppearancePatentParsedPayloadDto.class,
|
||||
List.class,
|
||||
Map.class);
|
||||
assertEquals(void.class, method.getReturnType(), "返回类型不变");
|
||||
assertEquals(4, method.getParameterCount(), "参数个数不变");
|
||||
}
|
||||
|
||||
// ---------- 2 委托各组件 ----------
|
||||
|
||||
@Test
|
||||
void test_write_result_workbook_delegates_sheet_builder() throws Exception {
|
||||
AppearancePatentParsedRowVo row = parsedRow("2_1", "B01A", "US", "f1.xlsx");
|
||||
AppearancePatentResultRowDto resultRow = resultRow("2_1", "B01A", "US", "f1.xlsx::row::2");
|
||||
File out = new File("target/appearance-patent-delegation-tmp", "delegate.xlsx");
|
||||
if (out.exists()) {
|
||||
out.delete();
|
||||
}
|
||||
|
||||
invokeWriteResultWorkbook(out, row, resultRow);
|
||||
|
||||
assertTrue(out.exists(), "委托后结果文件已生成");
|
||||
try (XSSFWorkbook wb = new XSSFWorkbook(out)) {
|
||||
Sheet main = wb.getSheet("外观专利检测结果");
|
||||
assertNotNull(main, "主 sheet 由 SheetBuilder 产出");
|
||||
Row data = main.getRow(1);
|
||||
assertEquals("2_1", data.getCell(0).getStringCellValue());
|
||||
assertEquals("B01A", data.getCell(1).getStringCellValue());
|
||||
assertEquals("US", data.getCell(2).getStringCellValue());
|
||||
assertEquals("brand-A", data.getCell(4).getStringCellValue(), "品牌经委托解析");
|
||||
assertEquals("19.90", data.getCell(5).getStringCellValue(), "价格经委托解析");
|
||||
assertEquals("无风险", data.getCell(9).getStringCellValue(), "标题维度经委托");
|
||||
assertEquals("无风险", data.getCell(10).getStringCellValue(), "外观维度经委托");
|
||||
assertEquals("已侵权", data.getCell(11).getStringCellValue());
|
||||
assertEquals("成功", data.getCell(12).getStringCellValue(), "状态经委托 resolveResultStatus");
|
||||
assertNotNull(wb.getSheet("原因"), "原因 sheet 由 SheetBuilder 产出");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_row_token_matching_preserved_in_delegated_builder() throws Exception {
|
||||
// 关键行为:resultMap 键为 rowToken(service rowKey(row) 优先 token),
|
||||
// 委托查找函数必须保持 rowToken 优先匹配(SheetBuilder 默认 legacy-only,门面必须传入自己的 findResultRow)
|
||||
AppearancePatentParsedRowVo row = parsedRow("2_1", "B01A", "US", "f1.xlsx");
|
||||
AppearancePatentResultRowDto resultRow = resultRow("2_1", "B01A", "US", "f1.xlsx::row::2");
|
||||
// 注意:displayId/asin/country 与 resultRow 相同,若用 legacy key 也能匹配;
|
||||
// 构造一个 legacy 不同但 rowToken 相同的用例验证 token 优先
|
||||
row.setDisplayId("x_other");
|
||||
row.setSourceId("x_other");
|
||||
row.setAsin("B999");
|
||||
row.setCountry("FR");
|
||||
File out = new File("target/appearance-patent-delegation-tmp", "token.xlsx");
|
||||
if (out.exists()) {
|
||||
out.delete();
|
||||
}
|
||||
|
||||
invokeWriteResultWorkbook(out, row, resultRow);
|
||||
|
||||
try (XSSFWorkbook wb = new XSSFWorkbook(out)) {
|
||||
Row data = wb.getSheet("外观专利检测结果").getRow(1);
|
||||
assertEquals("已侵权", data.getCell(11).getStringCellValue(),
|
||||
"rowToken 命中 resultRow(legacy 键不匹配仍能解析结论)");
|
||||
assertEquals("成功", data.getCell(12).getStringCellValue());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_delegation_without_result_rows_uses_parsed_fallback() throws Exception {
|
||||
AppearancePatentParsedRowVo row = parsedRow("2_1", "B01A", "US", "f1.xlsx");
|
||||
File out = new File("target/appearance-patent-delegation-tmp", "nofallback.xlsx");
|
||||
if (out.exists()) {
|
||||
out.delete();
|
||||
}
|
||||
|
||||
invokeWriteResultWorkbook(out, row, null);
|
||||
|
||||
try (XSSFWorkbook wb = new XSSFWorkbook(out)) {
|
||||
Row data = wb.getSheet("外观专利检测结果").getRow(1);
|
||||
assertEquals("", data.getCell(9).getStringCellValue(), "无结果行 LLM 列留空");
|
||||
assertEquals("", data.getCell(12).getStringCellValue(), "无结果行状态留空");
|
||||
assertEquals("brand-A", data.getCell(4).getStringCellValue(), "品牌仍从解析行 values 回退");
|
||||
assertEquals("", data.getCell(5).getStringCellValue(), "无结果行价格留空(resolvePrice 仅读结果行,与现状一致)");
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- 3 结果一致(多行 + 原因 sheet 去重) ----------
|
||||
|
||||
@Test
|
||||
void test_multiple_rows_and_reason_dedup() throws Exception {
|
||||
AppearancePatentParsedRowVo p1 = parsedRow("2_1", "B01A", "US", "f1.xlsx");
|
||||
AppearancePatentParsedRowVo p2 = parsedRow("2_2", "B01A", "DE", "f1.xlsx");
|
||||
p2.setRowToken("f1.xlsx::row::3");
|
||||
p2.setRowIndex(3);
|
||||
AppearancePatentResultRowDto r1 = resultRow("2_1", "B01A", "US", "f1.xlsx::row::2");
|
||||
r1.setAppearanceReason("外观理由");
|
||||
r1.setPatentReason("专利理由");
|
||||
r1.setTitleReason("标题理由");
|
||||
File out = new File("target/appearance-patent-delegation-tmp", "multi.xlsx");
|
||||
if (out.exists()) {
|
||||
out.delete();
|
||||
}
|
||||
|
||||
invokeWriteResultWorkbook(out, List.of(p1, p2), Map.of(tokenKey(r1), r1));
|
||||
|
||||
try (XSSFWorkbook wb = new XSSFWorkbook(out)) {
|
||||
Sheet main = wb.getSheet("外观专利检测结果");
|
||||
assertEquals(2, main.getLastRowNum(), "两行数据");
|
||||
Sheet reason = wb.getSheet("原因");
|
||||
assertEquals("B01A", reason.getRow(1).getCell(0).getStringCellValue());
|
||||
assertEquals("外观理由", reason.getRow(1).getCell(1).getStringCellValue());
|
||||
assertEquals("专利理由", reason.getRow(1).getCell(2).getStringCellValue());
|
||||
assertEquals("标题理由", reason.getRow(1).getCell(3).getStringCellValue());
|
||||
assertNull(reason.getRow(2), "同 ASIN 去重");
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- 4 异常一致 ----------
|
||||
|
||||
@Test
|
||||
void test_write_exception_wrapped_with_original_message() throws Exception {
|
||||
// 写出目标为已存在目录 → FileOutputStream 抛异常 → 门面包成"生成外观专利检测结果失败"
|
||||
File badDir = new File("target/appearance-patent-delegation-tmp", "bad-dir");
|
||||
if (!badDir.exists()) {
|
||||
badDir.mkdirs();
|
||||
}
|
||||
AppearancePatentParsedRowVo row = parsedRow("2_1", "B01A", "US", "f1.xlsx");
|
||||
|
||||
Throwable thrown = assertThrows(Throwable.class,
|
||||
() -> invokeWriteResultWorkbook(badDir, row, null));
|
||||
Throwable cause = thrown instanceof java.lang.reflect.InvocationTargetException
|
||||
? ((java.lang.reflect.InvocationTargetException) thrown).getCause()
|
||||
: thrown;
|
||||
assertTrue(cause instanceof BusinessException, "实际异常类型: " + cause.getClass());
|
||||
assertEquals("生成外观专利检测结果失败", cause.getMessage(), "异常包装一致");
|
||||
}
|
||||
|
||||
// ---------- 5 双事务路径不动 ----------
|
||||
|
||||
@Test
|
||||
void test_submit_result_dual_transaction_kept() throws Exception {
|
||||
// transactionManager 非空 → submitResultLocked 走 REQUIRES_NEW 双事务(persist + complete)
|
||||
FileTaskEntity t = runningTask();
|
||||
when(fileTaskMapper.selectById(90001L)).thenReturn(t);
|
||||
when(taskChunkMapper.selectOne(any())).thenReturn(null);
|
||||
when(taskScopeStateMapper.selectOne(any())).thenReturn(null);
|
||||
when(taskChunkMapper.selectCount(any())).thenReturn(1L);
|
||||
when(instanceMetadata.getInstanceId()).thenReturn("server-110");
|
||||
when(transientPayloadStorageService.storeChunkPayload(anyString(), any(), any(), any(), anyString()))
|
||||
.thenReturn("transient:stored");
|
||||
// 提交完成后的调度查询链
|
||||
when(taskChunkMapper.selectList(any())).thenReturn(List.of());
|
||||
when(fileResultMapper.selectList(any())).thenReturn(List.of());
|
||||
|
||||
service.submitResult(90001L, submitRequest("submission-x"));
|
||||
|
||||
verify(taskChunkMapper, Mockito.atLeast(1)).insert(any(TaskChunkEntity.class));
|
||||
verify(transactionManager, Mockito.atLeast(2)).getTransaction(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_submit_result_keeps_task_status_check() throws Exception {
|
||||
// 非 RUNNING 任务 → 双事务路径内 persist 仍抛"任务不是运行中状态"
|
||||
FileTaskEntity t = task("{\"allItems\":[]}");
|
||||
t.setStatus("SUCCESS");
|
||||
when(fileTaskMapper.selectById(90001L)).thenReturn(t);
|
||||
|
||||
BusinessException ex = assertThrows(BusinessException.class,
|
||||
() -> service.submitResult(90001L, submitRequest("submission-status")));
|
||||
assertTrue(ex.getMessage().contains("任务不是运行中状态"), "实际: " + ex.getMessage());
|
||||
}
|
||||
|
||||
// ---------- 6 owner 检查不动 ----------
|
||||
|
||||
@Test
|
||||
void test_owner_check_rejects_foreign_owner() throws Exception {
|
||||
service = serviceWithoutTransactionManager();
|
||||
FileTaskEntity t = task("{\"allItems\":[],\"ownerInstanceId\":\"server-121\"}");
|
||||
t.setStatus("RUNNING");
|
||||
when(fileTaskMapper.selectById(90001L)).thenReturn(t);
|
||||
when(instanceMetadata.getInstanceId()).thenReturn("server-110");
|
||||
|
||||
assertThrows(com.nanri.aiimage.common.exception.TaskOwnerMismatchException.class,
|
||||
() -> service.submitResult(90001L, submitRequest("submission-owner")), "owner 检查仍在手动路径生效");
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_owner_check_passes_for_matching_owner() throws Exception {
|
||||
service = serviceWithoutTransactionManager();
|
||||
FileTaskEntity t = task("{\"allItems\":[],\"ownerInstanceId\":\"server-110\"}");
|
||||
t.setStatus("RUNNING");
|
||||
when(fileTaskMapper.selectById(90001L)).thenReturn(t);
|
||||
when(instanceMetadata.getInstanceId()).thenReturn("server-110");
|
||||
when(taskChunkMapper.selectOne(any())).thenReturn(null);
|
||||
when(taskScopeStateMapper.selectOne(any())).thenReturn(null);
|
||||
when(taskChunkMapper.selectCount(any())).thenReturn(1L);
|
||||
when(transientPayloadStorageService.storeChunkPayload(anyString(), any(), any(), any(), anyString()))
|
||||
.thenReturn("transient:stored");
|
||||
|
||||
service.submitResult(90001L, submitRequest("submission-owner-ok"));
|
||||
|
||||
verify(taskChunkMapper, Mockito.atLeast(1)).insert(any(TaskChunkEntity.class));
|
||||
verify(fileTaskMapper, Mockito.atLeast(1)).updateById(any(FileTaskEntity.class));
|
||||
}
|
||||
|
||||
// ---------- 7 行值解析语义由 SheetBuilder 承接 ----------
|
||||
|
||||
@Test
|
||||
void test_resolve_helpers_semantics_kept_in_sheet_builder() {
|
||||
AppearancePatentParsedRowVo parsedRow = new AppearancePatentParsedRowVo();
|
||||
Map<String, String> values = new LinkedHashMap<>();
|
||||
values.put("品牌", "Source Brand");
|
||||
parsedRow.setValues(values);
|
||||
|
||||
AppearancePatentResultRowDto resultRow = new AppearancePatentResultRowDto();
|
||||
resultRow.setBrand("Python Brand");
|
||||
assertEquals("Python Brand", AppearancePatentSheetBuilder.resolveBrand(resultRow, parsedRow));
|
||||
resultRow.setBrand(" ");
|
||||
assertEquals("Source Brand", AppearancePatentSheetBuilder.resolveBrand(resultRow, parsedRow));
|
||||
assertEquals("", AppearancePatentSheetBuilder.resolvePrice(null));
|
||||
assertEquals("失败", AppearancePatentSheetBuilder.resolveResultStatus(" "));
|
||||
assertEquals("成功", AppearancePatentSheetBuilder.resolveResultStatus("已侵权"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_llm_failure_semantics_kept_in_sheet_builder() {
|
||||
assertTrue(AppearancePatentSheetBuilder.isTechnicalLlmFailure("coze 工作流节点执行超限"));
|
||||
assertFalse(AppearancePatentSheetBuilder.isTechnicalLlmFailure("无风险"));
|
||||
AppearancePatentResultRowDto row = new AppearancePatentResultRowDto();
|
||||
row.setError("coze 调用超时");
|
||||
row.setTitleRisk("coze 调用超时");
|
||||
row.setConclusion("");
|
||||
assertEquals("coze 调用超时", AppearancePatentSheetBuilder.userFacingLlmCellValue(row, row.getTitleRisk()));
|
||||
assertEquals("成功", AppearancePatentSheetBuilder.userFacingStatus(row));
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_normalizer_semantics_unchanged() {
|
||||
assertEquals("", AppearancePatentRowNormalizer.normalize(null));
|
||||
assertEquals("b01x", AppearancePatentRowNormalizer.normalize("b01x "));
|
||||
assertEquals("A", AppearancePatentRowNormalizer.firstNonBlank(null, "A"));
|
||||
}
|
||||
|
||||
// ---------- 私有门面反射调用 ----------
|
||||
|
||||
private void invokeWriteResultWorkbook(File out, AppearancePatentParsedRowVo row,
|
||||
AppearancePatentResultRowDto resultRow) throws Exception {
|
||||
invokeWriteResultWorkbook(out, row == null ? List.of() : List.of(row),
|
||||
resultRow == null ? Map.of() : Map.of(tokenKey(resultRow), resultRow));
|
||||
}
|
||||
|
||||
private void invokeWriteResultWorkbook(File out, List<AppearancePatentParsedRowVo> rows,
|
||||
Map<String, AppearancePatentResultRowDto> resultMap) throws Exception {
|
||||
File parent = out.getParentFile();
|
||||
if (parent != null && !parent.exists()) {
|
||||
parent.mkdirs();
|
||||
}
|
||||
AppearancePatentParsedPayloadDto payload = new AppearancePatentParsedPayloadDto();
|
||||
payload.setAllItems(rows);
|
||||
Method method = AppearancePatentTaskService.class.getDeclaredMethod(
|
||||
"writeResultWorkbook", File.class, AppearancePatentParsedPayloadDto.class, List.class, Map.class);
|
||||
method.setAccessible(true);
|
||||
method.invoke(service, out, payload, rows, resultMap);
|
||||
}
|
||||
|
||||
private static String tokenKey(AppearancePatentResultRowDto row) {
|
||||
return AppearancePatentRowNormalizer.normalize(row.getRowToken());
|
||||
}
|
||||
|
||||
private static FileTaskEntity runningTask() {
|
||||
FileTaskEntity t = task("{\"allItems\":[]}");
|
||||
t.setStatus("RUNNING");
|
||||
return t;
|
||||
}
|
||||
|
||||
private static FileTaskEntity task(String resultJson) {
|
||||
FileTaskEntity t = new FileTaskEntity();
|
||||
t.setId(90001L);
|
||||
t.setModuleType("APPEARANCE_PATENT");
|
||||
t.setResultJson(resultJson);
|
||||
t.setUserId(7L);
|
||||
t.setUpdatedAt(java.time.LocalDateTime.now());
|
||||
return t;
|
||||
}
|
||||
|
||||
private static AppearancePatentSubmitResultRequest submitRequest(String submissionId) {
|
||||
AppearancePatentSubmitResultRequest request = new AppearancePatentSubmitResultRequest();
|
||||
request.setChunkIndex(0);
|
||||
request.setChunkTotal(1);
|
||||
request.setSubmissionId(submissionId);
|
||||
request.setItems(List.of());
|
||||
return request;
|
||||
}
|
||||
|
||||
private static AppearancePatentParsedRowVo parsedRow(String id, String asin, String country, String fileKey) {
|
||||
AppearancePatentParsedRowVo row = new AppearancePatentParsedRowVo();
|
||||
row.setDisplayId(id);
|
||||
row.setSourceId(id);
|
||||
row.setAsin(asin);
|
||||
row.setCountry(country);
|
||||
row.setSourceFileKey(fileKey);
|
||||
row.setRowToken(fileKey + "::row::" + 2);
|
||||
row.setRowIndex(2);
|
||||
row.setSourceFilename("delegation-source.xlsx");
|
||||
Map<String, String> values = new LinkedHashMap<>();
|
||||
values.put("卖家名称", "seller-A");
|
||||
values.put("品牌", "brand-A");
|
||||
values.put("价格", "19.90");
|
||||
row.setValues(values);
|
||||
return row;
|
||||
}
|
||||
|
||||
private static AppearancePatentResultRowDto resultRow(String id, String asin, String country, String rowToken) {
|
||||
AppearancePatentResultRowDto row = new AppearancePatentResultRowDto();
|
||||
row.setId(id);
|
||||
row.setAsin(asin);
|
||||
row.setCountry(country);
|
||||
row.setRowToken(rowToken);
|
||||
row.setTitleRisk("无风险");
|
||||
row.setAppearanceRisk("无风险");
|
||||
row.setConclusion("已侵权");
|
||||
row.setStatus("成功");
|
||||
row.setBrand("brand-A");
|
||||
row.setPrice("19.90");
|
||||
return row;
|
||||
}
|
||||
}
|
||||
+370
@@ -0,0 +1,370 @@
|
||||
package com.nanri.aiimage.modules.appearancepatent.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.MybatisConfiguration;
|
||||
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
||||
import com.nanri.aiimage.common.service.DistributedJobLockService;
|
||||
import com.nanri.aiimage.config.AppearancePatentProperties;
|
||||
import com.nanri.aiimage.config.InstanceMetadata;
|
||||
import com.nanri.aiimage.config.StorageProperties;
|
||||
import com.nanri.aiimage.modules.appearancepatent.client.AppearancePatentLlmClient;
|
||||
import com.nanri.aiimage.modules.appearancepatent.model.vo.AppearancePatentHistoryItemVo;
|
||||
import com.nanri.aiimage.modules.appearancepatent.model.vo.AppearancePatentHistoryVo;
|
||||
import com.nanri.aiimage.modules.appearancepatent.service.AppearancePatentTaskCacheService;
|
||||
import com.nanri.aiimage.modules.file.service.LocalFileStorageService;
|
||||
import com.nanri.aiimage.modules.task.mapper.FileResultMapper;
|
||||
import com.nanri.aiimage.modules.task.mapper.FileTaskMapper;
|
||||
import com.nanri.aiimage.modules.task.mapper.TaskChunkMapper;
|
||||
import com.nanri.aiimage.modules.task.mapper.TaskScopeStateMapper;
|
||||
import com.nanri.aiimage.modules.task.model.entity.FileResultEntity;
|
||||
import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity;
|
||||
import com.nanri.aiimage.modules.task.model.entity.TaskFileJobEntity;
|
||||
import com.nanri.aiimage.modules.task.model.entity.TaskScopeStateEntity;
|
||||
import com.nanri.aiimage.modules.task.model.entity.TaskChunkEntity;
|
||||
import com.nanri.aiimage.modules.task.service.TaskDistributedLockService;
|
||||
import com.nanri.aiimage.modules.task.service.TaskFileJobService;
|
||||
import com.nanri.aiimage.modules.task.service.TaskProgressLightAssembler;
|
||||
import com.nanri.aiimage.modules.task.service.TaskProgressSnapshotService;
|
||||
import com.nanri.aiimage.modules.task.service.TransientPayloadStorageService;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.apache.ibatis.builder.MapperBuilderAssistant;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
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.anyList;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.lenient;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.times;
|
||||
|
||||
/**
|
||||
* 任务 106:appearancepatent 历史列表批量加载(同 105 模式)。
|
||||
* history() 的任务/结果/Job 关联数据走 IN 批量查询 + 按 ID Map 装配,
|
||||
* 不允许逐条 N+1:结果 1 次、任务 1 次、Job 1 次(恒定 3 次查询,与结果行数无关);
|
||||
* 排序(priority → activityTime → createdAt → id)、分页、过滤、输出与现状一致。
|
||||
*/
|
||||
class AppearancePatentTaskServiceHistoryBatchTest {
|
||||
|
||||
private static final String MODULE = "APPEARANCE_PATENT";
|
||||
|
||||
private final List<FileResultEntity> resultDb = new ArrayList<>();
|
||||
private final List<FileTaskEntity> taskDb = new ArrayList<>();
|
||||
private final List<TaskFileJobEntity> jobDb = new ArrayList<>();
|
||||
|
||||
private final AtomicInteger resultSelectCount = new AtomicInteger();
|
||||
private final AtomicInteger taskSelectCount = new AtomicInteger();
|
||||
private final AtomicInteger jobSelectCount = new AtomicInteger();
|
||||
private final List<Long> lastResultTaskIds = new ArrayList<>();
|
||||
|
||||
private FileTaskMapper fileTaskMapper;
|
||||
private FileResultMapper fileResultMapper;
|
||||
private TaskScopeStateMapper taskScopeStateMapper;
|
||||
private TaskChunkMapper taskChunkMapper;
|
||||
private LocalFileStorageService localFileStorageService;
|
||||
private AppearancePatentLlmClient llmClient;
|
||||
private AppearancePatentTaskCacheService taskCacheService;
|
||||
private AppearancePatentProperties properties;
|
||||
private StorageProperties storageProperties;
|
||||
private TaskFileJobService taskFileJobService;
|
||||
private TaskProgressSnapshotService taskProgressSnapshotService;
|
||||
private TransientPayloadStorageService transientPayloadStorageService;
|
||||
private PlatformTransactionManager transactionManager;
|
||||
private DistributedJobLockService distributedJobLockService;
|
||||
private TaskDistributedLockService taskDistributedLockService;
|
||||
private InstanceMetadata instanceMetadata;
|
||||
|
||||
private AppearancePatentTaskService service;
|
||||
|
||||
/** 从 wrapper SQL 片段解析所有 #{ew.paramNameValuePairs.<key>} 引用的参数值。 */
|
||||
private static List<Object> paramValuesOf(LambdaQueryWrapper<?> q, String segment) {
|
||||
List<Object> values = new ArrayList<>();
|
||||
java.util.regex.Matcher m = java.util.regex.Pattern
|
||||
.compile("#\\{ew\\.paramNameValuePairs\\.(\\w+)}").matcher(segment);
|
||||
Map<String, Object> params = q.getParamNameValuePairs();
|
||||
while (m.find()) {
|
||||
Object value = params.get(m.group(1));
|
||||
if (value != null) {
|
||||
values.add(value);
|
||||
}
|
||||
}
|
||||
return values;
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void setUp() throws Exception {
|
||||
TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""), FileResultEntity.class);
|
||||
TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""), FileTaskEntity.class);
|
||||
TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""), TaskScopeStateEntity.class);
|
||||
TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""), TaskFileJobEntity.class);
|
||||
TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""), TaskChunkEntity.class);
|
||||
|
||||
fileTaskMapper = mock(FileTaskMapper.class);
|
||||
fileResultMapper = mock(FileResultMapper.class);
|
||||
taskScopeStateMapper = mock(TaskScopeStateMapper.class);
|
||||
taskChunkMapper = mock(TaskChunkMapper.class);
|
||||
localFileStorageService = mock(LocalFileStorageService.class);
|
||||
llmClient = mock(AppearancePatentLlmClient.class);
|
||||
taskCacheService = mock(AppearancePatentTaskCacheService.class);
|
||||
properties = mock(AppearancePatentProperties.class);
|
||||
storageProperties = mock(StorageProperties.class);
|
||||
taskFileJobService = mock(TaskFileJobService.class);
|
||||
taskProgressSnapshotService = mock(TaskProgressSnapshotService.class);
|
||||
transientPayloadStorageService = mock(TransientPayloadStorageService.class);
|
||||
transactionManager = mock(PlatformTransactionManager.class);
|
||||
distributedJobLockService = mock(DistributedJobLockService.class);
|
||||
taskDistributedLockService = mock(TaskDistributedLockService.class);
|
||||
instanceMetadata = mock(InstanceMetadata.class);
|
||||
|
||||
// 结果表:按 wrapper 的 user/module/limit 过滤并保持 createdAt 倒序
|
||||
doAnswer(invocation -> {
|
||||
resultSelectCount.incrementAndGet();
|
||||
@SuppressWarnings("unchecked")
|
||||
LambdaQueryWrapper<FileResultEntity> q = invocation.getArgument(0);
|
||||
String segment = q.getSqlSegment() == null ? "" : q.getSqlSegment();
|
||||
List<Object> params = paramValuesOf(q, segment);
|
||||
List<FileResultEntity> filtered = new ArrayList<>(resultDb);
|
||||
Long userId = params.stream().filter(Long.class::isInstance).map(Long.class::cast).findFirst().orElse(null);
|
||||
if (userId != null) {
|
||||
filtered.removeIf(r -> !userId.equals(r.getUserId()));
|
||||
}
|
||||
if (segment.contains("module_type")) {
|
||||
filtered.removeIf(r -> !MODULE.equals(r.getModuleType()));
|
||||
}
|
||||
filtered.sort((a, b) -> b.getCreatedAt().compareTo(a.getCreatedAt()));
|
||||
lastResultTaskIds.clear();
|
||||
lastResultTaskIds.addAll(filtered.stream().map(FileResultEntity::getTaskId).toList());
|
||||
if (segment.contains("limit")) {
|
||||
int cap = 50;
|
||||
java.util.regex.Matcher m = java.util.regex.Pattern
|
||||
.compile("limit\\s+(\\d+)", java.util.regex.Pattern.CASE_INSENSITIVE).matcher(segment);
|
||||
if (m.find()) {
|
||||
cap = Integer.parseInt(m.group(1));
|
||||
}
|
||||
return filtered.subList(0, Math.min(cap, filtered.size()));
|
||||
}
|
||||
return filtered;
|
||||
}).when(fileResultMapper).selectList(any());
|
||||
|
||||
// 任务表:IN 查询返回 id 命中集合
|
||||
doAnswer(invocation -> {
|
||||
taskSelectCount.incrementAndGet();
|
||||
@SuppressWarnings("unchecked")
|
||||
LambdaQueryWrapper<FileTaskEntity> q = invocation.getArgument(0);
|
||||
String segment = q.getSqlSegment() == null ? "" : q.getSqlSegment();
|
||||
List<Object> params = paramValuesOf(q, segment);
|
||||
List<Long> wanted = new ArrayList<>();
|
||||
for (Object value : params) {
|
||||
if (value instanceof List<?> list) {
|
||||
for (Object item : list) {
|
||||
if (item instanceof Long id) {
|
||||
wanted.add(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (wanted.isEmpty()) {
|
||||
wanted.addAll(lastResultTaskIds);
|
||||
}
|
||||
if (wanted.isEmpty()) {
|
||||
return List.of();
|
||||
}
|
||||
return taskDb.stream().filter(t -> wanted.contains(t.getId())).toList();
|
||||
}).when(fileTaskMapper).selectList(any());
|
||||
|
||||
lenient().when(taskFileJobService.findAssembleJobsByResultIds(eq(MODULE), anyList())).thenAnswer(invocation -> {
|
||||
jobSelectCount.incrementAndGet();
|
||||
List<Long> resultIds = invocation.getArgument(1);
|
||||
Map<Long, TaskFileJobEntity> map = new HashMap<>();
|
||||
if (resultIds != null) {
|
||||
for (TaskFileJobEntity job : jobDb) {
|
||||
if (job.getResultId() != null && resultIds.contains(job.getResultId())) {
|
||||
map.put(job.getResultId(), job);
|
||||
}
|
||||
}
|
||||
}
|
||||
return map;
|
||||
});
|
||||
|
||||
lenient().when(taskChunkMapper.selectList(any())).thenReturn(List.of());
|
||||
lenient().when(taskScopeStateMapper.selectList(any())).thenReturn(List.of());
|
||||
lenient().when(taskScopeStateMapper.selectCount(any())).thenReturn(0L);
|
||||
lenient().when(taskProgressSnapshotService.find(any(), any())).thenReturn(null);
|
||||
|
||||
service = new AppearancePatentTaskService(
|
||||
localFileStorageService, null, storageProperties, fileTaskMapper, fileResultMapper,
|
||||
taskScopeStateMapper, taskChunkMapper, new ObjectMapper(), llmClient, taskCacheService,
|
||||
properties, taskFileJobService, taskProgressSnapshotService,
|
||||
transientPayloadStorageService, transactionManager, distributedJobLockService,
|
||||
taskDistributedLockService, instanceMetadata,
|
||||
mock(TaskProgressLightAssembler.class));
|
||||
}
|
||||
|
||||
private static FileResultEntity result(Long id, Long taskId, Long userId, LocalDateTime createdAt) {
|
||||
FileResultEntity row = new FileResultEntity();
|
||||
row.setId(id);
|
||||
row.setTaskId(taskId);
|
||||
row.setUserId(userId);
|
||||
row.setModuleType(MODULE);
|
||||
row.setSourceFilename("s" + id + ".xlsx");
|
||||
row.setResultFilename("s" + id + "-result.xlsx");
|
||||
row.setResultFileUrl("result/appearance-patent/" + id + "/out.xlsx");
|
||||
row.setSuccess(1);
|
||||
row.setRowCount(3);
|
||||
row.setCreatedAt(createdAt);
|
||||
return row;
|
||||
}
|
||||
|
||||
private static FileTaskEntity task(Long id, String status, LocalDateTime createdAt) {
|
||||
FileTaskEntity task = new FileTaskEntity();
|
||||
task.setId(id);
|
||||
task.setModuleType(MODULE);
|
||||
task.setStatus(status);
|
||||
task.setCreatedAt(createdAt);
|
||||
task.setUpdatedAt(createdAt);
|
||||
task.setFinishedAt(createdAt.plusMinutes(5));
|
||||
return task;
|
||||
}
|
||||
|
||||
private static TaskFileJobEntity job(Long id, Long resultId, String status) {
|
||||
TaskFileJobEntity job = new TaskFileJobEntity();
|
||||
job.setId(id);
|
||||
job.setResultId(resultId);
|
||||
job.setModuleType(MODULE);
|
||||
job.setStatus(status);
|
||||
return job;
|
||||
}
|
||||
|
||||
private void seed(int rows, int tasks, int jobs) {
|
||||
for (int i = 1; i <= tasks; i++) {
|
||||
taskDb.add(task(100L + i, "SUCCESS", LocalDateTime.of(2026, 8, 1, 9, i)));
|
||||
}
|
||||
for (int i = 1; i <= rows; i++) {
|
||||
resultDb.add(result(200L + i, 100L + (i % tasks) + 1, 1L, LocalDateTime.of(2026, 8, 1, 10, i)));
|
||||
}
|
||||
for (int i = 1; i <= jobs; i++) {
|
||||
jobDb.add(job(300L + i, 200L + i, "SUCCESS"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void historyBatchLoads() throws Exception {
|
||||
seed(5, 3, 3);
|
||||
AppearancePatentHistoryVo vo = service.history(1L, 50);
|
||||
assertEquals(5, vo.getItems().size(), "5 条结果全部装配");
|
||||
for (AppearancePatentHistoryItemVo item : vo.getItems()) {
|
||||
assertNotNull(item.getTaskStatus(), "task 状态从批量任务 Map 装配");
|
||||
assertEquals("SUCCESS", item.getFileStatus(), "job 状态从批量 Job Map 装配");
|
||||
assertTrue(item.getFileReady(), "结果文件就绪");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void historySingleRow() throws Exception {
|
||||
seed(1, 1, 1);
|
||||
AppearancePatentHistoryVo vo = service.history(1L, 50);
|
||||
assertEquals(1, vo.getItems().size());
|
||||
assertEquals(201L, vo.getItems().getFirst().getResultId());
|
||||
assertEquals("SUCCESS", vo.getItems().getFirst().getTaskStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void historyMultipleTasks() throws Exception {
|
||||
seed(4, 4, 4);
|
||||
AppearancePatentHistoryVo vo = service.history(1L, 50);
|
||||
assertEquals(4, vo.getItems().size());
|
||||
long distinctTasks = vo.getItems().stream().map(AppearancePatentHistoryItemVo::getTaskId).distinct().count();
|
||||
assertEquals(4, distinctTasks, "多个任务分别按 ID Map 装配");
|
||||
}
|
||||
|
||||
@Test
|
||||
void historyNoJobs() throws Exception {
|
||||
seed(3, 3, 0);
|
||||
AppearancePatentHistoryVo vo = service.history(1L, 50);
|
||||
assertEquals(3, vo.getItems().size());
|
||||
for (AppearancePatentHistoryItemVo item : vo.getItems()) {
|
||||
assertNull(item.getFileJobId(), "无 Job 时不附加 jobId");
|
||||
assertEquals("SUCCESS", item.getFileStatus(), "文件就绪无 Job 状态为 SUCCESS");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void historyOrderUnchanged() throws Exception {
|
||||
seed(3, 1, 0);
|
||||
AppearancePatentHistoryVo vo = service.history(1L, 50);
|
||||
assertEquals(List.of(203L, 202L, 201L),
|
||||
vo.getItems().stream().map(AppearancePatentHistoryItemVo::getResultId).toList(),
|
||||
"createdAt 倒序保持");
|
||||
}
|
||||
|
||||
@Test
|
||||
void historyPagination() throws Exception {
|
||||
seed(12, 1, 0);
|
||||
AppearancePatentHistoryVo vo = service.history(1L, 5);
|
||||
assertEquals(5, vo.getItems().size(), "limit 5 只取前 5");
|
||||
assertEquals(List.of(212L, 211L, 210L, 209L, 208L),
|
||||
vo.getItems().stream().map(AppearancePatentHistoryItemVo::getResultId).toList());
|
||||
}
|
||||
|
||||
@Test
|
||||
void historyMissingRelated() throws Exception {
|
||||
resultDb.add(result(201L, 9999L, 1L, LocalDateTime.of(2026, 8, 1, 10, 1)));
|
||||
resultDb.add(result(202L, 1001L, 1L, LocalDateTime.of(2026, 8, 1, 10, 0)));
|
||||
taskDb.add(task(1001L, "SUCCESS", LocalDateTime.of(2026, 8, 1, 9, 0)));
|
||||
AppearancePatentHistoryVo vo = service.history(1L, 50);
|
||||
assertEquals(2, vo.getItems().size());
|
||||
assertNull(vo.getItems().getFirst().getTaskStatus(), "关联任务缺失时状态为 null");
|
||||
assertEquals("SUCCESS", vo.getItems().get(1).getTaskStatus(), "有关联任务的正常装配");
|
||||
}
|
||||
|
||||
@Test
|
||||
void historyActiveRunningPrioritized() throws Exception {
|
||||
// 结果 201 关联 RUNNING 任务(活跃优先),202 关联 SUCCESS 任务(已完成)
|
||||
resultDb.add(result(201L, 1001L, 1L, LocalDateTime.of(2026, 8, 1, 10, 0)));
|
||||
resultDb.add(result(202L, 1002L, 1L, LocalDateTime.of(2026, 8, 1, 10, 1)));
|
||||
taskDb.add(task(1001L, "RUNNING", LocalDateTime.of(2026, 8, 1, 9, 0)));
|
||||
taskDb.add(task(1002L, "SUCCESS", LocalDateTime.of(2026, 8, 1, 9, 1)));
|
||||
AppearancePatentHistoryVo vo = service.history(1L, 50);
|
||||
assertEquals(2, vo.getItems().size());
|
||||
assertEquals(201L, vo.getItems().getFirst().getResultId(), "RUNNING 任务记录排前");
|
||||
assertEquals(202L, vo.getItems().get(1).getResultId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void historyUserFiltered() throws Exception {
|
||||
resultDb.add(result(201L, 1001L, 1L, LocalDateTime.of(2026, 8, 1, 10, 0)));
|
||||
resultDb.add(result(202L, 1002L, 2L, LocalDateTime.of(2026, 8, 1, 10, 1)));
|
||||
taskDb.add(task(1001L, "SUCCESS", LocalDateTime.of(2026, 8, 1, 9, 0)));
|
||||
taskDb.add(task(1002L, "SUCCESS", LocalDateTime.of(2026, 8, 1, 9, 1)));
|
||||
AppearancePatentHistoryVo vo = service.history(2L, 50);
|
||||
assertEquals(1, vo.getItems().size(), "只返回当前用户结果");
|
||||
assertEquals(202L, vo.getItems().getFirst().getResultId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void historySqlCountConstant() throws Exception {
|
||||
seed(20, 4, 20);
|
||||
service.history(1L, 50);
|
||||
verify(fileResultMapper, times(1)).selectList(any());
|
||||
verify(fileTaskMapper, times(1)).selectList(any());
|
||||
verify(taskFileJobService, times(1)).findAssembleJobsByResultIds(eq(MODULE), anyList());
|
||||
assertEquals(1, resultSelectCount.get(), "结果 1 次 IN 查询");
|
||||
assertEquals(1, taskSelectCount.get(), "任务 1 次 IN 查询");
|
||||
assertEquals(1, jobSelectCount.get(), "Job 1 次 IN 查询");
|
||||
assertEquals(20, resultSelectCount.get() * 20, "查询次数与结果行数无关(无 N+1)");
|
||||
}
|
||||
}
|
||||
-41
@@ -1,11 +1,7 @@
|
||||
package com.nanri.aiimage.modules.appearancepatent.service;
|
||||
|
||||
import com.nanri.aiimage.modules.appearancepatent.model.dto.AppearancePatentResultRowDto;
|
||||
import com.nanri.aiimage.modules.appearancepatent.model.vo.AppearancePatentParsedRowVo;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class AppearancePatentTaskServiceTest {
|
||||
@@ -16,41 +12,4 @@ class AppearancePatentTaskServiceTest {
|
||||
assertEquals("SUCCESS", AppearancePatentTaskService.resolveTaskExecutionStatus(false, false));
|
||||
assertEquals("FAILED", AppearancePatentTaskService.resolveTaskExecutionStatus(false, true));
|
||||
}
|
||||
|
||||
@Test
|
||||
void resultStatusFailsOnlyWhenConclusionIsEmpty() {
|
||||
assertEquals("\u5931\u8d25", AppearancePatentTaskService.resolveResultStatus(null));
|
||||
assertEquals("\u5931\u8d25", AppearancePatentTaskService.resolveResultStatus(" "));
|
||||
assertEquals("\u6210\u529f", AppearancePatentTaskService.resolveResultStatus("\u4fb5\u6743"));
|
||||
assertEquals("\u6210\u529f", AppearancePatentTaskService.resolveResultStatus("\u65e0\u4fb5\u6743"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void resultBrandPrefersPythonThenFallsBackToSourceFile() {
|
||||
AppearancePatentParsedRowVo parsedRow = new AppearancePatentParsedRowVo();
|
||||
parsedRow.setValues(new LinkedHashMap<>());
|
||||
parsedRow.getValues().put("品牌", "Source Brand");
|
||||
|
||||
AppearancePatentResultRowDto resultRow = new AppearancePatentResultRowDto();
|
||||
resultRow.setBrand("Python Brand");
|
||||
assertEquals("Python Brand", AppearancePatentTaskService.resolveBrand(resultRow, parsedRow));
|
||||
|
||||
resultRow.setBrand(" ");
|
||||
assertEquals("Source Brand", AppearancePatentTaskService.resolveBrand(resultRow, parsedRow));
|
||||
|
||||
parsedRow.getValues().clear();
|
||||
assertEquals("", AppearancePatentTaskService.resolveBrand(resultRow, parsedRow));
|
||||
}
|
||||
|
||||
@Test
|
||||
void resultPriceUsesOnlyPythonSubmittedValue() {
|
||||
assertEquals("", AppearancePatentTaskService.resolvePrice(null));
|
||||
|
||||
AppearancePatentResultRowDto resultRow = new AppearancePatentResultRowDto();
|
||||
resultRow.setPrice(" 12.99 ");
|
||||
assertEquals("12.99", AppearancePatentTaskService.resolvePrice(resultRow));
|
||||
|
||||
resultRow.setPrice(null);
|
||||
assertEquals("", AppearancePatentTaskService.resolvePrice(resultRow));
|
||||
}
|
||||
}
|
||||
|
||||
+167
@@ -0,0 +1,167 @@
|
||||
package com.nanri.aiimage.modules.appearancepatent.service.support;
|
||||
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import org.apache.poi.ss.usermodel.Row;
|
||||
import org.apache.poi.ss.usermodel.Sheet;
|
||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* 任务 93:AppearancePatentExcelParser 行解析器。
|
||||
* POI 读 Excel 行 → 中间行对象(原始单元格值)。语义与 AppearancePatentTaskService.parseWorkbook
|
||||
* 对应段落一致:cell 归一化(BOM/全角空格/trim/连续空白折叠)、表头别名匹配、空行跳过、
|
||||
* 必填表头缺失抛错。注意:appearancepatent 无 2000 截断、无错误值转空(与 similarasin 不同)。
|
||||
*/
|
||||
class AppearancePatentExcelParserTest {
|
||||
|
||||
private static final String[] REQUIRED = {"id", "asin", "国家"};
|
||||
|
||||
private static File workbook(String[] headers, List<String[]> rows) throws Exception {
|
||||
File file = File.createTempFile("appearance-patent-parse-", ".xlsx");
|
||||
try (XSSFWorkbook wb = new XSSFWorkbook(); FileOutputStream out = new FileOutputStream(file)) {
|
||||
Sheet sheet = wb.createSheet("Sheet1");
|
||||
Row header = sheet.createRow(0);
|
||||
for (int i = 0; i < headers.length; i++) {
|
||||
header.createCell(i).setCellValue(headers[i]);
|
||||
}
|
||||
for (int r = 0; r < rows.size(); r++) {
|
||||
Row row = sheet.createRow(r + 1);
|
||||
for (int c = 0; c < rows.get(r).length; c++) {
|
||||
row.createCell(c).setCellValue(rows.get(r)[c]);
|
||||
}
|
||||
}
|
||||
wb.write(out);
|
||||
}
|
||||
return file;
|
||||
}
|
||||
|
||||
private static final String[] FULL_HEADERS = {"id", "asin", "国家", "价格", "seller sku", "图片链接", "标题"};
|
||||
|
||||
@Test
|
||||
void test_parser_normal_rows() throws Exception {
|
||||
File file = workbook(FULL_HEADERS, List.of(
|
||||
new String[]{"2_1", "b01a", "US", "19.90", "SKU-1", "http://img/a.jpg", "title a"},
|
||||
new String[]{"2_2", "b01b", "DE", "29.90", "SKU-2", "http://img/b.jpg", "title b"}));
|
||||
|
||||
AppearancePatentExcelParser.ParsedSheet parsed = new AppearancePatentExcelParser().parse(file);
|
||||
|
||||
assertEquals(7, parsed.headers().size(), "表头 7 列");
|
||||
assertEquals(2, parsed.rows().size());
|
||||
AppearancePatentExcelParser.AppearanceExcelRow first = parsed.rows().get(0);
|
||||
assertEquals(2, first.rowIndex(), "行号从 2 开始(表头占 1)");
|
||||
assertEquals("2_1", first.id());
|
||||
assertEquals("B01A", first.asin(), "asin 归一化后大写");
|
||||
assertEquals("US", first.country());
|
||||
assertEquals("19.90", first.price());
|
||||
assertEquals("SKU-1", first.sku());
|
||||
assertEquals("http://img/a.jpg", first.url());
|
||||
assertEquals("title a", first.title());
|
||||
assertEquals("19.90", first.values().get("价格"), "values 按表头键取值");
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_parser_blank_row_skipped() throws Exception {
|
||||
File file = workbook(FULL_HEADERS, List.of(
|
||||
new String[]{"", "", ""},
|
||||
new String[]{"1", "B01X", "US"},
|
||||
new String[]{"", "", ""}));
|
||||
|
||||
AppearancePatentExcelParser.ParsedSheet parsed = new AppearancePatentExcelParser().parse(file);
|
||||
|
||||
assertEquals(1, parsed.rows().size(), "全空行跳过");
|
||||
assertEquals("1", parsed.rows().get(0).id());
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_parser_empty_sheet() throws Exception {
|
||||
File file = workbook(REQUIRED, List.of());
|
||||
|
||||
AppearancePatentExcelParser.ParsedSheet parsed = new AppearancePatentExcelParser().parse(file);
|
||||
|
||||
assertTrue(parsed.rows().isEmpty(), "无数据行返回空");
|
||||
assertEquals(3, parsed.headers().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_parser_header_aliases() throws Exception {
|
||||
// 国家用 country、sku 用 merchant_sku、url 用 主图链接 均可识别
|
||||
File file = workbook(new String[]{"id", "asin", "country", "merchant_sku", "主图链接"},
|
||||
List.<String[]>of(new String[]{"1", "B01X", "FR", "MS-1", "http://img/x.jpg"}));
|
||||
|
||||
AppearancePatentExcelParser.ParsedSheet parsed = new AppearancePatentExcelParser().parse(file);
|
||||
|
||||
assertEquals(1, parsed.rows().size());
|
||||
AppearancePatentExcelParser.AppearanceExcelRow row = parsed.rows().get(0);
|
||||
assertEquals("FR", row.country());
|
||||
assertEquals("MS-1", row.sku());
|
||||
assertEquals("http://img/x.jpg", row.url());
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_parser_missing_required_header_throws() throws Exception {
|
||||
File file = workbook(new String[]{"id", "asin"}, List.<String[]>of(new String[]{"1", "B01X"}));
|
||||
|
||||
BusinessException ex = assertThrows(BusinessException.class,
|
||||
() -> new AppearancePatentExcelParser().parse(file));
|
||||
assertTrue(ex.getMessage().contains("缺少必要表头"), "必填表头缺失抛错,实际: " + ex.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_parser_empty_header_row_throws() throws Exception {
|
||||
File file = workbook(new String[]{}, List.of());
|
||||
|
||||
BusinessException ex = assertThrows(BusinessException.class,
|
||||
() -> new AppearancePatentExcelParser().parse(file));
|
||||
assertTrue(ex.getMessage().contains("表头为空") || ex.getMessage().contains("缺少必要表头"),
|
||||
"表头缺失抛错,实际: " + ex.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_parser_cell_normalization() throws Exception {
|
||||
File file = workbook(FULL_HEADERS, List.<String[]>of(
|
||||
new String[]{"2", "b01x", " US ", " 19.90 ", " SKU 1 ", " http://img/x.jpg ", " t a "}));
|
||||
|
||||
AppearancePatentExcelParser.ParsedSheet parsed = new AppearancePatentExcelParser().parse(file);
|
||||
|
||||
AppearancePatentExcelParser.AppearanceExcelRow row = parsed.rows().get(0);
|
||||
assertEquals("2", row.id(), "BOM 剥离");
|
||||
assertEquals("B01X", row.asin(), "大写 + trim");
|
||||
assertEquals("US", row.country(), "trim");
|
||||
assertEquals("SKU 1", row.sku(), "全角空格转半角 + 折叠");
|
||||
assertEquals("http://img/x.jpg", row.url(), "url trim");
|
||||
assertEquals("t a", row.title(), "连续空白折叠");
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_parser_no_2000_truncation() throws Exception {
|
||||
// appearancepatent 无字段截断(与 similarasin 不同):超长字段原样保留
|
||||
String longSku = "S".repeat(5000);
|
||||
File file = workbook(FULL_HEADERS, List.<String[]>of(new String[]{"1", "B01X", "US", "", longSku, "", ""}));
|
||||
|
||||
AppearancePatentExcelParser.ParsedSheet parsed = new AppearancePatentExcelParser().parse(file);
|
||||
|
||||
assertEquals(longSku, parsed.rows().get(0).sku(), "超长字段不截断");
|
||||
assertEquals(5000, parsed.rows().get(0).sku().length());
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_parser_header_fallback_names() throws Exception {
|
||||
// 空表头列名回退为 "列N";重复列取首列
|
||||
File file = workbook(new String[]{"id", "", "asin", "asin", "国家"}, List.<String[]>of(new String[]{"1", "x", "B01A", "B01B", "US"}));
|
||||
|
||||
AppearancePatentExcelParser.ParsedSheet parsed = new AppearancePatentExcelParser().parse(file);
|
||||
|
||||
assertEquals("列2", parsed.headers().get(1), "空表头回退列N");
|
||||
assertEquals("asin", parsed.headers().get(2));
|
||||
assertEquals("asin", parsed.headers().get(3), "重复表头保留");
|
||||
assertEquals("B01A", parsed.rows().get(0).asin(), "重复列取首个匹配列");
|
||||
}
|
||||
}
|
||||
+221
@@ -0,0 +1,221 @@
|
||||
package com.nanri.aiimage.modules.appearancepatent.service.support;
|
||||
|
||||
import com.nanri.aiimage.modules.appearancepatent.model.vo.AppearancePatentHistoryItemVo;
|
||||
import com.nanri.aiimage.modules.task.model.entity.FileResultEntity;
|
||||
import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity;
|
||||
import com.nanri.aiimage.modules.task.model.entity.TaskFileJobEntity;
|
||||
import com.nanri.aiimage.modules.task.model.entity.TaskProgressSnapshotEntity;
|
||||
import com.nanri.aiimage.modules.task.service.TaskProgressSnapshotService;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
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.time.LocalDateTime;
|
||||
|
||||
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.anyLong;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.lenient;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* 任务 97:AppearancePatentHistoryAssembler 历史查询组装器。
|
||||
* 历史列表 VO 拼装(toHistoryItem + 进度链)抽到独立组件;只读不落库;输出与现状一致。
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class AppearancePatentHistoryAssemblerTest {
|
||||
|
||||
@Mock private TaskProgressSnapshotService taskProgressSnapshotService;
|
||||
|
||||
private AppearancePatentHistoryAssembler assembler;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
assembler = new AppearancePatentHistoryAssembler(
|
||||
taskProgressSnapshotService,
|
||||
(current, total, job, baseTime) -> current <= 0 ? 1 : Math.min(99, current * 100 / 2),
|
||||
snapshot -> 42);
|
||||
lenient().when(taskProgressSnapshotService.find(anyLong(), anyString())).thenReturn(null);
|
||||
}
|
||||
|
||||
private static FileResultEntity result(Long id, Long taskId, String source, String resultFileUrl, Integer success) {
|
||||
FileResultEntity row = new FileResultEntity();
|
||||
row.setId(id);
|
||||
row.setTaskId(taskId);
|
||||
row.setSourceFilename(source);
|
||||
row.setResultFilename(source == null ? null : source.replace(".xlsx", "-result.xlsx"));
|
||||
row.setResultFileUrl(resultFileUrl);
|
||||
row.setSuccess(success);
|
||||
row.setErrorMessage(null);
|
||||
row.setRowCount(12);
|
||||
row.setCreatedAt(LocalDateTime.of(2026, 8, 1, 10, 0));
|
||||
return row;
|
||||
}
|
||||
|
||||
private static FileTaskEntity task(Long id, String status, LocalDateTime createdAt) {
|
||||
FileTaskEntity task = new FileTaskEntity();
|
||||
task.setId(id);
|
||||
task.setStatus(status);
|
||||
task.setCreatedAt(createdAt);
|
||||
task.setUpdatedAt(createdAt);
|
||||
task.setFinishedAt(createdAt == null ? null : createdAt.plusMinutes(5));
|
||||
return task;
|
||||
}
|
||||
|
||||
private static TaskFileJobEntity job(Long id, String status) {
|
||||
TaskFileJobEntity job = new TaskFileJobEntity();
|
||||
job.setId(id);
|
||||
job.setStatus(status);
|
||||
job.setErrorMessage(null);
|
||||
job.setUpdatedAt(LocalDateTime.of(2026, 8, 1, 9, 30));
|
||||
return job;
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_assembler_history_items() {
|
||||
AppearancePatentHistoryItemVo item = assembler.toHistoryItem(
|
||||
result(100L, 10L, "a.xlsx", "result/10/a.xlsx", 1),
|
||||
task(10L, "SUCCESS", LocalDateTime.of(2026, 8, 1, 9, 0)),
|
||||
null);
|
||||
|
||||
assertEquals(100L, item.getResultId());
|
||||
assertEquals(10L, item.getTaskId());
|
||||
assertEquals("a.xlsx", item.getSourceFilename());
|
||||
assertEquals("a-result.xlsx", item.getResultFilename());
|
||||
assertNull(item.getDownloadUrl(), "appearancepatent 无下载 URL 生成");
|
||||
assertEquals("SUCCESS", item.getTaskStatus());
|
||||
assertEquals(Boolean.TRUE, item.getSuccess(), "文件 URL 就绪即成功");
|
||||
assertEquals(12, item.getRowCount());
|
||||
assertEquals("2026-08-01T10:00", item.getCreatedAt());
|
||||
assertEquals("2026-08-01T09:00", item.getStartedAt(), "任务开始时间复用 task.createdAt");
|
||||
assertEquals("2026-08-01T09:05", item.getFinishedAt(), "任务结束时间取 task.finishedAt");
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_assembler_row_with_null_fields() {
|
||||
FileResultEntity row = result(100L, 10L, null, null, null);
|
||||
row.setCreatedAt(null);
|
||||
|
||||
AppearancePatentHistoryItemVo item = assembler.toHistoryItem(row, null, null);
|
||||
|
||||
assertNull(item.getSourceFilename(), "空文件名保留");
|
||||
assertEquals(Boolean.FALSE, item.getSuccess(), "无文件 URL 且 success 为空视为失败");
|
||||
assertNull(item.getStartedAt(), "task 与 createdAt 均缺时无开始时间");
|
||||
assertFalse(Boolean.TRUE.equals(item.getFileReady()), "文件未就绪");
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_assembler_file_state() {
|
||||
FileResultEntity row = result(100L, 10L, "a.xlsx", "result/10/a.xlsx", 1);
|
||||
|
||||
AppearancePatentHistoryItemVo item = assembler.toHistoryItem(row, task(10L, "SUCCESS", null), job(7L, "RUNNING"));
|
||||
|
||||
assertTrue(item.getFileReady(), "结果文件就绪");
|
||||
assertEquals(7L, item.getFileJobId());
|
||||
assertEquals("RUNNING", item.getFileStatus());
|
||||
assertEquals(Integer.valueOf(100), item.getFileProgressPercent(), "就绪即 100%");
|
||||
assertEquals("结果文件已生成", item.getFileProgressMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_assembler_order_priority() {
|
||||
// RUNNING 任务优先(priority 0)于 SUCCESS 且文件就绪(priority 1)
|
||||
FileResultEntity pending = result(200L, 2L, "b.xlsx", null, 0);
|
||||
FileResultEntity done = result(100L, 1L, "a.xlsx", "result/1/a.xlsx", 1);
|
||||
|
||||
assertEquals(0, assembler.historyPriority(pending, task(2L, "RUNNING", null), null));
|
||||
assertEquals(1, assembler.historyPriority(done, task(1L, "SUCCESS", null), null));
|
||||
|
||||
// SUCCESS 但文件未生成 → 构建中 → priority 0
|
||||
FileResultEntity building = result(300L, 3L, "c.xlsx", null, 0);
|
||||
assertEquals(0, assembler.historyPriority(building, task(3L, "SUCCESS", null), job(5L, "RUNNING")));
|
||||
|
||||
// 活动时间取 latestTime(row.createdAt 10:00 最晚)
|
||||
assertEquals(LocalDateTime.of(2026, 8, 1, 10, 0),
|
||||
assembler.historyActivityTime(done, task(1L, "SUCCESS", LocalDateTime.of(2026, 8, 1, 9, 0)), job(3L, "RUNNING")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_assembler_file_building() {
|
||||
FileResultEntity row = result(100L, 10L, "a.xlsx", null, 0);
|
||||
assertTrue(assembler.isHistoryFileBuilding(row, "SUCCESS", job(3L, "RUNNING")), "SUCCESS 任务文件未生成视为构建中");
|
||||
assertFalse(assembler.isHistoryFileBuilding(row, "SUCCESS", job(4L, "FAILED")), "job 失败不算构建中");
|
||||
assertFalse(assembler.isHistoryFileBuilding(row, "FAILED", null), "非 SUCCESS 任务不算构建中");
|
||||
row.setResultFileUrl("result/10/a.xlsx");
|
||||
assertFalse(assembler.isHistoryFileBuilding(row, "SUCCESS", null), "文件就绪不算构建中");
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_assembler_null_task() {
|
||||
FileResultEntity row = result(100L, 10L, "a.xlsx", null, 1);
|
||||
row.setCreatedAt(LocalDateTime.of(2026, 8, 1, 11, 0));
|
||||
|
||||
AppearancePatentHistoryItemVo item = assembler.toHistoryItem(row, null, null);
|
||||
|
||||
assertNull(item.getTaskStatus(), "缺 task 状态为空");
|
||||
assertEquals("2026-08-01T11:00", item.getStartedAt(), "缺 task 回退 result.createdAt");
|
||||
assertNull(item.getFinishedAt(), "缺 task 无结束时间");
|
||||
assertNull(item.getFileStatus(), "无 job 且文件未就绪时状态为空");
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_assembler_file_error_attached() {
|
||||
TaskFileJobEntity failedJob = job(9L, "FAILED");
|
||||
failedJob.setErrorMessage("assemble boom");
|
||||
FileResultEntity row = result(100L, 10L, "a.xlsx", null, 0);
|
||||
|
||||
AppearancePatentHistoryItemVo item = assembler.toHistoryItem(row, task(10L, "RUNNING", null), failedJob);
|
||||
|
||||
assertEquals("FAILED", item.getFileStatus(), "job 失败状态附带");
|
||||
assertEquals("assemble boom", item.getFileError(), "job 错误信息附带");
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_assembler_snapshot_progress() {
|
||||
FileResultEntity row = result(100L, 10L, "a.xlsx", null, 0);
|
||||
TaskProgressSnapshotEntity snapshot = new TaskProgressSnapshotEntity();
|
||||
snapshot.setTotalCount(10);
|
||||
snapshot.setSuccessCount(5);
|
||||
snapshot.setMessage("LLM 处理中");
|
||||
when(taskProgressSnapshotService.find(10L, "APPEARANCE_PATENT")).thenReturn(snapshot);
|
||||
|
||||
AppearancePatentHistoryItemVo item = assembler.toHistoryItem(row, task(10L, "RUNNING", null), null);
|
||||
|
||||
assertEquals(Integer.valueOf(5), item.getFileProgressCurrent(), "快照 current 附带");
|
||||
assertEquals(Integer.valueOf(10), item.getFileProgressTotal(), "快照 total 附带");
|
||||
assertEquals(Integer.valueOf(99), item.getFileProgressPercent(), "百分比 = max(注入计算值 99, 注入提取值 42)");
|
||||
assertEquals("LLM 处理中", item.getFileProgressMessage());
|
||||
verify(taskProgressSnapshotService).find(10L, "APPEARANCE_PATENT");
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_assembler_immutable_input() {
|
||||
FileResultEntity row = result(100L, 10L, "a.xlsx", "result/10/a.xlsx", 1);
|
||||
FileTaskEntity t = task(10L, "SUCCESS", LocalDateTime.of(2026, 8, 1, 9, 0));
|
||||
|
||||
assembler.toHistoryItem(row, t, null);
|
||||
|
||||
assertEquals("a.xlsx", row.getSourceFilename(), "result 不被修改");
|
||||
assertEquals("SUCCESS", t.getStatus(), "task 不被修改");
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_assembler_consistency() {
|
||||
FileResultEntity row = result(100L, 10L, "a.xlsx", null, 0);
|
||||
row.setErrorMessage("python timeout");
|
||||
row.setCreatedAt(LocalDateTime.of(2026, 8, 1, 10, 0));
|
||||
|
||||
AppearancePatentHistoryItemVo item = assembler.toHistoryItem(row, task(10L, "FAILED", LocalDateTime.of(2026, 8, 1, 9, 30)), null);
|
||||
|
||||
assertEquals(Boolean.FALSE, item.getSuccess(), "无文件 URL 且 success=0 为失败");
|
||||
assertEquals("python timeout", item.getError());
|
||||
assertEquals("FAILED", item.getTaskStatus());
|
||||
assertEquals(12, item.getRowCount());
|
||||
}
|
||||
}
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
package com.nanri.aiimage.modules.appearancepatent.service.support;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* 任务 93:AppearancePatentRowNormalizer 字段归一化器。
|
||||
* 规则与 AppearancePatentTaskService.normalize / firstNonBlank / baseId / normalizeDisplayId
|
||||
* 现状逐字节一致:BOM 剥离、全角空格转半角、trim、连续空白折叠、firstNonBlank 取首非空。
|
||||
*/
|
||||
class AppearancePatentRowNormalizerTest {
|
||||
|
||||
@Test
|
||||
void test_normalize_null_returns_empty() {
|
||||
assertEquals("", AppearancePatentRowNormalizer.normalize(null), "null 归一化为空串");
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_normalize_bom_stripped() {
|
||||
assertEquals("B01X", AppearancePatentRowNormalizer.normalize("B01X"), "BOM 剥离");
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_normalize_fullwidth_space_to_halfwidth() {
|
||||
assertEquals("SKU 1", AppearancePatentRowNormalizer.normalize("SKU 1"), "全角空格转半角");
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_normalize_trim_and_collapse_whitespace() {
|
||||
assertEquals("a b c", AppearancePatentRowNormalizer.normalize(" a\t b \nc "), "trim + 连续空白折叠为单空格");
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_first_non_blank_prefers_first() {
|
||||
assertEquals("preferred", AppearancePatentRowNormalizer.firstNonBlank("preferred", "fallback"));
|
||||
assertEquals("fallback", AppearancePatentRowNormalizer.firstNonBlank(" ", "fallback"), "首选空白回退");
|
||||
assertEquals("fallback", AppearancePatentRowNormalizer.firstNonBlank(null, "fallback"), "首选 null 回退");
|
||||
assertEquals("kept", AppearancePatentRowNormalizer.firstNonBlank(" kept ", "fallback"), "结果去首尾空白");
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_base_id_splits_underscore_block() {
|
||||
assertEquals("2", AppearancePatentRowNormalizer.baseId("2_1"), "块基 id 取下划线前");
|
||||
assertEquals("3", AppearancePatentRowNormalizer.baseId("3"), "无下划线原样");
|
||||
assertEquals("2", AppearancePatentRowNormalizer.baseId(" 2_1 "), "归一化后再取基 id");
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_normalize_display_id_trims_only() {
|
||||
assertEquals("2_1", AppearancePatentRowNormalizer.normalizeDisplayId(" 2_1 "), "displayId 只 trim 不折叠内部");
|
||||
assertEquals("", AppearancePatentRowNormalizer.normalizeDisplayId(null));
|
||||
assertEquals("", AppearancePatentRowNormalizer.normalizeDisplayId(" "));
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_normalize_keeps_other_fullwidth_punct() {
|
||||
assertEquals("(外观)", AppearancePatentRowNormalizer.normalize(" (外观) "), "非空格全角字符保留");
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_normalize_not_mutating_shared_rules() {
|
||||
String sample = " X Y ";
|
||||
String once = AppearancePatentRowNormalizer.normalize(sample);
|
||||
assertEquals(once, AppearancePatentRowNormalizer.normalize(sample), "幂等");
|
||||
assertNotEquals(sample, once);
|
||||
assertFalse(once.contains(""), "BOM 不残留");
|
||||
assertFalse(once.contains(" "), "全角空格不残留");
|
||||
assertTrue(once.equals("X Y"));
|
||||
}
|
||||
}
|
||||
+227
@@ -0,0 +1,227 @@
|
||||
package com.nanri.aiimage.modules.appearancepatent.service.support;
|
||||
|
||||
import com.nanri.aiimage.modules.appearancepatent.model.vo.AppearancePatentParsedRowVo;
|
||||
import com.nanri.aiimage.modules.appearancepatent.model.dto.AppearancePatentResultRowDto;
|
||||
import org.apache.poi.ss.usermodel.Cell;
|
||||
import org.apache.poi.ss.usermodel.Row;
|
||||
import org.apache.poi.ss.usermodel.Sheet;
|
||||
import org.apache.poi.ss.usermodel.Workbook;
|
||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* 任务 94:AppearancePatentSheetBuilder Sheet 构造器。
|
||||
* 结果 Workbook/Sheet 构造辅助(表头、列顺序、样式、行值派生)。
|
||||
* 与现状 writeResultWorkbook / writeReasonSheet 一致:主 sheet 名"外观专利检测结果"、
|
||||
* 13 列表头(RESULT_HEADERS 10 列 + 第 6/7/8 位插入标题/图片链接/sku)、加粗表头、
|
||||
* 数据行从第 1 行;"原因"sheet 4 列按 ASIN 去重。不落库、无 IO 依赖。
|
||||
*/
|
||||
class AppearancePatentSheetBuilderTest {
|
||||
|
||||
private static final String[] RESULT_HEADERS_13 = {
|
||||
"id", "asin", "国家", "卖家名称", "品牌", "价格",
|
||||
"标题", "图片链接", "sku",
|
||||
"标题维度(商标)", "外观维度(外观设计专利)", "结论", "状态"
|
||||
};
|
||||
|
||||
private static AppearancePatentParsedRowVo parsedRow(String id, String asin, String country,
|
||||
String title, String url, String sku) {
|
||||
AppearancePatentParsedRowVo row = new AppearancePatentParsedRowVo();
|
||||
row.setDisplayId(id);
|
||||
row.setSourceId(id);
|
||||
row.setAsin(asin);
|
||||
row.setCountry(country);
|
||||
row.setTitle(title);
|
||||
row.setUrl(url);
|
||||
row.setSku(sku);
|
||||
Map<String, String> values = new LinkedHashMap<>();
|
||||
values.put("卖家名称", "seller-A");
|
||||
values.put("品牌", "brand-A");
|
||||
values.put("价格", "19.90");
|
||||
row.setValues(values);
|
||||
return row;
|
||||
}
|
||||
|
||||
private static AppearancePatentResultRowDto resultRow(String id, String asin, String country) {
|
||||
AppearancePatentResultRowDto row = new AppearancePatentResultRowDto();
|
||||
row.setId(id);
|
||||
row.setAsin(asin);
|
||||
row.setCountry(country);
|
||||
row.setTitleRisk("无风险");
|
||||
row.setAppearanceRisk("无风险");
|
||||
row.setConclusion("已侵权");
|
||||
row.setStatus("成功");
|
||||
row.setError("");
|
||||
row.setPrice("19.90");
|
||||
return row;
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_sheet_builder_headers_and_sheet_names() throws Exception {
|
||||
try (Workbook wb = new XSSFWorkbook()) {
|
||||
AppearancePatentSheetBuilder.buildResultSheet(wb, List.of(), Map.of());
|
||||
|
||||
Sheet main = wb.getSheet("外观专利检测结果");
|
||||
Sheet reason = wb.getSheet("原因");
|
||||
assertTrue(main != null, "主 sheet 存在");
|
||||
assertTrue(reason != null, "原因 sheet 存在");
|
||||
|
||||
Row header = main.getRow(0);
|
||||
assertEquals(13, header.getLastCellNum(), "主 sheet 13 列");
|
||||
for (int i = 0; i < RESULT_HEADERS_13.length; i++) {
|
||||
assertEquals(RESULT_HEADERS_13[i], header.getCell(i).getStringCellValue(),
|
||||
"第 " + i + " 列表头");
|
||||
}
|
||||
Row reasonHeader = reason.getRow(0);
|
||||
assertEquals("ASIN", reasonHeader.getCell(0).getStringCellValue());
|
||||
assertEquals("外观原因", reasonHeader.getCell(1).getStringCellValue());
|
||||
assertEquals("专利原因", reasonHeader.getCell(2).getStringCellValue());
|
||||
assertEquals("标题原因", reasonHeader.getCell(3).getStringCellValue());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_sheet_builder_header_bold_style() throws Exception {
|
||||
try (XSSFWorkbook wb = new XSSFWorkbook()) {
|
||||
AppearancePatentSheetBuilder.buildResultSheet(wb, List.of(), Map.of());
|
||||
Cell headerCell = wb.getSheet("外观专利检测结果").getRow(0).getCell(0);
|
||||
org.apache.poi.xssf.usermodel.XSSFCellStyle style =
|
||||
(org.apache.poi.xssf.usermodel.XSSFCellStyle) headerCell.getCellStyle();
|
||||
assertTrue(style.getFont().getBold(), "表头加粗");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_sheet_builder_data_rows() throws Exception {
|
||||
try (Workbook wb = new XSSFWorkbook()) {
|
||||
AppearancePatentParsedRowVo p = parsedRow("2_1", "B01A", "US", "title-a", "http://img/a.jpg", "SKU-1");
|
||||
AppearancePatentResultRowDto r = resultRow("2_1", "B01A", "US");
|
||||
AppearancePatentSheetBuilder.buildResultSheet(wb, List.of(p), Map.of(
|
||||
AppearancePatentSheetBuilder.rowKey(r), r));
|
||||
|
||||
Sheet main = wb.getSheet("外观专利检测结果");
|
||||
Row data = main.getRow(1);
|
||||
assertEquals("2_1", data.getCell(0).getStringCellValue(), "id 列");
|
||||
assertEquals("B01A", data.getCell(1).getStringCellValue(), "asin 列");
|
||||
assertEquals("US", data.getCell(2).getStringCellValue(), "国家列");
|
||||
assertEquals("seller-A", data.getCell(3).getStringCellValue(), "卖家名称列");
|
||||
assertEquals("brand-A", data.getCell(4).getStringCellValue(), "品牌列");
|
||||
assertEquals("19.90", data.getCell(5).getStringCellValue(), "价格列");
|
||||
assertEquals("title-a", data.getCell(6).getStringCellValue(), "标题列");
|
||||
assertEquals("http://img/a.jpg", data.getCell(7).getStringCellValue(), "图片链接列");
|
||||
assertEquals("SKU-1", data.getCell(8).getStringCellValue(), "sku 列");
|
||||
assertEquals("无风险", data.getCell(9).getStringCellValue(), "标题维度列");
|
||||
assertEquals("无风险", data.getCell(10).getStringCellValue(), "外观维度列");
|
||||
assertEquals("已侵权", data.getCell(11).getStringCellValue(), "结论列");
|
||||
assertEquals("成功", data.getCell(12).getStringCellValue(), "状态列");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_sheet_builder_empty_data_rows() throws Exception {
|
||||
try (Workbook wb = new XSSFWorkbook()) {
|
||||
AppearancePatentSheetBuilder.buildResultSheet(wb, List.of(), Map.of());
|
||||
Sheet main = wb.getSheet("外观专利检测结果");
|
||||
assertNull(main.getRow(1), "无数据行时只有表头");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_sheet_builder_no_result_row_uses_parsed_fallback() throws Exception {
|
||||
try (Workbook wb = new XSSFWorkbook()) {
|
||||
AppearancePatentParsedRowVo p = parsedRow("2_1", "B01A", "US", "title-a", "http://img/a.jpg", "SKU-1");
|
||||
AppearancePatentSheetBuilder.buildResultSheet(wb, List.of(p), Map.of());
|
||||
|
||||
Row data = wb.getSheet("外观专利检测结果").getRow(1);
|
||||
assertEquals("title-a", data.getCell(6).getStringCellValue(), "无结果行回退解析行标题");
|
||||
assertEquals("http://img/a.jpg", data.getCell(7).getStringCellValue(), "回退解析行 URL");
|
||||
assertEquals("SKU-1", data.getCell(8).getStringCellValue(), "回退解析行 sku");
|
||||
assertEquals("", data.getCell(9).getStringCellValue(), "无结果行 LLM 列留空");
|
||||
assertEquals("", data.getCell(12).getStringCellValue(), "无结果行状态留空");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_sheet_builder_reason_sheet_dedup_by_asin() throws Exception {
|
||||
try (Workbook wb = new XSSFWorkbook()) {
|
||||
AppearancePatentParsedRowVo p1 = parsedRow("2_1", "B01A", "US", "t", "", "");
|
||||
AppearancePatentParsedRowVo p2 = parsedRow("2_2", "B01A", "DE", "t", "", "");
|
||||
AppearancePatentResultRowDto r = resultRow("2_1", "B01A", "US");
|
||||
r.setAppearanceReason("外观理由");
|
||||
r.setPatentReason("专利理由");
|
||||
r.setTitleReason("标题理由");
|
||||
AppearancePatentSheetBuilder.buildResultSheet(wb, List.of(p1, p2), Map.of(
|
||||
AppearancePatentSheetBuilder.rowKey(r), r));
|
||||
|
||||
Sheet reason = wb.getSheet("原因");
|
||||
Row row1 = reason.getRow(1);
|
||||
assertEquals("B01A", row1.getCell(0).getStringCellValue());
|
||||
assertEquals("外观理由", row1.getCell(1).getStringCellValue());
|
||||
assertEquals("专利理由", row1.getCell(2).getStringCellValue());
|
||||
assertEquals("标题理由", row1.getCell(3).getStringCellValue());
|
||||
assertNull(reason.getRow(2), "同 ASIN 第二行去重");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_sheet_builder_writable_output_stream() throws Exception {
|
||||
try (Workbook wb = new XSSFWorkbook()) {
|
||||
AppearancePatentSheetBuilder.buildResultSheet(wb,
|
||||
List.of(parsedRow("1", "B01X", "US", "t", "u", "s")), Map.of());
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
wb.write(out);
|
||||
assertTrue(out.size() > 0, "workbook 可写出");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_sheet_builder_row_key_helpers() {
|
||||
AppearancePatentResultRowDto r = resultRow("2_1", "b01a", "US");
|
||||
assertEquals("2_1::B01A::US", AppearancePatentSheetBuilder.rowKey(r), "rowKey 归一化 + 大写 asin");
|
||||
|
||||
AppearancePatentParsedRowVo p = new AppearancePatentParsedRowVo();
|
||||
p.setDisplayId("2_1");
|
||||
p.setAsin("b01a");
|
||||
p.setCountry("US");
|
||||
assertEquals("2_1::B01A::US", AppearancePatentSheetBuilder.rowKey(p), "解析行 rowKey 同构");
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_sheet_builder_llm_failure_user_facing_cells() throws Exception {
|
||||
try (Workbook wb = new XSSFWorkbook()) {
|
||||
AppearancePatentParsedRowVo p = parsedRow("2_1", "B01A", "US", "t", "", "");
|
||||
AppearancePatentResultRowDto r = resultRow("2_1", "B01A", "US");
|
||||
r.setTitleRisk("coze 调用超时");
|
||||
r.setError("coze 工作流节点执行超限");
|
||||
r.setConclusion("coze 调用超时");
|
||||
AppearancePatentSheetBuilder.buildResultSheet(wb, List.of(p), Map.of(
|
||||
AppearancePatentSheetBuilder.rowKey(r), r));
|
||||
|
||||
Row data = wb.getSheet("外观专利检测结果").getRow(1);
|
||||
assertEquals("coze 工作流节点执行超限", data.getCell(9).getStringCellValue(),
|
||||
"LLM 技术失败展示错误信息");
|
||||
assertEquals("成功", data.getCell(12).getStringCellValue(),
|
||||
"结论回退错误信息后非空即成功(与现状一致)");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_sheet_builder_blank_asin_reason_row_skipped() throws Exception {
|
||||
try (Workbook wb = new XSSFWorkbook()) {
|
||||
AppearancePatentParsedRowVo p = parsedRow("2_1", " ", "US", "t", "", "");
|
||||
AppearancePatentSheetBuilder.buildResultSheet(wb, List.of(p), Map.of());
|
||||
|
||||
Sheet reason = wb.getSheet("原因");
|
||||
assertNull(reason.getRow(1), "空白 ASIN 不入原因 sheet");
|
||||
}
|
||||
}
|
||||
}
|
||||
+323
@@ -0,0 +1,323 @@
|
||||
package com.nanri.aiimage.modules.appearancepatent.service.support;
|
||||
|
||||
import com.nanri.aiimage.modules.appearancepatent.model.vo.AppearancePatentParsedRowVo;
|
||||
import org.apache.poi.ss.usermodel.Row;
|
||||
import org.apache.poi.ss.usermodel.Sheet;
|
||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* 任务 96:AppearancePatent 快照对比测试。
|
||||
* 夹具 Excel(appearancepatent 实际结构)→ 解析结果快照(golden 文件);
|
||||
* 同一夹具输出完全一致;快照变更即失败(防行为漂移)。
|
||||
* 管线与服务侧 parseWorkbook 对应段落一致:parser.parse → VO 组装(displayId/baseId/
|
||||
* groupKey/rowToken)→ hydratePromptFields(组内 title/url/sku 回填);夹具无"状态"列,
|
||||
* 不走 FailedStatusRowFilter。
|
||||
* golden 文件:src/test/resources/appearancepatent/golden/parse-snapshot.txt
|
||||
*/
|
||||
class AppearancePatentSnapshotTest {
|
||||
|
||||
private static final File GOLDEN_PARSE =
|
||||
new File("src/test/resources/appearancepatent/golden/parse-snapshot.txt");
|
||||
|
||||
private static final String[] HEADERS = {"id", "asin", "国家", "价格", "seller sku", "图片链接", "标题", "卖家名称", "品牌"};
|
||||
|
||||
// ---- 夹具 ----
|
||||
|
||||
private static File workbook(String[] headers, List<String[]> rows) throws Exception {
|
||||
File file = File.createTempFile("appearance-patent-snapshot-", ".xlsx");
|
||||
try (XSSFWorkbook wb = new XSSFWorkbook(); FileOutputStream out = new FileOutputStream(file)) {
|
||||
Sheet sheet = wb.createSheet("Sheet1");
|
||||
Row header = sheet.createRow(0);
|
||||
for (int i = 0; i < headers.length; i++) {
|
||||
header.createCell(i).setCellValue(headers[i]);
|
||||
}
|
||||
for (int r = 0; r < rows.size(); r++) {
|
||||
Row row = sheet.createRow(r + 1);
|
||||
for (int c = 0; c < rows.get(r).length; c++) {
|
||||
row.createCell(c).setCellValue(rows.get(r)[c]);
|
||||
}
|
||||
}
|
||||
wb.write(out);
|
||||
}
|
||||
return file;
|
||||
}
|
||||
|
||||
/** 5 有效行:2_1/2_2/2_3 同块(2_2/2_3 标题空缺待组内回填)、单块 5、单块 6。 */
|
||||
private static File fixtureMain() throws Exception {
|
||||
return workbook(HEADERS, List.of(
|
||||
new String[]{"2_1", "B01A", "US", "19.90", "SKU-1", "http://img/a.jpg", "title a", "seller-A", "brand-A"},
|
||||
new String[]{"2_2", "B01B", "DE", "29.90", "SKU-2", "http://img/b.jpg", "", "seller-B", "brand-B"},
|
||||
new String[]{"2_3", "B01C", "FR", "", "", "http://img/c.jpg", "", "seller-C", "brand-C"},
|
||||
new String[]{"5", "B01D", "UK", "9.90", "SKU-4", "http://img/d.jpg", "title d", "seller-D", "brand-D"},
|
||||
new String[]{"6", "B01E", "US", "1.00", "SKU-5", "http://img/e.jpg", "title e", "seller-E", "brand-E"}));
|
||||
}
|
||||
|
||||
/** 缺 id/asin/国家 各 1 行 + 1 全空行;解析后剩 3 有效行。 */
|
||||
private static File fixtureError() throws Exception {
|
||||
return workbook(HEADERS, List.of(
|
||||
new String[]{"", "B02A", "US", "", "", "", "", "seller", "brand"},
|
||||
new String[]{"9", "", "DE", "", "", "", "", "seller", "brand"},
|
||||
new String[]{"10", "B02B", "", "", "", "", "", "seller", "brand"},
|
||||
new String[]{"11", "B02C", "FR", "", "", "", "", "seller", "brand"},
|
||||
new String[]{"12", "B02D", "FR", "", "", "", "", "seller", "brand"},
|
||||
new String[]{"13", "B02E", "FR", "", "", "", "", "seller", "brand"},
|
||||
new String[]{"", "", ""}));
|
||||
}
|
||||
|
||||
// ---- 解析管线(与服务侧 parseWorkbook 对应段落语义一致) ----
|
||||
|
||||
private static List<AppearancePatentParsedRowVo> toRows(File file, String fileKey, String filename) throws Exception {
|
||||
AppearancePatentExcelParser.ParsedSheet parsed = new AppearancePatentExcelParser().parse(file);
|
||||
List<AppearancePatentParsedRowVo> rows = new ArrayList<>();
|
||||
String currentBlockBaseId = "";
|
||||
String currentGroupKey = "";
|
||||
for (AppearancePatentExcelParser.AppearanceExcelRow parsedRow : parsed.rows()) {
|
||||
String id = parsedRow.id();
|
||||
String asin = parsedRow.asin();
|
||||
String country = parsedRow.country();
|
||||
if (id.isBlank() && asin.isBlank() && country.isBlank()) {
|
||||
continue;
|
||||
}
|
||||
if (id.isBlank() || asin.isBlank() || country.isBlank()) {
|
||||
continue;
|
||||
}
|
||||
AppearancePatentParsedRowVo vo = new AppearancePatentParsedRowVo();
|
||||
vo.setSourceFileKey(fileKey);
|
||||
vo.setSourceFilename(filename);
|
||||
vo.setRowIndex(parsedRow.rowIndex());
|
||||
vo.setSourceId(id);
|
||||
vo.setDisplayId(normalizeDisplayId(id));
|
||||
String rowBaseId = baseId(vo.getDisplayId());
|
||||
if (!Objects.equals(currentBlockBaseId, rowBaseId)) {
|
||||
currentBlockBaseId = rowBaseId;
|
||||
currentGroupKey = buildGroupKey(fileKey, rowBaseId, vo.getRowIndex());
|
||||
}
|
||||
vo.setGroupKey(currentGroupKey);
|
||||
vo.setRowToken(buildRowToken(fileKey, vo.getRowIndex()));
|
||||
vo.setAsin(asin);
|
||||
vo.setCountry(country);
|
||||
vo.setPrice(parsedRow.price());
|
||||
vo.setSku(parsedRow.sku());
|
||||
vo.setUrl(parsedRow.url());
|
||||
vo.setTitle(parsedRow.title());
|
||||
vo.setValues(parsedRow.values());
|
||||
rows.add(vo);
|
||||
}
|
||||
hydratePromptFields(rows);
|
||||
return rows;
|
||||
}
|
||||
|
||||
private static void hydratePromptFields(List<AppearancePatentParsedRowVo> rows) {
|
||||
if (rows == null || rows.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
Map<String, List<AppearancePatentParsedRowVo>> rowsByBaseId = new LinkedHashMap<>();
|
||||
for (AppearancePatentParsedRowVo row : rows) {
|
||||
String key = firstNonBlank(normalize(row.getGroupKey()), baseId(row.getDisplayId()));
|
||||
rowsByBaseId.computeIfAbsent(key, ignored -> new ArrayList<>()).add(row);
|
||||
}
|
||||
for (List<AppearancePatentParsedRowVo> siblings : rowsByBaseId.values()) {
|
||||
String title = "";
|
||||
String url = "";
|
||||
String sku = "";
|
||||
for (AppearancePatentParsedRowVo sibling : siblings) {
|
||||
title = firstNonBlank(title, sibling.getTitle());
|
||||
url = firstNonBlank(url, sibling.getUrl());
|
||||
sku = firstNonBlank(sku, sibling.getSku());
|
||||
}
|
||||
for (AppearancePatentParsedRowVo sibling : siblings) {
|
||||
if (normalize(sibling.getTitle()).isBlank()) {
|
||||
sibling.setTitle(title);
|
||||
}
|
||||
if (normalize(sibling.getUrl()).isBlank()) {
|
||||
sibling.setUrl(url);
|
||||
}
|
||||
if (normalize(sibling.getSku()).isBlank()) {
|
||||
sibling.setSku(sku);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static String normalize(String val) {
|
||||
return val == null ? "" : val.replace(String.valueOf((char) 0xFEFF), "").replace((char) 0x3000, ' ').trim().replaceAll("\\s+", " ");
|
||||
}
|
||||
|
||||
private static String baseId(String id) {
|
||||
String s = normalize(id);
|
||||
int idx = s.indexOf('_');
|
||||
return idx > 0 ? s.substring(0, idx) : s;
|
||||
}
|
||||
|
||||
private static String normalizeDisplayId(String id) {
|
||||
return id == null ? "" : id.trim();
|
||||
}
|
||||
|
||||
private static String buildRowToken(String sourceFileKey, Integer rowIndex) {
|
||||
return normalize(sourceFileKey) + "::row::" + (rowIndex == null ? 0 : rowIndex);
|
||||
}
|
||||
|
||||
private static String buildGroupKey(String sourceFileKey, String rowBaseId, Integer rowIndex) {
|
||||
return normalize(sourceFileKey) + "::" + normalize(rowBaseId) + "@" + (rowIndex == null ? 0 : rowIndex);
|
||||
}
|
||||
|
||||
private static String firstNonBlank(String preferred, String fallback) {
|
||||
return preferred == null || preferred.isBlank() ? fallback : preferred.trim();
|
||||
}
|
||||
|
||||
// ---- 快照渲染 ----
|
||||
|
||||
private static String renderRow(AppearancePatentParsedRowVo row) {
|
||||
return " row: idx=" + row.getRowIndex()
|
||||
+ " sourceId=" + row.getSourceId()
|
||||
+ " displayId=" + row.getDisplayId()
|
||||
+ " asin=" + row.getAsin()
|
||||
+ " country=" + row.getCountry()
|
||||
+ " price=" + row.getPrice()
|
||||
+ " sku=" + row.getSku()
|
||||
+ " url=" + row.getUrl()
|
||||
+ " title=" + row.getTitle()
|
||||
+ " groupKey=" + row.getGroupKey()
|
||||
+ " rowToken=" + row.getRowToken()
|
||||
+ " values.size=" + (row.getValues() == null ? 0 : row.getValues().size());
|
||||
}
|
||||
|
||||
private static String renderRows(List<AppearancePatentParsedRowVo> rows) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("rows=").append(rows.size()).append('\n');
|
||||
for (AppearancePatentParsedRowVo row : rows) {
|
||||
sb.append(renderRow(row)).append('\n');
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private static List<AppearancePatentParsedRowVo> mergeRows(List<List<AppearancePatentParsedRowVo>> all) {
|
||||
List<AppearancePatentParsedRowVo> merged = new ArrayList<>();
|
||||
for (List<AppearancePatentParsedRowVo> list : all) {
|
||||
merged.addAll(list);
|
||||
}
|
||||
return merged;
|
||||
}
|
||||
|
||||
public static String runAllParse() throws Exception {
|
||||
return renderRows(mergeRows(List.of(
|
||||
toRows(fixtureMain(), "uploads/main.xlsx", "main.xlsx"),
|
||||
toRows(fixtureError(), "uploads/error.xlsx", "error.xlsx"))));
|
||||
}
|
||||
|
||||
private static String read(File file) throws Exception {
|
||||
return Files.readString(file.toPath(), StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
// ---- 用例 ----
|
||||
|
||||
@Test
|
||||
void test_snapshot_parse_output() throws Exception {
|
||||
assertEquals(read(GOLDEN_PARSE), runAllParse(), "解析输出快照与 golden 一致");
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_snapshot_rows() throws Exception {
|
||||
List<AppearancePatentParsedRowVo> rows = mergeRows(List.of(
|
||||
toRows(fixtureMain(), "uploads/main.xlsx", "main.xlsx"),
|
||||
toRows(fixtureError(), "uploads/error.xlsx", "error.xlsx")));
|
||||
|
||||
assertEquals(8, rows.size(), "有效行 5 + 3");
|
||||
assertEquals("uploads/main.xlsx::2@2", rows.get(0).getGroupKey(), "首块 groupKey = fileKey::baseId@rowIndex");
|
||||
assertEquals("uploads/main.xlsx::row::2", rows.get(0).getRowToken(), "rowToken = fileKey::row::rowIndex");
|
||||
assertEquals("2_1", rows.get(0).getDisplayId(), "displayId 原样保留");
|
||||
assertEquals("B01A", rows.get(0).getAsin(), "asin 大写");
|
||||
assertEquals(9, rows.get(0).getValues().size(), "values 按 9 列表头键取值");
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_snapshot_block_grouping() throws Exception {
|
||||
List<AppearancePatentParsedRowVo> rows = toRows(fixtureMain(), "uploads/main.xlsx", "main.xlsx");
|
||||
|
||||
assertEquals(5, rows.size());
|
||||
assertEquals(rows.get(0).getGroupKey(), rows.get(1).getGroupKey(), "2_1/2_2 同块");
|
||||
assertEquals(rows.get(0).getGroupKey(), rows.get(2).getGroupKey(), "2_1/2_3 同块");
|
||||
assertFalse(rows.get(0).getGroupKey().equals(rows.get(3).getGroupKey()), "2_1 与 5 不同块");
|
||||
assertEquals("uploads/main.xlsx::5@5", rows.get(3).getGroupKey(), "新块 baseId=5");
|
||||
assertEquals("uploads/main.xlsx::6@6", rows.get(4).getGroupKey(), "新块 baseId=6");
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_snapshot_hydration() throws Exception {
|
||||
List<AppearancePatentParsedRowVo> rows = toRows(fixtureMain(), "uploads/main.xlsx", "main.xlsx");
|
||||
|
||||
assertEquals("title a", rows.get(1).getTitle(), "2_2 标题从组内回填");
|
||||
assertEquals("http://img/b.jpg", rows.get(1).getUrl(), "2_2 自身 url 保留");
|
||||
assertEquals("SKU-1", rows.get(2).getSku(), "2_3 sku 从组内回填");
|
||||
assertEquals("title a", rows.get(2).getTitle(), "2_3 标题从组内回填");
|
||||
assertEquals("http://img/c.jpg", rows.get(2).getUrl(), "2_3 自身 url 保留");
|
||||
assertEquals("", rows.get(2).getPrice(), "价格不回填(仅 title/url/sku)");
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_snapshot_error_cases() throws Exception {
|
||||
// 缺必填字段行与全空行均不进入结果;不抛异常
|
||||
List<AppearancePatentParsedRowVo> rows = toRows(fixtureError(), "uploads/error.xlsx", "error.xlsx");
|
||||
|
||||
assertEquals(3, rows.size(), "错误夹具剩 3 有效行");
|
||||
assertEquals("11", rows.get(0).getSourceId());
|
||||
assertEquals("12", rows.get(1).getSourceId());
|
||||
assertEquals("13", rows.get(2).getSourceId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_snapshot_reproducible() throws Exception {
|
||||
assertEquals(runAllParse(), runAllParse(), "同一夹具跑两次结果一致");
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_snapshot_golden_committed() {
|
||||
assertTrue(GOLDEN_PARSE.isFile(), "golden 文件必须存在并入库: " + GOLDEN_PARSE.getAbsolutePath());
|
||||
assertTrue(GOLDEN_PARSE.length() > 0, "golden 文件非空");
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_snapshot_diff_detected() throws Exception {
|
||||
String original = read(GOLDEN_PARSE);
|
||||
assertTrue(original.contains("rows="), "golden 内容合法");
|
||||
try {
|
||||
Files.writeString(GOLDEN_PARSE.toPath(), original + "\n# tampered", StandardCharsets.UTF_8);
|
||||
AssertionError failure = null;
|
||||
try {
|
||||
assertEquals(read(GOLDEN_PARSE), runAllParse(), "篡改后应与 golden 不一致");
|
||||
} catch (AssertionError ex) {
|
||||
failure = ex;
|
||||
}
|
||||
assertTrue(failure != null, "篡改 golden 后断言应失败");
|
||||
} finally {
|
||||
Files.writeString(GOLDEN_PARSE.toPath(), original, StandardCharsets.UTF_8);
|
||||
}
|
||||
assertEquals(original, read(GOLDEN_PARSE), "恢复原始 golden");
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_snapshot_regression_all_parse() throws Exception {
|
||||
assertEquals(read(GOLDEN_PARSE), runAllParse(), "全 parse 路径与 golden 一致");
|
||||
List<AppearancePatentParsedRowVo> rows = mergeRows(List.of(
|
||||
toRows(fixtureMain(), "uploads/main.xlsx", "main.xlsx"),
|
||||
toRows(fixtureError(), "uploads/error.xlsx", "error.xlsx")));
|
||||
assertEquals(8, rows.size(), "总行数 5 + 3");
|
||||
assertEquals("uploads/error.xlsx::row::5", rows.get(5).getRowToken(), "错误夹具首行 rowToken");
|
||||
assertEquals(9, rows.get(5).getValues().size(), "错误夹具 values 仍有 9 列");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user