refactor(测试跟踪): 去掉用例导入步骤单元格特殊字符解析

--bug=1029160 --user=宋昌昌 【测试跟踪】功能用例-excel导入未换行显示 https://www.tapd.cn/55049933/s/1406796
This commit is contained in:
song-cc-rock 2023-08-22 10:11:43 +08:00 committed by 刘瑞斌
parent 799c67fbee
commit 5123e14315
1 changed files with 64 additions and 97 deletions

View File

@ -35,6 +35,7 @@ import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.Serial;
import java.lang.reflect.Field;
import java.util.*;
import java.util.stream.Collectors;
@ -117,14 +118,14 @@ public class TestCaseNoModelDataListener extends AnalysisEventListener<Map<Integ
public TestCaseNoModelDataListener(TestCaseImportRequest request, Class c, Set<ExcelMergeInfo> mergeInfoSet) {
this.mergeInfoSet = mergeInfoSet;
this.excelDataClass = c;
this.testCaseService = CommonBeanFactory.getBean(TestCaseService.class);
this.testCaseNodeService = CommonBeanFactory.getBean(TestCaseNodeService.class);
excelDataClass = c;
testCaseService = CommonBeanFactory.getBean(TestCaseService.class);
testCaseNodeService = CommonBeanFactory.getBean(TestCaseNodeService.class);
customIds = new HashSet<>();
this.request = request;
this.customFieldValidatorMap = CustomFieldValidatorFactory.getValidatorMap();
customFieldValidatorMap = CustomFieldValidatorFactory.getValidatorMap();
List<CustomFieldDao> customFields = request.getCustomFields();
if (CollectionUtils.isNotEmpty(customFields)) {
@ -138,14 +139,15 @@ public class TestCaseNoModelDataListener extends AnalysisEventListener<Map<Integ
public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
this.headMap = headMap;
try {
this.genExcelHeadToFieldNameDicAndGetNotRequiredFields();
genExcelHeadToFieldNameDicAndGetNotRequiredFields();
} catch (NoSuchFieldException e) {
LogUtil.error(e);
}
this.formatHeadMap();
formatHeadMap();
super.invokeHeadMap(headMap, context);
}
@Override
public void invoke(Map<Integer, String> data, AnalysisContext analysisContext) {
if (headMap == null) {
@ -158,41 +160,51 @@ public class TestCaseNoModelDataListener extends AnalysisEventListener<Map<Integ
TestCaseExcelData testCaseExcelData;
// 读取名称列如果该列是合并单元格则读取多行数据后合并步骤
if (this.isMergeRow) {
if (isMergeRow) {
if (currentMergeData == null) {
this.firstMergeRowIndex = rowIndex;
firstMergeRowIndex = rowIndex;
// 如果是合并单元格的首行
testCaseExcelData = this.parseDataToModel(data);
testCaseExcelData.setMergeStepDesc(new ArrayList<>() {{
add(testCaseExcelData.getStepDesc());
}});
testCaseExcelData.setMergeStepResult(new ArrayList<>() {{
add(testCaseExcelData.getStepResult());
}});
testCaseExcelData = parseDataToModel(data);
testCaseExcelData.setMergeStepDesc(new ArrayList<>() {
@Serial
private static final long serialVersionUID = -2563948462432733672L;
{
add(testCaseExcelData.getStepDesc());
}
});
testCaseExcelData.setMergeStepResult(new ArrayList<>() {
@Serial
private static final long serialVersionUID = 8985001651375529701L;
{
add(testCaseExcelData.getStepResult());
}
});
// 记录下数据并返回
this.currentMergeData = testCaseExcelData;
if (!this.isMergeLastRow) {
currentMergeData = testCaseExcelData;
if (!isMergeLastRow) {
return;
} else {
this.currentMergeData = null;
currentMergeData = null;
}
} else {
// 获取存储的数据并添加多个步骤
this.currentMergeData.getMergeStepDesc()
currentMergeData.getMergeStepDesc()
.add(data.get(getStepDescColIndex()));
this.currentMergeData.getMergeStepResult()
currentMergeData.getMergeStepResult()
.add(data.get(getStepResultColIndex()));
// 是最后一行的合并单元格保存并清空 currentMergeData走之后的逻辑
if (this.isMergeLastRow) {
testCaseExcelData = this.currentMergeData;
this.currentMergeData = null;
if (isMergeLastRow) {
testCaseExcelData = currentMergeData;
currentMergeData = null;
} else {
return;
}
}
} else {
this.firstMergeRowIndex = null;
testCaseExcelData = this.parseDataToModel(data);
firstMergeRowIndex = null;
testCaseExcelData = parseDataToModel(data);
}
buildUpdateOrErrorList(rowIndex, testCaseExcelData);
@ -246,8 +258,8 @@ public class TestCaseNoModelDataListener extends AnalysisEventListener<Map<Integ
* @param rowIndex
*/
private void handleMergeData(Map<Integer, String> data, Integer rowIndex) {
this.isMergeRow = false;
this.isMergeLastRow = false;
isMergeRow = false;
isMergeLastRow = false;
if (getNameColIndex() == null) {
MSException.throwException("缺少名称表头");
}
@ -260,7 +272,7 @@ public class TestCaseNoModelDataListener extends AnalysisEventListener<Map<Integ
&& col.equals(mergeInfo.getFirstColumnIndex())) {
// 根据名称列是否是合并单元格判断是不是同一条用例
if (getNameColIndex().equals(col)) {
this.isMergeRow = true;
isMergeRow = true;
}
// 如果是合并单元格的第一个cell则把这个单元格的数据存起来
if (rowIndex.equals(mergeInfo.getFirstRowIndex())) {
@ -278,7 +290,7 @@ public class TestCaseNoModelDataListener extends AnalysisEventListener<Map<Integ
if (rowIndex.equals(mergeInfo.getLastRowIndex())) {
// 根据名称列是否是合并单元格判断是不是同一条用例
if (getNameColIndex().equals(col)) {
this.isMergeLastRow = true;
isMergeLastRow = true;
// 清除掉上一次已经遍历完成的数据提高查询效率
iterator.remove();
break;
@ -305,7 +317,7 @@ public class TestCaseNoModelDataListener extends AnalysisEventListener<Map<Integ
private void validateDbExist(TestCaseExcelData data, StringBuilder stringBuilder) {
// 校验模块是否存在没有存在则新建一个模块
testCaseNodeService.createNodeByNodePath(data.getNodePath(), request.getProjectId(), nodeTrees, pathMap);
if (this.isUpdateModel()) {
if (isUpdateModel()) {
return;
}
if (request.getTestCaseNames().contains(data.getName())) {
@ -411,6 +423,7 @@ public class TestCaseNoModelDataListener extends AnalysisEventListener<Map<Integ
/**
* 校验自定义字段并记录错误提示
* 如果填写的是自定义字段的选项值则转换成ID保存
*
* @param data
* @param stringBuilder
*/
@ -511,11 +524,11 @@ public class TestCaseNoModelDataListener extends AnalysisEventListener<Map<Integ
}
public List<String> getNames() {
return this.names;
return names;
}
public List<String> getIds() {
return this.ids;
return ids;
}
public void setNames(List<String> names) {
@ -533,27 +546,27 @@ public class TestCaseNoModelDataListener extends AnalysisEventListener<Map<Integ
return;
}
if ((isCreateModel() && CollectionUtils.isEmpty(list)) || (isUpdateModel() && CollectionUtils.isEmpty(updateList))) {
MSException.throwException(Translator.get("no_legitimate_case_tip"));
MSException.throwException(Translator.get("no_legitimate_case_tip"));
}
if (CollectionUtils.isNotEmpty(list)) {
List<TestCaseWithBLOBs> result = list.stream()
.map(item -> this.convert2TestCase(item))
.map(item -> convert2TestCase(item))
.collect(Collectors.toList());
testCaseService.saveImportData(result, request, testCaseCustomFieldMap, pathMap);
this.names = result.stream().map(TestCase::getName).collect(Collectors.toList());
this.ids = result.stream().map(TestCase::getId).collect(Collectors.toList());
this.isUpdated = true;
names = result.stream().map(TestCase::getName).collect(Collectors.toList());
ids = result.stream().map(TestCase::getId).collect(Collectors.toList());
isUpdated = true;
}
if (CollectionUtils.isNotEmpty(updateList)) {
List<TestCaseWithBLOBs> result2 = updateList.stream()
.map(item -> this.convert2TestCaseForUpdate(item))
.map(item -> convert2TestCaseForUpdate(item))
.collect(Collectors.toList());
testCaseService.updateImportData(result2, request, testCaseCustomFieldMap);
this.isUpdated = true;
this.names = result2.stream().map(TestCase::getName).collect(Collectors.toList());
this.ids = result2.stream().map(TestCase::getId).collect(Collectors.toList());
isUpdated = true;
names = result2.stream().map(TestCase::getName).collect(Collectors.toList());
ids = result2.stream().map(TestCase::getId).collect(Collectors.toList());
updateList.clear();
}
}
@ -573,6 +586,7 @@ public class TestCaseNoModelDataListener extends AnalysisEventListener<Map<Integ
/**
* 暂存功能自定义字段
*
* @param data
* @param testCase
*/
@ -581,7 +595,7 @@ public class TestCaseNoModelDataListener extends AnalysisEventListener<Map<Integ
Set<String> textFieldSet = data.getTextFieldSet();
List<CustomFieldResourceDTO> testCaseCustomFields = new ArrayList<>();
customData.forEach((k, v) -> {
if ((v instanceof List && CollectionUtils.isNotEmpty((List)v))
if ((v instanceof List && CollectionUtils.isNotEmpty((List) v))
|| StringUtils.isNotBlank(v.toString())) {
CustomFieldDao customFieldDao = customFieldsMap.get(k);
if (customFieldDao != null) {
@ -678,6 +692,7 @@ public class TestCaseNoModelDataListener extends AnalysisEventListener<Map<Integ
/**
* 解析合并步骤描述, 预期结果单元格数据
*
* @param data Excel数据
* @return 步骤JSON-String
*/
@ -699,8 +714,9 @@ public class TestCaseNoModelDataListener extends AnalysisEventListener<Map<Integ
/**
* 解析单行步骤描述, 预期结果数据
* @param cellDesc 步骤描述
* @param cellResult 预期结果
*
* @param cellDesc 步骤描述
* @param cellResult 预期结果
* @param startStepIndex 步骤开始序号
* @return 步骤JSON-String
*/
@ -716,11 +732,7 @@ public class TestCaseNoModelDataListener extends AnalysisEventListener<Map<Integ
stepDesc = Arrays.copyOfRange(stepDesc, 1, stepDesc.length);
}
int rowIndex = 1;
for (String row : stepDesc) {
RowInfo rowInfo = this.parseIndexInRow(row, rowIndex);
stepDescList.add(rowInfo.rowInfo);
}
stepDescList.addAll(Arrays.asList(stepDesc));
} else {
stepDescList.add(StringUtils.EMPTY);
}
@ -731,13 +743,7 @@ public class TestCaseNoModelDataListener extends AnalysisEventListener<Map<Integ
if (StringUtils.isEmpty(stepRes[0])) {
stepRes = Arrays.copyOfRange(stepRes, 1, stepRes.length);
}
int lastStepIndex = 1;
for (String row : stepRes) {
RowInfo rowInfo = this.parseIndexInRow(row, lastStepIndex);
String rowMessage = rowInfo.rowInfo;
stepResList.add(rowMessage);
lastStepIndex++;
}
stepResList.addAll(Arrays.asList(stepRes));
} else {
stepResList.add(StringUtils.EMPTY);
}
@ -764,45 +770,6 @@ public class TestCaseNoModelDataListener extends AnalysisEventListener<Map<Integ
return steps;
}
private RowInfo parseIndexInRow(String row, int rowIndex) {
RowInfo rowInfo = new RowInfo();
String parseString = row;
int index = -1;
String rowMessage = row;
String[] indexSplitCharArr = new String[]{")", "", "]", "", ".", ",", "", ""};
if (StringUtils.startsWithAny(row, "(", "", "[", "")) {
parseString = parseString.substring(1);
}
for (String splitChar : indexSplitCharArr) {
if (StringUtils.contains(parseString, splitChar)) {
String[] rowSplit = StringUtils.split(parseString, splitChar);
if (rowSplit.length > 0) {
String indexString = rowSplit[0];
if (StringUtils.isNumeric(indexString) && indexString.equals(String.valueOf(rowIndex))) {
try {
index = Integer.parseInt(indexString);
rowMessage = StringUtils.substring(parseString, indexString.length() + splitChar.length());
} catch (Exception e) {
}
if (index > -1) {
break;
} else {
rowMessage = row;
}
}
}
}
}
rowInfo.index = index;
if (rowMessage == null) {
rowMessage = StringUtils.EMPTY;
}
rowInfo.rowInfo = rowMessage;
return rowInfo;
}
private Integer getNameColIndex() {
return findColIndex("name");
}
@ -872,13 +839,13 @@ public class TestCaseNoModelDataListener extends AnalysisEventListener<Map<Integ
}
public List<ExcelErrData<TestCaseExcelData>> getErrList() {
return this.errList;
return errList;
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
// 如果文件最后一行是没有内容的步骤这里处理最后一条合并单元格的数据
if (this.currentMergeData != null) {
if (currentMergeData != null) {
buildUpdateOrErrorList(firstMergeRowIndex, currentMergeData);
}
saveData();