fix(接口测试): 不同文件导入加格式判断以及swagger文件导入的问题

--user=郭雨琦
--bug=1014627
【接口测试】接口定义导入,在ms格式下不选择覆盖模式点导入,然后切换har格式没提示格式错误
https://www.tapd.cn/55049933/bugtrace/bugs/view/1155049933001014627
https://www.tapd.cn/55049933/bugtrace/bugs/view/1155049933001014636
https://www.tapd.cn/55049933/bugtrace/bugs/view/1155049933001014632
https://www.tapd.cn/55049933/bugtrace/bugs/view/1155049933001014637
This commit is contained in:
guoyuqi 2022-07-05 14:38:36 +08:00 committed by fit2-zhao
parent cb7051ff9a
commit e3bb2d41a2
7 changed files with 61 additions and 18 deletions

View File

@ -46,7 +46,7 @@ public class HarParser extends HarAbstractParser {
MSException.throwException(e.getMessage()); MSException.throwException(e.getMessage());
LogUtil.error(e.getMessage(), e); LogUtil.error(e.getMessage(), e);
} }
if (ObjectUtils.isEmpty(har)) { if (ObjectUtils.isEmpty(har) || har.log == null) {
MSException.throwException("解析失败,请确认选择的是 Har 格式!"); MSException.throwException("解析失败,请确认选择的是 Har 格式!");
} }
ApiDefinitionImport definitionImport = new ApiDefinitionImport(); ApiDefinitionImport definitionImport = new ApiDefinitionImport();

View File

@ -11,6 +11,7 @@ import io.metersphere.api.parse.MsAbstractParser;
import io.metersphere.base.domain.ApiDefinitionWithBLOBs; import io.metersphere.base.domain.ApiDefinitionWithBLOBs;
import io.metersphere.base.domain.ApiTestCaseWithBLOBs; import io.metersphere.base.domain.ApiTestCaseWithBLOBs;
import io.metersphere.commons.constants.ApiImportPlatform; import io.metersphere.commons.constants.ApiImportPlatform;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.CommonBeanFactory; import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.commons.utils.LogUtil; import io.metersphere.commons.utils.LogUtil;
import io.metersphere.commons.utils.SessionUtils; import io.metersphere.commons.utils.SessionUtils;
@ -32,7 +33,9 @@ public class MsDefinitionParser extends MsAbstractParser<ApiDefinitionImport> {
String testStr = getApiTestStr(source); String testStr = getApiTestStr(source);
JSONObject testObject = JSONObject.parseObject(testStr, Feature.DisableSpecialKeyDetect); JSONObject testObject = JSONObject.parseObject(testStr, Feature.DisableSpecialKeyDetect);
this.projectId = request.getProjectId(); this.projectId = request.getProjectId();
if (this.projectId == null) {
MSException.throwException("wrong format");
}
if (testObject.get("projectName") != null || testObject.get("projectId") != null) {// metersphere 格式导入 if (testObject.get("projectName") != null || testObject.get("projectId") != null) {// metersphere 格式导入
return parseMsFormat(testStr, request); return parseMsFormat(testStr, request);
} else { // chrome 插件录制格式导入 } else { // chrome 插件录制格式导入

View File

@ -1,6 +1,7 @@
package io.metersphere.api.dto.definition.parse; package io.metersphere.api.dto.definition.parse;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
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.request.sampler.MsHTTPSamplerProxy; import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy;
@ -14,6 +15,7 @@ import io.metersphere.base.domain.ApiTestCaseWithBLOBs;
import io.metersphere.base.domain.Project; import io.metersphere.base.domain.Project;
import io.metersphere.base.mapper.ProjectMapper; import io.metersphere.base.mapper.ProjectMapper;
import io.metersphere.commons.constants.ProjectApplicationType; import io.metersphere.commons.constants.ProjectApplicationType;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.BeanUtils; 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;
@ -29,6 +31,16 @@ public class PostmanDefinitionParser extends PostmanAbstractParserParser<ApiDefi
public ApiDefinitionImport parse(InputStream source, ApiTestImportRequest request) { public ApiDefinitionImport parse(InputStream source, ApiTestImportRequest request) {
String testStr = getApiTestStr(source); String testStr = getApiTestStr(source);
this.projectId = request.getProjectId(); this.projectId = request.getProjectId();
JSONObject jsonObject = JSON.parseObject(testStr);
Object info = jsonObject.get("info");
if (info == null) {
MSException.throwException("wrong format");
} else {
JSONObject jsonObject1 = JSON.parseObject(info.toString());
if (jsonObject1.get("_postman_id") == null) {
MSException.throwException("wrong format");
}
}
PostmanCollection postmanCollection = JSON.parseObject(testStr, PostmanCollection.class, Feature.DisableSpecialKeyDetect); PostmanCollection postmanCollection = JSON.parseObject(testStr, PostmanCollection.class, Feature.DisableSpecialKeyDetect);
List<PostmanKeyValue> variables = postmanCollection.getVariable(); List<PostmanKeyValue> variables = postmanCollection.getVariable();
ApiDefinitionImport apiImport = new ApiDefinitionImport(); ApiDefinitionImport apiImport = new ApiDefinitionImport();

View File

@ -46,6 +46,12 @@ public class Swagger2Parser extends SwaggerAbstractParser {
} else { } else {
sourceStr = getApiTestStr(source); // 导入的二进制文件转换为 String sourceStr = getApiTestStr(source); // 导入的二进制文件转换为 String
JSONObject jsonObject = JSONObject.parseObject(sourceStr);
if (jsonObject.get("swagger") == null || jsonObject.get("swagger") == "null" || jsonObject.get("swagger") == " ") {
if (jsonObject.get("openapi") == null || jsonObject.get("openapi") == "null" || jsonObject.get("openapi") == " ") {
MSException.throwException("wrong format");
}
}
swagger = new SwaggerParser().readWithInfo(sourceStr, false).getSwagger(); swagger = new SwaggerParser().readWithInfo(sourceStr, false).getSwagger();
} }
if (swagger == null || swagger.getSwagger() == null) { // 不是 2.0 版本则尝试转换 3.0 if (swagger == null || swagger.getSwagger() == null) { // 不是 2.0 版本则尝试转换 3.0
@ -152,6 +158,9 @@ public class Swagger2Parser extends SwaggerAbstractParser {
} }
private String getModulePath(List<String> tagTree, StringBuilder modulePath) { private String getModulePath(List<String> tagTree, StringBuilder modulePath) {
if (tagTree == null) {
return "";
}
for (String s : tagTree) { for (String s : tagTree) {
if (s.contains("/")) { if (s.contains("/")) {
String[] split = s.split("/"); String[] split = s.split("/");

View File

@ -1252,9 +1252,24 @@ public class ApiDefinitionService {
public ApiDefinitionImport apiTestImport(MultipartFile file, ApiTestImportRequest request) { public ApiDefinitionImport apiTestImport(MultipartFile file, ApiTestImportRequest request) {
//通过platform获取对应的导入解析类型 //通过platform获取对应的导入解析类型
String originalFilename = file.getOriginalFilename();
String suffixName = originalFilename.substring(originalFilename.indexOf("."));
if (suffixName.equalsIgnoreCase("jmx")) {
if (!request.getPlatform().equalsIgnoreCase("JMeter")) {
MSException.throwException("文件格式不符合要求");
}
}
if (suffixName.equalsIgnoreCase("har")) {
if (!request.getPlatform().equalsIgnoreCase("Har")) {
MSException.throwException("文件格式不符合要求");
}
}
ApiImportParser runService = ApiDefinitionImportParserFactory.getApiImportParser(request.getPlatform()); ApiImportParser runService = ApiDefinitionImportParserFactory.getApiImportParser(request.getPlatform());
ApiDefinitionImport apiImport = null; ApiDefinitionImport apiImport = null;
if (StringUtils.isNotBlank(request.getSwaggerUrl())) { if (StringUtils.isNotBlank(request.getSwaggerUrl())) {
if (!request.getPlatform().equalsIgnoreCase("Swagger2")) {
MSException.throwException("文件格式不符合要求");
}
if (!UrlTestUtils.testUrlWithTimeOut(request.getSwaggerUrl(), 30000)) { if (!UrlTestUtils.testUrlWithTimeOut(request.getSwaggerUrl(), 30000)) {
MSException.throwException(Translator.get("connection_timeout")); MSException.throwException(Translator.get("connection_timeout"));
} }
@ -1273,9 +1288,13 @@ public class ApiDefinitionService {
String returnThrowException = e.getMessage(); String returnThrowException = e.getMessage();
if (StringUtils.contains(returnThrowException, "模块树最大深度为")) { if (StringUtils.contains(returnThrowException, "模块树最大深度为")) {
MSException.throwException(returnThrowException); MSException.throwException(returnThrowException);
} else {
if (returnThrowException.equals("wrong format")) {
MSException.throwException("文件格式不符合要求");
} else { } else {
MSException.throwException(Translator.get("parse_data_error")); MSException.throwException(Translator.get("parse_data_error"));
} }
}
// 发送通知 // 发送通知
if (StringUtils.equals(request.getType(), "schedule")) { if (StringUtils.equals(request.getType(), "schedule")) {
String scheduleId = scheduleService.getScheduleInfo(request.getResourceId()); String scheduleId = scheduleService.getScheduleInfo(request.getResourceId());
@ -1589,11 +1608,6 @@ public class ApiDefinitionService {
(query) -> extApiDefinitionMapper.selectIds(query)); (query) -> extApiDefinitionMapper.selectIds(query));
this.removeToGc(request.getIds()); this.removeToGc(request.getIds());
// ApiDefinitionExampleWithOperation example = new ApiDefinitionExampleWithOperation();
// example.createCriteria().andIdIn(request.getIds());
// example.setOperator(SessionUtils.getUserId());
// example.setOperationTime(System.currentTimeMillis());
// extApiDefinitionMapper.removeToGcByExample(example);
} }
public Pager<List<ApiDefinitionResult>> listRelevance(ApiDefinitionRequest request, int goPage, int pageSize) { public Pager<List<ApiDefinitionResult>> listRelevance(ApiDefinitionRequest request, int goPage, int pageSize) {

View File

@ -89,13 +89,6 @@ public class ApiModuleService extends NodeTreeService<ApiModuleDTO> {
Map<String, List<String>> filters = new LinkedHashMap<>(); Map<String, List<String>> filters = new LinkedHashMap<>();
filters.put("status", list); filters.put("status", list);
request.setFilters(filters); request.setFilters(filters);
// apiModules.forEach(node -> {
// List<String> moduleIds = new ArrayList<>();
// moduleIds = this.nodeList(apiModules, node.getId(), moduleIds);
// moduleIds.add(node.getId());
// request.setModuleIds(moduleIds);
// node.setCaseNum(extApiDefinitionMapper.moduleCount(request));
// });
//优化 所有统计SQL一次查询出来 //优化 所有统计SQL一次查询出来
List<String> allModuleIdList = new ArrayList<>(); List<String> allModuleIdList = new ArrayList<>();
@ -786,6 +779,8 @@ public class ApiModuleService extends NodeTreeService<ApiModuleDTO> {
apiDefinitionWithBLOBs.setVersionId(v.getVersionId()); apiDefinitionWithBLOBs.setVersionId(v.getVersionId());
apiDefinitionWithBLOBs.setModuleId(v.getModuleId()); apiDefinitionWithBLOBs.setModuleId(v.getModuleId());
apiDefinitionWithBLOBs.setModulePath(v.getModulePath()); apiDefinitionWithBLOBs.setModulePath(v.getModulePath());
apiDefinitionWithBLOBs.setNum(v.getNum());
apiDefinitionWithBLOBs.setStatus(v.getStatus());
toUpdateList.add(apiDefinitionWithBLOBs); toUpdateList.add(apiDefinitionWithBLOBs);
} }
}); });
@ -801,6 +796,8 @@ public class ApiModuleService extends NodeTreeService<ApiModuleDTO> {
if (apiDefinitionWithBLOBs != null) { if (apiDefinitionWithBLOBs != null) {
apiDefinitionWithBLOBs.setId(v.getId()); apiDefinitionWithBLOBs.setId(v.getId());
apiDefinitionWithBLOBs.setVersionId(v.getVersionId()); apiDefinitionWithBLOBs.setVersionId(v.getVersionId());
apiDefinitionWithBLOBs.setNum(v.getNum());
apiDefinitionWithBLOBs.setStatus(v.getStatus());
toUpdateList.add(apiDefinitionWithBLOBs); toUpdateList.add(apiDefinitionWithBLOBs);
} }
}); });
@ -964,6 +961,8 @@ public class ApiModuleService extends NodeTreeService<ApiModuleDTO> {
api.setOrder(definitionWithBLOBs.getOrder()); api.setOrder(definitionWithBLOBs.getOrder());
api.setRefId(apiDefinitionWithBLOBs.getRefId()); api.setRefId(apiDefinitionWithBLOBs.getRefId());
api.setLatest(apiDefinitionWithBLOBs.getLatest()); api.setLatest(apiDefinitionWithBLOBs.getLatest());
api.setNum(definitionWithBLOBs.getNum());
api.setStatus(definitionWithBLOBs.getStatus());
coverApiList.add(api); coverApiList.add(api);
} }
optionData.remove(apiDefinitionWithBLOBs); optionData.remove(apiDefinitionWithBLOBs);
@ -981,6 +980,8 @@ public class ApiModuleService extends NodeTreeService<ApiModuleDTO> {
ApiDefinitionWithBLOBs api = new ApiDefinitionWithBLOBs(); ApiDefinitionWithBLOBs api = new ApiDefinitionWithBLOBs();
BeanUtils.copyBean(api, apiDefinitionWithBLOBs); BeanUtils.copyBean(api, apiDefinitionWithBLOBs);
api.setId(definitionWithBLOBs.getId()); api.setId(definitionWithBLOBs.getId());
api.setNum(definitionWithBLOBs.getNum());
api.setStatus(definitionWithBLOBs.getStatus());
api.setVersionId(definitionWithBLOBs.getVersionId()); api.setVersionId(definitionWithBLOBs.getVersionId());
api.setModuleId(definitionWithBLOBs.getModuleId()); api.setModuleId(definitionWithBLOBs.getModuleId());
api.setModulePath(definitionWithBLOBs.getModulePath()); api.setModulePath(definitionWithBLOBs.getModulePath());

View File

@ -621,6 +621,8 @@ public class ApiScenarioModuleService extends NodeTreeService<ApiScenarioModuleD
apiScenarioWithBLOBs.setVersionId(v.getVersionId()); apiScenarioWithBLOBs.setVersionId(v.getVersionId());
apiScenarioWithBLOBs.setApiScenarioModuleId(v.getApiScenarioModuleId()); apiScenarioWithBLOBs.setApiScenarioModuleId(v.getApiScenarioModuleId());
apiScenarioWithBLOBs.setModulePath(v.getModulePath()); apiScenarioWithBLOBs.setModulePath(v.getModulePath());
apiScenarioWithBLOBs.setNum(v.getNum());
apiScenarioWithBLOBs.setStatus(v.getStatus());
toUpdateList.add(apiScenarioWithBLOBs); toUpdateList.add(apiScenarioWithBLOBs);
} }
}); });
@ -640,6 +642,8 @@ public class ApiScenarioModuleService extends NodeTreeService<ApiScenarioModuleD
if (apiScenarioWithBLOBs != null) { if (apiScenarioWithBLOBs != null) {
apiScenarioWithBLOBs.setId(v.getId()); apiScenarioWithBLOBs.setId(v.getId());
apiScenarioWithBLOBs.setVersionId(v.getVersionId()); apiScenarioWithBLOBs.setVersionId(v.getVersionId());
apiScenarioWithBLOBs.setNum(v.getNum());
apiScenarioWithBLOBs.setStatus(v.getStatus());
toUpdateList.add(apiScenarioWithBLOBs); toUpdateList.add(apiScenarioWithBLOBs);
} }
}); });