From b8364cc0421653d62a99ef2a8ec76088d6db6a80 Mon Sep 17 00:00:00 2001 From: AgAngle <1323481023@qq.com> Date: Fri, 29 Mar 2024 15:20:49 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=9C=BA=E6=99=AF=E6=9F=A5=E8=AF=A2=E8=B7=A8?= =?UTF-8?q?=E9=A1=B9=E7=9B=AE=E4=BF=A1=E6=81=AF=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../scenario/ApiScenarioController.java | 11 +++++-- .../dto/scenario/ApiScenarioDetailDTO.java | 16 +++++++++ .../service/scenario/ApiScenarioService.java | 27 +++++++++++++++ .../metersphere/api/utils/ApiDataUtils.java | 6 ++-- .../ApiScenarioControllerTests.java | 33 +++++++++++++++++-- 5 files changed, 85 insertions(+), 8 deletions(-) create mode 100644 backend/services/api-test/src/main/java/io/metersphere/api/dto/scenario/ApiScenarioDetailDTO.java diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/controller/scenario/ApiScenarioController.java b/backend/services/api-test/src/main/java/io/metersphere/api/controller/scenario/ApiScenarioController.java index 4697d5a5c6..402429df8a 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/controller/scenario/ApiScenarioController.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/controller/scenario/ApiScenarioController.java @@ -14,6 +14,7 @@ import io.metersphere.api.service.ApiValidateService; import io.metersphere.api.service.scenario.ApiScenarioLogService; import io.metersphere.api.service.scenario.ApiScenarioNoticeService; import io.metersphere.api.service.scenario.ApiScenarioService; +import io.metersphere.project.domain.Project; import io.metersphere.project.service.FileModuleService; import io.metersphere.sdk.constants.PermissionConstants; import io.metersphere.sdk.dto.api.task.TaskRequestDTO; @@ -130,8 +131,8 @@ public class ApiScenarioController { @Operation(summary = "接口测试-接口场景管理-获取场景详情") @RequiresPermissions(PermissionConstants.PROJECT_API_SCENARIO_READ) @CheckOwner(resourceId = "#scenarioId", resourceType = "api_scenario") - public ApiScenarioDetail get(@PathVariable String scenarioId) { - return apiScenarioService.get(scenarioId); + public ApiScenarioDetailDTO getApiScenarioDetailDTO(@PathVariable String scenarioId) { + return apiScenarioService.getApiScenarioDetailDTO(scenarioId, SessionUtils.getUserId()); } @GetMapping("/step/get/{stepId}") @@ -141,6 +142,12 @@ public class ApiScenarioController { return apiScenarioService.getStepDetail(stepId); } + @GetMapping("/step/project-ifo/{projectId}") + @Operation(summary = "接口测试-接口场景管理-获取步骤的所属项目信息") + @RequiresPermissions(PermissionConstants.PROJECT_API_SCENARIO_READ) + public Project getStepResourceProjectInfo(@PathVariable String projectId) { + return apiScenarioService.getStepResourceProjectInfo(projectId); + } //需求补充:回收站里的相关操作都不需要发通知 @GetMapping("/recover/{id}") diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/dto/scenario/ApiScenarioDetailDTO.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/scenario/ApiScenarioDetailDTO.java new file mode 100644 index 0000000000..91bad60f46 --- /dev/null +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/scenario/ApiScenarioDetailDTO.java @@ -0,0 +1,16 @@ +package io.metersphere.api.dto.scenario; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; +import lombok.EqualsAndHashCode; + +@Data +@EqualsAndHashCode(callSuper = false) +public class ApiScenarioDetailDTO extends ApiScenarioDetail { + @Schema(description = "创建人名称") + private String createUserName; + @Schema(description = "更新人名称") + private String updateUserName; + @Schema(description = "当前用户是否关注") + private Boolean follow = false; +} diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioService.java index 7bfe9fb992..e3331f2b71 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioService.java @@ -1868,6 +1868,22 @@ public class ApiScenarioService extends MoveNodeService { apiScenarioMapper.updateByPrimaryKeySelective(update); } + public ApiScenarioDetailDTO getApiScenarioDetailDTO(String scenarioId, String userId) { + ApiScenarioDetail apiScenarioDetail = get(scenarioId); + ApiScenarioDetailDTO apiScenarioDetailDTO = BeanUtils.copyBean(new ApiScenarioDetailDTO(), apiScenarioDetail); + Map userNameMap = userLoginService.getUserNameMap(List.of(apiScenarioDetail.getCreateUser(), apiScenarioDetail.getUpdateUser())); + apiScenarioDetailDTO.setCreateUserName(userNameMap.get(apiScenarioDetail.getCreateUser())); + apiScenarioDetailDTO.setUpdateUserName(userNameMap.get(apiScenarioDetail.getUpdateUser())); + + // 设置是否关注 + ApiScenarioFollowerExample followerExample = new ApiScenarioFollowerExample(); + followerExample.createCriteria().andApiScenarioIdEqualTo(scenarioId); + followerExample.createCriteria().andUserIdEqualTo(userId); + List followers = apiScenarioFollowerMapper.selectByExample(followerExample); + apiScenarioDetailDTO.setFollow(CollectionUtils.isNotEmpty(followers)); + return apiScenarioDetailDTO; + } + public ApiScenarioDetail get(String scenarioId) { ApiScenario apiScenario = checkResourceExist(scenarioId); ApiScenarioDetail apiScenarioDetail = BeanUtils.copyBean(new ApiScenarioDetail(), apiScenario); @@ -2559,4 +2575,15 @@ public class ApiScenarioService extends MoveNodeService { public List getReference(ReferenceRequest request) { return extApiDefinitionMapper.getReference(request); } + + public Project getStepResourceProjectInfo(String projectId) { + Project project = projectMapper.selectByPrimaryKey(projectId); + if (project == null) { + return null; + } + Project projectInfo = new Project(); + projectInfo.setId(project.getId()); + projectInfo.setName(project.getName()); + return projectInfo; + } } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/utils/ApiDataUtils.java b/backend/services/api-test/src/main/java/io/metersphere/api/utils/ApiDataUtils.java index d2d24cb0c6..178c3e6ce1 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/utils/ApiDataUtils.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/utils/ApiDataUtils.java @@ -11,10 +11,7 @@ import com.fasterxml.jackson.databind.json.JsonMapper; import com.fasterxml.jackson.databind.jsontype.NamedType; import com.fasterxml.jackson.databind.type.CollectionType; import io.metersphere.api.dto.request.MsCommonElement; -import io.metersphere.api.dto.request.controller.MsConstantTimerController; -import io.metersphere.api.dto.request.controller.MsIfController; -import io.metersphere.api.dto.request.controller.MsLoopController; -import io.metersphere.api.dto.request.controller.MsOnceOnlyController; +import io.metersphere.api.dto.request.controller.*; import io.metersphere.api.dto.request.http.MsHTTPElement; import io.metersphere.sdk.exception.MSException; @@ -42,6 +39,7 @@ public class ApiDataUtils { namedTypes.add(new NamedType(MsLoopController.class, MsLoopController.class.getSimpleName())); namedTypes.add(new NamedType(MsOnceOnlyController.class, MsOnceOnlyController.class.getSimpleName())); namedTypes.add(new NamedType(MsConstantTimerController.class, MsConstantTimerController.class.getSimpleName())); + namedTypes.add(new NamedType(MsScriptElement.class, MsScriptElement.class.getSimpleName())); setObjectMapper(objectMapper); namedTypes.forEach(objectMapper::registerSubtypes); } diff --git a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiScenarioControllerTests.java b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiScenarioControllerTests.java index 259257a008..fea8db614a 100644 --- a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiScenarioControllerTests.java +++ b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiScenarioControllerTests.java @@ -30,10 +30,12 @@ import io.metersphere.api.utils.ApiDataUtils; import io.metersphere.plugin.api.spi.AbstractMsTestElement; import io.metersphere.project.api.assertion.MsResponseCodeAssertion; import io.metersphere.project.api.assertion.MsScriptAssertion; +import io.metersphere.project.domain.Project; import io.metersphere.project.domain.ProjectVersion; import io.metersphere.project.dto.environment.variables.CommonVariables; import io.metersphere.project.dto.filemanagement.request.FileUploadRequest; import io.metersphere.project.mapper.ExtBaseProjectVersionMapper; +import io.metersphere.project.mapper.ProjectMapper; import io.metersphere.project.mapper.ProjectVersionMapper; import io.metersphere.project.service.FileMetadataService; import io.metersphere.sdk.constants.*; @@ -57,6 +59,7 @@ import io.metersphere.system.dto.request.PluginUpdateRequest; import io.metersphere.system.dto.sdk.request.PosRequest; import io.metersphere.system.log.constants.OperationLogType; import io.metersphere.system.mapper.ScheduleMapper; +import io.metersphere.system.mapper.UserMapper; import io.metersphere.system.service.PluginService; import io.metersphere.system.uid.IDGenerator; import io.metersphere.system.uid.NumGenerator; @@ -97,6 +100,7 @@ public class ApiScenarioControllerTests extends BaseTest { protected static final String UPLOAD_TEMP_FILE = "upload/temp/file"; protected static final String DELETE_TO_GC = "delete-to-gc/{0}"; protected static final String STEP_GET = "step/get/{0}"; + protected static final String STEP_PROJECT_INFO = "step/project-ifo/{0}"; protected static final String DEBUG = "debug"; protected static final String RUN = "run/{0}"; protected static final String RUN_REAL_TIME = "run/{0}?reportId={1}"; @@ -164,6 +168,10 @@ public class ApiScenarioControllerTests extends BaseTest { private BaseFileManagementTestService baseFileManagementTestService; @Resource private ApiCommonService apiCommonService; + @Resource + private ProjectMapper projectMapper; + @Resource + private UserMapper userMapper; private static String fileMetadataId; private static String localFileId; @@ -1174,12 +1182,18 @@ public class ApiScenarioControllerTests extends BaseTest { @Order(7) public void get() throws Exception { MvcResult mvcResult = this.requestGetWithOkAndReturn(DEFAULT_GET, addApiScenario.getId()); - ApiScenarioDetail apiScenarioDetail = getResultData(mvcResult, ApiScenarioDetail.class); + ApiScenarioDetailDTO apiScenarioDetail = getResultData(mvcResult, ApiScenarioDetailDTO.class); + Assertions.assertEquals(apiScenarioDetail.getCreateUserName(), userMapper.selectByPrimaryKey(apiScenarioDetail.getCreateUser()).getName()); + Assertions.assertEquals(apiScenarioDetail.getUpdateUserName(), userMapper.selectByPrimaryKey(apiScenarioDetail.getUpdateUser()).getName()); + Assertions.assertFalse(apiScenarioDetail.getFollow()); // 验证数据 asserGetApiScenarioSteps(this.addApiScenarioSteps, apiScenarioDetail.getSteps()); + apiScenarioService.follow(anOtherAddApiScenario.getId(), "admin"); mvcResult = this.requestGetWithOkAndReturn(DEFAULT_GET, anOtherAddApiScenario.getId()); - apiScenarioDetail = getResultData(mvcResult, ApiScenarioDetail.class); + apiScenarioDetail = getResultData(mvcResult, ApiScenarioDetailDTO.class); + Assertions.assertTrue(apiScenarioDetail.getFollow()); + // 验证数据 Assertions.assertEquals(this.anOtherAddApiScenarioSteps.size(), apiScenarioDetail.getSteps().size()); // @@校验权限 @@ -1201,6 +1215,21 @@ public class ApiScenarioControllerTests extends BaseTest { requestGetPermissionTest(PermissionConstants.PROJECT_API_SCENARIO_READ, STEP_GET, addApiScenario.getId()); } + @Test + @Order(7) + public void getStepResourceProjectInfo() throws Exception { + MvcResult mvcResult = this.requestGetAndReturn(STEP_PROJECT_INFO, DEFAULT_PROJECT_ID); + Project project = getResultData(mvcResult, Project.class); + Assertions.assertEquals(project.getName(), projectMapper.selectByPrimaryKey(project.getId()).getName()); + + mvcResult = this.requestGetAndReturn(STEP_PROJECT_INFO, "tyuio"); + project = getResultData(mvcResult, Project.class); + Assertions.assertNull(project); + + // @@校验权限 + requestGetPermissionTest(PermissionConstants.PROJECT_API_SCENARIO_READ, STEP_PROJECT_INFO, DEFAULT_PROJECT_ID); + } + private void requestGetStepDetail(List steps) throws Exception { if (CollectionUtils.isEmpty(steps)) { return;