From 160a11f42d8189dfd8b208242a839c1fdba90ce6 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Tue, 29 Jun 2021 18:00:41 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E5=8C=96):=20=E5=AE=9A=E6=97=B6=E4=BB=BB=E5=8A=A1=E8=B0=83?= =?UTF-8?q?=E7=94=A8=E8=8A=82=E7=82=B9=E6=89=A7=E8=A1=8C=E6=97=B6=20?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E4=BF=AE=E5=A4=8D=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../metersphere/api/jmeter/JMeterService.java | 9 +- .../api/service/ApiAutomationService.java | 104 +++++++++--------- .../api/service/ApiDefinitionService.java | 11 +- .../task/ParallelScenarioExecTask.java | 2 +- .../service/task/SerialScenarioExecTask.java | 2 +- 5 files changed, 67 insertions(+), 61 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/jmeter/JMeterService.java b/backend/src/main/java/io/metersphere/api/jmeter/JMeterService.java index d2e5f6632d..2631f0226a 100644 --- a/backend/src/main/java/io/metersphere/api/jmeter/JMeterService.java +++ b/backend/src/main/java/io/metersphere/api/jmeter/JMeterService.java @@ -306,7 +306,7 @@ public class JMeterService { return multipartFiles; } - public void runTest(String testId, HashTree hashTree, String runMode, boolean isDebug, RunModeConfig config) { + public void runTest(String testId, HashTree hashTree, String runMode, boolean isDebug, String userId, RunModeConfig config) { // 获取JMX使用到的附件 List multipartFiles = getMultipartFiles(hashTree); // 获取JAR @@ -347,7 +347,12 @@ public class JMeterService { runRequest.setDebug(isDebug); runRequest.setRunMode(runMode); runRequest.setConfig(config); - runRequest.setUserId(Objects.requireNonNull(SessionUtils.getUser()).getId()); + if (SessionUtils.getUser() != null) { + runRequest.setUserId(SessionUtils.getUser().getId()); + } + if (StringUtils.isNotEmpty(userId)) { + runRequest.setUserId(userId); + } runRequest.setJmx(new MsTestPlan().getJmx(hashTree)); MultiValueMap postParameters = new LinkedMultiValueMap<>(); if (CollectionUtils.isEmpty(multipartFiles)) { 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 2b47dab229..4ba772d712 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java @@ -319,7 +319,7 @@ public class ApiAutomationService { List list = apiScenarioMapper.selectByExample(example); if (CollectionUtils.isNotEmpty(list)) { return true; - }else { + } else { return false; } } @@ -601,7 +601,7 @@ public class ApiAutomationService { } } if (http.isEnable()) { - if (StringUtils.isBlank(http.getUrl()) || (http.getIsRefEnvironment()!= null && http.getIsRefEnvironment())) { + if (StringUtils.isBlank(http.getUrl()) || (http.getIsRefEnvironment() != null && http.getIsRefEnvironment())) { env.getProjectIds().add(http.getProjectId()); env.setFullUrl(false); } @@ -633,7 +633,7 @@ public class ApiAutomationService { // 校验是否是全路径 MsHTTPSamplerProxy httpSamplerProxy = (MsHTTPSamplerProxy) tr; if (httpSamplerProxy.isEnable()) { - if (StringUtils.isBlank(httpSamplerProxy.getUrl()) || (httpSamplerProxy.getIsRefEnvironment()!= null && httpSamplerProxy.getIsRefEnvironment())) { + if (StringUtils.isBlank(httpSamplerProxy.getUrl()) || (httpSamplerProxy.getIsRefEnvironment() != null && httpSamplerProxy.getIsRefEnvironment())) { env.getProjectIds().add(httpSamplerProxy.getProjectId()); env.setFullUrl(false); } @@ -674,7 +674,7 @@ public class ApiAutomationService { http.setUrl(apiDefinition.getPath()); } if (http.isEnable()) { - if (StringUtils.isBlank(http.getUrl()) || (http.getIsRefEnvironment()!= null && http.getIsRefEnvironment())) { + if (StringUtils.isBlank(http.getUrl()) || (http.getIsRefEnvironment() != null && http.getIsRefEnvironment())) { env.setFullUrl(false); env.getProjectIds().add(http.getProjectId()); } @@ -1259,7 +1259,7 @@ public class ApiAutomationService { try { HashTree hashTree = generateHashTree(apiScenarios, request, reportIds); if (request.getConfig() != null && StringUtils.isNotBlank(request.getConfig().getResourcePoolId())) { - jMeterService.runTest(JSON.toJSONString(reportIds), hashTree, runMode, false, request.getConfig()); + jMeterService.runTest(JSON.toJSONString(reportIds), hashTree, runMode, false, request.getUserId(), request.getConfig()); } else { jMeterService.runSerial(JSON.toJSONString(reportIds), hashTree, request.getReportId(), runMode, request.getConfig()); } @@ -1590,7 +1590,7 @@ public class ApiAutomationService { ApiScenarioWithBLOBs apiScenarioWithBLOBs = new ApiScenarioWithBLOBs(); BeanUtils.copyBean(apiScenarioWithBLOBs, request); apiScenarioWithBLOBs.setUpdateTime(System.currentTimeMillis()); - if(apiScenarioWithBLOBs.getScenarioDefinition() != null){ + if (apiScenarioWithBLOBs.getScenarioDefinition() != null) { List useUrl = this.parseUrl(apiScenarioWithBLOBs); apiScenarioWithBLOBs.setUseUrl(JSONArray.toJSONString(useUrl)); } @@ -1817,11 +1817,11 @@ public class ApiAutomationService { * 1.场景中复制的接口 * 2.场景中引用/复制的案例 * 3.场景中的自定义路径与接口定义中的匹配 - * + *

* 匹配场景中用到的路径 * - * @param allScenarioInfoList 场景集合(id / scenario大字段 必须有数据) - * @param allEffectiveApiList 接口集合(id / path 必须有数据) + * @param allScenarioInfoList 场景集合(id / scenario大字段 必须有数据) + * @param allEffectiveApiList 接口集合(id / path 必须有数据) * @return */ public float countInterfaceCoverage(List allScenarioInfoList, List allEffectiveApiList) { @@ -1830,20 +1830,20 @@ public class ApiAutomationService { return 100; } - Map> urlMap = new HashMap<>(); + Map> urlMap = new HashMap<>(); for (ApiDefinition model : allEffectiveApiList) { String url = model.getPath(); String method = model.getMethod(); String id = model.getId(); - ApiMethodUrlDTO dto = new ApiMethodUrlDTO(url,method); + ApiMethodUrlDTO dto = new ApiMethodUrlDTO(url, method); - if(urlMap.containsKey(dto)){ + if (urlMap.containsKey(dto)) { urlMap.get(dto).add(id); - }else{ + } else { List list = new ArrayList<>(); list.add(id); - urlMap.put(dto,list); + urlMap.put(dto, list); } } @@ -1854,9 +1854,9 @@ public class ApiAutomationService { List urlList = new ArrayList<>(); for (ApiScenarioWithBLOBs model : allScenarioInfoList) { List useUrl = this.getScenarioUseUrl(model); - if(CollectionUtils.isNotEmpty(useUrl)){ - for (ApiMethodUrlDTO dto :useUrl) { - if(!urlList.contains(dto)){ + if (CollectionUtils.isNotEmpty(useUrl)) { + for (ApiMethodUrlDTO dto : useUrl) { + if (!urlList.contains(dto)) { urlList.add(dto); } } @@ -1877,8 +1877,8 @@ public class ApiAutomationService { } int allApiIdCount = 0; - for (List allApiIdList:urlMap.values()){ - if(CollectionUtils.isNotEmpty(allApiIdList)){ + for (List allApiIdList : urlMap.values()) { + if (CollectionUtils.isNotEmpty(allApiIdList)) { allApiIdCount += allApiIdList.size(); } } @@ -1890,8 +1890,8 @@ public class ApiAutomationService { private List getScenarioUseUrl(ApiScenarioWithBLOBs model) { List useUrlList = new ArrayList<>(); try { - useUrlList = JSONArray.parseArray(model.getUseUrl(),ApiMethodUrlDTO.class); - }catch (Exception e){ + useUrlList = JSONArray.parseArray(model.getUseUrl(), ApiMethodUrlDTO.class); + } catch (Exception e) { } return useUrlList; } @@ -1902,9 +1902,9 @@ public class ApiAutomationService { try { String scenarioDefiniton = scenario.getScenarioDefinition(); JSONObject scenarioObj = JSONObject.parseObject(scenarioDefiniton); - List stepUrlList = this.getMethodUrlDTOByHashTreeJsonObj(scenarioObj); - if(CollectionUtils.isNotEmpty(stepUrlList)){ - Collection unionList = CollectionUtils.union(urlList,stepUrlList); + List stepUrlList = this.getMethodUrlDTOByHashTreeJsonObj(scenarioObj); + if (CollectionUtils.isNotEmpty(stepUrlList)) { + Collection unionList = CollectionUtils.union(urlList, stepUrlList); urlList = new ArrayList<>(unionList); } } catch (Exception e) { @@ -1919,22 +1919,22 @@ public class ApiAutomationService { JSONArray hashArr = obj.getJSONArray("hashTree"); for (int i = 0; i < hashArr.size(); i++) { JSONObject elementObj = hashArr.getJSONObject(i); - if(elementObj == null){ + if (elementObj == null) { continue; } if (elementObj.containsKey("url") && elementObj.containsKey("method")) { String url = elementObj.getString("url"); String method = elementObj.getString("method"); - ApiMethodUrlDTO dto = new ApiMethodUrlDTO(url,method); - if(!returnList.contains(dto)){ + ApiMethodUrlDTO dto = new ApiMethodUrlDTO(url, method); + if (!returnList.contains(dto)) { returnList.add(dto); } } if (elementObj.containsKey("path") && elementObj.containsKey("method")) { String path = elementObj.getString("path"); String method = elementObj.getString("method"); - ApiMethodUrlDTO dto = new ApiMethodUrlDTO(path,method); - if(!returnList.contains(dto)){ + ApiMethodUrlDTO dto = new ApiMethodUrlDTO(path, method); + if (!returnList.contains(dto)) { returnList.add(dto); } } @@ -1942,28 +1942,28 @@ public class ApiAutomationService { if (elementObj.containsKey("id") && elementObj.containsKey("refType")) { String refType = elementObj.getString("refType"); String id = elementObj.getString("id"); - if(StringUtils.equals("CASE",refType)){ + if (StringUtils.equals("CASE", refType)) { ApiDefinition apiDefinition = apiTestCaseService.findApiUrlAndMethodById(id); - if(apiDefinition != null){ - ApiMethodUrlDTO dto = new ApiMethodUrlDTO(apiDefinition.getPath(),apiDefinition.getMethod()); - if(!returnList.contains(dto)){ + if (apiDefinition != null) { + ApiMethodUrlDTO dto = new ApiMethodUrlDTO(apiDefinition.getPath(), apiDefinition.getMethod()); + if (!returnList.contains(dto)) { returnList.add(dto); } } - }else if(StringUtils.equals("API",refType)){ + } else if (StringUtils.equals("API", refType)) { ApiDefinition apiDefinition = apiDefinitionService.selectUrlAndMethodById(id); - if(apiDefinition != null){ - ApiMethodUrlDTO dto = new ApiMethodUrlDTO(apiDefinition.getPath(),apiDefinition.getMethod()); - if(!returnList.contains(dto)){ + if (apiDefinition != null) { + ApiMethodUrlDTO dto = new ApiMethodUrlDTO(apiDefinition.getPath(), apiDefinition.getMethod()); + if (!returnList.contains(dto)) { returnList.add(dto); } } } } - List stepUrlList = this.getMethodUrlDTOByHashTreeJsonObj(elementObj); - if(CollectionUtils.isNotEmpty(stepUrlList)){ - Collection unionList = CollectionUtils.union(returnList,stepUrlList); + List stepUrlList = this.getMethodUrlDTOByHashTreeJsonObj(elementObj); + if (CollectionUtils.isNotEmpty(stepUrlList)) { + Collection unionList = CollectionUtils.union(returnList, stepUrlList); returnList = new ArrayList<>(unionList); } } @@ -1998,7 +1998,6 @@ public class ApiAutomationService { } - public ScenarioEnv getApiScenarioProjectId(String id) { ApiScenarioWithBLOBs scenario = apiScenarioMapper.selectByPrimaryKey(id); ScenarioEnv scenarioEnv = new ScenarioEnv(); @@ -2101,11 +2100,11 @@ public class ApiAutomationService { public void checkApiScenarioUseUrl() { List noUrlScenarioIdList = extApiScenarioMapper.selectIdsByUseUrlIsNull(); for (String id : noUrlScenarioIdList) { - try{ + try { ApiScenarioWithBLOBs scenario = apiScenarioMapper.selectByPrimaryKey(id); - if(scenario.getUseUrl() == null){ + if (scenario.getUseUrl() == null) { List useUrl = this.parseUrl(scenario); - if(useUrl != null){ + if (useUrl != null) { ApiScenarioWithBLOBs updateModel = new ApiScenarioWithBLOBs(); updateModel.setId(scenario.getId()); updateModel.setUseUrl(JSONArray.toJSONString(useUrl)); @@ -2114,7 +2113,7 @@ public class ApiAutomationService { } } scenario = null; - }catch (Exception e){ + } catch (Exception e) { e.printStackTrace(); } @@ -2130,8 +2129,8 @@ public class ApiAutomationService { List apiScenarioList = extApiScenarioMapper.selectIds(ids); if (CollectionUtils.isEmpty(apiScenarioList)) { return returnList; - }else { - apiScenarioList.forEach(item ->{ + } else { + apiScenarioList.forEach(item -> { String testName = item.getName(); MsTestPlan testPlan = new MsTestPlan(); testPlan.setHashTree(new LinkedList<>()); @@ -2150,10 +2149,10 @@ public class ApiAutomationService { ServiceUtils.getSelectAllIds(batchRequest, batchRequest.getCondition(), (query) -> extApiScenarioMapper.selectIdsByQuery((ApiScenarioRequest) query)); List apiScenarioList = extApiScenarioMapper.selectIds(batchRequest.getIds()); - for (ApiScenarioWithBLOBs apiModel:apiScenarioList) { + for (ApiScenarioWithBLOBs apiModel : apiScenarioList) { ApiScenarioWithBLOBs newModel = apiModel; newModel.setId(UUID.randomUUID().toString()); - newModel.setName("copy_"+apiModel.getName()); + newModel.setName("copy_" + apiModel.getName()); newModel.setCreateTime(System.currentTimeMillis()); newModel.setNum(getNextNum(newModel.getProjectId())); @@ -2162,7 +2161,7 @@ public class ApiAutomationService { andProjectIdEqualTo(newModel.getProjectId()).andStatusNotEqualTo("Trash").andIdNotEqualTo(newModel.getId()); if (apiScenarioMapper.countByExample(example) > 0) { continue; - }else { + } else { boolean insertFlag = true; if (StringUtils.isNotBlank(newModel.getCustomNum())) { insertFlag = false; @@ -2178,13 +2177,14 @@ public class ApiAutomationService { boolean isCustomNumExist = true; try { isCustomNumExist = this.isCustomNumExist(newModel); - }catch (Exception e){} + } catch (Exception e) { + } insertFlag = !isCustomNumExist; } } } - if(insertFlag){ + if (insertFlag) { apiScenarioMapper.insert(newModel); } } diff --git a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java index c50e583b49..3190285e6c 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java @@ -120,7 +120,7 @@ public class ApiDefinitionService { public List listBatch(ApiBatchRequest request) { ServiceUtils.getSelectAllIds(request, request.getCondition(), (query) -> extApiDefinitionMapper.selectIds(query)); - if(CollectionUtils.isEmpty(request.getIds())){ + if (CollectionUtils.isEmpty(request.getIds())) { return new ArrayList<>(); } List resList = extApiDefinitionMapper.listByIds(request.getIds()); @@ -279,7 +279,7 @@ public class ApiDefinitionService { if (StringUtils.equals(request.getMethod(), "ESB")) { //ESB的接口类型数据,采用TCP方式去发送。并将方法类型改为TCP。 并修改发送数据 request = esbApiParamService.handleEsbRequest(request); - }else if(StringUtils.equals(request.getMethod(), "TCP")) { + } else if (StringUtils.equals(request.getMethod(), "TCP")) { request = tcpApiParamService.handleTcpRequest(request); } final ApiDefinitionWithBLOBs test = new ApiDefinitionWithBLOBs(); @@ -586,10 +586,10 @@ public class ApiDefinitionService { } - try{ + try { //检查TCP数据结构,等其他进行处理 tcpApiParamService.checkTestElement(request.getTestElement()); - }catch (Exception e){ + } catch (Exception e) { } HashTree hashTree = request.getTestElement().generateHashTree(config); @@ -600,7 +600,7 @@ public class ApiDefinitionService { // 调用执行方法 if (request.getConfig() != null && StringUtils.isNotBlank(request.getConfig().getResourcePoolId())) { - jMeterService.runTest(request.getId(), hashTree, runMode, request.getReportId() != null, request.getConfig()); + jMeterService.runTest(request.getId(), hashTree, runMode, request.getReportId() != null, null, request.getConfig()); } else { jMeterService.runDefinition(request.getId(), hashTree, request.getReportId(), runMode); } @@ -969,6 +969,7 @@ public class ApiDefinitionService { /** * 列表开关切换 + * * @param request */ public void switchSchedule(Schedule request) { diff --git a/backend/src/main/java/io/metersphere/api/service/task/ParallelScenarioExecTask.java b/backend/src/main/java/io/metersphere/api/service/task/ParallelScenarioExecTask.java index bbbe1e257a..aac0bf9355 100644 --- a/backend/src/main/java/io/metersphere/api/service/task/ParallelScenarioExecTask.java +++ b/backend/src/main/java/io/metersphere/api/service/task/ParallelScenarioExecTask.java @@ -29,7 +29,7 @@ public class ParallelScenarioExecTask implements Callable { public T call() { try { if (request.getConfig() != null && StringUtils.isNotBlank(request.getConfig().getResourcePoolId())) { - jMeterService.runTest(id, hashTree, request.getRunMode(), false, request.getConfig()); + jMeterService.runTest(id, hashTree, request.getRunMode(), false, request.getUserId(), request.getConfig()); } else { jMeterService.runSerial(id, hashTree, request.getReportId(), request.getRunMode(), request.getConfig()); } diff --git a/backend/src/main/java/io/metersphere/api/service/task/SerialScenarioExecTask.java b/backend/src/main/java/io/metersphere/api/service/task/SerialScenarioExecTask.java index 2dec30ad69..f678109068 100644 --- a/backend/src/main/java/io/metersphere/api/service/task/SerialScenarioExecTask.java +++ b/backend/src/main/java/io/metersphere/api/service/task/SerialScenarioExecTask.java @@ -35,7 +35,7 @@ public class SerialScenarioExecTask implements Callable { public T call() { try { if (request.getConfig() != null && StringUtils.isNotBlank(request.getConfig().getResourcePoolId())) { - jMeterService.runTest(id, hashTree, request.getRunMode(), false, request.getConfig()); + jMeterService.runTest(id, hashTree, request.getRunMode(), false, request.getUserId(), request.getConfig()); } else { jMeterService.runSerial(id, hashTree, request.getReportId(), request.getRunMode(), request.getConfig()); }