Merge remote-tracking branch 'origin/master'

This commit is contained in:
2025-03-20 07:30:14 +08:00
13 changed files with 261 additions and 81 deletions

View File

@@ -17,6 +17,7 @@ import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.concurrent.CompletableFuture;
/**
* 用例Controller
@@ -87,6 +88,10 @@ public class TestCaseController extends BaseController {
@PostMapping("/run")
public AjaxResult run(@RequestBody IDQO qo) {
log.info("执行用例 id:{}, jmeterHomePath:{}", qo.getId(), jmeterHomePath);
return success(testCaseService.executeTestCaseById(qo.getId(), jmeterHomePath));
// 异步执行任务
CompletableFuture.runAsync(() -> {
testCaseService.executeTestCaseById(qo.getId(), jmeterHomePath);
});
return toAjax(true);
}
}

View File

@@ -6,16 +6,18 @@ import com.test.common.core.domain.AjaxResult;
import com.test.common.core.page.TableDataInfo;
import com.test.common.enums.BusinessType;
import com.test.common.utils.DateUtils;
import com.test.test.domain.TestCase;
import com.test.common.utils.SecurityUtils;
import com.test.test.domain.TestTask;
import com.test.test.domain.qo.GroupIdQO;
import com.test.test.domain.qo.IDQO;
import com.test.test.service.ITestTaskService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.concurrent.CompletableFuture;
/**
* 定时任务Controller
@@ -23,6 +25,10 @@ import java.util.List;
@RestController
@RequestMapping("/test/task")
public class TestTaskController extends BaseController {
@Value("${test.jmeterHomePath:/opt/apache-jmeter}")
private String jmeterHomePath;
@Resource
private ITestTaskService testTaskService;
@@ -73,5 +79,18 @@ public class TestTaskController extends BaseController {
return toAjax(testTaskService.deleteTestTaskById(qo.getId()));
}
/**
* 执行任务
*/
@Log(title = "任务", businessType = BusinessType.OTHER)
@PostMapping("/run")
public AjaxResult run(@RequestBody IDQO qo) {
// 异步执行任务
CompletableFuture.runAsync(() -> {
testTaskService.executeTestTaskById(qo.getId(), 2, null, jmeterHomePath, SecurityUtils.getUsername());
});
return toAjax(true);
}
}

View File

@@ -280,8 +280,10 @@ public class TestCaseServiceImpl implements ITestCaseService
}
String url = "jdbc:mysql://"+ testDatasource.getHost() +":"+ testDatasource.getPort() +"/" + testDatasource.getDbName();
List<String> columnNameList = new ArrayList<>();
// sql查询特有字段结果存储
Map<String, String> sqlSpecialResultMap = new HashMap<>();
// 获取所有sql查询集合对象
List<Map<String, Object>> resultMapList = MySQLExecutor.executeQuery(testCaseStep.getSqlCommand(), url, testDatasource.getUsername(), testDatasource.getPassword(), columnNameList);
List<Map<String, Object>> resultMapList = MySQLExecutor.executeQuery(testCaseStep.getSqlCommand(), url, testDatasource.getUsername(), testDatasource.getPassword(), columnNameList, sqlSpecialResultMap);
if (!CollectionUtils.isEmpty(resultMapList)) {
SqlResult sqlResult = new SqlResult();
sqlResult.setColumnNameList(columnNameList);
@@ -293,7 +295,7 @@ public class TestCaseServiceImpl implements ITestCaseService
testCaseResult.setSqlResult(gson.toJson(sqlResult));
// 处理参数提取
Map<String, String> assignmentResultMap = new HashMap<>();
String assignmentResult = this.dealDataSourceTestCaseStepAssignment(resultMapList, assignmentResultMap, assignment);
String assignmentResult = this.dealDataSourceTestCaseStepAssignment(resultMapList, assignmentResultMap, sqlSpecialResultMap, assignment);
testCaseResult.setAssignment(assignmentResult);
// 根据提取结果处理校验规则
String assertionResult = this.dealTestCaseStepAssertion(assignmentResultMap, assertion);
@@ -522,10 +524,11 @@ public class TestCaseServiceImpl implements ITestCaseService
* 处理数据源响应字段提取
* @param resultMapList
* @param assignmentResultMap
* @param sqlSpecialResultMap 存储sql特有的响应信息字段结果值
* @param assignment
* @return 处理后的提取结果
*/
private String dealDataSourceTestCaseStepAssignment(List<Map<String, Object>> resultMapList, Map<String, String> assignmentResultMap, String assignment) {
private String dealDataSourceTestCaseStepAssignment(List<Map<String, Object>> resultMapList, Map<String, String> assignmentResultMap, Map<String, String> sqlSpecialResultMap, String assignment) {
if (!StringUtils.isEmpty(assignment) && !"[]".equals(assignment)) {
Gson gson = new Gson();
String responseBody = gson.toJson(resultMapList);
@@ -547,6 +550,20 @@ public class TestCaseServiceImpl implements ITestCaseService
String value = JMeterUtil.extractFieldValue(responseBody, path);
ruleResult.setValue(value);
}
} else if ("${RESULT_DATA_LENGTH}".equals(content)) {
if (!CollectionUtils.isEmpty(resultMapList)) {
String value = String.valueOf(resultMapList.size());
ruleResult.setValue(value);
} else {
ruleResult.setValue("0");
}
} else if ("${AFFECT_ROWS}".equals(content) || "${CHANGE_ROWS}".equals(content)) {
String affectRows = sqlSpecialResultMap.get("${AFFECT_ROWS}");
if (affectRows == null) {
ruleResult.setValue("0");
} else {
ruleResult.setValue(affectRows);
}
}
assignmentResultMap.put(rule.getName(), ruleResult.getValue());
ruleResultList.add(ruleResult);
@@ -576,6 +593,13 @@ public class TestCaseServiceImpl implements ITestCaseService
String source = rule.getSource();
String target = rule.getTarget();
String fn = rule.getFn();
if (source.equals("${RESPONSE_BODY}")) {
source = "responseBody";
} else if (source.equals("${RESPONSE_CODE}")) {
source = "responseCode";
} else if (source.equals("${RESPONSE_HEADER}")) {
source = "responseHeader";
}
String value = assignmentResultMap.get(source);
ruleResult.setValue(value);
if ("等于".equals(fn)) {

View File

@@ -63,7 +63,7 @@ public class TestCaseStepServiceImpl implements ITestCaseStepService {
int i = 1;
for (TestCaseResult testCaseResult : testCaseResultList) {
String dateStr = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, testCaseResult.getExecuteTime());
testCaseResult.setTitle("结果" + i + "" + dateStr + "),状态:" + testCaseResult.getStatus());
testCaseResult.setTitle("结果" + i + "" + dateStr + "),状态:" + testCaseResult.getStatus() + "耗时ms" + testCaseResult.getUseTime());
i ++;
}
}

View File

@@ -110,9 +110,8 @@ public class TestTaskServiceImpl implements ITestTaskService {
@Override
public boolean executeTestTaskById(Long id, Integer triggerType, String environment, String jmeterHomePath, String operUser) {
TestTask testTask = testTaskMapper.selectTestTaskById(id);
if (testTask == null || testTask.getStatus() == null
|| testTask.getStatus() > 0 || "2".equals(testTask.getDelFlag())) {
log.error("定时任务已删除或未启用,不能执行!");
if (testTask == null || "2".equals(testTask.getDelFlag())) {
log.error("定时任务已删除,不能执行!");
return false;
}
TestTaskLog testTaskLog = new TestTaskLog();