From cc5fd7f7d75c5090d695d9b0e1c58a7f088ab73b Mon Sep 17 00:00:00 2001 From: song-tianyang Date: Thu, 5 Jan 2023 15:28:33 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E5=88=97=E8=A1=A8=E6=89=B9=E9=87=8F=E5=A4=8D?= =?UTF-8?q?=E5=88=B6=E6=8C=87=E5=AE=9A=E7=89=88=E6=9C=AC=E7=9A=84case?= =?UTF-8?q?=E6=97=B6=E6=96=87=E4=BB=B6=E4=B9=9F=E4=BC=9A=E4=B8=80=E5=90=8C?= =?UTF-8?q?=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 接口列表批量复制指定版本的case时文件也会一同处理 --- .../utils/BatchProcessingUtil.java | 3 +- .../definition/ApiDefinitionService.java | 18 +++++--- .../ext/ExtFileAssociationService.java | 41 +++++++++++++++++++ .../components/version/VersionSelector.vue | 21 +++++++--- api-test/frontend/src/i18n/lang/en-US.js | 2 + api-test/frontend/src/i18n/lang/zh-CN.js | 1 + api-test/frontend/src/i18n/lang/zh-TW.js | 1 + .../metersphere/commons/utils/FileUtils.java | 12 ++++++ 8 files changed, 86 insertions(+), 13 deletions(-) rename api-test/backend/src/main/java/io/metersphere/{ => commons}/utils/BatchProcessingUtil.java (95%) diff --git a/api-test/backend/src/main/java/io/metersphere/utils/BatchProcessingUtil.java b/api-test/backend/src/main/java/io/metersphere/commons/utils/BatchProcessingUtil.java similarity index 95% rename from api-test/backend/src/main/java/io/metersphere/utils/BatchProcessingUtil.java rename to api-test/backend/src/main/java/io/metersphere/commons/utils/BatchProcessingUtil.java index fb68e008e5..efdaac2050 100644 --- a/api-test/backend/src/main/java/io/metersphere/utils/BatchProcessingUtil.java +++ b/api-test/backend/src/main/java/io/metersphere/commons/utils/BatchProcessingUtil.java @@ -1,7 +1,6 @@ -package io.metersphere.utils; +package io.metersphere.commons.utils; import io.metersphere.api.dto.definition.BatchDataCopyRequest; -import io.metersphere.commons.utils.BeanUtils; import org.apache.commons.collections.CollectionUtils; import java.util.ArrayList; diff --git a/api-test/backend/src/main/java/io/metersphere/service/definition/ApiDefinitionService.java b/api-test/backend/src/main/java/io/metersphere/service/definition/ApiDefinitionService.java index 140d426fbc..aeb95c0e5c 100644 --- a/api-test/backend/src/main/java/io/metersphere/service/definition/ApiDefinitionService.java +++ b/api-test/backend/src/main/java/io/metersphere/service/definition/ApiDefinitionService.java @@ -50,7 +50,6 @@ import io.metersphere.service.ext.ExtApiScheduleService; import io.metersphere.service.ext.ExtFileAssociationService; import io.metersphere.service.plan.TestPlanApiCaseService; import io.metersphere.service.scenario.ApiScenarioService; -import io.metersphere.utils.BatchProcessingUtil; import io.metersphere.xpack.api.service.ApiCaseBatchSyncService; import io.metersphere.xpack.api.service.ApiDefinitionSyncService; import io.metersphere.xpack.quota.service.QuotaService; @@ -2287,7 +2286,7 @@ public class ApiDefinitionService { saveMockExpectList.forEach(mockExpectConfigBatchMapper::insert); } if (CollectionUtils.isNotEmpty(updateMockExpectList)) { - updateMockExpectList.forEach(mockExpectConfigBatchMapper::updateByPrimaryKey); + updateMockExpectList.forEach(mockExpectConfigBatchMapper::updateByPrimaryKeyWithBLOBs); } } } @@ -2312,9 +2311,11 @@ public class ApiDefinitionService { List saveCaseList = new ArrayList<>(); List updateCaseList = new ArrayList<>(); Map lastCaseNumMap = new LinkedHashMap<>(); + //用例文件关联关系数据 <要复制的用例ID, 生成的用例ID> + Map forceOverrideFileMap = new HashMap<>(); sourceApiCaseList.forEach(item -> { - String oldApiId = item.getApiDefinitionId(); - String refId = sourceApiIdRefIdMap.get(oldApiId); + String sourceApiId = item.getApiDefinitionId(); + String refId = sourceApiIdRefIdMap.get(sourceApiId); if (StringUtils.isNotBlank(refId)) { ApiDefinition api = refIdMap.get(refId); if (api != null) { @@ -2349,15 +2350,22 @@ public class ApiDefinitionService { newCase.setUpdateTime(timeStamp); updateCaseList.add(newCase); } + + forceOverrideFileMap.put(item.getId(), newCase.getId()); + //本地文件覆盖 + FileUtils.forceOverrideBodyFiles(item.getId(), newCase.getId()); } } }); + FileAssociationMapper batchFileAssociationMapper = batchSqlSession.getMapper(FileAssociationMapper.class); + extFileAssociationService.forceOverrideFileAssociation(forceOverrideFileMap, batchFileAssociationMapper); + ApiTestCaseMapper apiTestCaseBatchMapper = batchSqlSession.getMapper(ApiTestCaseMapper.class); if (CollectionUtils.isNotEmpty(saveCaseList)) { saveCaseList.forEach(apiTestCaseBatchMapper::insert); } if (CollectionUtils.isNotEmpty(updateCaseList)) { - updateCaseList.forEach(apiTestCaseBatchMapper::updateByPrimaryKey); + updateCaseList.forEach(apiTestCaseBatchMapper::updateByPrimaryKeyWithBLOBs); } } } diff --git a/api-test/backend/src/main/java/io/metersphere/service/ext/ExtFileAssociationService.java b/api-test/backend/src/main/java/io/metersphere/service/ext/ExtFileAssociationService.java index 0291858e25..e637225ce1 100644 --- a/api-test/backend/src/main/java/io/metersphere/service/ext/ExtFileAssociationService.java +++ b/api-test/backend/src/main/java/io/metersphere/service/ext/ExtFileAssociationService.java @@ -5,10 +5,15 @@ import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy; import io.metersphere.api.dto.definition.request.variable.ScenarioVariable; import io.metersphere.api.dto.scenario.Body; import io.metersphere.api.dto.scenario.KeyValue; +import io.metersphere.base.domain.FileAssociation; +import io.metersphere.base.domain.FileAssociationExample; +import io.metersphere.base.mapper.FileAssociationMapper; import io.metersphere.commons.enums.FileAssociationTypeEnums; +import io.metersphere.commons.utils.BeanUtils; import io.metersphere.metadata.service.FileAssociationService; import io.metersphere.plugin.core.MsTestElement; import io.metersphere.request.BodyFile; +import org.apache.commons.collections.MapUtils; import org.apache.commons.lang3.StringUtils; import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy; import org.springframework.stereotype.Service; @@ -16,6 +21,8 @@ import org.springframework.util.CollectionUtils; import java.util.ArrayList; import java.util.List; +import java.util.Map; +import java.util.UUID; import java.util.stream.Collectors; @Service @@ -76,4 +83,38 @@ public class ExtFileAssociationService extends FileAssociationService { } return files; } + + /** + * 强制覆盖文件关系 + * + * @param overrideIdMap <来源ID - 强制覆盖的ID> + * @param batchProcessingMapper 批量处理Mapper, 在该方法调用地方进行事务提交 + */ + public void forceOverrideFileAssociation(Map overrideIdMap, FileAssociationMapper batchProcessingMapper) { + if (MapUtils.isEmpty(overrideIdMap) || batchProcessingMapper == null) { + return; + } + //删除原来的数据 + FileAssociationExample example = new FileAssociationExample(); + example.createCriteria().andSourceIdIn(new ArrayList<>(overrideIdMap.values())); + batchProcessingMapper.deleteByExample(example); + + example.clear(); + example.createCriteria().andSourceIdIn(new ArrayList<>(overrideIdMap.keySet())); + List fileAssociationList = batchProcessingMapper.selectByExample(example); + List saveList = new ArrayList<>(); + fileAssociationList.forEach(item -> { + String overrideId = overrideIdMap.get(item.getSourceId()); + if (StringUtils.isNotBlank(overrideId)) { + FileAssociation overrideFileAssociation = new FileAssociation(); + BeanUtils.copyBean(overrideFileAssociation, item); + overrideFileAssociation.setId(UUID.randomUUID().toString()); + overrideFileAssociation.setSourceId(overrideId); + saveList.add(overrideFileAssociation); + } + }); + + saveList.forEach(batchProcessingMapper::insert); + + } } diff --git a/api-test/frontend/src/business/definition/components/version/VersionSelector.vue b/api-test/frontend/src/business/definition/components/version/VersionSelector.vue index 3e7be50ab8..de6b2f257d 100644 --- a/api-test/frontend/src/business/definition/components/version/VersionSelector.vue +++ b/api-test/frontend/src/business/definition/components/version/VersionSelector.vue @@ -12,17 +12,26 @@ + + + - {{ $t('commons.api_case') }} + {{ + $t('commons.api_case') + }} {{ $t('commons.mock') }} @@ -37,7 +46,7 @@ export default { return { loading: false, visible: false, - saving:false, + saving: false, versionId: '', versionData: [], selectCase: true, @@ -82,7 +91,7 @@ export default { this.$error(this.$t('project.version.please_input_version')); } else { this.saving = true; - this.$emit('handleSave', this.versionId,this.selectCase,this.selectMock); + this.$emit('handleSave', this.versionId, this.selectCase, this.selectMock); } }, }, diff --git a/api-test/frontend/src/i18n/lang/en-US.js b/api-test/frontend/src/i18n/lang/en-US.js index b199db2b6a..52c4d86cbb 100644 --- a/api-test/frontend/src/i18n/lang/en-US.js +++ b/api-test/frontend/src/i18n/lang/en-US.js @@ -19,6 +19,8 @@ const message = { default_value: 'Default value', }, copy_data_from_other_version: 'Copy data from other version', + copy_data_from_other_version_tips: + 'Use cases with the same name and mock expectations will be forcibly overwritten!', body: { json_format_error: 'JSON format error', }, diff --git a/api-test/frontend/src/i18n/lang/zh-CN.js b/api-test/frontend/src/i18n/lang/zh-CN.js index 573af2e0d3..4df86e3d68 100644 --- a/api-test/frontend/src/i18n/lang/zh-CN.js +++ b/api-test/frontend/src/i18n/lang/zh-CN.js @@ -18,6 +18,7 @@ const message = { default_value: '默认值', }, copy_data_from_other_version: '复制版本数据', + copy_data_from_other_version_tips: '名称相同的用例和Mock期望会进行强制覆盖!', body: { json_format_error: 'JSON格式错误', }, diff --git a/api-test/frontend/src/i18n/lang/zh-TW.js b/api-test/frontend/src/i18n/lang/zh-TW.js index 3d9320c7da..86e94657db 100644 --- a/api-test/frontend/src/i18n/lang/zh-TW.js +++ b/api-test/frontend/src/i18n/lang/zh-TW.js @@ -18,6 +18,7 @@ const message = { default_value: '默認值', }, copy_data_from_other_version: '複製版本數據', + copy_data_from_other_version_tips: '名稱相同的用例和Mock期望會進行強制覆蓋!', body: { json_format_error: 'JSON格式錯誤', }, diff --git a/framework/sdk-parent/sdk/src/main/java/io/metersphere/commons/utils/FileUtils.java b/framework/sdk-parent/sdk/src/main/java/io/metersphere/commons/utils/FileUtils.java index cf08b9aa40..3ec25567de 100644 --- a/framework/sdk-parent/sdk/src/main/java/io/metersphere/commons/utils/FileUtils.java +++ b/framework/sdk-parent/sdk/src/main/java/io/metersphere/commons/utils/FileUtils.java @@ -171,6 +171,18 @@ public class FileUtils { } } + /** + * 强制覆盖文件 + * + * @param sourceId 源ID + * @param targetId 目标ID + */ + public static void forceOverrideBodyFiles(String sourceId, String targetId) { + //删除源文件 + deleteBodyFiles(targetId); + copyBodyFiles(sourceId, targetId); + } + /** * 复制文件夹(使用缓冲字节流) *