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