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("/" + msScenario.getName());
scenarioWithBLOBsList.add(scenarioWithBLOBs);
}

View File

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

View File

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

View File

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

View File

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

View File

@ -144,6 +144,7 @@ public class Swagger2Parser extends SwaggerAbstractParser {
/*
buildModule(selectModule, apiDefinition, operation.getTags(), selectModulePath);
*/
buildModulePath(apiDefinition, operation.getTags());
if (operation.isDeprecated() != null && operation.isDeprecated()) {
apiDefinition.setTags("[\"Deleted\"]");
}
@ -155,6 +156,28 @@ public class Swagger2Parser extends SwaggerAbstractParser {
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) {
String name = "";
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.parser.Feature;
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.parse.swagger.SwaggerApiInfo;
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.KeyValue;
import io.metersphere.api.dto.scenario.request.RequestType;
import io.metersphere.api.service.ApiModuleService;
import io.metersphere.base.domain.ApiDefinitionWithBLOBs;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.commons.utils.XMLUtils;
import io.swagger.parser.OpenAPIParser;
@ -130,15 +127,6 @@ public class Swagger3Parser extends SwaggerAbstractParser {
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) {
PathItem pathItem = paths.get(pathName);
@ -166,9 +154,8 @@ public class Swagger3Parser extends SwaggerAbstractParser {
} // 有数据的话去掉 Kvs 里初始化的第一个全 null 的数据否则有空行
apiDefinition.setRequest(JSON.toJSONString(request));
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()) {
apiDefinition.setTags("[\"Deleted\"]");
}
@ -180,6 +167,12 @@ public class Swagger3Parser extends SwaggerAbstractParser {
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) {
String name;
if (StringUtils.isNotBlank(operation.getSummary())) {
@ -589,15 +582,28 @@ public class Swagger3Parser extends SwaggerAbstractParser {
SwaggerApiInfo swaggerApiInfo = new SwaggerApiInfo(); // {tags:, summary:, description:, parameters:}
swaggerApiInfo.setSummary(apiDefinition.getName());
// 设置导入后的模块名 根据 api moduleID 查库获得所属模块作为导出的模块名
ApiModuleService apiModuleService = CommonBeanFactory.getBean(ApiModuleService.class);
/*ApiModuleService apiModuleService = CommonBeanFactory.getBean(ApiModuleService.class);
String moduleName = "";
if (apiDefinition.getModuleId() != null) { // module_id 可能为空
ApiModuleDTO node = apiModuleService.getNode(apiDefinition.getModuleId());
if (node != null) {
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);
}
if (modulePath.contains("/")) {
split = modulePath.split("/");
}
swaggerApiInfo.setTags(Arrays.asList(split));
}
swaggerApiInfo.setTags(Arrays.asList(moduleName));
// 设置请求体
JSONObject requestObject = JSON.parseObject(apiDefinition.getRequest(), Feature.DisableSpecialKeyDetect); // 将api的request属性转换成JSON对象以便获得参数
JSONObject requestBody = buildRequestBody(requestObject);
@ -991,4 +997,21 @@ public class Swagger3Parser extends SwaggerAbstractParser {
}
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());
}
if (moduleList != null) {
for (ApiModule apiModule : moduleList) {
apiModuleMapper.insert(apiModule);
}
}
for (int i = 0; i < data.size(); i++) {
ApiDefinitionWithBLOBs item = data.get(i);
this.setModule(item);
@ -1420,11 +1426,6 @@ public class ApiDefinitionService {
} else {
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) {
sqlSession.flushStatements();
}

View File

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

View File

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