diff --git a/backend/src/main/java/io/metersphere/api/controller/ApiDefinitionController.java b/backend/src/main/java/io/metersphere/api/controller/ApiDefinitionController.java index 9aa8fea318..6f2b5eca02 100644 --- a/backend/src/main/java/io/metersphere/api/controller/ApiDefinitionController.java +++ b/backend/src/main/java/io/metersphere/api/controller/ApiDefinitionController.java @@ -7,6 +7,7 @@ import io.metersphere.api.dto.ApiTestImportRequest; import io.metersphere.api.dto.automation.ApiScenarioRequest; import io.metersphere.api.dto.automation.ReferenceDTO; import io.metersphere.api.dto.definition.*; +import io.metersphere.api.dto.definition.parse.ApiDefinitionImport; import io.metersphere.api.service.ApiDefinitionService; import io.metersphere.base.domain.ApiDefinition; import io.metersphere.commons.constants.RoleConstants; @@ -93,7 +94,7 @@ public class ApiDefinitionController { @PostMapping(value = "/import", consumes = {"multipart/form-data"}) @RequiresRoles(value = {RoleConstants.TEST_USER, RoleConstants.TEST_MANAGER}, logical = Logical.OR) - public String testCaseImport(@RequestPart(value = "file", required = false) MultipartFile file, @RequestPart("request") ApiTestImportRequest request) { + public ApiDefinitionImport testCaseImport(@RequestPart(value = "file", required = false) MultipartFile file, @RequestPart("request") ApiTestImportRequest request) { return apiDefinitionService.apiTestImport(file, request); } diff --git a/backend/src/main/java/io/metersphere/api/dto/ApiTestImportRequest.java b/backend/src/main/java/io/metersphere/api/dto/ApiTestImportRequest.java index 704dadc295..28980a6993 100644 --- a/backend/src/main/java/io/metersphere/api/dto/ApiTestImportRequest.java +++ b/backend/src/main/java/io/metersphere/api/dto/ApiTestImportRequest.java @@ -13,5 +13,7 @@ public class ApiTestImportRequest { private String projectId; private String platform; private Boolean useEnvironment; + // 来自场景的导入不需要存储 + private boolean saved = true; private String swaggerUrl; } diff --git a/backend/src/main/java/io/metersphere/api/parse/MsParser.java b/backend/src/main/java/io/metersphere/api/parse/MsParser.java index 78effbfff9..d297c3174a 100644 --- a/backend/src/main/java/io/metersphere/api/parse/MsParser.java +++ b/backend/src/main/java/io/metersphere/api/parse/MsParser.java @@ -32,7 +32,7 @@ public class MsParser extends ApiImportAbstractParser { if (testObject.get("projectName") != null) { return parseMsFormat(testStr, request); } else { - return parsePluginFormat(testObject); + return parsePluginFormat(testObject, request.isSaved()); } } @@ -53,14 +53,16 @@ public class MsParser extends ApiImportAbstractParser { return apiDefinitionImport; } - private ApiDefinitionImport parsePluginFormat(JSONObject testObject) { + private ApiDefinitionImport parsePluginFormat(JSONObject testObject, boolean isSaved) { List results = new ArrayList<>(); ApiDefinitionImport apiImport = new ApiDefinitionImport(); apiImport.setProtocol(RequestType.HTTP); apiImport.setData(results); testObject.keySet().forEach(tag -> { ApiModule module = apiModuleService.getNewModule(tag, this.projectId, 1); - createModule(module); + if (isSaved) { + createModule(module); + } JSONObject requests = testObject.getJSONObject(tag); requests.keySet().forEach(requestName -> { diff --git a/backend/src/main/java/io/metersphere/api/parse/PostmanParser.java b/backend/src/main/java/io/metersphere/api/parse/PostmanParser.java index ec5f63a386..9809326b35 100644 --- a/backend/src/main/java/io/metersphere/api/parse/PostmanParser.java +++ b/backend/src/main/java/io/metersphere/api/parse/PostmanParser.java @@ -30,17 +30,17 @@ public class PostmanParser extends ApiImportAbstractParser { List variables = postmanCollection.getVariable(); ApiDefinitionImport apiImport = new ApiDefinitionImport(); List results = new ArrayList<>(); - parseItem(postmanCollection.getItem(), variables, results, buildModule(postmanCollection.getInfo().getName(), null)); + parseItem(postmanCollection.getItem(), variables, results, buildModule(postmanCollection.getInfo().getName(), null, request.isSaved()), request.isSaved()); apiImport.setData(results); return apiImport; } - private void parseItem(List items, List variables, List results, ApiModule parentModule) { + private void parseItem(List items, List variables, List results, ApiModule parentModule, boolean isSaved) { for (PostmanItem item : items) { List childItems = item.getItem(); if (childItems != null) { - ApiModule module = buildModule(item.getName(), parentModule); - parseItem(childItems, variables, results, module); + ApiModule module = buildModule(item.getName(), parentModule, isSaved); + parseItem(childItems, variables, results, module, isSaved); } else { ApiDefinitionResult request = parsePostman(item); if (request != null) { @@ -53,7 +53,7 @@ public class PostmanParser extends ApiImportAbstractParser { } } - private ApiModule buildModule(String name, ApiModule parentModule) { + private ApiModule buildModule(String name, ApiModule parentModule, boolean isSaved) { apiModuleService = CommonBeanFactory.getBean(ApiModuleService.class); ApiModule module; if (parentModule != null) { @@ -62,7 +62,9 @@ public class PostmanParser extends ApiImportAbstractParser { } else { module = apiModuleService.getNewModule(name, this.projectId, 1); } - createModule(module); + if (isSaved) { + createModule(module); + } return module; } diff --git a/backend/src/main/java/io/metersphere/api/parse/Swagger2Parser.java b/backend/src/main/java/io/metersphere/api/parse/Swagger2Parser.java index b5dea282b7..8be3f760f7 100644 --- a/backend/src/main/java/io/metersphere/api/parse/Swagger2Parser.java +++ b/backend/src/main/java/io/metersphere/api/parse/Swagger2Parser.java @@ -39,11 +39,11 @@ public class Swagger2Parser extends ApiImportAbstractParser { } ApiDefinitionImport definitionImport = new ApiDefinitionImport(); this.projectId = request.getProjectId(); - definitionImport.setData(parseRequests(swagger)); + definitionImport.setData(parseRequests(swagger, request.isSaved())); return definitionImport; } - private List parseRequests(Swagger swagger) { + private List parseRequests(Swagger swagger, boolean isSaved) { Map paths = swagger.getPaths(); Set pathNames = paths.keySet(); @@ -62,7 +62,7 @@ public class Swagger2Parser extends ApiImportAbstractParser { parseParameters(operation, request); apiDefinition.setRequest(JSON.toJSONString(request)); apiDefinition.setResponse(JSON.toJSONString(parseResponse(operation.getResponses()))); - buildModule(apiDefinition, operation); + buildModule(apiDefinition, operation, isSaved); results.add(apiDefinition); } } @@ -71,13 +71,15 @@ public class Swagger2Parser extends ApiImportAbstractParser { return results; } - private void buildModule(ApiDefinitionResult apiDefinition, Operation operation) { + private void buildModule(ApiDefinitionResult apiDefinition, Operation operation, boolean isSaved) { List tags = operation.getTags(); if (tags != null) { tags.forEach(tag -> { apiModuleService = CommonBeanFactory.getBean(ApiModuleService.class); ApiModule module = apiModuleService.getNewModule(tag, this.projectId, 1); - createModule(module); + if (isSaved) { + createModule(module); + } apiDefinition.setModuleId(module.getId()); }); } @@ -113,8 +115,8 @@ public class Swagger2Parser extends ApiImportAbstractParser { for (Parameter parameter : parameters) { switch (parameter.getIn()) { case SwaggerParameterType.PATH: - parsePathParameters(parameter, request.getRest()); - break; + parsePathParameters(parameter, request.getRest()); + break; case SwaggerParameterType.QUERY: parseQueryParameters(parameter, request.getArguments()); break; @@ -297,14 +299,14 @@ public class Swagger2Parser extends ApiImportAbstractParser { || value instanceof DecimalProperty) { return 0.0; } else {// todo 其他类型? - return getDefaultStringValue(value.getDescription()); + return getDefaultStringValue(value.getDescription()); } } private void parseFormDataParameters(FormParameter parameter, Body body) { List keyValues = Optional.ofNullable(body.getKvs()).orElse(new ArrayList<>()); KeyValue kv = new KeyValue(parameter.getName(), "", getDefaultStringValue(parameter.getDescription())); - if (StringUtils.equals(parameter.getType(), "file") ) { + if (StringUtils.equals(parameter.getType(), "file")) { kv.setType("file"); } keyValues.add(kv); diff --git a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionExecResultService.java b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionExecResultService.java index a3466cbe9e..f5a175d62e 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionExecResultService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionExecResultService.java @@ -4,7 +4,6 @@ import com.alibaba.fastjson.JSON; import io.metersphere.api.jmeter.TestResult; import io.metersphere.base.domain.ApiDefinitionExecResult; import io.metersphere.base.mapper.ApiDefinitionExecResultMapper; -import io.metersphere.base.mapper.ext.ExtApiDefinitionExecResultMapper; import io.metersphere.commons.utils.SessionUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -18,14 +17,9 @@ import java.util.UUID; public class ApiDefinitionExecResultService { @Resource private ApiDefinitionExecResultMapper apiDefinitionExecResultMapper; - @Resource - private ExtApiDefinitionExecResultMapper extApiDefinitionExecResultMapper; - public void saveApiResult(TestResult result) { result.getScenarios().get(0).getRequestResults().forEach(item -> { - // 清理原始资源,每个执行 保留一条结果 - extApiDefinitionExecResultMapper.deleteByResourceId(item.getName()); ApiDefinitionExecResult saveResult = new ApiDefinitionExecResult(); saveResult.setId(UUID.randomUUID().toString()); saveResult.setUserId(Objects.requireNonNull(SessionUtils.getUser()).getId()); 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 789009212a..6b1fa87426 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java @@ -326,7 +326,7 @@ public class ApiDefinitionService { * @return */ public APIReportResult getDbResult(String testId) { - ApiDefinitionExecResult result = extApiDefinitionExecResultMapper.selectByResourceId(testId); + ApiDefinitionExecResult result = extApiDefinitionExecResultMapper.selectMaxResultByResourceId(testId); if (result == null) { return null; } @@ -336,7 +336,7 @@ public class ApiDefinitionService { } - public String apiTestImport(MultipartFile file, ApiTestImportRequest request) { + public ApiDefinitionImport apiTestImport(MultipartFile file, ApiTestImportRequest request) { ApiImportParser apiImportParser = ApiImportParserFactory.getApiImportParser(request.getPlatform()); ApiDefinitionImport apiImport = null; try { @@ -345,8 +345,10 @@ public class ApiDefinitionService { LogUtil.error(e.getMessage(), e); MSException.throwException(Translator.get("parse_data_error")); } - importApiTest(request, apiImport); - return "SUCCESS"; + if (request.isSaved()) { + importApiTest(request, apiImport); + } + return apiImport; } private void importApiTest(ApiTestImportRequest importRequest, ApiDefinitionImport apiImport) { diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiDefinitionExecResultMapper.java b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiDefinitionExecResultMapper.java index e9a480815b..8c33a17d5f 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiDefinitionExecResultMapper.java +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiDefinitionExecResultMapper.java @@ -6,7 +6,6 @@ public interface ExtApiDefinitionExecResultMapper { void deleteByResourceId(String id); - ApiDefinitionExecResult selectByResourceId(String resourceId); - + ApiDefinitionExecResult selectMaxResultByResourceId(String resourceId); } \ No newline at end of file diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiDefinitionExecResultMapper.xml b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiDefinitionExecResultMapper.xml index 8031d82c59..fec17fae05 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiDefinitionExecResultMapper.xml +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiDefinitionExecResultMapper.xml @@ -5,8 +5,8 @@ delete from api_definition_exec_result where resource_id = #{id,jdbcType=VARCHAR} - select * from api_definition_exec_result - where resource_id = #{resourceId,jdbcType=VARCHAR} + where resource_id = #{resourceId,jdbcType=VARCHAR} ORDER BY update_time DESC LIMIT 1 \ No newline at end of file diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiDefinitionMapper.xml b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiDefinitionMapper.xml index 545bc9b71a..8ffd1aa754 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiDefinitionMapper.xml +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiDefinitionMapper.xml @@ -89,17 +89,29 @@ diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiTestCaseMapper.xml b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiTestCaseMapper.xml index cf45823784..3eed364772 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiTestCaseMapper.xml +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiTestCaseMapper.xml @@ -146,15 +146,45 @@