diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/parser/api/dataimport/HarParserApiScenario.java b/backend/services/api-test/src/main/java/io/metersphere/api/parser/api/dataimport/HarParserApiScenario.java index 20d84343d2..0159eb8370 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/parser/api/dataimport/HarParserApiScenario.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/parser/api/dataimport/HarParserApiScenario.java @@ -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; - } - } } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/utils/ApiDefinitionImportUtils.java b/backend/services/api-test/src/main/java/io/metersphere/api/utils/ApiDefinitionImportUtils.java index b667d415a0..605c681a53 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/utils/ApiDefinitionImportUtils.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/utils/ApiDefinitionImportUtils.java @@ -153,12 +153,17 @@ public class ApiDefinitionImportUtils { return returnList; } - private static String getUniqueName(String originalName, List existenceNameList) { + public static String getUniqueName(String originalName, List existenceNameList) { String returnName = originalName; - int index = 1; - while (existenceNameList.contains(returnName)) { - returnName = originalName + " - " + index; - index++; + if (existenceNameList.contains(returnName)) { + if (originalName.length() > 250) { + originalName = originalName.trim().substring(0, 250); + } + int index = 1; + do { + returnName = originalName + "-" + index; + index++; + } while (existenceNameList.contains(returnName)); } return returnName; } diff --git a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiDefinitionControllerTests.java b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiDefinitionControllerTests.java index 0e6fee14e9..ba6215c998 100644 --- a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiDefinitionControllerTests.java +++ b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiDefinitionControllerTests.java @@ -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 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"); diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/utils/DataBaseStringUtils.java b/backend/services/project-management/src/main/java/io/metersphere/project/utils/DataBaseStringUtils.java new file mode 100644 index 0000000000..d5f92d12ed --- /dev/null +++ b/backend/services/project-management/src/main/java/io/metersphere/project/utils/DataBaseStringUtils.java @@ -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; + } + } +}