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.functional.excel.constants.FunctionalCaseExportOtherField;
import io.metersphere.sdk.util.LogUtils; import io.metersphere.sdk.util.LogUtils;
import java.lang.reflect.Constructor;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -12,14 +13,28 @@ import java.util.Map;
*/ */
public class FunctionalCaseExportConverterFactory { 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<>(); Map<String, FunctionalCaseExportConverter> converterMapResult = new HashMap<>();
try { try {
HashMap<String, Class<? extends FunctionalCaseExportConverter>> converterMap = getConverterMap(); HashMap<String, Class<? extends FunctionalCaseExportConverter>> converterMap = getConverterMap(projectId);
for (String key : keys) { for (String key : keys) {
Class<? extends FunctionalCaseExportConverter> clazz = converterMap.get(key); Class<? extends FunctionalCaseExportConverter> clazz = converterMap.get(key);
if (clazz != null) { if (clazz != null) {
// 获取所有构造函数
Constructor<?>[] constructors = clazz.getConstructors();
// 遍历构造函数选择适当的构造函数进行实例化
for (Constructor<?> constructor : constructors) {
Class<?>[] parameterTypes = constructor.getParameterTypes();
if (parameterTypes.length == 0) {
// 如果是无参构造直接调用 newInstance
converterMapResult.put(key, clazz.getDeclaredConstructor().newInstance()); converterMapResult.put(key, clazz.getDeclaredConstructor().newInstance());
break;
} else {
// 如果是有参构造可以传递参数
converterMapResult.put(key, clazz.getDeclaredConstructor(String.class).newInstance(projectId));
break;
}
}
} }
} }
} catch (Exception e) { } catch (Exception e) {
@ -28,11 +43,11 @@ public class FunctionalCaseExportConverterFactory {
return converterMapResult; return converterMapResult;
} }
public static FunctionalCaseExportConverter getConverter(String key) { public static FunctionalCaseExportConverter getConverter(String key, String projectId) {
try { try {
Class<? extends FunctionalCaseExportConverter> clazz = getConverterMap().get(key); Class<? extends FunctionalCaseExportConverter> clazz = getConverterMap(projectId).get(key);
if (clazz != null) { if (clazz != null) {
return clazz.getDeclaredConstructor().newInstance(); return clazz.getDeclaredConstructor(String.class).newInstance(projectId);
} }
} catch (Exception e) { } catch (Exception e) {
LogUtils.error(e); LogUtils.error(e);
@ -40,7 +55,7 @@ public class FunctionalCaseExportConverterFactory {
return null; return null;
} }
private static HashMap<String, Class<? extends FunctionalCaseExportConverter>> getConverterMap() { private static HashMap<String, Class<? extends FunctionalCaseExportConverter>> getConverterMap(String projectId) {
return new HashMap<>() {{ return new HashMap<>() {{
put(FunctionalCaseExportOtherField.CREATE_USER.getValue(), FunctionalCaseExportCreateUserConverter.class); put(FunctionalCaseExportOtherField.CREATE_USER.getValue(), FunctionalCaseExportCreateUserConverter.class);
put(FunctionalCaseExportOtherField.CREATE_TIME.getValue(), FunctionalCaseExportCreateTimeConverter.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.project.service.ProjectApplicationService;
import io.metersphere.sdk.util.CommonBeanFactory; import io.metersphere.sdk.util.CommonBeanFactory;
import io.metersphere.system.domain.User; import io.metersphere.system.domain.User;
import io.metersphere.system.utils.SessionUtils;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -20,9 +19,9 @@ public class FunctionalCaseExportCreateUserConverter implements FunctionalCaseEx
public Map<String, String> userMap = new HashMap<>(); public Map<String, String> userMap = new HashMap<>();
public FunctionalCaseExportCreateUserConverter() { public FunctionalCaseExportCreateUserConverter(String projectId) {
ProjectApplicationService projectApplicationService = CommonBeanFactory.getBean(ProjectApplicationService.class); 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())); 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 class FunctionalCaseExportUpdateUserConverter extends FunctionalCaseExportCreateUserConverter {
public FunctionalCaseExportUpdateUserConverter(String projectId) {
super(projectId);
}
@Override @Override
public String parse(FunctionalCase functionalCase, Map<String, List<FunctionalCaseComment>> caseCommentMap, Map<String, List<TestPlanCaseExecuteHistory>> executeCommentMap, Map<String, List<CaseReviewHistory>> reviewCommentMap) { 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()); 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.constants.ExportConstants;
import io.metersphere.system.domain.CustomFieldOption; import io.metersphere.system.domain.CustomFieldOption;
import io.metersphere.system.domain.SystemParameter; 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.BaseTreeNode;
import io.metersphere.system.dto.sdk.SessionUser; import io.metersphere.system.dto.sdk.SessionUser;
import io.metersphere.system.dto.sdk.TemplateCustomFieldDTO; 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.log.service.OperationLogService;
import io.metersphere.system.manager.ExportTaskManager; import io.metersphere.system.manager.ExportTaskManager;
import io.metersphere.system.mapper.SystemParameterMapper; import io.metersphere.system.mapper.SystemParameterMapper;
import io.metersphere.system.mapper.UserMapper;
import io.metersphere.system.service.FileService; import io.metersphere.system.service.FileService;
import io.metersphere.system.service.NoticeSendService;
import io.metersphere.system.uid.IDGenerator; import io.metersphere.system.uid.IDGenerator;
import io.metersphere.system.utils.ServiceUtils; import io.metersphere.system.utils.ServiceUtils;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
@ -115,6 +118,10 @@ public class FunctionalCaseFileService {
private static final String ZIP = "zip"; private static final String ZIP = "zip";
@Resource @Resource
private OperationLogService operationLogService; private OperationLogService operationLogService;
@Resource
private NoticeSendService noticeSendService;
@Resource
private UserMapper userMapper;
/** /**
* 下载excel导入模板 * 下载excel导入模板
@ -435,6 +442,8 @@ public class FunctionalCaseFileService {
File tmpDir = null; File tmpDir = null;
String fileType = ""; String fileType = "";
try { try {
User user = userMapper.selectByPrimaryKey(userId);
noticeSendService.setLanguage(user.getLanguage());
tmpDir = new File(LocalRepositoryDir.getSystemTempDir() + File.separatorChar + EXPORT_CASE_TMP_DIR + "_" + IDGenerator.nextStr()); tmpDir = new File(LocalRepositoryDir.getSystemTempDir() + File.separatorChar + EXPORT_CASE_TMP_DIR + "_" + IDGenerator.nextStr());
if (!tmpDir.exists() && !tmpDir.mkdirs()) { if (!tmpDir.exists() && !tmpDir.mkdirs()) {
throw new MSException(Translator.get("upload_fail")); throw new MSException(Translator.get("upload_fail"));
@ -693,7 +702,7 @@ public class FunctionalCaseFileService {
} }
List<FunctionalCaseHeader> otherFields = request.getOtherFields(); List<FunctionalCaseHeader> otherFields = request.getOtherFields();
List<String> keys = otherFields.stream().map(FunctionalCaseHeader::getId).toList(); 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<>(); HashMap<String, String> other = new HashMap<>();
otherFields.forEach(header -> { otherFields.forEach(header -> {
FunctionalCaseExportConverter converter = converterMaps.get(header.getId()); 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.LogUtils;
import io.metersphere.sdk.util.Translator; import io.metersphere.sdk.util.Translator;
import io.metersphere.system.constants.ExportConstants; 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.BaseTreeNode;
import io.metersphere.system.dto.sdk.TemplateCustomFieldDTO; import io.metersphere.system.dto.sdk.TemplateCustomFieldDTO;
import io.metersphere.system.log.dto.LogDTO; import io.metersphere.system.log.dto.LogDTO;
import io.metersphere.system.log.service.OperationLogService; import io.metersphere.system.log.service.OperationLogService;
import io.metersphere.system.manager.ExportTaskManager; 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 io.metersphere.system.uid.IDGenerator;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.io.File; import java.io.File;
import java.io.InputStream; import java.io.InputStream;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.*;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
@ -65,6 +66,10 @@ public class FunctionalCaseXmindService {
private static final String XMIND = "xmind"; private static final String XMIND = "xmind";
@Resource @Resource
private OperationLogService operationLogService; private OperationLogService operationLogService;
@Resource
private NoticeSendService noticeSendService;
@Resource
private UserMapper userMapper;
public void downloadXmindTemplate(String projectId, HttpServletResponse response) { public void downloadXmindTemplate(String projectId, HttpServletResponse response) {
List<TemplateCustomFieldDTO> customFields = functionalCaseFileService.getCustomFields(projectId); List<TemplateCustomFieldDTO> customFields = functionalCaseFileService.getCustomFields(projectId);
@ -121,6 +126,8 @@ public class FunctionalCaseXmindService {
File dir = null; File dir = null;
File tmpFile = null; File tmpFile = null;
try { try {
User user = userMapper.selectByPrimaryKey(userId);
noticeSendService.setLanguage(user.getLanguage());
FunctionalCaseXmindData xmindData = buildXmindData(ids, request); FunctionalCaseXmindData xmindData = buildXmindData(ids, request);
dir = new File(LocalRepositoryDir.getSystemTempDir()); dir = new File(LocalRepositoryDir.getSystemTempDir());
if (!dir.exists() && !dir.mkdir()) { if (!dir.exists() && !dir.mkdir()) {