From 953a76435607b3b95b0c134b64378d7ae08ff275 Mon Sep 17 00:00:00 2001 From: Jianguo-Genius Date: Tue, 19 Nov 2024 18:11:06 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E9=87=8D=E5=86=99=E6=8E=A5=E5=8F=A3=E5=AF=BC=E5=85=A5=E7=9A=84?= =?UTF-8?q?=E9=87=8D=E5=91=BD=E5=90=8D=E6=96=B9=E6=B3=95=EF=BC=8C=E9=98=B2?= =?UTF-8?q?=E6=AD=A2=E5=A2=9E=E5=8A=A0=E4=B8=8B=E6=A0=87=E6=97=B6=E4=BC=9A?= =?UTF-8?q?=E8=BD=BB=E6=98=93=E7=9A=84=E6=95=B0=E6=8D=AE=E5=BA=93=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=E6=BA=A2=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/dataimport/HarParserApiScenario.java | 14 ++------------ .../api/utils/ApiDefinitionImportUtils.java | 15 ++++++++++----- .../ApiDefinitionControllerTests.java | 13 +++++++++++++ .../project/utils/DataBaseStringUtils.java | 17 +++++++++++++++++ 4 files changed, 42 insertions(+), 17 deletions(-) create mode 100644 backend/services/project-management/src/main/java/io/metersphere/project/utils/DataBaseStringUtils.java 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; + } + } +}