refactor(测试计划): 优化测试计划关联接口的列表展示顺序

This commit is contained in:
wxg0103 2024-06-21 15:43:51 +08:00 committed by Craftsman
parent b85a2e99c8
commit 85200437fd
4 changed files with 31 additions and 21 deletions

View File

@ -60,10 +60,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import java.util.ArrayList; import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -587,8 +584,9 @@ public class TestPlanApiCaseService extends TestPlanResourceService {
apiCaseList.forEach(apiCase -> { apiCaseList.forEach(apiCase -> {
List<String> apiCaseIds = apiCase.getIds(); List<String> apiCaseIds = apiCase.getIds();
if (CollectionUtils.isNotEmpty(apiCaseIds)) { if (CollectionUtils.isNotEmpty(apiCaseIds)) {
List<ApiTestCase> apiTestCases = apiTestCaseList.stream().filter(item -> apiCaseIds.contains(item.getId())).collect(Collectors.toList()); List<ApiTestCase> apiTestCases = apiTestCaseList.stream().filter(item -> apiCaseIds.contains(item.getId())).toList();
buildTestPlanApiCase(testPlan, apiTestCases, apiCase.getCollectionId(), user, testPlanApiCaseList, logDTOS); Map<String, ApiTestCase> apiTestCaseMap = apiTestCases.stream().collect(Collectors.toMap(ApiTestCase::getId, Function.identity()));
buildTestPlanApiCase(testPlan, apiCaseIds, apiTestCaseMap, apiCase.getCollectionId(), user, testPlanApiCaseList, logDTOS);
} }
}); });
} }
@ -601,9 +599,16 @@ public class TestPlanApiCaseService extends TestPlanResourceService {
List<ApiTestCase> apiTestCaseList = extTestPlanApiCaseMapper.selectApiCaseByDefinitionIds(ids, isRepeat, testPlan.getId()); List<ApiTestCase> apiTestCaseList = extTestPlanApiCaseMapper.selectApiCaseByDefinitionIds(ids, isRepeat, testPlan.getId());
apiCaseList.forEach(apiCase -> { apiCaseList.forEach(apiCase -> {
List<String> apiCaseIds = apiCase.getIds(); List<String> apiCaseIds = apiCase.getIds();
if (CollectionUtils.isNotEmpty(apiCaseIds)) { if (CollectionUtils.isNotEmpty(apiCaseIds) && CollectionUtils.isNotEmpty(apiTestCaseList)) {
List<ApiTestCase> apiTestCases = apiTestCaseList.stream().filter(item -> apiCaseIds.contains(item.getApiDefinitionId())).collect(Collectors.toList()); List<ApiTestCase> apiTestCases = apiTestCaseList.stream().filter(item -> apiCaseIds.contains(item.getApiDefinitionId())).toList();
buildTestPlanApiCase(testPlan, apiTestCases, apiCase.getCollectionId(), user, testPlanApiCaseList, logDTOS); //生成map集合 key是apiDefinitionId value是apiTestCase
Map<String, ApiTestCase> apiTestCaseMap = apiTestCases.stream().collect(Collectors.toMap(ApiTestCase::getId, Function.identity()));
// apiTestCases 生成map key是apiDefinitionId value是List<String> id
Map<String, List<String>> apiTestCaseMapIds = apiTestCases.stream().collect(Collectors.groupingBy(ApiTestCase::getApiDefinitionId, Collectors.mapping(ApiTestCase::getId, Collectors.toList())));
Collections.reverse(apiCaseIds);
apiCaseIds.forEach(apiCaseId -> {
buildTestPlanApiCase(testPlan, apiTestCaseMapIds.get(apiCaseId), apiTestCaseMap, apiCase.getCollectionId(), user, testPlanApiCaseList, logDTOS);
});
} }
}); });
} }
@ -618,10 +623,12 @@ public class TestPlanApiCaseService extends TestPlanResourceService {
* @param user * @param user
* @param testPlanApiCaseList * @param testPlanApiCaseList
*/ */
private void buildTestPlanApiCase(TestPlan testPlan, List<ApiTestCase> apiTestCases, String collectionId, SessionUser user, List<TestPlanApiCase> testPlanApiCaseList, List<LogDTO> logDTOS) { private void buildTestPlanApiCase(TestPlan testPlan, List<String> ids, Map<String, ApiTestCase> apiTestCases, String collectionId, SessionUser user, List<TestPlanApiCase> testPlanApiCaseList, List<LogDTO> logDTOS) {
super.checkCollection(testPlan.getId(), collectionId, CaseType.API_CASE.getKey()); super.checkCollection(testPlan.getId(), collectionId, CaseType.API_CASE.getKey());
AtomicLong nextOrder = new AtomicLong(getNextOrder(collectionId)); AtomicLong nextOrder = new AtomicLong(getNextOrder(collectionId));
apiTestCases.forEach(apiTestCase -> { Collections.reverse(ids);
ids.forEach(id -> {
ApiTestCase apiTestCase = apiTestCases.get(id);
TestPlanApiCase testPlanApiCase = new TestPlanApiCase(); TestPlanApiCase testPlanApiCase = new TestPlanApiCase();
testPlanApiCase.setId(IDGenerator.nextStr()); testPlanApiCase.setId(IDGenerator.nextStr());
testPlanApiCase.setTestPlanCollectionId(collectionId); testPlanApiCase.setTestPlanCollectionId(collectionId);

View File

@ -58,10 +58,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import java.util.ArrayList; import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -197,8 +194,10 @@ public class TestPlanApiScenarioService extends TestPlanResourceService {
apiScenarioList.forEach(apiScenario -> { apiScenarioList.forEach(apiScenario -> {
List<String> apiScenarioIds = apiScenario.getIds(); List<String> apiScenarioIds = apiScenario.getIds();
if (CollectionUtils.isNotEmpty(apiScenarioIds)) { if (CollectionUtils.isNotEmpty(apiScenarioIds)) {
List<ApiScenario> scenarios = apiScenarios.stream().filter(item -> apiScenarioIds.contains(item.getId())).collect(Collectors.toList()); List<ApiScenario> scenarios = apiScenarios.stream().filter(item -> apiScenarioIds.contains(item.getId())).toList();
buildTestPlanApiScenario(testPlan, scenarios, apiScenario.getCollectionId(), user, testPlanApiScenarioList, logDTOS); // 生成map key为id value为scenario
Map<String, ApiScenario> scenarioMap = scenarios.stream().collect(Collectors.toMap(ApiScenario::getId, Function.identity()));
buildTestPlanApiScenario(testPlan, apiScenarioIds, scenarioMap, apiScenario.getCollectionId(), user, testPlanApiScenarioList, logDTOS);
} }
}); });
} }
@ -213,10 +212,12 @@ public class TestPlanApiScenarioService extends TestPlanResourceService {
* @param user * @param user
* @param testPlanApiScenarioList * @param testPlanApiScenarioList
*/ */
private void buildTestPlanApiScenario(TestPlan testPlan, List<ApiScenario> scenarios, String collectionId, SessionUser user, List<TestPlanApiScenario> testPlanApiScenarioList, List<LogDTO> logDTOS) { private void buildTestPlanApiScenario(TestPlan testPlan, List<String> ids, Map<String, ApiScenario> scenarios, String collectionId, SessionUser user, List<TestPlanApiScenario> testPlanApiScenarioList, List<LogDTO> logDTOS) {
super.checkCollection(testPlan.getId(), collectionId, CaseType.SCENARIO_CASE.getKey()); super.checkCollection(testPlan.getId(), collectionId, CaseType.SCENARIO_CASE.getKey());
AtomicLong nextOrder = new AtomicLong(getNextOrder(collectionId)); AtomicLong nextOrder = new AtomicLong(getNextOrder(collectionId));
scenarios.forEach(scenario -> { Collections.reverse(ids);
ids.forEach(id -> {
ApiScenario scenario = scenarios.get(id);
TestPlanApiScenario testPlanApiScenario = new TestPlanApiScenario(); TestPlanApiScenario testPlanApiScenario = new TestPlanApiScenario();
testPlanApiScenario.setId(IDGenerator.nextStr()); testPlanApiScenario.setId(IDGenerator.nextStr());
testPlanApiScenario.setTestPlanId(testPlan.getId()); testPlanApiScenario.setTestPlanId(testPlan.getId());

View File

@ -787,6 +787,8 @@ public class TestPlanFunctionalCaseService extends TestPlanResourceService {
List<FunctionalCase> functionalCases = functionalCaseMapper.selectByExample(example); List<FunctionalCase> functionalCases = functionalCaseMapper.selectByExample(example);
AtomicLong nextOrder = new AtomicLong(getNextOrder(functional.getCollectionId())); AtomicLong nextOrder = new AtomicLong(getNextOrder(functional.getCollectionId()));
Map<String, FunctionalCase> collect = functionalCases.stream().collect(Collectors.toMap(FunctionalCase::getId, functionalCase -> functionalCase)); Map<String, FunctionalCase> collect = functionalCases.stream().collect(Collectors.toMap(FunctionalCase::getId, functionalCase -> functionalCase));
// 需要按列表顺序插入所以需要反转列表
Collections.reverse(functionalIds);
functionalIds.forEach(functionalId -> { functionalIds.forEach(functionalId -> {
FunctionalCase functionalCase = collect.get(functionalId); FunctionalCase functionalCase = collect.get(functionalId);
TestPlanFunctionalCase testPlanFunctionalCase = new TestPlanFunctionalCase(); TestPlanFunctionalCase testPlanFunctionalCase = new TestPlanFunctionalCase();

View File

@ -6,8 +6,8 @@ VALUES
INSERT INTO `test_plan_config`(`test_plan_id`, `automatic_status_update`, `repeat_case`, `pass_threshold`, INSERT INTO `test_plan_config`(`test_plan_id`, `automatic_status_update`, `repeat_case`, `pass_threshold`,
`case_run_mode`) `case_run_mode`)
VALUES ('wxxx_1', b'0', b'0', 100, 'PARALLEL'), VALUES ('wxxx_1', b'0', b'1', 100, 'PARALLEL'),
('wxxx_2', b'0', b'0', 100, 'PARALLEL'); ('wxxx_2', b'0', b'1', 100, 'PARALLEL');
INSERT INTO `api_definition`(`id`, `name`, `protocol`, `method`, `path`, `status`, `num`, `tags`, `pos`, `project_id`, `module_id`, `latest`, `version_id`, `ref_id`, `description`, `create_time`, `create_user`, `update_time`, `update_user`, `delete_user`, `delete_time`, `deleted`) INSERT INTO `api_definition`(`id`, `name`, `protocol`, `method`, `path`, `status`, `num`, `tags`, `pos`, `project_id`, `module_id`, `latest`, `version_id`, `ref_id`, `description`, `create_time`, `create_user`, `update_time`, `update_user`, `delete_user`, `delete_time`, `deleted`)