fix(测试用例,接口定义,接口自动化): 增加默认模块,默认创建用例都放到默认模块下

This commit is contained in:
fit2-zhao 2021-03-30 15:03:31 +08:00
parent c84d1e8535
commit cff32b828c
12 changed files with 116 additions and 71 deletions

View File

@ -21,10 +21,7 @@ import io.metersphere.api.dto.scenario.environment.EnvironmentConfig;
import io.metersphere.api.jmeter.JMeterService;
import io.metersphere.api.parse.ApiImportParser;
import io.metersphere.base.domain.*;
import io.metersphere.base.mapper.ApiScenarioMapper;
import io.metersphere.base.mapper.ApiScenarioReportMapper;
import io.metersphere.base.mapper.TestCaseReviewScenarioMapper;
import io.metersphere.base.mapper.TestPlanApiScenarioMapper;
import io.metersphere.base.mapper.*;
import io.metersphere.base.mapper.ext.*;
import io.metersphere.commons.constants.*;
import io.metersphere.commons.exception.MSException;
@ -61,6 +58,8 @@ import java.util.stream.Collectors;
@Service
@Transactional(rollbackFor = Exception.class)
public class ApiAutomationService {
@Resource
ApiScenarioModuleMapper apiScenarioModuleMapper;
@Resource
private ExtScheduleMapper extScheduleMapper;
@Resource
@ -242,9 +241,14 @@ public class ApiAutomationService {
} else {
scenario.setUserId(request.getUserId());
}
if (StringUtils.isEmpty(request.getApiScenarioModuleId()) || StringUtils.isEmpty(request.getModulePath())) {
scenario.setApiScenarioModuleId("default-module");
scenario.setModulePath("/默认模块");
if (StringUtils.isEmpty(request.getApiScenarioModuleId()) || StringUtils.isEmpty(request.getModulePath()) || "default-module".equals(request.getApiScenarioModuleId())) {
ApiScenarioModuleExample example = new ApiScenarioModuleExample();
example.createCriteria().andProjectIdEqualTo(request.getProjectId()).andNameEqualTo("默认模块");
List<ApiScenarioModule> modules = apiScenarioModuleMapper.selectByExample(example);
if (CollectionUtils.isNotEmpty(modules)) {
scenario.setApiScenarioModuleId(modules.get(0).getId());
scenario.setModulePath(modules.get(0).getName());
}
}
return scenario;
}

View File

@ -94,6 +94,8 @@ public class ApiDefinitionService {
private ApiTestEnvironmentService environmentService;
@Resource
private EsbApiParamService esbApiParamService;
@Resource
ApiModuleMapper apiModuleMapper;
private static Cache cache = Cache.newHardMemoryCache(0, 3600 * 24);
@ -262,9 +264,14 @@ public class ApiDefinitionService {
test.setEnvironmentId(request.getEnvironmentId());
test.setUserId(request.getUserId());
test.setTags(request.getTags());
if (StringUtils.isEmpty(request.getModulePath()) || StringUtils.isEmpty(request.getModuleId())) {
test.setModulePath("/默认模块");
test.setModuleId("default-module");
if (StringUtils.isEmpty(request.getModulePath()) || StringUtils.isEmpty(request.getModuleId()) || "default-module".equals(request.getModuleId())) {
ApiModuleExample example = new ApiModuleExample();
example.createCriteria().andProjectIdEqualTo(test.getProjectId()).andProtocolEqualTo(test.getProtocol()).andNameEqualTo("默认模块");
List<ApiModule> modules = apiModuleMapper.selectByExample(example);
if (CollectionUtils.isNotEmpty(modules)) {
test.setModuleId(modules.get(0).getId());
test.setModulePath(modules.get(0).getName());
}
}
apiDefinitionMapper.updateByPrimaryKeySelective(test);
return test;
@ -290,9 +297,14 @@ public class ApiDefinitionService {
test.setStatus(APITestStatus.Underway.name());
test.setModulePath(request.getModulePath());
test.setModuleId(request.getModuleId());
if (StringUtils.isEmpty(request.getModulePath()) || StringUtils.isEmpty(request.getModuleId())) {
test.setModulePath("/默认模块");
test.setModuleId("default-module");
if (StringUtils.isEmpty(request.getModulePath()) || StringUtils.isEmpty(request.getModuleId()) || "default-module".equals(request.getModuleId())) {
ApiModuleExample example = new ApiModuleExample();
example.createCriteria().andProjectIdEqualTo(test.getProjectId()).andProtocolEqualTo(test.getProtocol()).andNameEqualTo("默认模块");
List<ApiModule> modules = apiModuleMapper.selectByExample(example);
if (CollectionUtils.isNotEmpty(modules)) {
test.setModuleId(modules.get(0).getId());
test.setModulePath("/默认模块");
}
}
test.setResponse(JSONObject.toJSONString(request.getResponse()));
test.setEnvironmentId(request.getEnvironmentId());
@ -604,9 +616,14 @@ public class ApiDefinitionService {
}
for (int i = 0; i < data.size(); i++) {
ApiDefinitionWithBLOBs item = data.get(i);
if (StringUtils.isEmpty(item.getModuleId()) || StringUtils.isEmpty(item.getModulePath())) {
item.setModuleId("default-module");
item.setModulePath("/默认模块");
if (StringUtils.isEmpty(item.getModuleId()) || StringUtils.isEmpty(item.getModulePath()) || "default-module".equals(item.getModuleId())) {
ApiModuleExample example = new ApiModuleExample();
example.createCriteria().andProjectIdEqualTo(item.getProjectId()).andProtocolEqualTo(item.getProtocol()).andNameEqualTo("默认模块");
List<ApiModule> modules = apiModuleMapper.selectByExample(example);
if (CollectionUtils.isNotEmpty(modules)) {
item.setModuleId(modules.get(0).getId());
item.setModulePath(modules.get(0).getName());
}
}
if (item.getName().length() > 255) {
item.setName(item.getName().substring(0, 255));

View File

@ -39,8 +39,6 @@ public class ApiModuleService extends NodeTreeService<ApiModuleDTO> {
@Resource
ExtApiModuleMapper extApiModuleMapper;
@Resource
private ApiDefinitionMapper apiDefinitionMapper;
@Resource
private ExtApiDefinitionMapper extApiDefinitionMapper;
@Resource
private TestPlanProjectService testPlanProjectService;
@ -61,6 +59,22 @@ public class ApiModuleService extends NodeTreeService<ApiModuleDTO> {
}
public List<ApiModuleDTO> getNodeTreeByProjectId(String projectId, String protocol) {
// 判断当前项目下是否有默认模块没有添加默认模块
ApiModuleExample example = new ApiModuleExample();
example.createCriteria().andProjectIdEqualTo(projectId).andProtocolEqualTo(protocol).andNameEqualTo("默认模块");
long count = apiModuleMapper.countByExample(example);
if (count <= 0) {
ApiModule record = new ApiModule();
record.setId(UUID.randomUUID().toString());
record.setName("默认模块");
record.setProtocol(protocol);
record.setPos(1.0);
record.setLevel(1);
record.setCreateTime(System.currentTimeMillis());
record.setUpdateTime(System.currentTimeMillis());
record.setProjectId(projectId);
apiModuleMapper.insert(record);
}
List<ApiModuleDTO> apiModules = extApiModuleMapper.getNodeTreeByProjectId(projectId, protocol);
return getNodeTrees(apiModules);
}

View File

@ -53,6 +53,22 @@ public class ApiScenarioModuleService extends NodeTreeService<ApiScenarioModuleD
}
public List<ApiScenarioModuleDTO> getNodeTreeByProjectId(String projectId) {
// 判断当前项目下是否有默认模块没有添加默认模块
ApiScenarioModuleExample example = new ApiScenarioModuleExample();
example.createCriteria().andProjectIdEqualTo(projectId).andNameEqualTo("默认模块");
long count = apiScenarioModuleMapper.countByExample(example);
if (count <= 0) {
ApiScenarioModule record = new ApiScenarioModule();
record.setId(UUID.randomUUID().toString());
record.setName("默认模块");
record.setPos(1.0);
record.setLevel(1);
record.setCreateTime(System.currentTimeMillis());
record.setUpdateTime(System.currentTimeMillis());
record.setProjectId(projectId);
apiScenarioModuleMapper.insert(record);
}
List<ApiScenarioModuleDTO> nodes = extApiScenarioModuleMapper.getNodeTreeByProjectId(projectId);
return getNodeTrees(nodes);
}

View File

@ -107,6 +107,21 @@ public class TestCaseNodeService extends NodeTreeService<TestCaseNodeDTO> {
}
public List<TestCaseNodeDTO> getNodeTreeByProjectId(String projectId) {
// 判断当前项目下是否有默认模块没有添加默认模块
TestCaseNodeExample example = new TestCaseNodeExample();
example.createCriteria().andProjectIdEqualTo(projectId).andNameEqualTo("默认模块");
long count = testCaseNodeMapper.countByExample(example);
if (count <= 0) {
TestCaseNode record = new TestCaseNode();
record.setId(UUID.randomUUID().toString());
record.setName("默认模块");
record.setPos(1.0);
record.setLevel(1);
record.setCreateTime(System.currentTimeMillis());
record.setUpdateTime(System.currentTimeMillis());
record.setProjectId(projectId);
testCaseNodeMapper.insert(record);
}
List<TestCaseNodeDTO> testCaseNodes = extTestCaseNodeMapper.getNodeTreeByProjectId(projectId);
return getNodeTrees(testCaseNodes);
}

View File

@ -52,6 +52,8 @@ import java.util.stream.Collectors;
@Service
@Transactional(rollbackFor = Exception.class)
public class TestCaseService {
@Resource
TestCaseNodeMapper testCaseNodeMapper;
@Resource
TestCaseMapper testCaseMapper;
@ -90,6 +92,18 @@ public class TestCaseService {
@Resource
TestCaseTestMapper testCaseTestMapper;
private void setNode(TestCaseWithBLOBs testCase){
if (StringUtils.isEmpty(testCase.getNodeId()) || StringUtils.isEmpty(testCase.getNodePath()) || "default-module".equals(testCase.getNodeId())) {
TestCaseNodeExample example = new TestCaseNodeExample();
example.createCriteria().andProjectIdEqualTo(testCase.getProjectId()).andNameEqualTo("默认模块");
List<TestCaseNode> nodes = testCaseNodeMapper.selectByExample(example);
if (CollectionUtils.isNotEmpty(nodes)) {
testCase.setNodeId(nodes.get(0).getId());
testCase.setNodePath("/" + nodes.get(0).getName());
}
}
}
public TestCaseWithBLOBs addTestCase(TestCaseWithBLOBs testCase) {
testCase.setName(testCase.getName());
checkTestCaseExist(testCase);
@ -100,7 +114,7 @@ public class TestCaseService {
testCase.setReviewStatus(TestCaseReviewStatus.Prepare.name());
testCase.setDemandId(testCase.getDemandId());
testCase.setDemandName(testCase.getDemandName());
this.setNode(testCase);
testCaseMapper.insert(testCase);
return testCase;
}
@ -179,7 +193,7 @@ public class TestCaseService {
String steps = tc.getSteps();
String remark = tc.getRemark();
if (StringUtils.equals(steps, testCase.getSteps()) && StringUtils.equals(remark, caseRemark)) {
//MSException.throwException(Translator.get("test_case_already_exists"));
//MSException.throwException(Translator.get("test_case_already_exists"));
isExt = true;
}
}
@ -195,16 +209,16 @@ public class TestCaseService {
* 根据id和pojectId查询id是否在数据库中存在
* 在数据库中单id的话是可重复的,id与projectId的组合是唯一的
*/
public Integer checkIdExist(Integer id, String projectId){
public Integer checkIdExist(Integer id, String projectId) {
TestCaseExample example = new TestCaseExample();
TestCaseExample.Criteria criteria = example.createCriteria();
if (null != id) {
criteria.andNumEqualTo(id);
criteria.andProjectIdEqualTo(projectId);
long count = testCaseMapper.countByExample(example); //查询是否有包含此ID的数据
if(count == 0){ //如果ID不存在
if (count == 0) { //如果ID不存在
return null;
}else { //有对应ID的数据
} else { //有对应ID的数据
return id;
}
}
@ -415,7 +429,7 @@ public class TestCaseService {
testcase.setSort(sort.getAndIncrement());
testcase.setNum(num.decrementAndGet());
testcase.setReviewStatus(TestCaseReviewStatus.Prepare.name());
mapper.insert(testcase);
mapper.insert(testcase);
});
}
sqlSession.flushStatements();
@ -443,6 +457,7 @@ public class TestCaseService {
/**
* 把Excel中带ID的数据更新到数据库
*
* @param testCases
* @param projectId
*/
@ -820,14 +835,14 @@ public class TestCaseService {
if (files != null) {
files.forEach(file -> {
final FileMetadata fileMetadata = fileService.saveFile(file,testCaseWithBLOBs.getProjectId());
final FileMetadata fileMetadata = fileService.saveFile(file, testCaseWithBLOBs.getProjectId());
TestCaseFile testCaseFile = new TestCaseFile();
testCaseFile.setFileId(fileMetadata.getId());
testCaseFile.setCaseId(request.getId());
testCaseFileMapper.insert(testCaseFile);
});
}
this.setNode(request);
editTestCase(request);
return request.getId();
}

View File

@ -139,13 +139,6 @@
this.result = this.$get("/api/automation/module/list/" + this.projectId, response => {
if (response.data != undefined && response.data != null) {
this.data = response.data;
this.data.unshift({
"id": "default-module",
"name": this.$t('commons.module_title'),
"level": 0,
"path": "/" + this.$t('commons.module_title'),
"children": [],
});
this.data.forEach(node => {
buildTree(node, {path: ''});
});
@ -166,13 +159,6 @@
this.result = this.$get("/api/automation/module/list/" + this.projectId, response => {
if (response.data != undefined && response.data != null) {
this.data = response.data;
this.data.unshift({
"id": "default-module",
"name": this.$t('commons.module_title'),
"level": 0,
"path": "/" + this.$t('commons.module_title'),
"children": [],
});
this.data.forEach(node => {
buildTree(node, {path: ''});
});
@ -196,13 +182,6 @@
this.result = this.$get(url, response => {
if (response.data != undefined && response.data != null) {
this.data = response.data;
this.data.unshift({
"id": "default-module",
"name": this.$t('commons.module_title'),
"level": 0,
"path": "/" + this.$t('commons.module_title'),
"children": [],
});
this.data.forEach(node => {
buildTree(node, {path: ''});
});

View File

@ -252,13 +252,6 @@
this.result = this.$get(url, response => {
if (response.data != undefined && response.data != null) {
this.moduleOptions = response.data;
this.moduleOptions.unshift({
"id": "default-module",
"name": this.$t('commons.module_title'),
"level": 0,
"path": "/" + this.$t('commons.module_title'),
"children": [],
});
this.moduleOptions.forEach(node => {
buildTree(node, {path: ''});
});

View File

@ -130,13 +130,6 @@
this.result = this.$get(url, response => {
if (response.data != undefined && response.data != null) {
this.data = response.data;
this.data.unshift({
"id": "default-module",
"name": this.$t('commons.module_title'),
"level": 0,
"path": "/" + this.$t('commons.module_title'),
"children": [],
});
this.data.forEach(node => {
buildTree(node, {path: ''});
});

View File

@ -315,7 +315,7 @@ export default {
form: {
name: '',
module: 'default-module',
nodePath:'',
nodePath:'/默认模块',
maintainer: getCurrentUser().id,
priority: 'P0',
type: '',
@ -416,6 +416,10 @@ export default {
};
});
}, 1000);
if(this.selectNode && this.selectNode.data && !this.form.id){
this.form.module = this.selectNode.data.id;
this.form.nodePath = this.selectNode.data.path;
}
},
watch: {
treeNodes() {
@ -428,9 +432,11 @@ export default {
created() {
this.loadOptions();
this.addListener(); // ctrl s
if(this.selectNode && this.selectNode.data && this.form.id){
if(this.selectNode && this.selectNode.data && !this.form.id){
this.form.module = this.selectNode.data.id;
this.form.nodePath = this.selectNode.data.path;
}else{
this.form.module =this.treeNodes && this.length>0? this.treeNodes[0].id:"";
}
if (this.type === 'edit' || this.type === 'copy') {
this.form.module = this.currentTestCaseInfo.nodeId;

View File

@ -116,13 +116,6 @@ export default {
if (this.projectId) {
this.result = this.$get("/case/node/list/" + this.projectId, response => {
this.treeNodes = response.data;
this.treeNodes.unshift({
"id": "default-module",
"name": this.$t('commons.module_title'),
"level": 0,
"path": "/" + this.$t('commons.module_title'),
"children": [],
});
this.treeNodes.forEach(node => {
buildTree(node, {path: ''});
});

@ -1 +1 @@
Subproject commit 2115bd28a90854d2b6276a90878934715498c584
Subproject commit 07951ba17aef6f29e50cfd68e40de3266f9a60cd