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; package io.metersphere.commons.constants;
import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;
public enum CustomFieldType { public enum CustomFieldType {
INPUT("input"), INPUT("input", false),
TEXTAREA("textarea"), TEXTAREA("textarea", false),
SELECT("select"), SELECT("select", true),
MULTIPLE_SELECT("multipleSelect"), MULTIPLE_SELECT("multipleSelect", true),
RADIO("radio"), RADIO("radio", true),
CHECKBOX("checkbox"), CHECKBOX("checkbox", true),
MEMBER("member"), MEMBER("member", true),
MULTIPLE_MEMBER("multipleMember"), MULTIPLE_MEMBER("multipleMember", true),
DATE("date"), DATE("date", false),
DATETIME("datetime"), DATETIME("datetime", false),
INT("int"), INT("int", false),
FLOAT("float"), FLOAT("float", false),
MULTIPLE_INPUT("multipleInput"), MULTIPLE_INPUT("multipleInput", false),
RICH_TEXT("richText"); RICH_TEXT("richText", false);
String value; private String value;
private Boolean hasOption;
CustomFieldType(String value) { CustomFieldType(String value, Boolean hasOption) {
this.value = value; 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() { public String getValue() {

View File

@ -1561,7 +1561,8 @@ public class TestCaseService {
customFieldList = testCaseTemplate.getCustomFields(); 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++) { for (int rowIndex = 0; rowIndex < testCaseList.size(); rowIndex++) {
TestCaseDTO t = testCaseList.get(rowIndex); TestCaseDTO t = testCaseList.get(rowIndex);
@ -1573,7 +1574,7 @@ public class TestCaseService {
BeanUtils.copyBean(data, t); BeanUtils.copyBean(data, t);
buildExportCustomNum(isUseCustomId, t, data); buildExportCustomNum(isUseCustomId, t, data);
buildExportStep(t, stepDescList, stepResultList, data); buildExportStep(t, stepDescList, stepResultList, data);
buildExportCustomField(customSelectValueMap, customNameMap, t, data); buildExportCustomField(customSelectValueMap, customNameMap, t, data, textFields);
buildExportOtherField(data, t, otherHeaders); buildExportOtherField(data, t, otherHeaders);
if (CollectionUtils.isNotEmpty(stepDescList)) { 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 { try {
List<CustomFieldResource> fields = customFieldTestCaseService.getByResourceId(t.getId()); List<CustomFieldResource> fields = customFieldTestCaseService.getByResourceId(t.getId());
Map<String, String> map = new HashMap<>(); Map<String, String> map = new HashMap<>();
@ -1649,13 +1651,28 @@ public class TestCaseService {
CustomFieldResource field = fields.get(index); CustomFieldResource field = fields.get(index);
//进行key value对换 //进行key value对换
String id = field.getFieldId(); String id = field.getFieldId();
if (StringUtils.isNotBlank(field.getValue())) { if (textFields.contains(id)) {
String value = JSONObject.parse(field.getValue()).toString(); map.put(customNameMap.get(id), field.getTextValue());
if (customSelectValueMap.containsKey(id) continue;
&& customSelectValueMap.get(id).containsKey(value)) { }
value = customSelectValueMap.get(id).get(value); if (StringUtils.isNotBlank(field.getValue())) {
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); data.setCustomData(map);
@ -1698,17 +1715,16 @@ 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) { for (CustomFieldDao dto : customFieldList) {
Map<String, String> map = new HashMap<>(); Map<String, String> map = new HashMap<>();
if (StringUtils.equals("select", dto.getType())) { if (CustomFieldType.getHasOptionValueSet().contains(dto.getType())) {
try { try {
JSONArray optionsArr = JSONArray.parseArray(dto.getOptions()); List<CustomFieldOption> options = JSONArray.parseArray(dto.getOptions(), CustomFieldOption.class);
for (int i = 0; i < optionsArr.size(); i++) { options.forEach(option -> {
JSONObject obj = optionsArr.getJSONObject(i); String text = option.getText();
if (obj.containsKey("text") && obj.containsKey("value")) { String value = option.getValue();
String value = obj.getString("value");
String text = obj.getString("text");
if (StringUtils.equals(text, "test_track.case.status_finished")) { if (StringUtils.equals(text, "test_track.case.status_finished")) {
text = Translator.get("test_case_status_finished"); text = Translator.get("test_case_status_finished");
} else if (StringUtils.equals(text, "test_track.case.status_prepare")) { } else if (StringUtils.equals(text, "test_track.case.status_prepare")) {
@ -1719,11 +1735,15 @@ public class TestCaseService {
if (StringUtils.isNotEmpty(value)) { if (StringUtils.isNotEmpty(value)) {
map.put(value, text); map.put(value, text);
} }
} });
}
} catch (Exception e) { } 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); customSelectValueMap.put(dto.getId(), map);
customNameMap.put(dto.getId(), dto.getName()); customNameMap.put(dto.getId(), dto.getName());
} }