diff --git a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java index 2288683269..d39222cdaf 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java @@ -48,6 +48,7 @@ import io.metersphere.track.dto.TestPlanDTO; import io.metersphere.track.request.testcase.ApiCaseRelevanceRequest; import io.metersphere.track.request.testcase.QueryTestPlanRequest; import io.metersphere.track.request.testplan.FileOperationRequest; +import io.metersphere.track.service.TestCaseService; import io.metersphere.track.service.TestPlanScenarioCaseService; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections4.MapUtils; @@ -111,6 +112,8 @@ public class ApiAutomationService { @Lazy private TestPlanScenarioCaseService testPlanScenarioCaseService; @Resource + private TestCaseService testCaseService; + @Resource private EsbApiParamService esbApiParamService; @Resource private ApiTestEnvironmentMapper apiTestEnvironmentMapper; @@ -588,6 +591,7 @@ public class ApiAutomationService { Map scenarioIdDefinitionMap = apiScenarioWithBLOBs.stream().collect(Collectors.toMap(ApiScenarioWithBLOBs::getId, scenario -> scenario.getScenarioDefinition() == null ? " " : scenario.getScenarioDefinition())); preDelAndResource(scenarioIdDefinitionMap); testPlanScenarioCaseService.bathDeleteByScenarioIds(scenarioIds); + testCaseService.deleteTestCaseTestByTestIds(ids); deleteScenarioByIds(scenarioIds); } @@ -1941,7 +1945,9 @@ public class ApiAutomationService { public List getScenarioCaseByIds(List ids) { if (CollectionUtils.isNotEmpty(ids)) { ApiScenarioExample example = new ApiScenarioExample(); - example.createCriteria().andIdIn(ids); + example.createCriteria() + .andIdIn(ids) + .andStatusNotEqualTo(CommonConstants.TrashStatus); return apiScenarioMapper.selectByExample(example); } return new ArrayList<>(); diff --git a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java index 2398b6271a..b87a104bbe 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java @@ -54,6 +54,7 @@ import io.metersphere.notice.service.NoticeSendService; import io.metersphere.service.*; import io.metersphere.track.request.testcase.ApiCaseRelevanceRequest; import io.metersphere.track.request.testcase.QueryTestPlanRequest; +import io.metersphere.track.service.TestCaseService; import io.metersphere.track.service.TestPlanService; import org.apache.commons.beanutils.BeanComparator; import org.apache.commons.collections.CollectionUtils; @@ -132,6 +133,8 @@ public class ApiDefinitionService { @Resource private RelationshipEdgeService relationshipEdgeService; @Resource + private TestCaseService testCaseService; + @Resource private ApiDefinitionFollowMapper apiDefinitionFollowMapper; @Resource @Lazy @@ -395,6 +398,8 @@ public class ApiDefinitionService { FileUtils.deleteBodyFiles(api.getId()); deleteFollows(api.getId()); }); + // 删除用例和接口的关联关系 + testCaseService.deleteTestCaseTestByTestIds(Arrays.asList(apiId)); } private void deleteFollows(String apiId) { @@ -416,6 +421,7 @@ public class ApiDefinitionService { mockConfigService.deleteMockConfigByApiId(apiId); deleteFollows(apiId); } + testCaseService.deleteTestCaseTestByTestIds(apiIds); } public void removeToGc(List apiIds) { diff --git a/backend/src/main/java/io/metersphere/api/service/ApiTestCaseService.java b/backend/src/main/java/io/metersphere/api/service/ApiTestCaseService.java index 6a47e842e0..33703d5803 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiTestCaseService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiTestCaseService.java @@ -19,6 +19,7 @@ import io.metersphere.base.domain.*; import io.metersphere.base.mapper.*; import io.metersphere.base.mapper.ext.*; import io.metersphere.commons.constants.APITestStatus; +import io.metersphere.commons.constants.CommonConstants; import io.metersphere.commons.constants.MsTestElementConstants; import io.metersphere.commons.constants.TestPlanStatus; import io.metersphere.commons.exception.MSException; @@ -35,6 +36,7 @@ import io.metersphere.service.ApiCaseExecutionInfoService; import io.metersphere.service.FileService; import io.metersphere.service.UserService; import io.metersphere.track.request.testcase.ApiCaseRelevanceRequest; +import io.metersphere.track.service.TestPlanApiCaseService; import io.metersphere.track.service.TestPlanService; import org.apache.commons.beanutils.BeanComparator; import org.apache.commons.collections.CollectionUtils; @@ -86,6 +88,8 @@ public class ApiTestCaseService { @Resource private EsbApiParamService esbApiParamService; @Resource + private TestPlanApiCaseService testPlanApiCaseService; + @Resource private ApiScenarioReferenceIdService apiScenarioReferenceIdService; @Resource private ExtApiScenarioMapper extApiScenarioMapper; @@ -294,6 +298,7 @@ public class ApiTestCaseService { apiCaseExecutionInfoService.deleteByApiCaseId(testId); apiTestCaseMapper.deleteByPrimaryKey(testId); esbApiParamService.deleteByResourceId(testId); + testPlanApiCaseService.deleteByCaseId(testId); deleteBodyFiles(testId); deleteFollows(testId); } @@ -536,9 +541,7 @@ public class ApiTestCaseService { apiTestCaseMapper.deleteByExample(example); List apiTestCases = apiTestCaseMapper.selectByExample(example); List caseIds = apiTestCases.stream().map(ApiTestCase::getId).collect(Collectors.toList()); - for (String testId : caseIds) { - extTestPlanTestCaseMapper.deleteByTestCaseID(testId); - } + testPlanApiCaseService.deleteByCaseIds(caseIds); } public void relevanceByApi(ApiCaseRelevanceRequest request) { @@ -1085,7 +1088,9 @@ public class ApiTestCaseService { public List getApiCaseByIds(List apiCaseIds) { if (CollectionUtils.isNotEmpty(apiCaseIds)) { ApiTestCaseExample example = new ApiTestCaseExample(); - example.createCriteria().andIdIn(apiCaseIds); + example.createCriteria() + .andIdIn(apiCaseIds) + .andStatusNotEqualTo(CommonConstants.TrashStatus); return apiTestCaseMapper.selectByExample(example); } return new ArrayList<>(); diff --git a/backend/src/main/java/io/metersphere/commons/constants/CommonConstants.java b/backend/src/main/java/io/metersphere/commons/constants/CommonConstants.java new file mode 100644 index 0000000000..9899e51301 --- /dev/null +++ b/backend/src/main/java/io/metersphere/commons/constants/CommonConstants.java @@ -0,0 +1,5 @@ +package io.metersphere.commons.constants; + +public class CommonConstants { + public static final String TrashStatus = "Trash"; +} diff --git a/backend/src/main/java/io/metersphere/commons/constants/TestCaseTestType.java b/backend/src/main/java/io/metersphere/commons/constants/TestCaseTestType.java new file mode 100644 index 0000000000..0eea90cfb4 --- /dev/null +++ b/backend/src/main/java/io/metersphere/commons/constants/TestCaseTestType.java @@ -0,0 +1,5 @@ +package io.metersphere.commons.constants; + +public enum TestCaseTestType { + testcase, automation, performance +} diff --git a/backend/src/main/java/io/metersphere/performance/service/PerformanceTestService.java b/backend/src/main/java/io/metersphere/performance/service/PerformanceTestService.java index e5408d63bd..8a1a0a83c3 100644 --- a/backend/src/main/java/io/metersphere/performance/service/PerformanceTestService.java +++ b/backend/src/main/java/io/metersphere/performance/service/PerformanceTestService.java @@ -170,6 +170,8 @@ public class PerformanceTestService { testPlanLoadCaseService.deleteByTestId(test.getId()); + testCaseService.deleteTestCaseTestByTestId(test.getId()); + detachFileByTestId(test.getId()); deleteFollows(test.getId()); @@ -1033,7 +1035,9 @@ public class PerformanceTestService { public List getLoadCaseByIds(List ids) { if (org.apache.commons.collections.CollectionUtils.isNotEmpty(ids)) { LoadTestExample example = new LoadTestExample(); - example.createCriteria().andIdIn(ids); + example.createCriteria() + .andIdIn(ids) + .andStatusNotEqualTo(CommonConstants.TrashStatus); return loadTestMapper.selectByExample(example); } return new ArrayList<>(); diff --git a/backend/src/main/java/io/metersphere/track/service/TestCaseService.java b/backend/src/main/java/io/metersphere/track/service/TestCaseService.java index d878c117df..07e949c56b 100644 --- a/backend/src/main/java/io/metersphere/track/service/TestCaseService.java +++ b/backend/src/main/java/io/metersphere/track/service/TestCaseService.java @@ -610,6 +610,16 @@ public class TestCaseService { return testCaseMapper.deleteByPrimaryKey(testCaseId); } + public int deleteTestCaseTestByTestId(String testId) { + return deleteTestCaseTestByTestIds(Arrays.asList(testId)); + } + + public int deleteTestCaseTestByTestIds(List testIds) { + TestCaseTestExample examples = new TestCaseTestExample(); + examples.createCriteria().andTestIdIn(testIds); + return testCaseTestMapper.deleteByExample(examples); + } + public int deleteTestCaseBySameVersion(String testCaseId) { TestCase testCase = testCaseMapper.selectByPrimaryKey(testCaseId); if (testCase == null) { @@ -2415,13 +2425,13 @@ public class TestCaseService { Map testCaseTestsMap = testCaseTests.stream() .collect(Collectors.toMap(TestCaseTest::getTestId, i -> i)); List apiCases = apiTestCaseService.getApiCaseByIds( - getTestIds(testCaseTests, "testcase") + getTestIds(testCaseTests, TestCaseTestType.testcase.name()) ); List apiScenarios = apiAutomationService.getScenarioCaseByIds( - getTestIds(testCaseTests, "automation") + getTestIds(testCaseTests, TestCaseTestType.automation.name()) ); List apiLoadTests = performanceTestService.getLoadCaseByIds( - getTestIds(testCaseTests, "performance") + getTestIds(testCaseTests, TestCaseTestType.performance.name()) ); List projectIds = apiCases.stream().map(c -> c.getProjectId()).collect(Collectors.toList()); projectIds.addAll(apiScenarios.stream().map(s -> s.getProjectId()).collect(Collectors.toList())); @@ -2453,15 +2463,15 @@ public class TestCaseService { List testCaseTestList = new ArrayList<>(); apiCases.forEach(item -> { - getTestCaseTestDaoList("testcase", item.getNum(), item.getName(), item.getId(), projectNameMap.get(item.getProjectId()), verisonNameMap.get(item.getVersionId()), + getTestCaseTestDaoList(TestCaseTestType.testcase.name(), item.getNum(), item.getName(), item.getId(), projectNameMap.get(item.getProjectId()), verisonNameMap.get(item.getVersionId()), testCaseTestList, testCaseTestsMap); }); apiScenarios.forEach(item -> { - getTestCaseTestDaoList("automation", item.getNum(), item.getName(), item.getId(), projectNameMap.get(item.getProjectId()), verisonNameMap.get(item.getVersionId()), + getTestCaseTestDaoList(TestCaseTestType.automation.name(), item.getNum(), item.getName(), item.getId(), projectNameMap.get(item.getProjectId()), verisonNameMap.get(item.getVersionId()), testCaseTestList, testCaseTestsMap); }); apiLoadTests.forEach(item -> { - getTestCaseTestDaoList("performance", item.getNum(), item.getName(), item.getId(), projectNameMap.get(item.getProjectId()), verisonNameMap.get(item.getVersionId()), + getTestCaseTestDaoList(TestCaseTestType.performance.name(), item.getNum(), item.getName(), item.getId(), projectNameMap.get(item.getProjectId()), verisonNameMap.get(item.getVersionId()), testCaseTestList, testCaseTestsMap); }); return testCaseTestList; diff --git a/backend/src/main/java/io/metersphere/track/service/TestPlanApiCaseService.java b/backend/src/main/java/io/metersphere/track/service/TestPlanApiCaseService.java index 416286d3b0..52c45b837c 100644 --- a/backend/src/main/java/io/metersphere/track/service/TestPlanApiCaseService.java +++ b/backend/src/main/java/io/metersphere/track/service/TestPlanApiCaseService.java @@ -19,7 +19,6 @@ import io.metersphere.base.mapper.TestPlanMapper; import io.metersphere.base.mapper.ext.ExtTestPlanApiCaseMapper; import io.metersphere.commons.constants.ExecuteResult; import io.metersphere.commons.utils.*; -import io.metersphere.controller.request.OrderRequest; import io.metersphere.controller.request.ResetOrderRequest; import io.metersphere.dto.MsExecResponseDTO; import io.metersphere.dto.RunModeConfigDTO; @@ -30,7 +29,6 @@ import io.metersphere.track.dto.TestCaseReportStatusResultDTO; import io.metersphere.track.dto.TestPlanApiResultReportDTO; import io.metersphere.track.dto.TestPlanSimpleReportDTO; import io.metersphere.track.request.testcase.TestPlanApiCaseBatchRequest; -import org.apache.commons.collections4.MapUtils; import org.apache.commons.lang3.StringUtils; import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.SqlSession; @@ -133,7 +131,17 @@ public class TestPlanApiCaseService { TestPlanApiCaseExample example = new TestPlanApiCaseExample(); example.createCriteria() .andIdEqualTo(id); + return testPlanApiCaseMapper.deleteByExample(example); + } + public int deleteByCaseId(String caseId) { + return this.deleteByCaseIds(Arrays.asList(caseId)); + } + + public int deleteByCaseIds(List caseIds) { + TestPlanApiCaseExample example = new TestPlanApiCaseExample(); + example.createCriteria() + .andApiCaseIdIn(caseIds); return testPlanApiCaseMapper.deleteByExample(example); }