test(phase-3): 3D 收口——publish 建单段 golden 落地,七支「先补再动」最后一格缺口闭合
PublishTaskService 一字未改。新落 PublishCreateTaskSnapshotTest(吃真入口
parseAndCreateTask)+ publish/golden/create-task-snapshot.txt:7 段场景由
7 支执行用例渲染,第 8 支是写档器(无 SHUFU_SNAPSHOT_WRITE 即 skip)。
补这支的理由:3D 迁移本体当时把 newTaskHeader/insertTask/newScopeRow 换成底座件,
等值证据只有「旧块/现块/底座件体做 setter 差集」——推理级证据,不是可复跑的防线。
本支在册两条快照都碰不到建单:PublishBatchLoadingSnapshotEquivTest 吃读侧
(history/dashboard/getTaskDetail/getTaskProgress),PublishSubmitResultSnapshotTest
吃 submitResult。
钉住的现网形状(逐段读 golden 得来,非推测):
1. 解析在事务外、落库在事务内:tx.begin→task.insert→file.insert→item.insertBatch
→result.insert→tx.commit,一次 execute();
2. 建单时刻一枚复用给头行/文件行/结果行(同一枚引用),而 insertRows 自己再取一次
now ⇒ 行项目 createdAt 与之不同——这条不对称由 itemCreatedAtEqualsTaskNow=false 钉住;
3. 文件解析失败不抛只标 FAILED,有一个可用任务仍 PENDING;全失败才 FAILED
+「全部文件解析或店铺匹配失败」+ finishedAt,且结果行照插(rowCount=0);
4. 计数与落库不同径:totalRows/rowCount 取 rows.size() 含 null,insertRows 跳 null;
0 行不调 insertBatch 但文件仍 PENDING、任务照样建起来;
5. 结果行 success 写 0(AP 留 NULL、ShopDataCrawl 写 -1,三种形状);
6. 行内品牌在落库那格过 BrandLabel.strip:"品牌: 耐克"→"耐克"、
"Brand: NIKE: X"→"NIKE: X"(只剥行首一枚标签)、全角冒号同样剥。
两处夹具自踩(都是「不判红就看不见的假证据」那一族):① 兜底 stub 若用 when(any())
注册在调用方 when("shop-a") 之后,Mockito 后注册者优先会把具体桩顶掉 ⇒ 匹配分流
收敛成一枚 thenAnswer;② tx.begin 整条不含空格,照 AP 那版 substring(0, indexOf(' '))
取事件名当场越界 ⇒ 补 eventKind 容错。
门禁:mvn -o test-compile rc=0;整类 9 例/0 红/1 skip;定向(Publish* 全面+
SourceScan/TaskModuleCoverage/CatchSilence/ArchitectureBoundary/LockSkip/
TaskStatusVocabulary/TaskLifecycleDebt/ForbiddenLogAudit/SqlLayering/
TaskServiceWiringSlice)205 例/0 红/2 skip;后端整量单跑 4616 例/0 红/0 错/
17 skipped,BUILD SUCCESS,真实 rc=0;golden 非写模式逐字节复现,
git status 仅本笔两件、其余快照一字未动。ops 面 ruff All checks passed。
真机启动验收本格仍欠(本机无 MySQL/Redis 监听,本笔零生产码改动)。
This commit is contained in:
+638
@@ -0,0 +1,638 @@
|
||||
package com.nanri.aiimage.modules.publish.service.support;
|
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import com.fasterxml.jackson.databind.json.JsonMapper;
|
||||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||
import cn.hutool.crypto.digest.DigestUtil;
|
||||
import com.nanri.aiimage.common.service.DistributedJobLockService;
|
||||
import com.nanri.aiimage.config.InstanceMetadata;
|
||||
import com.nanri.aiimage.modules.file.service.LocalFileStorageService;
|
||||
import com.nanri.aiimage.modules.file.service.oss.OssStorageService;
|
||||
import com.nanri.aiimage.modules.publish.mapper.PublishFileMapper;
|
||||
import com.nanri.aiimage.modules.publish.mapper.PublishItemMapper;
|
||||
import com.nanri.aiimage.modules.publish.model.dto.PublishParseRequest;
|
||||
import com.nanri.aiimage.modules.publish.model.dto.PublishRowDto;
|
||||
import com.nanri.aiimage.modules.publish.model.dto.PublishSourceFileDto;
|
||||
import com.nanri.aiimage.modules.publish.model.entity.PublishFileEntity;
|
||||
import com.nanri.aiimage.modules.publish.model.entity.PublishItemEntity;
|
||||
import com.nanri.aiimage.modules.publish.model.vo.PublishFileVo;
|
||||
import com.nanri.aiimage.modules.publish.model.vo.PublishParseVo;
|
||||
import com.nanri.aiimage.modules.publish.service.PublishTaskService;
|
||||
import com.nanri.aiimage.modules.publish.service.PublishWorkbookService;
|
||||
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.TaskScopeStateEntity;
|
||||
import com.nanri.aiimage.modules.task.service.TaskDistributedLockService;
|
||||
import com.nanri.aiimage.modules.task.service.TaskFileJobService;
|
||||
import com.nanri.aiimage.modules.task.service.TaskHeartbeatPositionService;
|
||||
import com.nanri.aiimage.modules.task.service.TaskProgressLightAssembler;
|
||||
import com.nanri.aiimage.modules.task.service.TransientPayloadStorageService;
|
||||
import com.nanri.aiimage.modules.ziniao.service.ZiniaoShopIndexService;
|
||||
import com.nanri.aiimage.modules.ziniao.service.ZiniaoShopSwitchService;
|
||||
import com.nanri.aiimage.modules.ziniao.model.vo.ZiniaoShopMatchResultVo;
|
||||
import org.apache.ibatis.builder.MapperBuilderAssistant;
|
||||
import org.apache.ibatis.session.Configuration;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.transaction.TransactionStatus;
|
||||
import org.springframework.transaction.support.TransactionCallback;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
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.Mockito.lenient;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* 上架(publish)**建单段**的现网行为快照——Phase 3 收口清单里那条「publish 建单段 golden 缺口」。
|
||||
*
|
||||
* <p><b>为什么这一格一直欠着。</b>本支在册两条快照里 {@code PublishBatchLoadingSnapshotEquivTest}
|
||||
* 吃的是读侧入口({@code history}/{@code dashboard}/{@code getTaskDetail}/{@code getTaskProgress}),
|
||||
* {@code PublishSubmitResultSnapshotTest} 吃的是回传入口 {@code submitResult};
|
||||
* <b>建单入口 {@code parseAndCreateTask} 一次都没进过</b>。3D 第三笔把
|
||||
* {@code newTaskHeader}/{@code insertTask}/{@code newScopeRow} 换成了底座件,
|
||||
* 当时的等值证据是「旧块/现块/底座件体的 setter 集合做差集」——那是推理级的证据,
|
||||
* 不是可复跑的防线。这一支把那段补成防线:以后谁再动建单段,流水当场出声。
|
||||
*
|
||||
* <p><b>快照吃的是事件流水</b>({@code tx.begin → task.insert → file.insert →
|
||||
* item.insertBatch → result.insert → tx.commit}),顺序换了即红。同时钉住六条本支特有的现网形状:
|
||||
* <ol>
|
||||
* <li><b>解析在事务外、落库在事务内</b>:{@code prepareFile} 全部跑完才开 {@code execute()},
|
||||
* 而 {@code execute} 返回 null 时抛「创建上架任务失败」——本支是七支里
|
||||
* {@code TransactionTemplate} 那一派(AppearancePatent/DeleteBrand 全程无事务)。</li>
|
||||
* <li><b>一枚建单时刻复用给三处、第四处不复用</b>:{@code now} 同时喂头行(底座六参
|
||||
* {@code newTaskHeader})、每个文件行、结果行(三处 {@code createdAtEqualsTaskNow=true},
|
||||
* 比的是同一枚引用而非大小);而 {@code insertRows} 自己再取一次 {@code now()} 给
|
||||
* {@code biz_publish_item}({@code itemCreatedAtEqualsTaskNow=false})。
|
||||
* 「时钟不进快照」指的是值不进,复用关系可以进——3C 那笔登记的五参版微差
|
||||
* (头行两列逐字相同)在本支不构成风险,本支走的正是显式时刻那一派。</li>
|
||||
* <li><b>文件解析失败不抛、只把该文件标 FAILED</b>:三种失败出口各有各的文案
|
||||
* (店铺名解析不出/店铺未匹配到紫鸟索引/匹配服务抛异常 ⇒ {@code matchStatus} 归 PENDING
|
||||
* + Excel 解析抛)。只要有一个文件可用,任务仍是 PENDING;<b>全失败</b>才直接 FAILED
|
||||
* 并写 {@code errorMessage}「全部文件解析或店铺匹配失败」+{@code finishedAt}。</li>
|
||||
* <li><b>计数与落库不同径</b>:{@code file.totalRows} 与 {@code result.rowCount} 取
|
||||
* {@code rows.size()}(<b>含 null 元素</b>),而 {@code insertRows} 逐行 {@code continue} 跳过 null
|
||||
* ⇒ 落库行数可以少于计数。0 行文件也不会调 {@code insertBatch}(空列表早退),
|
||||
* 但 0 行文件仍算「可处理」,任务照样建起来进队列。</li>
|
||||
* <li><b>结果行的 {@code success} 写 0</b>(不是 NULL、不是 -1):与 AppearancePatent 留 NULL、
|
||||
* ShopDataCrawl 的 -1 是三种形状 ⇒ 底座 {@code newScopeRow} 那两列刻意可空这件事,
|
||||
* 本支由调用点显式交 0。</li>
|
||||
* <li><b>行内品牌过 {@code BrandLabel.strip}</b>:清洗落在落库那一格而不是解析那一格,
|
||||
* 所以 golden 里 {@code brands=[...]} 就是 {@code biz_publish_item.brand} 的真值形状。</li>
|
||||
* </ol>
|
||||
*
|
||||
* <p><b>刻意不进快照的</b>:雪花 id({@code taskNo} 擦成 {@code <snowflake>})、
|
||||
* 时钟本身(只断复用关系,不比时刻值)。
|
||||
*
|
||||
* golden:src/test/resources/publish/golden/create-task-snapshot.txt
|
||||
* 重新生成:{@code SHUFU_SNAPSHOT_WRITE=true mvn -o test -Dtest=PublishCreateTaskSnapshotTest}
|
||||
* (环境变量而非 {@code -D}:pom 的 {@code <argLine>} 是显式配置,命令行覆盖不动它。
|
||||
* 这条 golden 只许在改动本支<b>之前</b>生成一次,此后它就是是非源。)
|
||||
*/
|
||||
class PublishCreateTaskSnapshotTest {
|
||||
|
||||
private static final File GOLDEN =
|
||||
new File("src/test/resources/publish/golden/create-task-snapshot.txt");
|
||||
private static final String MODULE_TYPE = "PUBLISH";
|
||||
|
||||
@BeforeAll
|
||||
static void initializeMybatisMetadata() {
|
||||
MapperBuilderAssistant assistant = new MapperBuilderAssistant(new Configuration(), "");
|
||||
TableInfoHelper.initTableInfo(assistant, FileTaskEntity.class);
|
||||
TableInfoHelper.initTableInfo(assistant, FileResultEntity.class);
|
||||
TableInfoHelper.initTableInfo(assistant, PublishFileEntity.class);
|
||||
TableInfoHelper.initTableInfo(assistant, PublishItemEntity.class);
|
||||
TableInfoHelper.initTableInfo(assistant, TaskScopeStateEntity.class);
|
||||
}
|
||||
|
||||
/** Spring 注入的那只 ObjectMapper ≠ {@code new ObjectMapper()}:Boot 挂 jsr310(Phase 0 实测教训)。 */
|
||||
private static ObjectMapper bootLikeObjectMapper() {
|
||||
return JsonMapper.builder()
|
||||
.addModule(new JavaTimeModule())
|
||||
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
|
||||
.build();
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// 被测服务的装配:19 枚实参顺序=构造器声明顺序(四处 new PublishTaskService(…) 同形)
|
||||
// ------------------------------------------------------------------
|
||||
|
||||
private static final class Harness {
|
||||
final List<String> events = new ArrayList<>();
|
||||
final LocalFileStorageService localFileStorageService = mock(LocalFileStorageService.class);
|
||||
final ZiniaoShopSwitchService ziniaoShopSwitchService = mock(ZiniaoShopSwitchService.class);
|
||||
final PublishWorkbookService workbookService = mock(PublishWorkbookService.class);
|
||||
final PublishFileMapper publishFileMapper = mock(PublishFileMapper.class);
|
||||
final PublishItemMapper publishItemMapper = mock(PublishItemMapper.class);
|
||||
final FileTaskMapper fileTaskMapper = mock(FileTaskMapper.class);
|
||||
final FileResultMapper fileResultMapper = mock(FileResultMapper.class);
|
||||
final TransactionTemplate transactionTemplate = mock(TransactionTemplate.class);
|
||||
final InstanceMetadata instanceMetadata = mock(InstanceMetadata.class);
|
||||
|
||||
/** 头行的建单时刻:用来断「同一枚 now 复用给文件行与结果行」,不比大小。 */
|
||||
LocalDateTime taskCreatedAt;
|
||||
/** 匹配服务的行为:null=索引里没有该店;抛异常=模拟网络/索引侧故障。 */
|
||||
ZiniaoShopMatchResultVo matchToReturn;
|
||||
java.util.function.Function<String, ZiniaoShopMatchResultVo> matchResolver;
|
||||
RuntimeException matchFailure;
|
||||
|
||||
PublishTaskService service() {
|
||||
lenient().when(instanceMetadata.getInstanceId()).thenReturn("node-A");
|
||||
// 一格 answer 而不是两枚 when:service() 里的兜底 stub 注册在调用方之后,
|
||||
// Mockito 按「后注册者优先」匹配,any() 那一枚会把调用方按名字打的具体桩顶掉。
|
||||
lenient().when(ziniaoShopSwitchService.findIndexedStoreByName(any(), org.mockito.ArgumentMatchers.anyBoolean()))
|
||||
.thenAnswer(invocation -> {
|
||||
if (matchFailure != null) {
|
||||
throw matchFailure;
|
||||
}
|
||||
String shopName = invocation.getArgument(0);
|
||||
return matchResolver != null ? matchResolver.apply(shopName) : matchToReturn;
|
||||
});
|
||||
lenient().when(transactionTemplate.execute(any())).thenAnswer(invocation -> {
|
||||
TransactionCallback<?> callback = invocation.getArgument(0);
|
||||
events.add("tx.begin");
|
||||
Object value = callback.doInTransaction(mock(TransactionStatus.class));
|
||||
events.add("tx.commit");
|
||||
return value;
|
||||
});
|
||||
doAnswer("task");
|
||||
doAnswer("file");
|
||||
doAnswer("item");
|
||||
doAnswer("result");
|
||||
return new PublishTaskService(
|
||||
localFileStorageService,
|
||||
ziniaoShopSwitchService,
|
||||
workbookService,
|
||||
publishFileMapper,
|
||||
publishItemMapper,
|
||||
fileTaskMapper,
|
||||
fileResultMapper,
|
||||
mock(TaskChunkMapper.class),
|
||||
mock(TaskScopeStateMapper.class),
|
||||
mock(TaskFileJobService.class),
|
||||
mock(TaskDistributedLockService.class),
|
||||
mock(TransientPayloadStorageService.class),
|
||||
mock(OssStorageService.class),
|
||||
bootLikeObjectMapper(),
|
||||
transactionTemplate,
|
||||
mock(DistributedJobLockService.class),
|
||||
instanceMetadata,
|
||||
mock(TaskProgressLightAssembler.class),
|
||||
mock(TaskHeartbeatPositionService.class));
|
||||
}
|
||||
|
||||
private void doAnswer(String which) {
|
||||
switch (which) {
|
||||
case "task" -> lenient().when(fileTaskMapper.insert(any(FileTaskEntity.class)))
|
||||
.thenAnswer(invocation -> {
|
||||
FileTaskEntity task = invocation.getArgument(0);
|
||||
task.setId(1001L);
|
||||
taskCreatedAt = task.getCreatedAt();
|
||||
events.add("task.insert taskNo=" + maskTaskNo(task.getTaskNo())
|
||||
+ " moduleType=" + task.getModuleType()
|
||||
+ " status=" + task.getStatus()
|
||||
+ " taskMode=" + task.getTaskMode()
|
||||
+ " sourceFileCount=" + task.getSourceFileCount()
|
||||
+ " successFileCount=" + task.getSuccessFileCount()
|
||||
+ " failedFileCount=" + task.getFailedFileCount()
|
||||
+ " userId=" + task.getUserId()
|
||||
+ " createdBy=" + task.getCreatedBy()
|
||||
+ " deviceId=" + task.getDeviceId()
|
||||
+ " errorMessage=" + task.getErrorMessage()
|
||||
+ " finishedAtSet=" + (task.getFinishedAt() != null)
|
||||
+ " requestJson.keys=" + jsonKeys(task.getRequestJson())
|
||||
+ " requestJson.digest=" + digest12(String.valueOf(task.getRequestJson()))
|
||||
+ " resultJson=" + task.getResultJson());
|
||||
return 1;
|
||||
});
|
||||
case "file" -> lenient().when(publishFileMapper.insert(any(PublishFileEntity.class)))
|
||||
.thenAnswer(invocation -> {
|
||||
PublishFileEntity file = invocation.getArgument(0);
|
||||
file.setId(2001L + fileSeq++);
|
||||
events.add("file.insert taskId=" + file.getTaskId()
|
||||
+ " fileId=" + file.getId()
|
||||
+ " fileKey=" + file.getFileKey()
|
||||
+ " sourceFilename=" + file.getSourceFilename()
|
||||
+ " relativePath=" + file.getRelativePath()
|
||||
+ " shopName=" + file.getShopName()
|
||||
+ " matched=" + file.getMatched()
|
||||
+ " shopId=" + file.getShopId()
|
||||
+ " matchedUserId=" + file.getMatchedUserId()
|
||||
+ " platform=" + file.getPlatform()
|
||||
+ " companyName=" + file.getCompanyName()
|
||||
+ " matchStatus=" + file.getMatchStatus()
|
||||
+ " matchMessage=" + file.getMatchMessage()
|
||||
+ " status=" + file.getStatus()
|
||||
+ " totalRows=" + file.getTotalRows()
|
||||
+ " processedRows=" + file.getProcessedRows()
|
||||
+ " errorMessage=" + file.getErrorMessage()
|
||||
+ " finishedAtSet=" + (file.getFinishedAt() != null)
|
||||
+ " createdAtEqualsTaskNow=" + (file.getCreatedAt() == taskCreatedAt));
|
||||
return 1;
|
||||
});
|
||||
case "item" -> lenient().when(publishItemMapper.insertBatch(any())).thenAnswer(invocation -> {
|
||||
List<PublishItemEntity> rows = invocation.getArgument(0);
|
||||
StringBuilder rowIndexes = new StringBuilder();
|
||||
StringBuilder asins = new StringBuilder();
|
||||
StringBuilder brands = new StringBuilder();
|
||||
for (PublishItemEntity row : rows) {
|
||||
rowIndexes.append(rowIndexes.length() == 0 ? "" : ",").append(row.getRowIndex());
|
||||
asins.append(asins.length() == 0 ? "" : ",").append(row.getAsin());
|
||||
brands.append(brands.length() == 0 ? "" : ",").append(row.getBrand());
|
||||
if (!rows.isEmpty()) {
|
||||
// 同一批内 createdAt 必须也是那一枚建单时刻(insertRows 用的是自己的 now,见下条读数)
|
||||
firstItemCreatedAt = row.getCreatedAt();
|
||||
}
|
||||
}
|
||||
events.add("item.insertBatch count=" + rows.size()
|
||||
+ " taskId=" + (rows.isEmpty() ? "null" : rows.get(0).getTaskId())
|
||||
+ " fileId=" + (rows.isEmpty() ? "null" : rows.get(0).getFileId())
|
||||
+ " rowIndex=[" + rowIndexes + "]"
|
||||
+ " asins=[" + asins + "]"
|
||||
+ " brands=[" + brands + "]"
|
||||
+ " itemCreatedAtEqualsTaskNow=" + (firstItemCreatedAt == taskCreatedAt));
|
||||
return rows.size();
|
||||
});
|
||||
default -> lenient().when(fileResultMapper.insert(any(FileResultEntity.class)))
|
||||
.thenAnswer(invocation -> {
|
||||
FileResultEntity result = invocation.getArgument(0);
|
||||
result.setId(3001L);
|
||||
events.add("result.insert taskId=" + result.getTaskId()
|
||||
+ " moduleType=" + result.getModuleType()
|
||||
+ " sourceFilename=" + result.getSourceFilename()
|
||||
+ " sourceFileUrl=" + result.getSourceFileUrl()
|
||||
+ " resultContentType=" + result.getResultContentType()
|
||||
+ " rowCount=" + result.getRowCount()
|
||||
+ " success=" + result.getSuccess()
|
||||
+ " errorMessage=" + result.getErrorMessage()
|
||||
+ " userId=" + result.getUserId()
|
||||
+ " createdAtEqualsTaskNow=" + (result.getCreatedAt() == taskCreatedAt));
|
||||
return 1;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private int fileSeq;
|
||||
private LocalDateTime firstItemCreatedAt;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// 夹具
|
||||
// ------------------------------------------------------------------
|
||||
|
||||
private static File existingFile() throws Exception {
|
||||
File file = File.createTempFile("publish-create-task-", ".xlsx");
|
||||
file.deleteOnExit();
|
||||
return file;
|
||||
}
|
||||
|
||||
private static ZiniaoShopMatchResultVo matchedVo() {
|
||||
ZiniaoShopMatchResultVo match = new ZiniaoShopMatchResultVo();
|
||||
match.setMatched(true);
|
||||
match.setShopId("shop-9");
|
||||
match.setShopName("shop-a");
|
||||
match.setCompanyName("ACME 有限公司");
|
||||
match.setPlatform("amazon");
|
||||
match.setMatchedUserId(42L);
|
||||
match.setMatchStatus(ZiniaoShopIndexService.MATCH_STATUS_MATCHED);
|
||||
match.setMatchMessage("");
|
||||
return match;
|
||||
}
|
||||
|
||||
private static PublishRowDto row(String sourceId, String asin, String brand) {
|
||||
PublishRowDto row = new PublishRowDto();
|
||||
row.setSourceId(sourceId);
|
||||
row.setAsin(asin);
|
||||
row.setCountry("US");
|
||||
row.setBrand(brand);
|
||||
row.setPrice("19.90");
|
||||
row.setStatus("在售");
|
||||
row.setSyncStatus(null);
|
||||
row.setSyncCountries(null);
|
||||
return row;
|
||||
}
|
||||
|
||||
private static PublishSourceFileDto source(String fileKey, String originalFilename, String relativePath) {
|
||||
PublishSourceFileDto dto = new PublishSourceFileDto();
|
||||
dto.setFileKey(fileKey);
|
||||
dto.setOriginalFilename(originalFilename);
|
||||
dto.setRelativePath(relativePath);
|
||||
return dto;
|
||||
}
|
||||
|
||||
private static PublishParseRequest request(Long userId, List<PublishSourceFileDto> files, String country) {
|
||||
PublishParseRequest request = new PublishParseRequest();
|
||||
request.setUserId(userId);
|
||||
request.setFiles(files);
|
||||
request.setPublishCountry(country);
|
||||
return request;
|
||||
}
|
||||
|
||||
private static Harness harnessWithFile(String fileKey, List<PublishRowDto> rows) throws Exception {
|
||||
Harness harness = new Harness();
|
||||
harness.matchToReturn = matchedVo();
|
||||
File file = existingFile();
|
||||
when(harness.localFileStorageService.findLocalSourceFile(fileKey)).thenReturn(file);
|
||||
when(harness.workbookService.parse(file)).thenReturn(new PublishWorkbookService.ParsedWorkbook(rows));
|
||||
return harness;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// 读数整形:只擦不稳定量,不改结构
|
||||
// ------------------------------------------------------------------
|
||||
|
||||
private static String maskTaskNo(String taskNo) {
|
||||
int idx = taskNo == null ? -1 : taskNo.lastIndexOf('-');
|
||||
return idx < 0 ? String.valueOf(taskNo) : taskNo.substring(0, idx) + "-<snowflake>";
|
||||
}
|
||||
|
||||
private static String digest12(String value) {
|
||||
return DigestUtil.sha256Hex(value).substring(0, 12);
|
||||
}
|
||||
|
||||
/** JSON 只取顶层键序+内容摘要:golden 要灵敏于变化,又不能变成一堵 JSON 墙。 */
|
||||
private static String jsonKeys(String json) {
|
||||
if (json == null || json.isBlank()) {
|
||||
return "[]";
|
||||
}
|
||||
List<String> keys = new ArrayList<>();
|
||||
Matcher matcher = Pattern.compile("\"([^\"]+)\"\\s*:").matcher(json);
|
||||
while (matcher.find()) {
|
||||
keys.add(matcher.group(1));
|
||||
}
|
||||
return keys.isEmpty() ? "[<scalar>]" : "[" + String.join(",", keys) + "]";
|
||||
}
|
||||
|
||||
private static String describe(String label, Runnable action) {
|
||||
try {
|
||||
action.run();
|
||||
return label + " -> 未抛异常";
|
||||
} catch (Exception ex) {
|
||||
return label + " -> " + ex.getClass().getSimpleName() + ": " + ex.getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
private static String renderEvents(Harness harness) {
|
||||
return String.join("\n", harness.events);
|
||||
}
|
||||
|
||||
/**
|
||||
* 事件名前缀:{@code tx.begin}/{@code tx.commit} 这两格<b>没有参数</b>、整条不含空格,
|
||||
* 照 AP 那版直接 {@code substring(0, indexOf(' '))} 会抛越界(本件第一次跑就是这么红的)。
|
||||
*/
|
||||
private static String eventKind(String event) {
|
||||
int idx = event.indexOf(' ');
|
||||
return idx < 0 ? event : event.substring(0, idx);
|
||||
}
|
||||
|
||||
private static String renderVo(PublishParseVo vo) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("vo: taskId=").append(vo.getTaskId())
|
||||
.append(" taskNo=").append(maskTaskNo(vo.getTaskNo()))
|
||||
.append(" sourceFileCount=").append(vo.getSourceFileCount())
|
||||
.append(" totalRows=").append(vo.getTotalRows())
|
||||
.append(" pageSize=").append(vo.getPageSize())
|
||||
.append('\n');
|
||||
for (PublishFileVo file : vo.getFiles()) {
|
||||
sb.append(" vo.file fileId=").append(file.getFileId())
|
||||
.append(" fileKey=").append(file.getFileKey())
|
||||
.append(" shopName=").append(file.getShopName())
|
||||
.append(" matched=").append(file.isMatched())
|
||||
.append(" status=").append(file.getStatus())
|
||||
.append(" totalRows=").append(file.getTotalRows())
|
||||
.append(" percent=").append(file.getPercent())
|
||||
.append(" errorMessage=").append(file.getErrorMessage())
|
||||
.append('\n');
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// 各场景(golden 的每一节)
|
||||
// ------------------------------------------------------------------
|
||||
|
||||
private static String caseSingleFile() throws Exception {
|
||||
Harness harness = harnessWithFile("uploads/shop-a.xlsx", List.of(
|
||||
row("1", "B001", "品牌: 耐克"),
|
||||
row("2", "B002", "Adidas")));
|
||||
PublishParseVo vo = harness.service().parseAndCreateTask(
|
||||
request(7L, List.of(source("uploads/shop-a.xlsx", "shop-a.xlsx", "shop-a.xlsx")), "US"),
|
||||
"device-1");
|
||||
return "== 单文件建单:匹配成功、解析两行 ==\n"
|
||||
+ renderEvents(harness) + "\n" + renderVo(vo);
|
||||
}
|
||||
|
||||
private static String caseTwoFilesAggregate() throws Exception {
|
||||
Harness harness = new Harness();
|
||||
harness.matchToReturn = matchedVo();
|
||||
File first = existingFile();
|
||||
File second = existingFile();
|
||||
when(harness.localFileStorageService.findLocalSourceFile("uploads/a.xlsx")).thenReturn(first);
|
||||
when(harness.localFileStorageService.findLocalSourceFile("uploads/b.xlsx")).thenReturn(second);
|
||||
when(harness.workbookService.parse(first))
|
||||
.thenReturn(new PublishWorkbookService.ParsedWorkbook(List.of(row("1", "B001", "Nike"))));
|
||||
when(harness.workbookService.parse(second))
|
||||
.thenReturn(new PublishWorkbookService.ParsedWorkbook(List.of(row("2", "B002", "Puma"), row("3", "B003", "Asics"))));
|
||||
PublishParseVo vo = harness.service().parseAndCreateTask(request(7L, List.of(
|
||||
source("uploads/a.xlsx", "a.xlsx", "a.xlsx"),
|
||||
source("uploads/b.xlsx", "b.xlsx", "b.xlsx")), "DE"), " device-2 ");
|
||||
return "== 双文件聚合:结果类型换 zip、URL 取首文件、行号逐文件重计 ==\n"
|
||||
+ renderEvents(harness) + "\n" + renderVo(vo);
|
||||
}
|
||||
|
||||
private static String casePartialFailure() throws Exception {
|
||||
Harness harness = new Harness();
|
||||
harness.matchResolver = name -> "shop-a".equals(name) ? matchedVo() : null;
|
||||
File okFile = existingFile();
|
||||
when(harness.localFileStorageService.findLocalSourceFile("uploads/a.xlsx")).thenReturn(okFile);
|
||||
when(harness.workbookService.parse(okFile))
|
||||
.thenReturn(new PublishWorkbookService.ParsedWorkbook(List.of(row("1", "B001", "Nike"))));
|
||||
PublishParseVo vo = harness.service().parseAndCreateTask(request(7L, List.of(
|
||||
source("uploads/a.xlsx", "shop-a.xlsx", "a.xlsx"),
|
||||
source("uploads/b.xlsx", "shop-b.xlsx", "b.xlsx")), "US"), null);
|
||||
return "== 店铺未匹配:文件行 FAILED 但任务仍 PENDING(有一个可用就进队列) ==\n"
|
||||
+ renderEvents(harness) + "\n" + renderVo(vo);
|
||||
}
|
||||
|
||||
private static String caseAllFilesFailed() throws Exception {
|
||||
Harness harness = new Harness();
|
||||
harness.matchFailure = new IllegalStateException("紫鸟索引不可用");
|
||||
PublishParseVo vo = harness.service().parseAndCreateTask(request(7L, List.of(
|
||||
source("uploads/a.xlsx", "shop-a.xlsx", "a.xlsx")), "US"), "d");
|
||||
return "== 匹配服务抛异常:全文件失败 ⇒ 任务直接 FAILED 并封口 ==\n"
|
||||
+ renderEvents(harness) + "\n" + renderVo(vo);
|
||||
}
|
||||
|
||||
private static String caseZeroRowFileStillQueued() throws Exception {
|
||||
Harness harness = harnessWithFile("uploads/empty.xlsx", List.of());
|
||||
PublishParseVo vo = harness.service().parseAndCreateTask(
|
||||
request(7L, List.of(source("uploads/empty.xlsx", "shop-a.xlsx", "empty.xlsx")), "US"), "d");
|
||||
return "== 解析出 0 行:不调 insertBatch,但文件仍 PENDING、任务仍建起来 ==\n"
|
||||
+ renderEvents(harness) + "\n" + renderVo(vo);
|
||||
}
|
||||
|
||||
private static String caseNullRowCountedButNotInserted() throws Exception {
|
||||
Harness harness = harnessWithFile("uploads/null-row.xlsx",
|
||||
Arrays.asList(row("1", "B001", "Brand: NIKE: X"), null, row("2", "B002", "品牌:李宁")));
|
||||
PublishParseVo vo = harness.service().parseAndCreateTask(
|
||||
request(7L, List.of(source("uploads/null-row.xlsx", "shop-a.xlsx", "n.xlsx")), "US"), "d");
|
||||
return "== 行含 null:计数含 null 而落库跳 null,品牌落库前过 BrandLabel.strip ==\n"
|
||||
+ renderEvents(harness) + "\n" + renderVo(vo);
|
||||
}
|
||||
|
||||
private static String caseRejectionsTouchNothing() throws Exception {
|
||||
StringBuilder sb = new StringBuilder("== 建单被拒的七格:事务未开、一次库都不碰 ==\n");
|
||||
Harness blankKey = new Harness();
|
||||
blankKey.matchToReturn = matchedVo();
|
||||
File realFile = existingFile();
|
||||
when(blankKey.localFileStorageService.findLocalSourceFile("uploads/dup.xlsx")).thenReturn(realFile);
|
||||
when(blankKey.workbookService.parse(realFile))
|
||||
.thenReturn(new PublishWorkbookService.ParsedWorkbook(List.of(row("1", "B001", "Nike"))));
|
||||
sb.append(describe("request=null", () -> new Harness().service().parseAndCreateTask(null, "d"))).append('\n');
|
||||
sb.append(describe("userId=null", () -> new Harness().service()
|
||||
.parseAndCreateTask(request(null, List.of(source("k", "k.xlsx", "k")), "US"), "d"))).append('\n');
|
||||
sb.append(describe("userId=0", () -> new Harness().service()
|
||||
.parseAndCreateTask(request(0L, List.of(source("k", "k.xlsx", "k")), "US"), "d"))).append('\n');
|
||||
sb.append(describe("files=null", () -> new Harness().service()
|
||||
.parseAndCreateTask(request(7L, null, "US"), "d"))).append('\n');
|
||||
sb.append(describe("files=空", () -> new Harness().service()
|
||||
.parseAndCreateTask(request(7L, List.of(), "US"), "d"))).append('\n');
|
||||
sb.append(describe("publishCountry=空白", () -> new Harness().service()
|
||||
.parseAndCreateTask(request(7L, List.of(source("k", "k.xlsx", "k")), " "), "d"))).append('\n');
|
||||
sb.append(describe("fileKey 重复", () -> blankKey.service()
|
||||
.parseAndCreateTask(request(7L, List.of(
|
||||
source("uploads/dup.xlsx", "shop-a.xlsx", "d.xlsx"),
|
||||
source("uploads/dup.xlsx", "shop-a.xlsx", "d.xlsx")), "US"), "d"))).append('\n');
|
||||
sb.append("events=").append(blankKey.events.size())
|
||||
.append("(重复 fileKey 那格已 prepare 过首文件,但落库与事务都未开始)\n");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private static String renderAll() throws Exception {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(caseSingleFile()).append('\n');
|
||||
sb.append(caseTwoFilesAggregate()).append('\n');
|
||||
sb.append(casePartialFailure()).append('\n');
|
||||
sb.append(caseAllFilesFailed()).append('\n');
|
||||
sb.append(caseZeroRowFileStillQueued()).append('\n');
|
||||
sb.append(caseNullRowCountedButNotInserted()).append('\n');
|
||||
sb.append(caseRejectionsTouchNothing());
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private static String read(File file) throws Exception {
|
||||
return Files.readString(file.toPath(), StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// 用例
|
||||
// ------------------------------------------------------------------
|
||||
|
||||
@Test
|
||||
void 建单段落库流水与golden一致() throws Exception {
|
||||
assertEquals(read(GOLDEN), renderAll(), "建单段事件流水必须与 golden 逐字节相等");
|
||||
}
|
||||
|
||||
@Test
|
||||
void 副作用顺序是解析在事务外落库在事务内() throws Exception {
|
||||
Harness harness = harnessWithFile("uploads/shop-a.xlsx", List.of(row("1", "B001", "Nike")));
|
||||
harness.service().parseAndCreateTask(
|
||||
request(7L, List.of(source("uploads/shop-a.xlsx", "shop-a.xlsx", "a.xlsx")), "US"), "d");
|
||||
assertEquals(List.of("tx.begin", "task.insert", "file.insert", "item.insertBatch", "result.insert", "tx.commit"),
|
||||
harness.events.stream().map(PublishCreateTaskSnapshotTest::eventKind).toList(),
|
||||
"头行→文件行→行批插→结果行,整段在一次 execute() 里");
|
||||
}
|
||||
|
||||
@Test
|
||||
void 同一枚建单时刻复用给头行文件行与结果行() throws Exception {
|
||||
Harness harness = harnessWithFile("uploads/shop-a.xlsx", List.of(row("1", "B001", "Nike")));
|
||||
harness.service().parseAndCreateTask(
|
||||
request(7L, List.of(source("uploads/shop-a.xlsx", "shop-a.xlsx", "a.xlsx")), "US"), "d");
|
||||
assertTrue(harness.events.stream().anyMatch(e -> e.startsWith("file.insert") && e.contains("createdAtEqualsTaskNow=true")),
|
||||
"文件行的 createdAt 必须就是底座头行那枚 now");
|
||||
assertTrue(harness.events.stream().anyMatch(e -> e.startsWith("result.insert") && e.contains("createdAtEqualsTaskNow=true")),
|
||||
"结果行的 createdAt 必须就是底座头行那枚 now");
|
||||
assertTrue(harness.events.stream().anyMatch(e -> e.contains("itemCreatedAtEqualsTaskNow=false")),
|
||||
"insertRows 自己再取一次 now,行项目的 createdAt 与建单时刻不同一枚——"
|
||||
+ "这条现网不对称由本断言钉住,谁顺手统一就必须先过这里");
|
||||
}
|
||||
|
||||
@Test
|
||||
void 全失败才把任务直接判FAILED并封口() throws Exception {
|
||||
Harness harness = new Harness();
|
||||
harness.matchFailure = new IllegalStateException("紫鸟索引不可用");
|
||||
harness.service().parseAndCreateTask(
|
||||
request(7L, List.of(source("uploads/a.xlsx", "shop-a.xlsx", "a.xlsx")), "US"), "d");
|
||||
assertTrue(harness.events.stream().anyMatch(e -> e.startsWith("task.insert") && e.contains(" status=FAILED")),
|
||||
"无可用文件时任务不再进队列:" + harness.events);
|
||||
assertTrue(harness.events.stream().anyMatch(e -> e.contains("全部文件解析或店铺匹配失败")));
|
||||
assertTrue(harness.events.stream().anyMatch(e -> e.startsWith("result.insert") && e.contains(" rowCount=0")),
|
||||
"结果行仍然 insert,只是零行");
|
||||
}
|
||||
|
||||
@Test
|
||||
void 非法建单一次库都不碰() {
|
||||
Harness harness = new Harness();
|
||||
assertThrows(com.nanri.aiimage.common.exception.BusinessException.class,
|
||||
() -> harness.service().parseAndCreateTask(request(null, List.of(source("k", "k.xlsx", "k")), "US"), "d"));
|
||||
assertTrue(harness.events.isEmpty(), "user_id 不合法时不得开事务、不得落库");
|
||||
}
|
||||
|
||||
@Test
|
||||
void golden文件已入库且非空() {
|
||||
assertTrue(GOLDEN.isFile(), "golden 必须入库: " + GOLDEN.getAbsolutePath());
|
||||
assertTrue(GOLDEN.length() > 0, "golden 非空");
|
||||
}
|
||||
|
||||
@Test
|
||||
void 篡改golden必须判红() throws Exception {
|
||||
String original = read(GOLDEN);
|
||||
assertTrue(original.contains("== 单文件建单"), "golden 内容合法");
|
||||
try {
|
||||
Files.writeString(GOLDEN.toPath(), original + "\n# tampered", StandardCharsets.UTF_8);
|
||||
AssertionError failure = null;
|
||||
try {
|
||||
assertEquals(read(GOLDEN), renderAll(), "篡改后应与 golden 不一致");
|
||||
} catch (AssertionError ex) {
|
||||
failure = ex;
|
||||
}
|
||||
assertNotNull(failure, "篡改 golden 后断言必须失败,否则这支是恒绿的");
|
||||
} finally {
|
||||
Files.writeString(GOLDEN.toPath(), original, StandardCharsets.UTF_8);
|
||||
}
|
||||
assertEquals(original, read(GOLDEN), "恢复原始 golden");
|
||||
}
|
||||
|
||||
@Test
|
||||
void 同一夹具跑两次流水一致() throws Exception {
|
||||
assertEquals(renderAll(), renderAll(), "快照必须可复现(雪花 id 与时钟值已擦除)");
|
||||
}
|
||||
|
||||
/** 生成 golden 用:{@code SHUFU_SNAPSHOT_WRITE=true} 时把当前流水写回文件。 */
|
||||
@Test
|
||||
@org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable(named = "SHUFU_SNAPSHOT_WRITE", matches = "true")
|
||||
void 重新生成golden() throws Exception {
|
||||
Files.createDirectories(GOLDEN.toPath().getParent());
|
||||
Files.writeString(GOLDEN.toPath(), renderAll(), StandardCharsets.UTF_8);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user