diff --git a/backend/src/main/java/io/metersphere/api/controller/ApiTestCaseController.java b/backend/src/main/java/io/metersphere/api/controller/ApiTestCaseController.java index 9580937882..689588aa63 100644 --- a/backend/src/main/java/io/metersphere/api/controller/ApiTestCaseController.java +++ b/backend/src/main/java/io/metersphere/api/controller/ApiTestCaseController.java @@ -196,7 +196,7 @@ public class ApiTestCaseController { @PostMapping(value = "/jenkins/run") @MsAuditLog(module = "api_definition_case", type = OperLogConstants.EXECUTE, content = "#msClass.getLogDetails(#request.caseId)", msClass = ApiTestCaseService.class) public String jenkinsRun(@RequestBody RunCaseRequest request) { - return apiTestCaseService.run(request); + return apiTestCaseService.jenkinsRun(request); } @GetMapping(value = "/jenkins/exec/result/{id}") diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/MsScenario.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/MsScenario.java index da5018dedf..3da52f26a7 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/MsScenario.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/MsScenario.java @@ -121,10 +121,8 @@ public class MsScenario extends MsTestElement { }); this.setHeaders(headers); } - if (element.get("environmentMap") != null) { - this.setEnvironmentMap((Map) element.get("environmentMap")); - } this.setHashTree(sourceHashTree); + hashTree = sourceHashTree; } } catch (Exception ex) { diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/assertions/MsAssertions.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/assertions/MsAssertions.java index bf56db71b5..75d1d67cb9 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/assertions/MsAssertions.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/assertions/MsAssertions.java @@ -174,9 +174,9 @@ public class MsAssertions extends MsTestElement { JSR223Assertion assertion = new JSR223Assertion(); assertion.setEnabled(this.isEnable()); if (StringUtils.isNotEmpty(assertionJSR223.getDesc())) { - assertion.setName("JSR223" + "==" + this.getName() + "==" + assertionJSR223.getDesc() + "==" + assertionJSR223.getScript()); + assertion.setName("JSR223" + "==" + this.getName() + "==" + assertionJSR223.getDesc() + "&&" + assertionJSR223.getScript()); } else { - assertion.setName("JSR223" + "==" + this.getName() + "==" + "JSR223Assertion" + "==" + assertionJSR223.getScript()); + assertion.setName("JSR223" + "==" + this.getName() + "==" + "JSR223Assertion" + "&&" + assertionJSR223.getScript()); } assertion.setProperty(TestElement.TEST_CLASS, JSR223Assertion.class.getName()); assertion.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TestBeanGUI")); diff --git a/backend/src/main/java/io/metersphere/api/service/ApiJmeterFileService.java b/backend/src/main/java/io/metersphere/api/service/ApiJmeterFileService.java index 8e66039ea7..663b225f15 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiJmeterFileService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiJmeterFileService.java @@ -73,7 +73,7 @@ public class ApiJmeterFileService { } } HashTree hashTree; - if (StringUtils.equalsAnyIgnoreCase(runMode, ApiRunMode.DEFINITION.name(), ApiRunMode.API_PLAN.name(), ApiRunMode.SCHEDULE_API_PLAN.name(), ApiRunMode.MANUAL_PLAN.name())) { + if (StringUtils.equalsAnyIgnoreCase(runMode, ApiRunMode.DEFINITION.name(), ApiRunMode.JENKINS_API_PLAN.name(),ApiRunMode.API_PLAN.name(), ApiRunMode.SCHEDULE_API_PLAN.name(), ApiRunMode.MANUAL_PLAN.name())) { String testId = remoteTestId; if (remoteTestId.contains(":")) { //执行测试计划案例时会有拼接ID,ID为 planTestCaseId:测试计划报告ID diff --git a/backend/src/main/java/io/metersphere/api/service/ApiTestCaseService.java b/backend/src/main/java/io/metersphere/api/service/ApiTestCaseService.java index 568e1d6445..da60991019 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiTestCaseService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiTestCaseService.java @@ -736,16 +736,20 @@ public class ApiTestCaseService { return ids; } - private ApiDefinitionExecResult addResult(String id, String status) { + private ApiDefinitionExecResult addResult(String resourceId, String status, String reportId) { ApiDefinitionExecResult apiResult = new ApiDefinitionExecResult(); - apiResult.setId(UUID.randomUUID().toString()); + if (StringUtils.isEmpty(reportId)) { + apiResult.setId(UUID.randomUUID().toString()); + } else { + apiResult.setId(reportId); + } apiResult.setCreateTime(System.currentTimeMillis()); apiResult.setStartTime(System.currentTimeMillis()); apiResult.setEndTime(System.currentTimeMillis()); apiResult.setTriggerMode(TriggerMode.BATCH.name()); apiResult.setActuator("LOCAL"); apiResult.setUserId(Objects.requireNonNull(SessionUtils.getUser()).getId()); - apiResult.setResourceId(id); + apiResult.setResourceId(resourceId); apiResult.setStartTime(System.currentTimeMillis()); apiResult.setType(ApiRunMode.DEFINITION.name()); apiResult.setStatus(status); @@ -765,7 +769,7 @@ public class ApiTestCaseService { ApiTestCaseMapper sqlSessionMapper = sqlSession.getMapper(ApiTestCaseMapper.class); for (ApiTestCaseWithBLOBs caseWithBLOBs : list) { - ApiDefinitionExecResult report = addResult(caseWithBLOBs.getId(), APITestStatus.Running.name()); + ApiDefinitionExecResult report = addResult(caseWithBLOBs.getId(), APITestStatus.Running.name(), null); report.setName(caseWithBLOBs.getName()); caseWithBLOBs.setLastResultId(report.getId()); caseWithBLOBs.setUpdateTime(System.currentTimeMillis()); @@ -792,6 +796,38 @@ public class ApiTestCaseService { } } + public String jenkinsRun(RunCaseRequest request) { + ApiTestCaseWithBLOBs caseWithBLOBs = null; + if (request.getBloBs() == null) { + caseWithBLOBs = apiTestCaseMapper.selectByPrimaryKey(request.getCaseId()); + if (caseWithBLOBs == null) { + return null; + } + request.setBloBs(caseWithBLOBs); + } else { + caseWithBLOBs = request.getBloBs(); + } + if (caseWithBLOBs == null) { + return null; + } + if (StringUtils.isBlank(request.getEnvironmentId())) { + request.setEnvironmentId(extApiTestCaseMapper.getApiCaseEnvironment(request.getCaseId())); + } + //提前生成报告 + ApiDefinitionExecResult report = addResult(caseWithBLOBs.getId(), APITestStatus.Running.name(), request.getReportId()); + report.setName(caseWithBLOBs.getName()); + report.setTriggerMode(ApiRunMode.API.name()); + report.setType(ApiRunMode.JENKINS.name()); + apiDefinitionExecResultMapper.insert(report); + //更新接口案例的最后执行状态等信息 + caseWithBLOBs.setLastResultId(report.getId()); + caseWithBLOBs.setUpdateTime(System.currentTimeMillis()); + caseWithBLOBs.setStatus(APITestStatus.Running.name()); + apiTestCaseMapper.updateByPrimaryKey(caseWithBLOBs); + request.setReport(report); + return this.run(request); + } + public String run(RunCaseRequest request) { ApiTestCaseWithBLOBs testCaseWithBLOBs = request.getBloBs(); if (StringUtils.equals(request.getRunMode(), ApiRunMode.JENKINS_API_PLAN.name())) { @@ -800,15 +836,13 @@ public class ApiTestCaseService { //通过测试计划id查询环境 request.setReportId(request.getTestPlanId()); } - if (StringUtils.equals(request.getRunMode(), ApiRunMode.JENKINS.name())) { - request.setReportId(request.getEnvironmentId()); - } + // 多态JSON普通转换会丢失内容,需要通过 ObjectMapper 获取 if (testCaseWithBLOBs != null && StringUtils.isNotEmpty(testCaseWithBLOBs.getRequest())) { try { HashTree jmeterHashTree = this.generateHashTree(request, testCaseWithBLOBs); // 调用执行方法 - jMeterService.runLocal(request.getReportId(),null, jmeterHashTree, null, request.getRunMode()); + jMeterService.runLocal(request.getReportId(), null, jmeterHashTree, null, request.getRunMode()); } catch (Exception ex) { ApiDefinitionExecResult result = MessageCache.caseExecResourceLock.get(request.getReportId()); @@ -834,7 +868,7 @@ public class ApiTestCaseService { request.setTestPlanId(testPlanID); HashTree jmeterHashTree = this.generateHashTree(request, apiCaseBolbs); // 调用执行方法 - jMeterService.runLocal(id,null, jmeterHashTree, debugReportId, runMode); + jMeterService.runLocal(id, null, jmeterHashTree, debugReportId, runMode); } catch (Exception ex) { LogUtil.error(ex); } @@ -1171,9 +1205,7 @@ public class ApiTestCaseService { if (StringUtils.isBlank(environmentId)) { return null; } - // "environmentId" try { - environmentId = environmentId.substring(1, environmentId.length() - 1); return apiTestEnvironmentMapper.selectByPrimaryKey(environmentId); } catch (Exception e) { LogUtil.error("api case environmentId incorrect parsing. api case id: " + caseId); diff --git a/backend/src/main/java/io/metersphere/api/service/MsResultService.java b/backend/src/main/java/io/metersphere/api/service/MsResultService.java index 84c749f5db..9b6fa71db2 100644 --- a/backend/src/main/java/io/metersphere/api/service/MsResultService.java +++ b/backend/src/main/java/io/metersphere/api/service/MsResultService.java @@ -252,14 +252,22 @@ public class MsResultService { ResponseAssertionResult responseAssertionResult = new ResponseAssertionResult(); responseAssertionResult.setName(assertionResult.getName()); if (StringUtils.isNotEmpty(assertionResult.getName()) && assertionResult.getName().indexOf("==") != -1) { - String[] array = assertionResult.getName().split("=="); - if ("JSR223".equals(array[0])) { - responseAssertionResult.setName(array[1]); - responseAssertionResult.setContent(array[2]); - if (array.length > 3) { - responseAssertionResult.setScript(array[3]); + if (assertionResult.getName().indexOf("JSR223") != -1) { + String[] array = assertionResult.getName().split("==", 3); + if ("JSR223".equals(array[0])) { + responseAssertionResult.setName(array[1]); + if (array[2].indexOf("&&") != -1) { + String[] content = array[2].split("&&"); + responseAssertionResult.setContent(content[0]); + if (content.length > 1) { + responseAssertionResult.setScript(content[1]); + } + } else { + responseAssertionResult.setContent(array[2]); + } } } else { + String[] array = assertionResult.getName().split("=="); responseAssertionResult.setName(array[0]); StringBuffer content = new StringBuffer(); for (int i = 1; i < array.length; i++) { diff --git a/backend/src/main/java/io/metersphere/api/service/TestResultService.java b/backend/src/main/java/io/metersphere/api/service/TestResultService.java index b38918dfa2..9fe412c105 100644 --- a/backend/src/main/java/io/metersphere/api/service/TestResultService.java +++ b/backend/src/main/java/io/metersphere/api/service/TestResultService.java @@ -72,6 +72,7 @@ public class TestResultService { reportTask.setTriggerMode(TriggerMode.API.name()); reportTask.setName(apiTestCaseWithBLOBs.getName()); reportTask.setExecutor(userName); + reportTask.setUserId(apiTestCaseWithBLOBs.getCreateUserId()); reportTask.setExecutionTime(DateUtils.getTimeString(apiTestCaseWithBLOBs.getCreateTime())); reportTask.setEnvironment(name); //测试计划用例,定时,jenkins @@ -105,18 +106,17 @@ public class TestResultService { } //报告内容 reportTask = new ApiTestReportVariable(); - if (StringUtils.equalsAny(runMode, ApiRunMode.SCHEDULE_SCENARIO.name())) { - reportTask.setStatus(scenarioReport.getStatus()); - reportTask.setId(scenarioReport.getId()); - reportTask.setTriggerMode(scenarioReport.getTriggerMode()); - reportTask.setName(scenarioReport.getName()); - reportTask.setExecutor(userName); - reportTask.setPrincipal(principal); - reportTask.setExecutionTime(DateUtils.getTimeString(scenarioReport.getUpdateTime())); - reportTask.setEnvironment(environment); - SystemParameterService systemParameterService = CommonBeanFactory.getBean(SystemParameterService.class); - assert systemParameterService != null; - } + reportTask.setStatus(scenarioReport.getStatus()); + reportTask.setId(scenarioReport.getId()); + reportTask.setTriggerMode(scenarioReport.getTriggerMode()); + reportTask.setName(scenarioReport.getName()); + reportTask.setExecutor(userName); + reportTask.setUserId(scenarioReport.getUserId()); + reportTask.setPrincipal(principal); + reportTask.setExecutionTime(DateUtils.getTimeString(scenarioReport.getUpdateTime())); + reportTask.setEnvironment(environment); + SystemParameterService systemParameterService = CommonBeanFactory.getBean(SystemParameterService.class); + assert systemParameterService != null; testResult.setTestId(scenarioReport.getScenarioId()); planScenarioId = scenarioReport.getTestPlanScenarioId(); } diff --git a/backend/src/main/java/io/metersphere/base/domain/CustomField.java b/backend/src/main/java/io/metersphere/base/domain/CustomField.java index 9df3ccd301..cd3e94a75d 100644 --- a/backend/src/main/java/io/metersphere/base/domain/CustomField.java +++ b/backend/src/main/java/io/metersphere/base/domain/CustomField.java @@ -30,5 +30,7 @@ public class CustomField implements Serializable { private String options; + private String defaultValue; + private static final long serialVersionUID = 1L; } \ No newline at end of file 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 ccf20585a3..6dbe512d44 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 @@ -726,7 +726,7 @@ order by `order` desc limit 1; update api_test_case diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtProjectMapper.java b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtProjectMapper.java index 06273e0600..dedcb072c9 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtProjectMapper.java +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtProjectMapper.java @@ -35,4 +35,6 @@ public interface ExtProjectMapper { long getProjectMemberSize(@Param("projectId") String projectId); ListgetProjectByUserId(@Param("userId")String userId); + + int getProjectPlanBugSize(@Param("projectId") String projectId); } diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtProjectMapper.xml b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtProjectMapper.xml index f9b5e6f44a..5ea046fa25 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtProjectMapper.xml +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtProjectMapper.xml @@ -244,4 +244,11 @@ + diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestCaseMapper.xml b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestCaseMapper.xml index 5397eca82a..70d6bbee88 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestCaseMapper.xml +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestCaseMapper.xml @@ -214,21 +214,14 @@