refactor(接口定义): 重构用例存储及结果统计

This commit is contained in:
fit2-zhao 2021-01-20 10:29:17 +08:00
parent 66c14eea89
commit ea7090b3eb
9 changed files with 156 additions and 73 deletions

View File

@ -15,15 +15,15 @@ import io.metersphere.track.dto.TestPlanDTO;
import io.metersphere.track.request.testcase.QueryTestPlanRequest;
import io.metersphere.track.service.TestPlanApiCaseService;
import io.metersphere.track.service.TestPlanService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.*;
@Service
@ -37,32 +37,39 @@ public class ApiDefinitionExecResultService {
private TestPlanApiCaseService testPlanApiCaseService;
@Resource
private TestPlanService testPlanService;
@Resource
SqlSessionFactory sqlSessionFactory;
public void saveApiResult(TestResult result, String type) {
result.getScenarios().get(0).getRequestResults().forEach(item -> {
ApiDefinitionExecResult saveResult = new ApiDefinitionExecResult();
saveResult.setId(UUID.randomUUID().toString());
saveResult.setCreateTime(System.currentTimeMillis());
saveResult.setUserId(Objects.requireNonNull(SessionUtils.getUser()).getId());
saveResult.setName(item.getName());
saveResult.setResourceId(item.getName());
saveResult.setContent(JSON.toJSONString(item));
saveResult.setStartTime(item.getStartTime());
String status = item.isSuccess() ? "success" : "error";
saveResult.setEndTime(item.getResponseResult().getResponseTime());
saveResult.setType(type);
saveResult.setStatus(status);
if (StringUtils.equals(type, ApiRunMode.API_PLAN.name())) {
testPlanApiCaseService.setExecResult(item.getName(), status);
}
apiDefinitionExecResultMapper.insert(saveResult);
});
if (CollectionUtils.isNotEmpty(result.getScenarios())) {
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
ApiDefinitionExecResultMapper definitionExecResultMapper = sqlSession.getMapper(ApiDefinitionExecResultMapper.class);
result.getScenarios().get(0).getRequestResults().forEach(item -> {
ApiDefinitionExecResult saveResult = new ApiDefinitionExecResult();
saveResult.setId(UUID.randomUUID().toString());
saveResult.setCreateTime(System.currentTimeMillis());
saveResult.setUserId(Objects.requireNonNull(SessionUtils.getUser()).getId());
saveResult.setName(item.getName());
saveResult.setResourceId(item.getName());
saveResult.setContent(JSON.toJSONString(item));
saveResult.setStartTime(item.getStartTime());
String status = item.isSuccess() ? "success" : "error";
saveResult.setEndTime(item.getResponseResult().getResponseTime());
saveResult.setType(type);
saveResult.setStatus(status);
if (StringUtils.equals(type, ApiRunMode.API_PLAN.name())) {
testPlanApiCaseService.setExecResult(item.getName(), status);
}
definitionExecResultMapper.insert(saveResult);
});
sqlSession.flushStatements();
}
}
/**
* 定时任务触发的保存逻辑
* 定时任务时userID要改为定时任务中的用户
* 定时任务时userID要改为定时任务中的用户
*
* @param result
* @param type
*/
@ -87,7 +94,7 @@ public class ApiDefinitionExecResultService {
userID = scheduleCreateUser;
apiCase.setStatus(status);
testPlanApiCaseService.updateByPrimaryKeySelective(apiCase);
}else {
} else {
userID = Objects.requireNonNull(SessionUtils.getUser()).getId();
testPlanApiCaseService.setExecResult(item.getName(), status);
}
@ -144,26 +151,26 @@ public class ApiDefinitionExecResultService {
if (startTime == null) {
return new ArrayList<>(0);
} else {
List<ExecutedCaseInfoResult>list = extApiDefinitionExecResultMapper.findFaliureCaseInfoByProjectIDAndExecuteTimeAndLimitNumber(projectId, startTime.getTime());
List<ExecutedCaseInfoResult> list = extApiDefinitionExecResultMapper.findFaliureCaseInfoByProjectIDAndExecuteTimeAndLimitNumber(projectId, startTime.getTime());
List<ExecutedCaseInfoResult> returnList = new ArrayList<>(limitNumber);
for(int i = 0;i<list.size();i++){
if(i<limitNumber){
for (int i = 0; i < list.size(); i++) {
if (i < limitNumber) {
//开始遍历查询TestPlan信息 --> 提供前台做超链接
ExecutedCaseInfoResult item = list.get(i);
QueryTestPlanRequest planRequest = new QueryTestPlanRequest();
planRequest.setProjectId(projectId);
if("scenario".equals(item.getCaseType())){
if ("scenario".equals(item.getCaseType())) {
planRequest.setScenarioId(item.getTestCaseID());
}else if("apiCase".equals(item.getCaseType())){
} else if ("apiCase".equals(item.getCaseType())) {
planRequest.setApiId(item.getTestCaseID());
}
List<TestPlanDTO> dtoList = testPlanService.selectTestPlanByRelevancy(planRequest);
item.setTestPlanDTOList(dtoList);
returnList.add(item);
}else {
} else {
break;
}
}

View File

@ -222,7 +222,6 @@ public class ApiTestCaseService {
test.setUpdateUserId(Objects.requireNonNull(SessionUtils.getUser()).getId());
test.setProjectId(request.getProjectId());
test.setRequest(JSONObject.toJSONString(request.getRequest()));
test.setResponse(JSONObject.toJSONString(request.getResponse()));
test.setPriority(request.getPriority());
test.setUpdateTime(System.currentTimeMillis());
test.setDescription(request.getDescription());
@ -242,7 +241,6 @@ public class ApiTestCaseService {
test.setUpdateUserId(Objects.requireNonNull(SessionUtils.getUser()).getId());
test.setProjectId(request.getProjectId());
test.setRequest(JSONObject.toJSONString(request.getRequest()));
test.setResponse(JSONObject.toJSONString(request.getResponse()));
test.setCreateTime(System.currentTimeMillis());
test.setPriority(request.getPriority());
test.setUpdateTime(System.currentTimeMillis());
@ -438,7 +436,7 @@ public class ApiTestCaseService {
// 多态JSON普通转换会丢失内容需要通过 ObjectMapper 获取
if (testCaseWithBLOBs != null && StringUtils.isNotEmpty(testCaseWithBLOBs.getRequest())) {
try {
HashTree jmeterHashTree = this.generateHashTree(request,testCaseWithBLOBs);
HashTree jmeterHashTree = this.generateHashTree(request, testCaseWithBLOBs);
String runMode = ApiRunMode.DEFINITION.name();
// 调用执行方法
jMeterService.runDefinition(request.getReportId(), jmeterHashTree, request.getReportId(), runMode);
@ -449,7 +447,8 @@ public class ApiTestCaseService {
}
return request.getReportId();
}
public String run(ApiTestCaseWithBLOBs apiCaseBolbs,String id,String debugReportId,String testPlanID,String runMode) {
public String run(ApiTestCaseWithBLOBs apiCaseBolbs, String id, String debugReportId, String testPlanID, String runMode) {
// 多态JSON普通转换会丢失内容需要通过 ObjectMapper 获取
if (apiCaseBolbs != null && StringUtils.isNotEmpty(apiCaseBolbs.getRequest())) {
try {
@ -457,7 +456,7 @@ public class ApiTestCaseService {
RunCaseRequest request = new RunCaseRequest();
request.setCaseId(apiTestCase.getId());
request.setTestPlanId(testPlanID);
HashTree jmeterHashTree = this.generateHashTree(request,apiCaseBolbs);
HashTree jmeterHashTree = this.generateHashTree(request, apiCaseBolbs);
// 调用执行方法
jMeterService.runDefinition(id, jmeterHashTree, debugReportId, runMode);
@ -467,18 +466,19 @@ public class ApiTestCaseService {
}
return id;
}
public HashTree generateHashTree(RunCaseRequest request,ApiTestCaseWithBLOBs testCaseWithBLOBs) throws Exception {
public HashTree generateHashTree(RunCaseRequest request, ApiTestCaseWithBLOBs testCaseWithBLOBs) throws Exception {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
MsTestElement element = mapper.readValue(testCaseWithBLOBs.getRequest(), new TypeReference<MsTestElement>() {
});
if(StringUtils.isBlank(request.getEnvironmentId())){
if (StringUtils.isBlank(request.getEnvironmentId())) {
TestPlanApiCaseExample example = new TestPlanApiCaseExample();
example.createCriteria().andTestPlanIdEqualTo(request.getTestPlanId()).andApiCaseIdEqualTo(request.getCaseId());
List<TestPlanApiCase> list=testPlanApiCaseMapper.selectByExample(example);
List<TestPlanApiCase> list = testPlanApiCaseMapper.selectByExample(example);
request.setEnvironmentId(list.get(0).getEnvironmentId());
element.setName(list.get(0).getId());
}else{
} else {
element.setName(request.getCaseId());
}
@ -498,19 +498,19 @@ public class ApiTestCaseService {
testPlan.getHashTree().add(group);
ApiTestEnvironmentService environmentService = CommonBeanFactory.getBean(ApiTestEnvironmentService.class);
ApiTestEnvironmentWithBLOBs environment = environmentService.get(request.getEnvironmentId());
ParameterConfig parameterConfig=new ParameterConfig();
ParameterConfig parameterConfig = new ParameterConfig();
if (environment != null && environment.getConfig() != null) {
parameterConfig.setConfig( JSONObject.parseObject(environment.getConfig(), EnvironmentConfig.class));
parameterConfig.setConfig(JSONObject.parseObject(environment.getConfig(), EnvironmentConfig.class));
}
testPlan.toHashTree(jmeterHashTree, testPlan.getHashTree(), parameterConfig);
return jmeterHashTree;
}
public String getExecResult(String id){
public String getExecResult(String id) {
ApiDefinitionExecResultExample apidefinitionexecresultexample = new ApiDefinitionExecResultExample();
ApiDefinitionExecResultExample.Criteria criteria = apidefinitionexecresultexample.createCriteria();
criteria.andResourceIdEqualTo(id);
String status=apiDefinitionExecResultMapper.selectByExample(apidefinitionexecresultexample).get(0).getStatus();
String status = apiDefinitionExecResultMapper.selectByExample(apidefinitionexecresultexample).get(0).getStatus();
return status;
}
}

View File

@ -28,5 +28,7 @@ public class ApiTestCase implements Serializable {
private String tags;
private String lastResultId;
private static final long serialVersionUID = 1L;
}

View File

@ -843,6 +843,76 @@ public class ApiTestCaseExample {
addCriterion("tags not between", value1, value2, "tags");
return (Criteria) this;
}
public Criteria andLastResultIdIsNull() {
addCriterion("last_result_id is null");
return (Criteria) this;
}
public Criteria andLastResultIdIsNotNull() {
addCriterion("last_result_id is not null");
return (Criteria) this;
}
public Criteria andLastResultIdEqualTo(String value) {
addCriterion("last_result_id =", value, "lastResultId");
return (Criteria) this;
}
public Criteria andLastResultIdNotEqualTo(String value) {
addCriterion("last_result_id <>", value, "lastResultId");
return (Criteria) this;
}
public Criteria andLastResultIdGreaterThan(String value) {
addCriterion("last_result_id >", value, "lastResultId");
return (Criteria) this;
}
public Criteria andLastResultIdGreaterThanOrEqualTo(String value) {
addCriterion("last_result_id >=", value, "lastResultId");
return (Criteria) this;
}
public Criteria andLastResultIdLessThan(String value) {
addCriterion("last_result_id <", value, "lastResultId");
return (Criteria) this;
}
public Criteria andLastResultIdLessThanOrEqualTo(String value) {
addCriterion("last_result_id <=", value, "lastResultId");
return (Criteria) this;
}
public Criteria andLastResultIdLike(String value) {
addCriterion("last_result_id like", value, "lastResultId");
return (Criteria) this;
}
public Criteria andLastResultIdNotLike(String value) {
addCriterion("last_result_id not like", value, "lastResultId");
return (Criteria) this;
}
public Criteria andLastResultIdIn(List<String> values) {
addCriterion("last_result_id in", values, "lastResultId");
return (Criteria) this;
}
public Criteria andLastResultIdNotIn(List<String> values) {
addCriterion("last_result_id not in", values, "lastResultId");
return (Criteria) this;
}
public Criteria andLastResultIdBetween(String value1, String value2) {
addCriterion("last_result_id between", value1, value2, "lastResultId");
return (Criteria) this;
}
public Criteria andLastResultIdNotBetween(String value1, String value2) {
addCriterion("last_result_id not between", value1, value2, "lastResultId");
return (Criteria) this;
}
}
public static class Criteria extends GeneratedCriteria {

View File

@ -13,7 +13,5 @@ public class ApiTestCaseWithBLOBs extends ApiTestCase implements Serializable {
private String request;
private String response;
private static final long serialVersionUID = 1L;
}

View File

@ -13,11 +13,11 @@
<result column="update_time" jdbcType="BIGINT" property="updateTime" />
<result column="num" jdbcType="INTEGER" property="num" />
<result column="tags" jdbcType="VARCHAR" property="tags" />
<result column="last_result_id" jdbcType="VARCHAR" property="lastResultId" />
</resultMap>
<resultMap extends="BaseResultMap" id="ResultMapWithBLOBs" type="io.metersphere.base.domain.ApiTestCaseWithBLOBs">
<result column="description" jdbcType="LONGVARCHAR" property="description" />
<result column="request" jdbcType="LONGVARCHAR" property="request" />
<result column="response" jdbcType="LONGVARCHAR" property="response" />
</resultMap>
<sql id="Example_Where_Clause">
<where>
@ -79,10 +79,10 @@
</sql>
<sql id="Base_Column_List">
id, project_id, `name`, priority, api_definition_id, create_user_id, update_user_id,
create_time, update_time, num, tags
create_time, update_time, num, tags, last_result_id
</sql>
<sql id="Blob_Column_List">
description, request, response
description, request
</sql>
<select id="selectByExampleWithBLOBs" parameterType="io.metersphere.base.domain.ApiTestCaseExample" resultMap="ResultMapWithBLOBs">
select
@ -136,13 +136,13 @@
insert into api_test_case (id, project_id, `name`,
priority, api_definition_id, create_user_id,
update_user_id, create_time, update_time,
num, tags, description,
request, response)
num, tags, last_result_id,
description, request)
values (#{id,jdbcType=VARCHAR}, #{projectId,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR},
#{priority,jdbcType=VARCHAR}, #{apiDefinitionId,jdbcType=VARCHAR}, #{createUserId,jdbcType=VARCHAR},
#{updateUserId,jdbcType=VARCHAR}, #{createTime,jdbcType=BIGINT}, #{updateTime,jdbcType=BIGINT},
#{num,jdbcType=INTEGER}, #{tags,jdbcType=VARCHAR}, #{description,jdbcType=LONGVARCHAR},
#{request,jdbcType=LONGVARCHAR}, #{response,jdbcType=LONGVARCHAR})
#{num,jdbcType=INTEGER}, #{tags,jdbcType=VARCHAR}, #{lastResultId,jdbcType=VARCHAR},
#{description,jdbcType=LONGVARCHAR}, #{request,jdbcType=LONGVARCHAR})
</insert>
<insert id="insertSelective" parameterType="io.metersphere.base.domain.ApiTestCaseWithBLOBs">
insert into api_test_case
@ -180,15 +180,15 @@
<if test="tags != null">
tags,
</if>
<if test="lastResultId != null">
last_result_id,
</if>
<if test="description != null">
description,
</if>
<if test="request != null">
request,
</if>
<if test="response != null">
response,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
@ -224,15 +224,15 @@
<if test="tags != null">
#{tags,jdbcType=VARCHAR},
</if>
<if test="lastResultId != null">
#{lastResultId,jdbcType=VARCHAR},
</if>
<if test="description != null">
#{description,jdbcType=LONGVARCHAR},
</if>
<if test="request != null">
#{request,jdbcType=LONGVARCHAR},
</if>
<if test="response != null">
#{response,jdbcType=LONGVARCHAR},
</if>
</trim>
</insert>
<select id="countByExample" parameterType="io.metersphere.base.domain.ApiTestCaseExample" resultType="java.lang.Long">
@ -277,15 +277,15 @@
<if test="record.tags != null">
tags = #{record.tags,jdbcType=VARCHAR},
</if>
<if test="record.lastResultId != null">
last_result_id = #{record.lastResultId,jdbcType=VARCHAR},
</if>
<if test="record.description != null">
description = #{record.description,jdbcType=LONGVARCHAR},
</if>
<if test="record.request != null">
request = #{record.request,jdbcType=LONGVARCHAR},
</if>
<if test="record.response != null">
response = #{record.response,jdbcType=LONGVARCHAR},
</if>
</set>
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
@ -304,9 +304,9 @@
update_time = #{record.updateTime,jdbcType=BIGINT},
num = #{record.num,jdbcType=INTEGER},
tags = #{record.tags,jdbcType=VARCHAR},
last_result_id = #{record.lastResultId,jdbcType=VARCHAR},
description = #{record.description,jdbcType=LONGVARCHAR},
request = #{record.request,jdbcType=LONGVARCHAR},
response = #{record.response,jdbcType=LONGVARCHAR}
request = #{record.request,jdbcType=LONGVARCHAR}
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
</if>
@ -323,7 +323,8 @@
create_time = #{record.createTime,jdbcType=BIGINT},
update_time = #{record.updateTime,jdbcType=BIGINT},
num = #{record.num,jdbcType=INTEGER},
tags = #{record.tags,jdbcType=VARCHAR}
tags = #{record.tags,jdbcType=VARCHAR},
last_result_id = #{record.lastResultId,jdbcType=VARCHAR}
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
</if>
@ -361,15 +362,15 @@
<if test="tags != null">
tags = #{tags,jdbcType=VARCHAR},
</if>
<if test="lastResultId != null">
last_result_id = #{lastResultId,jdbcType=VARCHAR},
</if>
<if test="description != null">
description = #{description,jdbcType=LONGVARCHAR},
</if>
<if test="request != null">
request = #{request,jdbcType=LONGVARCHAR},
</if>
<if test="response != null">
response = #{response,jdbcType=LONGVARCHAR},
</if>
</set>
where id = #{id,jdbcType=VARCHAR}
</update>
@ -385,9 +386,9 @@
update_time = #{updateTime,jdbcType=BIGINT},
num = #{num,jdbcType=INTEGER},
tags = #{tags,jdbcType=VARCHAR},
last_result_id = #{lastResultId,jdbcType=VARCHAR},
description = #{description,jdbcType=LONGVARCHAR},
request = #{request,jdbcType=LONGVARCHAR},
response = #{response,jdbcType=LONGVARCHAR}
request = #{request,jdbcType=LONGVARCHAR}
where id = #{id,jdbcType=VARCHAR}
</update>
<update id="updateByPrimaryKey" parameterType="io.metersphere.base.domain.ApiTestCase">
@ -401,7 +402,8 @@
create_time = #{createTime,jdbcType=BIGINT},
update_time = #{updateTime,jdbcType=BIGINT},
num = #{num,jdbcType=INTEGER},
tags = #{tags,jdbcType=VARCHAR}
tags = #{tags,jdbcType=VARCHAR},
last_result_id = #{lastResultId,jdbcType=VARCHAR}
where id = #{id,jdbcType=VARCHAR}
</update>
</mapper>

View File

@ -15,7 +15,6 @@
</resultMap>
<resultMap extends="BaseResultMap" id="ResultMapWithBLOBs" type="io.metersphere.base.domain.ApiTestCase">
<result column="request" jdbcType="LONGVARCHAR" property="request"/>
<result column="response" jdbcType="LONGVARCHAR" property="response"/>
</resultMap>
<sql id="Example_Where_Clause">
<where>
@ -200,7 +199,6 @@
u2.name as updateUser,
atc.description,
atc.request,
atc.response,
atc.create_user_id,
atc.create_time,
atc.update_user_id,

View File

@ -0,0 +1,2 @@
ALTER TABLE api_test_case drop COLUMN response;
ALTER TABLE api_test_case add COLUMN last_result_id varchar(64) COMMENT 'Last ApiDefinitionExecResult ID';

View File

@ -304,4 +304,8 @@
.is-selected {
background: #EFF7FF;
}
.icon.is-active {
transform: rotate(90deg);
}
</style>