feat(组织管理): 当项目模板关联了多选自定义字段并设置默认值,创建项目报错

This commit is contained in:
AgAngle 2023-11-27 18:07:19 +08:00 committed by jianxing
parent 70a79726e4
commit 2dab26545d
4 changed files with 77 additions and 3 deletions

View File

@ -5,6 +5,7 @@ import io.metersphere.sdk.util.CommonBeanFactory;
import io.metersphere.system.domain.CustomFieldOption;
import io.metersphere.system.dto.CustomFieldDao;
import io.metersphere.system.service.BaseCustomFieldOptionService;
import org.apache.commons.lang3.StringUtils;
import java.util.List;
import java.util.Set;
@ -19,6 +20,9 @@ public class CustomFieldSelectResolver extends AbstractCustomFieldResolver {
return;
}
validateString(customField.getName(), value);
if (StringUtils.isBlank((String) value)) {
return;
}
List<CustomFieldOption> options = getOptions(customField.getId());
Set<String> values = options.stream().map(CustomFieldOption::getValue).collect(Collectors.toSet());
if (!values.contains(value)) {

View File

@ -4,16 +4,21 @@ import io.metersphere.project.domain.Project;
import io.metersphere.project.mapper.ProjectMapper;
import io.metersphere.sdk.constants.TemplateScene;
import io.metersphere.sdk.constants.TemplateScopeType;
import io.metersphere.sdk.util.LogUtils;
import io.metersphere.system.dto.sdk.request.TemplateCustomFieldRequest;
import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.system.domain.*;
import io.metersphere.system.dto.sdk.request.TemplateSystemCustomFieldRequest;
import io.metersphere.system.resolver.field.AbstractCustomFieldResolver;
import io.metersphere.system.resolver.field.CustomFieldResolverFactory;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.function.Function;
import java.util.stream.Collectors;
@Component
@ -98,15 +103,32 @@ public class CreateTemplateResourceService implements CreateProjectResourceServi
// 同步创建项目级别模板
List<Template> orgTemplates = baseTemplateService.getTemplates(organizationId, scene.name());
List<String> orgTemplateIds = orgTemplates.stream().map(Template::getId).toList();
Map<String, List<TemplateCustomField>> customFieldMap = baseTemplateCustomFieldService.getByTemplateIds(orgTemplateIds)
Map<String, List<TemplateCustomField>> templateCustomFieldMap = baseTemplateCustomFieldService.getByTemplateIds(orgTemplateIds)
.stream()
.collect(Collectors.groupingBy(item -> item.getTemplateId()));
Map<String, CustomField> customFieldMap = baseCustomFieldService.getByScopeIdAndScene(organizationId, scene.name()).stream()
.collect(Collectors.toMap(CustomField::getId, Function.identity()));
orgTemplates.forEach((template) -> {
List<TemplateCustomField> templateCustomFields = customFieldMap.get(template.getId());
List<TemplateCustomField> templateCustomFields = templateCustomFieldMap.get(template.getId());
templateCustomFields = templateCustomFields == null ? List.of() : templateCustomFields;
List<TemplateCustomFieldRequest> templateCustomFieldRequests = templateCustomFields.stream()
.map(templateCustomField -> BeanUtils.copyBean(new TemplateCustomFieldRequest(), templateCustomField))
.map(templateCustomField -> {
TemplateCustomFieldRequest templateCustomFieldRequest = BeanUtils.copyBean(new TemplateCustomFieldRequest(), templateCustomField);
CustomField customField = customFieldMap.get(templateCustomField.getFieldId());
templateCustomFieldRequest.setDefaultValue(null);
try {
if (StringUtils.isNotBlank(templateCustomField.getDefaultValue())) {
// 将字符串转成对应的对象方便调用统一的创建方法
AbstractCustomFieldResolver customFieldResolver = CustomFieldResolverFactory.getResolver(customField.getType());
templateCustomFieldRequest.setDefaultValue(customFieldResolver.parse2Value(templateCustomField.getDefaultValue()));
}
} catch (Exception e) {
LogUtils.error(e);
}
return templateCustomFieldRequest;
})
.toList();
addRefProjectTemplate(projectId, template, templateCustomFieldRequests, null);
});

View File

@ -74,6 +74,7 @@ public class BaseCustomFieldTestService {
emptyValueMap.put(CustomFieldType.MULTIPLE_MEMBER, List.of());
emptyValueMap.put(CustomFieldType.INT, null);
emptyValueMap.put(CustomFieldType.FLOAT, null);
emptyValueMap.put(CustomFieldType.SELECT, StringUtils.EMPTY);
assertValidateError(emptyValueMap);
}
@ -100,6 +101,10 @@ public class BaseCustomFieldTestService {
errorValueMap.put(CustomFieldType.INT, "dd");
errorValueMap.put(CustomFieldType.FLOAT, "dd");
assertValidateError(errorValueMap);
errorValueMap.clear();
errorValueMap.put(CustomFieldType.MULTIPLE_SELECT, "1");
errorValueMap.put(CustomFieldType.SELECT, "1");
}
public void testResolverParse() {

View File

@ -17,6 +17,9 @@ import io.metersphere.system.dto.request.DefaultFunctionalCustomField;
import io.metersphere.system.dto.request.ProjectAddMemberRequest;
import io.metersphere.system.dto.request.ProjectMemberRequest;
import io.metersphere.system.dto.request.ProjectRequest;
import io.metersphere.system.dto.sdk.request.CustomFieldOptionRequest;
import io.metersphere.system.dto.sdk.request.TemplateCustomFieldRequest;
import io.metersphere.system.dto.sdk.request.TemplateUpdateRequest;
import io.metersphere.system.dto.user.UserExtendDTO;
import io.metersphere.system.invoker.ProjectServiceInvoker;
import io.metersphere.system.job.CleanProjectJob;
@ -90,6 +93,10 @@ public class SystemProjectControllerTests extends BaseTest {
@Resource
private BaseCustomFieldService baseCustomFieldService;
@Resource
private OrganizationCustomFieldService organizationCustomFieldService;
@Resource
private OrganizationTemplateService organizationTemplateService;
@Resource
private BaseStatusItemService baseStatusItemService;
@Resource
private BaseStatusDefinitionService baseStatusDefinitionService;
@ -303,6 +310,7 @@ public class SystemProjectControllerTests extends BaseTest {
executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
public void testAddProjectSuccess() throws Exception {
initData();
addMultipleCustomField();
AddProjectRequest project = this.generatorAdd(DEFAULT_ORGANIZATION_ID,"name", "description", true, List.of("admin"));
MvcResult mvcResult = this.responsePost(addProject, project);
ProjectDTO result = parseObjectFromMvcResult(mvcResult, ProjectDTO.class);
@ -524,6 +532,41 @@ public class SystemProjectControllerTests extends BaseTest {
}
}
/**
* 添加多选的自定义字段
* 校验初始化项目模板时数组类型的默认值是否正常转化
*/
private void addMultipleCustomField() {
// 新建一个多选的自定义字段
String scene = TemplateScene.TEST_PLAN.name();
CustomField customField = new CustomField();
customField.setScene(scene);
customField.setName("test project default value");
customField.setType(CustomFieldType.MULTIPLE_SELECT.name());
customField.setScopeId(DEFAULT_ORGANIZATION_ID);
customField.setEnableOptionKey(true);
CustomFieldOptionRequest customFieldOptionRequest1 = new CustomFieldOptionRequest();
customFieldOptionRequest1.setValue("1");
customFieldOptionRequest1.setText("test1");
CustomFieldOptionRequest customFieldOptionRequest2 = new CustomFieldOptionRequest();
customFieldOptionRequest2.setValue("2");
customFieldOptionRequest2.setText("test2");
customField.setCreateUser("admin");
List<CustomFieldOptionRequest> optionRequests = Arrays.asList(customFieldOptionRequest1, customFieldOptionRequest2);
organizationCustomFieldService.add(customField, optionRequests);
// 关联模板
TemplateUpdateRequest request = new TemplateUpdateRequest();
Template template = baseTemplateService.getTemplates(DEFAULT_ORGANIZATION_ID, scene).get(0);
TemplateCustomFieldRequest templateCustomFieldRequest = new TemplateCustomFieldRequest();
templateCustomFieldRequest.setFieldId(customField.getId());
templateCustomFieldRequest.setRequired(false);
templateCustomFieldRequest.setDefaultValue(List.of("1", "2"));
request.setId(template.getId());
request.setCustomFields(List.of(templateCustomFieldRequest));
organizationTemplateService.update(request);
}
private void changeOrgTemplateEnable(boolean enable) {
changeOrgTemplateEnable(enable, OrganizationParameterConstants.ORGANIZATION_FUNCTIONAL_TEMPLATE_ENABLE_KEY);
changeOrgTemplateEnable(enable, OrganizationParameterConstants.ORGANIZATION_BUG_TEMPLATE_ENABLE_KEY);