refactor(接口测试): 优化获取本地时区
This commit is contained in:
parent
99b1b4eb48
commit
748eef36b5
|
@ -107,7 +107,7 @@ public class ApiReportShareService {
|
|||
|
||||
public ShareInfoDTO gen(ApiReportShareRequest shareRequest, String userId) {
|
||||
UserDTO userDTO = baseUserMapper.selectById(userId);
|
||||
String lang = userDTO.getLanguage() == null ? LocaleContextHolder.getLocale().toString() : userDTO.getLanguage();
|
||||
String lang = userDTO.getLanguage() == null ? LocaleContextHolder.getLocale().toString().split("_#")[0] : userDTO.getLanguage();
|
||||
ShareInfo request = new ShareInfo();
|
||||
BeanUtils.copyBean(request, shareRequest);
|
||||
request.setLang(lang);
|
||||
|
|
|
@ -47,7 +47,7 @@ public class ApiShareService {
|
|||
public ShareInfoDTO genApiDocShareInfo(ApiDefinitionDocRequest request, SessionUser user) {
|
||||
ShareInfo shareInfoRequest = new ShareInfo();
|
||||
String customData = genCustomData(request, shareInfoRequest);
|
||||
String lang = user.getLanguage() == null ? LocaleContextHolder.getLocale().toString() : user.getLanguage();
|
||||
String lang = user.getLanguage() == null ? LocaleContextHolder.getLocale().toString().split("_#")[0] : user.getLanguage();
|
||||
|
||||
Optional.ofNullable(customData)
|
||||
.ifPresent(data -> {
|
||||
|
|
|
@ -34,160 +34,167 @@ import static io.metersphere.sdk.util.ShareUtil.getTimeMills;
|
|||
@Transactional(rollbackFor = Exception.class)
|
||||
public class TestPlanReportShareService {
|
||||
|
||||
@Resource
|
||||
private BaseUserMapper baseUserMapper;
|
||||
@Resource
|
||||
private ShareInfoMapper shareInfoMapper;
|
||||
@Resource
|
||||
private TestPlanReportMapper testPlanReportMapper;
|
||||
@Resource
|
||||
private ProjectApplicationMapper projectApplicationMapper;
|
||||
@Resource
|
||||
private BaseUserMapper baseUserMapper;
|
||||
@Resource
|
||||
private ShareInfoMapper shareInfoMapper;
|
||||
@Resource
|
||||
private TestPlanReportMapper testPlanReportMapper;
|
||||
@Resource
|
||||
private ProjectApplicationMapper projectApplicationMapper;
|
||||
|
||||
private static final Long DEFAULT = 1000L * 60 * 60 * 24;
|
||||
private static final Long DEFAULT = 1000L * 60 * 60 * 24;
|
||||
|
||||
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||
public void validateExpired(ShareInfo shareInfo) {
|
||||
String shareType = shareInfo.getShareType();
|
||||
String projectId = "";
|
||||
if (StringUtils.equals(shareType, ShareInfoType.TEST_PLAN_SHARE_REPORT.name())) {
|
||||
TestPlanReport planReport = testPlanReportMapper.selectByPrimaryKey(new String(shareInfo.getCustomData()));
|
||||
if (planReport != null && BooleanUtils.isFalse(planReport.getDeleted())) {
|
||||
projectId = planReport.getProjectId();
|
||||
}
|
||||
}
|
||||
if (StringUtils.isBlank(projectId)) {
|
||||
throw new MSException(Translator.get("test_plan_report_not_exist"));
|
||||
}
|
||||
ProjectApplicationExample example = new ProjectApplicationExample();
|
||||
example.createCriteria().andProjectIdEqualTo(projectId).andTypeEqualTo(shareType);
|
||||
List<ProjectApplication> projectApplications = projectApplicationMapper.selectByExample(example);
|
||||
if (CollectionUtils.isEmpty(projectApplications)) {
|
||||
millisCheck(System.currentTimeMillis() - shareInfo.getUpdateTime(), DEFAULT, shareInfo.getId());
|
||||
} else {
|
||||
String expr = projectApplications.getFirst().getTypeValue();
|
||||
long timeMills = getTimeMills(shareInfo.getUpdateTime(), expr);
|
||||
millisCheck(System.currentTimeMillis(), timeMills, shareInfo.getId());
|
||||
}
|
||||
}
|
||||
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||
public void validateExpired(ShareInfo shareInfo) {
|
||||
String shareType = shareInfo.getShareType();
|
||||
String projectId = "";
|
||||
if (StringUtils.equals(shareType, ShareInfoType.TEST_PLAN_SHARE_REPORT.name())) {
|
||||
TestPlanReport planReport = testPlanReportMapper.selectByPrimaryKey(new String(shareInfo.getCustomData()));
|
||||
if (planReport != null && BooleanUtils.isFalse(planReport.getDeleted())) {
|
||||
projectId = planReport.getProjectId();
|
||||
}
|
||||
}
|
||||
if (StringUtils.isBlank(projectId)) {
|
||||
throw new MSException(Translator.get("test_plan_report_not_exist"));
|
||||
}
|
||||
ProjectApplicationExample example = new ProjectApplicationExample();
|
||||
example.createCriteria().andProjectIdEqualTo(projectId).andTypeEqualTo(shareType);
|
||||
List<ProjectApplication> projectApplications = projectApplicationMapper.selectByExample(example);
|
||||
if (CollectionUtils.isEmpty(projectApplications)) {
|
||||
millisCheck(System.currentTimeMillis() - shareInfo.getUpdateTime(), DEFAULT, shareInfo.getId());
|
||||
} else {
|
||||
String expr = projectApplications.getFirst().getTypeValue();
|
||||
long timeMills = getTimeMills(shareInfo.getUpdateTime(), expr);
|
||||
millisCheck(System.currentTimeMillis(), timeMills, shareInfo.getId());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成计划报告分享信息
|
||||
* @param shareRequest 分享请求参数
|
||||
* @param currentUser 当前用户
|
||||
* @return 计划报告分享信息
|
||||
*/
|
||||
public TestPlanShareInfo gen(TestPlanReportShareRequest shareRequest, String currentUser) {
|
||||
UserDTO userDTO = baseUserMapper.selectById(currentUser);
|
||||
String lang = userDTO.getLanguage() == null ? LocaleContextHolder.getLocale().toString() : userDTO.getLanguage();
|
||||
ShareInfo request = new ShareInfo();
|
||||
BeanUtils.copyBean(request, shareRequest);
|
||||
request.setLang(lang);
|
||||
request.setCreateUser(currentUser);
|
||||
request.setCustomData(shareRequest.getReportId().getBytes());
|
||||
request.setShareType(ShareInfoType.TEST_PLAN_SHARE_REPORT.name());
|
||||
ShareInfo shareInfo = createShareInfo(request);
|
||||
return conversionShareInfoToDTO(shareInfo);
|
||||
}
|
||||
/**
|
||||
* 生成计划报告分享信息
|
||||
*
|
||||
* @param shareRequest 分享请求参数
|
||||
* @param currentUser 当前用户
|
||||
* @return 计划报告分享信息
|
||||
*/
|
||||
public TestPlanShareInfo gen(TestPlanReportShareRequest shareRequest, String currentUser) {
|
||||
UserDTO userDTO = baseUserMapper.selectById(currentUser);
|
||||
String lang = userDTO.getLanguage() == null ? LocaleContextHolder.getLocale().toString().split("_#")[0] : userDTO.getLanguage();
|
||||
ShareInfo request = new ShareInfo();
|
||||
BeanUtils.copyBean(request, shareRequest);
|
||||
request.setLang(lang);
|
||||
request.setCreateUser(currentUser);
|
||||
request.setCustomData(shareRequest.getReportId().getBytes());
|
||||
request.setShareType(ShareInfoType.TEST_PLAN_SHARE_REPORT.name());
|
||||
ShareInfo shareInfo = createShareInfo(request);
|
||||
return conversionShareInfoToDTO(shareInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取分享信息
|
||||
* @param id 分享ID
|
||||
* @return 分享信息
|
||||
*/
|
||||
public TestPlanShareResponse get(String id) {
|
||||
TestPlanShareResponse dto = new TestPlanShareResponse();
|
||||
ShareInfo shareInfo = shareInfoMapper.selectByPrimaryKey(id);
|
||||
// 无分享记录, 过期直接返回
|
||||
if (shareInfo == null) {
|
||||
dto.setExpired(true);
|
||||
return dto;
|
||||
}
|
||||
BeanUtils.copyBean(dto, shareInfo);
|
||||
dto.setReportId(new String(shareInfo.getCustomData()));
|
||||
// 检查报告ID是否存在
|
||||
TestPlanReport testPlanReport = testPlanReportMapper.selectByPrimaryKey(dto.getReportId());
|
||||
if (testPlanReport == null || testPlanReport.getDeleted()) {
|
||||
dto.setDeleted(true);
|
||||
}
|
||||
// 报告正常, 校验分享时间是否过期
|
||||
if (!dto.isDeleted()) {
|
||||
try {
|
||||
validateExpired(shareInfo);
|
||||
} catch (Exception e) {
|
||||
dto.setExpired(true);
|
||||
}
|
||||
}
|
||||
return dto;
|
||||
}
|
||||
/**
|
||||
* 获取分享信息
|
||||
*
|
||||
* @param id 分享ID
|
||||
* @return 分享信息
|
||||
*/
|
||||
public TestPlanShareResponse get(String id) {
|
||||
TestPlanShareResponse dto = new TestPlanShareResponse();
|
||||
ShareInfo shareInfo = shareInfoMapper.selectByPrimaryKey(id);
|
||||
// 无分享记录, 过期直接返回
|
||||
if (shareInfo == null) {
|
||||
dto.setExpired(true);
|
||||
return dto;
|
||||
}
|
||||
BeanUtils.copyBean(dto, shareInfo);
|
||||
dto.setReportId(new String(shareInfo.getCustomData()));
|
||||
// 检查报告ID是否存在
|
||||
TestPlanReport testPlanReport = testPlanReportMapper.selectByPrimaryKey(dto.getReportId());
|
||||
if (testPlanReport == null || testPlanReport.getDeleted()) {
|
||||
dto.setDeleted(true);
|
||||
}
|
||||
// 报告正常, 校验分享时间是否过期
|
||||
if (!dto.isDeleted()) {
|
||||
try {
|
||||
validateExpired(shareInfo);
|
||||
} catch (Exception e) {
|
||||
dto.setExpired(true);
|
||||
}
|
||||
}
|
||||
return dto;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取项目计划报告分享有效期
|
||||
* @param projectId 项目ID
|
||||
* @return 有效期
|
||||
*/
|
||||
public String getShareTime(String projectId) {
|
||||
ProjectApplicationExample example = new ProjectApplicationExample();
|
||||
example.createCriteria().andProjectIdEqualTo(projectId).andTypeEqualTo(ShareInfoType.TEST_PLAN_SHARE_REPORT.name());
|
||||
List<ProjectApplication> projectApplications = projectApplicationMapper.selectByExample(example);
|
||||
if (CollectionUtils.isEmpty(projectApplications)) {
|
||||
return "1D";
|
||||
} else {
|
||||
return projectApplications.getFirst().getTypeValue();
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 获取项目计划报告分享有效期
|
||||
*
|
||||
* @param projectId 项目ID
|
||||
* @return 有效期
|
||||
*/
|
||||
public String getShareTime(String projectId) {
|
||||
ProjectApplicationExample example = new ProjectApplicationExample();
|
||||
example.createCriteria().andProjectIdEqualTo(projectId).andTypeEqualTo(ShareInfoType.TEST_PLAN_SHARE_REPORT.name());
|
||||
List<ProjectApplication> projectApplications = projectApplicationMapper.selectByExample(example);
|
||||
if (CollectionUtils.isEmpty(projectApplications)) {
|
||||
return "1D";
|
||||
} else {
|
||||
return projectApplications.getFirst().getTypeValue();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验分享的资源是否存在
|
||||
* @param id 分享ID
|
||||
* @return 分享资源信息
|
||||
*/
|
||||
public ShareInfo checkResource(String id) {
|
||||
ShareInfo shareInfo = shareInfoMapper.selectByPrimaryKey(id);
|
||||
if (shareInfo == null) {
|
||||
throw new RuntimeException(Translator.get("connection_expired"));
|
||||
}
|
||||
return shareInfo;
|
||||
}
|
||||
/**
|
||||
* 校验分享的资源是否存在
|
||||
*
|
||||
* @param id 分享ID
|
||||
* @return 分享资源信息
|
||||
*/
|
||||
public ShareInfo checkResource(String id) {
|
||||
ShareInfo shareInfo = shareInfoMapper.selectByPrimaryKey(id);
|
||||
if (shareInfo == null) {
|
||||
throw new RuntimeException(Translator.get("connection_expired"));
|
||||
}
|
||||
return shareInfo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建分享信息
|
||||
* @param shareInfo 分享的信息
|
||||
* @return 分享信息
|
||||
*/
|
||||
private ShareInfo createShareInfo(ShareInfo shareInfo) {
|
||||
long createTime = System.currentTimeMillis();
|
||||
shareInfo.setId(IDGenerator.nextStr());
|
||||
shareInfo.setCreateTime(createTime);
|
||||
shareInfo.setUpdateTime(createTime);
|
||||
shareInfoMapper.insert(shareInfo);
|
||||
return shareInfo;
|
||||
}
|
||||
/**
|
||||
* 创建分享信息
|
||||
*
|
||||
* @param shareInfo 分享的信息
|
||||
* @return 分享信息
|
||||
*/
|
||||
private ShareInfo createShareInfo(ShareInfo shareInfo) {
|
||||
long createTime = System.currentTimeMillis();
|
||||
shareInfo.setId(IDGenerator.nextStr());
|
||||
shareInfo.setCreateTime(createTime);
|
||||
shareInfo.setUpdateTime(createTime);
|
||||
shareInfoMapper.insert(shareInfo);
|
||||
return shareInfo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置分享信息并返回
|
||||
* @param shareInfo 分享信息
|
||||
* @return 计划报告分享信息返回
|
||||
*/
|
||||
private TestPlanShareInfo conversionShareInfoToDTO(ShareInfo shareInfo) {
|
||||
TestPlanShareInfo testPlanShareInfo = new TestPlanShareInfo();
|
||||
if (shareInfo.getCustomData() != null) {
|
||||
String url = "?shareId=" + shareInfo.getId();
|
||||
testPlanShareInfo.setId(shareInfo.getId());
|
||||
testPlanShareInfo.setShareUrl(url);
|
||||
}
|
||||
return testPlanShareInfo;
|
||||
}
|
||||
/**
|
||||
* 设置分享信息并返回
|
||||
*
|
||||
* @param shareInfo 分享信息
|
||||
* @return 计划报告分享信息返回
|
||||
*/
|
||||
private TestPlanShareInfo conversionShareInfoToDTO(ShareInfo shareInfo) {
|
||||
TestPlanShareInfo testPlanShareInfo = new TestPlanShareInfo();
|
||||
if (shareInfo.getCustomData() != null) {
|
||||
String url = "?shareId=" + shareInfo.getId();
|
||||
testPlanShareInfo.setId(shareInfo.getId());
|
||||
testPlanShareInfo.setShareUrl(url);
|
||||
}
|
||||
return testPlanShareInfo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验时间是否过期
|
||||
* @param compareMillis 比较时间
|
||||
* @param millis 分享时间
|
||||
* @param shareInfoId 分享ID
|
||||
*/
|
||||
private void millisCheck(long compareMillis, long millis, String shareInfoId) {
|
||||
if (compareMillis > millis) {
|
||||
shareInfoMapper.deleteByPrimaryKey(shareInfoId);
|
||||
throw new MSException(Translator.get("connection_expired"));
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 校验时间是否过期
|
||||
*
|
||||
* @param compareMillis 比较时间
|
||||
* @param millis 分享时间
|
||||
* @param shareInfoId 分享ID
|
||||
*/
|
||||
private void millisCheck(long compareMillis, long millis, String shareInfoId) {
|
||||
if (compareMillis > millis) {
|
||||
shareInfoMapper.deleteByPrimaryKey(shareInfoId);
|
||||
throw new MSException(Translator.get("connection_expired"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue