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 b1f3ddba8d..a92d0d1bf8 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 @@ -2,6 +2,7 @@ package io.metersphere.api.dto.definition.parse; import io.metersphere.api.dto.definition.request.MsScenario; import io.metersphere.base.domain.ApiDefinitionWithBLOBs; +import io.metersphere.base.domain.ApiScenarioWithBLOBs; import io.metersphere.base.domain.ApiTestCaseWithBLOBs; import lombok.Data; @@ -15,6 +16,7 @@ public class ApiDefinitionImport { //导入场景 private MsScenario scenarioDefinition; + private List scenarioDefinitionData; // 新版本带用例导出 private List cases; 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 7b939a84af..e8b69e112c 100644 --- a/backend/src/main/java/io/metersphere/api/parse/MsParser.java +++ b/backend/src/main/java/io/metersphere/api/parse/MsParser.java @@ -39,7 +39,7 @@ public class MsParser extends ApiImportAbstractParser { return parseMsFormat(testStr, request); } else { request.setPlatform(ApiImportPlatform.Plugin.name()); - return parsePluginFormat(testObject, request); + return parsePluginFormat(testObject, request, true); } } @@ -65,17 +65,20 @@ public class MsParser extends ApiImportAbstractParser { apiDefinition.setRequest(JSONObject.toJSONString(requestObj)); } - private ApiDefinitionImport parsePluginFormat(JSONObject testObject, ApiTestImportRequest importRequest) { + protected ApiDefinitionImport parsePluginFormat(JSONObject testObject, ApiTestImportRequest importRequest, Boolean isCreateModule) { List results = new ArrayList<>(); ApiDefinitionImport apiImport = new ApiDefinitionImport(); apiImport.setProtocol(RequestType.HTTP); apiImport.setData(results); testObject.keySet().forEach(tag -> { - ApiModule parentModule = getSelectModule(importRequest.getModuleId()); - ApiModule module = buildModule(parentModule, tag); - + ApiModule module = null; + if (isCreateModule) { + module = buildModule(getSelectModule(importRequest.getModuleId()), tag); + } JSONObject requests = testObject.getJSONObject(tag); + String moduleId = module.getId(); + requests.keySet().forEach(requestName -> { JSONObject requestObject = requests.getJSONObject(requestName); @@ -84,7 +87,7 @@ public class MsParser extends ApiImportAbstractParser { MsHTTPSamplerProxy request = buildRequest(requestName, path, method); ApiDefinitionWithBLOBs apiDefinition = buildApiDefinition(request.getId(), requestName, path, method,importRequest); - apiDefinition.setModuleId(module.getId()); + apiDefinition.setModuleId(moduleId); apiDefinition.setProjectId(this.projectId); parseBody(requestObject, request.getBody()); parseHeader(requestObject, request.getHeaders()); diff --git a/backend/src/main/java/io/metersphere/api/parse/ScenarioMsParser.java b/backend/src/main/java/io/metersphere/api/parse/ScenarioMsParser.java new file mode 100644 index 0000000000..29a6f4c244 --- /dev/null +++ b/backend/src/main/java/io/metersphere/api/parse/ScenarioMsParser.java @@ -0,0 +1,71 @@ +package io.metersphere.api.parse; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.alibaba.fastjson.parser.Feature; +import io.metersphere.api.dto.ApiTestImportRequest; +import io.metersphere.api.dto.automation.ApiScenrioExportResult; +import io.metersphere.api.dto.definition.parse.ApiDefinitionImport; +import io.metersphere.api.dto.definition.request.MsScenario; +import io.metersphere.api.dto.definition.request.MsTestElement; +import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy; +import io.metersphere.api.service.ApiModuleService; +import io.metersphere.base.domain.ApiScenarioWithBLOBs; +import io.metersphere.commons.constants.ApiImportPlatform; +import io.metersphere.commons.utils.CommonBeanFactory; +import org.apache.commons.lang3.StringUtils; + +import java.io.InputStream; +import java.util.LinkedList; +import java.util.UUID; + +public class ScenarioMsParser extends MsParser { + + @Override + public ApiDefinitionImport parse(InputStream source, ApiTestImportRequest request) { + String testStr = getApiTestStr(source); + JSONObject testObject = JSONObject.parseObject(testStr, Feature.OrderedField); + apiModuleService = CommonBeanFactory.getBean(ApiModuleService.class); + this.projectId = request.getProjectId(); + if (testObject.get("projectId") != null) { + return parseMsFormat(testStr, request); + } else { + request.setPlatform(ApiImportPlatform.Plugin.name()); + ApiDefinitionImport apiDefinitionImport = parsePluginFormat(testObject, request, false); + MsScenario msScenario = new MsScenario(); + LinkedList msHTTPSamplerProxies = new LinkedList<>(); + apiDefinitionImport.getData().forEach(res -> { + msHTTPSamplerProxies.add(JSONObject.parseObject(res.getRequest(), MsHTTPSamplerProxy.class)); + }); + msScenario.setHashTree(msHTTPSamplerProxies); + msScenario.setType("scenario"); + msScenario.setName("test"); + apiDefinitionImport.setScenarioDefinition(msScenario); + return apiDefinitionImport; + } + } + + private ApiDefinitionImport parseMsFormat(String testStr, ApiTestImportRequest importRequest) { + ApiScenrioExportResult apiScenrioExportResult = JSON.parseObject(testStr, ApiScenrioExportResult.class); + apiScenrioExportResult.getData().forEach(scenario -> { + parseApiDefinition(scenario, importRequest); + }); + ApiDefinitionImport apiDefinitionImport = new ApiDefinitionImport(); + apiDefinitionImport.setScenarioDefinitionData(apiScenrioExportResult.getData()); + return apiDefinitionImport; + } + + private void parseApiDefinition(ApiScenarioWithBLOBs scenario, ApiTestImportRequest importRequest) { + String id = UUID.randomUUID().toString(); + if (StringUtils.isBlank(scenario.getModulePath())) { + scenario.setApiScenarioModuleId(null); + } +// parseModule(scenario, importRequest); + scenario.setId(id); + scenario.setProjectId(this.projectId); + String scenarioDefinition = scenario.getScenarioDefinition(); + JSONObject scenarioDefinitionObj = JSONObject.parseObject(scenarioDefinition); + scenarioDefinitionObj.put("id", id); + scenario.setScenarioDefinition(JSONObject.toJSONString(scenarioDefinitionObj)); + } +} 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 1f7f2d26c2..a82f7baa79 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java @@ -10,7 +10,6 @@ import io.metersphere.api.dto.DeleteAPIReportRequest; import io.metersphere.api.dto.JmxInfoDTO; import io.metersphere.api.dto.automation.*; import io.metersphere.api.dto.datacount.ApiDataCountResult; -import io.metersphere.api.dto.definition.ApiExportResult; import io.metersphere.api.dto.definition.RunDefinitionRequest; import io.metersphere.api.dto.definition.parse.ApiDefinitionImport; import io.metersphere.api.dto.definition.request.*;