test(appearance-patent): 出表成功→同步落表→后管可查 全链 mock 回环——mapper mock 成按 XML 真实语义的内存假表,真实同步/查询两服务跑通:未出表不写行、严格口径只落无侵权、覆盖式重写零重复、分公司隔离、任务号/ASIN 前缀/日期窗筛选
This commit is contained in:
+285
@@ -0,0 +1,285 @@
|
||||
package com.nanri.aiimage.modules.appearancepatent.service;
|
||||
|
||||
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.appearancepatent.model.vo.AppearancePatentCleanItemPageVo;
|
||||
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.service.TransientPayloadStorageService;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* 「出表成功 → 同步落表 → 后管立即可查」全链 mock 回环(2026-11-10,用户口径
|
||||
* 「mock 一下测一下看看能不能同步」)。
|
||||
*
|
||||
* <p>与 {@code AppearancePatentCleanSyncTest}(写侧逐格判据)互补:本类把 mapper mock 成一张
|
||||
* <b>内存假表</b>,按 {@code AppearancePatentCleanItemMapper.xml} 的真实语义模拟
|
||||
* {@code biz_appearance_patent_clean_item}——id 自增、synced_at 取 DB 默认 CURRENT_TIMESTAMP
|
||||
* (同秒自增保序)、过滤面 company eq/task eq/ASIN 前缀/synced_at 半开区间、
|
||||
* ORDER BY synced_at DESC, id DESC——然后驱动两支<b>真实服务</b>跑完整链:</p>
|
||||
*
|
||||
* <ul>
|
||||
* <li>写侧 {@code AppearancePatentCleanSyncService}:出表未成功不写行;url 回填后同步,
|
||||
* 严格口径只落「无侵权」行;补检重出按 result_id 覆盖式重写;</li>
|
||||
* <li>读侧 {@code AppearancePatentCleanItemQueryService}(后管只读页同一入口):同步后立即可查,
|
||||
* 分公司隔离(本公司看得见、别家看不见)、任务号/ASIN 前缀/同步日期窗口筛选与 SQL 同语义。</li>
|
||||
* </ul>
|
||||
*/
|
||||
class AppearancePatentCleanSyncRoundTripTest {
|
||||
|
||||
@Test
|
||||
void 出表成功同步落表后管立即可查全链回环() throws Exception {
|
||||
FakeCleanItemTable table = new FakeCleanItemTable();
|
||||
AppearancePatentCleanItemMapper cleanItemMapper = mapperOver(table);
|
||||
TaskChunkMapper taskChunkMapper = mock(TaskChunkMapper.class);
|
||||
AdminUserMapper adminUserMapper = mock(AdminUserMapper.class);
|
||||
ObjectMapper json = new ObjectMapper();
|
||||
TransientPayloadStorageService storage = mock(TransientPayloadStorageService.class);
|
||||
when(taskChunkMapper.selectList(any())).thenReturn(List.of(chunk(33001L)));
|
||||
when(adminUserMapper.selectById(77L)).thenReturn(user(77L, 7L));
|
||||
// 第一次出表 4 行 payload(2 无侵权+1 待复核+1 侵权);补检后首行翻成侵权
|
||||
when(storage.resolvePayload(anyString(), anyString()))
|
||||
.thenReturn(json.writeValueAsString(List.of(
|
||||
row("t1", "B0CLEAN001", "无侵权"),
|
||||
row("t2", "B0CLEAN002", "无侵权"),
|
||||
row("t3", "B0RECHK01", "外观无侵权·商标待复核"),
|
||||
row("t4", "B0BAD0001", "侵权"))))
|
||||
.thenReturn(json.writeValueAsString(List.of(
|
||||
row("t1", "B0CLEAN001", "侵权"),
|
||||
row("t2", "B0CLEAN002", "无侵权"))));
|
||||
AppearancePatentCleanSyncService sync = new AppearancePatentCleanSyncService(
|
||||
cleanItemMapper, taskChunkMapper, adminUserMapper, json, storage,
|
||||
mock(org.springframework.transaction.PlatformTransactionManager.class));
|
||||
AppearancePatentCleanItemQueryService query = new AppearancePatentCleanItemQueryService(cleanItemMapper);
|
||||
FileTaskEntity task = task(33001L, 77L);
|
||||
|
||||
// ⓪ 出表未成功(url 未回填):不写任何行,后管查不到
|
||||
sync.syncFromResult(task, result(34819L, null));
|
||||
assertEquals(0, table.rows.size(), "url 未回填=出表未成功,不得写行");
|
||||
|
||||
// ① 出表成功 → 同步:4 行 payload 只落 2 行「无侵权」(严格口径,待复核/侵权零落表)
|
||||
sync.syncFromResult(task, result(34819L, "oss://bucket/appearance_patent_33001.xlsx"));
|
||||
assertEquals(2, table.rows.size(), "同步后假表恰有 2 行无侵权");
|
||||
assertEquals(0, table.rows.get(0).getRowNo(), "row_no 按 payload 全行序号计(含被过滤行)");
|
||||
|
||||
// ② 后管同一查询入口立即可见(超管全量),行值与 payload 一致(AC-1)、synced_at 倒序
|
||||
AppearancePatentCleanItemPageVo vo = query.page(null, null, null, null, null, null, null, null);
|
||||
assertEquals(2L, vo.getTotal(), "后管全量 total=2");
|
||||
assertEquals("B0CLEAN002", vo.getItems().get(0).getAsin(), "synced_at 倒序=后同步的在前");
|
||||
assertEquals("英国", vo.getItems().get(0).getCountry());
|
||||
assertEquals("SKU-B0CLEAN002", vo.getItems().get(0).getSku());
|
||||
assertEquals("Nike", vo.getItems().get(0).getBrand());
|
||||
assertEquals("无侵权", vo.getItems().get(0).getConclusion());
|
||||
assertEquals("appearance_patent_33001.xlsx", vo.getItems().get(0).getSourceFilename());
|
||||
assertEquals(7L, vo.getItems().get(0).getCompanyId(), "company_id=任务 owner 公司(物化列)");
|
||||
|
||||
// ③ 分公司隔离:本公司(7)看得见,别家公司(9)看不见
|
||||
assertEquals(2L, query.page(null, null, null, null, null, null, null, 7L).getTotal());
|
||||
assertEquals(0L, query.page(null, null, null, null, null, null, null, 9L).getTotal(),
|
||||
"别的公司查不到本公司同步行");
|
||||
|
||||
// ④ 补检重新出表 → 覆盖式重写:首行翻成侵权后全库只剩 1 行(uk(result_id,row_no) 零重复)
|
||||
sync.syncFromResult(task, result(34819L, "oss://bucket/appearance_patent_33001_retry.xlsx"));
|
||||
assertEquals(1, table.rows.size(), "覆盖式重写后只剩仍满足严格口径的 1 行");
|
||||
assertEquals(1, table.rows.get(0).getRowNo());
|
||||
assertEquals("B0CLEAN002", table.rows.get(0).getAsin());
|
||||
assertEquals(1L, query.page(null, null, null, null, null, null, null, null).getTotal(),
|
||||
"重出后后管立即可见新结果面");
|
||||
|
||||
// ⑤ 后管筛选面与 XML 同语义:任务号 eq、ASIN 前缀、同步日期半开区间
|
||||
assertEquals(1L, query.page(null, null, 33001L, null, null, null, null, null).getTotal());
|
||||
assertEquals(0L, query.page(null, null, 99999L, null, null, null, null, null).getTotal());
|
||||
assertEquals(1L, query.page(null, null, null, "B0CLEAN", null, null, null, null).getTotal(),
|
||||
"ASIN 前缀命中");
|
||||
assertEquals(0L, query.page(null, null, null, "B0NOPE", null, null, null, null).getTotal());
|
||||
LocalDate syncDay = table.lastSyncedAt.toLocalDate();
|
||||
assertEquals(1L, query.page(null, null, null, null, null, syncDay, syncDay, null).getTotal(),
|
||||
"同步当天闭区间命中(synced_at < 次日 0 点)");
|
||||
assertEquals(0L, query.page(null, null, null, null, null,
|
||||
syncDay.plusDays(1), syncDay.plusDays(1), null).getTotal(),
|
||||
"同步日之后的一天查不到");
|
||||
assertTrue(vo.getPage() >= 1 && vo.getPageSize() >= 1, "分页归一形状随行");
|
||||
}
|
||||
|
||||
// ===================== 夹具 =====================
|
||||
|
||||
/**
|
||||
* 内存假表:按 {@code AppearancePatentCleanItemMapper.xml} 真实语义模拟
|
||||
* {@code biz_appearance_patent_clean_item}(含 id 自增与 synced_at DB 默认值)。
|
||||
*/
|
||||
private static final class FakeCleanItemTable {
|
||||
private final List<CleanItemEntity> rows = new ArrayList<>();
|
||||
private long idSeq = 0;
|
||||
/** 模拟 DB CURRENT_TIMESTAMP 默认值;逐行 +1s 使「后同步在前」可断言。 */
|
||||
private LocalDateTime lastSyncedAt = LocalDateTime.of(2026, 11, 10, 10, 30, 0);
|
||||
|
||||
int deleteByResultId(Long resultId) {
|
||||
int before = rows.size();
|
||||
rows.removeIf(row -> resultId.equals(row.getResultId()));
|
||||
return before - rows.size();
|
||||
}
|
||||
|
||||
int insertBatch(List<CleanItemEntity> batch) {
|
||||
for (CleanItemEntity row : batch) {
|
||||
CleanItemEntity stored = new CleanItemEntity();
|
||||
stored.setId(++idSeq);
|
||||
stored.setResultId(row.getResultId());
|
||||
stored.setTaskId(row.getTaskId());
|
||||
stored.setUserId(row.getUserId());
|
||||
stored.setCompanyId(row.getCompanyId());
|
||||
stored.setRowNo(row.getRowNo());
|
||||
stored.setAsin(row.getAsin());
|
||||
stored.setCountry(row.getCountry());
|
||||
stored.setSku(row.getSku());
|
||||
stored.setBrand(row.getBrand());
|
||||
stored.setPrice(row.getPrice());
|
||||
stored.setTitle(row.getTitle());
|
||||
stored.setImageUrl(row.getImageUrl());
|
||||
stored.setItemUrl(row.getItemUrl());
|
||||
stored.setConclusion(row.getConclusion());
|
||||
stored.setTitleReason(row.getTitleReason());
|
||||
stored.setAppearanceReason(row.getAppearanceReason());
|
||||
stored.setPatentReason(row.getPatentReason());
|
||||
stored.setSourceFilename(row.getSourceFilename());
|
||||
lastSyncedAt = lastSyncedAt.plusSeconds(1);
|
||||
stored.setSyncedAt(lastSyncedAt);
|
||||
rows.add(stored);
|
||||
}
|
||||
return batch == null ? 0 : batch.size();
|
||||
}
|
||||
|
||||
long count(Long companyId, Long taskId, String asin, String country,
|
||||
LocalDate startDate, LocalDate endDate) {
|
||||
return matching(companyId, taskId, asin, country, startDate, endDate).size();
|
||||
}
|
||||
|
||||
List<CleanItemEntity> page(Long companyId, Long taskId, String asin, String country,
|
||||
LocalDate startDate, LocalDate endDate, long offset, int limit) {
|
||||
List<CleanItemEntity> matched = matching(companyId, taskId, asin, country, startDate, endDate);
|
||||
matched.sort(Comparator.comparing((CleanItemEntity row) -> row.getSyncedAt()).reversed()
|
||||
.thenComparing(row -> row.getId(), Comparator.reverseOrder()));
|
||||
int from = (int) Math.min(offset, matched.size());
|
||||
int to = (int) Math.min(offset + limit, matched.size());
|
||||
return new ArrayList<>(matched.subList(from, to));
|
||||
}
|
||||
|
||||
/** 过滤面照 cleanItemConds:company eq/task eq/asin 前缀/country eq/synced_at 半开区间。 */
|
||||
private List<CleanItemEntity> matching(Long companyId, Long taskId, String asin, String country,
|
||||
LocalDate startDate, LocalDate endDate) {
|
||||
List<CleanItemEntity> out = new ArrayList<>();
|
||||
for (CleanItemEntity row : rows) {
|
||||
if (companyId != null && !companyId.equals(row.getCompanyId())) {
|
||||
continue;
|
||||
}
|
||||
if (taskId != null && !taskId.equals(row.getTaskId())) {
|
||||
continue;
|
||||
}
|
||||
if (asin != null && !asin.isEmpty()
|
||||
&& (row.getAsin() == null || !row.getAsin().startsWith(asin))) {
|
||||
continue;
|
||||
}
|
||||
if (country != null && !country.isEmpty() && !country.equals(row.getCountry())) {
|
||||
continue;
|
||||
}
|
||||
if (startDate != null && row.getSyncedAt().isBefore(startDate.atStartOfDay())) {
|
||||
continue;
|
||||
}
|
||||
if (endDate != null && !row.getSyncedAt().isBefore(endDate.plusDays(1).atStartOfDay())) {
|
||||
continue;
|
||||
}
|
||||
out.add(row);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
}
|
||||
|
||||
/** mapper mock 全部接在假表上:四支方法与 XML 同语义。 */
|
||||
private static AppearancePatentCleanItemMapper mapperOver(FakeCleanItemTable table) {
|
||||
AppearancePatentCleanItemMapper mapper = mock(AppearancePatentCleanItemMapper.class);
|
||||
when(mapper.deleteByResultId(anyLong()))
|
||||
.thenAnswer(inv -> table.deleteByResultId(inv.getArgument(0, Long.class)));
|
||||
when(mapper.insertBatch(any()))
|
||||
.thenAnswer(inv -> table.insertBatch(inv.getArgument(0)));
|
||||
when(mapper.countCleanItems(any(), any(), any(), any(), any(), any()))
|
||||
.thenAnswer(inv -> table.count(inv.getArgument(0), inv.getArgument(1), inv.getArgument(2),
|
||||
inv.getArgument(3), inv.getArgument(4), inv.getArgument(5)));
|
||||
when(mapper.selectCleanItemPage(any(), any(), any(), any(), any(), any(), anyLong(), anyInt()))
|
||||
.thenAnswer(inv -> table.page(inv.getArgument(0), inv.getArgument(1), inv.getArgument(2),
|
||||
inv.getArgument(3), inv.getArgument(4), inv.getArgument(5),
|
||||
inv.getArgument(6, Long.class), inv.getArgument(7, Integer.class)));
|
||||
return mapper;
|
||||
}
|
||||
|
||||
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, String fileUrl) {
|
||||
FileResultEntity result = new FileResultEntity();
|
||||
result.setId(resultId);
|
||||
result.setTaskId(33001L);
|
||||
result.setModuleType("APPEARANCE_PATENT");
|
||||
result.setResultFileUrl(fileUrl);
|
||||
return result;
|
||||
}
|
||||
|
||||
private static AdminUserEntity user(long id, Long companyId) {
|
||||
AdminUserEntity user = new AdminUserEntity();
|
||||
user.setId(id);
|
||||
user.setCompanyId(companyId);
|
||||
return user;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user