场景导入-插件

This commit is contained in:
chenjianxing 2021-02-19 16:37:11 +08:00
parent 54258fb01e
commit 3f23ce02d7
4 changed files with 82 additions and 7 deletions

View File

@ -2,6 +2,7 @@ package io.metersphere.api.dto.definition.parse;
import io.metersphere.api.dto.definition.request.MsScenario; import io.metersphere.api.dto.definition.request.MsScenario;
import io.metersphere.base.domain.ApiDefinitionWithBLOBs; import io.metersphere.base.domain.ApiDefinitionWithBLOBs;
import io.metersphere.base.domain.ApiScenarioWithBLOBs;
import io.metersphere.base.domain.ApiTestCaseWithBLOBs; import io.metersphere.base.domain.ApiTestCaseWithBLOBs;
import lombok.Data; import lombok.Data;
@ -15,6 +16,7 @@ public class ApiDefinitionImport {
//导入场景 //导入场景
private MsScenario scenarioDefinition; private MsScenario scenarioDefinition;
private List<ApiScenarioWithBLOBs> scenarioDefinitionData;
// 新版本带用例导出 // 新版本带用例导出
private List<ApiTestCaseWithBLOBs> cases; private List<ApiTestCaseWithBLOBs> cases;

View File

@ -39,7 +39,7 @@ public class MsParser extends ApiImportAbstractParser {
return parseMsFormat(testStr, request); return parseMsFormat(testStr, request);
} else { } else {
request.setPlatform(ApiImportPlatform.Plugin.name()); 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)); apiDefinition.setRequest(JSONObject.toJSONString(requestObj));
} }
private ApiDefinitionImport parsePluginFormat(JSONObject testObject, ApiTestImportRequest importRequest) { protected ApiDefinitionImport parsePluginFormat(JSONObject testObject, ApiTestImportRequest importRequest, Boolean isCreateModule) {
List<ApiDefinitionWithBLOBs> results = new ArrayList<>(); List<ApiDefinitionWithBLOBs> results = new ArrayList<>();
ApiDefinitionImport apiImport = new ApiDefinitionImport(); ApiDefinitionImport apiImport = new ApiDefinitionImport();
apiImport.setProtocol(RequestType.HTTP); apiImport.setProtocol(RequestType.HTTP);
apiImport.setData(results); apiImport.setData(results);
testObject.keySet().forEach(tag -> { testObject.keySet().forEach(tag -> {
ApiModule parentModule = getSelectModule(importRequest.getModuleId()); ApiModule module = null;
ApiModule module = buildModule(parentModule, tag); if (isCreateModule) {
module = buildModule(getSelectModule(importRequest.getModuleId()), tag);
}
JSONObject requests = testObject.getJSONObject(tag); JSONObject requests = testObject.getJSONObject(tag);
String moduleId = module.getId();
requests.keySet().forEach(requestName -> { requests.keySet().forEach(requestName -> {
JSONObject requestObject = requests.getJSONObject(requestName); JSONObject requestObject = requests.getJSONObject(requestName);
@ -84,7 +87,7 @@ public class MsParser extends ApiImportAbstractParser {
MsHTTPSamplerProxy request = buildRequest(requestName, path, method); MsHTTPSamplerProxy request = buildRequest(requestName, path, method);
ApiDefinitionWithBLOBs apiDefinition = buildApiDefinition(request.getId(), requestName, path, method,importRequest); ApiDefinitionWithBLOBs apiDefinition = buildApiDefinition(request.getId(), requestName, path, method,importRequest);
apiDefinition.setModuleId(module.getId()); apiDefinition.setModuleId(moduleId);
apiDefinition.setProjectId(this.projectId); apiDefinition.setProjectId(this.projectId);
parseBody(requestObject, request.getBody()); parseBody(requestObject, request.getBody());
parseHeader(requestObject, request.getHeaders()); parseHeader(requestObject, request.getHeaders());

View File

@ -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<MsTestElement> 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));
}
}

View File

@ -10,7 +10,6 @@ import io.metersphere.api.dto.DeleteAPIReportRequest;
import io.metersphere.api.dto.JmxInfoDTO; import io.metersphere.api.dto.JmxInfoDTO;
import io.metersphere.api.dto.automation.*; import io.metersphere.api.dto.automation.*;
import io.metersphere.api.dto.datacount.ApiDataCountResult; 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.RunDefinitionRequest;
import io.metersphere.api.dto.definition.parse.ApiDefinitionImport; import io.metersphere.api.dto.definition.parse.ApiDefinitionImport;
import io.metersphere.api.dto.definition.request.*; import io.metersphere.api.dto.definition.request.*;