fix(接口测试): 重写接口导入的重命名方法,防止增加下标时会轻易的数据库字段溢出

This commit is contained in:
Jianguo-Genius 2024-11-19 18:11:06 +08:00 committed by 建国
parent 72bdde0de7
commit 953a764356
4 changed files with 42 additions and 17 deletions

View File

@ -15,6 +15,7 @@ import io.metersphere.api.parser.api.har.model.Har;
import io.metersphere.api.parser.api.har.model.HarEntry;
import io.metersphere.api.parser.api.har.model.HarRequest;
import io.metersphere.api.utils.ApiDefinitionImportUtils;
import io.metersphere.project.utils.DataBaseStringUtils;
import io.metersphere.sdk.constants.HttpMethodConstants;
import io.metersphere.sdk.exception.MSException;
import io.metersphere.sdk.util.JSON;
@ -116,7 +117,7 @@ public class HarParserApiScenario implements ApiScenarioImportParser {
apiScenarioStep.setId(IDGenerator.nextStr());
apiScenarioStep.setProjectId(projectId);
apiScenarioStep.setOriginProjectId(projectId);
apiScenarioStep.setName(this.parseName(msTestElement.getName()));
apiScenarioStep.setName(DataBaseStringUtils.parseMaxString(msTestElement.getName(), 255));
apiScenarioStep.setUniqueId(IDGenerator.nextStr());
msTestElement.setStepId(apiScenarioStep.getId());
msTestElement.setProjectId(apiScenarioStep.getProjectId());
@ -132,17 +133,6 @@ public class HarParserApiScenario implements ApiScenarioImportParser {
}
return parseResult;
}
private String parseName(String name) {
if (StringUtils.isBlank(name)) {
return StringUtils.EMPTY;
}
if (name.length() > 253) {
return name.substring(0, 200) + "...";
} else {
return name;
}
}
}

View File

@ -153,12 +153,17 @@ public class ApiDefinitionImportUtils {
return returnList;
}
private static String getUniqueName(String originalName, List<String> existenceNameList) {
public static String getUniqueName(String originalName, List<String> existenceNameList) {
String returnName = originalName;
if (existenceNameList.contains(returnName)) {
if (originalName.length() > 250) {
originalName = originalName.trim().substring(0, 250);
}
int index = 1;
while (existenceNameList.contains(returnName)) {
returnName = originalName + " - " + index;
do {
returnName = originalName + "-" + index;
index++;
} while (existenceNameList.contains(returnName));
}
return returnName;
}

View File

@ -26,6 +26,7 @@ import io.metersphere.api.service.BaseFileManagementTestService;
import io.metersphere.api.service.definition.ApiDefinitionService;
import io.metersphere.api.service.definition.ApiTestCaseService;
import io.metersphere.api.utils.ApiDataUtils;
import io.metersphere.api.utils.ApiDefinitionImportUtils;
import io.metersphere.functional.domain.ExportTask;
import io.metersphere.plugin.api.spi.AbstractMsTestElement;
import io.metersphere.project.domain.Project;
@ -1629,6 +1630,18 @@ public class ApiDefinitionControllerTests extends BaseTest {
@Test
@Order(102)
public void testImport() throws Exception {
// 测试 getUniqueName 方法
List<String> uniqueNameTestList = new ArrayList<>() {{
this.add("用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例-1");
this.add("用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录666");
}};
Assertions.assertEquals("用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例-2",
ApiDefinitionImportUtils.getUniqueName(
"用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录66成功用例登录666",
uniqueNameTestList
));
LogUtils.info("import api test");
ApiDefinitionModule apiDefinitionModule = new ApiDefinitionModule();
apiDefinitionModule.setId("test-import");

View File

@ -0,0 +1,17 @@
package io.metersphere.project.utils;
import org.apache.commons.lang3.StringUtils;
public class DataBaseStringUtils {
public static String parseMaxString(String name, int maxLength) {
if (StringUtils.isBlank(name)) {
return StringUtils.EMPTY;
}
if (name.length() > maxLength) {
return name.substring(0, maxLength - 3) + "...";
} else {
return name;
}
}
}