fix(测试用例): 修复异步导致国际化&session问题

This commit is contained in:
WangXu10 2024-08-15 14:14:49 +08:00 committed by 刘瑞斌
parent 2b1e078f49
commit 1ec24a72ac
5 changed files with 49 additions and 15 deletions

View File

@ -3,6 +3,7 @@ package io.metersphere.functional.excel.converter;
import io.metersphere.functional.excel.constants.FunctionalCaseExportOtherField;
import io.metersphere.sdk.util.LogUtils;
import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -12,14 +13,28 @@ import java.util.Map;
*/
public class FunctionalCaseExportConverterFactory {
public static Map<String, FunctionalCaseExportConverter> getConverters(List<String> keys) {
public static Map<String, FunctionalCaseExportConverter> getConverters(List<String> keys, String projectId) {
Map<String, FunctionalCaseExportConverter> converterMapResult = new HashMap<>();
try {
HashMap<String, Class<? extends FunctionalCaseExportConverter>> converterMap = getConverterMap();
HashMap<String, Class<? extends FunctionalCaseExportConverter>> converterMap = getConverterMap(projectId);
for (String key : keys) {
Class<? extends FunctionalCaseExportConverter> clazz = converterMap.get(key);
if (clazz != null) {
converterMapResult.put(key, clazz.getDeclaredConstructor().newInstance());
// 获取所有构造函数
Constructor<?>[] constructors = clazz.getConstructors();
// 遍历构造函数选择适当的构造函数进行实例化
for (Constructor<?> constructor : constructors) {
Class<?>[] parameterTypes = constructor.getParameterTypes();
if (parameterTypes.length == 0) {
// 如果是无参构造直接调用 newInstance
converterMapResult.put(key, clazz.getDeclaredConstructor().newInstance());
break;
} else {
// 如果是有参构造可以传递参数
converterMapResult.put(key, clazz.getDeclaredConstructor(String.class).newInstance(projectId));
break;
}
}
}
}
} catch (Exception e) {
@ -28,11 +43,11 @@ public class FunctionalCaseExportConverterFactory {
return converterMapResult;
}
public static FunctionalCaseExportConverter getConverter(String key) {
public static FunctionalCaseExportConverter getConverter(String key, String projectId) {
try {
Class<? extends FunctionalCaseExportConverter> clazz = getConverterMap().get(key);
Class<? extends FunctionalCaseExportConverter> clazz = getConverterMap(projectId).get(key);
if (clazz != null) {
return clazz.getDeclaredConstructor().newInstance();
return clazz.getDeclaredConstructor(String.class).newInstance(projectId);
}
} catch (Exception e) {
LogUtils.error(e);
@ -40,7 +55,7 @@ public class FunctionalCaseExportConverterFactory {
return null;
}
private static HashMap<String, Class<? extends FunctionalCaseExportConverter>> getConverterMap() {
private static HashMap<String, Class<? extends FunctionalCaseExportConverter>> getConverterMap(String projectId) {
return new HashMap<>() {{
put(FunctionalCaseExportOtherField.CREATE_USER.getValue(), FunctionalCaseExportCreateUserConverter.class);
put(FunctionalCaseExportOtherField.CREATE_TIME.getValue(), FunctionalCaseExportCreateTimeConverter.class);

View File

@ -7,7 +7,6 @@ import io.metersphere.plan.domain.TestPlanCaseExecuteHistory;
import io.metersphere.project.service.ProjectApplicationService;
import io.metersphere.sdk.util.CommonBeanFactory;
import io.metersphere.system.domain.User;
import io.metersphere.system.utils.SessionUtils;
import java.util.HashMap;
import java.util.List;
@ -20,9 +19,9 @@ public class FunctionalCaseExportCreateUserConverter implements FunctionalCaseEx
public Map<String, String> userMap = new HashMap<>();
public FunctionalCaseExportCreateUserConverter() {
public FunctionalCaseExportCreateUserConverter(String projectId) {
ProjectApplicationService projectApplicationService = CommonBeanFactory.getBean(ProjectApplicationService.class);
List<User> memberOption = projectApplicationService.getProjectUserList(SessionUtils.getCurrentProjectId());
List<User> memberOption = projectApplicationService.getProjectUserList(projectId);
memberOption.forEach(option -> userMap.put(option.getId(), option.getName()));
}

View File

@ -14,6 +14,10 @@ import java.util.Map;
public class FunctionalCaseExportUpdateUserConverter extends FunctionalCaseExportCreateUserConverter {
public FunctionalCaseExportUpdateUserConverter(String projectId) {
super(projectId);
}
@Override
public String parse(FunctionalCase functionalCase, Map<String, List<FunctionalCaseComment>> caseCommentMap, Map<String, List<TestPlanCaseExecuteHistory>> executeCommentMap, Map<String, List<CaseReviewHistory>> reviewCommentMap) {
return getFromMapOfNullable(userMap, functionalCase.getUpdateUser());

View File

@ -42,6 +42,7 @@ import io.metersphere.sdk.util.*;
import io.metersphere.system.constants.ExportConstants;
import io.metersphere.system.domain.CustomFieldOption;
import io.metersphere.system.domain.SystemParameter;
import io.metersphere.system.domain.User;
import io.metersphere.system.dto.sdk.BaseTreeNode;
import io.metersphere.system.dto.sdk.SessionUser;
import io.metersphere.system.dto.sdk.TemplateCustomFieldDTO;
@ -52,7 +53,9 @@ import io.metersphere.system.log.dto.LogDTO;
import io.metersphere.system.log.service.OperationLogService;
import io.metersphere.system.manager.ExportTaskManager;
import io.metersphere.system.mapper.SystemParameterMapper;
import io.metersphere.system.mapper.UserMapper;
import io.metersphere.system.service.FileService;
import io.metersphere.system.service.NoticeSendService;
import io.metersphere.system.uid.IDGenerator;
import io.metersphere.system.utils.ServiceUtils;
import jakarta.annotation.Resource;
@ -115,6 +118,10 @@ public class FunctionalCaseFileService {
private static final String ZIP = "zip";
@Resource
private OperationLogService operationLogService;
@Resource
private NoticeSendService noticeSendService;
@Resource
private UserMapper userMapper;
/**
* 下载excel导入模板
@ -435,6 +442,8 @@ public class FunctionalCaseFileService {
File tmpDir = null;
String fileType = "";
try {
User user = userMapper.selectByPrimaryKey(userId);
noticeSendService.setLanguage(user.getLanguage());
tmpDir = new File(LocalRepositoryDir.getSystemTempDir() + File.separatorChar + EXPORT_CASE_TMP_DIR + "_" + IDGenerator.nextStr());
if (!tmpDir.exists() && !tmpDir.mkdirs()) {
throw new MSException(Translator.get("upload_fail"));
@ -693,7 +702,7 @@ public class FunctionalCaseFileService {
}
List<FunctionalCaseHeader> otherFields = request.getOtherFields();
List<String> keys = otherFields.stream().map(FunctionalCaseHeader::getId).toList();
Map<String, FunctionalCaseExportConverter> converterMaps = FunctionalCaseExportConverterFactory.getConverters(keys);
Map<String, FunctionalCaseExportConverter> converterMaps = FunctionalCaseExportConverterFactory.getConverters(keys, request.getProjectId());
HashMap<String, String> other = new HashMap<>();
otherFields.forEach(header -> {
FunctionalCaseExportConverter converter = converterMaps.get(header.getId());

View File

@ -17,27 +17,28 @@ import io.metersphere.sdk.util.JSON;
import io.metersphere.sdk.util.LogUtils;
import io.metersphere.sdk.util.Translator;
import io.metersphere.system.constants.ExportConstants;
import io.metersphere.system.domain.User;
import io.metersphere.system.dto.sdk.BaseTreeNode;
import io.metersphere.system.dto.sdk.TemplateCustomFieldDTO;
import io.metersphere.system.log.dto.LogDTO;
import io.metersphere.system.log.service.OperationLogService;
import io.metersphere.system.manager.ExportTaskManager;
import io.metersphere.system.mapper.UserMapper;
import io.metersphere.system.service.NoticeSendService;
import io.metersphere.system.uid.IDGenerator;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.io.File;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;
/**
@ -65,6 +66,10 @@ public class FunctionalCaseXmindService {
private static final String XMIND = "xmind";
@Resource
private OperationLogService operationLogService;
@Resource
private NoticeSendService noticeSendService;
@Resource
private UserMapper userMapper;
public void downloadXmindTemplate(String projectId, HttpServletResponse response) {
List<TemplateCustomFieldDTO> customFields = functionalCaseFileService.getCustomFields(projectId);
@ -121,6 +126,8 @@ public class FunctionalCaseXmindService {
File dir = null;
File tmpFile = null;
try {
User user = userMapper.selectByPrimaryKey(userId);
noticeSendService.setLanguage(user.getLanguage());
FunctionalCaseXmindData xmindData = buildXmindData(ids, request);
dir = new File(LocalRepositoryDir.getSystemTempDir());
if (!dir.exists() && !dir.mkdir()) {