fix(测试跟踪): 功能用例导出,多选框导出的是ID

--bug=1016070 --user=陈建星 【测试跟踪】用例模版设置自定义字段,类型是多选框,导出后,该字段内容是id https://www.tapd.cn/55049933/s/1230151
This commit is contained in:
chenjianxing 2022-08-22 18:29:24 +08:00 committed by jianxing
parent e3b1d59b61
commit a3f49e4ca2
2 changed files with 80 additions and 43 deletions

View File

@ -1,25 +1,42 @@
package io.metersphere.commons.constants;
import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;
public enum CustomFieldType {
INPUT("input"),
TEXTAREA("textarea"),
SELECT("select"),
MULTIPLE_SELECT("multipleSelect"),
RADIO("radio"),
CHECKBOX("checkbox"),
MEMBER("member"),
MULTIPLE_MEMBER("multipleMember"),
DATE("date"),
DATETIME("datetime"),
INT("int"),
FLOAT("float"),
MULTIPLE_INPUT("multipleInput"),
RICH_TEXT("richText");
INPUT("input", false),
TEXTAREA("textarea", false),
SELECT("select", true),
MULTIPLE_SELECT("multipleSelect", true),
RADIO("radio", true),
CHECKBOX("checkbox", true),
MEMBER("member", true),
MULTIPLE_MEMBER("multipleMember", true),
DATE("date", false),
DATETIME("datetime", false),
INT("int", false),
FLOAT("float", false),
MULTIPLE_INPUT("multipleInput", false),
RICH_TEXT("richText", false);
String value;
private String value;
private Boolean hasOption;
CustomFieldType(String value) {
CustomFieldType(String value, Boolean hasOption) {
this.value = value;
this.hasOption = hasOption;
}
public Boolean getHasOption() {
return this.hasOption;
}
public static Set<String> getHasOptionValueSet() {
return Arrays.stream(CustomFieldType.values())
.filter(CustomFieldType::getHasOption)
.map(CustomFieldType::getValue)
.collect(Collectors.toSet());
}
public String getValue() {

View File

@ -1561,7 +1561,8 @@ public class TestCaseService {
customFieldList = testCaseTemplate.getCustomFields();
}
buildExportCustomFieldMap(customSelectValueMap, customNameMap, customFieldList);
Set<String> textFields = new HashSet<>();
buildExportCustomFieldMap(customSelectValueMap, customNameMap, customFieldList, textFields);
for (int rowIndex = 0; rowIndex < testCaseList.size(); rowIndex++) {
TestCaseDTO t = testCaseList.get(rowIndex);
@ -1573,7 +1574,7 @@ public class TestCaseService {
BeanUtils.copyBean(data, t);
buildExportCustomNum(isUseCustomId, t, data);
buildExportStep(t, stepDescList, stepResultList, data);
buildExportCustomField(customSelectValueMap, customNameMap, t, data);
buildExportCustomField(customSelectValueMap, customNameMap, t, data, textFields);
buildExportOtherField(data, t, otherHeaders);
if (CollectionUtils.isNotEmpty(stepDescList)) {
@ -1641,7 +1642,8 @@ public class TestCaseService {
}
}
private void buildExportCustomField(Map<String, Map<String, String>> customSelectValueMap, Map<String, String> customNameMap, TestCaseDTO t, TestCaseExcelData data) {
private void buildExportCustomField(Map<String, Map<String, String>> customSelectValueMap,
Map<String, String> customNameMap, TestCaseDTO t, TestCaseExcelData data, Set<String> textFields) {
try {
List<CustomFieldResource> fields = customFieldTestCaseService.getByResourceId(t.getId());
Map<String, String> map = new HashMap<>();
@ -1649,13 +1651,28 @@ public class TestCaseService {
CustomFieldResource field = fields.get(index);
//进行key value对换
String id = field.getFieldId();
if (textFields.contains(id)) {
map.put(customNameMap.get(id), field.getTextValue());
continue;
}
if (StringUtils.isNotBlank(field.getValue())) {
String value = JSONObject.parse(field.getValue()).toString();
if (customSelectValueMap.containsKey(id)
&& customSelectValueMap.get(id).containsKey(value)) {
value = customSelectValueMap.get(id).get(value);
Object value = JSONObject.parse(field.getValue());
Map<String, String> optionMap = customSelectValueMap.get(id);
if (value instanceof String) {
if (MapUtils.isNotEmpty(optionMap) && optionMap.containsKey(value)) {
value = optionMap.get(value);
}
map.put(customNameMap.get(id), value.toString());
} else if (value instanceof JSONArray) {
List<String> results = new ArrayList<>();
JSONArray values = (JSONArray) value;
values.forEach(item -> {
if (MapUtils.isNotEmpty(optionMap) && optionMap.containsKey(item.toString())) {
results.add(optionMap.get(item.toString()));
}
});
map.put(customNameMap.get(id), results.toString());
}
map.put(customNameMap.get(id), value);
}
}
data.setCustomData(map);
@ -1698,32 +1715,35 @@ public class TestCaseService {
}
}
private void buildExportCustomFieldMap(Map<String, Map<String, String>> customSelectValueMap, Map<String, String> customNameMap, List<CustomFieldDao> customFieldList) {
private void buildExportCustomFieldMap(Map<String, Map<String, String>> customSelectValueMap, Map<String, String> customNameMap,
List<CustomFieldDao> customFieldList, Set<String> textFields) {
for (CustomFieldDao dto : customFieldList) {
Map<String, String> map = new HashMap<>();
if (StringUtils.equals("select", dto.getType())) {
if (CustomFieldType.getHasOptionValueSet().contains(dto.getType())) {
try {
JSONArray optionsArr = JSONArray.parseArray(dto.getOptions());
for (int i = 0; i < optionsArr.size(); i++) {
JSONObject obj = optionsArr.getJSONObject(i);
if (obj.containsKey("text") && obj.containsKey("value")) {
String value = obj.getString("value");
String text = obj.getString("text");
if (StringUtils.equals(text, "test_track.case.status_finished")) {
text = Translator.get("test_case_status_finished");
} else if (StringUtils.equals(text, "test_track.case.status_prepare")) {
text = Translator.get("test_case_status_prepare");
} else if (StringUtils.equals(text, "test_track.case.status_running")) {
text = Translator.get("test_case_status_running");
}
if (StringUtils.isNotEmpty(value)) {
map.put(value, text);
}
List<CustomFieldOption> options = JSONArray.parseArray(dto.getOptions(), CustomFieldOption.class);
options.forEach(option -> {
String text = option.getText();
String value = option.getValue();
if (StringUtils.equals(text, "test_track.case.status_finished")) {
text = Translator.get("test_case_status_finished");
} else if (StringUtils.equals(text, "test_track.case.status_prepare")) {
text = Translator.get("test_case_status_prepare");
} else if (StringUtils.equals(text, "test_track.case.status_running")) {
text = Translator.get("test_case_status_running");
}
}
if (StringUtils.isNotEmpty(value)) {
map.put(value, text);
}
});
} catch (Exception e) {
LogUtil.error(e);
}
}
if (StringUtils.equalsAny(dto.getType(), CustomFieldType.TEXTAREA.getValue(), CustomFieldType.RICH_TEXT.getValue())) {
textFields.add(dto.getId());
}
customSelectValueMap.put(dto.getId(), map);
customNameMap.put(dto.getId(), dto.getName());
}