diff --git a/backend/framework/domain/src/main/resources/migration/3.0.0/dml/V3.0.0_11_1__data.sql b/backend/framework/domain/src/main/resources/migration/3.0.0/dml/V3.0.0_11_1__data.sql index b47dea58a6..09c0b8f21c 100644 --- a/backend/framework/domain/src/main/resources/migration/3.0.0/dml/V3.0.0_11_1__data.sql +++ b/backend/framework/domain/src/main/resources/migration/3.0.0/dml/V3.0.0_11_1__data.sql @@ -637,5 +637,8 @@ INSERT INTO project_application (`project_id`, `type`, `type_value`) VALUES ('10 -- 初始化默认项目mock环境 INSERT INTO environment (`id`, `project_id`, `name`, `create_user`, `create_time`, `update_user`, `update_time`, `mock`,`pos`) VALUES (UUID_SHORT(), '100001100001', 'Mock环境', 'admin', unix_timestamp() * 1000, 'admin', unix_timestamp() * 1000, true, 5000); +INSERT INTO environment_blob (id,config) +VALUES ((SELECT id FROM environment where name = 'Mock环境') ,CONVERT('{"commonParams":{"requestTimeout":600000,"responseTimeout":600000},"commonVariables":[],"httpConfig":[{"protocol":"HTTP","hostname":"http://127.0.0.1:8081/mock-server/100001","type":"NONE","pathMatchRule":{"condition":null,"path":null},"moduleMatchRule":{"modules":[]},"headers":[],"description":null,"order":0,"moduleMatchRuleOrder":2}],"dataSources":[],"hostConfig":{"enable":null,"hosts":[]},"authConfig":{"username":null,"password":null,"verification":null,"sslConfig":{"entry":null,"files":null,"defaultAlias":null}},"preProcessorConfig":{"apiProcessorConfig":{"planProcessorConfig":{"processors":[]},"scenarioProcessorConfig":{"processors":[]},"requestProcessorConfig":{"processors":[]}}},"postProcessorConfig":{"apiProcessorConfig":{"planProcessorConfig":{"processors":[]},"scenarioProcessorConfig":{"processors":[]},"requestProcessorConfig":{"processors":[]}}},"assertionConfig":{"assertions":[]},"pluginConfigMap":{}}' USING UTF8)); + -- set innodb lock wait timeout to default SET SESSION innodb_lock_wait_timeout = DEFAULT; diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/controller/ApiTestController.java b/backend/services/api-test/src/main/java/io/metersphere/api/controller/ApiTestController.java index 3d14a0cd1c..cbea996b14 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/controller/ApiTestController.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/controller/ApiTestController.java @@ -7,6 +7,7 @@ import io.metersphere.jmeter.mock.Mock; import io.metersphere.plugin.api.dto.ApiPluginSelectOption; import io.metersphere.project.dto.customfunction.request.CustomFunctionRunRequest; import io.metersphere.sdk.constants.PermissionConstants; +import io.metersphere.sdk.domain.Environment; import io.metersphere.sdk.dto.api.task.TaskRequestDTO; import io.metersphere.system.dto.ProtocolDTO; import io.swagger.v3.oas.annotations.Operation; @@ -75,4 +76,16 @@ public class ApiTestController { public List getFormOptions(@Validated @RequestBody ApiTestPluginOptionRequest request) { return apiTestService.getFormOptions(request); } + + @GetMapping("/env-list/{projectId}") + @Operation(summary = "接口测试-环境列表") + @RequiresPermissions(value = { + PermissionConstants.PROJECT_API_DEFINITION_READ, + PermissionConstants.PROJECT_API_DEFINITION_CASE_READ, + PermissionConstants.PROJECT_API_DEBUG_READ, + PermissionConstants.PROJECT_API_SCENARIO_READ + }, logical = Logical.OR) + public List getEnvList(@PathVariable String projectId) { + return apiTestService.getEnvList(projectId); + } } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiTestService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiTestService.java index aefbdef5e4..f2254a073f 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiTestService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiTestService.java @@ -7,6 +7,8 @@ import io.metersphere.plugin.api.dto.ApiPluginOptionsRequest; import io.metersphere.plugin.api.dto.ApiPluginSelectOption; import io.metersphere.plugin.api.spi.AbstractApiPlugin; import io.metersphere.plugin.api.spi.AbstractProtocolPlugin; +import io.metersphere.project.mapper.ExtEnvironmentMapper; +import io.metersphere.sdk.domain.Environment; import io.metersphere.sdk.util.BeanUtils; import io.metersphere.system.dto.ProtocolDTO; import io.metersphere.system.service.ApiPluginService; @@ -32,6 +34,8 @@ public class ApiTestService { private ApiPluginService apiPluginService; @Resource private PluginLoadService pluginLoadService; + @Resource + private ExtEnvironmentMapper extEnvironmentMapper; public List getProtocols(String orgId) { List protocols = apiPluginService.getProtocols(orgId); @@ -73,4 +77,8 @@ public class ApiTestService { public Object checkResourceExist(PluginWrapper pluginWrapper) { return ServiceUtils.checkResourceExist(pluginWrapper, "permission.system_plugin.name"); } + + public List getEnvList(String projectId) { + return extEnvironmentMapper.selectByKeyword(null, false, projectId); + } } \ No newline at end of file diff --git a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiTestControllerTests.java b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiTestControllerTests.java index 77174bea7c..2f46fe5066 100644 --- a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiTestControllerTests.java +++ b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiTestControllerTests.java @@ -180,4 +180,10 @@ public class ApiTestControllerTests extends BaseTest { return pluginService.add(request, mockMultipartFile); } + @Test + public void getEnvList() throws Exception { + // @@请求成功 + this.requestGet("/env-list/" + DEFAULT_PROJECT_ID); + } + } diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/dto/environment/http/HttpConfig.java b/backend/services/project-management/src/main/java/io/metersphere/project/dto/environment/http/HttpConfig.java index cd76979b95..6bae21bca4 100644 --- a/backend/services/project-management/src/main/java/io/metersphere/project/dto/environment/http/HttpConfig.java +++ b/backend/services/project-management/src/main/java/io/metersphere/project/dto/environment/http/HttpConfig.java @@ -17,6 +17,8 @@ import java.util.List; public class HttpConfig implements Serializable { @Serial private static final long serialVersionUID = 1L; + @Schema(description = "id") + private String id; @Schema(description = "http协议类型(http/https)") @EnumValue(enumClass = HttpProtocolType.class) private String protocol = HttpProtocolType.HTTP.name(); diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/service/CreateEnvironmentResourceService.java b/backend/services/project-management/src/main/java/io/metersphere/project/service/CreateEnvironmentResourceService.java index 3e10e5f33e..241f3c93e5 100644 --- a/backend/services/project-management/src/main/java/io/metersphere/project/service/CreateEnvironmentResourceService.java +++ b/backend/services/project-management/src/main/java/io/metersphere/project/service/CreateEnvironmentResourceService.java @@ -1,22 +1,38 @@ package io.metersphere.project.service; import io.metersphere.project.domain.Project; +import io.metersphere.project.dto.environment.EnvironmentConfig; +import io.metersphere.project.dto.environment.http.HttpConfig; import io.metersphere.project.mapper.ProjectMapper; import io.metersphere.sdk.domain.Environment; +import io.metersphere.sdk.domain.EnvironmentBlob; import io.metersphere.sdk.domain.EnvironmentExample; +import io.metersphere.sdk.mapper.EnvironmentBlobMapper; import io.metersphere.sdk.mapper.EnvironmentMapper; +import io.metersphere.sdk.util.CommonBeanFactory; +import io.metersphere.sdk.util.JSON; +import io.metersphere.system.dto.sdk.BaseSystemConfigDTO; import io.metersphere.system.service.CreateProjectResourceService; +import io.metersphere.system.service.SystemParameterService; import io.metersphere.system.uid.IDGenerator; import jakarta.annotation.Resource; +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Component; +import java.util.ArrayList; +import java.util.List; + @Component public class CreateEnvironmentResourceService implements CreateProjectResourceService { public static final String MOCK_EVN_NAME = "Mock环境"; + private static final String MOCK_EVN_SOCKET = "/mock-server/"; @Resource private EnvironmentMapper environmentMapper; @Resource + private EnvironmentBlobMapper environmentBlobMapper; + @Resource private ProjectMapper projectMapper; @Override @@ -37,6 +53,27 @@ public class CreateEnvironmentResourceService implements CreateProjectResourceSe environment.setUpdateUser(project.getCreateUser()); environment.setUpdateTime(System.currentTimeMillis()); environmentMapper.insert(environment); + SystemParameterService systemParameterService = CommonBeanFactory.getBean(SystemParameterService.class); + if (systemParameterService != null) { + EnvironmentConfig environmentConfig = new EnvironmentConfig(); + List httpConfigs = new ArrayList<>(); + BaseSystemConfigDTO baseSystemConfigDTO = systemParameterService.getBaseInfo(); + String baseUrl = baseSystemConfigDTO.getUrl(); + if (StringUtils.isNotEmpty(baseUrl)) { + if (CollectionUtils.isEmpty(httpConfigs)) { + HttpConfig httpConfig = new HttpConfig(); + httpConfig.setHostname(StringUtils.join(baseUrl, MOCK_EVN_SOCKET, "100001")); + httpConfigs.add(httpConfig); + } + } + environmentConfig.setHttpConfig(httpConfigs); + EnvironmentBlob environmentBlob = new EnvironmentBlob(); + environmentBlob.setId(environment.getId()); + environmentBlob.setConfig(JSON.toJSONBytes(environmentConfig)); + environmentBlobMapper.insert(environmentBlob); + } } + + } } diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/service/EnvironmentGroupService.java b/backend/services/project-management/src/main/java/io/metersphere/project/service/EnvironmentGroupService.java index 943470ff3f..a883c55d18 100644 --- a/backend/services/project-management/src/main/java/io/metersphere/project/service/EnvironmentGroupService.java +++ b/backend/services/project-management/src/main/java/io/metersphere/project/service/EnvironmentGroupService.java @@ -14,16 +14,20 @@ import io.metersphere.sdk.mapper.EnvironmentGroupMapper; import io.metersphere.sdk.mapper.EnvironmentGroupRelationMapper; import io.metersphere.sdk.mapper.EnvironmentMapper; import io.metersphere.sdk.util.BeanUtils; +import io.metersphere.sdk.util.CommonBeanFactory; import io.metersphere.sdk.util.JSON; import io.metersphere.sdk.util.Translator; import io.metersphere.system.domain.UserRoleRelationExample; +import io.metersphere.system.dto.sdk.BaseSystemConfigDTO; import io.metersphere.system.dto.sdk.OptionDTO; import io.metersphere.system.dto.sdk.request.PosRequest; import io.metersphere.system.mapper.UserRoleRelationMapper; +import io.metersphere.system.service.SystemParameterService; import io.metersphere.system.uid.IDGenerator; import io.metersphere.system.utils.ServiceUtils; import jakarta.annotation.Resource; import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; import org.apache.ibatis.session.ExecutorType; @@ -60,6 +64,7 @@ public class EnvironmentGroupService { private EnvironmentService environmentService; public static final Long ORDER_STEP = 5000L; + private static final String MOCK_EVN_SOCKET = "/mock-server/"; public EnvironmentGroup add(EnvironmentGroupRequest request, String userId) { EnvironmentGroup environmentGroup = new EnvironmentGroup(); @@ -198,6 +203,17 @@ public class EnvironmentGroupService { EnvironmentConfig environmentConfig = JSON.parseObject(new String(environmentBlob.getConfig()), EnvironmentConfig.class); dto.setDomain(ObjectUtils.isNotEmpty(environmentConfig) ? environmentConfig.getHttpConfig() : new ArrayList<>()); } + if (environment !=null && BooleanUtils.isTrue(environment.getMock())) { + SystemParameterService systemParameterService = CommonBeanFactory.getBean(SystemParameterService.class); + if (systemParameterService != null) { + BaseSystemConfigDTO baseSystemConfigDTO = systemParameterService.getBaseInfo(); + String baseUrl = baseSystemConfigDTO.getUrl(); + if (StringUtils.isNotEmpty(baseUrl)) { + Project project = projectMapper.selectByPrimaryKey(environment.getProjectId()); + dto.getDomain().getFirst().setHostname(StringUtils.join(baseUrl, MOCK_EVN_SOCKET, project.getNum())); + } + } + } result.add(dto); }); BeanUtils.copyBean(environmentGroupDTO, environmentGroup); diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/service/EnvironmentService.java b/backend/services/project-management/src/main/java/io/metersphere/project/service/EnvironmentService.java index 56a8d71498..2cffdf7971 100644 --- a/backend/services/project-management/src/main/java/io/metersphere/project/service/EnvironmentService.java +++ b/backend/services/project-management/src/main/java/io/metersphere/project/service/EnvironmentService.java @@ -6,7 +6,6 @@ import io.metersphere.project.domain.Project; import io.metersphere.project.domain.ProjectExample; import io.metersphere.project.dto.environment.*; import io.metersphere.project.dto.environment.datasource.DataSource; -import io.metersphere.project.dto.environment.http.HttpConfig; import io.metersphere.project.mapper.ExtEnvironmentMapper; import io.metersphere.project.mapper.ProjectMapper; import io.metersphere.sdk.constants.DefaultRepositoryDir; @@ -189,16 +188,12 @@ public class EnvironmentService { } if (BooleanUtils.isTrue(environment.getMock())) { SystemParameterService systemParameterService = CommonBeanFactory.getBean(SystemParameterService.class); - assert systemParameterService != null; - BaseSystemConfigDTO baseSystemConfigDTO = systemParameterService.getBaseInfo(); - String baseUrl = baseSystemConfigDTO.getUrl(); - if (StringUtils.isNotEmpty(baseUrl)) { - Project project = projectMapper.selectByPrimaryKey(environment.getProjectId()); - List httpConfigs = environmentInfoDTO.getConfig().getHttpConfig(); - if (CollectionUtils.isEmpty(httpConfigs)) { - HttpConfig httpConfig = new HttpConfig(); - httpConfig.setHostname(StringUtils.join(baseUrl, MOCK_EVN_SOCKET, project.getNum())); - httpConfigs.add(new HttpConfig()); + if (systemParameterService != null) { + BaseSystemConfigDTO baseSystemConfigDTO = systemParameterService.getBaseInfo(); + String baseUrl = baseSystemConfigDTO.getUrl(); + if (StringUtils.isNotEmpty(baseUrl)) { + Project project = projectMapper.selectByPrimaryKey(environment.getProjectId()); + environmentInfoDTO.getConfig().getHttpConfig().getFirst().setHostname(StringUtils.join(baseUrl, MOCK_EVN_SOCKET, project.getNum())); } } } @@ -395,7 +390,7 @@ public class EnvironmentService { // 接口协议环境脚本 List pluginScripts = new ArrayList<>(orgApiPluginWrappers.size()); - orgApiPluginWrappers.stream().forEach(wrapper -> { + orgApiPluginWrappers.forEach(wrapper -> { Plugin plugin = wrapper.getPlugin(); if (plugin instanceof AbstractProtocolPlugin protocolPlugin) { Optional.ofNullable(pluginScriptService.get(wrapper.getPluginId(), protocolPlugin.getEnvProtocolScriptId())) @@ -435,12 +430,8 @@ public class EnvironmentService { for (Environment environment : environments) { EnvironmentInfoDTO environmentInfo = BeanUtils.copyBean(new EnvironmentInfoDTO(), environment); EnvironmentBlob environmentBlob = envBlobMap.get(environment.getId()); - if (environmentBlob == null) { - environmentInfo.setConfig(new EnvironmentConfig()); - } else { - if (environmentBlob.getConfig() != null) { - environmentInfo.setConfig(JSON.parseObject(new String(environmentBlob.getConfig()), EnvironmentConfig.class)); - } + if (environmentBlob.getConfig() != null) { + environmentInfo.setConfig(JSON.parseObject(new String(environmentBlob.getConfig()), EnvironmentConfig.class)); } if (BooleanUtils.isTrue(environment.getMock())) { @@ -500,6 +491,17 @@ public class EnvironmentService { if (environmentConfig != null && CollectionUtils.isNotEmpty(environmentConfig.getHttpConfig())) { environmentOptionsDTO.setDomain(environmentConfig.getHttpConfig()); } + if (BooleanUtils.isTrue(environment.getMock())) { + SystemParameterService systemParameterService = CommonBeanFactory.getBean(SystemParameterService.class); + if (systemParameterService != null) { + BaseSystemConfigDTO baseSystemConfigDTO = systemParameterService.getBaseInfo(); + String baseUrl = baseSystemConfigDTO.getUrl(); + if (StringUtils.isNotEmpty(baseUrl)) { + Project project = projectMapper.selectByPrimaryKey(environment.getProjectId()); + environmentOptionsDTO.getDomain().getFirst().setHostname(StringUtils.join(baseUrl, MOCK_EVN_SOCKET, project.getNum())); + } + } + } } environmentOptions.add(environmentOptionsDTO); }); diff --git a/backend/services/project-management/src/test/java/io/metersphere/project/controller/CreateEnvironmentTests.java b/backend/services/project-management/src/test/java/io/metersphere/project/controller/CreateEnvironmentTests.java index 87e0d82204..94ff4540fa 100644 --- a/backend/services/project-management/src/test/java/io/metersphere/project/controller/CreateEnvironmentTests.java +++ b/backend/services/project-management/src/test/java/io/metersphere/project/controller/CreateEnvironmentTests.java @@ -3,8 +3,8 @@ package io.metersphere.project.controller; import io.metersphere.project.domain.Project; import io.metersphere.project.mapper.ProjectMapper; import io.metersphere.project.service.CreateEnvironmentResourceService; -import io.metersphere.project.service.EnvironmentService; import io.metersphere.sdk.domain.Environment; +import io.metersphere.sdk.domain.EnvironmentBlob; import io.metersphere.sdk.domain.EnvironmentExample; import io.metersphere.sdk.mapper.EnvironmentBlobMapper; import io.metersphere.sdk.mapper.EnvironmentMapper; @@ -30,11 +30,9 @@ public class CreateEnvironmentTests extends BaseTest { @Resource private EnvironmentMapper environmentMapper; @Resource - private ProjectMapper projectMapper; - @Resource - private EnvironmentService environmentService; - @Resource private EnvironmentBlobMapper environmentBlobMapper; + @Resource + private ProjectMapper projectMapper; @Test @Order(1) @@ -57,6 +55,8 @@ public class CreateEnvironmentTests extends BaseTest { environmentExample.createCriteria().andProjectIdEqualTo(initProject.getId()).andNameEqualTo("Mock环境"); List environments = environmentMapper.selectByExample(environmentExample); assert environments.size() == 1; + EnvironmentBlob environmentBlob = environmentBlobMapper.selectByPrimaryKey(environments.get(0).getId()); + assert environmentBlob != null; } } diff --git a/backend/services/project-management/src/test/java/io/metersphere/project/controller/EnvironmentControllerTests.java b/backend/services/project-management/src/test/java/io/metersphere/project/controller/EnvironmentControllerTests.java index 80b392e1a5..4abd1d1ff3 100644 --- a/backend/services/project-management/src/test/java/io/metersphere/project/controller/EnvironmentControllerTests.java +++ b/backend/services/project-management/src/test/java/io/metersphere/project/controller/EnvironmentControllerTests.java @@ -1226,7 +1226,7 @@ public class EnvironmentControllerTests extends BaseTest { public void addCover() throws Exception { List environments = environmentMapper.selectByExample(new EnvironmentExample()); //获取id集合 过滤一下mock为false的 - List ids = environments.stream().filter(environment -> !environment.getMock()).map(Environment::getId).toList(); + List ids = environments.stream().map(Environment::getId).toList(); environmentService.getByIds(ids); environmentService.getEnvironmentBlobsByIds(List.of()); } diff --git a/backend/services/project-management/src/test/java/io/metersphere/project/controller/EnvironmentGroupControllerTests.java b/backend/services/project-management/src/test/java/io/metersphere/project/controller/EnvironmentGroupControllerTests.java index d04a2e3047..4a73b5d731 100644 --- a/backend/services/project-management/src/test/java/io/metersphere/project/controller/EnvironmentGroupControllerTests.java +++ b/backend/services/project-management/src/test/java/io/metersphere/project/controller/EnvironmentGroupControllerTests.java @@ -139,16 +139,19 @@ public class EnvironmentGroupControllerTests extends BaseTest { MvcResult mvcResult = this.requestMultipartWithOkAndReturn("/project/environment/add", paramMap); EnvironmentRequest response = parseObjectFromMvcResult(mvcResult, EnvironmentRequest.class); Assertions.assertNotNull(response); - Environment environment = environmentMapper.selectByPrimaryKey(response.getId()); //创建环境组 EnvironmentGroupRequest groupRequest = new EnvironmentGroupRequest(); groupRequest.setProjectId(DEFAULT_PROJECT_ID); groupRequest.setName("group"); - EnvironmentGroupProjectDTO environmentGroupProjectDTO = new EnvironmentGroupProjectDTO(); - environmentGroupProjectDTO.setEnvironmentId(environment.getId()); - environmentGroupProjectDTO.setProjectId(DEFAULT_PROJECT_ID); - groupRequest.setEnvGroupProject(List.of(environmentGroupProjectDTO)); + //塞mock环境 + EnvironmentGroupProjectDTO environmentGroupProjectDTO1 = new EnvironmentGroupProjectDTO(); + EnvironmentExample environmentExample = new EnvironmentExample(); + environmentExample.createCriteria().andProjectIdEqualTo(DEFAULT_PROJECT_ID).andMockEqualTo(true); + List environments = environmentMapper.selectByExample(environmentExample); + environmentGroupProjectDTO1.setEnvironmentId(environments.getFirst().getId()); + environmentGroupProjectDTO1.setProjectId(DEFAULT_PROJECT_ID); + groupRequest.setEnvGroupProject(List.of(environmentGroupProjectDTO1)); mvcResult = this.responsePost(add, groupRequest); EnvironmentGroup groupResponse = parseObjectFromMvcResult(mvcResult, EnvironmentGroup.class); Assertions.assertNotNull(groupResponse);