From d8bc177b21c4fa56f0c9c20d18258fdcefbe6138 Mon Sep 17 00:00:00 2001 From: song-tianyang Date: Thu, 5 May 2022 15:09:33 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E7=B3=BB=E7=BB=9F=E8=AE=BE=E7=BD=AE):=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BF=AE=E6=94=B9=E7=B3=BB=E7=BB=9F=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E6=97=B6=E4=BC=9A=E9=87=8D=E7=BD=AEmock=E7=8E=AF?= =?UTF-8?q?=E5=A2=83=E5=85=B6=E4=BD=99=E9=85=8D=E7=BD=AE=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1012789 --user=宋天阳 【接口测试】mock环境每次使用都会被初始化 https://www.tapd.cn/55049933/s/1152810 --- .../service/ApiTestEnvironmentService.java | 76 ++++++++++--------- 1 file changed, 42 insertions(+), 34 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/service/ApiTestEnvironmentService.java b/backend/src/main/java/io/metersphere/api/service/ApiTestEnvironmentService.java index 5bcfcf88ae..824c12418f 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiTestEnvironmentService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiTestEnvironmentService.java @@ -255,8 +255,7 @@ public class ApiTestEnvironmentService { } if (needUpdate) { String id = returnModel.getId(); - returnModel = this.genHttpApiTestEnvironmentByUrl(project,projectNumber, protocal, name, url); - returnModel.setId(id); + returnModel = this.genHttpApiTestEnvironmentByUrl(id,project,projectNumber, protocal, name, url); apiTestEnvironmentMapper.updateByPrimaryKeyWithBLOBs(returnModel); } return returnModel; @@ -266,12 +265,12 @@ public class ApiTestEnvironmentService { ProjectService projectService = CommonBeanFactory.getBean(ProjectService.class); Project project = projectService.getProjectById(projectId); if(project != null){ - return this.genHttpApiTestEnvironmentByUrl(project,projectNumber, protocal, name, baseUrl); + return this.genHttpApiTestEnvironmentByUrl(null,project,projectNumber, protocal, name, baseUrl); } return null; } - private ApiTestEnvironmentWithBLOBs genHttpApiTestEnvironmentByUrl(Project project,String projectNumber, String protocal, String name, String baseUrl) { + private ApiTestEnvironmentWithBLOBs genHttpApiTestEnvironmentByUrl(String envId,Project project,String projectNumber, String protocal, String name, String baseUrl) { if(project == null){ return null; } @@ -303,15 +302,6 @@ public class ApiTestEnvironmentService { } } - JSONObject commonConfigObj = new JSONObject(); - JSONArray commonVariablesArr = new JSONArray(); - Map commonMap = new HashMap<>(); - commonMap.put("enable", true); - commonVariablesArr.add(commonMap); - commonConfigObj.put("variables", commonVariablesArr); - commonConfigObj.put("enableHost", false); - commonConfigObj.put("hosts", new String[]{}); - JSONObject httpConfig = new JSONObject(); httpConfig.put("socket", null); httpConfig.put("isMock", true); @@ -352,32 +342,50 @@ public class ApiTestEnvironmentService { httpConfig.put("conditions", httpItemArr); httpConfig.put("defaultCondition", "NONE"); - JSONArray databaseConfigObj = new JSONArray(); - - JSONObject tcpConfigObj = new JSONObject(); - tcpConfigObj.put("classname", "TCPClientImpl"); - tcpConfigObj.put("reUseConnection", false); - tcpConfigObj.put("nodelay", false); - tcpConfigObj.put("closeConnection", false); - if(project != null){ - ProjectConfig config = projectApplicationService.getSpecificTypeValue(project.getId(), ProjectApplicationType.MOCK_TCP_PORT.name()); - Integer mockPort = config.getMockTcpPort(); - if(mockPort != null && mockPort != 0){ - tcpConfigObj.put("server", tcpSocket); - tcpConfigObj.put("port", mockPort); - } + ApiTestEnvironmentWithBLOBs blobs = null; + if(StringUtils.isNotEmpty(envId)) { + blobs = this.get(envId); } + if(blobs != null && StringUtils.isNotEmpty(blobs.getConfig())){ + JSONObject object = JSONObject.parseObject(blobs.getConfig()); + object.put("httpConfig", httpConfig); + blobs.setConfig(object.toString()); + }else { + blobs = new ApiTestEnvironmentWithBLOBs(); + JSONObject commonConfigObj = new JSONObject(); + JSONArray commonVariablesArr = new JSONArray(); + Map commonMap = new HashMap<>(); + commonMap.put("enable", true); + commonVariablesArr.add(commonMap); + commonConfigObj.put("variables", commonVariablesArr); + commonConfigObj.put("enableHost", false); + commonConfigObj.put("hosts", new String[]{}); - JSONObject object = new JSONObject(); - object.put("commonConfig", commonConfigObj); - object.put("httpConfig", httpConfig); - object.put("databaseConfigs", databaseConfigObj); - object.put("tcpConfig", tcpConfigObj); + JSONArray databaseConfigObj = new JSONArray(); - ApiTestEnvironmentWithBLOBs blobs = new ApiTestEnvironmentWithBLOBs(); + JSONObject tcpConfigObj = new JSONObject(); + tcpConfigObj.put("classname", "TCPClientImpl"); + tcpConfigObj.put("reUseConnection", false); + tcpConfigObj.put("nodelay", false); + tcpConfigObj.put("closeConnection", false); + if(project != null){ + ProjectConfig config = projectApplicationService.getSpecificTypeValue(project.getId(), ProjectApplicationType.MOCK_TCP_PORT.name()); + Integer mockPort = config.getMockTcpPort(); + if(mockPort != null && mockPort != 0){ + tcpConfigObj.put("server", tcpSocket); + tcpConfigObj.put("port", mockPort); + } + } + + JSONObject object = new JSONObject(); + object.put("commonConfig", commonConfigObj); + object.put("httpConfig", httpConfig); + object.put("databaseConfigs", databaseConfigObj); + object.put("tcpConfig", tcpConfigObj); + blobs.setConfig(object.toString()); + } blobs.setProjectId(project.getId()); blobs.setName(name); - blobs.setConfig(object.toString()); return blobs; }