refactor: 接口用例和场景用例导入判断id是否存在

This commit is contained in:
chenjianxing 2021-09-22 13:12:42 +08:00 committed by jianxing
parent 6b2f6bfcfc
commit eed91bada2
6 changed files with 44 additions and 25 deletions

View File

@ -124,7 +124,7 @@ public class MsScenarioParser extends MsAbstractParser<ScenarioImport> {
parseModule(item.getModulePath(), importRequest, item); parseModule(item.getModulePath(), importRequest, item);
} }
item.setId(UUID.randomUUID().toString()); // item.setId(UUID.randomUUID().toString());
item.setProjectId(this.projectId); item.setProjectId(this.projectId);
}); });
} }

View File

@ -1759,9 +1759,20 @@ public class ApiAutomationService {
} }
} }
public List<ApiScenarioWithBLOBs> getWithBLOBs(ApiScenarioWithBLOBs request) { public List<ApiScenarioWithBLOBs> getSameScenario(ApiScenarioWithBLOBs request) {
ApiScenarioExample example = new ApiScenarioExample(); ApiScenarioExample example = new ApiScenarioExample();
example.createCriteria().andNameEqualTo(request.getName()).andProjectIdEqualTo(request.getProjectId()).andStatusNotEqualTo("Trash").andIdNotEqualTo(request.getId()); ApiScenarioExample.Criteria criteria = example.createCriteria();
criteria.andProjectIdEqualTo(request.getProjectId())
.andStatusNotEqualTo("Trash")
.andNameEqualTo(request.getName());
if (StringUtils.isNotBlank(request.getId())) {
// id 不为空 则判断id一样或者名字一样则是同一个用例
ApiScenarioExample.Criteria criteria1 = example.createCriteria();
criteria1.andProjectIdEqualTo(request.getProjectId())
.andStatusNotEqualTo("Trash")
.andIdEqualTo(request.getId());
example.or(criteria1);
}
return apiScenarioMapper.selectByExampleWithBLOBs(example); return apiScenarioMapper.selectByExampleWithBLOBs(example);
} }
@ -1803,7 +1814,7 @@ public class ApiAutomationService {
} }
scenarioWithBLOBs.setDescription(request.getDescription()); scenarioWithBLOBs.setDescription(request.getDescription());
List<ApiScenarioWithBLOBs> sameRequest = getWithBLOBs(scenarioWithBLOBs); List<ApiScenarioWithBLOBs> sameRequest = getSameScenario(scenarioWithBLOBs);
Boolean openCustomNum = apiTestImportRequest.getOpenCustomNum(); Boolean openCustomNum = apiTestImportRequest.getOpenCustomNum();
List<ApiScenario> list = new ArrayList<>(); List<ApiScenario> list = new ArrayList<>();
@ -1857,8 +1868,8 @@ public class ApiAutomationService {
int num = 0; int num = 0;
Project project = new Project(); Project project = new Project();
if (!CollectionUtils.isEmpty(data) && data.get(0) != null && data.get(0).getProjectId() != null) { if (!CollectionUtils.isEmpty(data) && data.get(0) != null && data.get(0).getProjectId() != null) {
num = getNextNum(data.get(0).getProjectId());
project = projectMapper.selectByPrimaryKey(data.get(0).getProjectId()); project = projectMapper.selectByPrimaryKey(data.get(0).getProjectId());
num = getNextNum(data.get(0).getProjectId());
request.setOpenCustomNum(project.getScenarioCustomNum()); request.setOpenCustomNum(project.getScenarioCustomNum());
} }
for (int i = 0; i < data.size(); i++) { for (int i = 0; i < data.size(); i++) {
@ -1876,6 +1887,9 @@ public class ApiAutomationService {
} }
} }
num++; num++;
if (StringUtils.isBlank(item.getId())) {
item.setId(UUID.randomUUID().toString());
}
importCreate(item, batchMapper, request); importCreate(item, batchMapper, request);
if (i % 300 == 0) { if (i % 300 == 0) {
sqlSession.flushStatements(); sqlSession.flushStatements();

View File

@ -648,7 +648,8 @@ public class ApiDefinitionService {
SaveApiTestCaseRequest checkRequest = new SaveApiTestCaseRequest(); SaveApiTestCaseRequest checkRequest = new SaveApiTestCaseRequest();
checkRequest.setName(apiTestCase.getName()); checkRequest.setName(apiTestCase.getName());
checkRequest.setApiDefinitionId(apiTestCase.getApiDefinitionId()); checkRequest.setApiDefinitionId(apiTestCase.getApiDefinitionId());
ApiTestCase sameCase = apiTestCaseService.getSameCase(checkRequest); checkRequest.setId(apiTestCase.getId());
ApiTestCase sameCase = apiTestCaseService.getImportSameCase(checkRequest);
apiTestCase.setUpdateUserId(SessionUtils.getUserId()); apiTestCase.setUpdateUserId(SessionUtils.getUserId());
if (sameCase == null) { if (sameCase == null) {
apiTestCase.setId(UUID.randomUUID().toString()); apiTestCase.setId(UUID.randomUUID().toString());

View File

@ -265,19 +265,6 @@ public class ApiTestCaseService {
} }
public void checkNameExist(SaveApiTestCaseRequest request) { public void checkNameExist(SaveApiTestCaseRequest request) {
if (hasSameCase(request)) {
MSException.throwException(Translator.get("load_test_already_exists"));
}
}
public Boolean hasSameCase(SaveApiTestCaseRequest request) {
if (getSameCase(request) != null) {
return true;
}
return false;
}
public ApiTestCase getSameCase(SaveApiTestCaseRequest request) {
ApiTestCaseExample example = new ApiTestCaseExample(); ApiTestCaseExample example = new ApiTestCaseExample();
ApiTestCaseExample.Criteria criteria = example.createCriteria(); ApiTestCaseExample.Criteria criteria = example.createCriteria();
criteria.andStatusNotEqualTo("Trash").andNameEqualTo(request.getName()).andApiDefinitionIdEqualTo(request.getApiDefinitionId()); criteria.andStatusNotEqualTo("Trash").andNameEqualTo(request.getName()).andApiDefinitionIdEqualTo(request.getApiDefinitionId());
@ -286,9 +273,14 @@ public class ApiTestCaseService {
} }
List<ApiTestCase> apiTestCases = apiTestCaseMapper.selectByExample(example); List<ApiTestCase> apiTestCases = apiTestCaseMapper.selectByExample(example);
if (CollectionUtils.isNotEmpty(apiTestCases)) { if (CollectionUtils.isNotEmpty(apiTestCases)) {
return apiTestCases.get(0); if (apiTestCases.get(0) != null) {
MSException.throwException(Translator.get("load_test_already_exists"));
};
} }
return null; }
public ApiTestCase getImportSameCase(SaveApiTestCaseRequest request) {
return extApiTestCaseMapper.selectSameCase(request);
} }
private ApiTestCase updateTest(SaveApiTestCaseRequest request) { private ApiTestCase updateTest(SaveApiTestCaseRequest request) {

View File

@ -1,10 +1,7 @@
package io.metersphere.base.mapper.ext; package io.metersphere.base.mapper.ext;
import io.metersphere.api.dto.datacount.ApiDataCountResult; import io.metersphere.api.dto.datacount.ApiDataCountResult;
import io.metersphere.api.dto.definition.ApiTestCaseDTO; import io.metersphere.api.dto.definition.*;
import io.metersphere.api.dto.definition.ApiTestCaseInfo;
import io.metersphere.api.dto.definition.ApiTestCaseRequest;
import io.metersphere.api.dto.definition.ApiTestCaseResult;
import io.metersphere.base.domain.ApiDefinition; import io.metersphere.base.domain.ApiDefinition;
import io.metersphere.base.domain.ApiTestCase; import io.metersphere.base.domain.ApiTestCase;
import io.metersphere.controller.request.BaseQueryRequest; import io.metersphere.controller.request.BaseQueryRequest;
@ -60,4 +57,6 @@ public interface ExtApiTestCaseMapper {
Long getPreOrder(@Param("projectId")String projectId, @Param("baseOrder") Long baseOrder); Long getPreOrder(@Param("projectId")String projectId, @Param("baseOrder") Long baseOrder);
Long getLastOrder(@Param("projectId")String projectId, @Param("baseOrder") Long baseOrder); Long getLastOrder(@Param("projectId")String projectId, @Param("baseOrder") Long baseOrder);
ApiTestCase selectSameCase(@Param("request") SaveApiTestCaseRequest request);
} }

View File

@ -596,6 +596,19 @@
</if> </if>
order by `order` desc limit 1; order by `order` desc limit 1;
</select> </select>
<select id="selectSameCase" resultType="io.metersphere.base.domain.ApiTestCase">
select id
from api_test_case
WHERE
api_definition_id = #{request.apiDefinitionId} and (`status` &lt;&gt; 'Trash' or `status` is null)
<if test="request.id != null and request.id != ''">
and id = #{request.id}
</if>
<if test="request.id == null or request.id == ''">
and name = #{request.name}
</if>
limit 1;
</select>
<update id="deleteToGc" parameterType="io.metersphere.api.dto.definition.ApiTestCaseRequest"> <update id="deleteToGc" parameterType="io.metersphere.api.dto.definition.ApiTestCaseRequest">
update api_test_case update api_test_case