refactor(接口测试): 优化接口分享报告

This commit is contained in:
wxg0103 2024-03-22 16:42:22 +08:00 committed by 刘瑞斌
parent c88cfa3ad1
commit af988df1b8
6 changed files with 76 additions and 14 deletions

View File

@ -1,8 +1,9 @@
package io.metersphere.api.controller; package io.metersphere.api.controller;
import io.metersphere.api.dto.share.ApiReportShareDTO;
import io.metersphere.api.dto.share.ApiReportShareRequest;
import io.metersphere.api.dto.share.ShareInfoDTO; import io.metersphere.api.dto.share.ShareInfoDTO;
import io.metersphere.api.service.ApiReportShareService; import io.metersphere.api.service.ApiReportShareService;
import io.metersphere.sdk.domain.ShareInfo;
import io.metersphere.system.utils.SessionUtils; import io.metersphere.system.utils.SessionUtils;
import io.metersphere.validation.groups.Created; import io.metersphere.validation.groups.Created;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
@ -21,12 +22,12 @@ public class ApiReportShareController {
private ApiReportShareService apiReportShareService; private ApiReportShareService apiReportShareService;
@PostMapping("/gen") @PostMapping("/gen")
public ShareInfoDTO generateShareInfo(@Validated(Created.class) @RequestBody ShareInfo request) { public ShareInfoDTO generateShareInfo(@Validated(Created.class) @RequestBody ApiReportShareRequest request) {
return apiReportShareService.gen(request, Objects.requireNonNull(SessionUtils.getUser())); return apiReportShareService.gen(request, Objects.requireNonNull(SessionUtils.getUser()));
} }
@GetMapping("/get/{id}") @GetMapping("/get/{id}")
public ShareInfo get(@PathVariable String id) { public ApiReportShareDTO get(@PathVariable String id) {
return apiReportShareService.get(id); return apiReportShareService.get(id);
} }

View File

@ -0,0 +1,22 @@
package io.metersphere.api.dto.share;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
@Data
public class ApiReportShareDTO {
@Schema(description = "分享id")
private String id;
@Schema(description = "分享类型 资源的类型 Single, Batch, API_SHARE_REPORT, TEST_PLAN_SHARE_REPORT")
private String shareType;
@Schema(description = "语言")
private String lang;
private String projectId;
@Schema(description = "分享扩展数据 资源的id" ,requiredMode = Schema.RequiredMode.REQUIRED)
private String reportId;
}

View File

@ -0,0 +1,27 @@
package io.metersphere.api.dto.share;
import io.metersphere.validation.groups.Created;
import io.metersphere.validation.groups.Updated;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import lombok.Data;
@Data
public class ApiReportShareRequest {
@Schema(description = "分享类型 资源的类型 Single, Batch, API_SHARE_REPORT, TEST_PLAN_SHARE_REPORT")
private String shareType;
@Schema(description = "语言")
private String lang;
@Schema(description = "项目id", requiredMode = Schema.RequiredMode.REQUIRED)
@NotBlank(message = "{share_info.project_id.not_blank}", groups = {Created.class})
@Size(min = 1, max = 50, message = "{share_info.project_id.length_range}", groups = {Created.class, Updated.class})
private String projectId;
@Schema(description = "分享扩展数据 资源的id" ,requiredMode = Schema.RequiredMode.REQUIRED)
@NotBlank(message = "{share_info.project_id.not_blank}")
private String reportId;
}

View File

@ -3,6 +3,8 @@ package io.metersphere.api.service;
import io.metersphere.api.constants.ShareInfoType; import io.metersphere.api.constants.ShareInfoType;
import io.metersphere.api.domain.ApiReport; import io.metersphere.api.domain.ApiReport;
import io.metersphere.api.domain.ApiScenarioReport; import io.metersphere.api.domain.ApiScenarioReport;
import io.metersphere.api.dto.share.ApiReportShareDTO;
import io.metersphere.api.dto.share.ApiReportShareRequest;
import io.metersphere.api.dto.share.ShareInfoDTO; import io.metersphere.api.dto.share.ShareInfoDTO;
import io.metersphere.api.mapper.ApiReportMapper; import io.metersphere.api.mapper.ApiReportMapper;
import io.metersphere.api.mapper.ApiScenarioReportMapper; import io.metersphere.api.mapper.ApiScenarioReportMapper;
@ -12,6 +14,7 @@ import io.metersphere.project.mapper.ProjectApplicationMapper;
import io.metersphere.sdk.domain.ShareInfo; import io.metersphere.sdk.domain.ShareInfo;
import io.metersphere.sdk.exception.MSException; import io.metersphere.sdk.exception.MSException;
import io.metersphere.sdk.mapper.ShareInfoMapper; import io.metersphere.sdk.mapper.ShareInfoMapper;
import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.sdk.util.Translator; import io.metersphere.sdk.util.Translator;
import io.metersphere.system.dto.sdk.SessionUser; import io.metersphere.system.dto.sdk.SessionUser;
import io.metersphere.system.uid.IDGenerator; import io.metersphere.system.uid.IDGenerator;
@ -99,10 +102,13 @@ public class ApiReportShareService {
return returnDTO; return returnDTO;
} }
public ShareInfoDTO gen(ShareInfo request, SessionUser user) { public ShareInfoDTO gen(ApiReportShareRequest shareRequest, SessionUser user) {
String lang = user.getLanguage() == null ? LocaleContextHolder.getLocale().toString() : user.getLanguage(); String lang = user.getLanguage() == null ? LocaleContextHolder.getLocale().toString() : user.getLanguage();
ShareInfo request = new ShareInfo();
BeanUtils.copyBean(request, shareRequest);
request.setLang(lang); request.setLang(lang);
request.setCreateUser(user.getId()); request.setCreateUser(user.getId());
request.setCustomData(shareRequest.getReportId().getBytes());
request.setShareType(ShareInfoType.API_SHARE_REPORT.name()); request.setShareType(ShareInfoType.API_SHARE_REPORT.name());
ShareInfo shareInfo = createShareInfo(request); ShareInfo shareInfo = createShareInfo(request);
return conversionShareInfoToDTO(shareInfo); return conversionShareInfoToDTO(shareInfo);
@ -116,7 +122,11 @@ public class ApiReportShareService {
return shareInfo; return shareInfo;
} }
public ShareInfo get(String id) { public ApiReportShareDTO get(String id) {
return checkResource(id); ShareInfo shareInfo = checkResource(id);
ApiReportShareDTO dto = new ApiReportShareDTO();
BeanUtils.copyBean(dto, shareInfo);
dto.setReportId(new String(shareInfo.getCustomData()));
return dto;
} }
} }

View File

@ -7,6 +7,7 @@ import io.metersphere.api.dto.definition.ApiReportDTO;
import io.metersphere.api.dto.definition.ApiReportDetailDTO; import io.metersphere.api.dto.definition.ApiReportDetailDTO;
import io.metersphere.api.dto.definition.ApiReportPageRequest; import io.metersphere.api.dto.definition.ApiReportPageRequest;
import io.metersphere.api.dto.scenario.ApiScenarioDTO; import io.metersphere.api.dto.scenario.ApiScenarioDTO;
import io.metersphere.api.dto.share.ApiReportShareRequest;
import io.metersphere.api.dto.share.ShareInfoDTO; import io.metersphere.api.dto.share.ShareInfoDTO;
import io.metersphere.api.mapper.ApiReportDetailMapper; import io.metersphere.api.mapper.ApiReportDetailMapper;
import io.metersphere.api.mapper.ApiReportLogMapper; import io.metersphere.api.mapper.ApiReportLogMapper;
@ -416,8 +417,8 @@ public class ApiReportControllerTests extends BaseTest {
@Test @Test
@Order(8) @Order(8)
public void generateUrl() throws Exception { public void generateUrl() throws Exception {
ShareInfo shareInfo = new ShareInfo(); ApiReportShareRequest shareInfo = new ApiReportShareRequest();
shareInfo.setCustomData("test-report-id".getBytes()); shareInfo.setReportId("test-report-id");
shareInfo.setProjectId(DEFAULT_PROJECT_ID); shareInfo.setProjectId(DEFAULT_PROJECT_ID);
shareInfo.setShareType(ShareInfoType.API_SHARE_REPORT.name()); shareInfo.setShareType(ShareInfoType.API_SHARE_REPORT.name());
MvcResult mvcResult = responsePost("/api/report/share/gen", shareInfo); MvcResult mvcResult = responsePost("/api/report/share/gen", shareInfo);
@ -455,8 +456,8 @@ public class ApiReportControllerTests extends BaseTest {
//TODO 过期的校验 未完成 需要补充 //TODO 过期的校验 未完成 需要补充
//项目当前设置了分享时间 并且没有过期 //项目当前设置了分享时间 并且没有过期
shareInfo = new ShareInfo(); shareInfo = new ApiReportShareRequest();
shareInfo.setCustomData("test-report-id".getBytes()); shareInfo.setReportId("test-report-id");
shareInfo.setProjectId(DEFAULT_PROJECT_ID); shareInfo.setProjectId(DEFAULT_PROJECT_ID);
shareInfo.setShareType(ShareInfoType.API_SHARE_REPORT.name()); shareInfo.setShareType(ShareInfoType.API_SHARE_REPORT.name());
mvcResult = responsePost("/api/report/share/gen", shareInfo); mvcResult = responsePost("/api/report/share/gen", shareInfo);

View File

@ -8,6 +8,7 @@ import io.metersphere.api.dto.definition.ApiReportPageRequest;
import io.metersphere.api.dto.scenario.ApiScenarioDTO; import io.metersphere.api.dto.scenario.ApiScenarioDTO;
import io.metersphere.api.dto.scenario.ApiScenarioReportDTO; import io.metersphere.api.dto.scenario.ApiScenarioReportDTO;
import io.metersphere.api.dto.scenario.ApiScenarioReportDetailDTO; import io.metersphere.api.dto.scenario.ApiScenarioReportDetailDTO;
import io.metersphere.api.dto.share.ApiReportShareRequest;
import io.metersphere.api.dto.share.ShareInfoDTO; import io.metersphere.api.dto.share.ShareInfoDTO;
import io.metersphere.api.mapper.ApiScenarioReportDetailMapper; import io.metersphere.api.mapper.ApiScenarioReportDetailMapper;
import io.metersphere.api.mapper.ApiScenarioReportLogMapper; import io.metersphere.api.mapper.ApiScenarioReportLogMapper;
@ -376,8 +377,8 @@ public class ApiScenarioReportControllerTests extends BaseTest {
@Test @Test
@Order(8) @Order(8)
public void generateUrl() throws Exception { public void generateUrl() throws Exception {
ShareInfo shareInfo = new ShareInfo(); ApiReportShareRequest shareInfo = new ApiReportShareRequest();
shareInfo.setCustomData("test-scenario-report-id".getBytes()); shareInfo.setReportId("test-scenario-report-id");
shareInfo.setProjectId(DEFAULT_PROJECT_ID); shareInfo.setProjectId(DEFAULT_PROJECT_ID);
shareInfo.setShareType(ShareInfoType.API_SHARE_REPORT.name()); shareInfo.setShareType(ShareInfoType.API_SHARE_REPORT.name());
MvcResult mvcResult = responsePost("/api/report/share/gen", shareInfo); MvcResult mvcResult = responsePost("/api/report/share/gen", shareInfo);
@ -413,8 +414,8 @@ public class ApiScenarioReportControllerTests extends BaseTest {
.andExpect(status().is5xxServerError()); .andExpect(status().is5xxServerError());
shareInfo = new ShareInfo(); shareInfo = new ApiReportShareRequest();
shareInfo.setCustomData("test-scenario-report-id".getBytes()); shareInfo.setReportId("test-scenario-report-id");
shareInfo.setProjectId(DEFAULT_PROJECT_ID); shareInfo.setProjectId(DEFAULT_PROJECT_ID);
shareInfo.setShareType(ShareInfoType.API_SHARE_REPORT.name()); shareInfo.setShareType(ShareInfoType.API_SHARE_REPORT.name());
mvcResult = responsePost("/api/report/share/gen", shareInfo); mvcResult = responsePost("/api/report/share/gen", shareInfo);