fix(接口测试): 解决全新安装默认项目初始化模块重复问题

--bug=1021358 --user=赵勇 【接口测试】在安装过ms的环境重新安装ms,会出现2个未规划模块 https://www.tapd.cn/55049933/s/1320747
This commit is contained in:
fit2-zhao 2022-12-29 15:10:49 +08:00 committed by fit2-zhao
parent 5639088349
commit 3d80530beb
3 changed files with 71 additions and 53 deletions

View File

@ -3,15 +3,13 @@ package io.metersphere.listener;
import com.mchange.lang.IntegerUtils;
import io.metersphere.api.exec.queue.ExecThreadPoolExecutor;
import io.metersphere.api.jmeter.JMeterService;
import io.metersphere.service.ApiExecutionQueueService;
import io.metersphere.service.MockConfigService;
import io.metersphere.service.PluginService;
import io.metersphere.commons.constants.ScheduleGroup;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.dto.BaseSystemConfigDTO;
import io.metersphere.service.BaseScheduleService;
import io.metersphere.service.SystemParameterService;
import io.metersphere.service.*;
import io.metersphere.service.definition.ApiModuleService;
import io.metersphere.service.scenario.ApiScenarioModuleService;
import org.apache.commons.lang3.StringUtils;
import org.python.core.Options;
import org.python.util.PythonInterpreter;
@ -36,6 +34,10 @@ public class ApiAppStartListener implements ApplicationRunner {
private PluginService pluginService;
@Resource
private ApiExecutionQueueService apiExecutionQueueService;
@Resource
private ApiModuleService apiModuleService;
@Resource
private ApiScenarioModuleService apiScenarioModuleService;
@Value("${jmeter.home}")
private String jmeterHome;
@ -59,6 +61,12 @@ public class ApiAppStartListener implements ApplicationRunner {
LogUtil.info("处理重启导致未执行完成的报告");
apiExecutionQueueService.exceptionHandling();
LogUtil.info("初始化默认项目接口模块");
apiModuleService.initDefaultNode();
LogUtil.info("初始化默认项目场景模块");
apiScenarioModuleService.initDefaultModule();
BaseSystemConfigDTO dto = systemParameterService.getBaseInfo();
LogUtil.info("设置并发队列核心数", dto.getConcurrency());
if (StringUtils.isNotEmpty(dto.getConcurrency())) {

View File

@ -6,11 +6,13 @@ import io.metersphere.api.parse.api.ApiDefinitionImport;
import io.metersphere.base.domain.*;
import io.metersphere.base.mapper.ApiDefinitionMapper;
import io.metersphere.base.mapper.ApiModuleMapper;
import io.metersphere.base.mapper.ProjectMapper;
import io.metersphere.base.mapper.ext.ExtApiDefinitionMapper;
import io.metersphere.base.mapper.ext.ExtApiModuleMapper;
import io.metersphere.base.mapper.ext.ExtApiTestCaseMapper;
import io.metersphere.commons.constants.ProjectModuleDefaultNodeEnum;
import io.metersphere.commons.constants.PropertyConstant;
import io.metersphere.commons.constants.RequestTypeConstants;
import io.metersphere.commons.constants.TestCaseConstants;
import io.metersphere.commons.enums.ApiTestDataStatus;
import io.metersphere.commons.exception.MSException;
@ -31,7 +33,6 @@ import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.jetbrains.annotations.NotNull;
import org.mybatis.spring.SqlSessionUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -54,6 +55,8 @@ public class ApiModuleService extends NodeTreeService<ApiModuleDTO> {
private ApiDefinitionService apiDefinitionService;
@Resource
private ExtApiTestCaseMapper extApiTestCaseMapper;
@Resource
private ProjectMapper projectMapper;
@Resource
SqlSessionFactory sqlSessionFactory;
@ -141,8 +144,6 @@ public class ApiModuleService extends NodeTreeService<ApiModuleDTO> {
}
public List<ApiModuleDTO> getNodeTreeByProjectId(String projectId, String protocol, String versionId) {
// 判断当前项目下是否有默认模块没有添加默认模块
this.getDefaultNode(projectId, protocol);
ApiDefinitionRequest request = new ApiDefinitionRequest();
List<ApiModuleDTO> apiModules = getApiModulesByProjectAndPro(projectId, protocol);
request.setProjectId(projectId);
@ -189,8 +190,6 @@ public class ApiModuleService extends NodeTreeService<ApiModuleDTO> {
}
public List<ApiModuleDTO> getNodeTreeByCondition(String projectId, String protocol, String versionId, ApiDefinitionRequest request) {
// 判断当前项目下是否有默认模块没有添加默认模块
this.getDefaultNode(projectId, protocol);
List<ApiModuleDTO> apiModules = getApiModulesByProjectAndPro(projectId, protocol);
request.setProjectId(projectId);
request.setProtocol(protocol);
@ -570,19 +569,29 @@ public class ApiModuleService extends NodeTreeService<ApiModuleDTO> {
return apiModuleMapper.countByExample(example);
}
public void initDefaultNode() {
ProjectExample projectExample = new ProjectExample();
projectExample.createCriteria().andNameEqualTo("默认项目");
List<Project> projects = projectMapper.selectByExample(projectExample);
if (CollectionUtils.isNotEmpty(projects)) {
String[] protocols = {RequestTypeConstants.HTTP, RequestTypeConstants.DUBBO, RequestTypeConstants.SQL, RequestTypeConstants.TCP};
for (String protocol : protocols) {
saveDefault(projects.get(0).getId(), protocol);
}
}
}
public ApiModule getDefaultNode(String projectId, String protocol) {
ApiModuleExample example = new ApiModuleExample();
example.createCriteria().andProjectIdEqualTo(projectId).andProtocolEqualTo(protocol).andNameEqualTo(ProjectModuleDefaultNodeEnum.API_MODULE_DEFAULT_NODE.getNodeName()).andParentIdIsNull();
List<ApiModule> list = apiModuleMapper.selectByExample(example);
if (CollectionUtils.isEmpty(list)) {
return saveDefault(projectId, protocol);
} else {
if (CollectionUtils.isNotEmpty(list)) {
return list.get(0);
}
return null;
}
@Async
public synchronized ApiModule saveDefault(String projectId, String protocol) {
public ApiModule saveDefault(String projectId, String protocol) {
ApiModuleExample example = new ApiModuleExample();
example.createCriteria().andProjectIdEqualTo(projectId).andProtocolEqualTo(protocol).andNameEqualTo(ProjectModuleDefaultNodeEnum.API_MODULE_DEFAULT_NODE.getNodeName()).andParentIdIsNull();
List<ApiModule> list = apiModuleMapper.selectByExample(example);

View File

@ -3,12 +3,10 @@ package io.metersphere.service.scenario;
import io.metersphere.api.dto.ApiTestImportRequest;
import io.metersphere.api.dto.automation.*;
import io.metersphere.base.domain.ApiScenario;
import io.metersphere.base.domain.ApiScenarioModule;
import io.metersphere.base.domain.ApiScenarioModuleExample;
import io.metersphere.base.domain.ApiScenarioWithBLOBs;
import io.metersphere.base.domain.*;
import io.metersphere.base.mapper.ApiScenarioMapper;
import io.metersphere.base.mapper.ApiScenarioModuleMapper;
import io.metersphere.base.mapper.ProjectMapper;
import io.metersphere.base.mapper.ext.ExtApiScenarioMapper;
import io.metersphere.base.mapper.ext.ExtApiScenarioModuleMapper;
import io.metersphere.commons.constants.ProjectModuleDefaultNodeEnum;
@ -30,10 +28,9 @@ import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.apache.commons.collections.CollectionUtils;
import javax.annotation.Resource;
import java.util.*;
@ -55,14 +52,14 @@ public class ApiScenarioModuleService extends NodeTreeService<ApiScenarioModuleD
SqlSessionFactory sqlSessionFactory;
@Resource
private ExtApiScenarioMapper extApiScenarioMapper;
@Resource
private ProjectMapper projectMapper;
public ApiScenarioModuleService() {
super(ApiScenarioModuleDTO.class);
}
public List<ApiScenarioModuleDTO> getNodeTreeByProjectId(String projectId) {
// 判断当前项目下是否有默认模块没有添加默认模块
this.getDefaultNode(projectId);
List<ApiScenarioModuleDTO> nodes = extApiScenarioModuleMapper.getNodeTreeByProjectId(projectId);
ApiScenarioRequest request = new ApiScenarioRequest();
request.setProjectId(projectId);
@ -103,8 +100,6 @@ public class ApiScenarioModuleService extends NodeTreeService<ApiScenarioModuleD
}
public List<ApiScenarioModuleDTO> getNodeTreeByProjectId(String projectId, ApiScenarioRequest request) {
// 判断当前项目下是否有默认模块没有添加默认模块
this.getDefaultNode(projectId);
List<ApiScenarioModuleDTO> nodes = extApiScenarioModuleMapper.getNodeTreeByProjectId(projectId);
request.setProjectId(projectId);
List<String> list = new ArrayList<>();
@ -174,7 +169,7 @@ public class ApiScenarioModuleService extends NodeTreeService<ApiScenarioModuleD
}
private void initApiCount(List<ApiScenarioModuleDTO> moduleDTOList, Map<String, List<ApiScenario>> scenarioMap) {
if (org.apache.commons.collections.CollectionUtils.isNotEmpty(moduleDTOList) && MapUtils.isNotEmpty(scenarioMap)) {
if (CollectionUtils.isNotEmpty(moduleDTOList) && MapUtils.isNotEmpty(scenarioMap)) {
moduleDTOList.forEach(node -> {
List<String> moduleIds = new ArrayList<>();
moduleIds = this.nodeList(moduleDTOList, node.getId(), moduleIds);
@ -191,7 +186,7 @@ public class ApiScenarioModuleService extends NodeTreeService<ApiScenarioModuleD
}
private List<ApiScenarioModuleDTO> selectTreeStructModuleById(Collection<String> ids) {
if (org.apache.commons.collections.CollectionUtils.isEmpty(ids)) {
if (CollectionUtils.isEmpty(ids)) {
return new ArrayList<>(0);
} else {
List<String> parentIdList = new ArrayList<>();
@ -246,7 +241,7 @@ public class ApiScenarioModuleService extends NodeTreeService<ApiScenarioModuleD
public double getNextLevelPos(String projectId, int level, String parentId) {
List<ApiScenarioModule> list = getPos(projectId, level, parentId, "pos desc");
if (!CollectionUtils.isEmpty(list) && list.get(0) != null && list.get(0).getPos() != null) {
if (CollectionUtils.isNotEmpty(list) && list.get(0) != null && list.get(0).getPos() != null) {
return list.get(0).getPos() + DEFAULT_POS;
} else {
return DEFAULT_POS;
@ -463,7 +458,7 @@ public class ApiScenarioModuleService extends NodeTreeService<ApiScenarioModuleD
ApiScenarioModuleExample example = new ApiScenarioModuleExample();
example.createCriteria().andIdIn(ids);
List<ApiScenarioModule> nodes = apiScenarioModuleMapper.selectByExample(example);
if (org.apache.commons.collections.CollectionUtils.isNotEmpty(nodes)) {
if (CollectionUtils.isNotEmpty(nodes)) {
List<String> names = nodes.stream().map(ApiScenarioModule::getName).collect(Collectors.toList());
OperatingLogDetails details = new OperatingLogDetails(JSON.toJSONString(ids), nodes.get(0).getProjectId(), String.join(",", names), nodes.get(0).getCreateUser(), new LinkedList<>());
return JSON.toJSONString(details);
@ -489,7 +484,7 @@ public class ApiScenarioModuleService extends NodeTreeService<ApiScenarioModuleD
criteria.andIdNotEqualTo(node.getId());
}
List<ApiScenarioModule> list = apiScenarioModuleMapper.selectByExample(example);
if (org.apache.commons.collections.CollectionUtils.isNotEmpty(list)) {
if (CollectionUtils.isNotEmpty(list)) {
module = list.get(0);
}
}
@ -509,34 +504,40 @@ public class ApiScenarioModuleService extends NodeTreeService<ApiScenarioModuleD
public ApiScenarioModule getDefaultNode(String projectId) {
ApiScenarioModuleExample example = new ApiScenarioModuleExample();
example.createCriteria().andProjectIdEqualTo(projectId).andNameEqualTo(ProjectModuleDefaultNodeEnum.API_SCENARIO_DEFAULT_NODE.getNodeName()).andParentIdIsNull();
example.createCriteria()
.andProjectIdEqualTo(projectId)
.andNameEqualTo(ProjectModuleDefaultNodeEnum.API_SCENARIO_DEFAULT_NODE.getNodeName())
.andParentIdIsNull();
List<ApiScenarioModule> list = apiScenarioModuleMapper.selectByExample(example);
if (CollectionUtils.isEmpty(list)) {
return saveDefault(projectId);
} else {
if (!CollectionUtils.isEmpty(list)) {
return list.get(0);
}
return null;
}
@Async
public synchronized ApiScenarioModule saveDefault(String projectId) {
ApiScenarioModuleExample example = new ApiScenarioModuleExample();
example.createCriteria().andProjectIdEqualTo(projectId).andNameEqualTo(ProjectModuleDefaultNodeEnum.API_SCENARIO_DEFAULT_NODE.getNodeName()).andParentIdIsNull();
List<ApiScenarioModule> list = apiScenarioModuleMapper.selectByExample(example);
if (CollectionUtils.isEmpty(list)) {
ApiScenarioModule module = new ApiScenarioModule();
module.setId(UUID.randomUUID().toString());
module.setName(ProjectModuleDefaultNodeEnum.API_SCENARIO_DEFAULT_NODE.getNodeName());
module.setPos(1.0);
module.setLevel(1);
module.setCreateTime(System.currentTimeMillis());
module.setUpdateTime(System.currentTimeMillis());
module.setProjectId(projectId);
module.setCreateUser(SessionUtils.getUserId());
apiScenarioModuleMapper.insert(module);
return module;
} else {
return list.get(0);
public void initDefaultModule() {
ProjectExample projectExample = new ProjectExample();
projectExample.createCriteria().andNameEqualTo("默认项目");
List<Project> projects = projectMapper.selectByExample(projectExample);
if (!CollectionUtils.isEmpty(projects)) {
ApiScenarioModuleExample example = new ApiScenarioModuleExample();
example.createCriteria()
.andProjectIdEqualTo(projects.get(0).getId())
.andNameEqualTo(ProjectModuleDefaultNodeEnum.API_SCENARIO_DEFAULT_NODE.getNodeName())
.andParentIdIsNull();
List<ApiScenarioModule> list = apiScenarioModuleMapper.selectByExample(example);
if (CollectionUtils.isEmpty(list)) {
ApiScenarioModule module = new ApiScenarioModule();
module.setId(UUID.randomUUID().toString());
module.setName(ProjectModuleDefaultNodeEnum.API_SCENARIO_DEFAULT_NODE.getNodeName());
module.setPos(1.0);
module.setLevel(1);
module.setCreateTime(System.currentTimeMillis());
module.setUpdateTime(System.currentTimeMillis());
module.setProjectId(projects.get(0).getId());
module.setCreateUser(SessionUtils.getUserId());
apiScenarioModuleMapper.insert(module);
}
}
}