diff --git a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java index 006fbee84f..9120cd514f 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java @@ -22,7 +22,6 @@ import io.metersphere.commons.constants.APITestStatus; import io.metersphere.commons.constants.ApiRunMode; import io.metersphere.commons.constants.ReportTriggerMode; import io.metersphere.commons.exception.MSException; -import io.metersphere.commons.utils.LogUtil; import io.metersphere.commons.utils.SessionUtils; import io.metersphere.i18n.Translator; import io.metersphere.track.dto.TestPlanDTO; @@ -34,13 +33,11 @@ import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.jorphan.collections.HashTree; import org.apache.jorphan.collections.ListedHashTree; -import org.aspectj.util.FileUtil; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; -import java.io.*; import java.util.*; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -51,6 +48,8 @@ public class ApiAutomationService { @Resource private ApiScenarioMapper apiScenarioMapper; @Resource + private ApiDefinitionService apiDefinitionService; + @Resource private ExtApiScenarioMapper extApiScenarioMapper; @Resource private ApiTagMapper apiTagMapper; @@ -126,13 +125,13 @@ public class ApiAutomationService { apiScenarioMapper.insert(scenario); List bodyUploadIds = new ArrayList<>(request.getBodyUploadIds()); - createBodyFiles(bodyUploadIds, bodyFiles); + apiDefinitionService.createBodyFiles(bodyUploadIds, bodyFiles); } public void update(SaveApiScenarioRequest request, List bodyFiles) { checkNameExist(request); - List bodyUploadIds = new ArrayList<>(request.getBodyUploadIds()); - createBodyFiles(bodyUploadIds, bodyFiles); + List bodyUploadIds = request.getBodyUploadIds(); + apiDefinitionService.createBodyFiles(bodyUploadIds, bodyFiles); final ApiScenario scenario = new ApiScenario(); scenario.setId(request.getId()); @@ -177,7 +176,7 @@ public class ApiAutomationService { private void checkNameExist(SaveApiScenarioRequest request) { ApiScenarioExample example = new ApiScenarioExample(); - example.createCriteria().andNameEqualTo(request.getName()).andProjectIdEqualTo(request.getProjectId()).andIdNotEqualTo(request.getId()); + example.createCriteria().andNameEqualTo(request.getName()).andProjectIdEqualTo(request.getProjectId()).andStatusNotEqualTo("Trash").andIdNotEqualTo(request.getId()); if (apiScenarioMapper.countByExample(example) > 0) { MSException.throwException(Translator.get("automation_name_already_exists")); } @@ -194,26 +193,6 @@ public class ApiAutomationService { return new ArrayList<>(); } - private void createBodyFiles(List bodyUploadIds, List bodyFiles) { - if (!bodyUploadIds.isEmpty() && !bodyFiles.isEmpty()) { - File testDir = new File(BODY_FILE_DIR); - if (!testDir.exists()) { - testDir.mkdirs(); - } - for (int i = 0; i < bodyUploadIds.size(); i++) { - MultipartFile item = bodyFiles.get(i); - File file = new File(BODY_FILE_DIR + "/" + bodyUploadIds.get(i) + "_" + item.getOriginalFilename()); - try (InputStream in = item.getInputStream(); OutputStream out = new FileOutputStream(file)) { - file.createNewFile(); - FileUtil.copyStream(in, out); - } catch (IOException e) { - LogUtil.error(e); - MSException.throwException(Translator.get("upload_fail")); - } - } - } - } - public void deleteTag(String id) { List list = extApiScenarioMapper.selectByTagId(id); if (!list.isEmpty()) { @@ -297,7 +276,7 @@ public class ApiAutomationService { */ public String run(RunDefinitionRequest request, List bodyFiles) { List bodyUploadIds = new ArrayList<>(request.getBodyUploadIds()); - createBodyFiles(bodyUploadIds, bodyFiles); + apiDefinitionService.createBodyFiles(bodyUploadIds, bodyFiles); EnvironmentConfig envConfig = null; if (request.getEnvironmentId() != null) { ApiTestEnvironmentWithBLOBs environment = environmentService.get(request.getEnvironmentId()); diff --git a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java index fef7732035..7aa80d95ef 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java @@ -25,7 +25,7 @@ import io.metersphere.commons.utils.ServiceUtils; import io.metersphere.commons.utils.SessionUtils; import io.metersphere.i18n.Translator; import io.metersphere.service.FileService; -import org.apache.commons.lang3.StringUtils; +import org.apache.commons.collections.CollectionUtils; import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; @@ -33,13 +33,15 @@ import org.apache.jorphan.collections.HashTree; import org.aspectj.util.FileUtil; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import org.springframework.util.CollectionUtils; import org.springframework.web.multipart.MultipartFile; import sun.security.util.Cache; import javax.annotation.Resource; import java.io.*; -import java.util.*; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Objects; import java.util.function.Function; import java.util.stream.Collectors; @@ -103,15 +105,17 @@ public class ApiDefinitionService { } public void update(SaveApiDefinitionRequest request, List bodyFiles) { - deleteFileByTestId(request.getRequest().getId()); - List bodyUploadIds = new ArrayList<>(request.getBodyUploadIds()); + if (request.getRequest() != null) { + deleteFileByTestId(request.getRequest().getId()); + } + List bodyUploadIds = request.getBodyUploadIds(); request.setBodyUploadIds(null); updateTest(request); createBodyFiles(bodyUploadIds, bodyFiles); } - private void createBodyFiles(List bodyUploadIds, List bodyFiles) { - if (bodyUploadIds.size() > 0) { + public void createBodyFiles(List bodyUploadIds, List bodyFiles) { + if (CollectionUtils.isNotEmpty(bodyUploadIds) && CollectionUtils.isNotEmpty(bodyFiles)) { File testDir = new File(BODY_FILE_DIR); if (!testDir.exists()) { testDir.mkdirs(); @@ -139,10 +143,9 @@ public class ApiDefinitionService { } public void deleteBatch(List apiIds) { - // 简单处理后续优化 - apiIds.forEach(item -> { - delete(item); - }); + ApiDefinitionExample example = new ApiDefinitionExample(); + example.createCriteria().andIdIn(apiIds); + apiDefinitionMapper.deleteByExample(example); } public void removeToGc(List apiIds) { @@ -160,14 +163,14 @@ public class ApiDefinitionService { private void checkNameExist(SaveApiDefinitionRequest request) { ApiDefinitionExample example = new ApiDefinitionExample(); if (request.getProtocol().equals(RequestType.HTTP)) { - example.createCriteria().andMethodEqualTo(request.getMethod()) + example.createCriteria().andMethodEqualTo(request.getMethod()).andStatusNotEqualTo("Trash") .andProtocolEqualTo(request.getProtocol()).andPathEqualTo(request.getPath()) .andProjectIdEqualTo(request.getProjectId()).andIdNotEqualTo(request.getId()); if (apiDefinitionMapper.countByExample(example) > 0) { MSException.throwException(Translator.get("api_definition_url_not_repeating")); } } else { - example.createCriteria().andProtocolEqualTo(request.getProtocol()) + example.createCriteria().andProtocolEqualTo(request.getProtocol()).andStatusNotEqualTo("Trash") .andNameEqualTo(request.getName()).andProjectIdEqualTo(request.getProjectId()) .andIdNotEqualTo(request.getId()); if (apiDefinitionMapper.countByExample(example) > 0) { @@ -176,7 +179,6 @@ public class ApiDefinitionService { } } - private ApiDefinition updateTest(SaveApiDefinitionRequest request) { checkNameExist(request); final ApiDefinitionWithBLOBs test = new ApiDefinitionWithBLOBs(); diff --git a/frontend/src/business/components/api/automation/scenario/ApiScenarioList.vue b/frontend/src/business/components/api/automation/scenario/ApiScenarioList.vue index 78bd291342..438b2525b0 100644 --- a/frontend/src/business/components/api/automation/scenario/ApiScenarioList.vue +++ b/frontend/src/business/components/api/automation/scenario/ApiScenarioList.vue @@ -47,11 +47,16 @@ show-overflow-tooltip/> @@ -202,6 +207,13 @@ edit(row) { this.$emit('edit', row); }, + reductionApi(row) { + let obj = {id: row.id, projectId: row.projectId, name: row.name, status: 'Underway'} + this.$fileUpload("/api/automation/update", null, [], obj, () => { + this.$success(this.$t('commons.save_success')); + this.search(); + }) + }, execute(row) { this.infoDb = false; let url = "/api/automation/run"; diff --git a/frontend/src/business/components/api/automation/scenario/EditApiScenario.vue b/frontend/src/business/components/api/automation/scenario/EditApiScenario.vue index de392d5a95..968213f16b 100644 --- a/frontend/src/business/components/api/automation/scenario/EditApiScenario.vue +++ b/frontend/src/business/components/api/automation/scenario/EditApiScenario.vue @@ -240,7 +240,7 @@ - {{ $t('commons.copy') }} + {{ $t('commons.copy') }} @@ -326,6 +326,7 @@ userId: [{required: true, message: this.$t('test_track.case.input_maintainer'), trigger: 'change'}], apiScenarioModuleId: [{required: true, message: this.$t('test_track.case.input_module'), trigger: 'change'}], status: [{required: true, message: this.$t('commons.please_select'), trigger: 'change'}], + principal: [{required: true, message: this.$t('api_test.definition.request.responsible'), trigger: 'change'}], }, environments: [], tags: [], @@ -684,9 +685,11 @@ this.path = "/api/automation/update"; if (response.data.scenarioDefinition != null) { let obj = JSON.parse(response.data.scenarioDefinition); - this.currentEnvironmentId = obj.environmentId; - this.currentScenario.variables = obj.variables; - this.scenarioDefinition = obj.hashTree; + if (obj) { + this.currentEnvironmentId = obj.environmentId; + this.currentScenario.variables = obj.variables; + this.scenarioDefinition = obj.hashTree; + } } } }) diff --git a/frontend/src/business/components/api/definition/components/ApiList.vue b/frontend/src/business/components/api/definition/components/ApiList.vue index abf0d5352f..b9c7582656 100644 --- a/frontend/src/business/components/api/definition/components/ApiList.vue +++ b/frontend/src/business/components/api/definition/components/ApiList.vue @@ -77,7 +77,8 @@ 用例
- 编辑 + 恢复 + 编辑 用例 删除
@@ -256,6 +257,15 @@ editApi(row) { this.$emit('editApi', row); }, + reductionApi(row) { + row.status = 'Underway'; + row.request = null; + row.response = null; + this.$fileUpload("/api/definition/update", null, [], row, () => { + this.$success(this.$t('commons.save_success')); + this.search(); + }); + }, handleDeleteBatch() { if (this.currentModule != undefined && this.currentModule.id == "gc") { this.$alert(this.$t('api_test.definition.request.delete_confirm') + "?", '', {