feat(测试计划): 测试计划详情用例批量关联缺陷

This commit is contained in:
WangXu10 2024-08-26 18:52:21 +08:00 committed by 刘瑞斌
parent bf1bc4a677
commit 1bf05cd0c1
4 changed files with 90 additions and 4 deletions

View File

@ -223,4 +223,12 @@ public class TestPlanFunctionalCaseController {
}
@PostMapping("/batch/associate-bug")
@Operation(summary = "测试计划-计划详情-功能用例-批量关联缺陷")
@RequiresPermissions(PermissionConstants.TEST_PLAN_READ_EXECUTE)
@CheckOwner(resourceId = "#request.getProjectId()", resourceType = "project")
public void batchAssociateBug(@Validated @RequestBody TestPlanCaseBatchAssociateBugRequest request) {
testPlanFunctionalCaseService.batchAssociateBugByIds(request, SessionUtils.getUserId());
}
}

View File

@ -0,0 +1,18 @@
package io.metersphere.plan.dto.request;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotEmpty;
import lombok.Data;
import java.util.List;
/**
* @author wx
*/
@Data
public class TestPlanCaseBatchAssociateBugRequest extends BasePlanCaseBatchRequest {
@Schema(description = "缺陷ID集合", requiredMode = Schema.RequiredMode.REQUIRED)
@NotEmpty(message = "{bug.id.not_blank}")
private List<String> bugIds;
}

View File

@ -1013,10 +1013,7 @@ public class TestPlanFunctionalCaseService extends TestPlanResourceService {
public void batchAssociateBug(TestPlanCaseBatchAddBugRequest request, String bugId, String userId) {
List<String> ids = doSelectIds(request);
if (CollectionUtils.isNotEmpty(ids)) {
TestPlanFunctionalCaseExample example = new TestPlanFunctionalCaseExample();
example.createCriteria().andIdIn(ids);
List<TestPlanFunctionalCase> caseList = testPlanFunctionalCaseMapper.selectByExample(example);
Map<String, String> caseMap = caseList.stream().collect(Collectors.toMap(TestPlanFunctionalCase::getId, TestPlanFunctionalCase::getFunctionalCaseId));
Map<String, String> caseMap = getCaseMap(ids);
List<BugRelationCase> list = new ArrayList<>();
ids.forEach(id -> {
BugRelationCase bugRelationCase = new BugRelationCase();
@ -1034,4 +1031,54 @@ public class TestPlanFunctionalCaseService extends TestPlanResourceService {
bugRelationCaseMapper.batchInsert(list);
}
}
private Map<String, String> getCaseMap(List<String> ids) {
TestPlanFunctionalCaseExample example = new TestPlanFunctionalCaseExample();
example.createCriteria().andIdIn(ids);
List<TestPlanFunctionalCase> caseList = testPlanFunctionalCaseMapper.selectByExample(example);
return caseList.stream().collect(Collectors.toMap(TestPlanFunctionalCase::getId, TestPlanFunctionalCase::getFunctionalCaseId));
}
public void batchAssociateBugByIds(TestPlanCaseBatchAssociateBugRequest request, String userId) {
List<String> ids = doSelectIds(request);
if (CollectionUtils.isNotEmpty(ids)) {
BugRelationCaseExample example = new BugRelationCaseExample();
example.createCriteria().andTestPlanCaseIdIn(ids).andTestPlanIdEqualTo(request.getTestPlanId()).andBugIdIn(request.getBugIds());
List<BugRelationCase> bugRelationCases = bugRelationCaseMapper.selectByExample(example);
Map<String, List<String>> bugMap = bugRelationCases.stream()
.collect(Collectors.groupingBy(
BugRelationCase::getTestPlanCaseId,
Collectors.mapping(BugRelationCase::getBugId, Collectors.toList())
));
Map<String, String> caseMap = getCaseMap(ids);
List<BugRelationCase> list = new ArrayList<>();
ids.forEach(item -> {
buildAssociateBugData(item, bugMap, list, request, caseMap, userId);
});
if (CollectionUtils.isNotEmpty(list)) {
bugRelationCaseMapper.batchInsert(list);
}
}
}
private void buildAssociateBugData(String id, Map<String, List<String>> bugMap, List<BugRelationCase> list, TestPlanCaseBatchAssociateBugRequest request, Map<String, String> caseMap, String userId) {
List<String> bugIds = request.getBugIds();
if (bugMap.containsKey(id)) {
bugIds.removeAll(bugMap.get(id));
}
bugIds.forEach(bugId -> {
BugRelationCase bugRelationCase = new BugRelationCase();
bugRelationCase.setId(IDGenerator.nextStr());
bugRelationCase.setBugId(bugId);
bugRelationCase.setCaseId(caseMap.get(id));
bugRelationCase.setCaseType(CaseType.FUNCTIONAL_CASE.getKey());
bugRelationCase.setCreateUser(userId);
bugRelationCase.setCreateTime(System.currentTimeMillis());
bugRelationCase.setUpdateTime(System.currentTimeMillis());
bugRelationCase.setTestPlanCaseId(id);
bugRelationCase.setTestPlanId(request.getTestPlanId());
list.add(bugRelationCase);
});
}
}

View File

@ -67,6 +67,7 @@ public class TestPlanCaseControllerTests extends BaseTest {
public static final String USER_URL = "/test-plan/functional/case/user-option/";
public static final String FUNCTIONAL_CASE_BATCH_MOVE_URL = "/test-plan/functional/case/batch/move";
public static final String FUNCTIONAL_CASE_BATCH_ADD_BUG_URL = "/test-plan/functional/case/batch/add-bug";
public static final String FUNCTIONAL_CASE_BATCH_ASSOCIATE_BUG_URL = "/test-plan/functional/case/batch/associate-bug";
@Resource
private TestPlanFunctionalCaseMapper testPlanFunctionalCaseMapper;
@Resource
@ -491,4 +492,16 @@ public class TestPlanCaseControllerTests extends BaseTest {
return request;
}
@Test
@Order(19)
public void testBatchAssociateBug() throws Exception {
TestPlanCaseBatchAssociateBugRequest request = new TestPlanCaseBatchAssociateBugRequest();
request.setBugIds(Arrays.asList("123456"));
request.setTestPlanId("plan_1");
this.requestPostWithOk(FUNCTIONAL_CASE_BATCH_ASSOCIATE_BUG_URL, request);
request.setSelectAll(true);
this.requestPostWithOk(FUNCTIONAL_CASE_BATCH_ASSOCIATE_BUG_URL, request);
}
}