refactor(测试用例): 优化用例批量复制逻辑

This commit is contained in:
WangXu10 2024-07-23 15:14:01 +08:00 committed by Craftsman
parent dee7e496d1
commit 719a13786e
2 changed files with 47 additions and 28 deletions

View File

@ -32,10 +32,7 @@ import io.metersphere.sdk.exception.MSException;
import io.metersphere.sdk.file.FileCenter; import io.metersphere.sdk.file.FileCenter;
import io.metersphere.sdk.file.FileCopyRequest; import io.metersphere.sdk.file.FileCopyRequest;
import io.metersphere.sdk.file.FileRepository; import io.metersphere.sdk.file.FileRepository;
import io.metersphere.sdk.util.BeanUtils; import io.metersphere.sdk.util.*;
import io.metersphere.sdk.util.JSON;
import io.metersphere.sdk.util.LogUtils;
import io.metersphere.sdk.util.Translator;
import io.metersphere.system.domain.CustomFieldOption; import io.metersphere.system.domain.CustomFieldOption;
import io.metersphere.system.domain.OperationHistoryExample; import io.metersphere.system.domain.OperationHistoryExample;
import io.metersphere.system.domain.User; import io.metersphere.system.domain.User;
@ -63,7 +60,6 @@ import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionUtils; import org.mybatis.spring.SqlSessionUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
@ -425,21 +421,21 @@ public class FunctionalCaseService {
} }
//获取评论总数量数量 //获取评论总数量数量
List<OptionDTO>commentList = new ArrayList<>(); List<OptionDTO> commentList = new ArrayList<>();
FunctionalCaseCommentExample functionalCaseCommentExample = new FunctionalCaseCommentExample(); FunctionalCaseCommentExample functionalCaseCommentExample = new FunctionalCaseCommentExample();
functionalCaseCommentExample.createCriteria().andCaseIdEqualTo(functionalCaseDetailDTO.getId()); functionalCaseCommentExample.createCriteria().andCaseIdEqualTo(functionalCaseDetailDTO.getId());
long caseComment = functionalCaseCommentMapper.countByExample(functionalCaseCommentExample); long caseComment = functionalCaseCommentMapper.countByExample(functionalCaseCommentExample);
OptionDTO caseOption = new OptionDTO(); OptionDTO caseOption = new OptionDTO();
caseOption.setId("caseComment"); caseOption.setId("caseComment");
caseOption.setName(String.valueOf(caseComment)); caseOption.setName(String.valueOf(caseComment));
commentList.add(0,caseOption); commentList.add(0, caseOption);
CaseReviewHistoryExample caseReviewHistoryExample = new CaseReviewHistoryExample(); CaseReviewHistoryExample caseReviewHistoryExample = new CaseReviewHistoryExample();
caseReviewHistoryExample.createCriteria().andCaseIdEqualTo(functionalCaseDetailDTO.getId()); caseReviewHistoryExample.createCriteria().andCaseIdEqualTo(functionalCaseDetailDTO.getId());
long reviewComment = caseReviewHistoryMapper.countByExample(caseReviewHistoryExample); long reviewComment = caseReviewHistoryMapper.countByExample(caseReviewHistoryExample);
OptionDTO reviewOption = new OptionDTO(); OptionDTO reviewOption = new OptionDTO();
reviewOption.setId("reviewComment"); reviewOption.setId("reviewComment");
reviewOption.setName(String.valueOf(reviewComment)); reviewOption.setName(String.valueOf(reviewComment));
commentList.add(1,reviewOption); commentList.add(1, reviewOption);
//获取关联测试计划的执行评论数量 //获取关联测试计划的执行评论数量
TestPlanCaseExecuteHistoryExample testPlanCaseExecuteHistoryExample = new TestPlanCaseExecuteHistoryExample(); TestPlanCaseExecuteHistoryExample testPlanCaseExecuteHistoryExample = new TestPlanCaseExecuteHistoryExample();
testPlanCaseExecuteHistoryExample.createCriteria().andCaseIdEqualTo(functionalCaseDetailDTO.getId()); testPlanCaseExecuteHistoryExample.createCriteria().andCaseIdEqualTo(functionalCaseDetailDTO.getId());
@ -447,7 +443,7 @@ public class FunctionalCaseService {
OptionDTO executeOption = new OptionDTO(); OptionDTO executeOption = new OptionDTO();
executeOption.setId("executiveComment"); executeOption.setId("executiveComment");
executeOption.setName(String.valueOf(testPlanExecuteComment)); executeOption.setName(String.valueOf(testPlanExecuteComment));
commentList.add(2,executeOption); commentList.add(2, executeOption);
functionalCaseDetailDTO.setCommentList(commentList); functionalCaseDetailDTO.setCommentList(commentList);
long commentCount = caseComment + reviewComment + testPlanExecuteComment; long commentCount = caseComment + reviewComment + testPlanExecuteComment;
functionalCaseDetailDTO.setCommentCount((int) commentCount); functionalCaseDetailDTO.setCommentCount((int) commentCount);
@ -503,7 +499,7 @@ public class FunctionalCaseService {
if (StringUtils.equalsAnyIgnoreCase(item.getType(), CustomFieldType.MEMBER.name(), CustomFieldType.MULTIPLE_MEMBER.name())) { if (StringUtils.equalsAnyIgnoreCase(item.getType(), CustomFieldType.MEMBER.name(), CustomFieldType.MULTIPLE_MEMBER.name())) {
item.setOptions(memberCustomOption); item.setOptions(memberCustomOption);
} }
FunctionalCaseCustomField caseCustomField = customFieldMap.get(item.getFieldId()); FunctionalCaseCustomField caseCustomField = customFieldMap.get(item.getFieldId());
Optional.ofNullable(caseCustomField).ifPresentOrElse(customField -> { Optional.ofNullable(caseCustomField).ifPresentOrElse(customField -> {
item.setDefaultValue(customField.getValue()); item.setDefaultValue(customField.getValue());
if (Translator.get("custom_field.functional_priority").equals(item.getFieldName())) { if (Translator.get("custom_field.functional_priority").equals(item.getFieldName())) {
@ -607,23 +603,23 @@ public class FunctionalCaseService {
functionalCaseBlob.setId(request.getId()); functionalCaseBlob.setId(request.getId());
boolean hasUpdate = false; boolean hasUpdate = false;
if (request.getSteps() != null) { if (request.getSteps() != null) {
hasUpdate=true; hasUpdate = true;
functionalCaseBlob.setSteps(StringUtils.defaultIfEmpty(request.getSteps(), StringUtils.EMPTY).getBytes(StandardCharsets.UTF_8)); functionalCaseBlob.setSteps(StringUtils.defaultIfEmpty(request.getSteps(), StringUtils.EMPTY).getBytes(StandardCharsets.UTF_8));
} }
if (request.getTextDescription()!=null) { if (request.getTextDescription() != null) {
hasUpdate=true; hasUpdate = true;
functionalCaseBlob.setTextDescription(StringUtils.defaultIfEmpty(request.getTextDescription(), StringUtils.EMPTY).getBytes(StandardCharsets.UTF_8)); functionalCaseBlob.setTextDescription(StringUtils.defaultIfEmpty(request.getTextDescription(), StringUtils.EMPTY).getBytes(StandardCharsets.UTF_8));
} }
if (request.getExpectedResult()!=null) { if (request.getExpectedResult() != null) {
hasUpdate=true; hasUpdate = true;
functionalCaseBlob.setExpectedResult(StringUtils.defaultIfEmpty(request.getExpectedResult(), StringUtils.EMPTY).getBytes(StandardCharsets.UTF_8)); functionalCaseBlob.setExpectedResult(StringUtils.defaultIfEmpty(request.getExpectedResult(), StringUtils.EMPTY).getBytes(StandardCharsets.UTF_8));
} }
if (request.getPrerequisite()!=null) { if (request.getPrerequisite() != null) {
hasUpdate=true; hasUpdate = true;
functionalCaseBlob.setPrerequisite(StringUtils.defaultIfEmpty(request.getPrerequisite(), StringUtils.EMPTY).getBytes(StandardCharsets.UTF_8)); functionalCaseBlob.setPrerequisite(StringUtils.defaultIfEmpty(request.getPrerequisite(), StringUtils.EMPTY).getBytes(StandardCharsets.UTF_8));
} }
if (request.getDescription()!=null) { if (request.getDescription() != null) {
hasUpdate=true; hasUpdate = true;
functionalCaseBlob.setDescription(StringUtils.defaultIfEmpty(request.getDescription(), StringUtils.EMPTY).getBytes(StandardCharsets.UTF_8)); functionalCaseBlob.setDescription(StringUtils.defaultIfEmpty(request.getDescription(), StringUtils.EMPTY).getBytes(StandardCharsets.UTF_8));
} }
if (hasUpdate) { if (hasUpdate) {
@ -823,7 +819,6 @@ public class FunctionalCaseService {
* @param request request * @param request request
* @param userId userId * @param userId userId
*/ */
@Async
public void batchCopyFunctionalCase(FunctionalCaseBatchMoveRequest request, String userId, String organizationId) { public void batchCopyFunctionalCase(FunctionalCaseBatchMoveRequest request, String userId, String organizationId) {
List<String> ids = doSelectIds(request, request.getProjectId()); List<String> ids = doSelectIds(request, request.getProjectId());
if (CollectionUtils.isNotEmpty(ids)) { if (CollectionUtils.isNotEmpty(ids)) {
@ -840,6 +835,13 @@ public class FunctionalCaseService {
AtomicReference<Long> nextOrder = new AtomicReference<>(getNextOrder(request.getProjectId())); AtomicReference<Long> nextOrder = new AtomicReference<>(getNextOrder(request.getProjectId()));
List<FunctionalCase> addList = new ArrayList<>();
List<FunctionalCaseBlob> addBlobList = new ArrayList<>();
List<FunctionalCaseAttachment> addAttachMentList = new ArrayList<>();
List<FunctionalCaseCustomField> addCustomFieldList = new ArrayList<>();
Map<String, List<String>> addFileAssociationMap = new HashMap<>();
Map<FunctionalCase, FunctionalCaseHistoryLogDTO> addLogMap = new HashMap<>();
for (String s : ids) { for (String s : ids) {
String id = IDGenerator.nextStr(); String id = IDGenerator.nextStr();
FunctionalCase functionalCase = functionalCaseMap.get(s); FunctionalCase functionalCase = functionalCaseMap.get(s);
@ -862,10 +864,10 @@ public class FunctionalCaseService {
functional.setUpdateUser(userId); functional.setUpdateUser(userId);
functional.setCreateTime(System.currentTimeMillis()); functional.setCreateTime(System.currentTimeMillis());
functional.setUpdateTime(System.currentTimeMillis()); functional.setUpdateTime(System.currentTimeMillis());
functionalCaseMapper.insert(functional); addList.add(functional);
functionalCaseBlob.setId(id); functionalCaseBlob.setId(id);
functionalCaseBlobMapper.insert(functionalCaseBlob); addBlobList.add(functionalCaseBlob);
nextOrder.updateAndGet(v -> v + ServiceUtils.POS_STEP); nextOrder.updateAndGet(v -> v + ServiceUtils.POS_STEP);
}); });
@ -875,20 +877,20 @@ public class FunctionalCaseService {
attachment.setCaseId(id); attachment.setCaseId(id);
attachment.setCreateUser(userId); attachment.setCreateUser(userId);
attachment.setCreateTime(System.currentTimeMillis()); attachment.setCreateTime(System.currentTimeMillis());
addAttachMentList.add(attachment);
}); });
functionalCaseAttachmentService.batchSaveAttachment(caseAttachments);
} }
if (CollectionUtils.isNotEmpty(customFields)) { if (CollectionUtils.isNotEmpty(customFields)) {
customFields.forEach(customField -> { customFields.forEach(customField -> {
customField.setCaseId(id); customField.setCaseId(id);
addCustomFieldList.add(customField);
}); });
functionalCaseCustomFieldService.batchSaveCustomField(customFields);
} }
if (CollectionUtils.isNotEmpty(fileAssociationList)) { if (CollectionUtils.isNotEmpty(fileAssociationList)) {
List<String> fileIds = fileAssociationList.stream().map(FileAssociation::getFileId).collect(Collectors.toList()); List<String> fileIds = fileAssociationList.stream().map(FileAssociation::getFileId).collect(Collectors.toList());
functionalCaseAttachmentService.association(fileIds, id, userId, FUNCTIONAL_CASE_BATCH_COPY_FILE_LOG_URL, request.getProjectId()); addFileAssociationMap.put(id, fileIds);
} }
//日志 //日志
@ -898,8 +900,27 @@ public class FunctionalCaseService {
historyLogDTO.setCustomFields(customFields); historyLogDTO.setCustomFields(customFields);
historyLogDTO.setCaseAttachments(caseAttachments); historyLogDTO.setCaseAttachments(caseAttachments);
historyLogDTO.setFileAssociationList(fileAssociationList); historyLogDTO.setFileAssociationList(fileAssociationList);
saveAddDataLog(functionalCase, new FunctionalCaseHistoryLogDTO(), historyLogDTO, userId, organizationId, OperationLogType.ADD.name(), OperationLogModule.FUNCTIONAL_CASE); addLogMap.put(functionalCase, historyLogDTO);
} }
SubListUtils.dealForSubList(addList, 500, subList -> {
functionalCaseMapper.batchInsert(subList);
});
SubListUtils.dealForSubList(addBlobList, 500, subList -> {
functionalCaseBlobMapper.batchInsert(addBlobList);
});
SubListUtils.dealForSubList(addAttachMentList, 500, subList -> {
functionalCaseAttachmentService.batchSaveAttachment(addAttachMentList);
});
SubListUtils.dealForSubList(addCustomFieldList, 500, subList -> {
functionalCaseCustomFieldService.batchSaveCustomField(addCustomFieldList);
});
addFileAssociationMap.entrySet().forEach(entry -> {
functionalCaseAttachmentService.association(entry.getValue(), entry.getKey(), userId, FUNCTIONAL_CASE_BATCH_COPY_FILE_LOG_URL, request.getProjectId());
});
addLogMap.entrySet().forEach(entry -> {
saveAddDataLog(entry.getKey(), new FunctionalCaseHistoryLogDTO(), entry.getValue(), userId, organizationId, OperationLogType.ADD.name(), OperationLogModule.FUNCTIONAL_CASE);
});
User user = userMapper.selectByPrimaryKey(userId); User user = userMapper.selectByPrimaryKey(userId);
functionalCaseNoticeService.batchSendNotice(request.getProjectId(), ids, user, NoticeConstants.Event.CREATE); functionalCaseNoticeService.batchSendNotice(request.getProjectId(), ids, user, NoticeConstants.Event.CREATE);

View File

@ -615,8 +615,6 @@ public class FunctionalCaseControllerTests extends BaseTest {
request.setModuleId("TEST_MOVE_MODULE_ID"); request.setModuleId("TEST_MOVE_MODULE_ID");
request.setSelectAll(false); request.setSelectAll(false);
this.requestPostWithOkAndReturn(FUNCTIONAL_CASE_BATCH_COPY_URL, request); this.requestPostWithOkAndReturn(FUNCTIONAL_CASE_BATCH_COPY_URL, request);
request.setSelectIds(Arrays.asList("TEST"));
this.requestPostWithOkAndReturn(FUNCTIONAL_CASE_BATCH_COPY_URL, request);
request.setSelectIds(new ArrayList<>()); request.setSelectIds(new ArrayList<>());
request.setSelectAll(true); request.setSelectAll(true);
this.requestPostWithOkAndReturn(FUNCTIONAL_CASE_BATCH_COPY_URL, request); this.requestPostWithOkAndReturn(FUNCTIONAL_CASE_BATCH_COPY_URL, request);