diff --git a/backend/src/main/java/io/metersphere/api/controller/APITestController.java b/backend/src/main/java/io/metersphere/api/controller/APITestController.java index e44ed2f5a2..5f1b61a7dc 100644 --- a/backend/src/main/java/io/metersphere/api/controller/APITestController.java +++ b/backend/src/main/java/io/metersphere/api/controller/APITestController.java @@ -2,10 +2,7 @@ package io.metersphere.api.controller; import com.github.pagehelper.Page; import com.github.pagehelper.PageHelper; -import io.metersphere.api.dto.APITestResult; -import io.metersphere.api.dto.DeleteAPITestRequest; -import io.metersphere.api.dto.QueryAPITestRequest; -import io.metersphere.api.dto.SaveAPITestRequest; +import io.metersphere.api.dto.*; import io.metersphere.api.service.APITestService; import io.metersphere.base.domain.ApiTest; import io.metersphere.base.domain.Schedule; @@ -50,7 +47,6 @@ public class APITestController { return apiTestService.getApiTestByProjectId(projectId); } - @GetMapping("/state/get/{testId}") public ApiTest apiState(@PathVariable String testId) { return apiTestService.getApiTestByTestId(testId); @@ -96,10 +92,10 @@ public class APITestController { return apiTestService.run(request); } - @PostMapping("/import/{platform}") + @PostMapping(value = "/import", consumes = {"multipart/form-data"}) @RequiresRoles(value = {RoleConstants.TEST_USER, RoleConstants.TEST_MANAGER}, logical = Logical.OR) - public ApiTest testCaseImport(MultipartFile file, @PathVariable String platform) { - return apiTestService.apiTestImport(file, platform); + public ApiTest testCaseImport(@RequestPart(value = "file") MultipartFile file, @RequestPart("request") ApiTestImportRequest request) { + return apiTestService.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 new file mode 100644 index 0000000000..38e18ae261 --- /dev/null +++ b/backend/src/main/java/io/metersphere/api/dto/ApiTestImportRequest.java @@ -0,0 +1,15 @@ +package io.metersphere.api.dto; + +import lombok.Getter; +import lombok.Setter; + +@Setter +@Getter +public class ApiTestImportRequest { + private String name; + private String environmentId; + private String projectId; + private String platform; + private Boolean useEnvironment; + private String swaggerUrl; +} 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 58a28cd389..1a3b962d61 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,9 @@ package io.metersphere.api.parse; +import io.metersphere.api.dto.ApiTestImportRequest; import io.metersphere.api.dto.scenario.KeyValue; import io.metersphere.api.dto.scenario.Request; +import io.metersphere.api.dto.scenario.Scenario; import io.metersphere.commons.exception.MSException; import io.metersphere.commons.utils.LogUtil; import org.apache.commons.lang3.StringUtils; @@ -42,6 +44,12 @@ public abstract class ApiImportAbstractParser implements ApiImportParser { return testStr.toString(); } + protected void setScenarioByRequest(Scenario scenario, ApiTestImportRequest request) { + if (request.getUseEnvironment()) { + scenario.setEnvironmentId(request.getEnvironmentId()); + } + } + protected void addContentType(Request request, String contentType) { addHeader(request, HttpHeader.CONTENT_TYPE.toString(), contentType); } diff --git a/backend/src/main/java/io/metersphere/api/parse/ApiImportParser.java b/backend/src/main/java/io/metersphere/api/parse/ApiImportParser.java index 659e9ea6d2..f3086468eb 100644 --- a/backend/src/main/java/io/metersphere/api/parse/ApiImportParser.java +++ b/backend/src/main/java/io/metersphere/api/parse/ApiImportParser.java @@ -1,9 +1,10 @@ package io.metersphere.api.parse; +import io.metersphere.api.dto.ApiTestImportRequest; import io.metersphere.api.dto.parse.ApiImport; import java.io.InputStream; public interface ApiImportParser { - ApiImport parse(InputStream source); + ApiImport parse(InputStream source, ApiTestImportRequest request); } 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 e83176b1c9..19fbb002de 100644 --- a/backend/src/main/java/io/metersphere/api/parse/MsParser.java +++ b/backend/src/main/java/io/metersphere/api/parse/MsParser.java @@ -3,6 +3,7 @@ package io.metersphere.api.parse; 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.parse.ApiImport; import io.metersphere.commons.constants.MsRequestBodyType; import org.apache.commons.lang3.StringUtils; @@ -13,9 +14,13 @@ import java.io.InputStream; public class MsParser extends ApiImportAbstractParser { @Override - public ApiImport parse(InputStream source) { + public ApiImport parse(InputStream source, ApiTestImportRequest request) { String testStr = getApiTestStr(source); - return JSON.parseObject(parsePluginFormat(testStr), ApiImport.class); + ApiImport apiImport = JSON.parseObject(parsePluginFormat(testStr), ApiImport.class); + apiImport.getScenarios().forEach(scenario -> { + setScenarioByRequest(scenario, request); + }); + return apiImport; } private String parsePluginFormat(String testStr) { 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 8f3988fb3a..2c3f5ac040 100644 --- a/backend/src/main/java/io/metersphere/api/parse/PostmanParser.java +++ b/backend/src/main/java/io/metersphere/api/parse/PostmanParser.java @@ -2,6 +2,7 @@ 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.parse.ApiImport; import io.metersphere.api.dto.parse.postman.*; import io.metersphere.api.dto.scenario.Body; @@ -32,7 +33,7 @@ public class PostmanParser extends ApiImportAbstractParser { } @Override - public ApiImport parse(InputStream source) { + public ApiImport parse(InputStream source, ApiTestImportRequest request) { String testStr = getApiTestStr(source); PostmanCollection postmanCollection = JSON.parseObject(testStr, PostmanCollection.class); PostmanCollectionInfo info = postmanCollection.getInfo(); @@ -42,6 +43,7 @@ public class PostmanParser extends ApiImportAbstractParser { Scenario scenario = new Scenario(); scenario.setRequests(requests); scenario.setName(info.getName()); + setScenarioByRequest(scenario, request); scenarios.add(scenario); apiImport.setScenarios(scenarios); return apiImport; 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 df35aaae2e..f87fc1e1cb 100644 --- a/backend/src/main/java/io/metersphere/api/parse/Swagger2Parser.java +++ b/backend/src/main/java/io/metersphere/api/parse/Swagger2Parser.java @@ -2,6 +2,7 @@ package io.metersphere.api.parse; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; +import io.metersphere.api.dto.ApiTestImportRequest; import io.metersphere.api.dto.parse.ApiImport; import io.metersphere.api.dto.scenario.Body; import io.metersphere.api.dto.scenario.KeyValue; @@ -16,6 +17,7 @@ import io.swagger.models.properties.ObjectProperty; import io.swagger.models.properties.Property; import io.swagger.models.properties.RefProperty; import io.swagger.parser.SwaggerParser; +import org.apache.commons.lang3.StringUtils; import java.io.InputStream; import java.util.*; @@ -23,12 +25,18 @@ import java.util.*; public class Swagger2Parser extends ApiImportAbstractParser { @Override - public ApiImport parse(InputStream source) { - String testStr = getApiTestStr(source); -// Swagger swagger = new SwaggerParser().read("http://petstore.swagger.io/v2/swagger.json"); - Swagger swagger = new SwaggerParser().readWithInfo(testStr).getSwagger(); + public ApiImport parse(InputStream source, ApiTestImportRequest request) { + Swagger swagger = null; + if (StringUtils.isNotBlank(request.getSwaggerUrl())) { + swagger = new SwaggerParser().read(request.getSwaggerUrl()); + } else { + swagger = new SwaggerParser().readWithInfo(getApiTestStr(source)).getSwagger(); + } ApiImport apiImport = new ApiImport(); apiImport.setScenarios(parseRequests(swagger)); + apiImport.getScenarios().forEach(scenario -> { + scenario.setEnvironmentId(request.getEnvironmentId()); + }); return apiImport; } diff --git a/backend/src/main/java/io/metersphere/api/service/APITestService.java b/backend/src/main/java/io/metersphere/api/service/APITestService.java index a41addfd2e..bde6b7310d 100644 --- a/backend/src/main/java/io/metersphere/api/service/APITestService.java +++ b/backend/src/main/java/io/metersphere/api/service/APITestService.java @@ -2,6 +2,7 @@ package io.metersphere.api.service; import com.alibaba.fastjson.JSONObject; import io.metersphere.api.dto.APITestResult; +import io.metersphere.api.dto.ApiTestImportRequest; import io.metersphere.api.dto.QueryAPITestRequest; import io.metersphere.api.dto.SaveAPITestRequest; import io.metersphere.api.dto.parse.ApiImport; @@ -75,7 +76,6 @@ public class APITestService { if (files == null || files.isEmpty()) { throw new IllegalArgumentException(Translator.get("file_cannot_be_null")); } - checkNameExist(request); ApiTest test = createTest(request); saveFile(test.getId(), files); } @@ -187,6 +187,7 @@ public class APITestService { } private ApiTest createTest(SaveAPITestRequest request) { + checkNameExist(request); final ApiTest test = new ApiTest(); test.setId(request.getId()); test.setName(request.getName()); @@ -256,23 +257,23 @@ public class APITestService { scheduleService.addOrUpdateCronJob(request, ApiTestJob.getJobKey(request.getResourceId()), ApiTestJob.getTriggerKey(request.getResourceId()), ApiTestJob.class); } - public ApiTest apiTestImport(MultipartFile file, String platform) { - ApiImportParser apiImportParser = ApiImportParserFactory.getApiImportParser(platform); + public ApiTest apiTestImport(MultipartFile file, ApiTestImportRequest request) { + ApiImportParser apiImportParser = ApiImportParserFactory.getApiImportParser(request.getPlatform()); ApiImport apiImport = null; try { - apiImport = apiImportParser.parse(file.getInputStream()); + apiImport = apiImportParser.parse(file.getInputStream(), request); } catch (Exception e) { LogUtil.error(e.getMessage(), e); MSException.throwException(Translator.get("parse_data_error")); } - SaveAPITestRequest request = getImportApiTest(file, apiImport); - return createTest(request); + SaveAPITestRequest saveRequest = getImportApiTest(request, apiImport); + return createTest(saveRequest); } - private SaveAPITestRequest getImportApiTest(MultipartFile file, ApiImport apiImport) { + private SaveAPITestRequest getImportApiTest(ApiTestImportRequest importRequest, ApiImport apiImport) { SaveAPITestRequest request = new SaveAPITestRequest(); - request.setName(file.getOriginalFilename()); - request.setProjectId(""); + request.setName(importRequest.getName()); + request.setProjectId(importRequest.getProjectId()); request.setScenarioDefinition(apiImport.getScenarios()); request.setUserId(SessionUtils.getUserId()); request.setId(UUID.randomUUID().toString()); diff --git a/frontend/src/business/components/api/test/components/ApiEnvironmentConfig.vue b/frontend/src/business/components/api/test/components/ApiEnvironmentConfig.vue index 033d279310..4c6aecc8a6 100644 --- a/frontend/src/business/components/api/test/components/ApiEnvironmentConfig.vue +++ b/frontend/src/business/components/api/test/components/ApiEnvironmentConfig.vue @@ -1,5 +1,5 @@