feat(phase-2): 无侵权行同步服务与出表成功点挂钩——覆盖式落表/公司归属读不出即跳过/失败只留日志不阻断
This commit is contained in:
+282
-6
@@ -1,23 +1,49 @@
|
||||
package com.nanri.aiimage.modules.appearancepatent.service;
|
||||
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.nanri.aiimage.common.mapper.AdminUserMapper;
|
||||
import com.nanri.aiimage.common.model.entity.AdminUserEntity;
|
||||
import com.nanri.aiimage.modules.appearancepatent.mapper.AppearancePatentCleanItemMapper;
|
||||
import com.nanri.aiimage.modules.appearancepatent.model.dto.AppearancePatentResultRowDto;
|
||||
import com.nanri.aiimage.modules.appearancepatent.model.entity.CleanItemEntity;
|
||||
import com.nanri.aiimage.modules.task.mapper.TaskChunkMapper;
|
||||
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.TaskChunkEntity;
|
||||
import com.nanri.aiimage.modules.task.model.entity.TaskFileJobEntity;
|
||||
import com.nanri.aiimage.modules.task.service.TransientPayloadStorageService;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.InOrder;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
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.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.inOrder;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* AC-1/AC-2/AC-3(v1.3 R1/R5):外观专利出表成功 → 「结论=无侵权」行同步落后管新表。
|
||||
*
|
||||
* <p>Pending Phase 2 激活。Phase 0 预置的是<b>接缝判据</b>(接缝名是判据的一部分,Phase 2 微调命名
|
||||
* 必须同步改本文件并在蓝图 §5 登记);激活时除本清单外,再补真实对象的行为样本:</p>
|
||||
* <p>Phase 2 已激活。前 5 支是 Phase 0 预置的<b>接缝判据</b>(接缝名是判据的一部分,
|
||||
* 改名必须同步改判据并在蓝图 §5 登记);后 8 支是激活时补的真实对象行为样本:</p>
|
||||
*
|
||||
* <ul>
|
||||
* <li><b>AC-1</b>:payload N 行、其中 M 行「结论=无侵权」→ {@code biz_appearance_patent_clean_item}
|
||||
@@ -27,10 +53,11 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
* <li><b>AC-2</b>:同一任务补检/重试重新出表 → 按 {@code result_id} 覆盖式重写,
|
||||
* {@code uk(result_id, row_no)} 下零重复行;出表失败(success=0/无持久化行)不写任何行;</li>
|
||||
* <li><b>AC-3</b>:同步抛任何异常 → 任务终态与 {@code biz_file_result} 不被改写,错误只进日志;
|
||||
* <b>同步装配路径(shouldAssembleSynchronously 分支)与异步出表作业两条触发点各一支判据</b>。</li>
|
||||
* <b>同步装配路径(shouldAssembleSynchronously 分支)与异步出表作业两条触发点各一支判据</b>——
|
||||
* 装配分支的判据是接缝格(该分支当前恒不启用,无法行为触发);异步作业与失败兜底两路
|
||||
* 另有 handler 级行为样本。</li>
|
||||
* </ul>
|
||||
*/
|
||||
@Disabled("Pending Phase 2: AC-1/AC-2/AC-3 外观专利无侵权行同步落表(激活时移除本注解)")
|
||||
class AppearancePatentCleanSyncTest {
|
||||
|
||||
private static final Path MODULE = Path.of("").toAbsolutePath();
|
||||
@@ -45,6 +72,8 @@ class AppearancePatentCleanSyncTest {
|
||||
private static final String TRIGGER_2_REL =
|
||||
"modules/appearancepatent/service/AppearancePatentResultFileJobHandler.java";
|
||||
|
||||
// ===================== Phase 0 预置接缝判据(激活) =====================
|
||||
|
||||
@Test
|
||||
void 同步服务类与入口接缝就位() throws Exception {
|
||||
Class<?> service = Class.forName(SYNC_SERVICE_FQCN,
|
||||
@@ -101,6 +130,253 @@ class AppearancePatentCleanSyncTest {
|
||||
"不得把 company_id 回填成默认 1——owner 读不出 company_id 必须跳过并留日志(铁律 30,AC-1)");
|
||||
}
|
||||
|
||||
// ===================== 激活时补的行为样本(AC-1/AC-2/AC-3) =====================
|
||||
|
||||
@Test
|
||||
void 出表成功后按严格口径落表且归属任务owner公司() throws Exception {
|
||||
AppearancePatentCleanItemMapper cleanItemMapper = mock(AppearancePatentCleanItemMapper.class);
|
||||
TaskChunkMapper taskChunkMapper = mock(TaskChunkMapper.class);
|
||||
AdminUserMapper adminUserMapper = mock(AdminUserMapper.class);
|
||||
when(taskChunkMapper.selectList(any())).thenReturn(List.of(chunk(33001L)));
|
||||
when(adminUserMapper.selectById(77L)).thenReturn(user(7L));
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
// 行序即 row_no 序:clean(0)/clean(1)/待复核(2)/侵权(3)。字面量取值口径由黄金测试
|
||||
// AppearancePatentChunkRowContractTest#无侵权常量快照单一来源 钉在 LlmClient。
|
||||
String payloadJson = mapper.writeValueAsString(List.of(
|
||||
row("t1", "B0CLEAN001", "无侵权"),
|
||||
row("t2", "B0CLEAN002", "无侵权"),
|
||||
row("t3", "B0RECHK01", "外观无侵权·商标待复核"),
|
||||
row("t4", "B0BAD0001", "侵权")));
|
||||
AppearancePatentCleanSyncService service = service(cleanItemMapper, taskChunkMapper, adminUserMapper, payloadJson);
|
||||
|
||||
service.syncFromResult(task(33001L, 77L), result(34819L, 33001L, "oss://result/x.xlsx"));
|
||||
|
||||
verify(cleanItemMapper).deleteByResultId(34819L);
|
||||
@SuppressWarnings("unchecked")
|
||||
ArgumentCaptor<List<CleanItemEntity>> rows = ArgumentCaptor.forClass((Class) List.class);
|
||||
verify(cleanItemMapper).insertBatch(rows.capture());
|
||||
assertEquals(2, rows.getValue().size(), "严格口径只应落 2 行:待复核与侵权行零落表(用户拍板 ①)");
|
||||
CleanItemEntity first = rows.getValue().get(0);
|
||||
assertEquals(34819L, first.getResultId());
|
||||
assertEquals(33001L, first.getTaskId());
|
||||
assertEquals(77L, first.getUserId());
|
||||
assertEquals(7L, first.getCompanyId(), "company_id=任务 owner 用户的 users.company_id(物化列)");
|
||||
assertEquals(0, first.getRowNo());
|
||||
assertEquals("B0CLEAN001", first.getAsin());
|
||||
assertEquals("英国", first.getCountry());
|
||||
assertEquals("SKU-B0CLEAN001", first.getSku());
|
||||
assertEquals("Nike", first.getBrand());
|
||||
assertEquals("12.99", first.getPrice());
|
||||
assertEquals("Women Floral Dress Summer Casual", first.getTitle());
|
||||
assertEquals("https://img.example/B0CLEAN001.jpg", first.getImageUrl());
|
||||
assertNull(first.getItemUrl(), "payload 23 键契约无 itemUrl 键:V189 立列但可空,按「payload 有才落」不伪造");
|
||||
assertEquals("无侵权", first.getConclusion(), "结论存原值不转写");
|
||||
assertEquals("标题维度原因", first.getTitleReason());
|
||||
assertEquals("外观维度原因", first.getAppearanceReason());
|
||||
assertEquals("专利维度原因", first.getPatentReason());
|
||||
assertEquals("appearance_patent_33001.xlsx", first.getSourceFilename());
|
||||
assertEquals(1, rows.getValue().get(1).getRowNo(), "row_no 按 payload 全行序号计(含未入选行),序号缺口即可审计被过滤的行");
|
||||
}
|
||||
|
||||
@Test
|
||||
void owner读不出公司归属时跳过不回填默认公司() throws Exception {
|
||||
AppearancePatentCleanItemMapper cleanItemMapper = mock(AppearancePatentCleanItemMapper.class);
|
||||
TaskChunkMapper taskChunkMapper = mock(TaskChunkMapper.class);
|
||||
AdminUserMapper adminUserMapper = mock(AdminUserMapper.class);
|
||||
when(taskChunkMapper.selectList(any())).thenReturn(List.of(chunk(33001L)));
|
||||
when(adminUserMapper.selectById(77L)).thenReturn(user(null));
|
||||
AppearancePatentCleanSyncService service = service(cleanItemMapper, taskChunkMapper, adminUserMapper,
|
||||
new ObjectMapper().writeValueAsString(List.of(row("t1", "B0CLEAN001", "无侵权"))));
|
||||
|
||||
service.syncFromResult(task(33001L, 77L), result(34819L, 33001L, "oss://result/x.xlsx"));
|
||||
|
||||
verifyNoInteractions(cleanItemMapper);
|
||||
}
|
||||
|
||||
@Test
|
||||
void 重新出表按resultId覆盖式重写零重复() throws Exception {
|
||||
AppearancePatentCleanItemMapper cleanItemMapper = mock(AppearancePatentCleanItemMapper.class);
|
||||
TaskChunkMapper taskChunkMapper = mock(TaskChunkMapper.class);
|
||||
AdminUserMapper adminUserMapper = mock(AdminUserMapper.class);
|
||||
when(taskChunkMapper.selectList(any())).thenReturn(List.of(chunk(33001L)));
|
||||
when(adminUserMapper.selectById(77L)).thenReturn(user(7L));
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
TransientPayloadStorageService storage = mock(TransientPayloadStorageService.class);
|
||||
// 补检后首行结论翻成侵权:覆盖式重写后只剩 1 行(row_no=1),uk(result_id, row_no) 下零重复
|
||||
when(storage.resolvePayload(anyString(), anyString()))
|
||||
.thenReturn(mapper.writeValueAsString(List.of(
|
||||
row("t1", "B0CLEAN001", "无侵权"),
|
||||
row("t2", "B0CLEAN002", "无侵权")))
|
||||
)
|
||||
.thenReturn(mapper.writeValueAsString(List.of(
|
||||
row("t1", "B0CLEAN001", "侵权"),
|
||||
row("t2", "B0CLEAN002", "无侵权")))
|
||||
);
|
||||
AppearancePatentCleanSyncService syncService = new AppearancePatentCleanSyncService(
|
||||
cleanItemMapper, taskChunkMapper, adminUserMapper, mapper, storage,
|
||||
mock(org.springframework.transaction.PlatformTransactionManager.class));
|
||||
|
||||
FileTaskEntity task = task(33001L, 77L);
|
||||
FileResultEntity result = result(34819L, 33001L, "oss://result/x.xlsx");
|
||||
syncService.syncFromResult(task, result);
|
||||
syncService.syncFromResult(task, result);
|
||||
|
||||
InOrder order = inOrder(cleanItemMapper);
|
||||
order.verify(cleanItemMapper, times(2)).deleteByResultId(34819L);
|
||||
@SuppressWarnings("unchecked")
|
||||
ArgumentCaptor<List<CleanItemEntity>> batches = ArgumentCaptor.forClass((Class) List.class);
|
||||
verify(cleanItemMapper, times(2)).insertBatch(batches.capture());
|
||||
assertEquals(2, batches.getAllValues().get(0).size());
|
||||
assertEquals(1, batches.getAllValues().get(1).size(), "覆盖式重写:重出后只落仍满足严格口径的行");
|
||||
}
|
||||
|
||||
@Test
|
||||
void 出表失败url未回填不写任何行() throws Exception {
|
||||
AppearancePatentCleanItemMapper cleanItemMapper = mock(AppearancePatentCleanItemMapper.class);
|
||||
TaskChunkMapper taskChunkMapper = mock(TaskChunkMapper.class);
|
||||
AdminUserMapper adminUserMapper = mock(AdminUserMapper.class);
|
||||
AppearancePatentCleanSyncService service = service(cleanItemMapper, taskChunkMapper, adminUserMapper,
|
||||
new ObjectMapper().writeValueAsString(List.of(row("t1", "B0CLEAN001", "无侵权"))));
|
||||
|
||||
service.syncFromResult(task(33001L, 77L), result(34819L, 33001L, null));
|
||||
|
||||
verifyNoInteractions(cleanItemMapper);
|
||||
verifyNoInteractions(taskChunkMapper);
|
||||
}
|
||||
|
||||
@Test
|
||||
void 分片不可读时绝不清空已同步行() throws Exception {
|
||||
AppearancePatentCleanItemMapper cleanItemMapper = mock(AppearancePatentCleanItemMapper.class);
|
||||
TaskChunkMapper taskChunkMapper = mock(TaskChunkMapper.class);
|
||||
AdminUserMapper adminUserMapper = mock(AdminUserMapper.class);
|
||||
when(taskChunkMapper.selectList(any())).thenReturn(List.of());
|
||||
when(adminUserMapper.selectById(77L)).thenReturn(user(7L));
|
||||
AppearancePatentCleanSyncService service = service(cleanItemMapper, taskChunkMapper, adminUserMapper, "[]");
|
||||
|
||||
// 已交付作业被事件重投:分片已清理,此时删除=清空用户已同步的数据,必须跳过
|
||||
service.syncFromResult(task(33001L, 77L), result(34819L, 33001L, "oss://result/x.xlsx"));
|
||||
|
||||
verifyNoInteractions(cleanItemMapper);
|
||||
}
|
||||
|
||||
@Test
|
||||
void 同步异常只进日志不外抛不碰任务与结果() throws Exception {
|
||||
AppearancePatentCleanItemMapper cleanItemMapper = mock(AppearancePatentCleanItemMapper.class);
|
||||
TaskChunkMapper taskChunkMapper = mock(TaskChunkMapper.class);
|
||||
AdminUserMapper adminUserMapper = mock(AdminUserMapper.class);
|
||||
when(taskChunkMapper.selectList(any())).thenReturn(List.of(chunk(33001L)));
|
||||
when(adminUserMapper.selectById(77L)).thenReturn(user(7L));
|
||||
when(cleanItemMapper.deleteByResultId(anyLong())).thenThrow(new RuntimeException("db down"));
|
||||
AppearancePatentCleanSyncService service = service(cleanItemMapper, taskChunkMapper, adminUserMapper,
|
||||
new ObjectMapper().writeValueAsString(List.of(row("t1", "B0CLEAN001", "无侵权"))));
|
||||
|
||||
FileTaskEntity task = task(33001L, 77L);
|
||||
FileResultEntity result = result(34819L, 33001L, "oss://result/x.xlsx");
|
||||
assertDoesNotThrow(() -> service.syncFromResult(task, result),
|
||||
"同步失败必须吞掉只留日志(AC-3):出表链不因同步失败改变任何终态");
|
||||
assertEquals("SUCCESS", task.getStatus(), "任务终态不被改写");
|
||||
assertEquals("oss://result/x.xlsx", result.getResultFileUrl(), "结果文件地址不被改写");
|
||||
}
|
||||
|
||||
@Test
|
||||
void 异步出表作业成功后触发同步且停放不触发() {
|
||||
AppearancePatentTaskService facade = mock(AppearancePatentTaskService.class);
|
||||
AppearancePatentResultFileJobHandler handler = new AppearancePatentResultFileJobHandler(facade);
|
||||
TaskFileJobEntity job = new TaskFileJobEntity();
|
||||
job.setTaskId(31769L);
|
||||
job.setResultId(34819L);
|
||||
|
||||
when(facade.processResultFileJob(job)).thenReturn(true);
|
||||
handler.process(job);
|
||||
verify(facade).syncFromResult(31769L, 34819L);
|
||||
|
||||
when(facade.processResultFileJob(job)).thenReturn(false);
|
||||
handler.process(job);
|
||||
// 停放(等待分片/补检)不是出表成功,不触发同步
|
||||
verify(facade, times(1)).syncFromResult(anyLong(), anyLong());
|
||||
}
|
||||
|
||||
@Test
|
||||
void 失败兜底出表成功后触发同步() {
|
||||
AppearancePatentTaskService facade = mock(AppearancePatentTaskService.class);
|
||||
AppearancePatentResultFileJobHandler handler = new AppearancePatentResultFileJobHandler(facade);
|
||||
TaskFileJobEntity job = new TaskFileJobEntity();
|
||||
job.setTaskId(31769L);
|
||||
job.setResultId(34819L);
|
||||
|
||||
when(facade.fallbackAssembleResultFile(job)).thenReturn(true);
|
||||
handler.fallbackAssembleOnFailure(job, "重试耗尽");
|
||||
verify(facade).syncFromResult(31769L, 34819L);
|
||||
|
||||
when(facade.fallbackAssembleResultFile(job)).thenReturn(false);
|
||||
handler.fallbackAssembleOnFailure(job, "兜底也失败");
|
||||
verify(facade, times(1)).syncFromResult(anyLong(), anyLong());
|
||||
}
|
||||
|
||||
// ===================== 夹具 =====================
|
||||
|
||||
private static AppearancePatentCleanSyncService service(AppearancePatentCleanItemMapper cleanItemMapper,
|
||||
TaskChunkMapper taskChunkMapper,
|
||||
AdminUserMapper adminUserMapper,
|
||||
String payloadJson) {
|
||||
TransientPayloadStorageService storage = mock(TransientPayloadStorageService.class);
|
||||
when(storage.resolvePayload(anyString(), anyString())).thenReturn(payloadJson);
|
||||
return new AppearancePatentCleanSyncService(cleanItemMapper, taskChunkMapper, adminUserMapper,
|
||||
new ObjectMapper(), storage, mock(org.springframework.transaction.PlatformTransactionManager.class));
|
||||
}
|
||||
|
||||
private static TaskChunkEntity chunk(long taskId) {
|
||||
TaskChunkEntity chunk = new TaskChunkEntity();
|
||||
chunk.setTaskId(taskId);
|
||||
chunk.setChunkIndex(0);
|
||||
chunk.setPayloadJson("chunk-0.json");
|
||||
return chunk;
|
||||
}
|
||||
|
||||
private static AppearancePatentResultRowDto row(String rowToken, String asin, String conclusion) {
|
||||
AppearancePatentResultRowDto row = new AppearancePatentResultRowDto();
|
||||
row.setRowToken(rowToken);
|
||||
row.setId("2_1");
|
||||
row.setAsin(asin);
|
||||
row.setCountry("英国");
|
||||
row.setSku("SKU-" + asin);
|
||||
row.setBrand("Nike");
|
||||
row.setPrice("12.99");
|
||||
row.setUrl("https://img.example/" + asin + ".jpg");
|
||||
row.setTitle("Women Floral Dress Summer Casual");
|
||||
row.setConclusion(conclusion);
|
||||
row.setTitleReason("标题维度原因");
|
||||
row.setAppearanceReason("外观维度原因");
|
||||
row.setPatentReason("专利维度原因");
|
||||
row.setSourceFilename("appearance_patent_33001.xlsx");
|
||||
return row;
|
||||
}
|
||||
|
||||
private static FileTaskEntity task(long taskId, Long userId) {
|
||||
FileTaskEntity task = new FileTaskEntity();
|
||||
task.setId(taskId);
|
||||
task.setModuleType("APPEARANCE_PATENT");
|
||||
task.setUserId(userId);
|
||||
task.setStatus("SUCCESS");
|
||||
return task;
|
||||
}
|
||||
|
||||
private static FileResultEntity result(long resultId, long taskId, String fileUrl) {
|
||||
FileResultEntity result = new FileResultEntity();
|
||||
result.setId(resultId);
|
||||
result.setTaskId(taskId);
|
||||
result.setModuleType("APPEARANCE_PATENT");
|
||||
result.setResultFileUrl(fileUrl);
|
||||
return result;
|
||||
}
|
||||
|
||||
private static AdminUserEntity user(Long companyId) {
|
||||
AdminUserEntity user = new AdminUserEntity();
|
||||
user.setId(77L);
|
||||
user.setCompanyId(companyId);
|
||||
return user;
|
||||
}
|
||||
|
||||
private static Path findCleanItemMapperXml() throws IOException {
|
||||
Path resources = MODULE.resolve("src/main/resources/mapper");
|
||||
if (!Files.exists(resources)) {
|
||||
|
||||
+2
-1
@@ -102,7 +102,8 @@ class AppearancePatentInterruptedResumeTest {
|
||||
fileTaskMapper, fileResultMapper, taskScopeStateMapper, taskChunkMapper,
|
||||
taskCacheService, null, null,
|
||||
payloadSupport, null, null, deliverySupport, progressSupport, null,
|
||||
null, null, null, null, null, null, null);
|
||||
null, null, null, null, null, null, null,
|
||||
null);
|
||||
}
|
||||
|
||||
private static FileTaskEntity task(String status) {
|
||||
|
||||
+2
-1
@@ -56,7 +56,8 @@ class AppearancePatentRecheckProgressMessageTest {
|
||||
"APPEARANCE_PATENT", 0L, null, null,
|
||||
null, null, null, null, null, null, null,
|
||||
null, null, null, deliverySupport, progressSupport, null,
|
||||
null, null, null, null, null, null, null);
|
||||
null, null, null, null, null, null, null,
|
||||
null);
|
||||
}
|
||||
|
||||
private FileTaskEntity task() {
|
||||
|
||||
+6
-2
@@ -118,8 +118,12 @@ class ResultFileJobHandlerMappingSnapshotTest {
|
||||
Method process = AppearancePatentResultFileJobHandler.class.getDeclaredMethod("process", TaskFileJobEntity.class);
|
||||
assertTrue(process.getReturnType() == boolean.class, "异步模块返回 boolean");
|
||||
String source = handlerSource("AppearancePatentResultFileJobHandler");
|
||||
assertTrue(source.contains("return appearancePatentTaskService.processResultFileJob(job)"),
|
||||
"原样透传 Service boolean");
|
||||
// v1.3(无侵权行同步)起成功分支追加 syncFromResult,返回值仍是 Service boolean 原样透传:
|
||||
// 接缝判据随蓝图 §2.2 的 hook 计划同步改形(见蓝图 §5 变更记录),语义判据拆成两支。
|
||||
assertTrue(source.contains("boolean delivered = appearancePatentTaskService.processResultFileJob(job);"),
|
||||
"透传 Service boolean(成功分支追加同步,返回值不变)");
|
||||
assertTrue(source.contains("return delivered;"), "process 原样透传 Service boolean,不翻转");
|
||||
assertTrue(source.contains("syncFromResult"), "出表成功点追加「无侵权」行同步(v1.3 R1,AC-3)");
|
||||
assertTrue(source.contains("supportsAsyncOffload()") && source.contains("return true;"),
|
||||
"offload 标记为 true");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user