From 4b728523afb00534d093c1789189e16fa46417b6 Mon Sep 17 00:00:00 2001 From: wxg0103 <727495428@qq.com> Date: Tue, 31 Oct 2023 17:16:04 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=8E=A5=E5=8F=A3=E7=AE=A1=E7=90=86=E7=8E=AF?= =?UTF-8?q?=E5=A2=83=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --task=1013408 --user=王孝刚 接口测试-接口管理-环境列表 https://www.tapd.cn/55049933/s/1432087 --- .../main/resources/i18n/api_en_US.properties | 8 +- .../main/resources/i18n/api_zh_CN.properties | 7 +- .../main/resources/i18n/api_zh_TW.properties | 7 +- .../{ => debug}/ApiDebugModuleController.java | 10 +- .../ApiDefinitionController.java | 6 +- .../ApiEnvironmentConfigController.java | 33 +++++ .../debug/ApiDebugRequest.java | 2 +- .../{request => dto}/debug/ApiTreeNode.java | 2 +- .../debug/DebugModuleCreateRequest.java | 2 +- .../debug/DebugModuleUpdateRequest.java | 2 +- .../api/mapper/ExtApiDebugModuleMapper.java | 4 +- .../api/mapper/ExtApiDebugModuleMapper.xml | 2 +- .../{ => debug}/ApiDebugModuleLogService.java | 2 +- .../{ => debug}/ApiDebugModuleService.java | 10 +- .../ApiDefinitionLogService.java | 4 +- .../ApiDefinitionService.java | 2 +- .../ApiEnvironmentConfigLogService.java | 44 +++++++ .../ApiEnvironmentConfigService.java | 78 ++++++++++++ .../ApiDebugModuleControllerTests.java | 8 +- .../ApiEnvironmentConfigControllerTests.java | 120 ++++++++++++++++++ .../log/constants/OperationLogModule.java | 2 + 21 files changed, 324 insertions(+), 31 deletions(-) rename backend/services/api-test/src/main/java/io/metersphere/api/controller/{ => debug}/ApiDebugModuleController.java (91%) rename backend/services/api-test/src/main/java/io/metersphere/api/controller/{ => definition}/ApiDefinitionController.java (94%) create mode 100644 backend/services/api-test/src/main/java/io/metersphere/api/controller/definition/ApiEnvironmentConfigController.java rename backend/services/api-test/src/main/java/io/metersphere/api/{request => dto}/debug/ApiDebugRequest.java (94%) rename backend/services/api-test/src/main/java/io/metersphere/api/{request => dto}/debug/ApiTreeNode.java (85%) rename backend/services/api-test/src/main/java/io/metersphere/api/{request => dto}/debug/DebugModuleCreateRequest.java (97%) rename backend/services/api-test/src/main/java/io/metersphere/api/{request => dto}/debug/DebugModuleUpdateRequest.java (94%) rename backend/services/api-test/src/main/java/io/metersphere/api/service/{ => debug}/ApiDebugModuleLogService.java (99%) rename backend/services/api-test/src/main/java/io/metersphere/api/service/{ => debug}/ApiDebugModuleService.java (98%) rename backend/services/api-test/src/main/java/io/metersphere/api/service/{ => definition}/ApiDefinitionLogService.java (99%) rename backend/services/api-test/src/main/java/io/metersphere/api/service/{ => definition}/ApiDefinitionService.java (96%) create mode 100644 backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiEnvironmentConfigLogService.java create mode 100644 backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiEnvironmentConfigService.java create mode 100644 backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiEnvironmentConfigControllerTests.java diff --git a/backend/framework/sdk/src/main/resources/i18n/api_en_US.properties b/backend/framework/sdk/src/main/resources/i18n/api_en_US.properties index 80eb61456c..9dde4e28e8 100644 --- a/backend/framework/sdk/src/main/resources/i18n/api_en_US.properties +++ b/backend/framework/sdk/src/main/resources/i18n/api_en_US.properties @@ -252,6 +252,7 @@ api_scenario_report_detail.resource_id.not_blank=The unique identifier requested #module:ApiDefinitionBlob api_definition_blob.api_definition_id.not_blank=The interface fk/one-to-one relationship cannot be empty +#module: ApiDebug api_debug.id.not_blank=ID cannot be blank api_debug.id.length_range=The interface ID length must be between 1-50 api_debug.name.not_blank=Interface name cannot be blank @@ -273,4 +274,9 @@ api_debug_module.project_id.not_blank=Project ID cannot be blank api_debug_module.project_id.length_range=Project ID length must be between 1-50 api_debug_module.pos.not_blank=Module position cannot be blank api_debug_module.name.not_contain_slash=Module name cannot contain slashes -api_debug_module.unplanned_request=Unplanned request \ No newline at end of file +api_debug_module.unplanned_request=Unplanned request + +#module: ApiEnvironmentConfig +api_environment_config.id.not_blank=ID不能为空 +api_environment_config.environment_id.length_range=环境ID长度必须在1-50之间 +api_environment_config.environment_id.not_blank=环境ID不能为空 \ No newline at end of file diff --git a/backend/framework/sdk/src/main/resources/i18n/api_zh_CN.properties b/backend/framework/sdk/src/main/resources/i18n/api_zh_CN.properties index 5ef1ce1063..b704c82baa 100644 --- a/backend/framework/sdk/src/main/resources/i18n/api_zh_CN.properties +++ b/backend/framework/sdk/src/main/resources/i18n/api_zh_CN.properties @@ -274,4 +274,9 @@ api_debug_module.project_id.not_blank=项目ID不能为空 api_debug_module.project_id.length_range=项目ID长度必须在1-50之间 api_debug_module.pos.not_blank=模块位置不能为空 api_debug_module.name.not_contain_slash=名称中不能包含正反斜杠 -api_debug_module.unplanned_request=未规划请求 \ No newline at end of file +api_debug_module.unplanned_request=未规划请求 + +#module: ApiEnvironmentConfig +api_environment_config.id.not_blank=ID不能为空 +api_environment_config.environment_id.length_range=环境ID长度必须在1-50之间 +api_environment_config.environment_id.not_blank=环境ID不能为空 \ No newline at end of file diff --git a/backend/framework/sdk/src/main/resources/i18n/api_zh_TW.properties b/backend/framework/sdk/src/main/resources/i18n/api_zh_TW.properties index 053e212f7d..212d86bb71 100644 --- a/backend/framework/sdk/src/main/resources/i18n/api_zh_TW.properties +++ b/backend/framework/sdk/src/main/resources/i18n/api_zh_TW.properties @@ -274,4 +274,9 @@ api_debug_module.project_id.not_blank=項目ID不能為空 api_debug_module.project_id.length_range=項目ID長度必須在1-50之間 api_debug_module.pos.not_blank=模塊位置不能為空 api_debug_module.name.not_contain_slash=模塊名稱不能包含斜線 -api_debug_module.unplanned_request=未規劃請求 \ No newline at end of file +api_debug_module.unplanned_request=未規劃請求 + +#module: ApiEnvironmentConfig +api_environment_config.id.not_blank=ID不能為空 +api_environment_config.environment_id.length_range=環境ID長度必須在1-50之間 +api_environment_config.environment_id.not_blank=環境ID不能為空 \ No newline at end of file diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/controller/ApiDebugModuleController.java b/backend/services/api-test/src/main/java/io/metersphere/api/controller/debug/ApiDebugModuleController.java similarity index 91% rename from backend/services/api-test/src/main/java/io/metersphere/api/controller/ApiDebugModuleController.java rename to backend/services/api-test/src/main/java/io/metersphere/api/controller/debug/ApiDebugModuleController.java index 5c825899e6..ae7b5968c7 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/controller/ApiDebugModuleController.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/controller/debug/ApiDebugModuleController.java @@ -1,9 +1,9 @@ -package io.metersphere.api.controller; +package io.metersphere.api.controller.debug; -import io.metersphere.api.request.debug.ApiDebugRequest; -import io.metersphere.api.request.debug.DebugModuleCreateRequest; -import io.metersphere.api.request.debug.DebugModuleUpdateRequest; -import io.metersphere.api.service.ApiDebugModuleService; +import io.metersphere.api.dto.debug.ApiDebugRequest; +import io.metersphere.api.dto.debug.DebugModuleCreateRequest; +import io.metersphere.api.dto.debug.DebugModuleUpdateRequest; +import io.metersphere.api.service.debug.ApiDebugModuleService; import io.metersphere.sdk.constants.PermissionConstants; import io.metersphere.system.dto.sdk.BaseTreeNode; import io.metersphere.system.dto.sdk.request.NodeMoveRequest; diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/controller/ApiDefinitionController.java b/backend/services/api-test/src/main/java/io/metersphere/api/controller/definition/ApiDefinitionController.java similarity index 94% rename from backend/services/api-test/src/main/java/io/metersphere/api/controller/ApiDefinitionController.java rename to backend/services/api-test/src/main/java/io/metersphere/api/controller/definition/ApiDefinitionController.java index 159d18d2c1..6d3e4e10dd 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/controller/ApiDefinitionController.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/controller/definition/ApiDefinitionController.java @@ -1,8 +1,8 @@ -package io.metersphere.api.controller; +package io.metersphere.api.controller.definition; import io.metersphere.api.dto.definition.ApiDefinitionDTO; -import io.metersphere.api.service.ApiDefinitionLogService; -import io.metersphere.api.service.ApiDefinitionService; +import io.metersphere.api.service.definition.ApiDefinitionLogService; +import io.metersphere.api.service.definition.ApiDefinitionService; import io.metersphere.sdk.constants.PermissionConstants; import io.metersphere.system.log.annotation.Log; import io.metersphere.system.log.constants.OperationLogType; diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/controller/definition/ApiEnvironmentConfigController.java b/backend/services/api-test/src/main/java/io/metersphere/api/controller/definition/ApiEnvironmentConfigController.java new file mode 100644 index 0000000000..18341641f9 --- /dev/null +++ b/backend/services/api-test/src/main/java/io/metersphere/api/controller/definition/ApiEnvironmentConfigController.java @@ -0,0 +1,33 @@ +package io.metersphere.api.controller.definition; + +import io.metersphere.api.domain.ApiEnvironmentConfig; +import io.metersphere.api.service.definition.ApiEnvironmentConfigService; +import io.metersphere.system.utils.SessionUtils; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.annotation.Resource; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@Tag(name = "接口测试-接口管理-环境") +@RestController +@RequestMapping(value = "/api/definition/env") +public class ApiEnvironmentConfigController { + @Resource + private ApiEnvironmentConfigService apiEnvironmentConfigService; + + @GetMapping("/get/{projectId}") + @Operation(summary = "接口测试-接口管理-获取当前用户配置的环境") + public ApiEnvironmentConfig get(@PathVariable String projectId) { + return apiEnvironmentConfigService.get(SessionUtils.getUserId(), projectId); + } + + @GetMapping(value = "/add/{environmentId}") + @Operation(summary = "接口测试-接口管理-添加当前用户的环境配置") + public String add(@PathVariable String environmentId) { + return apiEnvironmentConfigService.add(environmentId, SessionUtils.getUserId(), SessionUtils.getCurrentProjectId()); + } + +} diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/request/debug/ApiDebugRequest.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/debug/ApiDebugRequest.java similarity index 94% rename from backend/services/api-test/src/main/java/io/metersphere/api/request/debug/ApiDebugRequest.java rename to backend/services/api-test/src/main/java/io/metersphere/api/dto/debug/ApiDebugRequest.java index 1189f6a454..bfb0184eb9 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/request/debug/ApiDebugRequest.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/debug/ApiDebugRequest.java @@ -1,4 +1,4 @@ -package io.metersphere.api.request.debug; +package io.metersphere.api.dto.debug; import io.metersphere.sdk.constants.ModuleConstants; import io.swagger.v3.oas.annotations.media.Schema; diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/request/debug/ApiTreeNode.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/debug/ApiTreeNode.java similarity index 85% rename from backend/services/api-test/src/main/java/io/metersphere/api/request/debug/ApiTreeNode.java rename to backend/services/api-test/src/main/java/io/metersphere/api/dto/debug/ApiTreeNode.java index 4a0e26aa9c..22a9b1542f 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/request/debug/ApiTreeNode.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/debug/ApiTreeNode.java @@ -1,4 +1,4 @@ -package io.metersphere.api.request.debug; +package io.metersphere.api.dto.debug; import io.metersphere.system.dto.sdk.BaseTreeNode; import io.swagger.v3.oas.annotations.media.Schema; diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/request/debug/DebugModuleCreateRequest.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/debug/DebugModuleCreateRequest.java similarity index 97% rename from backend/services/api-test/src/main/java/io/metersphere/api/request/debug/DebugModuleCreateRequest.java rename to backend/services/api-test/src/main/java/io/metersphere/api/dto/debug/DebugModuleCreateRequest.java index f1194345d1..021db588a8 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/request/debug/DebugModuleCreateRequest.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/debug/DebugModuleCreateRequest.java @@ -1,4 +1,4 @@ -package io.metersphere.api.request.debug; +package io.metersphere.api.dto.debug; import io.metersphere.sdk.constants.ModuleConstants; import io.metersphere.validation.groups.Created; diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/request/debug/DebugModuleUpdateRequest.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/debug/DebugModuleUpdateRequest.java similarity index 94% rename from backend/services/api-test/src/main/java/io/metersphere/api/request/debug/DebugModuleUpdateRequest.java rename to backend/services/api-test/src/main/java/io/metersphere/api/dto/debug/DebugModuleUpdateRequest.java index e4464e02ec..b099c37b6d 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/request/debug/DebugModuleUpdateRequest.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/debug/DebugModuleUpdateRequest.java @@ -1,4 +1,4 @@ -package io.metersphere.api.request.debug; +package io.metersphere.api.dto.debug; import io.swagger.v3.oas.annotations.media.Schema; import jakarta.validation.constraints.NotBlank; diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/mapper/ExtApiDebugModuleMapper.java b/backend/services/api-test/src/main/java/io/metersphere/api/mapper/ExtApiDebugModuleMapper.java index 59a640f424..cf584b8942 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/mapper/ExtApiDebugModuleMapper.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/mapper/ExtApiDebugModuleMapper.java @@ -1,8 +1,8 @@ package io.metersphere.api.mapper; import io.metersphere.api.domain.ApiDebugModule; -import io.metersphere.api.request.debug.ApiDebugRequest; -import io.metersphere.api.request.debug.ApiTreeNode; +import io.metersphere.api.dto.debug.ApiDebugRequest; +import io.metersphere.api.dto.debug.ApiTreeNode; import io.metersphere.project.dto.ModuleCountDTO; import io.metersphere.system.dto.sdk.BaseTreeNode; import org.apache.ibatis.annotations.Param; diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/mapper/ExtApiDebugModuleMapper.xml b/backend/services/api-test/src/main/java/io/metersphere/api/mapper/ExtApiDebugModuleMapper.xml index c58669fa0d..e6f90ae3a4 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/mapper/ExtApiDebugModuleMapper.xml +++ b/backend/services/api-test/src/main/java/io/metersphere/api/mapper/ExtApiDebugModuleMapper.xml @@ -77,7 +77,7 @@ desc limit 1 - SELECT id, NAME, module_id AS parentId, diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiDebugModuleLogService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/debug/ApiDebugModuleLogService.java similarity index 99% rename from backend/services/api-test/src/main/java/io/metersphere/api/service/ApiDebugModuleLogService.java rename to backend/services/api-test/src/main/java/io/metersphere/api/service/debug/ApiDebugModuleLogService.java index ac9e4a7476..3feb0e1c26 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiDebugModuleLogService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/debug/ApiDebugModuleLogService.java @@ -1,4 +1,4 @@ -package io.metersphere.api.service; +package io.metersphere.api.service.debug; import io.metersphere.api.domain.ApiDebugModule; import io.metersphere.project.domain.Project; diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiDebugModuleService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/debug/ApiDebugModuleService.java similarity index 98% rename from backend/services/api-test/src/main/java/io/metersphere/api/service/ApiDebugModuleService.java rename to backend/services/api-test/src/main/java/io/metersphere/api/service/debug/ApiDebugModuleService.java index 4550668194..b4ca615598 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiDebugModuleService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/debug/ApiDebugModuleService.java @@ -1,14 +1,14 @@ -package io.metersphere.api.service; +package io.metersphere.api.service.debug; import io.metersphere.api.domain.*; +import io.metersphere.api.dto.debug.ApiDebugRequest; +import io.metersphere.api.dto.debug.ApiTreeNode; +import io.metersphere.api.dto.debug.DebugModuleCreateRequest; +import io.metersphere.api.dto.debug.DebugModuleUpdateRequest; import io.metersphere.api.mapper.ApiDebugBlobMapper; import io.metersphere.api.mapper.ApiDebugMapper; import io.metersphere.api.mapper.ApiDebugModuleMapper; import io.metersphere.api.mapper.ExtApiDebugModuleMapper; -import io.metersphere.api.request.debug.ApiDebugRequest; -import io.metersphere.api.request.debug.ApiTreeNode; -import io.metersphere.api.request.debug.DebugModuleCreateRequest; -import io.metersphere.api.request.debug.DebugModuleUpdateRequest; import io.metersphere.project.dto.ModuleCountDTO; import io.metersphere.project.dto.NodeSortDTO; import io.metersphere.project.service.ModuleTreeService; diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiDefinitionLogService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionLogService.java similarity index 99% rename from backend/services/api-test/src/main/java/io/metersphere/api/service/ApiDefinitionLogService.java rename to backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionLogService.java index ee8ff01049..8e2d05a441 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiDefinitionLogService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionLogService.java @@ -1,14 +1,14 @@ -package io.metersphere.api.service; +package io.metersphere.api.service.definition; import io.metersphere.api.domain.ApiDefinition; import io.metersphere.api.domain.ApiDefinitionExample; import io.metersphere.api.dto.definition.ApiDefinitionDTO; import io.metersphere.api.mapper.ApiDefinitionMapper; import io.metersphere.sdk.constants.HttpMethodConstants; -import io.metersphere.system.log.dto.LogDTO; import io.metersphere.sdk.util.JSON; import io.metersphere.system.log.constants.OperationLogModule; import io.metersphere.system.log.constants.OperationLogType; +import io.metersphere.system.log.dto.LogDTO; import jakarta.annotation.Resource; import org.springframework.stereotype.Service; diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiDefinitionService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionService.java similarity index 96% rename from backend/services/api-test/src/main/java/io/metersphere/api/service/ApiDefinitionService.java rename to backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionService.java index 8df2be12d1..56318a3e51 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiDefinitionService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionService.java @@ -1,4 +1,4 @@ -package io.metersphere.api.service; +package io.metersphere.api.service.definition; import io.metersphere.api.domain.ApiDefinition; import io.metersphere.api.domain.ApiDefinitionExample; diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiEnvironmentConfigLogService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiEnvironmentConfigLogService.java new file mode 100644 index 0000000000..4d7c0e2e9e --- /dev/null +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiEnvironmentConfigLogService.java @@ -0,0 +1,44 @@ +package io.metersphere.api.service.definition; + +import io.metersphere.api.domain.ApiEnvironmentConfig; +import io.metersphere.project.domain.Project; +import io.metersphere.project.mapper.ProjectMapper; +import io.metersphere.sdk.constants.HttpMethodConstants; +import io.metersphere.sdk.domain.Environment; +import io.metersphere.sdk.mapper.EnvironmentMapper; +import io.metersphere.system.dto.builder.LogDTOBuilder; +import io.metersphere.system.log.constants.OperationLogModule; +import io.metersphere.system.log.constants.OperationLogType; +import io.metersphere.system.log.dto.LogDTO; +import io.metersphere.system.log.service.OperationLogService; +import jakarta.annotation.Resource; +import org.springframework.stereotype.Service; + +@Service +public class ApiEnvironmentConfigLogService { + + @Resource + private EnvironmentMapper environmentMapper; + @Resource + private ProjectMapper projectMapper; + @Resource + private OperationLogService operationLogService; + + public void addLog(ApiEnvironmentConfig env, String projectId) { + Project project = projectMapper.selectByPrimaryKey(projectId); + Environment environment = environmentMapper.selectByPrimaryKey(env.getEnvironmentId()); + LogDTO dto = LogDTOBuilder.builder() + .projectId(projectId) + .organizationId(project.getOrganizationId()) + .type(OperationLogType.ADD.name()) + .module(OperationLogModule.API_DEFINITION_ENVIRONMENT) + .method(HttpMethodConstants.GET.name()) + .path("/api/definition/env/add/") + .sourceId(env.getId()) + .content(environment.getName()) + .createUser(env.getCreateUser()) + .build().getLogDTO(); + operationLogService.add(dto); + } + +} diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiEnvironmentConfigService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiEnvironmentConfigService.java new file mode 100644 index 0000000000..590a60dc5d --- /dev/null +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiEnvironmentConfigService.java @@ -0,0 +1,78 @@ +package io.metersphere.api.service.definition; + +import io.metersphere.api.domain.ApiEnvironmentConfig; +import io.metersphere.api.domain.ApiEnvironmentConfigExample; +import io.metersphere.api.mapper.ApiEnvironmentConfigMapper; +import io.metersphere.sdk.domain.EnvironmentExample; +import io.metersphere.sdk.mapper.EnvironmentMapper; +import io.metersphere.system.uid.IDGenerator; +import jakarta.annotation.Resource; +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; + + +@Service +@Transactional(rollbackFor = Exception.class) +public class ApiEnvironmentConfigService { + @Resource + private ApiEnvironmentConfigMapper apiEnvironmentConfigMapper; + @Resource + private EnvironmentMapper environmentMapper; + @Resource + private ApiEnvironmentConfigLogService apiEnvironmentConfigLogService; + + + public ApiEnvironmentConfig get(String userId, String projectId) { + ApiEnvironmentConfigExample example = new ApiEnvironmentConfigExample(); + example.createCriteria().andCreateUserEqualTo(userId); + List list = apiEnvironmentConfigMapper.selectByExample(example); + + if (CollectionUtils.isNotEmpty(list)) { + EnvironmentExample environmentExample = new EnvironmentExample(); + if (StringUtils.isNotEmpty(list.get(0).getEnvironmentId())) { + environmentExample.createCriteria().andProjectIdEqualTo(projectId).andIdEqualTo(list.get(0).getEnvironmentId()); + long count = environmentMapper.countByExample(environmentExample); + if (count > 0) { + return list.get(0); + } + } + } + //为空的 默认显示mock环境 TODO + return null; + } + + public String add(String envId, String userId, String projectId) { + ApiEnvironmentConfig env = new ApiEnvironmentConfig(); + env.setId(IDGenerator.nextStr()); + env.setCreateTime(System.currentTimeMillis()); + env.setUpdateTime(System.currentTimeMillis()); + env.setEnvironmentId(envId); + env.setCreateUser(userId); + ApiEnvironmentConfig apiDefinitionEnv = this.get(userId); + if (apiDefinitionEnv == null) { + apiEnvironmentConfigMapper.insert(env); + } else { + apiDefinitionEnv.setEnvironmentId(envId); + apiDefinitionEnv.setUpdateTime(System.currentTimeMillis()); + apiEnvironmentConfigMapper.updateByPrimaryKey(apiDefinitionEnv); + return apiDefinitionEnv.getId(); + } + apiEnvironmentConfigLogService.addLog(env, projectId); + return env.getId(); + } + + public ApiEnvironmentConfig get(String userId) { + ApiEnvironmentConfigExample example = new ApiEnvironmentConfigExample(); + example.createCriteria().andCreateUserEqualTo(userId); + List list = apiEnvironmentConfigMapper.selectByExample(example); + if (CollectionUtils.isNotEmpty(list)) { + return list.get(0); + } + return null; + } + +} diff --git a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiDebugModuleControllerTests.java b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiDebugModuleControllerTests.java index e06b6c18b9..517cb81878 100644 --- a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiDebugModuleControllerTests.java +++ b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiDebugModuleControllerTests.java @@ -1,13 +1,13 @@ package io.metersphere.api.controller; import io.metersphere.api.domain.*; +import io.metersphere.api.dto.debug.ApiDebugRequest; +import io.metersphere.api.dto.debug.DebugModuleCreateRequest; +import io.metersphere.api.dto.debug.DebugModuleUpdateRequest; import io.metersphere.api.mapper.ApiDebugBlobMapper; import io.metersphere.api.mapper.ApiDebugMapper; import io.metersphere.api.mapper.ApiDebugModuleMapper; -import io.metersphere.api.request.debug.ApiDebugRequest; -import io.metersphere.api.request.debug.DebugModuleCreateRequest; -import io.metersphere.api.request.debug.DebugModuleUpdateRequest; -import io.metersphere.api.service.ApiDebugModuleService; +import io.metersphere.api.service.debug.ApiDebugModuleService; import io.metersphere.project.domain.Project; import io.metersphere.project.mapper.ProjectMapper; import io.metersphere.sdk.constants.ModuleConstants; diff --git a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiEnvironmentConfigControllerTests.java b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiEnvironmentConfigControllerTests.java new file mode 100644 index 0000000000..4d07205a8f --- /dev/null +++ b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiEnvironmentConfigControllerTests.java @@ -0,0 +1,120 @@ +package io.metersphere.api.controller; + +import io.metersphere.api.domain.ApiEnvironmentConfig; +import io.metersphere.api.domain.ApiEnvironmentConfigExample; +import io.metersphere.api.mapper.ApiEnvironmentConfigMapper; +import io.metersphere.sdk.constants.SessionConstants; +import io.metersphere.sdk.domain.Environment; +import io.metersphere.sdk.mapper.EnvironmentMapper; +import io.metersphere.sdk.util.JSON; +import io.metersphere.system.base.BaseTest; +import io.metersphere.system.controller.handler.ResultHolder; +import io.metersphere.system.log.constants.OperationLogType; +import io.metersphere.system.uid.IDGenerator; +import jakarta.annotation.Resource; +import org.junit.jupiter.api.*; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.MvcResult; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; + +import java.nio.charset.StandardCharsets; +import java.util.List; + +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + + +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +@AutoConfigureMockMvc +public class ApiEnvironmentConfigControllerTests extends BaseTest { + private final static String add = "/api/definition/env/add/"; + private final static String get = "/api/definition/env/get/"; + private static String envId; + @Resource + private MockMvc mockMvc; + @Resource + private EnvironmentMapper environmentMapper; + @Resource + private ApiEnvironmentConfigMapper apiEnvironmentConfigMapper; + private Environment environment; + + public static T parseObjectFromMvcResult(MvcResult mvcResult, Class parseClass) { + try { + String returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); + ResultHolder resultHolder = JSON.parseObject(returnData, ResultHolder.class); + //返回请求正常 + Assertions.assertNotNull(resultHolder); + return JSON.parseObject(JSON.toJSONString(resultHolder.getData()), parseClass); + } catch (Exception ignore) { + } + return null; + } + + @BeforeEach + public void initData() { + //生成环境 + if (environment == null) { + Environment env = new Environment(); + env.setId(IDGenerator.nextStr()); + env.setName("test-env-name"); + env.setProjectId(DEFAULT_PROJECT_ID); + env.setCreateUser("admin"); + env.setCreateTime(System.currentTimeMillis()); + env.setUpdateUser("admin"); + env.setUpdateTime(System.currentTimeMillis()); + environmentMapper.insertSelective(env); + environment = environmentMapper.selectByPrimaryKey(env.getId()); + } + + } + + private MvcResult responseGet(String url, String projectId) throws Exception { + return mockMvc.perform(MockMvcRequestBuilders.get(url) + .header(SessionConstants.HEADER_TOKEN, sessionId) + .header(SessionConstants.CSRF_TOKEN, csrfToken) + .header(SessionConstants.CURRENT_PROJECT, projectId) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)).andReturn(); + } + + @Test + @Order(1) + public void addDataTestSuccess() throws Exception { + this.responseGet(add + environment.getId(), DEFAULT_PROJECT_ID); + ApiEnvironmentConfigExample example = new ApiEnvironmentConfigExample(); + example.createCriteria().andCreateUserEqualTo("admin"); + List apiEnvironmentConfigs = apiEnvironmentConfigMapper.selectByExample(example); + Assertions.assertEquals(apiEnvironmentConfigs.get(0).getEnvironmentId(), environment.getId()); + envId = apiEnvironmentConfigs.get(0).getEnvironmentId(); + //检查日志 + checkLog(apiEnvironmentConfigs.get(0).getId(), OperationLogType.ADD, add); + + } + + @Test + @Order(2) + public void getDataTestFail() throws Exception { + MvcResult mvcResult = responseGet(get + DEFAULT_PROJECT_ID, DEFAULT_PROJECT_ID); + ApiEnvironmentConfig environmentConfig = parseObjectFromMvcResult(mvcResult, ApiEnvironmentConfig.class); + Assertions.assertNotNull(environmentConfig); + Assertions.assertEquals(environmentConfig.getEnvironmentId(), envId); + + mvcResult = responseGet(get + "1111111", "1111111"); + environmentConfig = parseObjectFromMvcResult(mvcResult, ApiEnvironmentConfig.class); + Assertions.assertNull(environmentConfig); + + responseGet(add + "env-1111", DEFAULT_PROJECT_ID); + ApiEnvironmentConfigExample example = new ApiEnvironmentConfigExample(); + example.createCriteria().andCreateUserEqualTo("admin"); + List apiEnvironmentConfigs = apiEnvironmentConfigMapper.selectByExample(example); + Assertions.assertEquals(apiEnvironmentConfigs.get(0).getEnvironmentId(), "env-1111"); + + } + + +} diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/log/constants/OperationLogModule.java b/backend/services/system-setting/src/main/java/io/metersphere/system/log/constants/OperationLogModule.java index e02eb4c30e..ecef49cd11 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/log/constants/OperationLogModule.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/log/constants/OperationLogModule.java @@ -100,4 +100,6 @@ public class OperationLogModule { //接口调试 public static final String API_DEBUG = "API_DEBUG"; + //接口管理-环境 + public static final String API_DEFINITION_ENVIRONMENT = "API_DEFINITION_ENVIRONMENT"; }