fix(测试跟踪): 用例状态添加新选项导出后显示错误

--bug=1015649 --user=陈建星 【测试跟踪】功能用例,用例状态字段值修改为自定义内容后,导出文件里显示的编码 https://www.tapd.cn/55049933/s/1219125
This commit is contained in:
chenjianxing 2022-08-04 11:38:49 +08:00 committed by jianxing
parent 8c14a3871d
commit 78b1d7b921
12 changed files with 109 additions and 143 deletions

View File

@ -0,0 +1,11 @@
package io.metersphere.dto;
import lombok.Getter;
import lombok.Setter;
@Setter
@Getter
public class CustomFieldOption {
private String text;
private String value;
}

View File

@ -4,7 +4,6 @@ import com.alibaba.excel.annotation.ExcelIgnore;
import io.metersphere.dto.CustomFieldDao;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import java.util.*;
@ -46,33 +45,9 @@ public class TestCaseExcelData {
@ExcelIgnore
private String priority;
@ExcelIgnore
Map<String,String> customDatas = new LinkedHashMap<>();
Map<String, String> customDatas = new LinkedHashMap<>();
public Set<String> getExcludeColumnFiledNames(boolean needNum){
Set<String> excludeColumnFiledNames = new HashSet<>();
if(!needNum){
excludeColumnFiledNames.add("customNum");
}
excludeColumnFiledNames.add("id");
excludeColumnFiledNames.add("num");
return excludeColumnFiledNames;
}
public String parseStatus(String parseStatus){
String caseStatusValue = "";
if(StringUtils.equalsAnyIgnoreCase(parseStatus,"Underway","进行中","進行中")){
caseStatusValue = "Underway";
}else if(StringUtils.equalsAnyIgnoreCase(parseStatus,"Prepare","未开始","未開始")){
caseStatusValue = "Prepare";
}else if(StringUtils.equalsAnyIgnoreCase(parseStatus,"Completed","已完成","已完成")){
caseStatusValue = "Completed";
}else if(StringUtils.equalsAnyIgnoreCase(parseStatus,"Trash")){
caseStatusValue = "Trash";
}
return caseStatusValue;
}
public List<List<String>> getHead(boolean needNum, List<CustomFieldDao> customFields){
public List<List<String>> getHead(boolean needNum, List<CustomFieldDao> customFields) {
return new ArrayList<>();
}
}

View File

@ -85,9 +85,9 @@ public class TestCaseExcelDataCn extends TestCaseExcelData {
private String priority;
@Override
public List<List<String>> getHead(boolean needNum, List<CustomFieldDao> customFields){
public List<List<String>> getHead(boolean needNum, List<CustomFieldDao> customFields) {
List<List<String>> returnList = new ArrayList<>();
if(needNum){
if (needNum) {
List<String> list = new ArrayList<>();
list.add("ID");
returnList.add(list);
@ -129,9 +129,9 @@ public class TestCaseExcelDataCn extends TestCaseExcelData {
list9.add("用例等级");
returnList.add(list9);
if(CollectionUtils.isNotEmpty(customFields)){
for (CustomFieldDao dto:customFields) {
if(StringUtils.equals(dto.getName(),"用例等级")){
if (CollectionUtils.isNotEmpty(customFields)) {
for (CustomFieldDao dto : customFields) {
if (StringUtils.equals(dto.getName(), "用例等级")) {
continue;
}
List<String> list = new ArrayList<>();
@ -145,19 +145,4 @@ public class TestCaseExcelDataCn extends TestCaseExcelData {
}
return returnList;
}
@Override
public String parseStatus(String parseStatus){
String caseStatusValue = "";
if(StringUtils.equalsAnyIgnoreCase(parseStatus,"Underway","进行中","進行中")){
caseStatusValue = "进行中";
}else if(StringUtils.equalsAnyIgnoreCase(parseStatus,"Prepare","未开始","未開始")){
caseStatusValue = "未开始";
}else if(StringUtils.equalsAnyIgnoreCase(parseStatus,"Completed","已完成","已完成")){
caseStatusValue = "已完成";
}else if(StringUtils.equalsAnyIgnoreCase(parseStatus,"Trash","废弃","廢棄")){
caseStatusValue = "废弃";
}
return caseStatusValue;
}
}

View File

@ -85,9 +85,9 @@ public class TestCaseExcelDataTw extends TestCaseExcelData {
private String maintainer;
@Override
public List<List<String>> getHead(boolean needNum, List<CustomFieldDao> customFields){
public List<List<String>> getHead(boolean needNum, List<CustomFieldDao> customFields) {
List<List<String>> returnList = new ArrayList<>();
if(needNum){
if (needNum) {
List<String> list = new ArrayList<>();
list.add("ID");
returnList.add(list);
@ -133,12 +133,12 @@ public class TestCaseExcelDataTw extends TestCaseExcelData {
list10.add("用例狀態");
returnList.add(list10);
if(CollectionUtils.isNotEmpty(customFields)){
for (CustomFieldDao dto:customFields) {
if(StringUtils.equals(dto.getName(),"用例等級")){
if (CollectionUtils.isNotEmpty(customFields)) {
for (CustomFieldDao dto : customFields) {
if (StringUtils.equals(dto.getName(), "用例等級")) {
continue;
}
if(StringUtils.equals(dto.getName(),"用例状态")){
if (StringUtils.equals(dto.getName(), "用例状态")) {
continue;
}
List<String> list = new ArrayList<>();
@ -152,19 +152,4 @@ public class TestCaseExcelDataTw extends TestCaseExcelData {
}
return returnList;
}
@Override
public String parseStatus(String parseStatus){
String caseStatusValue = "";
if(StringUtils.equalsAnyIgnoreCase(parseStatus,"Underway","进行中","進行中")){
caseStatusValue = "進行中";
}else if(StringUtils.equalsAnyIgnoreCase(parseStatus,"Prepare","未开始","未開始")){
caseStatusValue = "未開始";
}else if(StringUtils.equalsAnyIgnoreCase(parseStatus,"Completed","已完成","已完成")){
caseStatusValue = "已完成";
}else if(StringUtils.equalsAnyIgnoreCase(parseStatus,"Trash","废弃","廢棄")){
caseStatusValue = "廢棄";
}
return caseStatusValue;
}
}

View File

@ -85,9 +85,9 @@ public class TestCaseExcelDataUs extends TestCaseExcelData {
private String maintainer;
@Override
public List<List<String>> getHead(boolean needNum, List<CustomFieldDao> customFields){
public List<List<String>> getHead(boolean needNum, List<CustomFieldDao> customFields) {
List<List<String>> returnList = new ArrayList<>();
if(needNum){
if (needNum) {
List<String> list = new ArrayList<>();
list.add("ID");
returnList.add(list);
@ -133,12 +133,12 @@ public class TestCaseExcelDataUs extends TestCaseExcelData {
list10.add("Case status");
returnList.add(list10);
if(CollectionUtils.isNotEmpty(customFields)){
for (CustomFieldDao dto:customFields) {
if(StringUtils.equals(dto.getName(),"用例等级")){
if (CollectionUtils.isNotEmpty(customFields)) {
for (CustomFieldDao dto : customFields) {
if (StringUtils.equals(dto.getName(), "用例等级")) {
continue;
}
if(StringUtils.equals(dto.getName(),"用例状态")){
if (StringUtils.equals(dto.getName(), "用例状态")) {
continue;
}
List<String> list = new ArrayList<>();
@ -152,19 +152,4 @@ public class TestCaseExcelDataUs extends TestCaseExcelData {
}
return returnList;
}
@Override
public String parseStatus(String parseStatus){
String caseStatusValue = "";
if(StringUtils.equalsAnyIgnoreCase(parseStatus,"Underway","进行中","進行中")){
caseStatusValue = "Underway";
}else if(StringUtils.equalsAnyIgnoreCase(parseStatus,"Prepare","未开始","未開始")){
caseStatusValue = "Prepare";
}else if(StringUtils.equalsAnyIgnoreCase(parseStatus,"Completed","已完成","已完成")){
caseStatusValue = "Completed";
}else if(StringUtils.equalsAnyIgnoreCase(parseStatus,"Trash")){
caseStatusValue = "Trash";
}
return caseStatusValue;
}
}

View File

@ -11,6 +11,7 @@ import io.metersphere.commons.constants.TestCaseConstants;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.*;
import io.metersphere.dto.CustomFieldDao;
import io.metersphere.dto.CustomFieldOption;
import io.metersphere.excel.annotation.NotRequired;
import io.metersphere.excel.domain.ExcelErrData;
import io.metersphere.excel.domain.TestCaseExcelData;
@ -207,10 +208,14 @@ public class TestCaseNoModelDataListener extends AnalysisEventListener<Map<Integ
if (field.getRequired()) {
String value = null;
if (StringUtils.equals(customName, "status")) {
value = data.getStatus();
if (!checkCaseStatus(value)) {
stringBuilder.append(Translator.get("case_status_not_exist") + "; ");
}
Map<String, String> valueMap = new HashMap<>() {{
put("Prepare", Translator.get("test_case_status_prepare"));
put("Underway", Translator.get("test_case_status_running"));
put("Completed", Translator.get("test_case_status_finished"));
}};
value = getCustomFieldValue(Translator.get("test_case_status"), data.getStatus(), field.getOptions(),
stringBuilder, valueMap);
data.setStatus(value);
} else if (StringUtils.equals(customName, "priority")) {
value = data.getPriority();
} else if (StringUtils.equals(customName, "maintainer")) {
@ -313,6 +318,36 @@ public class TestCaseNoModelDataListener extends AnalysisEventListener<Map<Integ
return stringBuilder.toString();
}
/**
* 根据自定义字段的选项值获取对应的选项id
* @param name
* @param text
* @param optionsStr
* @return
*/
public String getCustomFieldValue(String name, String text, String optionsStr, StringBuilder error,
Map<String, String> systemValueMap) {
List<String> textList = new ArrayList<>();
if (StringUtils.isNotEmpty(optionsStr)) {
List<CustomFieldOption> options = JSONObject.parseArray(optionsStr, CustomFieldOption.class);
for (CustomFieldOption option : options) {
// 系统字段需要翻译
String i18nText = systemValueMap.get(option.getValue());
if (i18nText != null) {
option.setText(i18nText);
}
if (StringUtils.equals(option.getValue(), text) ||
// 系统字段填写对应内容而不是key的情况比如用例状态填未开始而不是 Prepare
StringUtils.equals(option.getText(), text)) {
return option.getValue();
}
textList.add(option.getText());
}
}
error.append(String.format(Translator.get("custom_field_option_not_exist"), name, textList.toString()));
return text;
}
public List<String> getNames() {
return this.names;
}

View File

@ -730,7 +730,6 @@ public class TestCaseService {
buildProjectInfoWidthoutProject(list);
}
buildCustomField(list);
list = this.parseStatus(list);
}
return list;
}
@ -780,7 +779,6 @@ public class TestCaseService {
ServiceUtils.buildVersionInfo(returnList);
ServiceUtils.buildProjectInfo(returnList);
buildUserInfo(returnList);
returnList = this.parseStatus(returnList);
return returnList;
}
@ -791,41 +789,6 @@ public class TestCaseService {
request.setOrders(orders);
}
private List<TestCaseDTO> parseStatus(List<TestCaseDTO> returnList) {
if (CollectionUtils.isNotEmpty(returnList)) {
TestCaseExcelData excelData = new TestCaseExcelDataFactory().getTestCaseExcelDataLocal();
List<String> testCaseIdList = new ArrayList<>();
returnList.forEach(item -> {
testCaseIdList.add(item.getId());
});
for (TestCaseDTO data : returnList) {
String dataStatus = excelData.parseStatus(data.getStatus());
if (StringUtils.equalsAnyIgnoreCase(data.getStatus(), "Trash")) {
try {
JSONArray arr = JSONArray.parseArray(data.getCustomFields());
JSONArray newArr = new JSONArray();
for (int i = 0; i < arr.size(); i++) {
JSONObject obj = arr.getJSONObject(i);
if (obj.containsKey("name") && obj.containsKey("value")) {
String name = obj.getString("name");
if (StringUtils.equalsAny(name, "用例状态", "用例狀態", "Case status")) {
obj.put("value", dataStatus);
}
}
newArr.add(obj);
}
data.setCustomFields(newArr.toJSONString());
} catch (Exception e) {
LogUtil.error("Parse case exec status error:" + e.getMessage());
}
}
data.setStatus(dataStatus);
}
}
return returnList;
}
/**
* 初始化部分参数
*
@ -1166,7 +1129,9 @@ public class TestCaseService {
}
num++;
testCase.setReviewStatus(TestCaseReviewStatus.Prepare.name());
testCase.setStatus(TestCaseReviewStatus.Prepare.name());
if (StringUtils.isBlank(testCase.getStatus())) {
testCase.setStatus(TestCaseReviewStatus.Prepare.name());
}
testCase.setOrder(Long.valueOf(testCases.size() - (num - beforeInsertId)) * ServiceUtils.ORDER_STEP);
testCase.setRefId(testCase.getId());
testCase.setVersionId(request.getVersionId());
@ -1414,24 +1379,18 @@ public class TestCaseService {
public void testCaseExport(HttpServletResponse response, TestCaseBatchRequest request) {
try {
// EasyExcelExporter easyExcelExporter = new EasyExcelExporter(new TestCaseExcelDataFactory().getExcelDataByLocal());
// List<TestCaseExcelData> datas = generateTestCaseExcel(request);
// easyExcelExporter.export(response,datas,Translator.get("test_case_import_template_name"), Translator.get("test_case_import_template_sheet"));
TestCaseExcelData testCaseExcelData = new TestCaseExcelDataFactory().getTestCaseExcelDataLocal();
List<TestCaseExcelData> datas = generateTestCaseExcel(request);
boolean importFileNeedNum = true;
TestCaseTemplateService testCaseTemplateService = CommonBeanFactory.getBean(TestCaseTemplateService.class);
TestCaseTemplateDao testCaseTemplate = testCaseTemplateService.getTemplate(request.getProjectId());
List<CustomFieldDao> customFields = Optional.ofNullable(testCaseTemplate.getCustomFields()).orElse(new ArrayList<>());
List<List<String>> headList = testCaseExcelData.getHead(importFileNeedNum, customFields);
List<List<Object>> testCaseDataByExcelList = this.generateTestCaseExcel(headList, datas);
List<List<String>> headList = testCaseExcelData.getHead(true, customFields);
List<List<Object>> testCaseDataByExcelList = this.generateTestCaseExcel(headList, generateTestCaseExcel(request));
EasyExcelExporter easyExcelExporter = new EasyExcelExporter(testCaseExcelData.getClass());
easyExcelExporter.exportByCustomWriteHandler(response, headList, testCaseDataByExcelList,
Translator.get("test_case_import_template_name"), Translator.get("test_case_import_template_sheet"));
} catch (Exception e) {
LogUtil.error(e.getMessage(), e);
MSException.throwException(e);
@ -1572,8 +1531,8 @@ public class TestCaseService {
List<TestCaseDTO> testCaseList = this.getExportData(request);
boolean isUseCustomId = projectService.useCustomNum(request.getProjectId());
List<TestCaseExcelData> list = new ArrayList<>();
StringBuilder step = new StringBuilder("");
StringBuilder result = new StringBuilder("");
StringBuilder step = new StringBuilder();
StringBuilder result = new StringBuilder();
Map<String, Map<String, String>> customSelectValueMap = new HashMap<>();
Map<String, String> customNameMap = new HashMap<>();
@ -1617,6 +1576,7 @@ public class TestCaseService {
testCaseList.forEach(t -> {
setExportSystemField(t, customNameMap, customSelectValueMap);
TestCaseExcelData data = new TestCaseExcelData();
data.setNum(t.getNum());
data.setName(t.getName());
@ -1723,6 +1683,21 @@ public class TestCaseService {
return list;
}
public void setExportSystemField(TestCaseDTO testCase, Map<String, String> customNameMap,
Map<String, Map<String, String>> customSelectValueMap) {
String statusKey = null;
for (String k : customNameMap.keySet()) {
String v = customNameMap.get(k);
if (StringUtils.equals(v, "用例状态")) {
statusKey = k;
}
}
if (StringUtils.isNotEmpty(statusKey)) {
Map<String, String> valueMap = customSelectValueMap.get(statusKey);
testCase.setStatus(valueMap.get(testCase.getStatus()));
}
}
/**
* 更新自定义字段
*

View File

@ -172,6 +172,7 @@ id_required=ID required
id_repeat_in_table=ID is repeat in table
step_model_tip=Step description fill in STEP, text description please fill in TEXT (not required)
case_status_not_exist=The use case status must be Prepare, Underway way and Completed
custom_field_option_not_exist=%s must be %s
issue_project_not_exist=ID does not exist or other errors
tapd_project_not_exist=The associated TAPD item ID does not exist
zentao_get_project_builds_fail=Get Affecting Version Errors

View File

@ -172,6 +172,7 @@ id_required=ID必填
id_repeat_in_table=表格内ID重复
step_model_tip=步骤描述填写 STEP,文本描述请填写 TEXT (非必填)
case_status_not_exist=用例状态必须为未开始(Prepare)、进行中(Underway)、已完成(Completed)
custom_field_option_not_exist=%s必须为%s
issue_project_not_exist=ID不存在或其它错误
tapd_project_not_exist=关联的TAPD项目ID不存在
zentao_get_project_builds_fail=获取影响版本错误

View File

@ -172,6 +172,7 @@ id_required=ID必填
id_repeat_in_table=表格內ID重復
step_model_tip=步驟描述填寫 STEP,文本描述請填寫 TEXT (非必填)
case_status_not_exist=用例狀態必須為未開始Prepare、進行中Underway、已完成Completed
custom_field_option_not_exist=%s必須為%s
issue_project_not_exist=ID不存在或其它錯誤
tapd_project_not_exist=關聯的TAPD項目ID不存在
zentao_get_project_builds_fail=獲取影響版本錯誤

View File

@ -698,8 +698,6 @@ export default {
return row.priority;
} else if (field.name === '责任人') {
return row.maintainerName;
} else if (field.name === '用例状态') {
return row.status;
}
return value ? value : defaultVal;
},

View File

@ -448,6 +448,17 @@ export function saveCustomTableWidth(key, fieldKey, colWith) {
localStorage.setItem(key + '_WITH', JSON.stringify(fields));
}
function parseStatus(row, options) {
if (options) {
for (let option of options) {
if (option.value === row.status) {
return option.system ? i18n.t(option.text) : option.text;
}
}
}
return row.status;
}
/**
* 获取列表的自定义字段的显示值
* @param row
@ -456,6 +467,9 @@ export function saveCustomTableWidth(key, fieldKey, colWith) {
* @returns {VueI18n.TranslateResult|*}
*/
export function getCustomFieldValue(row, field, members) {
if (field.name === '用例状态' && field.system) {
return parseStatus(row, field.options);
}
if (row.fields) {
for (let i = 0; i < row.fields.length; i++) {
let item = row.fields[i];