用例步骤表触发JMeter调用接口

This commit is contained in:
liangdaliang
2025-02-14 16:10:23 +08:00
parent 186665beae
commit 7103c9eab0
7 changed files with 1030 additions and 3 deletions

View File

@@ -0,0 +1,131 @@
package com.test.test.domain;
import com.test.common.core.domain.BaseEntity;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import com.test.common.annotation.Excel;
/**
* 用例步骤对象 test_case_step
*
* @author liangdl
* @date 2025-02-11
*/
@Setter
@Getter
@ToString
public class TestCaseStep extends BaseEntity {
private static final long serialVersionUID = 1L;
/** 步骤id */
private Long id;
/** 父节点id */
@Excel(name = "父节点id")
private Long parentId;
/** 用例id */
@Excel(name = "用例id")
private Long caseId;
/** 步骤名 */
@Excel(name = "步骤名")
private String name;
/** 执行顺序 */
@Excel(name = "执行顺序")
private Long stepNum;
/** 步骤类型http接口1数据源2循环3轮询4 */
@Excel(name = "步骤类型", readConverterExp = "h=ttp接口1数据源2循环3轮询4")
private Long type;
/** http 请求方法(get or post) */
@Excel(name = "http 请求方法(get or post)")
private String requestMethod;
/** http 请求路径 */
@Excel(name = "http 请求路径")
private String requestUrl;
/** http 请求体 */
@Excel(name = "http 请求体")
private String requestBody;
/** http 请求头 */
@Excel(name = "http 请求头")
private String requestHeader;
/** http 查询参数 */
@Excel(name = "http 查询参数")
private String requestParams;
/** http 关联服务id */
@Excel(name = "http 关联服务id")
private Long apiHttpId;
/** http 请求主机 */
@Excel(name = "http 请求主机")
private String apiHost;
/** http 请求端口号 */
@Excel(name = "http 请求端口号")
private String apiPort;
/** http 请求协议(http, https) */
@Excel(name = "http 请求协议(http, https)")
private String apiProtocol;
/** sql 命令 */
@Excel(name = "sql 命令")
private String sqlCommand;
/** 循环次数 */
@Excel(name = "循环次数")
private Long count;
/** 循环并行开关 */
@Excel(name = "循环并行开关")
private Integer async;
/** 轮询间隔 */
@Excel(name = "轮询间隔")
private Long interval;
/** 轮询失败判定条件选择 */
@Excel(name = "轮询失败判定条件选择")
private String breakError;
/** 请求预处理 */
@Excel(name = "请求预处理")
private String preScript;
/** 响应预处理 */
@Excel(name = "响应预处理")
private String postScript;
/** 参数提取
[{
"name": "变量名",
"type": "提取方式",
"content": "提取对象",
"path": "提取表达式",
}] */
@Excel(name = "参数提取")
private String assignment;
/** 校验规则
[{
"name": "描述",
"source": "对象",
"fn": "条件",
"target": "内容"
}] */
@Excel(name = "校验规则")
private String assertion;
/** 删除标志0代表存在 2代表删除 */
private String delFlag;
}

View File

@@ -0,0 +1,61 @@
package com.test.test.mapper;
import java.util.List;
import com.test.test.domain.TestCaseStep;
/**
* 用例步骤Mapper接口
*
* @author liangdl
* @date 2025-02-11
*/
public interface TestCaseStepMapper
{
/**
* 查询用例步骤
*
* @param id 用例步骤主键
* @return 用例步骤
*/
public TestCaseStep selectTestCaseStepById(Long id);
/**
* 查询用例步骤列表
*
* @param testCaseStep 用例步骤
* @return 用例步骤集合
*/
public List<TestCaseStep> selectTestCaseStepList(TestCaseStep testCaseStep);
/**
* 新增用例步骤
*
* @param testCaseStep 用例步骤
* @return 结果
*/
public int insertTestCaseStep(TestCaseStep testCaseStep);
/**
* 修改用例步骤
*
* @param testCaseStep 用例步骤
* @return 结果
*/
public int updateTestCaseStep(TestCaseStep testCaseStep);
/**
* 删除用例步骤
*
* @param id 用例步骤主键
* @return 结果
*/
public int deleteTestCaseStepById(Long id);
/**
* 批量删除用例步骤
*
* @param ids 需要删除的数据主键集合
* @return 结果
*/
public int deleteTestCaseStepByIds(Long[] ids);
}

View File

@@ -0,0 +1,71 @@
package com.test.test.service;
import com.test.test.domain.TestCaseStep;
import java.util.List;
/**
* 用例步骤Service接口
*
* @author liangdl
* @date 2025-02-11
*/
public interface ITestCaseStepService
{
/**
* 查询用例步骤
*
* @param id 用例步骤主键
* @return 用例步骤
*/
public TestCaseStep selectTestCaseStepById(Long id);
/**
* 查询用例步骤列表
*
* @param testCaseStep 用例步骤
* @return 用例步骤集合
*/
public List<TestCaseStep> selectTestCaseStepList(TestCaseStep testCaseStep);
/**
* 新增用例步骤
*
* @param testCaseStep 用例步骤
* @return 结果
*/
public int insertTestCaseStep(TestCaseStep testCaseStep);
/**
* 修改用例步骤
*
* @param testCaseStep 用例步骤
* @return 结果
*/
public int updateTestCaseStep(TestCaseStep testCaseStep);
/**
* 批量删除用例步骤
*
* @param ids 需要删除的用例步骤主键集合
* @return 结果
*/
public int deleteTestCaseStepByIds(Long[] ids);
/**
* 删除用例步骤信息
*
* @param id 用例步骤主键
* @return 结果
*/
public int deleteTestCaseStepById(Long id);
/**
* 执行jmeter用例步骤计划
*
* @param id 用例步骤主键
* @param jmeterHomePath jmeter安装路径
* @return 结果
*/
public String executeJmeterTestCaseStepById(Long id, String jmeterHomePath);
}

View File

@@ -0,0 +1,128 @@
package com.test.test.service.impl;
import com.test.common.utils.DateUtils;
import com.test.common.utils.JMeterUtil;
import com.test.test.domain.TestCaseStep;
import com.test.test.mapper.TestCaseStepMapper;
import com.test.test.service.ITestCaseStepService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* 用例步骤Service业务层处理
*
* @author test
* @date 2025-02-11
*/
@Slf4j
@Service
public class TestCaseStepServiceImpl implements ITestCaseStepService
{
@Resource
private TestCaseStepMapper testCaseStepMapper;
/**
* 查询用例步骤
*
* @param id 用例步骤主键
* @return 用例步骤
*/
@Override
public TestCaseStep selectTestCaseStepById(Long id)
{
return testCaseStepMapper.selectTestCaseStepById(id);
}
/**
* 查询用例步骤列表
*
* @param testCaseStep 用例步骤
* @return 用例步骤
*/
@Override
public List<TestCaseStep> selectTestCaseStepList(TestCaseStep testCaseStep)
{
return testCaseStepMapper.selectTestCaseStepList(testCaseStep);
}
/**
* 新增用例步骤
*
* @param testCaseStep 用例步骤
* @return 结果
*/
@Override
public int insertTestCaseStep(TestCaseStep testCaseStep)
{
testCaseStep.setCreateTime(DateUtils.getNowDate());
return testCaseStepMapper.insertTestCaseStep(testCaseStep);
}
/**
* 修改用例步骤
*
* @param testCaseStep 用例步骤
* @return 结果
*/
@Override
public int updateTestCaseStep(TestCaseStep testCaseStep)
{
testCaseStep.setUpdateTime(DateUtils.getNowDate());
return testCaseStepMapper.updateTestCaseStep(testCaseStep);
}
/**
* 批量删除用例步骤
*
* @param ids 需要删除的用例步骤主键
* @return 结果
*/
@Override
public int deleteTestCaseStepByIds(Long[] ids)
{
return testCaseStepMapper.deleteTestCaseStepByIds(ids);
}
/**
* 删除用例步骤信息
*
* @param id 用例步骤主键
* @return 结果
*/
@Override
public int deleteTestCaseStepById(Long id)
{
return testCaseStepMapper.deleteTestCaseStepById(id);
}
/**
* 执行jmeter用例步骤计划
*
* @param id 用例步骤主键
* @param jmeterHomePath jmeter安装路径
* @return 结果
*/
@Override
public String executeJmeterTestCaseStepById(Long id, String jmeterHomePath) {
TestCaseStep testCaseStep = testCaseStepMapper.selectTestCaseStepById(id);
if (testCaseStep == null) {
log.error("根据主键id{}未查询到用例步骤计划", id);
return null;
}
String result = null;
String url = testCaseStep.getRequestUrl();
String method = testCaseStep.getRequestMethod().toUpperCase();
if (!method.equals("GET") && !method.equals("POST")) {
log.error("不支持的请求方式:{}", method);
return null;
}
String requestHeader = testCaseStep.getRequestHeader();
result = JMeterUtil.getJmeterResult(id, url, Integer.parseInt(testCaseStep.getApiPort()), testCaseStep.getRequestMethod(), testCaseStep.getRequestParams(), requestHeader, jmeterHomePath);
return result;
}
}

View File

@@ -0,0 +1,186 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.test.test.mapper.TestCaseStepMapper">
<resultMap type="TestCaseStep" id="TestCaseStepResult">
<result property="id" column="id" />
<result property="parentId" column="parent_id" />
<result property="caseId" column="case_id" />
<result property="name" column="name" />
<result property="stepNum" column="step_num" />
<result property="type" column="type" />
<result property="requestMethod" column="request_method" />
<result property="requestUrl" column="request_url" />
<result property="requestBody" column="request_body" />
<result property="requestHeader" column="request_header" />
<result property="requestParams" column="request_params" />
<result property="apiHttpId" column="api_http_id" />
<result property="apiHost" column="api_host" />
<result property="apiPort" column="api_port" />
<result property="apiProtocol" column="api_protocol" />
<result property="sqlCommand" column="sql_command" />
<result property="count" column="count" />
<result property="async" column="async" />
<result property="interval" column="interval" />
<result property="breakError" column="break_error" />
<result property="preScript" column="pre_script" />
<result property="postScript" column="post_script" />
<result property="assignment" column="assignment" />
<result property="assertion" column="assertion" />
<result property="delFlag" column="del_flag" />
<result property="createBy" column="create_by" />
<result property="createTime" column="create_time" />
<result property="updateBy" column="update_by" />
<result property="updateTime" column="update_time" />
</resultMap>
<sql id="selectTestCaseStepVo">
select id, parent_id, case_id, name, step_num, type, request_method, request_url, request_body, request_header, request_params, api_http_id, api_host, api_port, api_protocol, sql_command, count, async, interval, break_error, pre_script, post_script, assignment, assertion, del_flag, create_by, create_time, update_by, update_time from test_case_step
</sql>
<select id="selectTestCaseStepList" parameterType="TestCaseStep" resultMap="TestCaseStepResult">
<include refid="selectTestCaseStepVo"/>
<where>
<if test="parentId != null "> and parent_id = #{parentId}</if>
<if test="caseId != null "> and case_id = #{caseId}</if>
<if test="name != null and name != ''"> and name like concat('%', #{name}, '%')</if>
<if test="stepNum != null "> and step_num = #{stepNum}</if>
<if test="type != null "> and type = #{type}</if>
<if test="requestMethod != null and requestMethod != ''"> and request_method = #{requestMethod}</if>
<if test="requestUrl != null and requestUrl != ''"> and request_url = #{requestUrl}</if>
<if test="requestBody != null and requestBody != ''"> and request_body = #{requestBody}</if>
<if test="requestHeader != null and requestHeader != ''"> and request_header = #{requestHeader}</if>
<if test="requestParams != null and requestParams != ''"> and request_params = #{requestParams}</if>
<if test="apiHttpId != null "> and api_http_id = #{apiHttpId}</if>
<if test="apiHost != null and apiHost != ''"> and api_host = #{apiHost}</if>
<if test="apiPort != null and apiPort != ''"> and api_port = #{apiPort}</if>
<if test="apiProtocol != null and apiProtocol != ''"> and api_protocol = #{apiProtocol}</if>
<if test="sqlCommand != null and sqlCommand != ''"> and sql_command = #{sqlCommand}</if>
<if test="count != null "> and count = #{count}</if>
<if test="async != null "> and async = #{async}</if>
<if test="interval != null "> and interval = #{interval}</if>
<if test="breakError != null and breakError != ''"> and break_error = #{breakError}</if>
<if test="preScript != null and preScript != ''"> and pre_script = #{preScript}</if>
<if test="postScript != null and postScript != ''"> and post_script = #{postScript}</if>
<if test="assignment != null and assignment != ''"> and assignment = #{assignment}</if>
<if test="assertion != null and assertion != ''"> and assertion = #{assertion}</if>
</where>
</select>
<select id="selectTestCaseStepById" parameterType="Long" resultMap="TestCaseStepResult">
<include refid="selectTestCaseStepVo"/>
where id = #{id}
</select>
<insert id="insertTestCaseStep" parameterType="TestCaseStep" useGeneratedKeys="true" keyProperty="id">
insert into test_case_step
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="parentId != null">parent_id,</if>
<if test="caseId != null">case_id,</if>
<if test="name != null">name,</if>
<if test="stepNum != null">step_num,</if>
<if test="type != null">type,</if>
<if test="requestMethod != null">request_method,</if>
<if test="requestUrl != null">request_url,</if>
<if test="requestBody != null">request_body,</if>
<if test="requestHeader != null">request_header,</if>
<if test="requestParams != null">request_params,</if>
<if test="apiHttpId != null">api_http_id,</if>
<if test="apiHost != null">api_host,</if>
<if test="apiPort != null">api_port,</if>
<if test="apiProtocol != null">api_protocol,</if>
<if test="sqlCommand != null">sql_command,</if>
<if test="count != null">count,</if>
<if test="async != null">async,</if>
<if test="interval != null">interval,</if>
<if test="breakError != null">break_error,</if>
<if test="preScript != null">pre_script,</if>
<if test="postScript != null">post_script,</if>
<if test="assignment != null">assignment,</if>
<if test="assertion != null">assertion,</if>
<if test="delFlag != null">del_flag,</if>
<if test="createBy != null">create_by,</if>
<if test="createTime != null">create_time,</if>
<if test="updateBy != null">update_by,</if>
<if test="updateTime != null">update_time,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="parentId != null">#{parentId},</if>
<if test="caseId != null">#{caseId},</if>
<if test="name != null">#{name},</if>
<if test="stepNum != null">#{stepNum},</if>
<if test="type != null">#{type},</if>
<if test="requestMethod != null">#{requestMethod},</if>
<if test="requestUrl != null">#{requestUrl},</if>
<if test="requestBody != null">#{requestBody},</if>
<if test="requestHeader != null">#{requestHeader},</if>
<if test="requestParams != null">#{requestParams},</if>
<if test="apiHttpId != null">#{apiHttpId},</if>
<if test="apiHost != null">#{apiHost},</if>
<if test="apiPort != null">#{apiPort},</if>
<if test="apiProtocol != null">#{apiProtocol},</if>
<if test="sqlCommand != null">#{sqlCommand},</if>
<if test="count != null">#{count},</if>
<if test="async != null">#{async},</if>
<if test="interval != null">#{interval},</if>
<if test="breakError != null">#{breakError},</if>
<if test="preScript != null">#{preScript},</if>
<if test="postScript != null">#{postScript},</if>
<if test="assignment != null">#{assignment},</if>
<if test="assertion != null">#{assertion},</if>
<if test="delFlag != null">#{delFlag},</if>
<if test="createBy != null">#{createBy},</if>
<if test="createTime != null">#{createTime},</if>
<if test="updateBy != null">#{updateBy},</if>
<if test="updateTime != null">#{updateTime},</if>
</trim>
</insert>
<update id="updateTestCaseStep" parameterType="TestCaseStep">
update test_case_step
<trim prefix="SET" suffixOverrides=",">
<if test="parentId != null">parent_id = #{parentId},</if>
<if test="caseId != null">case_id = #{caseId},</if>
<if test="name != null">name = #{name},</if>
<if test="stepNum != null">step_num = #{stepNum},</if>
<if test="type != null">type = #{type},</if>
<if test="requestMethod != null">request_method = #{requestMethod},</if>
<if test="requestUrl != null">request_url = #{requestUrl},</if>
<if test="requestBody != null">request_body = #{requestBody},</if>
<if test="requestHeader != null">request_header = #{requestHeader},</if>
<if test="requestParams != null">request_params = #{requestParams},</if>
<if test="apiHttpId != null">api_http_id = #{apiHttpId},</if>
<if test="apiHost != null">api_host = #{apiHost},</if>
<if test="apiPort != null">api_port = #{apiPort},</if>
<if test="apiProtocol != null">api_protocol = #{apiProtocol},</if>
<if test="sqlCommand != null">sql_command = #{sqlCommand},</if>
<if test="count != null">count = #{count},</if>
<if test="async != null">async = #{async},</if>
<if test="interval != null">interval = #{interval},</if>
<if test="breakError != null">break_error = #{breakError},</if>
<if test="preScript != null">pre_script = #{preScript},</if>
<if test="postScript != null">post_script = #{postScript},</if>
<if test="assignment != null">assignment = #{assignment},</if>
<if test="assertion != null">assertion = #{assertion},</if>
<if test="delFlag != null">del_flag = #{delFlag},</if>
<if test="createBy != null">create_by = #{createBy},</if>
<if test="createTime != null">create_time = #{createTime},</if>
<if test="updateBy != null">update_by = #{updateBy},</if>
<if test="updateTime != null">update_time = #{updateTime},</if>
</trim>
where id = #{id}
</update>
<delete id="deleteTestCaseStepById" parameterType="Long">
delete from test_case_step where id = #{id}
</delete>
<delete id="deleteTestCaseStepByIds" parameterType="String">
delete from test_case_step where id in
<foreach item="id" collection="array" open="(" separator="," close=")">
#{id}
</foreach>
</delete>
</mapper>