fix(接口测试): 修复导入含有不同协议的jmx文件时出现的非当前协议接口没有挂在默认目录下的问题

--bug=1013876 --user=宋天阳
【接口测试】github#14497,TCP在接口定义这边导入包含http的JMX后,http的API关联的模块ID不对
{#_orginal_url#}
This commit is contained in:
song-tianyang 2022-06-09 15:18:01 +08:00 committed by f2c-ci-robot[bot]
parent 7a9b16200a
commit 86771f85b7
2 changed files with 82 additions and 13 deletions

View File

@ -139,6 +139,10 @@ public class ApiDefinitionService {
@Resource
private ExtTestPlanApiCaseMapper extTestPlanApiCaseMapper;
@Lazy
@Resource
private ApiModuleService apiModuleService;
private ThreadLocal<Long> currentApiOrder = new ThreadLocal<>();
private ThreadLocal<Long> currentApiCaseOrder = new ThreadLocal<>();
@ -1134,11 +1138,11 @@ public class ApiDefinitionService {
private String getReportNameByTestId(String testId) {
String testName = extApiDefinitionMapper.selectNameById(testId);
if(StringUtils.isEmpty(testName)){
if (StringUtils.isEmpty(testName)) {
testName = extApiTestCaseMapper.selectNameById(testId);
if(StringUtils.isEmpty(testName)){
if (StringUtils.isEmpty(testName)) {
String resourceID = extTestPlanApiCaseMapper.getApiTestCaseIdById(testId);
if(StringUtils.isNotEmpty(resourceID)){
if (StringUtils.isNotEmpty(resourceID)) {
testName = extApiTestCaseMapper.selectNameById(resourceID);
}
}
@ -1273,6 +1277,7 @@ public class ApiDefinitionService {
currentApiCaseOrder.remove();
currentApiOrder.remove();
List<ApiDefinitionWithBLOBs> data = apiImport.getData();
data = this.initApiModuleId(data);
ApiDefinitionMapper batchMapper = sqlSession.getMapper(ApiDefinitionMapper.class);
ApiTestCaseMapper apiTestCaseMapper = sqlSession.getMapper(ApiTestCaseMapper.class);
ExtApiDefinitionMapper extApiDefinitionMapper = sqlSession.getMapper(ExtApiDefinitionMapper.class);
@ -1342,6 +1347,36 @@ public class ApiDefinitionService {
}
}
/**
* 初始化apiModuleId如果apiModuleId不存在则赋默认的moduleId
*
* @param apiDefinitionList
* @return
*/
private List<ApiDefinitionWithBLOBs> initApiModuleId(List<ApiDefinitionWithBLOBs> apiDefinitionList) {
Map<String, List<String>> protocalModuleIdMap = new HashMap<>();
apiDefinitionList.forEach(apiDefinition -> {
if (protocalModuleIdMap.containsKey(apiDefinition.getProtocol()) && !protocalModuleIdMap.get(apiDefinition.getProtocol()).contains(apiDefinition.getModuleId())) {
protocalModuleIdMap.get(apiDefinition.getProtocol()).add(apiDefinition.getModuleId());
} else {
protocalModuleIdMap.put(apiDefinition.getProtocol(), new ArrayList<>() {{
this.add(apiDefinition.getModuleId());
}});
}
});
Map<String, List<String>> rightfulModuleIdMap = apiModuleService.checkModuleIds(protocalModuleIdMap);
for (ApiDefinitionWithBLOBs apiBlobs : apiDefinitionList) {
if (!rightfulModuleIdMap.containsKey(apiBlobs.getProtocol()) || !rightfulModuleIdMap.get(apiBlobs.getProtocol()).contains(apiBlobs.getModuleId())) {
ApiModule defaultModule = apiModuleService.getDefaultNode(apiBlobs.getProjectId(), apiBlobs.getProtocol());
if (defaultModule != null) {
apiBlobs.setModuleId(defaultModule.getId());
apiBlobs.setModulePath(defaultModule.getName());
}
}
}
return apiDefinitionList;
}
public ReferenceDTO getReference(ApiScenarioRequest request) {
ReferenceDTO dto = new ReferenceDTO();

View File

@ -13,6 +13,7 @@ import io.metersphere.base.mapper.ext.ExtApiDefinitionMapper;
import io.metersphere.base.mapper.ext.ExtApiModuleMapper;
import io.metersphere.commons.constants.TestCaseConstants;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.commons.utils.SessionUtils;
import io.metersphere.i18n.Translator;
import io.metersphere.log.utils.ReflexObjectUtil;
@ -24,6 +25,7 @@ import io.metersphere.service.ProjectService;
import io.metersphere.track.service.TestPlanApiCaseService;
import io.metersphere.track.service.TestPlanProjectService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
@ -126,16 +128,17 @@ public class ApiModuleService extends NodeTreeService<ApiModuleDTO> {
}
private Map<String, Integer> parseModuleCountList(List<Map<String, Object>> moduleCountList) {
Map<String,Integer> returnMap = new HashMap<>();
for (Map<String, Object> map: moduleCountList){
Map<String, Integer> returnMap = new HashMap<>();
for (Map<String, Object> map : moduleCountList) {
Object moduleIdObj = map.get("moduleId");
Object countNumObj = map.get("countNum");
if(moduleIdObj!= null && countNumObj != null){
if (moduleIdObj != null && countNumObj != null) {
String moduleId = String.valueOf(moduleIdObj);
try {
Integer countNumInteger = new Integer(String.valueOf(countNumObj));
returnMap.put(moduleId,countNumInteger);
}catch (Exception e){
returnMap.put(moduleId, countNumInteger);
} catch (Exception e) {
LogUtil.error("method parseModuleCountList has error:", e);
}
}
}
@ -303,7 +306,7 @@ public class ApiModuleService extends NodeTreeService<ApiModuleDTO> {
if (StringUtils.isNotBlank(node.getId())) {
criteria.andIdNotEqualTo(node.getId());
}
if(StringUtils.isNotEmpty(node.getProtocol())){
if (StringUtils.isNotEmpty(node.getProtocol())) {
criteria.andProtocolEqualTo(node.getProtocol());
}
return apiModuleMapper.selectByExample(example);
@ -520,10 +523,10 @@ public class ApiModuleService extends NodeTreeService<ApiModuleDTO> {
public long countById(String nodeId) {
ApiModuleExample example = new ApiModuleExample();
example.createCriteria().andIdEqualTo(nodeId);
return apiModuleMapper.countByExample(example);
return apiModuleMapper.countByExample(example);
}
public ApiModule getDefaultNode(String projectId,String protocol) {
public ApiModule getDefaultNode(String projectId, String protocol) {
ApiModuleExample example = new ApiModuleExample();
example.createCriteria().andProjectIdEqualTo(projectId).andProtocolEqualTo(protocol).andNameEqualTo("未规划接口").andParentIdIsNull();
List<ApiModule> list = apiModuleMapper.selectByExample(example);
@ -540,12 +543,12 @@ public class ApiModuleService extends NodeTreeService<ApiModuleDTO> {
record.setCreateUser(SessionUtils.getUserId());
apiModuleMapper.insert(record);
return record;
}else {
} else {
return list.get(0);
}
}
public ApiModule getDefaultNodeUnCreateNew(String projectId,String protocol) {
public ApiModule getDefaultNodeUnCreateNew(String projectId, String protocol) {
ApiModuleExample example = new ApiModuleExample();
example.createCriteria().andProjectIdEqualTo(projectId).andProtocolEqualTo(protocol).andNameEqualTo("未规划接口").andParentIdIsNull();
List<ApiModule> list = apiModuleMapper.selectByExample(example);
@ -565,4 +568,35 @@ public class ApiModuleService extends NodeTreeService<ApiModuleDTO> {
public String getModuleNameById(String moduleId) {
return extApiModuleMapper.getNameById(moduleId);
}
/**
* 返回数据库中存在的id
*
* @param protocalModuleIdMap <protocol , List<moduleId>>
* @return
*/
public Map<String, List<String>> checkModuleIds(Map<String, List<String>> protocalModuleIdMap) {
Map<String, List<String>> returnMap = new HashMap<>();
if (MapUtils.isNotEmpty(protocalModuleIdMap)) {
ApiModuleExample example = new ApiModuleExample();
for (Map.Entry<String, List<String>> entry : protocalModuleIdMap.entrySet()) {
String protocol = entry.getKey();
List<String> moduleIds = entry.getValue();
if (CollectionUtils.isNotEmpty(moduleIds)) {
example.clear();
example.createCriteria().andIdIn(moduleIds).andProtocolEqualTo(protocol);
List<ApiModule> moduleList = apiModuleMapper.selectByExample(example);
if (CollectionUtils.isNotEmpty(moduleList)) {
List<String> idLIst = new ArrayList<>();
moduleList.forEach(module -> {
idLIst.add(module.getId());
});
returnMap.put(protocol, idLIst);
}
}
}
}
return returnMap;
}
}