feat(接口测试): 接口测试获取环境详情信息

This commit is contained in:
AgAngle 2024-03-06 20:28:20 +08:00 committed by Craftsman
parent 7d960ce8d9
commit f870faefca
6 changed files with 258 additions and 162 deletions

View File

@ -6,10 +6,12 @@ import io.metersphere.api.service.ApiTestService;
import io.metersphere.jmeter.mock.Mock; import io.metersphere.jmeter.mock.Mock;
import io.metersphere.plugin.api.dto.ApiPluginSelectOption; import io.metersphere.plugin.api.dto.ApiPluginSelectOption;
import io.metersphere.project.dto.customfunction.request.CustomFunctionRunRequest; import io.metersphere.project.dto.customfunction.request.CustomFunctionRunRequest;
import io.metersphere.project.dto.environment.EnvironmentConfig;
import io.metersphere.sdk.constants.PermissionConstants; import io.metersphere.sdk.constants.PermissionConstants;
import io.metersphere.sdk.domain.Environment; import io.metersphere.sdk.domain.Environment;
import io.metersphere.sdk.dto.api.task.TaskRequestDTO; import io.metersphere.sdk.dto.api.task.TaskRequestDTO;
import io.metersphere.system.dto.ProtocolDTO; import io.metersphere.system.dto.ProtocolDTO;
import io.metersphere.system.security.CheckOwner;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
@ -36,6 +38,7 @@ public class ApiTestController {
@GetMapping("/protocol/{organizationId}") @GetMapping("/protocol/{organizationId}")
@Operation(summary = "获取协议插件的的协议列表") @Operation(summary = "获取协议插件的的协议列表")
@CheckOwner(resourceId = "#organizationId", resourceType = "organization")
public List<ProtocolDTO> getProtocols(@PathVariable String organizationId) { public List<ProtocolDTO> getProtocols(@PathVariable String organizationId) {
return apiTestService.getProtocols(organizationId); return apiTestService.getProtocols(organizationId);
} }
@ -84,7 +87,20 @@ public class ApiTestController {
PermissionConstants.PROJECT_API_DEFINITION_CASE_READ, PermissionConstants.PROJECT_API_DEFINITION_CASE_READ,
PermissionConstants.PROJECT_API_SCENARIO_READ PermissionConstants.PROJECT_API_SCENARIO_READ
}, logical = Logical.OR) }, logical = Logical.OR)
@CheckOwner(resourceId = "#projectId", resourceType = "project")
public List<Environment> getEnvList(@PathVariable String projectId) { public List<Environment> getEnvList(@PathVariable String projectId) {
return apiTestService.getEnvList(projectId); return apiTestService.getEnvList(projectId);
} }
@GetMapping("/environment/{environmentId}")
@Operation(summary = "接口测试-获取环境中数据源等参数")
@RequiresPermissions(value = {
PermissionConstants.PROJECT_API_DEFINITION_READ,
PermissionConstants.PROJECT_API_DEFINITION_CASE_READ,
PermissionConstants.PROJECT_API_SCENARIO_READ
}, logical = Logical.OR)
@CheckOwner(resourceId = "#environmentId", resourceType = "environment")
public EnvironmentConfig getEnvironmentConfig(@PathVariable String environmentId) {
return apiTestService.getEnvironmentConfig(environmentId);
}
} }

View File

@ -7,7 +7,11 @@ import io.metersphere.plugin.api.dto.ApiPluginOptionsRequest;
import io.metersphere.plugin.api.dto.ApiPluginSelectOption; import io.metersphere.plugin.api.dto.ApiPluginSelectOption;
import io.metersphere.plugin.api.spi.AbstractApiPlugin; import io.metersphere.plugin.api.spi.AbstractApiPlugin;
import io.metersphere.plugin.api.spi.AbstractProtocolPlugin; import io.metersphere.plugin.api.spi.AbstractProtocolPlugin;
import io.metersphere.project.dto.environment.EnvironmentConfig;
import io.metersphere.project.dto.environment.EnvironmentDTO;
import io.metersphere.project.dto.environment.datasource.DataSource;
import io.metersphere.project.mapper.ExtEnvironmentMapper; import io.metersphere.project.mapper.ExtEnvironmentMapper;
import io.metersphere.project.service.EnvironmentService;
import io.metersphere.sdk.domain.Environment; import io.metersphere.sdk.domain.Environment;
import io.metersphere.sdk.util.BeanUtils; import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.system.dto.ProtocolDTO; import io.metersphere.system.dto.ProtocolDTO;
@ -36,6 +40,8 @@ public class ApiTestService {
private PluginLoadService pluginLoadService; private PluginLoadService pluginLoadService;
@Resource @Resource
private ExtEnvironmentMapper extEnvironmentMapper; private ExtEnvironmentMapper extEnvironmentMapper;
@Resource
private EnvironmentService environmentService;
public List<ProtocolDTO> getProtocols(String orgId) { public List<ProtocolDTO> getProtocols(String orgId) {
List<ProtocolDTO> protocols = apiPluginService.getProtocols(orgId); List<ProtocolDTO> protocols = apiPluginService.getProtocols(orgId);
@ -81,4 +87,24 @@ public class ApiTestService {
public List<Environment> getEnvList(String projectId) { public List<Environment> getEnvList(String projectId) {
return extEnvironmentMapper.selectByKeyword(null, false, projectId); return extEnvironmentMapper.selectByKeyword(null, false, projectId);
} }
public EnvironmentConfig getEnvironmentConfig(String environmentId) {
EnvironmentConfig environmentConfig = environmentService.getEnvironmentConfig(environmentId);
// 数据脱敏
EnvironmentConfig newEnvironmentConfig = new EnvironmentConfig();
newEnvironmentConfig.setHttpConfig(environmentConfig.getHttpConfig());
newEnvironmentConfig.setCommonVariables(environmentConfig.getCommonVariables());
List<DataSource> dataSources = environmentConfig.getDataSources().stream().map(dataSource -> {
DataSource newDataSource = new DataSource();
newDataSource.setId(dataSource.getId());
newDataSource.setDataSource(dataSource.getDataSource());
return newDataSource;
}).toList();
newEnvironmentConfig.setAuthConfig(null);
newEnvironmentConfig.setPreProcessorConfig(null);
newEnvironmentConfig.setPostProcessorConfig(null);
newEnvironmentConfig.setAssertionConfig(null);
newEnvironmentConfig.setDataSources(dataSources);
return newEnvironmentConfig;
}
} }

View File

@ -17,10 +17,7 @@ import io.metersphere.api.dto.response.OperationDataInfo;
import io.metersphere.api.dto.scenario.*; import io.metersphere.api.dto.scenario.*;
import io.metersphere.api.job.ApiScenarioScheduleJob; import io.metersphere.api.job.ApiScenarioScheduleJob;
import io.metersphere.api.mapper.*; import io.metersphere.api.mapper.*;
import io.metersphere.api.service.ApiCommonService; import io.metersphere.api.service.*;
import io.metersphere.api.service.ApiScenarioBatchOperationTestService;
import io.metersphere.api.service.BaseFileManagementTestService;
import io.metersphere.api.service.BaseResourcePoolTestService;
import io.metersphere.api.service.definition.ApiDefinitionModuleService; import io.metersphere.api.service.definition.ApiDefinitionModuleService;
import io.metersphere.api.service.definition.ApiDefinitionService; import io.metersphere.api.service.definition.ApiDefinitionService;
import io.metersphere.api.service.definition.ApiTestCaseService; import io.metersphere.api.service.definition.ApiTestCaseService;
@ -28,29 +25,13 @@ import io.metersphere.api.service.scenario.ApiScenarioReportService;
import io.metersphere.api.service.scenario.ApiScenarioService; import io.metersphere.api.service.scenario.ApiScenarioService;
import io.metersphere.api.utils.ApiDataUtils; import io.metersphere.api.utils.ApiDataUtils;
import io.metersphere.plugin.api.spi.AbstractMsTestElement; import io.metersphere.plugin.api.spi.AbstractMsTestElement;
import io.metersphere.project.api.KeyValueEnableParam;
import io.metersphere.project.api.assertion.MsResponseCodeAssertion; import io.metersphere.project.api.assertion.MsResponseCodeAssertion;
import io.metersphere.project.api.assertion.MsScriptAssertion; import io.metersphere.project.api.assertion.MsScriptAssertion;
import io.metersphere.project.api.processor.MsProcessor;
import io.metersphere.project.api.processor.SQLProcessor;
import io.metersphere.project.domain.ProjectVersion; import io.metersphere.project.domain.ProjectVersion;
import io.metersphere.project.dto.environment.EnvironmentConfig;
import io.metersphere.project.dto.environment.EnvironmentGroupProjectDTO;
import io.metersphere.project.dto.environment.EnvironmentGroupRequest;
import io.metersphere.project.dto.environment.EnvironmentRequest;
import io.metersphere.project.dto.environment.datasource.DataSource;
import io.metersphere.project.dto.environment.http.HttpConfig;
import io.metersphere.project.dto.environment.http.HttpConfigPathMatchRule;
import io.metersphere.project.dto.environment.http.SelectModule;
import io.metersphere.project.dto.environment.processors.EnvProcessorConfig;
import io.metersphere.project.dto.environment.processors.EnvRequestScriptProcessor;
import io.metersphere.project.dto.environment.processors.EnvScenarioScriptProcessor;
import io.metersphere.project.dto.environment.variables.CommonVariables; import io.metersphere.project.dto.environment.variables.CommonVariables;
import io.metersphere.project.dto.filemanagement.request.FileUploadRequest; import io.metersphere.project.dto.filemanagement.request.FileUploadRequest;
import io.metersphere.project.mapper.ExtBaseProjectVersionMapper; import io.metersphere.project.mapper.ExtBaseProjectVersionMapper;
import io.metersphere.project.mapper.ProjectVersionMapper; import io.metersphere.project.mapper.ProjectVersionMapper;
import io.metersphere.project.service.EnvironmentGroupService;
import io.metersphere.project.service.EnvironmentService;
import io.metersphere.project.service.FileMetadataService; import io.metersphere.project.service.FileMetadataService;
import io.metersphere.sdk.constants.*; import io.metersphere.sdk.constants.*;
import io.metersphere.sdk.domain.Environment; import io.metersphere.sdk.domain.Environment;
@ -161,10 +142,6 @@ public class ApiScenarioControllerTests extends BaseTest {
@Resource @Resource
private ScheduleMapper scheduleMapper; private ScheduleMapper scheduleMapper;
@Resource @Resource
private EnvironmentService environmentService;
@Resource
private EnvironmentGroupService environmentGroupService;
@Resource
private PluginService pluginService; private PluginService pluginService;
@Resource @Resource
private ApiDefinitionMapper apiDefinitionMapper; private ApiDefinitionMapper apiDefinitionMapper;
@ -173,6 +150,8 @@ public class ApiScenarioControllerTests extends BaseTest {
@Resource @Resource
private ApiDefinitionModuleService apiDefinitionModuleService; private ApiDefinitionModuleService apiDefinitionModuleService;
@Resource @Resource
private BaseEnvTestService baseEnvTestService;
@Resource
private ApiScenarioReportService apiScenarioReportService; private ApiScenarioReportService apiScenarioReportService;
@Resource @Resource
private ProjectVersionMapper projectVersionMapper; private ProjectVersionMapper projectVersionMapper;
@ -320,7 +299,10 @@ public class ApiScenarioControllerTests extends BaseTest {
@Order(1) @Order(1)
public void add() throws Exception { public void add() throws Exception {
initModule(); initModule();
initEnv(); Environment environment = baseEnvTestService.initEnv(moduleId);
envId = environment.getId();
EnvironmentGroup environmentGroup = baseEnvTestService.initEnvGroup(environment);
envGroupId = environmentGroup.getId();
initTestData(); initTestData();
// @@请求成功 // @@请求成功
@ -1049,136 +1031,6 @@ public class ApiScenarioControllerTests extends BaseTest {
moduleId = apiDefinitionModuleService.add(request, "admin"); moduleId = apiDefinitionModuleService.add(request, "admin");
} }
private void initEnv() {
EnvironmentRequest envRequest = new EnvironmentRequest();
envRequest.setProjectId(DEFAULT_PROJECT_ID);
envRequest.setName("test scenario debug");
// 添加插件的环境配置供后续测试使用
Map<String, Map<String, Object>> pluginConfigMap = new HashMap<>();
pluginConfigMap.put("tcpp-sampler", new HashMap<>());
EnvScenarioScriptProcessor envScenarioScriptProcessor = new EnvScenarioScriptProcessor();
envScenarioScriptProcessor.setScript("test");
envScenarioScriptProcessor.setEnableCommonScript(false);
envScenarioScriptProcessor.setAssociateScenarioResult(true);
EnvScenarioScriptProcessor envScenarioScriptProcessor1 = BeanUtils.copyBean(new EnvScenarioScriptProcessor(), envScenarioScriptProcessor);
envScenarioScriptProcessor1.setAssociateScenarioResult(false);
SQLProcessor sqlProcessor = new SQLProcessor();
sqlProcessor.setScript("select * from test");
sqlProcessor.setName("select * from test");
EnvironmentConfig environmentConfig = new EnvironmentConfig();
DataSource dataSource = getDataSource();
environmentConfig.setDataSources(List.of(dataSource));
CommonVariables commonVariables1 = new CommonVariables();
commonVariables1.setType(VariableTypeConstants.CONSTANT.name());
commonVariables1.setKey("a");
commonVariables1.setValue("c");
CommonVariables commonVariables2 = new CommonVariables();
commonVariables2.setType(VariableTypeConstants.CONSTANT.name());
commonVariables2.setKey("q");
commonVariables2.setValue("qq");
CommonVariables commonVariables3 = new CommonVariables();
commonVariables3.setType(VariableTypeConstants.LIST.name());
commonVariables3.setKey("list1");
commonVariables3.setValue("1,2,3,5");
environmentConfig.setCommonVariables(List.of(commonVariables1, commonVariables2, commonVariables3));
EnvProcessorConfig preProcessorConfig = environmentConfig.getPreProcessorConfig();
EnvProcessorConfig postProcessorConfig = environmentConfig.getPostProcessorConfig();
List<MsProcessor> preProcessors = preProcessorConfig.getApiProcessorConfig().getScenarioProcessorConfig().getProcessors();
preProcessors.add(envScenarioScriptProcessor);
preProcessors.add(envScenarioScriptProcessor1);
preProcessors.add(sqlProcessor);
List<MsProcessor> postProcessors = postProcessorConfig.getApiProcessorConfig().getScenarioProcessorConfig().getProcessors();
postProcessors.add(envScenarioScriptProcessor);
postProcessors.add(envScenarioScriptProcessor1);
postProcessors.add(sqlProcessor);
EnvRequestScriptProcessor envRequestScriptProcessor = new EnvRequestScriptProcessor();
envRequestScriptProcessor.setScript("test");
envRequestScriptProcessor.setBeforeStepScript(true);
envRequestScriptProcessor.setIgnoreProtocols(List.of("TCP"));
EnvRequestScriptProcessor envRequestScriptProcessor1 = new EnvRequestScriptProcessor();
envRequestScriptProcessor1.setScript("test1");
envRequestScriptProcessor1.setBeforeStepScript(false);
envRequestScriptProcessor1.setIgnoreProtocols(List.of());
List<MsProcessor> preRequestProcessors = preProcessorConfig.getApiProcessorConfig().getRequestProcessorConfig().getProcessors();
preRequestProcessors.add(envRequestScriptProcessor);
preRequestProcessors.add(sqlProcessor);
List<MsProcessor> postRequestProcessors = postProcessorConfig.getApiProcessorConfig().getRequestProcessorConfig().getProcessors();
postRequestProcessors.add(envRequestScriptProcessor);
postRequestProcessors.add(sqlProcessor);
MsResponseCodeAssertion responseCodeAssertion = new MsResponseCodeAssertion();
responseCodeAssertion.setExpectedValue("200");
responseCodeAssertion.setCondition(MsAssertionCondition.EMPTY.name());
responseCodeAssertion.setName("test");
environmentConfig.getAssertionConfig().getAssertions().add(responseCodeAssertion);
KeyValueEnableParam header1 = new KeyValueEnableParam();
header1.setKey("a");
header1.setValue("aa");
KeyValueEnableParam header2 = new KeyValueEnableParam();
header2.setKey("b");
header2.setValue("bb");
KeyValueEnableParam header3 = new KeyValueEnableParam();
header3.setKey("Cookie");
header3.setValue("a=b");
HttpConfig httpNoneConfig = new HttpConfig();
httpNoneConfig.setHostname("localhost:8081");
httpNoneConfig.setType(HttpConfig.HttpConfigMatchType.NONE.name());
httpNoneConfig.setHeaders(List.of(header1, header2, header3));
HttpConfig httpModuleConfig = new HttpConfig();
httpModuleConfig.setHostname("localhost:8081");
httpModuleConfig.setType(HttpConfig.HttpConfigMatchType.MODULE.name());
SelectModule selectModule = new SelectModule();
selectModule.setModuleId(moduleId);
selectModule.setContainChildModule(true);
httpModuleConfig.getModuleMatchRule().setModules(List.of(selectModule));
httpModuleConfig.setHeaders(List.of(header1, header2, header3));
HttpConfig httpPathConfig = new HttpConfig();
httpPathConfig.setHostname("localhost:8081");
httpPathConfig.setType(HttpConfig.HttpConfigMatchType.PATH.name());
httpPathConfig.getPathMatchRule().setPath("/test");
httpPathConfig.getPathMatchRule().setCondition(HttpConfigPathMatchRule.MatchRuleCondition.CONTAINS.name());
httpPathConfig.setHeaders(List.of(header1, header2, header3));
environmentConfig.setHttpConfig(List.of(httpNoneConfig, httpModuleConfig, httpPathConfig));
environmentConfig.setPluginConfigMap(pluginConfigMap);
envRequest.setConfig(environmentConfig);
Environment environment = environmentService.add(envRequest, "admin", null);
envId = environment.getId();
EnvironmentGroupRequest groupRequest = new EnvironmentGroupRequest();
groupRequest.setProjectId(DEFAULT_PROJECT_ID);
groupRequest.setName("test scenario debug");
EnvironmentGroupProjectDTO environmentGroupProjectDTO = new EnvironmentGroupProjectDTO();
environmentGroupProjectDTO.setEnvironmentId(environment.getId());
environmentGroupProjectDTO.setProjectId(DEFAULT_PROJECT_ID);
groupRequest.setEnvGroupProject(List.of(environmentGroupProjectDTO));
envGroupId = environmentGroupService.add(groupRequest, "admin").getId();
}
private DataSource getDataSource() {
DataSource dataSource = new DataSource();
dataSource.setDataSource("test");
dataSource.setId("dataSourceId");
dataSource.setDriver("com.mysql.cj.jdbc.Driver");
dataSource.setUsername("root");
dataSource.setPassword("Password123@mysql");
dataSource.setDbUrl("jdbc:mysql://192.168.15.41:3306/metersphere");
return dataSource;
}
@Test @Test
@Order(7) @Order(7)
public void get() throws Exception { public void get() throws Exception {

View File

@ -1,12 +1,15 @@
package io.metersphere.api.controller; package io.metersphere.api.controller;
import io.metersphere.api.dto.ApiTestPluginOptionRequest; import io.metersphere.api.dto.ApiTestPluginOptionRequest;
import io.metersphere.api.service.BaseEnvTestService;
import io.metersphere.api.service.BaseResourcePoolTestService; import io.metersphere.api.service.BaseResourcePoolTestService;
import io.metersphere.plugin.api.dto.ApiPluginSelectOption; import io.metersphere.plugin.api.dto.ApiPluginSelectOption;
import io.metersphere.project.api.KeyValueParam; import io.metersphere.project.api.KeyValueParam;
import io.metersphere.project.constants.ScriptLanguageType; import io.metersphere.project.constants.ScriptLanguageType;
import io.metersphere.project.dto.customfunction.request.CustomFunctionRunRequest; import io.metersphere.project.dto.customfunction.request.CustomFunctionRunRequest;
import io.metersphere.project.dto.environment.EnvironmentConfig;
import io.metersphere.sdk.constants.PermissionConstants; import io.metersphere.sdk.constants.PermissionConstants;
import io.metersphere.sdk.domain.Environment;
import io.metersphere.system.base.BasePluginTestService; import io.metersphere.system.base.BasePluginTestService;
import io.metersphere.system.base.BaseTest; import io.metersphere.system.base.BaseTest;
import io.metersphere.system.domain.Plugin; import io.metersphere.system.domain.Plugin;
@ -44,6 +47,8 @@ public class ApiTestControllerTests extends BaseTest {
protected static final String CUSTOM_FUNC_RUN = "custom/func/run"; protected static final String CUSTOM_FUNC_RUN = "custom/func/run";
protected static final String PLUGIN_FORM_OPTION = "plugin/form/option"; protected static final String PLUGIN_FORM_OPTION = "plugin/form/option";
protected static final String PLUGIN_SCRIPT = "plugin/script/{0}"; protected static final String PLUGIN_SCRIPT = "plugin/script/{0}";
protected static final String ENV_LIST = "env-list/{0}";
protected static final String ENVIRONMENT = "environment/{0}";
@Resource @Resource
private BaseResourcePoolTestService baseResourcePoolTestService; private BaseResourcePoolTestService baseResourcePoolTestService;
@ -51,6 +56,8 @@ public class ApiTestControllerTests extends BaseTest {
private PluginService pluginService; private PluginService pluginService;
@Resource @Resource
private BasePluginTestService basePluginTestService; private BasePluginTestService basePluginTestService;
@Resource
private BaseEnvTestService baseEnvTestService;
@Override @Override
protected String getBasePath() { protected String getBasePath() {
return BASE_PATH; return BASE_PATH;
@ -183,7 +190,22 @@ public class ApiTestControllerTests extends BaseTest {
@Test @Test
public void getEnvList() throws Exception { public void getEnvList() throws Exception {
// @@请求成功 // @@请求成功
this.requestGet("/env-list/" + DEFAULT_PROJECT_ID); this.requestGet(ENV_LIST, DEFAULT_PROJECT_ID);
}
@Test
public void getEnvironmentConfig() throws Exception {
Environment environment = baseEnvTestService.initEnv("111");
MvcResult mvcResult = this.requestGetAndReturn(ENVIRONMENT, environment.getId());
EnvironmentConfig environmentConfig = getResultData(mvcResult, EnvironmentConfig.class);
Assertions.assertNull(environmentConfig.getAuthConfig());
Assertions.assertNull(environmentConfig.getPreProcessorConfig());
Assertions.assertNull(environmentConfig.getPostProcessorConfig());
Assertions.assertNull(environmentConfig.getAssertionConfig());
environmentConfig.getDataSources().forEach(dataSourceConfig -> {
Assertions.assertNull(dataSourceConfig.getPassword());
Assertions.assertNull(dataSourceConfig.getDbUrl());
});
} }
} }

View File

@ -0,0 +1,176 @@
package io.metersphere.api.service;
import io.metersphere.project.api.KeyValueEnableParam;
import io.metersphere.project.api.assertion.MsResponseCodeAssertion;
import io.metersphere.project.api.processor.MsProcessor;
import io.metersphere.project.api.processor.SQLProcessor;
import io.metersphere.project.dto.environment.EnvironmentConfig;
import io.metersphere.project.dto.environment.EnvironmentGroupProjectDTO;
import io.metersphere.project.dto.environment.EnvironmentGroupRequest;
import io.metersphere.project.dto.environment.EnvironmentRequest;
import io.metersphere.project.dto.environment.datasource.DataSource;
import io.metersphere.project.dto.environment.http.HttpConfig;
import io.metersphere.project.dto.environment.http.HttpConfigPathMatchRule;
import io.metersphere.project.dto.environment.http.SelectModule;
import io.metersphere.project.dto.environment.processors.EnvProcessorConfig;
import io.metersphere.project.dto.environment.processors.EnvRequestScriptProcessor;
import io.metersphere.project.dto.environment.processors.EnvScenarioScriptProcessor;
import io.metersphere.project.dto.environment.variables.CommonVariables;
import io.metersphere.project.service.EnvironmentGroupService;
import io.metersphere.project.service.EnvironmentService;
import io.metersphere.sdk.constants.MsAssertionCondition;
import io.metersphere.sdk.constants.VariableTypeConstants;
import io.metersphere.sdk.domain.Environment;
import io.metersphere.sdk.domain.EnvironmentGroup;
import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.system.base.BaseTest;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
/**
* @Author: jianxing
* @CreateTime: 2023-10-20 11:32
*/
@Service
public class BaseEnvTestService {
@Resource
private EnvironmentService environmentService;
@Resource
private EnvironmentGroupService environmentGroupService;
public Environment initEnv(String moduleId) {
EnvironmentRequest envRequest = new EnvironmentRequest();
envRequest.setProjectId(BaseTest.DEFAULT_PROJECT_ID);
envRequest.setName(UUID.randomUUID().toString());
// 添加插件的环境配置供后续测试使用
Map<String, Map<String, Object>> pluginConfigMap = new HashMap<>();
pluginConfigMap.put("tcpp-sampler", new HashMap<>());
EnvScenarioScriptProcessor envScenarioScriptProcessor = new EnvScenarioScriptProcessor();
envScenarioScriptProcessor.setScript("test");
envScenarioScriptProcessor.setEnableCommonScript(false);
envScenarioScriptProcessor.setAssociateScenarioResult(true);
EnvScenarioScriptProcessor envScenarioScriptProcessor1 = BeanUtils.copyBean(new EnvScenarioScriptProcessor(), envScenarioScriptProcessor);
envScenarioScriptProcessor1.setAssociateScenarioResult(false);
SQLProcessor sqlProcessor = new SQLProcessor();
sqlProcessor.setScript("select * from test");
sqlProcessor.setName("select * from test");
EnvironmentConfig environmentConfig = new EnvironmentConfig();
DataSource dataSource = getDataSource();
environmentConfig.setDataSources(List.of(dataSource));
CommonVariables commonVariables1 = new CommonVariables();
commonVariables1.setType(VariableTypeConstants.CONSTANT.name());
commonVariables1.setKey("a");
commonVariables1.setValue("c");
CommonVariables commonVariables2 = new CommonVariables();
commonVariables2.setType(VariableTypeConstants.CONSTANT.name());
commonVariables2.setKey("q");
commonVariables2.setValue("qq");
CommonVariables commonVariables3 = new CommonVariables();
commonVariables3.setType(VariableTypeConstants.LIST.name());
commonVariables3.setKey("list1");
commonVariables3.setValue("1,2,3,5");
environmentConfig.setCommonVariables(List.of(commonVariables1, commonVariables2, commonVariables3));
EnvProcessorConfig preProcessorConfig = environmentConfig.getPreProcessorConfig();
EnvProcessorConfig postProcessorConfig = environmentConfig.getPostProcessorConfig();
List<MsProcessor> preProcessors = preProcessorConfig.getApiProcessorConfig().getScenarioProcessorConfig().getProcessors();
preProcessors.add(envScenarioScriptProcessor);
preProcessors.add(envScenarioScriptProcessor1);
preProcessors.add(sqlProcessor);
List<MsProcessor> postProcessors = postProcessorConfig.getApiProcessorConfig().getScenarioProcessorConfig().getProcessors();
postProcessors.add(envScenarioScriptProcessor);
postProcessors.add(envScenarioScriptProcessor1);
postProcessors.add(sqlProcessor);
EnvRequestScriptProcessor envRequestScriptProcessor = new EnvRequestScriptProcessor();
envRequestScriptProcessor.setScript("test");
envRequestScriptProcessor.setBeforeStepScript(true);
envRequestScriptProcessor.setIgnoreProtocols(List.of("TCP"));
EnvRequestScriptProcessor envRequestScriptProcessor1 = new EnvRequestScriptProcessor();
envRequestScriptProcessor1.setScript("test1");
envRequestScriptProcessor1.setBeforeStepScript(false);
envRequestScriptProcessor1.setIgnoreProtocols(List.of());
List<MsProcessor> preRequestProcessors = preProcessorConfig.getApiProcessorConfig().getRequestProcessorConfig().getProcessors();
preRequestProcessors.add(envRequestScriptProcessor);
preRequestProcessors.add(sqlProcessor);
List<MsProcessor> postRequestProcessors = postProcessorConfig.getApiProcessorConfig().getRequestProcessorConfig().getProcessors();
postRequestProcessors.add(envRequestScriptProcessor);
postRequestProcessors.add(sqlProcessor);
MsResponseCodeAssertion responseCodeAssertion = new MsResponseCodeAssertion();
responseCodeAssertion.setExpectedValue("200");
responseCodeAssertion.setCondition(MsAssertionCondition.EMPTY.name());
responseCodeAssertion.setName("test");
environmentConfig.getAssertionConfig().getAssertions().add(responseCodeAssertion);
KeyValueEnableParam header1 = new KeyValueEnableParam();
header1.setKey("a");
header1.setValue("aa");
KeyValueEnableParam header2 = new KeyValueEnableParam();
header2.setKey("b");
header2.setValue("bb");
KeyValueEnableParam header3 = new KeyValueEnableParam();
header3.setKey("Cookie");
header3.setValue("a=b");
HttpConfig httpNoneConfig = new HttpConfig();
httpNoneConfig.setHostname("localhost:8081");
httpNoneConfig.setType(HttpConfig.HttpConfigMatchType.NONE.name());
httpNoneConfig.setHeaders(List.of(header1, header2, header3));
HttpConfig httpModuleConfig = new HttpConfig();
httpModuleConfig.setHostname("localhost:8081");
httpModuleConfig.setType(HttpConfig.HttpConfigMatchType.MODULE.name());
SelectModule selectModule = new SelectModule();
selectModule.setModuleId(moduleId);
selectModule.setContainChildModule(true);
httpModuleConfig.getModuleMatchRule().setModules(List.of(selectModule));
httpModuleConfig.setHeaders(List.of(header1, header2, header3));
HttpConfig httpPathConfig = new HttpConfig();
httpPathConfig.setHostname("localhost:8081");
httpPathConfig.setType(HttpConfig.HttpConfigMatchType.PATH.name());
httpPathConfig.getPathMatchRule().setPath("/test");
httpPathConfig.getPathMatchRule().setCondition(HttpConfigPathMatchRule.MatchRuleCondition.CONTAINS.name());
httpPathConfig.setHeaders(List.of(header1, header2, header3));
environmentConfig.setHttpConfig(List.of(httpNoneConfig, httpModuleConfig, httpPathConfig));
environmentConfig.setPluginConfigMap(pluginConfigMap);
envRequest.setConfig(environmentConfig);
return environmentService.add(envRequest, "admin", null);
}
public EnvironmentGroup initEnvGroup(Environment environment) {
EnvironmentGroupRequest groupRequest = new EnvironmentGroupRequest();
groupRequest.setProjectId(BaseTest.DEFAULT_PROJECT_ID);
groupRequest.setName(UUID.randomUUID().toString());
EnvironmentGroupProjectDTO environmentGroupProjectDTO = new EnvironmentGroupProjectDTO();
environmentGroupProjectDTO.setEnvironmentId(environment.getId());
environmentGroupProjectDTO.setProjectId(BaseTest.DEFAULT_PROJECT_ID);
groupRequest.setEnvGroupProject(List.of(environmentGroupProjectDTO));
return environmentGroupService.add(groupRequest, "admin");
}
private DataSource getDataSource() {
DataSource dataSource = new DataSource();
dataSource.setDataSource("test");
dataSource.setId("dataSourceId");
dataSource.setDriver("com.mysql.cj.jdbc.Driver");
dataSource.setUsername("root");
dataSource.setPassword("root");
dataSource.setDbUrl("jdbc:mysql://192.168.15.41:3306/metersphere");
return dataSource;
}
}

View File

@ -184,12 +184,7 @@ public class EnvironmentService {
environmentInfoDTO.setId(environment.getId()); environmentInfoDTO.setId(environment.getId());
environmentInfoDTO.setMock(environment.getMock()); environmentInfoDTO.setMock(environment.getMock());
BeanUtils.copyBean(environmentInfoDTO, environment); BeanUtils.copyBean(environmentInfoDTO, environment);
EnvironmentBlob environmentBlob = environmentBlobMapper.selectByPrimaryKey(environmentId); environmentInfoDTO.setConfig(getEnvironmentConfig(environmentId));
if (environmentBlob == null) {
environmentInfoDTO.setConfig(new EnvironmentConfig());
} else {
environmentInfoDTO.setConfig(JSON.parseObject(new String(environmentBlob.getConfig()), EnvironmentConfig.class));
}
if (BooleanUtils.isTrue(environment.getMock())) { if (BooleanUtils.isTrue(environment.getMock())) {
SystemParameterService systemParameterService = CommonBeanFactory.getBean(SystemParameterService.class); SystemParameterService systemParameterService = CommonBeanFactory.getBean(SystemParameterService.class);
if (systemParameterService != null) { if (systemParameterService != null) {
@ -205,6 +200,15 @@ public class EnvironmentService {
return environmentInfoDTO; return environmentInfoDTO;
} }
public EnvironmentConfig getEnvironmentConfig(String environmentId) {
EnvironmentBlob environmentBlob = environmentBlobMapper.selectByPrimaryKey(environmentId);
if (environmentBlob == null) {
return new EnvironmentConfig();
} else {
return JSON.parseObject(new String(environmentBlob.getConfig()), EnvironmentConfig.class);
}
}
public Long getNextOrder(String projectId) { public Long getNextOrder(String projectId) {
Long pos = extEnvironmentMapper.getPos(projectId); Long pos = extEnvironmentMapper.getPos(projectId);
return (pos == null ? 0 : pos) + ORDER_STEP; return (pos == null ? 0 : pos) + ORDER_STEP;