fix(用例管理): 导入数据,用例步骤&预期结果长度校验

--bug=1035913 --user=王旭 【用例管理】功能用例-导入用例,步骤描述中用例步骤和预期结果字符长度未限制1000个字符 https://www.tapd.cn/55049933/s/1466547
This commit is contained in:
WangXu10 2024-02-29 10:58:51 +08:00 committed by 刘瑞斌
parent 03a30337f2
commit b56c58c998
5 changed files with 99 additions and 2 deletions

View File

@ -323,6 +323,8 @@ test_case_status=Case status
id_not_rightful=ID is not rightful
tags_count=Tags count can not exceed 15
tag_length=Tag length cannot exceed 64 characters
step_length=Step length cannot exceed 1000 characters
result_length=Result length cannot exceed 1000 characters
project_reference_multiple_plateform=Projects point to multiple third-party platforms
# mock
mock_warning=No matching Mock expectation was found

View File

@ -324,6 +324,8 @@ test_case_status=用例状态
id_not_rightful=ID 不合法
tags_count=标签数量不能超过15个
tag_length=标签长度不能超过64个字符
step_length=用例步骤不能超过1000个字符
result_length=预期结果不能超过1000个字符
project_reference_multiple_plateform=项目指向多个第三方平台
# mock
mock_warning=未找到匹配的Mock期望

View File

@ -323,6 +323,8 @@ test_case_status=用例狀態
id_not_rightful=ID 不合法
tags_count=標簽數量不能超過15個
tag_length=標簽長度不能超過64個字符
step_length=用例步驟長度不能超過1000個字符
result_length=預期結果長度不能超過1000個字符
project_reference_multiple_plateform=項目指向多個第三方平臺
# mock
mock_warning=未找到匹配的Mock期望

View File

@ -3,6 +3,7 @@ package io.metersphere.functional.excel.listener;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import io.metersphere.functional.constants.FunctionalCaseTypeConstants;
import io.metersphere.functional.excel.annotation.NotRequired;
import io.metersphere.functional.excel.domain.ExcelMergeInfo;
import io.metersphere.functional.excel.domain.FunctionalCaseExcelData;
@ -14,11 +15,13 @@ import io.metersphere.functional.request.FunctionalCaseImportRequest;
import io.metersphere.functional.service.FunctionalCaseService;
import io.metersphere.sdk.exception.MSException;
import io.metersphere.sdk.util.CommonBeanFactory;
import io.metersphere.sdk.util.JSON;
import io.metersphere.sdk.util.LogUtils;
import io.metersphere.sdk.util.Translator;
import io.metersphere.system.dto.excel.ExcelValidateHelper;
import io.metersphere.system.dto.sdk.TemplateCustomFieldDTO;
import io.metersphere.system.excel.domain.ExcelErrData;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.Nullable;
@ -59,7 +62,8 @@ public class FunctionalCaseCheckEventListener extends AnalysisEventListener<Map<
private static final String ERROR_MSG_SEPARATOR = ";";
private HashMap<String, AbstractCustomFieldValidator> customFieldValidatorMap;
protected static final int TAGS_COUNT = 15;
protected static final int TAG_LENGTH = 15;
protected static final int TAG_LENGTH = 64;
protected static final int STEP_LENGTH = 1000;
private FunctionalCaseService functionalCaseService;
public FunctionalCaseCheckEventListener(FunctionalCaseImportRequest request, Class clazz, List<TemplateCustomFieldDTO> customFields, Set<ExcelMergeInfo> mergeInfoSet) {
@ -209,8 +213,95 @@ public class FunctionalCaseCheckEventListener extends AnalysisEventListener<Map<
validateIdExist(data, errMsg);
//标签长度校验
validateTags(data, errMsg);
//校验用例步骤和预期结果长度
validateStep(data, errMsg);
}
/**
* 校验用例步骤 预期结果
*
* @param data
* @param errMsg
*/
private void validateStep(FunctionalCaseExcelData data, StringBuilder errMsg) {
if (StringUtils.equalsIgnoreCase(data.getCaseEditType(), FunctionalCaseTypeConstants.CaseEditType.STEP.name())) {
getSteps(data, errMsg);
}
}
private String getSteps(FunctionalCaseExcelData data,StringBuilder errMsg) {
List<Map<String, Object>> steps = new ArrayList<>();
if (CollectionUtils.isNotEmpty(data.getMergeTextDescription()) || CollectionUtils.isNotEmpty(data.getMergeExpectedResult())) {
// 如果是合并单元格则组合多条单元格的数据
for (int i = 0; i < data.getMergeTextDescription().size(); i++) {
List<Map<String, Object>> rowSteps = getSingleRowSteps(data.getMergeTextDescription().get(i), data.getMergeExpectedResult().get(i), steps.size(),errMsg);
steps.addAll(rowSteps);
}
} else {
// 如果不是合并单元格则直接解析单元格数据
steps.addAll(getSingleRowSteps(data.getTextDescription(), data.getExpectedResult(), steps.size(),errMsg));
}
return JSON.toJSONString(steps);
}
private List<Map<String, Object>> getSingleRowSteps(String cellDesc, String cellResult, Integer startStepIndex,StringBuilder errMsg) {
List<Map<String, Object>> steps = new ArrayList<>();
List<String> stepDescList = parseStepCell(cellDesc);
List<String> stepResList = parseStepCell(cellResult);
int index = Math.max(stepDescList.size(), stepResList.size());
for (int i = 0; i < index; i++) {
// 保持插入顺序判断用例是否有相同的steps
Map<String, Object> step = new LinkedHashMap<>();
step.put("num", startStepIndex + i + 1);
if (i < stepDescList.size()) {
step.put("desc", stepDescList.get(i));
if (stepDescList.get(i).length() > STEP_LENGTH) {
errMsg.append(Translator.get("step_length"))
.append(ERROR_MSG_SEPARATOR);
return steps;
}
} else {
step.put("desc", StringUtils.EMPTY);
}
if (i < stepResList.size()) {
step.put("result", stepResList.get(i));
if (stepResList.get(i).length() > STEP_LENGTH) {
errMsg.append(Translator.get("result_length"))
.append(ERROR_MSG_SEPARATOR);
return steps;
}
} else {
step.put("result", StringUtils.EMPTY);
}
steps.add(step);
}
return steps;
}
private List<String> parseStepCell(String cellContent) {
List<String> cellStepContentList = new ArrayList<>();
if (StringUtils.isNotEmpty(cellContent)) {
// 根据[1], [2]...分割步骤描述, 开头空字符去掉, 末尾保留
String[] cellContentArr = cellContent.split("\\[\\d+]", -1);
if (StringUtils.isEmpty(cellContentArr[0])) {
cellContentArr = Arrays.copyOfRange(cellContentArr, 1, cellContentArr.length);
}
for (String stepContent : cellContentArr) {
cellStepContentList.add(stepContent.replaceAll("(?m)^\\s*|\\s*$", StringUtils.EMPTY));
}
} else {
cellStepContentList.add(StringUtils.EMPTY);
}
return cellStepContentList;
}
/**
* 校验标签长度 个数
*

View File

@ -82,7 +82,7 @@ public class FunctionalCaseImportEventListener extends AnalysisEventListener<Map
private int successCount = 0;
private Map<String, String> pathMap = new HashMap<>();
protected static final int TAGS_COUNT = 15;
protected static final int TAG_LENGTH = 15;
protected static final int TAG_LENGTH = 64;
public FunctionalCaseImportEventListener(FunctionalCaseImportRequest request, Class clazz, List<TemplateCustomFieldDTO> customFields, Set<ExcelMergeInfo> mergeInfoSet, SessionUser user) {
this.mergeInfoSet = mergeInfoSet;