feat(接口测试): 增加接口管理环境接口

--task=1013408 --user=王孝刚 接口测试-接口管理-环境列表
https://www.tapd.cn/55049933/s/1432087
This commit is contained in:
wxg0103 2023-10-31 17:16:04 +08:00 committed by Craftsman
parent 1fc5f76593
commit 4b728523af
21 changed files with 324 additions and 31 deletions

View File

@ -252,6 +252,7 @@ api_scenario_report_detail.resource_id.not_blank=The unique identifier requested
#moduleApiDefinitionBlob
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
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不能为空

View File

@ -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=未规划请求
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不能为空

View File

@ -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=未規劃請求
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不能為空

View File

@ -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;

View File

@ -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;

View File

@ -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());
}
}

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -77,7 +77,7 @@
desc
limit 1
</select>
<select id="selectApiDebugByProtocolAndUser" resultType="io.metersphere.api.request.debug.ApiTreeNode">
<select id="selectApiDebugByProtocolAndUser" resultType="io.metersphere.api.dto.debug.ApiTreeNode">
SELECT id,
NAME,
module_id AS parentId,

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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);
}
}

View File

@ -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<ApiEnvironmentConfig> 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<ApiEnvironmentConfig> list = apiEnvironmentConfigMapper.selectByExample(example);
if (CollectionUtils.isNotEmpty(list)) {
return list.get(0);
}
return null;
}
}

View File

@ -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;

View File

@ -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> T parseObjectFromMvcResult(MvcResult mvcResult, Class<T> 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<ApiEnvironmentConfig> 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<ApiEnvironmentConfig> apiEnvironmentConfigs = apiEnvironmentConfigMapper.selectByExample(example);
Assertions.assertEquals(apiEnvironmentConfigs.get(0).getEnvironmentId(), "env-1111");
}
}

View File

@ -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";
}