From 2cd2cfe01b76add5d314ce04a4371424a17d8bfd Mon Sep 17 00:00:00 2001 From: song-tianyang Date: Thu, 2 Sep 2021 14:33:36 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E7=8E=AF=E5=A2=83=E9=85=8D=E7=BD=AE):=20#?= =?UTF-8?q?1002361=20=E7=8E=AF=E5=A2=83=E9=85=8D=E7=BD=AE=E4=B8=AD?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=85=A8=E5=B1=80=E5=89=8D/=E5=90=8E?= =?UTF-8?q?=E7=BD=AE=E8=84=9A=E6=9C=AC=E3=80=81=E8=AE=A4=E8=AF=81=E9=85=8D?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 【环境配置中增加全局前/后置脚本】https://www.tapd.cn/55049933/prong/stories/view/1155049933001002361 --- .../request/sampler/MsHTTPSamplerProxy.java | 48 +++++++++++-- .../request/sampler/MsJDBCSampler.java | 43 +++++++++++- .../request/sampler/MsTCPSampler.java | 48 ++++++++++--- .../environment/EnvironmentConfig.java | 6 ++ .../metersphere/api/tcp/server/TCPServer.java | 3 +- .../api/tcp/server/TCPServicer.java | 29 +++----- .../io/metersphere/xmind/parser/ZipUtils.java | 3 + .../environment/EnvironmentEdit.vue | 1 - .../environment/EnvironmentEdit.vue | 69 ++++++++++++++++++- frontend/src/business/components/xpack | 2 +- 10 files changed, 213 insertions(+), 39 deletions(-) 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 2745ed41ee..3680871489 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 @@ -1,6 +1,7 @@ package io.metersphere.api.dto.definition.request.sampler; import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.annotation.JSONField; import com.alibaba.fastjson.annotation.JSONType; import com.fasterxml.jackson.core.type.TypeReference; @@ -10,6 +11,8 @@ import io.metersphere.api.dto.definition.request.MsTestElement; import io.metersphere.api.dto.definition.request.ParameterConfig; import io.metersphere.api.dto.definition.request.auth.MsAuthManager; import io.metersphere.api.dto.definition.request.dns.MsDNSCacheManager; +import io.metersphere.api.dto.definition.request.processors.post.MsJSR223PostProcessor; +import io.metersphere.api.dto.definition.request.processors.pre.MsJSR223PreProcessor; import io.metersphere.api.dto.scenario.Body; import io.metersphere.api.dto.scenario.HttpConfig; import io.metersphere.api.dto.scenario.HttpConfigCondition; @@ -120,6 +123,9 @@ public class MsHTTPSamplerProxy extends MsTestElement { @JSONField(ordinal = 38) private String alias; + private MsJSR223PreProcessor preProcessor; + private MsJSR223PostProcessor postProcessor; + private void setRefElement() { try { ApiDefinitionService apiDefinitionService = CommonBeanFactory.getBean(ApiDefinitionService.class); @@ -260,6 +266,27 @@ public class MsHTTPSamplerProxy extends MsTestElement { addCertificate(config, httpSamplerTree); + //增加全局前后至脚本 + if(this.preProcessor != null){ + if (this.preProcessor.getEnvironmentId() == null) { + if (this.getEnvironmentId() == null) { + this.preProcessor.setEnvironmentId(useEnvironment); + } else { + this.preProcessor.setEnvironmentId(this.getEnvironmentId()); + } + } + this.preProcessor.toHashTree(httpSamplerTree, this.preProcessor.getHashTree(), config); + } + if(this.postProcessor != null){ + if (this.postProcessor.getEnvironmentId() == null) { + if (this.getEnvironmentId() == null) { + this.postProcessor.setEnvironmentId(useEnvironment); + } else { + this.postProcessor.setEnvironmentId(this.getEnvironmentId()); + } + } + this.postProcessor.toHashTree(httpSamplerTree, this.postProcessor.getHashTree(), config); + } if (CollectionUtils.isNotEmpty(hashTree)) { for (MsTestElement el : hashTree) { if (el.getEnvironmentId() == null) { @@ -303,11 +330,24 @@ public class MsHTTPSamplerProxy extends MsTestElement { private HttpConfig getHttpConfig(ParameterConfig config) { if (config.isEffective(this.getProjectId())) { - String useEvnId = config.getConfig().get(this.getProjectId()).getApiEnvironmentid(); - if (StringUtils.isNotEmpty(useEvnId) && !StringUtils.equals(useEvnId, this.getEnvironmentId())) { - this.setEnvironmentId(useEvnId); + EnvironmentConfig environmentConfig = config.getConfig().get(this.getProjectId()); + if (environmentConfig != null){ + String useEvnId = environmentConfig.getApiEnvironmentid(); + this.preProcessor = environmentConfig.getPreProcessor(); + this.postProcessor = environmentConfig.getPostProcessor(); + if(this.authManager == null && environmentConfig.getAuthManager() != null && environmentConfig.getAuthManager().containsKey("hashTree") ){ + try { + JSONArray jsonArray = environmentConfig.getAuthManager().getJSONArray("hashTree"); + if(jsonArray.size() > 0){ + this.authManager = jsonArray.getJSONObject(0).toJavaObject(MsAuthManager.class); + } + }catch (Exception e){} + } + if (StringUtils.isNotEmpty(useEvnId) && !StringUtils.equals(useEvnId, this.getEnvironmentId())) { + this.setEnvironmentId(useEvnId); + } + return getHttpConfig(config.getConfig().get(this.getProjectId()).getHttpConfig()); } - return getHttpConfig(config.getConfig().get(this.getProjectId()).getHttpConfig()); } return null; } 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 b53e758b1b..66eac6bc47 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 @@ -9,6 +9,8 @@ import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import io.metersphere.api.dto.definition.request.MsTestElement; import io.metersphere.api.dto.definition.request.ParameterConfig; +import io.metersphere.api.dto.definition.request.processors.post.MsJSR223PostProcessor; +import io.metersphere.api.dto.definition.request.processors.pre.MsJSR223PreProcessor; import io.metersphere.api.dto.scenario.DatabaseConfig; import io.metersphere.api.dto.scenario.KeyValue; import io.metersphere.api.dto.scenario.environment.EnvironmentConfig; @@ -68,6 +70,9 @@ public class MsJDBCSampler extends MsTestElement { @JSONField(ordinal = 30) private String useEnvironment; + private MsJSR223PreProcessor preProcessor; + private MsJSR223PostProcessor postProcessor; + @Override public void toHashTree(HashTree tree, List hashTree, ParameterConfig config) { // 非导出操作,且不是启用状态则跳过执行 @@ -111,8 +116,15 @@ public class MsJDBCSampler extends MsTestElement { } else { this.dataSource = null; // 取当前环境下默认的一个数据源 - if (config.isEffective(this.getProjectId()) && CollectionUtils.isNotEmpty(config.getConfig().get(this.getProjectId()).getDatabaseConfigs())) { - this.dataSource = config.getConfig().get(this.getProjectId()).getDatabaseConfigs().get(0); + if (config.isEffective(this.getProjectId())) { + if(config.getConfig().get(this.getProjectId()) != null){ + EnvironmentConfig envConfig = config.getConfig().get(this.getProjectId()); + this.preProcessor = envConfig.getPreProcessor(); + this.postProcessor = envConfig.getPostProcessor(); + if(CollectionUtils.isNotEmpty(envConfig.getDatabaseConfigs())){ + this.dataSource = envConfig.getDatabaseConfigs().get(0); + } + } } } @@ -132,6 +144,29 @@ public class MsJDBCSampler extends MsTestElement { if (arguments != null) { tree.add(arguments); } + + //增加全局前后至脚本 + if(this.preProcessor != null){ + if (this.preProcessor.getEnvironmentId() == null) { + if (this.getEnvironmentId() == null) { + this.preProcessor.setEnvironmentId(useEnvironment); + } else { + this.preProcessor.setEnvironmentId(this.getEnvironmentId()); + } + } + this.preProcessor.toHashTree(samplerHashTree, this.preProcessor.getHashTree(), config); + } + if(this.postProcessor != null){ + if (this.postProcessor.getEnvironmentId() == null) { + if (this.getEnvironmentId() == null) { + this.postProcessor.setEnvironmentId(useEnvironment); + } else { + this.postProcessor.setEnvironmentId(this.getEnvironmentId()); + } + } + this.postProcessor.toHashTree(samplerHashTree, this.postProcessor.getHashTree(), config); + } + if (CollectionUtils.isNotEmpty(hashTree)) { hashTree.forEach(el -> { el.toHashTree(samplerHashTree, el.getHashTree(), config); @@ -211,6 +246,10 @@ public class MsJDBCSampler extends MsTestElement { } }); } + if(envConfig != null){ + this.preProcessor = envConfig.getPreProcessor(); + this.postProcessor = envConfig.getPostProcessor(); + } } } 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 b997823d25..84c2b53bd5 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 @@ -10,6 +10,7 @@ import io.metersphere.api.dto.automation.EsbDataStruct; import io.metersphere.api.dto.automation.TcpTreeTableDataStruct; import io.metersphere.api.dto.definition.request.MsTestElement; import io.metersphere.api.dto.definition.request.ParameterConfig; +import io.metersphere.api.dto.definition.request.processors.post.MsJSR223PostProcessor; import io.metersphere.api.dto.definition.request.processors.pre.MsJSR223PreProcessor; import io.metersphere.api.dto.scenario.KeyValue; import io.metersphere.api.dto.scenario.environment.EnvironmentConfig; @@ -98,6 +99,8 @@ public class MsTCPSampler extends MsTestElement { @JSONField(ordinal = 44) private String rawDataStruct; + private MsJSR223PreProcessor preProcessor; + private MsJSR223PostProcessor postProcessor; /** * 新加两个参数,场景保存/修改时需要的参数。不会传递JMeter,只是用于最后的保留。 @@ -136,6 +139,28 @@ public class MsTCPSampler extends MsTestElement { if (tcpPreProcessor != null && StringUtils.isNotBlank(tcpPreProcessor.getScript())) { samplerHashTree.add(tcpPreProcessor.getJSR223PreProcessor()); } + + //增加全局前后至脚本 + if(this.preProcessor != null){ + if (this.preProcessor.getEnvironmentId() == null) { + if (this.getEnvironmentId() == null) { + this.preProcessor.setEnvironmentId(useEnvironment); + } else { + this.preProcessor.setEnvironmentId(this.getEnvironmentId()); + } + } + this.preProcessor.toHashTree(samplerHashTree, this.preProcessor.getHashTree(), config); + } + if(this.postProcessor != null){ + if (this.postProcessor.getEnvironmentId() == null) { + if (this.getEnvironmentId() == null) { + this.postProcessor.setEnvironmentId(useEnvironment); + } else { + this.postProcessor.setEnvironmentId(this.getEnvironmentId()); + } + } + this.postProcessor.toHashTree(samplerHashTree, this.postProcessor.getHashTree(), config); + } if (CollectionUtils.isNotEmpty(hashTree)) { hashTree.forEach(el -> { el.toHashTree(samplerHashTree, el.getHashTree(), config); @@ -181,17 +206,22 @@ public class MsTCPSampler extends MsTestElement { } private void parseEnvironment(EnvironmentConfig config) { - if (!isCustomizeReq() && config != null && config.getTcpConfig() != null) { - this.server = config.getTcpConfig().getServer(); - this.port = config.getTcpConfig().getPort(); - if (StringUtils.equals(this.eolByte, " ")) { - this.eolByte = ""; - } else { - if (StringUtils.isEmpty(this.eolByte)) { - this.eolByte = config.getTcpConfig().getEolByte(); + if(config != null){ + this.preProcessor = config.getPreProcessor(); + this.postProcessor = config.getPostProcessor(); + } + if (!isCustomizeReq() && config != null) { + if (!isCustomizeReq() && config != null) { + this.server = config.getTcpConfig().getServer(); + this.port = config.getTcpConfig().getPort(); + if (StringUtils.equals(this.eolByte, " ")) { + this.eolByte = ""; + } else { + if (StringUtils.isEmpty(this.eolByte)) { + this.eolByte = config.getTcpConfig().getEolByte(); + } } } - } } diff --git a/backend/src/main/java/io/metersphere/api/dto/scenario/environment/EnvironmentConfig.java b/backend/src/main/java/io/metersphere/api/dto/scenario/environment/EnvironmentConfig.java index 1605c75c3a..6df3d6e472 100644 --- a/backend/src/main/java/io/metersphere/api/dto/scenario/environment/EnvironmentConfig.java +++ b/backend/src/main/java/io/metersphere/api/dto/scenario/environment/EnvironmentConfig.java @@ -1,5 +1,8 @@ package io.metersphere.api.dto.scenario.environment; +import com.alibaba.fastjson.JSONObject; +import io.metersphere.api.dto.definition.request.processors.post.MsJSR223PostProcessor; +import io.metersphere.api.dto.definition.request.processors.pre.MsJSR223PreProcessor; import io.metersphere.api.dto.scenario.DatabaseConfig; import io.metersphere.api.dto.scenario.HttpConfig; import io.metersphere.api.dto.scenario.TCPConfig; @@ -17,6 +20,9 @@ public class EnvironmentConfig { private List databaseConfigs; private TCPConfig tcpConfig; private KeyStoreConfig sslConfig; + private MsJSR223PostProcessor postProcessor; + private MsJSR223PreProcessor preProcessor; + private JSONObject authManager; public EnvironmentConfig() { this.commonConfig = new CommonConfig(); diff --git a/backend/src/main/java/io/metersphere/api/tcp/server/TCPServer.java b/backend/src/main/java/io/metersphere/api/tcp/server/TCPServer.java index e77d48e5c8..561e577593 100644 --- a/backend/src/main/java/io/metersphere/api/tcp/server/TCPServer.java +++ b/backend/src/main/java/io/metersphere/api/tcp/server/TCPServer.java @@ -34,7 +34,7 @@ public class TCPServer implements Runnable { } public boolean isSocketOpen(){ - if (this.serverSocket != null && !this.serverSocket.isClosed()) { + if (this.serverSocket != null && !this.serverSocket.isClosed() &&this.servicer != null) { return true; }else { return false; @@ -55,6 +55,7 @@ public class TCPServer implements Runnable { try { this.openSocket(); } catch (Exception e) { + e.printStackTrace(); } } } diff --git a/backend/src/main/java/io/metersphere/api/tcp/server/TCPServicer.java b/backend/src/main/java/io/metersphere/api/tcp/server/TCPServicer.java index 6997893212..00ae3d2dfb 100644 --- a/backend/src/main/java/io/metersphere/api/tcp/server/TCPServicer.java +++ b/backend/src/main/java/io/metersphere/api/tcp/server/TCPServicer.java @@ -28,37 +28,30 @@ public class TCPServicer { os = s.getOutputStream(); int len = is.read(b); message = new String(b,0,len); - -// } catch (Exception e) { -// e.printStackTrace(); -// } returnMsg = this.getReturnMsg(message); - -// try { os.write(returnMsg.getBytes()); } catch (Exception e) { e.printStackTrace(); }finally { this.close(); } - - //关闭资源 -// this.close(); } private String getReturnMsg(String message) { MockConfigService mockConfigService = CommonBeanFactory.getBean(MockConfigService.class); MockExpectConfigWithBLOBs matchdMockExpect = mockConfigService.matchTcpMockExpect(message,this.port); - String response = matchdMockExpect.getResponse(); - JSONObject responseObj = JSONObject.parseObject(response); - try { - int delayed = responseObj.getInteger("delayed"); - Thread.sleep(delayed); - } catch (InterruptedException e) { - e.printStackTrace(); + String returnMsg = ""; + if(matchdMockExpect != null){ + String response = matchdMockExpect.getResponse(); + JSONObject responseObj = JSONObject.parseObject(response); + try { + int delayed = responseObj.getInteger("delayed"); + Thread.sleep(delayed); + } catch (InterruptedException e) { + e.printStackTrace(); + } + returnMsg = responseObj.getString("body"); } - String returnMsg = responseObj.getString("body"); - return returnMsg; } diff --git a/backend/src/main/java/io/metersphere/xmind/parser/ZipUtils.java b/backend/src/main/java/io/metersphere/xmind/parser/ZipUtils.java index c82f4bbe36..0841a80a12 100644 --- a/backend/src/main/java/io/metersphere/xmind/parser/ZipUtils.java +++ b/backend/src/main/java/io/metersphere/xmind/parser/ZipUtils.java @@ -79,6 +79,9 @@ public class ZipUtils { BufferedReader bufferedReder = new BufferedReader(fileReader); StringBuilder stringBuffer = new StringBuilder(); while (bufferedReder.ready()) { + if(stringBuffer.length() > 0){ + stringBuffer.append("\r\n"); + } stringBuffer.append(bufferedReder.readLine()); } // 打开的文件需关闭,在unix下可以删除,否则在windows下不能删除(file.delete()) diff --git a/frontend/src/business/components/api/definition/components/environment/EnvironmentEdit.vue b/frontend/src/business/components/api/definition/components/environment/EnvironmentEdit.vue index acf373d4f3..1555643d9f 100644 --- a/frontend/src/business/components/api/definition/components/environment/EnvironmentEdit.vue +++ b/frontend/src/business/components/api/definition/components/environment/EnvironmentEdit.vue @@ -9,7 +9,6 @@ - diff --git a/frontend/src/business/components/api/test/components/environment/EnvironmentEdit.vue b/frontend/src/business/components/api/test/components/environment/EnvironmentEdit.vue index 3dcca8dbb6..23c0dfe167 100644 --- a/frontend/src/business/components/api/test/components/environment/EnvironmentEdit.vue +++ b/frontend/src/business/components/api/test/components/environment/EnvironmentEdit.vue @@ -10,7 +10,14 @@ - + + + + + @@ -26,8 +33,20 @@ + + + + + + + {{$t('api_test.definition.request.auth_config')}} + + + -