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