From a1f697f122a7bf7f4c32d04f55bee1d0bbc5c956 Mon Sep 17 00:00:00 2001 From: Matteo Mara Date: Thu, 8 Jun 2023 23:18:26 +0200 Subject: [PATCH] SONAR-19528 Portfolios and applications should not be created with already existing keys with different case --- .../server/component/ComponentUpdaterIT.java | 18 +++++++++++++++++- .../server/component/ComponentUpdater.java | 6 +----- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/component/ComponentUpdaterIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/component/ComponentUpdaterIT.java index afc40542c2b..2fff57a6f82 100644 --- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/component/ComponentUpdaterIT.java +++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/component/ComponentUpdaterIT.java @@ -54,6 +54,7 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import static org.sonar.api.resources.Qualifiers.APP; +import static org.sonar.api.resources.Qualifiers.PROJECT; import static org.sonar.api.resources.Qualifiers.VIEW; import static org.sonar.db.component.BranchDto.DEFAULT_MAIN_BRANCH_NAME; @@ -324,6 +325,20 @@ public class ComponentUpdaterIT { @Test public void create_shouldFail_whenCreatingProjectWithExistingKeyButDifferentCase() { + createComponent_shouldFail_whenCreatingComponentWithExistingKeyButDifferentCase(PROJECT); + } + + @Test + public void create_shouldFail_whenCreatingPortfolioWithExistingKeyButDifferentCase() { + createComponent_shouldFail_whenCreatingComponentWithExistingKeyButDifferentCase(VIEW); + } + + @Test + public void create_shouldFail_whenCreatingApplicationWithExistingKeyButDifferentCase() { + createComponent_shouldFail_whenCreatingComponentWithExistingKeyButDifferentCase(APP); + } + + private void createComponent_shouldFail_whenCreatingComponentWithExistingKeyButDifferentCase(String qualifier) { String existingKey = randomAlphabetic(5).toUpperCase(); db.components().insertPrivateProject(component -> component.setKey(existingKey)); String newKey = existingKey.toLowerCase(); @@ -331,10 +346,11 @@ public class ComponentUpdaterIT { NewComponent newComponent = NewComponent.newComponentBuilder() .setKey(newKey) .setName(DEFAULT_PROJECT_NAME) + .setQualifier(qualifier) .build(); DbSession dbSession = db.getSession(); - assertThatThrownBy(() -> underTest.create(dbSession, newComponent, null, null).mainBranchComponent()) + assertThatThrownBy(() -> underTest.create(dbSession, newComponent, null, null)) .isInstanceOf(BadRequestException.class) .hasMessage("Could not create Project with key: \"%s\". A similar key already exists: \"%s\"", newKey, existingKey); } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ComponentUpdater.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ComponentUpdater.java index 1126ed1f793..1242a470d86 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ComponentUpdater.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ComponentUpdater.java @@ -20,8 +20,6 @@ package org.sonar.server.component; import com.google.common.annotations.VisibleForTesting; -import java.util.ArrayList; -import java.util.Collections; import java.util.Date; import java.util.List; import java.util.Locale; @@ -171,9 +169,7 @@ public class ComponentUpdater { } private void checkKeyAlreadyExists(DbSession dbSession, NewComponent newComponent) { - List componentDtos = newComponent.isProject() - ? dbClient.componentDao().selectByKeyCaseInsensitive(dbSession, newComponent.key()) - : dbClient.componentDao().selectByKey(dbSession, newComponent.key()).map(Collections::singletonList).orElse(new ArrayList<>()); + List componentDtos = dbClient.componentDao().selectByKeyCaseInsensitive(dbSession, newComponent.key()); if (!componentDtos.isEmpty()) { String alreadyExistingKeys = componentDtos -- 2.39.5