]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-19528 Portfolios and applications should not be created with already existing...
authorMatteo Mara <matteo.mara@sonarsource.com>
Thu, 8 Jun 2023 21:18:26 +0000 (23:18 +0200)
committersonartech <sonartech@sonarsource.com>
Mon, 12 Jun 2023 20:02:49 +0000 (20:02 +0000)
server/sonar-webserver-webapi/src/it/java/org/sonar/server/component/ComponentUpdaterIT.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ComponentUpdater.java

index afc40542c2bdeefccc3b5f8530f600d6b6934e9b..2fff57a6f82dd200443df058b8cf9b6ae6bb8f0f 100644 (file)
@@ -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);
   }
index 1126ed1f7937f994804e761d0154278b86e152d5..1242a470d86bbeba9fe5a4c334c9c5b1c5fc59cd 100644 (file)
@@ -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<ComponentDto> componentDtos = newComponent.isProject()
-      ? dbClient.componentDao().selectByKeyCaseInsensitive(dbSession, newComponent.key())
-      : dbClient.componentDao().selectByKey(dbSession, newComponent.key()).map(Collections::singletonList).orElse(new ArrayList<>());
+    List<ComponentDto> componentDtos = dbClient.componentDao().selectByKeyCaseInsensitive(dbSession, newComponent.key());
 
     if (!componentDtos.isEmpty()) {
       String alreadyExistingKeys = componentDtos