feat: 接口用例支持自定义排序

This commit is contained in:
chenjianxing 2021-09-09 14:49:10 +08:00 committed by jianxing
parent 89684c404c
commit 0236bc3a6e
14 changed files with 205 additions and 48 deletions

View File

@ -12,6 +12,7 @@ import io.metersphere.commons.constants.NoticeConstants;
import io.metersphere.commons.constants.OperLogConstants;
import io.metersphere.commons.utils.PageUtils;
import io.metersphere.commons.utils.Pager;
import io.metersphere.controller.request.ResetOrderRequest;
import io.metersphere.log.annotation.MsAuditLog;
import io.metersphere.notice.annotation.SendNotice;
import io.metersphere.track.request.testcase.ApiCaseRelevanceRequest;
@ -133,6 +134,11 @@ public class ApiTestCaseController {
apiTestCaseService.editApiBathByParam(request);
}
@PostMapping("/edit/order")
public void orderCase(@RequestBody ResetOrderRequest request) {
apiTestCaseService.updateOrder(request);
}
@PostMapping("/reduction")
@MsAuditLog(module = "api_definition", type = OperLogConstants.RESTORE, beforeEvent = "#msClass.getLogDetails(#request.ids)", content = "#msClass.getLogDetails(#request.ids)", msClass = ApiTestCaseService.class)
public List<String> reduction(@RequestBody ApiTestBatchRequest request) {

View File

@ -24,6 +24,8 @@ import io.metersphere.base.mapper.ext.*;
import io.metersphere.commons.constants.*;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.*;
import io.metersphere.controller.request.OrderRequest;
import io.metersphere.controller.request.ResetOrderRequest;
import io.metersphere.i18n.Translator;
import io.metersphere.log.utils.ReflexObjectUtil;
import io.metersphere.log.vo.DetailColumn;
@ -102,7 +104,7 @@ public class ApiTestCaseService {
}
public List<ApiTestCaseResult> list(ApiTestCaseRequest request) {
request.setOrders(ServiceUtils.getDefaultOrder(request.getOrders()));
initRequest(request, true, false);
if (request.getModuleIds() == null && request.getModuleId() != null) {
List<String> moduleIds = new ArrayList<>();
moduleIds.add(request.getModuleId());
@ -145,7 +147,9 @@ public class ApiTestCaseService {
*/
private ApiTestCaseRequest initRequest(ApiTestCaseRequest request, boolean setDefultOrders, boolean checkThisWeekData) {
if (setDefultOrders) {
request.setOrders(ServiceUtils.getDefaultOrder(request.getOrders()));
List<OrderRequest> orders = ServiceUtils.getDefaultSortOrder(request.getOrders());
orders.forEach(i -> i.setPrefix("t1"));
request.setOrders(orders);
}
if (checkThisWeekData) {
if (request.isSelectThisWeedData()) {
@ -337,15 +341,14 @@ public class ApiTestCaseService {
test.setDescription(request.getDescription());
test.setNum(getNextNum(request.getApiDefinitionId()));
test.setFollowPeople(request.getFollowPeople());
test.setOrder(ServiceUtils.getNextOrder(request.getProjectId(), extApiTestCaseMapper::getLastOrder));
if (StringUtils.equals("[]", request.getTags())) {
test.setTags("");
} else {
test.setTags(request.getTags());
}
ApiTestCaseWithBLOBs apiTestCaseWithBLOBs = apiTestCaseMapper.selectByPrimaryKey(test.getId());
if (apiTestCaseWithBLOBs != null) {
apiTestCaseMapper.updateByPrimaryKey(apiTestCaseWithBLOBs);
} else {
if (apiTestCaseWithBLOBs == null) {
apiTestCaseMapper.insert(test);
}
return test;
@ -1018,4 +1021,22 @@ public class ApiTestCaseService {
}
return new ArrayList<>();
}
public void initOrderField() {
ServiceUtils.initOrderField(ApiTestCaseWithBLOBs.class, ApiTestCaseMapper.class,
extApiTestCaseMapper::selectProjectIds,
extApiTestCaseMapper::getIdsOrderByCreateTime);
}
/**
* 用例自定义排序
* @param request
*/
public void updateOrder(ResetOrderRequest request) {
ServiceUtils.updateOrderField(request, ApiTestCaseWithBLOBs.class,
apiTestCaseMapper::selectByPrimaryKey,
extApiTestCaseMapper::getPreOrder,
extApiTestCaseMapper::getLastOrder,
apiTestCaseMapper::updateByPrimaryKeySelective);
}
}

View File

@ -41,5 +41,7 @@ public class ApiTestCase implements Serializable {
private String followPeople;
private Long order;
private static final long serialVersionUID = 1L;
}
}

View File

@ -1313,6 +1313,66 @@ public class ApiTestCaseExample {
addCriterion("follow_people not between", value1, value2, "followPeople");
return (Criteria) this;
}
public Criteria andOrderIsNull() {
addCriterion("`order` is null");
return (Criteria) this;
}
public Criteria andOrderIsNotNull() {
addCriterion("`order` is not null");
return (Criteria) this;
}
public Criteria andOrderEqualTo(Long value) {
addCriterion("`order` =", value, "order");
return (Criteria) this;
}
public Criteria andOrderNotEqualTo(Long value) {
addCriterion("`order` <>", value, "order");
return (Criteria) this;
}
public Criteria andOrderGreaterThan(Long value) {
addCriterion("`order` >", value, "order");
return (Criteria) this;
}
public Criteria andOrderGreaterThanOrEqualTo(Long value) {
addCriterion("`order` >=", value, "order");
return (Criteria) this;
}
public Criteria andOrderLessThan(Long value) {
addCriterion("`order` <", value, "order");
return (Criteria) this;
}
public Criteria andOrderLessThanOrEqualTo(Long value) {
addCriterion("`order` <=", value, "order");
return (Criteria) this;
}
public Criteria andOrderIn(List<Long> values) {
addCriterion("`order` in", values, "order");
return (Criteria) this;
}
public Criteria andOrderNotIn(List<Long> values) {
addCriterion("`order` not in", values, "order");
return (Criteria) this;
}
public Criteria andOrderBetween(Long value1, Long value2) {
addCriterion("`order` between", value1, value2, "order");
return (Criteria) this;
}
public Criteria andOrderNotBetween(Long value1, Long value2) {
addCriterion("`order` not between", value1, value2, "order");
return (Criteria) this;
}
}
public static class Criteria extends GeneratedCriteria {
@ -1407,4 +1467,4 @@ public class ApiTestCaseExample {
this(condition, value, secondValue, null);
}
}
}
}

View File

@ -20,6 +20,7 @@
<result column="delete_user_id" jdbcType="VARCHAR" property="deleteUserId" />
<result column="version" jdbcType="INTEGER" property="version" />
<result column="follow_people" jdbcType="VARCHAR" property="followPeople" />
<result column="order" jdbcType="BIGINT" property="order" />
</resultMap>
<resultMap extends="BaseResultMap" id="ResultMapWithBLOBs" type="io.metersphere.base.domain.ApiTestCaseWithBLOBs">
<result column="description" jdbcType="LONGVARCHAR" property="description" />
@ -84,9 +85,9 @@
</where>
</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, last_result_id, `status`, original_status, delete_time,
delete_user_id, version, follow_people
id, project_id, `name`, priority, api_definition_id, create_user_id, update_user_id,
create_time, update_time, num, tags, last_result_id, `status`, original_status, delete_time,
delete_user_id, version, follow_people, `order`
</sql>
<sql id="Blob_Column_List">
description, request
@ -122,7 +123,7 @@
</if>
</select>
<select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="ResultMapWithBLOBs">
select
select
<include refid="Base_Column_List" />
,
<include refid="Blob_Column_List" />
@ -140,20 +141,22 @@
</if>
</delete>
<insert id="insert" parameterType="io.metersphere.base.domain.ApiTestCaseWithBLOBs">
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, last_result_id,
`status`, original_status, delete_time,
delete_user_id, version, follow_people,
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}, #{lastResultId,jdbcType=VARCHAR},
#{status,jdbcType=VARCHAR}, #{originalStatus,jdbcType=VARCHAR}, #{deleteTime,jdbcType=BIGINT},
#{deleteUserId,jdbcType=VARCHAR}, #{version,jdbcType=INTEGER}, #{followPeople,jdbcType=VARCHAR},
#{description,jdbcType=LONGVARCHAR}, #{request,jdbcType=LONGVARCHAR})
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, last_result_id,
`status`, original_status, delete_time,
delete_user_id, version, follow_people,
`order`, 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}, #{lastResultId,jdbcType=VARCHAR},
#{status,jdbcType=VARCHAR}, #{originalStatus,jdbcType=VARCHAR}, #{deleteTime,jdbcType=BIGINT},
#{deleteUserId,jdbcType=VARCHAR}, #{version,jdbcType=INTEGER}, #{followPeople,jdbcType=VARCHAR},
#{order,jdbcType=BIGINT}, #{description,jdbcType=LONGVARCHAR}, #{request,jdbcType=LONGVARCHAR}
)
</insert>
<insert id="insertSelective" parameterType="io.metersphere.base.domain.ApiTestCaseWithBLOBs">
insert into api_test_case
@ -212,6 +215,9 @@
<if test="followPeople != null">
follow_people,
</if>
<if test="order != null">
`order`,
</if>
<if test="description != null">
description,
</if>
@ -274,6 +280,9 @@
<if test="followPeople != null">
#{followPeople,jdbcType=VARCHAR},
</if>
<if test="order != null">
#{order,jdbcType=BIGINT},
</if>
<if test="description != null">
#{description,jdbcType=LONGVARCHAR},
</if>
@ -345,6 +354,9 @@
<if test="record.followPeople != null">
follow_people = #{record.followPeople,jdbcType=VARCHAR},
</if>
<if test="record.order != null">
`order` = #{record.order,jdbcType=BIGINT},
</if>
<if test="record.description != null">
description = #{record.description,jdbcType=LONGVARCHAR},
</if>
@ -376,6 +388,7 @@
delete_user_id = #{record.deleteUserId,jdbcType=VARCHAR},
version = #{record.version,jdbcType=INTEGER},
follow_people = #{record.followPeople,jdbcType=VARCHAR},
`order` = #{record.order,jdbcType=BIGINT},
description = #{record.description,jdbcType=LONGVARCHAR},
request = #{record.request,jdbcType=LONGVARCHAR}
<if test="_parameter != null">
@ -401,7 +414,8 @@
delete_time = #{record.deleteTime,jdbcType=BIGINT},
delete_user_id = #{record.deleteUserId,jdbcType=VARCHAR},
version = #{record.version,jdbcType=INTEGER},
follow_people = #{record.followPeople,jdbcType=VARCHAR}
follow_people = #{record.followPeople,jdbcType=VARCHAR},
`order` = #{record.order,jdbcType=BIGINT}
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
</if>
@ -460,6 +474,9 @@
<if test="followPeople != null">
follow_people = #{followPeople,jdbcType=VARCHAR},
</if>
<if test="order != null">
`order` = #{order,jdbcType=BIGINT},
</if>
<if test="description != null">
description = #{description,jdbcType=LONGVARCHAR},
</if>
@ -488,6 +505,7 @@
delete_user_id = #{deleteUserId,jdbcType=VARCHAR},
version = #{version,jdbcType=INTEGER},
follow_people = #{followPeople,jdbcType=VARCHAR},
`order` = #{order,jdbcType=BIGINT},
description = #{description,jdbcType=LONGVARCHAR},
request = #{request,jdbcType=LONGVARCHAR}
where id = #{id,jdbcType=VARCHAR}
@ -510,7 +528,8 @@
delete_time = #{deleteTime,jdbcType=BIGINT},
delete_user_id = #{deleteUserId,jdbcType=VARCHAR},
version = #{version,jdbcType=INTEGER},
follow_people = #{followPeople,jdbcType=VARCHAR}
follow_people = #{followPeople,jdbcType=VARCHAR},
`order` = #{order,jdbcType=BIGINT}
where id = #{id,jdbcType=VARCHAR}
</update>
</mapper>
</mapper>

View File

@ -52,4 +52,12 @@ public interface ExtApiTestCaseMapper {
String selectNameById(String id);
List<String> selectIdsByQuery(BaseQueryRequest query);
List<String> selectProjectIds();
List<String> getIdsOrderByCreateTime(@Param("projectId") String projectId);
Long getPreOrder(@Param("projectId")String projectId, @Param("baseOrder") Long baseOrder);
Long getLastOrder(@Param("projectId")String projectId, @Param("baseOrder") Long baseOrder);
}

View File

@ -321,12 +321,7 @@
<if test="request.filters == null || request.filters.size() == 0 ">
and (t1.status is null or t1.status != 'Trash')
</if>
<if test="request.orders != null and request.orders.size() > 0">
order by
<foreach collection="request.orders" separator="," item="order">
${order.name} ${order.type}
</foreach>
</if>
<include refid="io.metersphere.base.mapper.ext.ExtBaseMapper.orders"/>
</select>
<select id="idSimple" resultType="java.lang.String">
select
@ -581,6 +576,26 @@
</if>
<include refid="queryWhereCondition"/>
</select>
<select id="selectProjectIds" resultType="java.lang.String">
select DISTINCT project_id from api_test_case;
</select>
<select id="getIdsOrderByCreateTime" resultType="java.lang.String">
select id from api_test_case where project_id = #{projectId} order by create_time DESC;
</select>
<select id="getPreOrder" resultType="java.lang.Long">
select `order` from api_test_case where project_id = #{projectId}
<if test="baseOrder != null">
and `order` &lt; #{baseOrder}
</if>
order by `order` desc limit 1;
</select>
<select id="getLastOrder" resultType="java.lang.Long">
select `order` from api_test_case where project_id = #{projectId}
<if test="baseOrder != null">
and `order` &gt; #{baseOrder}
</if>
order by `order` desc limit 1;
</select>
<update id="deleteToGc" parameterType="io.metersphere.api.dto.definition.ApiTestCaseRequest">
update api_test_case

View File

@ -209,4 +209,15 @@ public class ServiceUtils {
MSException.throwException("更新 order 字段失败");
}
}
/**
* 创建时获取下一个 order
* @param projectId
* @param getLastOrderFunc
* @return
*/
public static Long getNextOrder(String projectId, BiFunction<String, Long, Long> getLastOrderFunc) {
Long lastOrder = getLastOrderFunc.apply(projectId, null);
return (lastOrder == null ? 0 : lastOrder) + 5000;
}
}

View File

@ -3,6 +3,7 @@ package io.metersphere.listener;
import io.metersphere.api.jmeter.JMeterService;
import io.metersphere.api.jmeter.NewDriverManager;
import io.metersphere.api.service.ApiAutomationService;
import io.metersphere.api.service.ApiTestCaseService;
import io.metersphere.base.domain.JarConfig;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.commons.utils.RunInterface;
@ -48,6 +49,8 @@ public class AppStartListener implements ApplicationListener<ApplicationReadyEve
private PluginService pluginService;
@Resource
private TestCaseService testCaseService;
@Resource
private ApiTestCaseService apiTestCaseService;
@Value("${jmeter.home}")
private String jmeterHome;
@ -107,6 +110,7 @@ public class AppStartListener implements ApplicationListener<ApplicationReadyEve
initOnceOperate(issuesService::issuesCount, "init.issueCount");
initOnceOperate(performanceTestService::initScenarioLoadTest, "init.scenario.load.test");
initOnceOperate(testCaseService::initOrderField, "init.sort.test.case");
initOnceOperate(apiTestCaseService::initOrderField, "init.sort.api.test.case");
}
/**

View File

@ -166,16 +166,11 @@ public class TestCaseService {
testCase.setDemandName(testCase.getDemandName());
testCase.setCreateUser(SessionUtils.getUserId());
this.setNode(testCase);
setNextOrder(testCase);
testCase.setOrder(ServiceUtils.getNextOrder(testCase.getProjectId(), extTestCaseMapper::getLastOrder));
testCaseMapper.insert(testCase);
return testCase;
}
public synchronized void setNextOrder(TestCaseWithBLOBs testCase) {
Long lastOrder = extTestCaseMapper.getLastOrder(testCase.getProjectId(), null);
testCase.setOrder((lastOrder == null ? 0 : lastOrder) + 5000);
}
private void checkTestCustomNum(TestCaseWithBLOBs testCase) {
if (StringUtils.isNotBlank(testCase.getCustomNum())) {
String projectId = testCase.getProjectId();

View File

@ -35,3 +35,4 @@ update api_scenario set module_path = replace (`module_path`,'/默认模块','/
ALTER TABLE test_case ADD `order` bigint(20) NOT NULL COMMENT '自定义排序间隔5000';
ALTER TABLE api_test_case ADD `order` bigint(20) NOT NULL COMMENT '自定义排序间隔5000';

View File

@ -20,6 +20,8 @@
:fields.sync="fields"
:field-key="tableHeaderKey"
:remember-order="true"
:enable-order-drag="enableOrderDrag"
row-key="id"
operator-width="190px"
@refresh="initTable"
ref="caseTable"
@ -112,6 +114,7 @@
<template v-slot:default="scope">
<ms-tag v-for="(itemName,index) in scope.row.tags" :key="index" type="success" effect="plain"
:content="itemName" style="margin-left: 0px; margin-right: 2px"/>
<span/>
</template>
</ms-table-column>
@ -213,12 +216,13 @@ import {
_filter,
_sort,
getCustomTableHeader,
getCustomTableWidth, getLastTableSortField
getCustomTableWidth, getLastTableSortField, handleRowDrop
} from "@/common/js/tableUtils";
import {API_CASE_LIST} from "@/common/js/constants";
import HeaderLabelOperate from "@/business/components/common/head/HeaderLabelOperate";
import ApiCaseBatchRun from "@/business/components/api/definition/components/list/ApiCaseBatchRun";
import MsRequestResultTail from "../../../../api/definition/components/response/RequestResultTail";
import {editApiTestCaseOrder} from "@/network/api";
export default {
name: "ApiCaseSimpleList",
@ -260,6 +264,7 @@ export default {
selectDataRange: "all",
clickRow: {},
buttons: [],
enableOrderDrag: true,
simpleButtons: [
{name: this.$t('api_test.definition.request.batch_delete'), handleClick: this.handleDeleteToGcBatch},
{name: this.$t('api_test.definition.request.batch_edit'), handleClick: this.handleEditBatch},
@ -374,8 +379,6 @@ export default {
planId: String
},
created: function () {
this.condition.orders = getLastTableSortField(this.tableHeaderKey);
if (this.trashEnable) {
this.operators = this.trashOperators;
this.buttons = this.trashButtons;
@ -493,12 +496,16 @@ export default {
if (this.$refs.caseTable) {
this.$refs.caseTable.clearSelectRows();
}
this.condition.orders = getLastTableSortField(this.tableHeaderKey);
if (this.condition.orders) {
const index = this.condition.orders.findIndex(d => d.name && d.name === 'case_path');
if (index !== -1) {
this.condition.orders.splice(index, 1);
}
}
this.enableOrderDrag = this.condition.orders.length > 0 ? false : true;
if (this.apiDefinitionId) {
this.condition.apiDefinitionId = this.apiDefinitionId;
}
@ -564,6 +571,12 @@ export default {
}
})
this.$nextTick(function () {
handleRowDrop(this.tableData, (param) => {
param.projectId = this.condition.projectId;
editApiTestCaseOrder(param);
});
if (this.$refs.caseTable) {
this.$refs.caseTable.doLayout();
this.$refs.caseTable.checkTableRowIsSelect();

View File

@ -518,10 +518,9 @@ export default {
//initCondition(this.condition);
initCondition(this.condition, this.condition.selectAll);
this.condition.orders = getLastTableSortField(this.tableHeaderKey);
this.enableOrderDrag = true;
if (this.condition.orders.length > 0) {
this.enableOrderDrag = false;
}
this.enableOrderDrag = this.condition.orders.length > 0 ? false : true;
if (this.planId) {
// param.planId = this.planId;
this.condition.planId = this.planId;

View File

@ -1,6 +1,6 @@
import {post} from "@/common/js/ajax";
import {success} from "@/common/js/message";
import {baseGet} from "@/network/base-network";
import {baseGet, basePost} from "@/network/base-network";
export function apiCaseBatchRun(condition) {
return post('/api/testcase/batch/run', condition, () => {
@ -25,3 +25,6 @@ export function getShareScenarioReport(shareId, reportId, callback) {
}
export function editApiTestCaseOrder(request, callback) {
return basePost('/api/testcase/edit/order', request, callback);
}