]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6315 do not check twice for existing QP
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Tue, 7 Mar 2017 15:12:34 +0000 (16:12 +0100)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 23 Mar 2017 16:38:34 +0000 (17:38 +0100)
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileCopier.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileFactory.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/CreateAction.java
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileFactoryMediumTest.java

index c164feaabf55780c8059171c4d0327cac73fd2f1..11ac59f74192fd0320e7c14d3ed2f4f4af9169c8 100644 (file)
@@ -57,7 +57,7 @@ public class QProfileCopier {
   public QualityProfileDto copyToName(DbSession dbSession, String fromKey, String toName) {
     QualityProfileDto from = db.qualityProfileDao().selectOrFailByKey(dbSession, fromKey);
     OrganizationDto organization = db.organizationDao().selectByUuid(dbSession, from.getOrganizationUuid())
-      .orElseThrow(() -> new IllegalStateException("Organization with UUID [" + from.getOrganizationUuid() + "] does not exist"));
+        .orElseThrow(() -> new IllegalStateException("Organization with UUID [" + from.getOrganizationUuid() + "] does not exist"));
     QualityProfileDto to = prepareTarget(dbSession, organization, from, toName);
     File backupFile = temp.newFile();
     try {
@@ -76,7 +76,7 @@ public class QProfileCopier {
     if (toProfile == null) {
       // Do not delegate creation to QProfileBackuper because we need to keep
       // the parent-child association, if exists.
-      toProfile = factory.create(dbSession, organization, toProfileName);
+      toProfile = factory.checkAndCreate(dbSession, organization, toProfileName);
       toProfile.setParentKee(from.getParentKee());
       db.qualityProfileDao().update(dbSession, toProfile);
       dbSession.commit();
@@ -87,12 +87,12 @@ public class QProfileCopier {
   private void verify(QualityProfileDto fromProfile, QProfileName toProfileName) {
     if (!StringUtils.equals(fromProfile.getLanguage(), toProfileName.getLanguage())) {
       throw new IllegalArgumentException(String.format(
-        "Source and target profiles do not have the same language: %s and %s",
-        fromProfile.getLanguage(), toProfileName.getLanguage()));
+          "Source and target profiles do not have the same language: %s and %s",
+          fromProfile.getLanguage(), toProfileName.getLanguage()));
     }
     if (fromProfile.getName().equals(toProfileName.getName())) {
       throw new IllegalArgumentException(String.format("Source and target profiles are equal: %s",
-        fromProfile.getName()));
+          fromProfile.getName()));
     }
   }
 
index c0f557f287c6654e9fcb5a5afbfd6415104df683..aba10a577732c3189e7d2172d7078aa5d10b092c 100644 (file)
@@ -27,6 +27,7 @@ import java.util.List;
 import java.util.Objects;
 import java.util.Set;
 import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
 import org.apache.commons.lang.RandomStringUtils;
 import org.apache.commons.lang.StringUtils;
 import org.sonar.core.util.Slug;
@@ -56,7 +57,7 @@ public class QProfileFactory {
   // ------------- CREATION
 
   QualityProfileDto getOrCreate(DbSession dbSession, OrganizationDto organization, QProfileName name) {
-    Objects.requireNonNull(organization, "Organization is required, when creating a quality profile.");
+    requireNonNull(organization);
     QualityProfileDto profile = db.qualityProfileDao().selectByNameAndLanguage(organization, name.getName(), name.getLanguage(), dbSession);
     if (profile == null) {
       profile = doCreate(dbSession, organization, name);
@@ -64,13 +65,32 @@ public class QProfileFactory {
     return profile;
   }
 
-  public QualityProfileDto create(DbSession dbSession, OrganizationDto organization, QProfileName name) {
-    Objects.requireNonNull(organization, "Organization is required, when creating a quality profile.");
+  /**
+   * Create the quality profile in DB with the specified name.
+   *
+   * @throws BadRequestException if a quality profile with the specified name already exists
+   */
+  public QualityProfileDto checkAndCreate(DbSession dbSession, OrganizationDto organization, QProfileName name) {
+    requireNonNull(organization);
     QualityProfileDto dto = db.qualityProfileDao().selectByNameAndLanguage(organization, name.getName(), name.getLanguage(), dbSession);
     checkRequest(dto == null, "Quality profile already exists: %s", name);
     return doCreate(dbSession, organization, name);
   }
 
+  /**
+   * Create the quality profile in DB with the specified name.
+   *
+   * A DB error will be thrown if the quality profile already exists.
+   */
+  public QualityProfileDto create(DbSession dbSession, OrganizationDto organization, QProfileName name) {
+    return doCreate(dbSession, requireNonNull(organization), name);
+  }
+
+  private static OrganizationDto requireNonNull(@Nullable OrganizationDto organization) {
+    Objects.requireNonNull(organization, "Organization is required, when creating a quality profile.");
+    return organization;
+  }
+
   private QualityProfileDto doCreate(DbSession dbSession, OrganizationDto organization, QProfileName name) {
     if (StringUtils.isEmpty(name.getName())) {
       throw BadRequestException.create("quality_profiles.profile_name_cant_be_blank");
index f2a8400ff6a849b9fea2da5be8655baa02deef02..94454baebf38f14a12c169b987e6229f8e402b7f 100644 (file)
@@ -123,7 +123,7 @@ public class CreateAction implements QProfileWsAction {
 
   private CreateWsResponse doHandle(DbSession dbSession, CreateRequest createRequest, Request request, OrganizationDto organization) {
     QProfileResult result = new QProfileResult();
-    QualityProfileDto profile = profileFactory.create(dbSession, organization,
+    QualityProfileDto profile = profileFactory.checkAndCreate(dbSession, organization,
       QProfileName.createFor(createRequest.getLanguage(), createRequest.getProfileName()));
     result.setProfile(profile);
     for (ProfileImporter importer : importers) {
index 1fbe5c58ce67995160feacf9334a555251fac232..d1033f33a02101ff7ba58e2b1da27f2f5724d9c5 100644 (file)
@@ -85,6 +85,31 @@ public class QProfileFactoryMediumTest {
     dbSession.close();
   }
 
+  @Test
+  public void checkAndCreate() {
+    String uuid = organization.getUuid();
+
+    QualityProfileDto writtenDto = factory.checkAndCreate(dbSession, organization, new QProfileName("xoo", "P1"));
+    dbSession.commit();
+    dbSession.clearCache();
+    assertThat(writtenDto.getOrganizationUuid()).isEqualTo(uuid);
+    assertThat(writtenDto.getKey()).startsWith("xoo-p1-");
+    assertThat(writtenDto.getName()).isEqualTo("P1");
+    assertThat(writtenDto.getLanguage()).isEqualTo("xoo");
+    assertThat(writtenDto.getId()).isNotNull();
+
+    // reload the dto
+    QualityProfileDto readDto = db.qualityProfileDao().selectByNameAndLanguage("P1", "xoo", dbSession);
+    assertThat(readDto.getOrganizationUuid()).isEqualTo(uuid);
+    assertThat(readDto.getName()).isEqualTo("P1");
+    assertThat(readDto.getKey()).startsWith("xoo-p1");
+    assertThat(readDto.getLanguage()).isEqualTo("xoo");
+    assertThat(readDto.getId()).isNotNull();
+    assertThat(readDto.getParentKee()).isNull();
+
+    assertThat(db.qualityProfileDao().selectAll(dbSession, organization)).hasSize(1);
+  }
+
   @Test
   public void create() {
     String uuid = organization.getUuid();
@@ -111,33 +136,61 @@ public class QProfileFactoryMediumTest {
   }
 
   @Test
-  public void fail_to_create_if_name_null() {
+  public void checkAndCreate_throws_BadRequestException_if_name_null() {
     QProfileName name = new QProfileName("xoo", null);
 
     expectBadRequestException("quality_profiles.profile_name_cant_be_blank");
 
-    factory.create(dbSession, organization, name);
+    factory.checkAndCreate(dbSession, organization, name);
   }
 
   @Test
-  public void fail_to_create_if_name_empty() {
+  public void checkAndCreate_throws_BadRequestException_if_name_empty() {
     QProfileName name = new QProfileName("xoo", "");
 
     expectBadRequestException("quality_profiles.profile_name_cant_be_blank");
 
-    factory.create(dbSession, organization, name);
+    factory.checkAndCreate(dbSession, organization, name);
   }
 
   @Test
-  public void fail_to_create_if_already_exists() {
+  public void checkAndCreate_throws_BadRequestException_if_already_exists() {
     QProfileName name = new QProfileName("xoo", "P1");
-    factory.create(dbSession, organization, name);
+    factory.checkAndCreate(dbSession, organization, name);
     dbSession.commit();
     dbSession.clearCache();
 
     expectBadRequestException("Quality profile already exists: {lang=xoo, name=P1}");
 
+    factory.checkAndCreate(dbSession, organization, name);
+  }
+
+  @Test
+  public void create_throws_BadRequestException_if_name_null() {
+    QProfileName name = new QProfileName("xoo", null);
+
+    expectBadRequestException("quality_profiles.profile_name_cant_be_blank");
+
+    factory.create(dbSession, organization, name);
+  }
+
+  @Test
+  public void create_throws_BadRequestException_if_name_empty() {
+    QProfileName name = new QProfileName("xoo", "");
+
+    expectBadRequestException("quality_profiles.profile_name_cant_be_blank");
+
+    factory.create(dbSession, organization, name);
+  }
+
+  @Test
+  public void create_does_not_fail_if_already_exists() {
+    QProfileName name = new QProfileName("xoo", "P1");
     factory.create(dbSession, organization, name);
+    dbSession.commit();
+    dbSession.clearCache();
+
+    assertThat(factory.create(dbSession, organization, name)).isNotNull();
   }
 
   @Test