Merge remote-tracking branch 'origin/master'

This commit is contained in:
song.tianyang 2021-01-20 11:06:14 +08:00
commit b58dd49a26
9 changed files with 209 additions and 110 deletions

View File

@ -5,8 +5,10 @@ import io.metersphere.api.dto.datacount.ExecutedCaseInfoResult;
import io.metersphere.api.jmeter.TestResult;
import io.metersphere.base.domain.ApiDefinitionExecResult;
import io.metersphere.base.domain.ApiDefinitionExecResultExample;
import io.metersphere.base.domain.ApiTestCaseWithBLOBs;
import io.metersphere.base.domain.TestPlanApiCase;
import io.metersphere.base.mapper.ApiDefinitionExecResultMapper;
import io.metersphere.base.mapper.ApiTestCaseMapper;
import io.metersphere.base.mapper.ext.ExtApiDefinitionExecResultMapper;
import io.metersphere.commons.constants.ApiRunMode;
import io.metersphere.commons.utils.DateUtils;
@ -15,15 +17,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,9 +39,16 @@ public class ApiDefinitionExecResultService {
private TestPlanApiCaseService testPlanApiCaseService;
@Resource
private TestPlanService testPlanService;
@Resource
private ApiTestCaseMapper apiTestCaseMapper;
@Resource
SqlSessionFactory sqlSessionFactory;
public void saveApiResult(TestResult result, String type) {
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());
@ -56,13 +65,22 @@ public class ApiDefinitionExecResultService {
if (StringUtils.equals(type, ApiRunMode.API_PLAN.name())) {
testPlanApiCaseService.setExecResult(item.getName(), status);
}
apiDefinitionExecResultMapper.insert(saveResult);
// 更新用例最后执行结果
ApiTestCaseWithBLOBs apiTestCaseWithBLOBs = new ApiTestCaseWithBLOBs();
apiTestCaseWithBLOBs.setId(saveResult.getResourceId());
apiTestCaseWithBLOBs.setLastResultId(saveResult.getId());
apiTestCaseMapper.updateByPrimaryKeySelective(apiTestCaseWithBLOBs);
definitionExecResultMapper.insert(saveResult);
});
sqlSession.flushStatements();
}
}
/**
* 定时任务触发的保存逻辑
* 定时任务时userID要改为定时任务中的用户
*
* @param result
* @param type
*/
@ -87,7 +105,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 +162,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

@ -239,7 +239,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());
@ -259,7 +258,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());
@ -455,7 +453,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);
@ -466,7 +464,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 {
@ -474,7 +473,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);
@ -484,18 +483,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());
}
@ -515,19 +515,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>
@ -190,39 +189,18 @@
</sql>
<select id="list" resultType="io.metersphere.api.dto.definition.ApiTestCaseResult">
select
atc.id,
atc.project_id,
atc.name,
atc.priority,
atc.api_definition_id,
u1.name as createUser ,
u2.name as updateUser,
atc.description,
atc.request,
atc.response,
atc.create_user_id,
atc.create_time,
atc.update_user_id,
atc.update_time,
atc.num,
atc.tags,
ader.status execResult,
ader.create_time execTime
from
api_test_case atc
left join user u1 on
atc.create_user_id = u1.id
left join user u2 on
atc.update_user_id = u2.id
left join (
select
max(create_time) create_time ,status ,id, resource_id
from
api_definition_exec_result
group by
resource_id) as ader
on atc.id = ader.resource_id
SELECT
t1.*,
t2.STATUS AS execResult,
t2.create_time AS execTime,
u2.NAME AS createUser,
u1.NAME AS updateUser
FROM
api_test_case t1
LEFT JOIN api_definition_exec_result t2 ON t1.last_result_id = t2.id
LEFT JOIN USER u1 ON t1.update_user_id = u1.id
LEFT JOIN USER u2 ON t1.create_user_id = u2.id
LEFT JOIN USER u3 ON t2.user_id = u3.id
<where>
<if test="request.combine != null">
<include refid="combine">
@ -232,26 +210,26 @@
</if>
<if test="request.name != null and request.name!=''">
and atc.name like CONCAT('%', #{request.name},'%')
and t1.name like CONCAT('%', #{request.name},'%')
</if>
<if test="request.id != null and request.id!=''">
AND atc.id = #{request.id}
AND t1.id = #{request.id}
</if>
<if test="request.priority != null and request.priority!=''">
AND atc.priority = #{request.priority}
AND t1.priority = #{request.priority}
</if>
<if test="request.projectId != null and request.projectId!=''">
AND atc.project_id = #{request.projectId}
AND t1.project_id = #{request.projectId}
</if>
<if test="request.apiDefinitionId != null and request.apiDefinitionId!=''">
AND atc.api_definition_id = #{request.apiDefinitionId}
AND t1.api_definition_id = #{request.apiDefinitionId}
</if>
</where>
<if test="request.orders != null and request.orders.size() > 0">
order by
<foreach collection="request.orders" separator="," item="order">
atc.${order.name} ${order.type}
t1.${order.name} ${order.type}
</foreach>
</if>

View File

@ -0,0 +1,27 @@
ALTER TABLE api_test_case drop COLUMN response;
ALTER TABLE api_test_case add COLUMN last_result_id varchar(64) COMMENT 'Last ApiDefinitionExecResult ID';
UPDATE api_test_case tt
INNER JOIN (
SELECT
atc.id,
ader.id AS result_id
FROM
api_test_case atc
LEFT JOIN USER u1 ON atc.create_user_id = u1.id
LEFT JOIN USER u2 ON atc.update_user_id = u2.id
LEFT JOIN (
SELECT
max( create_time ) create_time,
STATUS,
id,
resource_id
FROM
api_definition_exec_result
GROUP BY
resource_id
) AS ader ON atc.id = ader.resource_id
) tc
SET tt.last_result_id = tc.result_id
WHERE
tt.id = tc.id;

View File

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