fix(测试跟踪): 功能用例导入多选框自定义字段失败

This commit is contained in:
AnAngle 2022-08-23 10:24:45 +08:00 committed by jianxing
parent b18598f24e
commit 363d714938
9 changed files with 57 additions and 31 deletions

View File

@ -48,7 +48,7 @@ public class TestCaseExcelData {
@ExcelIgnore @ExcelIgnore
private String priority; private String priority;
@ExcelIgnore @ExcelIgnore
Map<String, String> customData = new LinkedHashMap<>(); Map<String, Object> customData = new LinkedHashMap<>();
@ExcelIgnore @ExcelIgnore
List<String> mergeStepDesc; List<String> mergeStepDesc;

View File

@ -388,29 +388,29 @@ public class TestCaseNoModelDataListener extends AnalysisEventListener<Map<Integ
* @param stringBuilder * @param stringBuilder
*/ */
private void validateCustomField(TestCaseExcelData data, StringBuilder stringBuilder) { private void validateCustomField(TestCaseExcelData data, StringBuilder stringBuilder) {
Map<String, String> customData = data.getCustomData(); Map<String, Object> customData = data.getCustomData();
for (String fieldName : customData.keySet()) { for (String fieldName : customData.keySet()) {
String value = customData.get(fieldName); Object value = customData.get(fieldName);
CustomFieldDao customField = customFieldsMap.get(fieldName); CustomFieldDao customField = customFieldsMap.get(fieldName);
if (customField == null) { if (customField == null) {
continue; continue;
} }
AbstractCustomFieldValidator customFieldValidator = customFieldValidatorMap.get(customField.getType()); AbstractCustomFieldValidator customFieldValidator = customFieldValidatorMap.get(customField.getType());
try { try {
customFieldValidator.validate(customField, value); customFieldValidator.validate(customField, value.toString());
if (customFieldValidator.isKVOption) {
// 这里如果填的是选项值替换成选项ID保存
customData.put(fieldName, customFieldValidator.parse2Key(value.toString(), customField));
}
} catch (CustomFieldValidateException e) { } catch (CustomFieldValidateException e) {
stringBuilder.append(e.getMessage().concat(ERROR_MSG_SEPARATOR)); stringBuilder.append(e.getMessage().concat(ERROR_MSG_SEPARATOR));
} }
if (customFieldValidator.isKVOption) {
// 这里如果填的是选项值替换成选项ID保存
customData.put(fieldName, customFieldValidator.parse2Key(value, customField));
}
if (StringUtils.equals(fieldName, TestCaseImportFiled.STATUS.getFiledLangMap().get(Locale.SIMPLIFIED_CHINESE))) { if (StringUtils.equals(fieldName, TestCaseImportFiled.STATUS.getFiledLangMap().get(Locale.SIMPLIFIED_CHINESE))) {
data.setStatus(customData.get(fieldName)); data.setStatus(customData.get(fieldName).toString());
} else if (StringUtils.equals(fieldName, TestCaseImportFiled.PRIORITY.getFiledLangMap().get(Locale.SIMPLIFIED_CHINESE))) { } else if (StringUtils.equals(fieldName, TestCaseImportFiled.PRIORITY.getFiledLangMap().get(Locale.SIMPLIFIED_CHINESE))) {
data.setPriority(customData.get(fieldName)); data.setPriority(customData.get(fieldName).toString());
} else if (StringUtils.equals(fieldName, TestCaseImportFiled.MAINTAINER.getFiledLangMap().get(Locale.SIMPLIFIED_CHINESE))) { } else if (StringUtils.equals(fieldName, TestCaseImportFiled.MAINTAINER.getFiledLangMap().get(Locale.SIMPLIFIED_CHINESE))) {
data.setMaintainer(customData.get(fieldName)); data.setMaintainer(customData.get(fieldName).toString());
} }
} }
} }
@ -545,10 +545,11 @@ public class TestCaseNoModelDataListener extends AnalysisEventListener<Map<Integ
* @param testCase * @param testCase
*/ */
private void buildTestCaseCustomFieldMap(TestCaseExcelData data, TestCaseWithBLOBs testCase) { private void buildTestCaseCustomFieldMap(TestCaseExcelData data, TestCaseWithBLOBs testCase) {
Map<String, String> customData = data.getCustomData(); Map<String, Object> customData = data.getCustomData();
List<CustomFieldResource> testCaseCustomFields = new ArrayList<>(); List<CustomFieldResource> testCaseCustomFields = new ArrayList<>();
customData.forEach((k, v) -> { customData.forEach((k, v) -> {
if (StringUtils.isNotBlank(v)) { if ((v instanceof List && CollectionUtils.isNotEmpty((List)v))
|| StringUtils.isNotBlank(v.toString())) {
CustomFieldDao customFieldDao = customFieldsMap.get(k); CustomFieldDao customFieldDao = customFieldsMap.get(k);
if (customFieldDao != null) { if (customFieldDao != null) {
CustomFieldResource customFieldResource = new CustomFieldResource(); CustomFieldResource customFieldResource = new CustomFieldResource();

View File

@ -1494,7 +1494,7 @@ public class TestCaseService {
for (TestCaseExcelData model : data) { for (TestCaseExcelData model : data) {
List<Object> fields = new ArrayList<>(); List<Object> fields = new ArrayList<>();
Map<String, String> customDataMaps = Optional.ofNullable(model.getCustomData()) Map<String, Object> customDataMaps = Optional.ofNullable(model.getCustomData())
.orElse(new HashMap<>()); .orElse(new HashMap<>());
Map<String, String> otherFieldMaps = Optional.ofNullable(model.getOtherFields()) Map<String, String> otherFieldMaps = Optional.ofNullable(model.getOtherFields())
.orElse(new HashMap<>()); .orElse(new HashMap<>());
@ -1507,7 +1507,7 @@ public class TestCaseService {
} }
} }
if (!isSystemField) { if (!isSystemField) {
String value = customDataMaps.get(head); Object value = customDataMaps.get(head);
if (value == null) { if (value == null) {
value = otherFieldMaps.get(head); value = otherFieldMaps.get(head);
} }
@ -1646,7 +1646,7 @@ public class TestCaseService {
Map<String, String> customNameMap, TestCaseDTO t, TestCaseExcelData data, Set<String> textFields) { 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, Object> map = new HashMap<>();
for (int index = 0; index < fields.size(); index++) { for (int index = 0; index < fields.size(); index++) {
CustomFieldResource field = fields.get(index); CustomFieldResource field = fields.get(index);
//进行key value对换 //进行key value对换

View File

@ -2,6 +2,7 @@ package io.metersphere.track.validate;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import io.metersphere.commons.exception.CustomFieldValidateException; import io.metersphere.commons.exception.CustomFieldValidateException;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.dto.CustomFieldDao; import io.metersphere.dto.CustomFieldDao;
import io.metersphere.i18n.Translator; import io.metersphere.i18n.Translator;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@ -19,6 +20,7 @@ public abstract class AbstractCustomFieldValidator {
/** /**
* 校验参数是否合法 * 校验参数是否合法
*
* @param customField * @param customField
* @param value * @param value
*/ */
@ -26,10 +28,11 @@ public abstract class AbstractCustomFieldValidator {
/** /**
* 将选项的值转化为对应的key * 将选项的值转化为对应的key
*
* @param keyOrValue * @param keyOrValue
* @return * @return
*/ */
public String parse2Key(String keyOrValue, CustomFieldDao customField) { public Object parse2Key(String keyOrValue, CustomFieldDao customField) {
return keyOrValue; return keyOrValue;
} }
@ -39,12 +42,35 @@ public abstract class AbstractCustomFieldValidator {
} }
} }
protected void validateArrayRequired(CustomFieldDao customField, String value) throws CustomFieldValidateException {
if (customField.getRequired() && (StringUtils.isBlank(value) || StringUtils.equals(value, "[]"))) {
CustomFieldValidateException.throwException(String.format(Translator.get("custom_field_required_tip"), customField.getName()));
}
}
protected List<String> parse2Array(String name, String value) throws CustomFieldValidateException { protected List<String> parse2Array(String name, String value) throws CustomFieldValidateException {
try { try {
// [a, b] => ["a","b"]
if (!StringUtils.equals(value, "[]")) {
value = value.replace("[", "[\"")
.replace("]", "\"]")
.replace(",", "\",\"")
.replace("", "\"\"")
.replace(" ", "");
}
return JSONArray.parseArray(value, String.class); return JSONArray.parseArray(value, String.class);
} catch (Exception e) { } catch (Exception e) {
CustomFieldValidateException.throwException(String.format(Translator.get("custom_field_required_tip"), name)); CustomFieldValidateException.throwException(String.format(Translator.get("custom_field_required_tip"), name));
} }
return new ArrayList<>(); return new ArrayList<>();
} }
protected List<String> parse2Array(String value) {
try {
return parse2Array(null, value);
} catch (CustomFieldValidateException e) {
LogUtil.error(e);
}
return new ArrayList<>();
}
} }

View File

@ -40,7 +40,7 @@ public class CustomFieldMemberValidator extends AbstractCustomFieldValidator {
} }
@Override @Override
public String parse2Key(String keyOrValue, CustomFieldDao customField) { public Object parse2Key(String keyOrValue, CustomFieldDao customField) {
if (userNameMap.containsKey(keyOrValue)) { if (userNameMap.containsKey(keyOrValue)) {
return userNameMap.get(keyOrValue); return userNameMap.get(keyOrValue);
} }

View File

@ -12,7 +12,7 @@ public class CustomFieldMultipleMemberValidator extends CustomFieldMemberValidat
@Override @Override
public void validate(CustomFieldDao customField, String value) throws CustomFieldValidateException { public void validate(CustomFieldDao customField, String value) throws CustomFieldValidateException {
validateRequired(customField, value); validateArrayRequired(customField, value);
if (StringUtils.isBlank(value)) { if (StringUtils.isBlank(value)) {
return; return;
} }
@ -27,11 +27,11 @@ public class CustomFieldMultipleMemberValidator extends CustomFieldMemberValidat
} }
@Override @Override
public String parse2Key(String keyOrValuesStr, CustomFieldDao customField) { public Object parse2Key(String keyOrValuesStr, CustomFieldDao customField) {
if (StringUtils.isBlank(keyOrValuesStr)) { if (StringUtils.isBlank(keyOrValuesStr)) {
return ""; return "";
} }
List<String> keyOrValues = JSONArray.parseArray(keyOrValuesStr, String.class); List<String> keyOrValues = parse2Array(keyOrValuesStr);
for (int i = 0; i < keyOrValues.size(); i++) { for (int i = 0; i < keyOrValues.size(); i++) {
String item = keyOrValues.get(i); String item = keyOrValues.get(i);
@ -39,6 +39,6 @@ public class CustomFieldMultipleMemberValidator extends CustomFieldMemberValidat
keyOrValues.set(i, userNameMap.get(item)); keyOrValues.set(i, userNameMap.get(item));
} }
} }
return JSONArray.toJSONString(keyOrValues); return keyOrValues;
} }
} }

View File

@ -14,7 +14,7 @@ public class CustomFieldMultipleSelectValidator extends CustomFieldSelectValidat
@Override @Override
public void validate(CustomFieldDao customField, String value) throws CustomFieldValidateException { public void validate(CustomFieldDao customField, String value) throws CustomFieldValidateException {
validateRequired(customField, value); validateArrayRequired(customField, value);
if (StringUtils.isBlank(value)) { if (StringUtils.isBlank(value)) {
return; return;
} }
@ -29,11 +29,11 @@ public class CustomFieldMultipleSelectValidator extends CustomFieldSelectValidat
} }
@Override @Override
public String parse2Key(String keyOrValuesStr, CustomFieldDao customField) { public Object parse2Key(String keyOrValuesStr, CustomFieldDao customField) {
if (StringUtils.isBlank(keyOrValuesStr)) { if (StringUtils.isBlank(keyOrValuesStr)) {
return ""; return "";
} }
List<String> keyOrValues = JSONArray.parseArray(keyOrValuesStr, String.class); List<String> keyOrValues = parse2Array(keyOrValuesStr);
Map<String, String> nameMap = optionTextMapCache.get(customField.getId()); Map<String, String> nameMap = optionTextMapCache.get(customField.getId());
for (int i = 0; i < keyOrValues.size(); i++) { for (int i = 0; i < keyOrValues.size(); i++) {
String item = keyOrValues.get(i); String item = keyOrValues.get(i);
@ -41,6 +41,6 @@ public class CustomFieldMultipleSelectValidator extends CustomFieldSelectValidat
keyOrValues.set(i, nameMap.get(item)); keyOrValues.set(i, nameMap.get(item));
} }
} }
return JSONArray.toJSONString(keyOrValues); return keyOrValues;
} }
} }

View File

@ -54,7 +54,7 @@ public class CustomFieldSelectValidator extends AbstractCustomFieldValidator {
} }
@Override @Override
public String parse2Key(String keyOrValuesStr, CustomFieldDao customField) { public Object parse2Key(String keyOrValuesStr, CustomFieldDao customField) {
Map<String, String> textMap = optionTextMapCache.get(customField.getId()); Map<String, String> textMap = optionTextMapCache.get(customField.getId());
if (MapUtils.isNotEmpty(textMap) && textMap.containsKey(keyOrValuesStr)) { if (MapUtils.isNotEmpty(textMap) && textMap.containsKey(keyOrValuesStr)) {
return textMap.get(keyOrValuesStr); return textMap.get(keyOrValuesStr);

View File

@ -718,10 +718,9 @@ export default {
}, },
getCustomFieldFilter(field) { getCustomFieldFilter(field) {
if (field.name === '用例状态') { if (field.name === '用例状态') {
let option = []; let option = null;
if (this.trashEnable) { if (!this.trashEnable) {
option.push({text: this.$t('test_track.plan.plan_status_trash'), value: 'Trash'}); option = [];
} else {
field.options.forEach((item) => { field.options.forEach((item) => {
option.push({ option.push({
text: this.$t(item.text), text: this.$t(item.text),