完成上架相关优化、oss迁移
This commit is contained in:
+77
-2
@@ -4,12 +4,16 @@ import com.baomidou.mybatisplus.core.MybatisConfiguration;
|
||||
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.common.exception.TaskOwnerMismatchException;
|
||||
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.PublishResultFileDto;
|
||||
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.dto.PublishSubmitResultRequest;
|
||||
import com.nanri.aiimage.modules.publish.model.entity.PublishFileEntity;
|
||||
import com.nanri.aiimage.modules.publish.model.entity.PublishItemEntity;
|
||||
@@ -20,6 +24,7 @@ import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity;
|
||||
import com.nanri.aiimage.modules.task.service.TaskDistributedLockService;
|
||||
import com.nanri.aiimage.modules.task.service.TaskFileJobService;
|
||||
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.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
@@ -29,9 +34,11 @@ import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.transaction.TransactionStatus;
|
||||
import org.springframework.transaction.support.TransactionCallback;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
@@ -42,6 +49,7 @@ 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.isNull;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.lenient;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
@@ -74,11 +82,17 @@ class PublishTaskServiceTest {
|
||||
@Mock private OssStorageService ossStorageService;
|
||||
@Mock private ObjectMapper objectMapper;
|
||||
@Mock private TransactionTemplate transactionTemplate;
|
||||
@Mock private InstanceMetadata instanceMetadata;
|
||||
|
||||
@InjectMocks private PublishTaskService service;
|
||||
|
||||
@BeforeEach
|
||||
void executeTransactionsInline() {
|
||||
lenient().when(instanceMetadata.getInstanceId()).thenReturn("instance-a");
|
||||
lenient().when(transactionTemplate.execute(any())).thenAnswer(invocation -> {
|
||||
TransactionCallback<?> callback = invocation.getArgument(0);
|
||||
return callback.doInTransaction(null);
|
||||
});
|
||||
lenient().doAnswer(invocation -> {
|
||||
Consumer<TransactionStatus> callback = invocation.getArgument(0);
|
||||
callback.accept(null);
|
||||
@@ -86,6 +100,65 @@ class PublishTaskServiceTest {
|
||||
}).when(transactionTemplate).executeWithoutResult(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseStoresCurrentInstanceAsTaskOwner() throws Exception {
|
||||
PublishParseRequest request = new PublishParseRequest();
|
||||
request.setUserId(7L);
|
||||
request.setPublishCountry("DE");
|
||||
PublishSourceFileDto source = new PublishSourceFileDto();
|
||||
source.setFileKey("uploads/shop.xlsx");
|
||||
source.setOriginalFilename("shop.xlsx");
|
||||
request.setFiles(List.of(source));
|
||||
|
||||
File localFile = mock(File.class);
|
||||
when(localFile.exists()).thenReturn(true);
|
||||
when(localFileStorageService.findLocalSourceFile(source.getFileKey())).thenReturn(localFile);
|
||||
when(ziniaoShopSwitchService.findIndexedStoreByName("shop", false)).thenReturn(
|
||||
new ZiniaoShopMatchResultVo(true, "shop-1", "shop", "company", "Amazon",
|
||||
7L, null, "MATCHED", null));
|
||||
when(workbookService.parse(localFile)).thenReturn(
|
||||
new PublishWorkbookService.ParsedWorkbook(List.of(row("1"))));
|
||||
ObjectMapper realObjectMapper = new ObjectMapper();
|
||||
when(objectMapper.writeValueAsString(any())).thenAnswer(invocation ->
|
||||
realObjectMapper.writeValueAsString(invocation.getArgument(0)));
|
||||
doAnswer(invocation -> {
|
||||
FileTaskEntity inserted = invocation.getArgument(0);
|
||||
inserted.setId(501L);
|
||||
return 1;
|
||||
}).when(fileTaskMapper).insert(any(FileTaskEntity.class));
|
||||
doAnswer(invocation -> {
|
||||
PublishFileEntity inserted = invocation.getArgument(0);
|
||||
inserted.setId(601L);
|
||||
return 1;
|
||||
}).when(publishFileMapper).insert(any(PublishFileEntity.class));
|
||||
|
||||
service.parseAndCreateTask(request);
|
||||
|
||||
org.mockito.ArgumentCaptor<FileTaskEntity> taskCaptor =
|
||||
org.mockito.ArgumentCaptor.forClass(FileTaskEntity.class);
|
||||
verify(fileTaskMapper).insert(taskCaptor.capture());
|
||||
assertEquals("instance-a", realObjectMapper.readTree(
|
||||
taskCaptor.getValue().getResultJson()).path("ownerInstanceId").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void taskAccessRejectsAnotherInstanceForExistingRouterToForward() throws Exception {
|
||||
long taskId = 109L;
|
||||
FileTaskEntity task = task(taskId, 7L, "RUNNING");
|
||||
task.setResultJson("{\"ownerInstanceId\":\"instance-b\"}");
|
||||
when(fileTaskMapper.selectById(taskId)).thenReturn(task);
|
||||
when(objectMapper.readTree(task.getResultJson())).thenReturn(
|
||||
new ObjectMapper().readTree(task.getResultJson()));
|
||||
|
||||
TaskOwnerMismatchException error = assertThrows(TaskOwnerMismatchException.class,
|
||||
() -> service.getTaskDetail(taskId, 7L));
|
||||
|
||||
assertEquals(taskId, error.getTaskId());
|
||||
assertEquals("instance-b", error.getOwnerInstanceId());
|
||||
assertEquals("instance-a", error.getCurrentInstanceId());
|
||||
verifyNoInteractions(publishItemMapper);
|
||||
}
|
||||
|
||||
@Test
|
||||
void resultCallbackRejectsAnotherUserBeforeReadingOrReplacingRows() {
|
||||
long taskId = 101L;
|
||||
@@ -180,7 +253,8 @@ class PublishTaskServiceTest {
|
||||
|
||||
assertEquals("SUCCESS", file.getStatus());
|
||||
verify(taskFileJobService).enqueueAssembleResult(
|
||||
taskId, PublishTaskService.MODULE_TYPE, resultId, "task:" + taskId);
|
||||
taskId, PublishTaskService.MODULE_TYPE, resultId,
|
||||
"task:" + taskId + ":owner:instance-a");
|
||||
verify(lock).close();
|
||||
}
|
||||
|
||||
@@ -265,7 +339,8 @@ class PublishTaskServiceTest {
|
||||
verify(taskFileJobService, times(2))
|
||||
.countUnfinishedAssembleJobs(taskId, PublishTaskService.MODULE_TYPE);
|
||||
verify(taskFileJobService).enqueueAssembleResult(
|
||||
taskId, PublishTaskService.MODULE_TYPE, resultId, "task:" + taskId);
|
||||
taskId, PublishTaskService.MODULE_TYPE, resultId,
|
||||
"task:" + taskId + ":owner:instance-a");
|
||||
verify(fileTaskMapper).updateById(task);
|
||||
verify(fileResultMapper).updateById(result);
|
||||
verify(lock).close();
|
||||
|
||||
+20
-10
@@ -22,6 +22,7 @@ import java.util.zip.ZipFile;
|
||||
|
||||
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.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@@ -30,13 +31,22 @@ class PublishWorkbookServiceTest {
|
||||
private final PublishWorkbookService service = new PublishWorkbookService();
|
||||
|
||||
@Test
|
||||
void parsesOnlyTheExactEightHeadersAcrossNonEmptySheets() throws Exception {
|
||||
void parsesOnlyFirstFiveColumnsAcrossNonEmptySheets() throws Exception {
|
||||
Path directory = Files.createTempDirectory("publish-parse-");
|
||||
try {
|
||||
File valid = directory.resolve("valid.xlsx").toFile();
|
||||
try (Workbook workbook = new XSSFWorkbook();
|
||||
FileOutputStream output = new FileOutputStream(valid)) {
|
||||
writeSourceSheet(workbook.createSheet("英国数据"), "英国", "B001");
|
||||
Sheet uk = workbook.createSheet("英国数据");
|
||||
writeSourceSheet(uk, "英国", "B001");
|
||||
for (int index = PublishWorkbookService.SOURCE_HEADERS.size();
|
||||
index < PublishWorkbookService.RESULT_HEADERS.size(); index++) {
|
||||
uk.getRow(0).createCell(index)
|
||||
.setCellValue(PublishWorkbookService.RESULT_HEADERS.get(index));
|
||||
}
|
||||
uk.getRow(1).createCell(5).setCellValue("旧状态");
|
||||
uk.getRow(1).createCell(6).setCellValue("旧同步状态");
|
||||
uk.getRow(1).createCell(7).setCellValue("旧同步国家");
|
||||
workbook.createSheet("空白页");
|
||||
writeSourceSheet(workbook.createSheet("德国数据"), "DE", "B002");
|
||||
workbook.write(output);
|
||||
@@ -45,6 +55,9 @@ class PublishWorkbookServiceTest {
|
||||
PublishWorkbookService.ParsedWorkbook parsed = service.parse(valid);
|
||||
assertEquals(2, parsed.rows().size());
|
||||
assertEquals("B001", parsed.rows().get(0).getAsin());
|
||||
assertNull(parsed.rows().get(0).getStatus());
|
||||
assertNull(parsed.rows().get(0).getSyncStatus());
|
||||
assertNull(parsed.rows().get(0).getSyncCountries());
|
||||
assertEquals("DE", parsed.rows().get(1).getCountry());
|
||||
|
||||
File invalid = directory.resolve("invalid.xlsx").toFile();
|
||||
@@ -52,7 +65,7 @@ class PublishWorkbookServiceTest {
|
||||
FileOutputStream output = new FileOutputStream(invalid)) {
|
||||
Sheet sheet = workbook.createSheet("错误表头");
|
||||
Row header = sheet.createRow(0);
|
||||
List<String> headers = new ArrayList<>(PublishWorkbookService.HEADERS);
|
||||
List<String> headers = new ArrayList<>(PublishWorkbookService.SOURCE_HEADERS);
|
||||
headers.set(1, "Asin");
|
||||
for (int index = 0; index < headers.size(); index++) {
|
||||
header.createCell(index).setCellValue(headers.get(index));
|
||||
@@ -82,8 +95,8 @@ class PublishWorkbookServiceTest {
|
||||
Set.of(workbook.getSheetName(0), workbook.getSheetName(1)));
|
||||
Sheet uk = workbook.getSheet("英国");
|
||||
assertNotNull(uk);
|
||||
for (int index = 0; index < PublishWorkbookService.HEADERS.size(); index++) {
|
||||
assertEquals(PublishWorkbookService.HEADERS.get(index),
|
||||
for (int index = 0; index < PublishWorkbookService.RESULT_HEADERS.size(); index++) {
|
||||
assertEquals(PublishWorkbookService.RESULT_HEADERS.get(index),
|
||||
uk.getRow(0).getCell(index).getStringCellValue());
|
||||
}
|
||||
assertEquals(CellType.NUMERIC, uk.getRow(1).getCell(4).getCellType());
|
||||
@@ -124,8 +137,8 @@ class PublishWorkbookServiceTest {
|
||||
|
||||
private void writeSourceSheet(Sheet sheet, String country, String asin) {
|
||||
Row header = sheet.createRow(0);
|
||||
for (int index = 0; index < PublishWorkbookService.HEADERS.size(); index++) {
|
||||
header.createCell(index).setCellValue(PublishWorkbookService.HEADERS.get(index));
|
||||
for (int index = 0; index < PublishWorkbookService.SOURCE_HEADERS.size(); index++) {
|
||||
header.createCell(index).setCellValue(PublishWorkbookService.SOURCE_HEADERS.get(index));
|
||||
}
|
||||
Row data = sheet.createRow(1);
|
||||
data.createCell(0).setCellValue("1");
|
||||
@@ -133,9 +146,6 @@ class PublishWorkbookServiceTest {
|
||||
data.createCell(2).setCellValue(country);
|
||||
data.createCell(3).setCellValue("Brand");
|
||||
data.createCell(4).setCellValue(50);
|
||||
data.createCell(5).setCellValue("成功");
|
||||
data.createCell(6).setCellValue("成功");
|
||||
data.createCell(7).setCellValue("德国,法国");
|
||||
}
|
||||
|
||||
private PublishRowDto row(String id, String asin, String country, String price) {
|
||||
|
||||
Reference in New Issue
Block a user