fix(Mock期望): #1007531 【接口测试】mock期望列表增加序号列

--bug=1007531 --user=宋天阳 【接口测试】mock期望列表增加序号列
https://www.tapd.cn/55049933/s/1062529
This commit is contained in:
song-tianyang 2021-11-01 18:54:12 +08:00 committed by song-tianyang
parent 04b1e49b0c
commit 213e9d0d01
12 changed files with 529 additions and 347 deletions

View File

@ -19,6 +19,8 @@ public class MockExpectConfigResponse {
private String id;
private String expectNum;
private String mockConfigId;
private String name;
@ -40,6 +42,7 @@ public class MockExpectConfigResponse {
public MockExpectConfigResponse(MockExpectConfigWithBLOBs expectConfig) {
this.id = expectConfig.getId();
this.mockConfigId = expectConfig.getMockConfigId();
this.expectNum = expectConfig.getExpectNum();
this.name = expectConfig.getName();
this.status = Boolean.parseBoolean(expectConfig.getStatus());
this.createTime = expectConfig.getCreateTime();

View File

@ -657,7 +657,7 @@ public class ApiDefinitionService {
apiDefinition.setId(UUID.randomUUID().toString());
apiDefinition.setOrder(getImportNextOrder(apiTestImportRequest.getProjectId()));
reSetImportCasesApiId(cases, originId, apiDefinition.getId());
reSetImportMocksApiId(mocks, originId, apiDefinition.getId());
reSetImportMocksApiId(mocks, originId, apiDefinition.getId(), apiDefinition.getNum());
if (StringUtils.equalsIgnoreCase(apiDefinition.getProtocol(), RequestType.HTTP)) {
batchMapper.insert(apiDefinition);
String request = setImportHashTree(apiDefinition);
@ -716,13 +716,16 @@ public class ApiDefinitionService {
}
}
private void reSetImportMocksApiId(List<MockConfigImportDTO> mocks, String originId, String newId) {
private void reSetImportMocksApiId(List<MockConfigImportDTO> mocks, String originId, String newId, int apiNum) {
if (CollectionUtils.isNotEmpty(mocks)) {
mocks.forEach(item -> {
int index = 1;
for(MockConfigImportDTO item : mocks){
if (StringUtils.equals(item.getApiId(), originId)) {
item.setApiId(newId);
}
});
item.setExpectNum(apiNum+"_"+index);
index++;
}
}
}

View File

@ -14,7 +14,6 @@ import io.metersphere.api.dto.mock.RequestMockParams;
import io.metersphere.api.dto.mockconfig.MockConfigImportDTO;
import io.metersphere.api.dto.mockconfig.MockConfigRequest;
import io.metersphere.api.dto.mockconfig.MockExpectConfigRequest;
import io.metersphere.api.dto.mockconfig.response.JsonSchemaReturnObj;
import io.metersphere.api.dto.mockconfig.response.MockConfigResponse;
import io.metersphere.api.dto.mockconfig.response.MockExpectConfigResponse;
import io.metersphere.base.domain.*;
@ -44,6 +43,7 @@ import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@Service
@Transactional(rollbackFor = Exception.class)
@ -109,15 +109,38 @@ public class MockConfigService {
expectConfigResponseList.add(response);
}
MockConfigResponse returnRsp = new MockConfigResponse(config, expectConfigResponseList);
return returnRsp;
return new MockConfigResponse(config, expectConfigResponseList);
} else {
return new MockConfigResponse(null, new ArrayList<>());
}
}
public void initExpectNum(){
MockExpectConfigExample example = new MockExpectConfigExample();
example.createCriteria().andExpectNumIsNull();
List<MockExpectConfigWithBLOBs> mockExpectConfigList = mockExpectConfigMapper.selectByExampleWithBLOBs(example);
Map<String,List<MockExpectConfigWithBLOBs>> mockConfigIdMap = mockExpectConfigList.stream().collect(Collectors.groupingBy(MockExpectConfig::getMockConfigId));
for (Map.Entry<String, List<MockExpectConfigWithBLOBs>> entry :
mockConfigIdMap.entrySet()) {
String mockConfigId = entry.getKey();
List<MockExpectConfigWithBLOBs> list = entry.getValue();
String apiNum = extMockExpectConfigMapper.selectApiNumberByMockConfigId(mockConfigId);
if(StringUtils.isEmpty(apiNum) || StringUtils.equalsIgnoreCase(apiNum,"null")){
continue;
}
int expectNumIndex = this.getMockExpectNumIndex(mockConfigId,apiNum);
for (MockExpectConfigWithBLOBs config : list) {
config.setExpectNum(apiNum+"_"+expectNumIndex);
mockExpectConfigMapper.updateByPrimaryKeySelective(config);
expectNumIndex ++;
}
}
}
public MockConfigResponse genMockConfig(MockConfigRequest request) {
MockConfigResponse returnRsp = null;
MockConfigResponse returnRsp;
MockConfigExample example = new MockConfigExample();
MockConfigExample.Criteria criteria = example.createCriteria();
@ -193,8 +216,11 @@ public class MockConfigService {
this.checkNameIsExists(request);
}
long timeStmp = System.currentTimeMillis();
String expectNum = this.getMockExpectId(request.getMockConfigId());
MockExpectConfigWithBLOBs model = new MockExpectConfigWithBLOBs();
model.setId(request.getId());
model.setExpectNum(expectNum);
model.setMockConfigId(request.getMockConfigId());
model.setUpdateTime(timeStmp);
model.setStatus(request.getStatus());
@ -223,6 +249,54 @@ public class MockConfigService {
return model;
}
private String getMockExpectId(String mockConfigId) {
List<String> savedExpectNumber = extMockExpectConfigMapper.selectExlectNumByMockConfigId(mockConfigId);
String apiNum = extMockExpectConfigMapper.selectApiNumberByMockConfigId(mockConfigId);
if(StringUtils.isEmpty(apiNum)){
apiNum = "";
}else {
apiNum = apiNum + "_";
}
int index = 1;
for(String expectNum : savedExpectNumber){
if(StringUtils.startsWith(expectNum,apiNum)){
String numStr = StringUtils.substringAfter(expectNum,apiNum);
try{
int savedIndex = Integer.parseInt(numStr);
if(index <= savedIndex){
index = savedIndex+1;
}
}catch (Exception ignored){}
}
}
return apiNum + index;
}
private int getMockExpectNumIndex(String mockConfigId,String apiNumber) {
List<String> savedExpectNumber = extMockExpectConfigMapper.selectExlectNumByMockConfigId(mockConfigId);
String apiNum = apiNumber;
if(StringUtils.isEmpty(apiNum)){
apiNum = "";
}else {
apiNum = apiNum + "_";
}
int index = 1;
for(String expectNum : savedExpectNumber){
if(StringUtils.startsWith(expectNum,apiNum)){
String numStr = StringUtils.substringAfter(expectNum,apiNum);
try{
int savedIndex = Integer.parseInt(numStr);
if(index <= savedIndex){
index = savedIndex+1;
}
}catch (Exception ignored ){}
}
}
return index;
}
private void checkNameIsExists(MockExpectConfigRequest request) {
MockExpectConfigExample example = new MockExpectConfigExample();
example.createCriteria().andMockConfigIdEqualTo(request.getMockConfigId()).andNameEqualTo(request.getName().trim()).andIdNotEqualTo(request.getId());
@ -259,14 +333,14 @@ public class MockConfigService {
continue;
}
JSONObject mockExpectRequestObj = model.getRequest();
boolean mathing = false;
boolean isMatch;
if (mockExpectRequestObj.containsKey("params")) {
mathing = this.isRequestMockExpectMatchingByParams(requestHeaderMap, mockExpectRequestObj, requestMockParams);
isMatch = this.isRequestMockExpectMatchingByParams(requestHeaderMap, mockExpectRequestObj, requestMockParams);
} else {
mathing = this.isRequestMockExpectMatching(mockExpectRequestObj, requestMockParams.getQueryParamsObj());
isMatch = this.isRequestMockExpectMatching(mockExpectRequestObj, requestMockParams.getQueryParamsObj());
}
if (mathing) {
if (isMatch) {
returnModel = model;
break;
}

View File

@ -1,7 +1,6 @@
package io.metersphere.base.domain;
import java.io.Serializable;
import lombok.Data;
@Data
@ -22,5 +21,7 @@ public class MockExpectConfig implements Serializable {
private String createUserId;
private String expectNum;
private static final long serialVersionUID = 1L;
}

View File

@ -643,6 +643,76 @@ public class MockExpectConfigExample {
addCriterion("create_user_id not between", value1, value2, "createUserId");
return (Criteria) this;
}
public Criteria andExpectNumIsNull() {
addCriterion("expect_num is null");
return (Criteria) this;
}
public Criteria andExpectNumIsNotNull() {
addCriterion("expect_num is not null");
return (Criteria) this;
}
public Criteria andExpectNumEqualTo(String value) {
addCriterion("expect_num =", value, "expectNum");
return (Criteria) this;
}
public Criteria andExpectNumNotEqualTo(String value) {
addCriterion("expect_num <>", value, "expectNum");
return (Criteria) this;
}
public Criteria andExpectNumGreaterThan(String value) {
addCriterion("expect_num >", value, "expectNum");
return (Criteria) this;
}
public Criteria andExpectNumGreaterThanOrEqualTo(String value) {
addCriterion("expect_num >=", value, "expectNum");
return (Criteria) this;
}
public Criteria andExpectNumLessThan(String value) {
addCriterion("expect_num <", value, "expectNum");
return (Criteria) this;
}
public Criteria andExpectNumLessThanOrEqualTo(String value) {
addCriterion("expect_num <=", value, "expectNum");
return (Criteria) this;
}
public Criteria andExpectNumLike(String value) {
addCriterion("expect_num like", value, "expectNum");
return (Criteria) this;
}
public Criteria andExpectNumNotLike(String value) {
addCriterion("expect_num not like", value, "expectNum");
return (Criteria) this;
}
public Criteria andExpectNumIn(List<String> values) {
addCriterion("expect_num in", values, "expectNum");
return (Criteria) this;
}
public Criteria andExpectNumNotIn(List<String> values) {
addCriterion("expect_num not in", values, "expectNum");
return (Criteria) this;
}
public Criteria andExpectNumBetween(String value1, String value2) {
addCriterion("expect_num between", value1, value2, "expectNum");
return (Criteria) this;
}
public Criteria andExpectNumNotBetween(String value1, String value2) {
addCriterion("expect_num not between", value1, value2, "expectNum");
return (Criteria) this;
}
}
public static class Criteria extends GeneratedCriteria {

View File

@ -1,7 +1,6 @@
package io.metersphere.base.domain;
import java.io.Serializable;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

View File

@ -3,9 +3,7 @@ package io.metersphere.base.mapper;
import io.metersphere.base.domain.MockExpectConfig;
import io.metersphere.base.domain.MockExpectConfigExample;
import io.metersphere.base.domain.MockExpectConfigWithBLOBs;
import java.util.List;
import org.apache.ibatis.annotations.Param;
public interface MockExpectConfigMapper {

View File

@ -10,9 +10,9 @@
<result column="create_time" jdbcType="BIGINT" property="createTime" />
<result column="update_time" jdbcType="BIGINT" property="updateTime" />
<result column="create_user_id" jdbcType="VARCHAR" property="createUserId" />
<result column="expect_num" jdbcType="VARCHAR" property="expectNum" />
</resultMap>
<resultMap extends="BaseResultMap" id="ResultMapWithBLOBs"
type="io.metersphere.base.domain.MockExpectConfigWithBLOBs">
<resultMap extends="BaseResultMap" id="ResultMapWithBLOBs" type="io.metersphere.base.domain.MockExpectConfigWithBLOBs">
<result column="request" jdbcType="LONGVARCHAR" property="request" />
<result column="response" jdbcType="LONGVARCHAR" property="response" />
</resultMap>
@ -34,8 +34,7 @@
</when>
<when test="criterion.listValue">
and ${criterion.condition}
<foreach close=")" collection="criterion.value" item="listItem" open="("
separator=",">
<foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
#{listItem}
</foreach>
</when>
@ -64,8 +63,7 @@
</when>
<when test="criterion.listValue">
and ${criterion.condition}
<foreach close=")" collection="criterion.value" item="listItem" open="("
separator=",">
<foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
#{listItem}
</foreach>
</when>
@ -77,13 +75,13 @@
</where>
</sql>
<sql id="Base_Column_List">
id, mock_config_id, `name`, tags, `STATUS`, create_time, update_time, create_user_id
id, mock_config_id, `name`, tags, `STATUS`, create_time, update_time, create_user_id,
expect_num
</sql>
<sql id="Blob_Column_List">
request, response
</sql>
<select id="selectByExampleWithBLOBs" parameterType="io.metersphere.base.domain.MockExpectConfigExample"
resultMap="ResultMapWithBLOBs">
<select id="selectByExampleWithBLOBs" parameterType="io.metersphere.base.domain.MockExpectConfigExample" resultMap="ResultMapWithBLOBs">
select
<if test="distinct">
distinct
@ -99,8 +97,7 @@
order by ${orderByClause}
</if>
</select>
<select id="selectByExample" parameterType="io.metersphere.base.domain.MockExpectConfigExample"
resultMap="BaseResultMap">
<select id="selectByExample" parameterType="io.metersphere.base.domain.MockExpectConfigExample" resultMap="BaseResultMap">
select
<if test="distinct">
distinct
@ -123,8 +120,7 @@
where id = #{id,jdbcType=VARCHAR}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.String">
delete
from mock_expect_config
delete from mock_expect_config
where id = #{id,jdbcType=VARCHAR}
</delete>
<delete id="deleteByExample" parameterType="io.metersphere.base.domain.MockExpectConfigExample">
@ -136,12 +132,12 @@
<insert id="insert" parameterType="io.metersphere.base.domain.MockExpectConfigWithBLOBs">
insert into mock_expect_config (id, mock_config_id, `name`,
tags, `STATUS`, create_time,
update_time, create_user_id, request,
response)
update_time, create_user_id, expect_num,
request, response)
values (#{id,jdbcType=VARCHAR}, #{mockConfigId,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR},
#{tags,jdbcType=VARCHAR}, #{status,jdbcType=VARCHAR}, #{createTime,jdbcType=BIGINT},
#{updateTime,jdbcType=BIGINT}, #{createUserId,jdbcType=VARCHAR}, #{request,jdbcType=LONGVARCHAR},
#{response,jdbcType=LONGVARCHAR})
#{updateTime,jdbcType=BIGINT}, #{createUserId,jdbcType=VARCHAR}, #{expectNum,jdbcType=VARCHAR},
#{request,jdbcType=LONGVARCHAR}, #{response,jdbcType=LONGVARCHAR})
</insert>
<insert id="insertSelective" parameterType="io.metersphere.base.domain.MockExpectConfigWithBLOBs">
insert into mock_expect_config
@ -170,6 +166,9 @@
<if test="createUserId != null">
create_user_id,
</if>
<if test="expectNum != null">
expect_num,
</if>
<if test="request != null">
request,
</if>
@ -202,6 +201,9 @@
<if test="createUserId != null">
#{createUserId,jdbcType=VARCHAR},
</if>
<if test="expectNum != null">
#{expectNum,jdbcType=VARCHAR},
</if>
<if test="request != null">
#{request,jdbcType=LONGVARCHAR},
</if>
@ -210,8 +212,7 @@
</if>
</trim>
</insert>
<select id="countByExample" parameterType="io.metersphere.base.domain.MockExpectConfigExample"
resultType="java.lang.Long">
<select id="countByExample" parameterType="io.metersphere.base.domain.MockExpectConfigExample" resultType="java.lang.Long">
select count(*) from mock_expect_config
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
@ -244,6 +245,9 @@
<if test="record.createUserId != null">
create_user_id = #{record.createUserId,jdbcType=VARCHAR},
</if>
<if test="record.expectNum != null">
expect_num = #{record.expectNum,jdbcType=VARCHAR},
</if>
<if test="record.request != null">
request = #{record.request,jdbcType=LONGVARCHAR},
</if>
@ -265,6 +269,7 @@
create_time = #{record.createTime,jdbcType=BIGINT},
update_time = #{record.updateTime,jdbcType=BIGINT},
create_user_id = #{record.createUserId,jdbcType=VARCHAR},
expect_num = #{record.expectNum,jdbcType=VARCHAR},
request = #{record.request,jdbcType=LONGVARCHAR},
response = #{record.response,jdbcType=LONGVARCHAR}
<if test="_parameter != null">
@ -280,7 +285,8 @@
`STATUS` = #{record.status,jdbcType=VARCHAR},
create_time = #{record.createTime,jdbcType=BIGINT},
update_time = #{record.updateTime,jdbcType=BIGINT},
create_user_id = #{record.createUserId,jdbcType=VARCHAR}
create_user_id = #{record.createUserId,jdbcType=VARCHAR},
expect_num = #{record.expectNum,jdbcType=VARCHAR}
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
</if>
@ -309,6 +315,9 @@
<if test="createUserId != null">
create_user_id = #{createUserId,jdbcType=VARCHAR},
</if>
<if test="expectNum != null">
expect_num = #{expectNum,jdbcType=VARCHAR},
</if>
<if test="request != null">
request = #{request,jdbcType=LONGVARCHAR},
</if>
@ -327,6 +336,7 @@
create_time = #{createTime,jdbcType=BIGINT},
update_time = #{updateTime,jdbcType=BIGINT},
create_user_id = #{createUserId,jdbcType=VARCHAR},
expect_num = #{expectNum,jdbcType=VARCHAR},
request = #{request,jdbcType=LONGVARCHAR},
response = #{response,jdbcType=LONGVARCHAR}
where id = #{id,jdbcType=VARCHAR}
@ -339,7 +349,8 @@
`STATUS` = #{status,jdbcType=VARCHAR},
create_time = #{createTime,jdbcType=BIGINT},
update_time = #{updateTime,jdbcType=BIGINT},
create_user_id = #{createUserId,jdbcType=VARCHAR}
create_user_id = #{createUserId,jdbcType=VARCHAR},
expect_num = #{expectNum,jdbcType=VARCHAR}
where id = #{id,jdbcType=VARCHAR}
</update>
</mapper>

View File

@ -12,4 +12,8 @@ public interface ExtMockExpectConfigMapper {
List<MockExpectConfigWithBLOBs> selectByApiId(String apiId);
List<MockExpectConfigWithBLOBs> selectByApiIdIn(@Param("values") List<String> apiIds);
List<String> selectExlectNumByMockConfigId(String mockConfigId);
String selectApiNumberByMockConfigId(String mockConfigId);
}

View File

@ -21,4 +21,14 @@
</foreach>
)
</select>
<select id="selectExlectNumByMockConfigId" resultType="java.lang.String">
SELECT expect_num FROM mock_expect_config WHERE mock_config_id = #{0}
</select>
<select id="selectApiNumberByMockConfigId" resultType="java.lang.String">
SELECT num FROM api_definition WHERE id IN (
select api_id from mock_config WHERE id = #{0}
)
</select>
</mapper>

View File

@ -5,6 +5,7 @@ import io.metersphere.api.jmeter.NewDriverManager;
import io.metersphere.api.service.ApiAutomationService;
import io.metersphere.api.service.ApiDefinitionService;
import io.metersphere.api.service.ApiTestCaseService;
import io.metersphere.api.service.MockConfigService;
import io.metersphere.base.domain.JarConfig;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.commons.utils.RunInterface;
@ -63,6 +64,8 @@ public class AppStartListener implements ApplicationListener<ApplicationReadyEve
private ApiDefinitionService apiDefinitionService;
@Resource
private TestReviewTestCaseService testReviewTestCaseService;
@Resource
private MockConfigService mockConfigService;
@Value("${jmeter.home}")
private String jmeterHome;
@ -132,7 +135,7 @@ public class AppStartListener implements ApplicationListener<ApplicationReadyEve
initOnceOperate(testPlanLoadCaseService::initOrderField, "init.sort.plan.api.load");
initOnceOperate(testReviewTestCaseService::initOrderField, "init.sort.review.test.case");
initOnceOperate(apiDefinitionService::initDefaultModuleId, "init.default.module.id");
initOnceOperate(mockConfigService::initExpectNum,"init.mock.expectNum");
}
/**

View File

@ -21,6 +21,12 @@
ref="table"
>
<ms-table-column
prop="expectNum"
:label="$t('commons.id')"
min-width="120px">
</ms-table-column>
<ms-table-column
prop="name"
:label="$t('api_test.mock.table.name')"