diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/ApiTestCaseDTO.java b/backend/src/main/java/io/metersphere/api/dto/definition/ApiTestCaseDTO.java index efd86e96183a39b1a7944253d5a953368c9edd87..f22c26431e162ecdde6265f29c14f407389cf085 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/ApiTestCaseDTO.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/ApiTestCaseDTO.java @@ -10,6 +10,7 @@ public class ApiTestCaseDTO extends ApiTestCase { private String moduleId; private String path; private String protocol; + private String casePath; private String updateUser; private String createUser; } diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/MsTestElement.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/MsTestElement.java index 65678ddc3a986443611b9e747e42f72362691a8a..bb11fb0751ef269952b89c942209f5184a2581d7 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/MsTestElement.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/MsTestElement.java @@ -46,6 +46,7 @@ import org.apache.jorphan.collections.ListedHashTree; import java.io.ByteArrayOutputStream; import java.io.File; +import java.net.URL; import java.util.HashMap; import java.util.LinkedList; import java.util.List; @@ -286,6 +287,18 @@ public abstract class MsTestElement { } return ""; } + public boolean isURL(String str) { + try { + new URL(str); + return true; + } catch (Exception e) { + // 支持包含变量的url + if (str.matches("^(http|https|ftp)://.*$") && str.matches(".*://\\$\\{.*$")) { + return true; + } + return false; + } + } } diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsDubboSampler.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsDubboSampler.java index d84e5046118f6b90cda58d75dd5280c43020842e..8c04584a10f84132b64c05175e764c99929a817b 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsDubboSampler.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsDubboSampler.java @@ -38,7 +38,9 @@ import java.util.stream.Collectors; @EqualsAndHashCode(callSuper = true) @JSONType(typeName = "DubboSampler") public class MsDubboSampler extends MsTestElement { - /** type 必须放最前面,以便能够转换正确的类 */ + /** + * type 必须放最前面,以便能够转换正确的类 + */ private String type = "DubboSampler"; @JSONField(ordinal = 52) @@ -90,40 +92,35 @@ public class MsDubboSampler extends MsTestElement { ApiDefinitionService apiDefinitionService = CommonBeanFactory.getBean(ApiDefinitionService.class); ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + MsDubboSampler proxy = null; if (StringUtils.equals(this.getRefType(), "CASE")) { ApiTestCaseService apiTestCaseService = CommonBeanFactory.getBean(ApiTestCaseService.class); ApiTestCaseWithBLOBs bloBs = apiTestCaseService.get(this.getId()); if (bloBs != null) { this.setProjectId(bloBs.getProjectId()); - MsDubboSampler proxy = mapper.readValue(bloBs.getRequest(), new TypeReference() { + proxy = mapper.readValue(bloBs.getRequest(), new TypeReference() { }); - this.setHashTree(proxy.getHashTree()); this.setName(bloBs.getName()); - this.setMethod(proxy.getMethod()); - this.set_interface(proxy.get_interface()); - this.setAttachmentArgs(proxy.getAttachmentArgs()); - this.setArgs(proxy.getArgs()); - this.setConsumerAndService(proxy.getConsumerAndService()); - this.setRegistryCenter(proxy.getRegistryCenter()); - this.setConfigCenter(proxy.getConfigCenter()); } } else { ApiDefinitionWithBLOBs apiDefinition = apiDefinitionService.getBLOBs(this.getId()); if (apiDefinition != null) { this.setProjectId(apiDefinition.getProjectId()); - MsDubboSampler proxy = mapper.readValue(apiDefinition.getRequest(), new TypeReference() { + proxy = mapper.readValue(apiDefinition.getRequest(), new TypeReference() { }); - this.setHashTree(proxy.getHashTree()); this.setName(apiDefinition.getName()); - this.setMethod(proxy.getMethod()); - this.set_interface(proxy.get_interface()); - this.setAttachmentArgs(proxy.getAttachmentArgs()); - this.setArgs(proxy.getArgs()); - this.setConsumerAndService(proxy.getConsumerAndService()); - this.setRegistryCenter(proxy.getRegistryCenter()); - this.setConfigCenter(proxy.getConfigCenter()); } } + if (proxy != null) { + this.setHashTree(proxy.getHashTree()); + this.setMethod(proxy.getMethod()); + this.set_interface(proxy.get_interface()); + this.setAttachmentArgs(proxy.getAttachmentArgs()); + this.setArgs(proxy.getArgs()); + this.setConsumerAndService(proxy.getConsumerAndService()); + this.setRegistryCenter(proxy.getRegistryCenter()); + this.setConfigCenter(proxy.getConfigCenter()); + } } catch (Exception ex) { ex.printStackTrace(); LogUtil.error(ex.getMessage()); diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsHTTPSamplerProxy.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsHTTPSamplerProxy.java index 93bd87b009b040be7ad3644f4ce5cf81efd206da..b97272971a1d8861c3afb33d08c2fc6f5223829f 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsHTTPSamplerProxy.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsHTTPSamplerProxy.java @@ -110,36 +110,33 @@ public class MsHTTPSamplerProxy extends MsTestElement { ApiDefinitionService apiDefinitionService = CommonBeanFactory.getBean(ApiDefinitionService.class); ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + MsHTTPSamplerProxy proxy = null; if (StringUtils.equals(this.getRefType(), "CASE")) { ApiTestCaseService apiTestCaseService = CommonBeanFactory.getBean(ApiTestCaseService.class); ApiTestCaseWithBLOBs bloBs = apiTestCaseService.get(this.getId()); if (bloBs != null) { this.setProjectId(bloBs.getProjectId()); - MsHTTPSamplerProxy proxy = mapper.readValue(bloBs.getRequest(), new TypeReference() { + proxy = mapper.readValue(bloBs.getRequest(), new TypeReference() { }); - this.setHashTree(proxy.getHashTree()); this.setName(bloBs.getName()); - this.setMethod(proxy.getMethod()); - this.setBody(proxy.getBody()); - this.setRest(proxy.getRest()); - this.setArguments(proxy.getArguments()); - this.setHeaders(proxy.getHeaders()); } } else { ApiDefinitionWithBLOBs apiDefinition = apiDefinitionService.getBLOBs(this.getId()); if (apiDefinition != null) { + this.setName(apiDefinition.getName()); this.setProjectId(apiDefinition.getProjectId()); - MsHTTPSamplerProxy proxy = mapper.readValue(apiDefinition.getRequest(), new TypeReference() { + proxy = mapper.readValue(apiDefinition.getRequest(), new TypeReference() { }); - this.setHashTree(proxy.getHashTree()); - this.setName(apiDefinition.getName()); - this.setMethod(proxy.getMethod()); - this.setBody(proxy.getBody()); - this.setRest(proxy.getRest()); - this.setArguments(proxy.getArguments()); - this.setHeaders(proxy.getHeaders()); } } + if (proxy != null) { + this.setHashTree(proxy.getHashTree()); + this.setMethod(proxy.getMethod()); + this.setBody(proxy.getBody()); + this.setRest(proxy.getRest()); + this.setArguments(proxy.getArguments()); + this.setHeaders(proxy.getHeaders()); + } } catch (Exception ex) { ex.printStackTrace(); LogUtil.error(ex.getMessage()); @@ -202,7 +199,6 @@ public class MsHTTPSamplerProxy extends MsTestElement { } } } - try { if (config.isEffective(this.getProjectId())) { HttpConfig httpConfig = getHttpConfig(config.getConfig().get(this.getProjectId()).getHttpConfig(), tree); @@ -428,20 +424,6 @@ public class MsHTTPSamplerProxy extends MsTestElement { tree.add(headerManager); } } - - private boolean isURL(String str) { - try { - new URL(str); - return true; - } catch (Exception e) { - // 支持包含变量的url - if (str.matches("^(http|https|ftp)://.*$") && str.matches(".*://\\$\\{.*$")) { - return true; - } - return false; - } - } - /** * 按照环境规则匹配环境 * @@ -453,7 +435,7 @@ public class MsHTTPSamplerProxy extends MsTestElement { if (CollectionUtils.isNotEmpty(httpConfig.getConditions())) { for (HttpConfigCondition item : httpConfig.getConditions()) { if (item.getType().equals(ConditionType.PATH.name())) { - HttpConfig config = httpConfig.getPathCondition(this.getPath()); + HttpConfig config = httpConfig.getPathCondition(this.getPath(), item); if (config != null) { isNext = false; httpConfig = config; @@ -470,7 +452,7 @@ public class MsHTTPSamplerProxy extends MsTestElement { apiDefinition = apiDefinitionService.get(this.getId()); } if (apiDefinition != null) { - HttpConfig config = httpConfig.getModuleCondition(apiDefinition.getModuleId()); + HttpConfig config = httpConfig.getModuleCondition(apiDefinition.getModuleId(), item); if (config != null) { isNext = false; httpConfig = config; diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsJDBCSampler.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsJDBCSampler.java index 1969f1d5ccff10c90e0ed14f51a26806c6149c6b..cd269213510278c59739309f127a2e3a0004d841 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsJDBCSampler.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsJDBCSampler.java @@ -151,40 +151,35 @@ public class MsJDBCSampler extends MsTestElement { ApiDefinitionService apiDefinitionService = CommonBeanFactory.getBean(ApiDefinitionService.class); ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + MsJDBCSampler proxy = null; if (StringUtils.equals(this.getRefType(), "CASE")) { ApiTestCaseService apiTestCaseService = CommonBeanFactory.getBean(ApiTestCaseService.class); ApiTestCaseWithBLOBs bloBs = apiTestCaseService.get(this.getId()); if (bloBs != null) { + this.setName(bloBs.getName()); this.setProjectId(bloBs.getProjectId()); - MsJDBCSampler proxy = mapper.readValue(bloBs.getRequest(), new TypeReference() { + proxy = mapper.readValue(bloBs.getRequest(), new TypeReference() { }); - this.setHashTree(proxy.getHashTree()); - this.setName(bloBs.getName()); - this.setDataSource(proxy.getDataSource()); - this.setDataSourceId(proxy.getDataSourceId()); - this.setQuery(proxy.getQuery()); - this.setVariables(proxy.getVariables()); - this.setVariableNames(proxy.getVariableNames()); - this.setResultVariable(proxy.getResultVariable()); - this.setQueryTimeout(proxy.getQueryTimeout()); } } else { ApiDefinitionWithBLOBs apiDefinition = apiDefinitionService.getBLOBs(this.getId()); if (apiDefinition != null) { this.setProjectId(apiDefinition.getProjectId()); - MsJDBCSampler proxy = mapper.readValue(apiDefinition.getRequest(), new TypeReference() { + proxy = mapper.readValue(apiDefinition.getRequest(), new TypeReference() { }); - this.setHashTree(proxy.getHashTree()); this.setName(apiDefinition.getName()); - this.setDataSource(proxy.getDataSource()); - this.setDataSourceId(proxy.getDataSourceId()); - this.setQuery(proxy.getQuery()); - this.setVariables(proxy.getVariables()); - this.setVariableNames(proxy.getVariableNames()); - this.setResultVariable(proxy.getResultVariable()); - this.setQueryTimeout(proxy.getQueryTimeout()); } } + if (proxy != null) { + this.setHashTree(proxy.getHashTree()); + this.setDataSource(proxy.getDataSource()); + this.setDataSourceId(proxy.getDataSourceId()); + this.setQuery(proxy.getQuery()); + this.setVariables(proxy.getVariables()); + this.setVariableNames(proxy.getVariableNames()); + this.setResultVariable(proxy.getResultVariable()); + this.setQueryTimeout(proxy.getQueryTimeout()); + } } catch (Exception ex) { ex.printStackTrace(); LogUtil.error(ex.getMessage()); diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsTCPSampler.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsTCPSampler.java index b94a325d2effe2e89c81143834267d545b30aaec..36bc12d123b4a7e89c31e5dc4cbffd5c504599d4 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsTCPSampler.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsTCPSampler.java @@ -129,34 +129,32 @@ public class MsTCPSampler extends MsTestElement { ApiDefinitionService apiDefinitionService = CommonBeanFactory.getBean(ApiDefinitionService.class); ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + MsTCPSampler proxy = null; if (StringUtils.equals(this.getRefType(), "CASE")) { ApiTestCaseService apiTestCaseService = CommonBeanFactory.getBean(ApiTestCaseService.class); ApiTestCaseWithBLOBs bloBs = apiTestCaseService.get(this.getId()); if (bloBs != null) { + this.setName(bloBs.getName()); this.setProjectId(bloBs.getProjectId()); - MsTCPSampler proxy = mapper.readValue(bloBs.getRequest(), new TypeReference() { + proxy = mapper.readValue(bloBs.getRequest(), new TypeReference() { }); - this.setHashTree(proxy.getHashTree()); - this.setName(bloBs.getName()); - this.setClassname(proxy.getClassname()); - this.setServer(proxy.getServer()); - this.setPort(proxy.getPort()); - this.setRequest(proxy.getRequest()); } } else { ApiDefinitionWithBLOBs apiDefinition = apiDefinitionService.getBLOBs(this.getId()); if (apiDefinition != null) { + this.setName(apiDefinition.getName()); this.setProjectId(apiDefinition.getProjectId()); - MsTCPSampler proxy = mapper.readValue(apiDefinition.getRequest(), new TypeReference() { + proxy = mapper.readValue(apiDefinition.getRequest(), new TypeReference() { }); - this.setHashTree(proxy.getHashTree()); - this.setName(apiDefinition.getName()); - this.setClassname(proxy.getClassname()); - this.setServer(proxy.getServer()); - this.setPort(proxy.getPort()); - this.setRequest(proxy.getRequest()); } } + if (proxy != null) { + this.setHashTree(proxy.getHashTree()); + this.setClassname(proxy.getClassname()); + this.setServer(proxy.getServer()); + this.setPort(proxy.getPort()); + this.setRequest(proxy.getRequest()); + } } catch (Exception ex) { ex.printStackTrace(); LogUtil.error(ex.getMessage()); diff --git a/backend/src/main/java/io/metersphere/api/dto/scenario/HttpConfig.java b/backend/src/main/java/io/metersphere/api/dto/scenario/HttpConfig.java index 747ab135597f835d2218f58570370f554dee760d..bd96e1f1d88f5de2b1b55a2e1eacbd0ed64aaccf 100644 --- a/backend/src/main/java/io/metersphere/api/dto/scenario/HttpConfig.java +++ b/backend/src/main/java/io/metersphere/api/dto/scenario/HttpConfig.java @@ -1,6 +1,5 @@ package io.metersphere.api.dto.scenario; -import io.metersphere.commons.constants.ConditionType; import io.metersphere.commons.utils.BeanUtils; import lombok.Data; import org.apache.commons.collections.CollectionUtils; @@ -31,27 +30,21 @@ public class HttpConfig { return config; } - public HttpConfig getPathCondition(String path) { - List conditions = this.getConditions().stream().filter(condition -> ConditionType.PATH.name().equals(condition.getType()) && CollectionUtils.isNotEmpty(condition.getDetails())).collect(Collectors.toList()); - if (CollectionUtils.isNotEmpty(conditions)) { - for (HttpConfigCondition item : conditions) { - List details = item.getDetails().stream().filter(detail -> (detail.getValue().equals("contains") && StringUtils.contains(path, detail.getName())) || (detail.getValue().equals("equals") && StringUtils.equals(path, detail.getName()))).collect(Collectors.toList()); - if (CollectionUtils.isNotEmpty(details)) { - return initHttpConfig(item); - } + public HttpConfig getPathCondition(String path, HttpConfigCondition configCondition) { + if (CollectionUtils.isNotEmpty(configCondition.getDetails())) { + List details = configCondition.getDetails().stream().filter(detail -> (detail.getValue().equals("contains") && StringUtils.contains(path, detail.getName())) || (detail.getValue().equals("equals") && StringUtils.equals(path, detail.getName()))).collect(Collectors.toList()); + if (CollectionUtils.isNotEmpty(details)) { + return initHttpConfig(configCondition); } } return null; } - public HttpConfig getModuleCondition(String moduleId) { - List conditions = this.getConditions().stream().filter(condition -> ConditionType.MODULE.name().equals(condition.getType()) && CollectionUtils.isNotEmpty(condition.getDetails())).collect(Collectors.toList()); - if (CollectionUtils.isNotEmpty(conditions)) { - for (HttpConfigCondition item : conditions) { - List details = item.getDetails().stream().filter(detail -> StringUtils.contains(detail.getValue(), moduleId)).collect(Collectors.toList()); - if (CollectionUtils.isNotEmpty(details)) { - return initHttpConfig(item); - } + public HttpConfig getModuleCondition(String moduleId, HttpConfigCondition configCondition) { + if (CollectionUtils.isNotEmpty(configCondition.getDetails())) { + List details = configCondition.getDetails().stream().filter(detail -> StringUtils.contains(detail.getValue(), moduleId)).collect(Collectors.toList()); + if (CollectionUtils.isNotEmpty(details)) { + return initHttpConfig(configCondition); } } return null; diff --git a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java index 1f75f43957a483639cc4943b7fb0c9380a390ac4..5506fdda56f82fed4ab2ba2012f6c0148753639f 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java @@ -399,7 +399,7 @@ public class ApiAutomationService { http.setUrl(apiDefinition.getPath()); } if (http.isEnable()) { - if (StringUtils.isBlank(http.getUrl()) || !isURL(http.getUrl())) { + if (StringUtils.isBlank(http.getUrl()) || !tr.isURL(http.getUrl())) { env.getProjectIds().add(http.getProjectId()); env.setFullUrl(false); } @@ -430,7 +430,7 @@ public class ApiAutomationService { // 校验是否是全路径 MsHTTPSamplerProxy httpSamplerProxy = (MsHTTPSamplerProxy) tr; if (httpSamplerProxy.isEnable()) { - if (StringUtils.isBlank(httpSamplerProxy.getUrl()) || !isURL(httpSamplerProxy.getUrl())) { + if (StringUtils.isBlank(httpSamplerProxy.getUrl()) || !tr.isURL(httpSamplerProxy.getUrl())) { env.getProjectIds().add(httpSamplerProxy.getProjectId()); env.setFullUrl(false); } @@ -473,7 +473,7 @@ public class ApiAutomationService { http.setUrl(apiDefinition.getPath()); } if (http.isEnable()) { - if (StringUtils.isBlank(http.getUrl()) || !this.isURL(http.getUrl())) { + if (StringUtils.isBlank(http.getUrl()) || !tr.isURL(http.getUrl())) { env.setFullUrl(false); env.getProjectIds().add(http.getProjectId()); } @@ -504,7 +504,7 @@ public class ApiAutomationService { // 校验是否是全路径 MsHTTPSamplerProxy httpSamplerProxy = (MsHTTPSamplerProxy) tr; if (httpSamplerProxy.isEnable()) { - if (StringUtils.isBlank(httpSamplerProxy.getUrl()) || !isURL(httpSamplerProxy.getUrl())) { + if (StringUtils.isBlank(httpSamplerProxy.getUrl()) || !tr.isURL(httpSamplerProxy.getUrl())) { env.setFullUrl(false); env.getProjectIds().add(httpSamplerProxy.getProjectId()); } @@ -528,21 +528,7 @@ public class ApiAutomationService { } } - private boolean isURL(String str) { - try { - String regex = "^((https|http|ftp|rtsp|mms)?://)" - + "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" - + "(([0-9]{1,3}\\.){3}[0-9]{1,3}" + "|" + "([0-9a-z_!~*'()-]+\\.)*" - + "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\\." - + "[a-z]{2,6})" - + "(:[0-9]{1,5})?" - + "((/?)|" - + "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$"; - return str.matches(regex) || (str.matches("^(http|https|ftp)://.*$") && str.matches(".*://\\$\\{.*$")); - } catch (Exception e) { - return false; - } - } + public List getApiScenarios(List ids) { if (CollectionUtils.isNotEmpty(ids)) { @@ -857,7 +843,7 @@ public class ApiAutomationService { } /** - * 场景串行 + * 场景执行 * * @param request * @return diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiTestCaseMapper.xml b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiTestCaseMapper.xml index 5b9992c7497e6568fc0e395d5c35284e7841a002..3757db57d9f52dfe7c16f30f228f37befc24d046 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiTestCaseMapper.xml +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiTestCaseMapper.xml @@ -265,7 +265,7 @@