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 51b1468a9e..3b2a394e4c 100644 --- a/backend/src/main/java/io/metersphere/api/controller/ApiDefinitionController.java +++ b/backend/src/main/java/io/metersphere/api/controller/ApiDefinitionController.java @@ -6,8 +6,6 @@ import io.metersphere.api.dto.APIReportResult; 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.datacount.request.ScheduleInfoRequest; -import io.metersphere.api.dto.datacount.response.TaskInfoResult; import io.metersphere.api.dto.definition.*; import io.metersphere.api.dto.definition.parse.ApiDefinitionImport; import io.metersphere.api.dto.definition.request.ScheduleInfoSwaggerUrlRequest; @@ -149,6 +147,13 @@ public class ApiDefinitionController { public ApiDefinitionImport testCaseImport(@RequestPart(value = "file", required = false) MultipartFile file, @RequestPart("request") ApiTestImportRequest request) { return apiDefinitionService.apiTestImport(file, request); } + + @PostMapping(value = "/export") + @RequiresRoles(value = {RoleConstants.TEST_USER, RoleConstants.TEST_MANAGER}, logical = Logical.OR) + public ApiExportResult export(@RequestBody ApiBatchRequest request) { + return apiDefinitionService.export(request); + } + //定时任务创建 @PostMapping(value = "/schedule/create") public void createSchedule(@RequestBody Schedule request) { diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/ApiExportResult.java b/backend/src/main/java/io/metersphere/api/dto/definition/ApiExportResult.java new file mode 100644 index 0000000000..ff0046a54a --- /dev/null +++ b/backend/src/main/java/io/metersphere/api/dto/definition/ApiExportResult.java @@ -0,0 +1,17 @@ +package io.metersphere.api.dto.definition; + +import io.metersphere.base.domain.ApiDefinitionWithBLOBs; +import io.metersphere.base.domain.ApiTestCaseWithBLOBs; +import lombok.Getter; +import lombok.Setter; + +import java.util.List; + +@Getter +@Setter +public class ApiExportResult { + private String projectName; + private String protocol; + private List data; + private List cases; +} diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/parse/ApiDefinitionImport.java b/backend/src/main/java/io/metersphere/api/dto/definition/parse/ApiDefinitionImport.java index f4475d097f..7a35339222 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/parse/ApiDefinitionImport.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/parse/ApiDefinitionImport.java @@ -1,6 +1,7 @@ package io.metersphere.api.dto.definition.parse; -import io.metersphere.api.dto.definition.ApiDefinitionResult; +import io.metersphere.base.domain.ApiDefinitionWithBLOBs; +import io.metersphere.base.domain.ApiTestCaseWithBLOBs; import lombok.Data; import java.util.List; @@ -9,5 +10,8 @@ import java.util.List; public class ApiDefinitionImport { private String projectName; private String protocol; - private List data; + private List data; + + // 新版本带用例导出 + private List cases; } diff --git a/backend/src/main/java/io/metersphere/api/parse/ApiImportAbstractParser.java b/backend/src/main/java/io/metersphere/api/parse/ApiImportAbstractParser.java index 36c5307c4f..12f7519ef9 100644 --- a/backend/src/main/java/io/metersphere/api/parse/ApiImportAbstractParser.java +++ b/backend/src/main/java/io/metersphere/api/parse/ApiImportAbstractParser.java @@ -1,7 +1,6 @@ package io.metersphere.api.parse; import io.metersphere.api.dto.ApiTestImportRequest; -import io.metersphere.api.dto.definition.ApiDefinitionResult; import io.metersphere.api.dto.definition.ApiModuleDTO; import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy; import io.metersphere.api.dto.scenario.Body; @@ -9,6 +8,7 @@ import io.metersphere.api.dto.scenario.KeyValue; import io.metersphere.api.dto.scenario.Scenario; import io.metersphere.api.dto.scenario.request.RequestType; import io.metersphere.api.service.ApiModuleService; +import io.metersphere.base.domain.ApiDefinitionWithBLOBs; import io.metersphere.base.domain.ApiModule; import io.metersphere.commons.exception.MSException; import io.metersphere.commons.utils.BeanUtils; @@ -151,8 +151,8 @@ public abstract class ApiImportAbstractParser implements ApiImportParser { } } - protected ApiDefinitionResult buildApiDefinition(String id, String name, String path, String method, ApiTestImportRequest importRequest) { - ApiDefinitionResult apiDefinition = new ApiDefinitionResult(); + protected ApiDefinitionWithBLOBs buildApiDefinition(String id, String name, String path, String method, ApiTestImportRequest importRequest) { + ApiDefinitionWithBLOBs apiDefinition = new ApiDefinitionWithBLOBs(); apiDefinition.setName(name); apiDefinition.setPath(formatPath(path)); apiDefinition.setProtocol(RequestType.HTTP); 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 7cf9ca65f0..82f691a6c8 100644 --- a/backend/src/main/java/io/metersphere/api/parse/MsParser.java +++ b/backend/src/main/java/io/metersphere/api/parse/MsParser.java @@ -6,13 +6,16 @@ import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.parser.Feature; import io.metersphere.api.dto.ApiTestImportRequest; import io.metersphere.api.dto.definition.ApiDefinitionResult; +import io.metersphere.api.dto.definition.ApiExportResult; import io.metersphere.api.dto.definition.parse.ApiDefinitionImport; import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy; import io.metersphere.api.dto.scenario.Body; import io.metersphere.api.dto.scenario.KeyValue; import io.metersphere.api.dto.scenario.request.RequestType; import io.metersphere.api.service.ApiModuleService; +import io.metersphere.base.domain.ApiDefinitionWithBLOBs; import io.metersphere.base.domain.ApiModule; +import io.metersphere.base.domain.ApiTestCaseWithBLOBs; import io.metersphere.commons.constants.ApiImportPlatform; import io.metersphere.commons.utils.CommonBeanFactory; import io.metersphere.commons.utils.LogUtil; @@ -41,27 +44,29 @@ public class MsParser extends ApiImportAbstractParser { } private ApiDefinitionImport parseMsFormat(String testStr, ApiTestImportRequest importRequest) { - ApiDefinitionImport apiDefinitionImport = JSON.parseObject(testStr, ApiDefinitionImport.class); - List data = apiDefinitionImport.getData(); - data.forEach(apiDefinition -> { - String id = UUID.randomUUID().toString(); - if (StringUtils.isBlank(apiDefinition.getModulePath())) { - apiDefinition.setModuleId(null); - } - parseModule(apiDefinition, importRequest); - apiDefinition.setId(id); - apiDefinition.setProjectId(this.projectId); - String request = apiDefinition.getRequest(); - JSONObject requestObj = JSONObject.parseObject(request); - requestObj.put("id", id); - apiDefinition.setRequest(JSONObject.toJSONString(requestObj)); + apiDefinitionImport.getData().forEach(apiDefinition -> { + parseApiDefinition(apiDefinition, importRequest); }); return apiDefinitionImport; } + private void parseApiDefinition(ApiDefinitionWithBLOBs apiDefinition, ApiTestImportRequest importRequest) { + String id = UUID.randomUUID().toString(); + if (StringUtils.isBlank(apiDefinition.getModulePath())) { + apiDefinition.setModuleId(null); + } + parseModule(apiDefinition, importRequest); + apiDefinition.setId(id); + apiDefinition.setProjectId(this.projectId); + String request = apiDefinition.getRequest(); + JSONObject requestObj = JSONObject.parseObject(request); + requestObj.put("id", id); + apiDefinition.setRequest(JSONObject.toJSONString(requestObj)); + } + private ApiDefinitionImport parsePluginFormat(JSONObject testObject, ApiTestImportRequest importRequest) { - List results = new ArrayList<>(); + List results = new ArrayList<>(); ApiDefinitionImport apiImport = new ApiDefinitionImport(); apiImport.setProtocol(RequestType.HTTP); apiImport.setData(results); @@ -78,7 +83,7 @@ public class MsParser extends ApiImportAbstractParser { String method = requestObject.getString("method"); MsHTTPSamplerProxy request = buildRequest(requestName, path, method); - ApiDefinitionResult apiDefinition = buildApiDefinition(request.getId(), requestName, path, method,importRequest); + ApiDefinitionWithBLOBs apiDefinition = buildApiDefinition(request.getId(), requestName, path, method,importRequest); apiDefinition.setModuleId(module.getId()); apiDefinition.setProjectId(this.projectId); parseBody(requestObject, request.getBody()); @@ -91,7 +96,7 @@ public class MsParser extends ApiImportAbstractParser { return apiImport; } - private void parsePath(MsHTTPSamplerProxy request, ApiDefinitionResult apiDefinition) { + private void parsePath(MsHTTPSamplerProxy request, ApiDefinitionWithBLOBs apiDefinition) { if (StringUtils.isNotBlank(request.getPath())) { String[] split = request.getPath().split("\\?"); String path = split[0]; @@ -160,7 +165,7 @@ public class MsParser extends ApiImportAbstractParser { } - private void parseModule(ApiDefinitionResult apiDefinition, ApiTestImportRequest importRequest) { + private void parseModule(ApiDefinitionWithBLOBs apiDefinition, ApiTestImportRequest importRequest) { String modulePath = apiDefinition.getModulePath(); if (StringUtils.isBlank(modulePath)) { return; 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 a1db04246b..0b7d3d5862 100644 --- a/backend/src/main/java/io/metersphere/api/parse/PostmanParser.java +++ b/backend/src/main/java/io/metersphere/api/parse/PostmanParser.java @@ -3,7 +3,6 @@ package io.metersphere.api.parse; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import io.metersphere.api.dto.ApiTestImportRequest; -import io.metersphere.api.dto.definition.ApiDefinitionResult; import io.metersphere.api.dto.definition.parse.ApiDefinitionImport; import io.metersphere.api.dto.definition.request.MsTestElement; import io.metersphere.api.dto.definition.request.processors.pre.MsJSR223PreProcessor; @@ -11,6 +10,7 @@ import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy; import io.metersphere.api.dto.parse.postman.*; import io.metersphere.api.dto.scenario.Body; import io.metersphere.api.dto.scenario.KeyValue; +import io.metersphere.base.domain.ApiDefinitionWithBLOBs; import io.metersphere.base.domain.ApiModule; import io.metersphere.commons.constants.MsRequestBodyType; import io.metersphere.commons.constants.PostmanRequestBodyMode; @@ -32,20 +32,20 @@ public class PostmanParser extends ApiImportAbstractParser { PostmanCollection postmanCollection = JSON.parseObject(testStr, PostmanCollection.class); List variables = postmanCollection.getVariable(); ApiDefinitionImport apiImport = new ApiDefinitionImport(); - List results = new ArrayList<>(); + List results = new ArrayList<>(); parseItem(postmanCollection.getItem(), variables, results, buildModule(getSelectModule(request.getModuleId()), postmanCollection.getInfo().getName(), request.isSaved()), request.isSaved()); apiImport.setData(results); return apiImport; } - private void parseItem(List items, List variables, List results, ApiModule parentModule, boolean isSaved) { + 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(parentModule, item.getName() , isSaved); parseItem(childItems, variables, results, module, isSaved); } else { - ApiDefinitionResult request = parsePostman(item); + ApiDefinitionWithBLOBs request = parsePostman(item); if (request != null) { results.add(request); } @@ -56,7 +56,7 @@ public class PostmanParser extends ApiImportAbstractParser { } } - private ApiDefinitionResult parsePostman(PostmanItem requestItem) { + private ApiDefinitionWithBLOBs parsePostman(PostmanItem requestItem) { PostmanRequest requestDesc = requestItem.getRequest(); if (requestDesc == null) { return null; @@ -64,7 +64,7 @@ public class PostmanParser extends ApiImportAbstractParser { requestDesc.getAuth(); // todo 认证方式等待优化 PostmanUrl url = requestDesc.getUrl(); MsHTTPSamplerProxy request = buildRequest(requestItem.getName(), url.getRaw(), requestDesc.getMethod()); - ApiDefinitionResult apiDefinition = + ApiDefinitionWithBLOBs apiDefinition = buildApiDefinition(request.getId(), requestItem.getName(), url.getRaw(), requestDesc.getMethod(),new ApiTestImportRequest()); if (StringUtils.isNotBlank(request.getPath())) { String path = request.getPath().split("\\?")[0]; 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 22879f8491..d2d2823dd2 100644 --- a/backend/src/main/java/io/metersphere/api/parse/Swagger2Parser.java +++ b/backend/src/main/java/io/metersphere/api/parse/Swagger2Parser.java @@ -4,13 +4,13 @@ import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import io.metersphere.api.dto.ApiTestImportRequest; -import io.metersphere.api.dto.definition.ApiDefinitionResult; import io.metersphere.api.dto.definition.parse.ApiDefinitionImport; import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy; import io.metersphere.api.dto.definition.response.HttpResponse; import io.metersphere.api.dto.scenario.Body; import io.metersphere.api.dto.scenario.KeyValue; import io.metersphere.api.dto.scenario.request.RequestType; +import io.metersphere.base.domain.ApiDefinitionWithBLOBs; import io.metersphere.base.domain.ApiModule; import io.metersphere.commons.constants.SwaggerParameterType; import io.swagger.models.*; @@ -49,13 +49,13 @@ public class Swagger2Parser extends SwaggerAbstractParser { return definitionImport; } - private List parseRequests(Swagger swagger, ApiTestImportRequest importRequest) { + private List parseRequests(Swagger swagger, ApiTestImportRequest importRequest) { Map paths = swagger.getPaths(); Set pathNames = paths.keySet(); this.definitions = swagger.getDefinitions(); - List results = new ArrayList<>(); + List results = new ArrayList<>(); ApiModule parentNode = getSelectModule(importRequest.getModuleId()); @@ -66,7 +66,7 @@ public class Swagger2Parser extends SwaggerAbstractParser { for (HttpMethod method : httpMethods) { Operation operation = operationMap.get(method); MsHTTPSamplerProxy request = buildRequest(operation, pathName, method.name()); - ApiDefinitionResult apiDefinition = buildApiDefinition(request.getId(), operation, pathName, method.name(),importRequest); + ApiDefinitionWithBLOBs apiDefinition = buildApiDefinition(request.getId(), operation, pathName, method.name(),importRequest); parseParameters(operation, request); addBodyHeader(request); apiDefinition.setRequest(JSON.toJSONString(request)); @@ -80,7 +80,7 @@ public class Swagger2Parser extends SwaggerAbstractParser { return results; } - private ApiDefinitionResult buildApiDefinition(String id, Operation operation, String path, String method,ApiTestImportRequest importRequest) { + private ApiDefinitionWithBLOBs buildApiDefinition(String id, Operation operation, String path, String method,ApiTestImportRequest importRequest) { String name = ""; if (StringUtils.isNotBlank(operation.getSummary())) { name = operation.getSummary(); diff --git a/backend/src/main/java/io/metersphere/api/parse/Swagger3Parser.java b/backend/src/main/java/io/metersphere/api/parse/Swagger3Parser.java index 62335c3c25..b189c89534 100644 --- a/backend/src/main/java/io/metersphere/api/parse/Swagger3Parser.java +++ b/backend/src/main/java/io/metersphere/api/parse/Swagger3Parser.java @@ -4,13 +4,13 @@ import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import io.metersphere.api.dto.ApiTestImportRequest; -import io.metersphere.api.dto.definition.ApiDefinitionResult; import io.metersphere.api.dto.definition.parse.ApiDefinitionImport; import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy; import io.metersphere.api.dto.definition.response.HttpResponse; import io.metersphere.api.dto.scenario.Body; import io.metersphere.api.dto.scenario.KeyValue; import io.metersphere.api.dto.scenario.request.RequestType; +import io.metersphere.base.domain.ApiDefinitionWithBLOBs; import io.metersphere.base.domain.ApiModule; import io.metersphere.commons.exception.MSException; import io.metersphere.commons.utils.LogUtil; @@ -69,14 +69,14 @@ public class Swagger3Parser extends SwaggerAbstractParser { return definitionImport; } - private List parseRequests(OpenAPI openAPI, ApiTestImportRequest importRequest) { + private List parseRequests(OpenAPI openAPI, ApiTestImportRequest importRequest) { Paths paths = openAPI.getPaths(); Set pathNames = paths.keySet(); this.components = openAPI.getComponents(); - List results = new ArrayList<>(); + List results = new ArrayList<>(); ApiModule parentNode = getSelectModule(importRequest.getModuleId()); @@ -97,7 +97,7 @@ public class Swagger3Parser extends SwaggerAbstractParser { Operation operation = operationsMap.get(method); if (operation != null) { MsHTTPSamplerProxy request = buildRequest(operation, pathName, method); - ApiDefinitionResult apiDefinition = buildApiDefinition(request.getId(), operation, pathName, method,importRequest); + ApiDefinitionWithBLOBs apiDefinition = buildApiDefinition(request.getId(), operation, pathName, method,importRequest); parseParameters(operation, request); parseRequestBody(operation.getRequestBody(), request.getBody()); addBodyHeader(request); @@ -112,7 +112,7 @@ public class Swagger3Parser extends SwaggerAbstractParser { return results; } - private ApiDefinitionResult buildApiDefinition(String id, Operation operation, String path, String method,ApiTestImportRequest importRequest) { + private ApiDefinitionWithBLOBs buildApiDefinition(String id, Operation operation, String path, String method,ApiTestImportRequest importRequest) { String name = ""; if (StringUtils.isNotBlank(operation.getSummary())) { name = operation.getSummary(); diff --git a/backend/src/main/java/io/metersphere/api/parse/SwaggerAbstractParser.java b/backend/src/main/java/io/metersphere/api/parse/SwaggerAbstractParser.java index 1973109c54..2a6fdf9f7a 100644 --- a/backend/src/main/java/io/metersphere/api/parse/SwaggerAbstractParser.java +++ b/backend/src/main/java/io/metersphere/api/parse/SwaggerAbstractParser.java @@ -1,13 +1,13 @@ package io.metersphere.api.parse; -import io.metersphere.api.dto.definition.ApiDefinitionResult; +import io.metersphere.base.domain.ApiDefinitionWithBLOBs; import io.metersphere.base.domain.ApiModule; import java.util.List; public abstract class SwaggerAbstractParser extends ApiImportAbstractParser { - protected void buildModule(ApiModule parentModule, ApiDefinitionResult apiDefinition, List tags, boolean isSaved) { + protected void buildModule(ApiModule parentModule, ApiDefinitionWithBLOBs apiDefinition, List tags, boolean isSaved) { if (tags != null) { tags.forEach(tag -> { ApiModule module = buildModule(parentModule, tag, isSaved); diff --git a/backend/src/main/java/io/metersphere/api/parse/old/PostmanParser.java b/backend/src/main/java/io/metersphere/api/parse/old/PostmanParser.java index aba9b3199a..56be3545dc 100644 --- a/backend/src/main/java/io/metersphere/api/parse/old/PostmanParser.java +++ b/backend/src/main/java/io/metersphere/api/parse/old/PostmanParser.java @@ -3,7 +3,6 @@ package io.metersphere.api.parse.old; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import io.metersphere.api.dto.ApiTestImportRequest; -import io.metersphere.api.dto.definition.ApiDefinitionResult; import io.metersphere.api.dto.definition.parse.ApiDefinitionImport; import io.metersphere.api.dto.definition.request.MsTestElement; import io.metersphere.api.dto.definition.request.configurations.MsHeaderManager; @@ -16,6 +15,7 @@ import io.metersphere.api.dto.scenario.Scenario; import io.metersphere.api.dto.scenario.request.HttpRequest; import io.metersphere.api.dto.scenario.request.Request; import io.metersphere.api.dto.scenario.request.RequestType; +import io.metersphere.base.domain.ApiDefinitionWithBLOBs; import io.metersphere.commons.constants.MsRequestBodyType; import io.metersphere.commons.constants.PostmanRequestBodyMode; import org.apache.commons.lang3.StringUtils; @@ -54,20 +54,20 @@ public class PostmanParser extends ApiImportAbstractParser { PostmanCollection postmanCollection = JSON.parseObject(testStr, PostmanCollection.class); List variables = postmanCollection.getVariable(); ApiDefinitionImport apiImport = new ApiDefinitionImport(); - List requests = new ArrayList<>(); + List requests = new ArrayList<>(); parseItem(postmanCollection.getItem(), variables, requests); apiImport.setData(requests); return apiImport; } - private void parseItem(List items, List variables, List scenarios) { + private void parseItem(List items, List variables, List scenarios) { for (PostmanItem item : items) { List childItems = item.getItem(); if (childItems != null) { parseItem(childItems, variables, scenarios); } else { - ApiDefinitionResult request = parsePostman(item); + ApiDefinitionWithBLOBs request = parsePostman(item); if (request != null) { scenarios.add(request); } @@ -75,13 +75,13 @@ public class PostmanParser extends ApiImportAbstractParser { } } - private ApiDefinitionResult parsePostman(PostmanItem requestItem) { + private ApiDefinitionWithBLOBs parsePostman(PostmanItem requestItem) { PostmanRequest requestDesc = requestItem.getRequest(); if (requestDesc == null) { return null; } PostmanUrl url = requestDesc.getUrl(); - ApiDefinitionResult request = new ApiDefinitionResult(); + ApiDefinitionWithBLOBs request = new ApiDefinitionWithBLOBs(); request.setName(requestItem.getName()); request.setPath(url.getRaw()); request.setMethod(requestDesc.getMethod()); diff --git a/backend/src/main/java/io/metersphere/api/parse/old/Swagger2Parser.java b/backend/src/main/java/io/metersphere/api/parse/old/Swagger2Parser.java index c3048a8e41..57512fca3f 100644 --- a/backend/src/main/java/io/metersphere/api/parse/old/Swagger2Parser.java +++ b/backend/src/main/java/io/metersphere/api/parse/old/Swagger2Parser.java @@ -4,7 +4,6 @@ import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import io.metersphere.api.dto.ApiTestImportRequest; -import io.metersphere.api.dto.definition.ApiDefinitionResult; import io.metersphere.api.dto.definition.parse.ApiDefinitionImport; import io.metersphere.api.dto.definition.request.MsTestElement; import io.metersphere.api.dto.definition.request.configurations.MsHeaderManager; @@ -16,6 +15,7 @@ import io.metersphere.api.dto.scenario.Scenario; import io.metersphere.api.dto.scenario.request.HttpRequest; import io.metersphere.api.dto.scenario.request.Request; import io.metersphere.api.dto.scenario.request.RequestType; +import io.metersphere.base.domain.ApiDefinitionWithBLOBs; import io.metersphere.commons.constants.MsRequestBodyType; import io.metersphere.commons.constants.SwaggerParameterType; import io.swagger.models.*; @@ -54,13 +54,13 @@ public class Swagger2Parser extends ApiImportAbstractParser { return definitionImport; } - private List parseSwagger(ApiImport apiImport) { - List results = new LinkedList<>(); + private List parseSwagger(ApiImport apiImport) { + List results = new LinkedList<>(); apiImport.getScenarios().forEach(item -> { item.getRequests().forEach(childItem -> { if (childItem instanceof HttpRequest) { HttpRequest res = (HttpRequest) childItem; - ApiDefinitionResult request = new ApiDefinitionResult(); + ApiDefinitionWithBLOBs request = new ApiDefinitionWithBLOBs(); request.setName(res.getName()); request.setPath(res.getPath()); request.setMethod(res.getMethod()); 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 75208d01bf..d803f26a5a 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java @@ -298,24 +298,23 @@ public class ApiDefinitionService { } } - private ApiDefinition importCreate(ApiDefinitionResult request, ApiDefinitionMapper batchMapper, ApiTestCaseMapper apiTestCaseMapper, ApiTestImportRequest apiTestImportRequest) { + private ApiDefinition importCreate(ApiDefinitionWithBLOBs apiDefinition, ApiDefinitionMapper batchMapper, + ApiTestCaseMapper apiTestCaseMapper, ApiTestImportRequest apiTestImportRequest, List cases) { SaveApiDefinitionRequest saveReq = new SaveApiDefinitionRequest(); - BeanUtils.copyBean(saveReq, request); - final ApiDefinitionWithBLOBs apiDefinition = new ApiDefinitionWithBLOBs(); - BeanUtils.copyBean(apiDefinition, request); + BeanUtils.copyBean(saveReq, apiDefinition); apiDefinition.setCreateTime(System.currentTimeMillis()); apiDefinition.setUpdateTime(System.currentTimeMillis()); apiDefinition.setStatus(APITestStatus.Underway.name()); - if (request.getUserId() == null) { + if (apiDefinition.getUserId() == null) { apiDefinition.setUserId(Objects.requireNonNull(SessionUtils.getUser()).getId()); } else { - apiDefinition.setUserId(request.getUserId()); + apiDefinition.setUserId(apiDefinition.getUserId()); } - apiDefinition.setDescription(request.getDescription()); + apiDefinition.setDescription(apiDefinition.getDescription()); List sameRequest = getSameRequest(saveReq); if (StringUtils.equals("fullCoverage", apiTestImportRequest.getModeId())) { - _importCreate(sameRequest, batchMapper, apiDefinition, apiTestCaseMapper, apiTestImportRequest); + _importCreate(sameRequest, batchMapper, apiDefinition, apiTestCaseMapper, apiTestImportRequest, cases); } else if (StringUtils.equals("incrementalMerge", apiTestImportRequest.getModeId())) { if (CollectionUtils.isEmpty(sameRequest)) { //postman 可能含有前置脚本,接口定义去掉脚本 @@ -325,25 +324,34 @@ public class ApiDefinitionService { importApiCase(apiDefinition, apiTestCaseMapper, apiTestImportRequest, true); } } else { - _importCreate(sameRequest, batchMapper, apiDefinition, apiTestCaseMapper, apiTestImportRequest); + _importCreate(sameRequest, batchMapper, apiDefinition, apiTestCaseMapper, apiTestImportRequest, cases); } return apiDefinition; } private void _importCreate(List sameRequest, ApiDefinitionMapper batchMapper, ApiDefinitionWithBLOBs apiDefinition, - ApiTestCaseMapper apiTestCaseMapper, ApiTestImportRequest apiTestImportRequest) { + ApiTestCaseMapper apiTestCaseMapper, ApiTestImportRequest apiTestImportRequest, List cases) { if (CollectionUtils.isEmpty(sameRequest)) { String request = setImportHashTree(apiDefinition); batchMapper.insert(apiDefinition); apiDefinition.setRequest(request); importApiCase(apiDefinition, apiTestCaseMapper, apiTestImportRequest, true); } else { + String originId = apiDefinition.getId(); //如果存在则修改 apiDefinition.setId(sameRequest.get(0).getId()); String request = setImportHashTree(apiDefinition); apiDefinitionMapper.updateByPrimaryKeyWithBLOBs(apiDefinition); apiDefinition.setRequest(request); importApiCase(apiDefinition, apiTestCaseMapper, apiTestImportRequest, false); + // 如果是带用例导出,重新设置接口id + if (CollectionUtils.isNotEmpty(cases)) { + cases.forEach(item -> { + if (StringUtils.equals(item.getApiDefinitionId(), originId)) { + item.setApiDefinitionId(apiDefinition.getId()); + } + }); + } } } @@ -356,10 +364,30 @@ public class ApiDefinitionService { return request; } - /** - * 导入是插件或者postman时创建用例 - * postman考虑是否有前置脚本 - */ + private void importMsCase(ApiDefinitionImport apiImport, SqlSession sqlSession, ApiTestCaseMapper apiTestCaseMapper) { + List cases = apiImport.getCases(); + if (CollectionUtils.isNotEmpty(cases)) { + int batchCount = 0; + cases.forEach(item -> { + item.setId(UUID.randomUUID().toString()); + item.setCreateTime(System.currentTimeMillis()); + item.setUpdateTime(System.currentTimeMillis()); + item.setCreateUserId(SessionUtils.getUserId()); + item.setUpdateUserId(SessionUtils.getUserId()); + item.setProjectId(SessionUtils.getCurrentProjectId()); + item.setNum(getNextNum(item.getApiDefinitionId())); + apiTestCaseMapper.insert(item); + }); + if (batchCount % 300 == 0) { + sqlSession.flushStatements(); + } + } + } + + /** + * 导入是插件或者postman时创建用例 + * postman考虑是否有前置脚本 + */ private void importApiCase(ApiDefinitionWithBLOBs apiDefinition, ApiTestCaseMapper apiTestCaseMapper, ApiTestImportRequest apiTestImportRequest, Boolean isInsert) { try { @@ -372,6 +400,7 @@ public class ApiDefinitionService { apiTestCase.setUpdateTime(System.currentTimeMillis()); apiTestCase.setCreateUserId(SessionUtils.getUserId()); apiTestCase.setUpdateUserId(SessionUtils.getUserId()); + apiTestCase.setNum(getNextNum(apiTestCase.getApiDefinitionId())); apiTestCase.setPriority("P0"); if (!isInsert) { apiTestCase.setName(apiTestCase.getName() + "_" + apiTestCase.getId().substring(0, 5)); @@ -497,20 +526,21 @@ public class ApiDefinitionService { private void importApi(ApiTestImportRequest request, ApiDefinitionImport apiImport) { SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); + List data = apiImport.getData(); ApiDefinitionMapper batchMapper = sqlSession.getMapper(ApiDefinitionMapper.class); ApiTestCaseMapper apiTestCaseMapper = sqlSession.getMapper(ApiTestCaseMapper.class); - List data = apiImport.getData(); int num = 0; if (!CollectionUtils.isEmpty(data) && data.get(0) != null && data.get(0).getProjectId() != null) { num = getNextNum(data.get(0).getProjectId()); } for (int i = 0; i < data.size(); i++) { - ApiDefinitionResult item = data.get(i); + ApiDefinitionWithBLOBs item = data.get(i); if (item.getName().length() > 255) { item.setName(item.getName().substring(0, 255)); } item.setNum(num++); - importCreate(item, batchMapper, apiTestCaseMapper, request); + importCreate(item, batchMapper, apiTestCaseMapper, request, apiImport.getCases()); + importMsCase(apiImport, sqlSession, apiTestCaseMapper); if (i % 300 == 0) { sqlSession.flushStatements(); } @@ -518,6 +548,7 @@ public class ApiDefinitionService { sqlSession.flushStatements(); } + public ReferenceDTO getReference(ApiScenarioRequest request) { ReferenceDTO dto = new ReferenceDTO(); dto.setScenarioList(extApiScenarioMapper.selectReference(request)); @@ -707,4 +738,17 @@ public class ApiDefinitionService { private void addOrUpdateSwaggerImportCronJob(Schedule request) { scheduleService.addOrUpdateCronJob(request, SwaggerUrlImportJob.getJobKey(request.getResourceId()), SwaggerUrlImportJob.getTriggerKey(request.getResourceId()), SwaggerUrlImportJob.class); } + + public ApiExportResult export(ApiBatchRequest request) { + ServiceUtils.getSelectAllIds(request, request.getCondition(), + (query) -> extApiDefinitionMapper.selectIds(query)); + ApiDefinitionExample example = new ApiDefinitionExample(); + example.createCriteria().andIdIn(request.getIds()); + ApiExportResult apiExportResult = new ApiExportResult(); + apiExportResult.setData(apiDefinitionMapper.selectByExampleWithBLOBs(example)); + apiExportResult.setCases(apiTestCaseService.selectCasesBydApiIds(request.getIds())); + apiExportResult.setProjectName(request.getProjectId()); + apiExportResult.setProtocol(request.getProtocol()); + return apiExportResult; + } } diff --git a/backend/src/main/java/io/metersphere/api/service/ApiTestCaseService.java b/backend/src/main/java/io/metersphere/api/service/ApiTestCaseService.java index 6ccaf75c36..c840e32793 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiTestCaseService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiTestCaseService.java @@ -386,6 +386,12 @@ public class ApiTestCaseService { return apiTestCaseMapper.selectByExample(example); } + public List selectCasesBydApiIds(List apiIds) { + ApiTestCaseExample example = new ApiTestCaseExample(); + example.createCriteria().andApiDefinitionIdIn(apiIds); + return apiTestCaseMapper.selectByExampleWithBLOBs(example); + } + public Map getRequest(ApiTestCaseRequest request) { List list = extApiTestCaseMapper.getRequest(request); return list.stream().collect(Collectors.toMap(ApiTestCaseWithBLOBs::getId, ApiTestCaseWithBLOBs::getRequest)); diff --git a/frontend/src/business/components/api/definition/ApiDefinition.vue b/frontend/src/business/components/api/definition/ApiDefinition.vue index 55ecc124b3..3c3ac69996 100644 --- a/frontend/src/business/components/api/definition/ApiDefinition.vue +++ b/frontend/src/business/components/api/definition/ApiDefinition.vue @@ -114,26 +114,25 @@