diff --git a/performance-test/backend/src/main/java/io/metersphere/api/service/ApiPerformanceService.java b/performance-test/backend/src/main/java/io/metersphere/api/service/ApiPerformanceService.java index 74637b97e4..0eadf19268 100644 --- a/performance-test/backend/src/main/java/io/metersphere/api/service/ApiPerformanceService.java +++ b/performance-test/backend/src/main/java/io/metersphere/api/service/ApiPerformanceService.java @@ -25,6 +25,7 @@ import org.springframework.transaction.annotation.Transactional; import java.io.File; import java.nio.charset.StandardCharsets; +import java.util.ArrayList; import java.util.List; import java.util.UUID; import java.util.stream.Collectors; @@ -124,11 +125,10 @@ public class ApiPerformanceService { if (!CollectionUtils.isEmpty(scenarioIds)) { ApiScenarioBatchRequest scenarioRequest = new ApiScenarioBatchRequest(); scenarioRequest.setIds(scenarioIds); - List apiScenrioExportJmxes = this.exportJmx(scenarioRequest).getScenarioJmxList(); - - deleteLoadTestFiles(loadTest.getId()); - - apiScenrioExportJmxes.forEach(item -> { + List apiScenarioExportJmxDTOList = this.exportJmx(scenarioRequest).getScenarioJmxList(); + List attachFileIds = this.getScenarioAttachFileIds(apiScenarioExportJmxDTOList); + deleteLoadTestFiles(loadTest.getId(), attachFileIds); + apiScenarioExportJmxDTOList.forEach(item -> { this.updateVersion(loadTest.getId(), item.getId(), item.getVersion()); saveJmxFile(item.getJmx(), item.getName(), loadTest.getProjectId(), loadTest.getId()); saveOtherFile(item.getFileMetadataList(), loadTest.getId()); @@ -136,6 +136,30 @@ public class ApiPerformanceService { } } + private List getScenarioAttachFileIds(List apiScenarioExportJmxDTOList) { + List returnList = new ArrayList<>(); + if (CollectionUtils.isNotEmpty(apiScenarioExportJmxDTOList)) { + for (ApiScenarioExportJmxDTO item : apiScenarioExportJmxDTOList) { + if (CollectionUtils.isNotEmpty(item.getFileMetadataList())) { + returnList.addAll(item.getFileMetadataList().stream().map(FileMetadata::getId).toList()); + } + } + } + return returnList; + } + + private List getAttachFileIds(List jmxInfoDTOS) { + List returnList = new ArrayList<>(); + if (CollectionUtils.isNotEmpty(jmxInfoDTOS)) { + for (JmxInfoDTO item : jmxInfoDTOS) { + if (CollectionUtils.isNotEmpty(item.getFileMetadataList())) { + returnList.addAll(item.getFileMetadataList().stream().map(FileMetadata::getId).toList()); + } + } + } + return returnList; + } + public void syncApiCase(LoadTestWithBLOBs loadTest, List apiLoadTests) { List caseIds = apiLoadTests.stream() .filter(i -> i.getType().equals(ApiLoadType.API_CASE.name())) @@ -146,7 +170,8 @@ public class ApiPerformanceService { request.setCaseIds(caseIds); request.setEnvId(apiLoadTests.get(0).getEnvId()); List jmxInfoDTOS = this.exportApiCaseJmx(request); - deleteLoadTestFiles(loadTest.getId()); + List attachmentFileIds = this.getAttachFileIds(jmxInfoDTOS); + deleteLoadTestFiles(loadTest.getId(), attachmentFileIds); jmxInfoDTOS.forEach(item -> { this.updateVersion(loadTest.getId(), item.getId(), item.getVersion()); saveJmxFile(item.getXml(), item.getName(), loadTest.getProjectId(), loadTest.getId()); @@ -207,9 +232,12 @@ public class ApiPerformanceService { } } - private void deleteLoadTestFiles(String testId) { + private void deleteLoadTestFiles(String testId, List notDeleteFileIds) { List originFiles = extLoadTestMapper.getFileMetadataByIds(testId); List originFileIds = originFiles.stream().map(FileMetadata::getId).collect(Collectors.toList()); + if (CollectionUtils.isNotEmpty(notDeleteFileIds)) { + originFileIds.removeAll(notDeleteFileIds); + } LoadTestFileExample example = new LoadTestFileExample(); example.createCriteria().andTestIdEqualTo(testId); loadTestFileMapper.deleteByExample(example);