feat(接口测试): 场景接口导入文件修改

--user=郭雨琦
This commit is contained in:
guoyuqi 2022-06-30 17:18:52 +08:00 committed by xiaomeinvG
parent da3e7b6891
commit c970a77d05
10 changed files with 104 additions and 27 deletions

View File

@ -76,6 +76,7 @@ public class HarScenarioParser extends HarScenarioAbstractParser<ScenarioImport>
scenarioWithBLOBs.setModulePath("/" + module.getName()); scenarioWithBLOBs.setModulePath("/" + module.getName());
} }
}*/ }*/
scenarioWithBLOBs.setModulePath("/" + msScenario.getName());
scenarioWithBLOBsList.add(scenarioWithBLOBs); scenarioWithBLOBsList.add(scenarioWithBLOBs);
} }

View File

@ -127,6 +127,7 @@ public class MsJmeterParser extends ApiImportAbstractParser<ScenarioImport> {
scenarioWithBLOBs.setProjectId(request.getProjectId()); scenarioWithBLOBs.setProjectId(request.getProjectId());
if (msScenario != null && CollectionUtils.isNotEmpty(msScenario.getHashTree())) { if (msScenario != null && CollectionUtils.isNotEmpty(msScenario.getHashTree())) {
scenarioWithBLOBs.setStepTotal(msScenario.getHashTree().size()); scenarioWithBLOBs.setStepTotal(msScenario.getHashTree().size());
scenarioWithBLOBs.setModulePath("/" + msScenario.getName());
} }
/*if (module != null) { /*if (module != null) {
scenarioWithBLOBs.setApiScenarioModuleId(module.getId()); scenarioWithBLOBs.setApiScenarioModuleId(module.getId());

View File

@ -58,6 +58,7 @@ public class PostmanScenarioParser extends PostmanAbstractParserParser<ScenarioI
scenarioWithBLOBs.setModulePath("/" + module.getName()); scenarioWithBLOBs.setModulePath("/" + module.getName());
} }
}*/ }*/
scenarioWithBLOBs.setModulePath("/" + msScenario.getName());
scenarioWithBLOBsList.add(scenarioWithBLOBs); scenarioWithBLOBsList.add(scenarioWithBLOBs);
} }

View File

@ -288,6 +288,7 @@ public class JmeterDefinitionParser extends ApiImportAbstractParser<ApiDefinitio
apiDefinition.setModulePath("/" + this.apiModule.getName()); apiDefinition.setModulePath("/" + this.apiModule.getName());
} }
}*/ }*/
apiDefinition.setModulePath("/" + this.planName);
// todo 除HTTP协议外其它协议设置默认模块 // todo 除HTTP协议外其它协议设置默认模块
apiDefinition.setStatus("Prepare"); apiDefinition.setStatus("Prepare");
apiDefinition.setProtocol(protocol); apiDefinition.setProtocol(protocol);

View File

@ -18,6 +18,7 @@ import io.metersphere.commons.utils.BeanUtils;
import io.metersphere.commons.utils.CommonBeanFactory; import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.dto.ProjectConfig; import io.metersphere.dto.ProjectConfig;
import io.metersphere.service.ProjectApplicationService; import io.metersphere.service.ProjectApplicationService;
import org.apache.commons.lang3.StringUtils;
import java.io.InputStream; import java.io.InputStream;
import java.util.*; import java.util.*;
@ -42,6 +43,10 @@ public class PostmanDefinitionParser extends PostmanAbstractParserParser<ApiDefi
} }
ApiModule apiModule = ApiDefinitionImportUtil.buildModule(this.selectModule, postmanCollection.getInfo().getName(), this.projectId);*/ ApiModule apiModule = ApiDefinitionImportUtil.buildModule(this.selectModule, postmanCollection.getInfo().getName(), this.projectId);*/
String modulePath = null;
if (StringUtils.isNotBlank(postmanCollection.getInfo().getName())) {
modulePath = "/" + postmanCollection.getInfo().getName();
}
List<ApiTestCaseWithBLOBs> cases = new ArrayList<>(); List<ApiTestCaseWithBLOBs> cases = new ArrayList<>();
Map<String, String> repeatMap = new HashMap(); Map<String, String> repeatMap = new HashMap();
ProjectMapper projectMapper = CommonBeanFactory.getBean(ProjectMapper.class); ProjectMapper projectMapper = CommonBeanFactory.getBean(ProjectMapper.class);
@ -49,7 +54,7 @@ public class PostmanDefinitionParser extends PostmanAbstractParserParser<ApiDefi
ProjectApplicationService projectApplicationService = CommonBeanFactory.getBean(ProjectApplicationService.class); ProjectApplicationService projectApplicationService = CommonBeanFactory.getBean(ProjectApplicationService.class);
ProjectConfig config = projectApplicationService.getSpecificTypeValue(project.getId(), ProjectApplicationType.URL_REPEATABLE.name()); ProjectConfig config = projectApplicationService.getSpecificTypeValue(project.getId(), ProjectApplicationType.URL_REPEATABLE.name());
boolean urlRepeat = config.getUrlRepeatable(); boolean urlRepeat = config.getUrlRepeatable();
parseItem(postmanCollection.getItem(), variables, results, parseItem(postmanCollection.getItem(), modulePath, variables, results,
cases, repeatMap, urlRepeat); cases, repeatMap, urlRepeat);
Collections.reverse(results); // 调整顺序 Collections.reverse(results); // 调整顺序
Collections.reverse(cases); Collections.reverse(cases);
@ -58,14 +63,17 @@ public class PostmanDefinitionParser extends PostmanAbstractParserParser<ApiDefi
return apiImport; return apiImport;
} }
protected void parseItem(List<PostmanItem> items, List<PostmanKeyValue> variables, List<ApiDefinitionWithBLOBs> results, protected void parseItem(List<PostmanItem> items, String modulePath, List<PostmanKeyValue> variables, List<ApiDefinitionWithBLOBs> results,
List<ApiTestCaseWithBLOBs> cases, Map<String, String> repeatMap, Boolean repeatable) { List<ApiTestCaseWithBLOBs> cases, Map<String, String> repeatMap, Boolean repeatable) {
for (PostmanItem item : items) { for (PostmanItem item : items) {
List<PostmanItem> childItems = item.getItem(); List<PostmanItem> childItems = item.getItem();
if (childItems != null) { if (childItems != null) {
/*ApiModule module = null; /*ApiModule module = null;
module = ApiDefinitionImportUtil.buildModule(parentModule, item.getName(), this.projectId);*/ module = ApiDefinitionImportUtil.buildModule(parentModule, item.getName(), this.projectId);*/
parseItem(childItems, variables, results, cases, repeatMap, repeatable); if (StringUtils.isNotBlank(modulePath) && StringUtils.isNotBlank(item.getName())) {
modulePath = modulePath + "/" + item.getName();
}
parseItem(childItems, modulePath, variables, results, cases, repeatMap, repeatable);
} else { } else {
MsHTTPSamplerProxy msHTTPSamplerProxy = parsePostman(item); MsHTTPSamplerProxy msHTTPSamplerProxy = parsePostman(item);
HttpResponse response = parsePostmanResponse(item); HttpResponse response = parsePostmanResponse(item);
@ -82,6 +90,9 @@ public class PostmanDefinitionParser extends PostmanAbstractParserParser<ApiDefi
request.setModulePath("/" + path); request.setModulePath("/" + path);
} }
}*/ }*/
if (StringUtils.isNotBlank(modulePath)) {
request.setModulePath(modulePath);
}
if (request != null) { if (request != null) {
if (repeatMap.keySet().contains(request.getMethod() + request.getPath()) if (repeatMap.keySet().contains(request.getMethod() + request.getPath())
&& (repeatable == null || repeatable == false)) { && (repeatable == null || repeatable == false)) {

View File

@ -144,6 +144,7 @@ public class Swagger2Parser extends SwaggerAbstractParser {
/* /*
buildModule(selectModule, apiDefinition, operation.getTags(), selectModulePath); buildModule(selectModule, apiDefinition, operation.getTags(), selectModulePath);
*/ */
buildModulePath(apiDefinition, operation.getTags());
if (operation.isDeprecated() != null && operation.isDeprecated()) { if (operation.isDeprecated() != null && operation.isDeprecated()) {
apiDefinition.setTags("[\"Deleted\"]"); apiDefinition.setTags("[\"Deleted\"]");
} }
@ -155,6 +156,28 @@ public class Swagger2Parser extends SwaggerAbstractParser {
return results; return results;
} }
private void buildModulePath(ApiDefinitionWithBLOBs apiDefinition, List<String> tags) {
StringBuilder modulePathBuilder = new StringBuilder();
String modulePath = getModulePath(tags, modulePathBuilder);
apiDefinition.setModulePath(modulePath);
}
private String getModulePath(List<String> tagTree, StringBuilder modulePath) {
for (String s : tagTree) {
if (s.contains("/")) {
String[] split = s.split("/");
if (split.length > 0) {
getModulePath(List.of(split), modulePath);
}
} else {
if (StringUtils.isNotBlank(s)) {
modulePath.append("/").append(s);
}
}
}
return modulePath.toString();
}
private ApiDefinitionWithBLOBs 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 = ""; String name = "";
if (StringUtils.isNotBlank(operation.getSummary())) { if (StringUtils.isNotBlank(operation.getSummary())) {

View File

@ -5,7 +5,6 @@ import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature; import com.alibaba.fastjson.parser.Feature;
import io.metersphere.api.dto.ApiTestImportRequest; import io.metersphere.api.dto.ApiTestImportRequest;
import io.metersphere.api.dto.definition.ApiModuleDTO;
import io.metersphere.api.dto.definition.SwaggerApiExportResult; import io.metersphere.api.dto.definition.SwaggerApiExportResult;
import io.metersphere.api.dto.definition.parse.swagger.SwaggerApiInfo; import io.metersphere.api.dto.definition.parse.swagger.SwaggerApiInfo;
import io.metersphere.api.dto.definition.parse.swagger.SwaggerInfo; import io.metersphere.api.dto.definition.parse.swagger.SwaggerInfo;
@ -16,10 +15,8 @@ import io.metersphere.api.dto.definition.response.HttpResponse;
import io.metersphere.api.dto.scenario.Body; import io.metersphere.api.dto.scenario.Body;
import io.metersphere.api.dto.scenario.KeyValue; import io.metersphere.api.dto.scenario.KeyValue;
import io.metersphere.api.dto.scenario.request.RequestType; 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.ApiDefinitionWithBLOBs;
import io.metersphere.commons.exception.MSException; import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.commons.utils.LogUtil; import io.metersphere.commons.utils.LogUtil;
import io.metersphere.commons.utils.XMLUtils; import io.metersphere.commons.utils.XMLUtils;
import io.swagger.parser.OpenAPIParser; import io.swagger.parser.OpenAPIParser;
@ -130,15 +127,6 @@ public class Swagger3Parser extends SwaggerAbstractParser {
List<ApiDefinitionWithBLOBs> results = new ArrayList<>(); List<ApiDefinitionWithBLOBs> results = new ArrayList<>();
/*ApiModule selectModule = null;
String selectModulePath = null;
if (StringUtils.isNotBlank(importRequest.getModuleId())) {
selectModule = ApiDefinitionImportUtil.getSelectModule(importRequest.getModuleId());
if (selectModule != null) {
selectModulePath = ApiDefinitionImportUtil.getSelectModulePath(selectModule.getName(), selectModule.getParentId());
}
}*/
for (String pathName : pathNames) { for (String pathName : pathNames) {
PathItem pathItem = paths.get(pathName); PathItem pathItem = paths.get(pathName);
@ -166,9 +154,8 @@ public class Swagger3Parser extends SwaggerAbstractParser {
} // 有数据的话去掉 Kvs 里初始化的第一个全 null 的数据否则有空行 } // 有数据的话去掉 Kvs 里初始化的第一个全 null 的数据否则有空行
apiDefinition.setRequest(JSON.toJSONString(request)); apiDefinition.setRequest(JSON.toJSONString(request));
apiDefinition.setResponse(JSON.toJSONString(parseResponse(operation.getResponses()))); apiDefinition.setResponse(JSON.toJSONString(parseResponse(operation.getResponses())));
/* //buildModule(selectModule, apiDefinition, operation.getTags(), selectModulePath);
buildModule(selectModule, apiDefinition, operation.getTags(), selectModulePath); buildModulePath(apiDefinition, operation.getTags());
*/
if (operation.getDeprecated() != null && operation.getDeprecated()) { if (operation.getDeprecated() != null && operation.getDeprecated()) {
apiDefinition.setTags("[\"Deleted\"]"); apiDefinition.setTags("[\"Deleted\"]");
} }
@ -180,6 +167,12 @@ public class Swagger3Parser extends SwaggerAbstractParser {
return results; return results;
} }
private void buildModulePath(ApiDefinitionWithBLOBs apiDefinition, List<String> tags) {
StringBuilder modulePathBuilder = new StringBuilder();
String modulePath = getModulePath(tags, modulePathBuilder);
apiDefinition.setModulePath(modulePath);
}
private ApiDefinitionWithBLOBs 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; String name;
if (StringUtils.isNotBlank(operation.getSummary())) { if (StringUtils.isNotBlank(operation.getSummary())) {
@ -589,15 +582,28 @@ public class Swagger3Parser extends SwaggerAbstractParser {
SwaggerApiInfo swaggerApiInfo = new SwaggerApiInfo(); // {tags:, summary:, description:, parameters:} SwaggerApiInfo swaggerApiInfo = new SwaggerApiInfo(); // {tags:, summary:, description:, parameters:}
swaggerApiInfo.setSummary(apiDefinition.getName()); swaggerApiInfo.setSummary(apiDefinition.getName());
// 设置导入后的模块名 根据 api moduleID 查库获得所属模块作为导出的模块名 // 设置导入后的模块名 根据 api moduleID 查库获得所属模块作为导出的模块名
ApiModuleService apiModuleService = CommonBeanFactory.getBean(ApiModuleService.class); /*ApiModuleService apiModuleService = CommonBeanFactory.getBean(ApiModuleService.class);
String moduleName = ""; String moduleName = "";
if (apiDefinition.getModuleId() != null) { // module_id 可能为空 if (apiDefinition.getModuleId() != null) { // module_id 可能为空
ApiModuleDTO node = apiModuleService.getNode(apiDefinition.getModuleId()); ApiModuleDTO node = apiModuleService.getNode(apiDefinition.getModuleId());
if (node != null) { if (node != null) {
moduleName = node.getName(); moduleName = node.getName();
} }
}*/
//直接导出完整路径
if (apiDefinition.getModulePath() != null) {
String[] split = new String[0];
String modulePath = apiDefinition.getModulePath();
String substring = modulePath.substring(0, 1);
if (substring.equals("/")) {
modulePath = modulePath.substring(1);
} }
swaggerApiInfo.setTags(Arrays.asList(moduleName)); if (modulePath.contains("/")) {
split = modulePath.split("/");
}
swaggerApiInfo.setTags(Arrays.asList(split));
}
// 设置请求体 // 设置请求体
JSONObject requestObject = JSON.parseObject(apiDefinition.getRequest(), Feature.DisableSpecialKeyDetect); // 将api的request属性转换成JSON对象以便获得参数 JSONObject requestObject = JSON.parseObject(apiDefinition.getRequest(), Feature.DisableSpecialKeyDetect); // 将api的request属性转换成JSON对象以便获得参数
JSONObject requestBody = buildRequestBody(requestObject); JSONObject requestBody = buildRequestBody(requestObject);
@ -991,4 +997,21 @@ public class Swagger3Parser extends SwaggerAbstractParser {
} }
return content; return content;
} }
private String getModulePath(List<String> tagTree, StringBuilder modulePath) {
for (String s : tagTree) {
if (s.contains("/")) {
String[] split = s.split("/");
if (split.length > 0) {
getModulePath(List.of(split), modulePath);
}
} else {
if (StringUtils.isNotBlank(s)) {
modulePath.append("/").append(s);
}
}
}
return modulePath.toString();
}
} }

View File

@ -1399,6 +1399,12 @@ public class ApiDefinitionService {
num = getNextNum(data.get(0).getProjectId()); num = getNextNum(data.get(0).getProjectId());
} }
if (moduleList != null) {
for (ApiModule apiModule : moduleList) {
apiModuleMapper.insert(apiModule);
}
}
for (int i = 0; i < data.size(); i++) { for (int i = 0; i < data.size(); i++) {
ApiDefinitionWithBLOBs item = data.get(i); ApiDefinitionWithBLOBs item = data.get(i);
this.setModule(item); this.setModule(item);
@ -1420,11 +1426,6 @@ public class ApiDefinitionService {
} else { } else {
importCreate(item, batchMapper, apiTestCaseMapper, extApiDefinitionMapper, request, apiImport.getCases(), apiImport.getMocks(), updateList); importCreate(item, batchMapper, apiTestCaseMapper, extApiDefinitionMapper, request, apiImport.getCases(), apiImport.getMocks(), updateList);
} }
if (moduleList != null) {
for (ApiModule apiModule : moduleList) {
apiModuleMapper.insert(apiModule);
}
}
if (i % 300 == 0) { if (i % 300 == 0) {
sqlSession.flushStatements(); sqlSession.flushStatements();
} }

View File

@ -618,6 +618,10 @@ public class ApiModuleService extends NodeTreeService<ApiModuleDTO> {
fullCoverage = false; fullCoverage = false;
} }
if (fullCoverageApi == null) {
fullCoverageApi = false;
}
//标准版ESB数据导入不区分是否覆盖默认都为覆盖 //标准版ESB数据导入不区分是否覆盖默认都为覆盖
if (apiImport.getEsbApiParamsMap() != null) { if (apiImport.getEsbApiParamsMap() != null) {
fullCoverage = true; fullCoverage = true;
@ -810,6 +814,9 @@ public class ApiModuleService extends NodeTreeService<ApiModuleDTO> {
BeanUtils.copyBean(api, apiDefinitionWithBLOBs); BeanUtils.copyBean(api, apiDefinitionWithBLOBs);
api.setId(definitionWithBLOBs.getId()); api.setId(definitionWithBLOBs.getId());
api.setVersionId(definitionWithBLOBs.getVersionId()); api.setVersionId(definitionWithBLOBs.getVersionId());
api.setOrder(definitionWithBLOBs.getOrder());
api.setRefId(apiDefinitionWithBLOBs.getRefId());
api.setLatest(apiDefinitionWithBLOBs.getLatest());
coverApiList.add(api); coverApiList.add(api);
} }
optionData.remove(apiDefinitionWithBLOBs); optionData.remove(apiDefinitionWithBLOBs);
@ -830,6 +837,9 @@ public class ApiModuleService extends NodeTreeService<ApiModuleDTO> {
api.setVersionId(definitionWithBLOBs.getVersionId()); api.setVersionId(definitionWithBLOBs.getVersionId());
api.setModuleId(definitionWithBLOBs.getModuleId()); api.setModuleId(definitionWithBLOBs.getModuleId());
api.setModulePath(definitionWithBLOBs.getModulePath()); api.setModulePath(definitionWithBLOBs.getModulePath());
api.setOrder(definitionWithBLOBs.getOrder());
api.setRefId(apiDefinitionWithBLOBs.getRefId());
api.setLatest(apiDefinitionWithBLOBs.getLatest());
coverApiList.add(api); coverApiList.add(api);
} }
optionData.remove(apiDefinitionWithBLOBs); optionData.remove(apiDefinitionWithBLOBs);
@ -963,6 +973,7 @@ public class ApiModuleService extends NodeTreeService<ApiModuleDTO> {
parentModule.setProjectId(pidChildrenMap.get("root").get(0).getProjectId()); parentModule.setProjectId(pidChildrenMap.get("root").get(0).getProjectId());
parentModule.setId("root"); parentModule.setId("root");
parentModule.setLevel(0); parentModule.setLevel(0);
parentModule.setProtocol(pidChildrenMap.get("root").get(0).getProtocol());
} else { } else {
if (!parentModuleList.isEmpty() && parentModule == null) { if (!parentModuleList.isEmpty() && parentModule == null) {
String parentId = parentModuleList.get(0).getParentId(); String parentId = parentModuleList.get(0).getParentId();
@ -1024,7 +1035,6 @@ public class ApiModuleService extends NodeTreeService<ApiModuleDTO> {
Map<String, List<ApiModuleDTO>> idChildrenMap = new HashMap<>(); Map<String, List<ApiModuleDTO>> idChildrenMap = new HashMap<>();
int i = 0; int i = 0;
Map<String, List<ApiModule>> idModuleMap = new HashMap<>(); Map<String, List<ApiModule>> idModuleMap = new HashMap<>();
List<ApiModule> moduleList = new ArrayList<>();
for (ApiModuleDTO apiModuleDTO : nodeTreeByProjectId) { for (ApiModuleDTO apiModuleDTO : nodeTreeByProjectId) {
if (StringUtils.isBlank(apiModuleDTO.getParentId())) { if (StringUtils.isBlank(apiModuleDTO.getParentId())) {
apiModuleDTO.setParentId("root"); apiModuleDTO.setParentId("root");
@ -1050,7 +1060,11 @@ public class ApiModuleService extends NodeTreeService<ApiModuleDTO> {
i = i + 1; i = i + 1;
List<ApiModuleDTO> childrenList = idChildrenMap.get(apiModuleDTO.getId()); List<ApiModuleDTO> childrenList = idChildrenMap.get(apiModuleDTO.getId());
if (apiModuleDTO.getChildren() != null) { if (apiModuleDTO.getChildren() != null) {
if (childrenList != null) {
childrenList.addAll(apiModuleDTO.getChildren()); childrenList.addAll(apiModuleDTO.getChildren());
} else {
idChildrenMap.put(apiModuleDTO.getId(), apiModuleDTO.getChildren());
}
} else { } else {
if (childrenList == null) { if (childrenList == null) {
pidChildrenMap.put(apiModuleDTO.getId(), new ArrayList<>()); pidChildrenMap.put(apiModuleDTO.getId(), new ArrayList<>());

View File

@ -491,6 +491,7 @@ public class ApiScenarioModuleService extends NodeTreeService<ApiScenarioModuleD
fullCoverageScenario = false; fullCoverageScenario = false;
} }
//获取当前项目的当前协议下的所有模块的Tree //获取当前项目的当前协议下的所有模块的Tree
List<ApiScenarioModuleDTO> scenarioModules = extApiScenarioModuleMapper.getNodeTreeByProjectId(projectId); List<ApiScenarioModuleDTO> scenarioModules = extApiScenarioModuleMapper.getNodeTreeByProjectId(projectId);
List<ApiScenarioModuleDTO> nodeTreeByProjectId = this.getNodeTrees(scenarioModules); List<ApiScenarioModuleDTO> nodeTreeByProjectId = this.getNodeTrees(scenarioModules);