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
private String priority;
@ExcelIgnore
Map<String, String> customData = new LinkedHashMap<>();
Map<String, Object> customData = new LinkedHashMap<>();
@ExcelIgnore
List<String> mergeStepDesc;

View File

@ -388,29 +388,29 @@ public class TestCaseNoModelDataListener extends AnalysisEventListener<Map<Integ
* @param 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()) {
String value = customData.get(fieldName);
Object value = customData.get(fieldName);
CustomFieldDao customField = customFieldsMap.get(fieldName);
if (customField == null) {
continue;
}
AbstractCustomFieldValidator customFieldValidator = customFieldValidatorMap.get(customField.getType());
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) {
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))) {
data.setStatus(customData.get(fieldName));
data.setStatus(customData.get(fieldName).toString());
} 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))) {
data.setMaintainer(customData.get(fieldName));
data.setMaintainer(customData.get(fieldName).toString());
}
}
}
@ -545,10 +545,11 @@ public class TestCaseNoModelDataListener extends AnalysisEventListener<Map<Integ
* @param 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<>();
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);
if (customFieldDao != null) {
CustomFieldResource customFieldResource = new CustomFieldResource();

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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