]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8889 do not drop Quality profile at startup if already exists
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Tue, 21 Mar 2017 15:03:23 +0000 (16:03 +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/DefinedQProfileCreationImpl.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileFactory.java
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/DefinedQProfileCreationImplTest.java
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileFactoryMediumTest.java

index 7359bc1c48ccc597b1ce35ff2b7c0bd1997d5f8b..dfec3d1690f1fd00ec6d53d6c1746a5b9a24cf1e 100644 (file)
@@ -42,18 +42,17 @@ public class DefinedQProfileCreationImpl implements DefinedQProfileCreation {
   @Override
   public void create(DbSession session, DefinedQProfile qualityProfile, OrganizationDto organization, List<ActiveRuleChange> changes) {
     QualityProfileDto profileDto = dbClient.qualityProfileDao().selectByNameAndLanguage(organization, qualityProfile.getName(), qualityProfile.getLanguage(), session);
-    if (profileDto != null) {
-      changes.addAll(profileFactory.deleteTree(session, profileDto.getKey(), true));
-    }
-    QualityProfileDto newQProfileDto = profileFactory.create(session, organization, qualityProfile.getQProfileName(), qualityProfile.isDefault());
-    for (org.sonar.api.rules.ActiveRule activeRule : qualityProfile.getActiveRules()) {
-      RuleKey ruleKey = RuleKey.of(activeRule.getRepositoryKey(), activeRule.getRuleKey());
-      RuleActivation activation = new RuleActivation(ruleKey);
-      activation.setSeverity(activeRule.getSeverity() != null ? activeRule.getSeverity().name() : null);
-      for (ActiveRuleParam param : activeRule.getActiveRuleParams()) {
-        activation.setParameter(param.getKey(), param.getValue());
+    if (profileDto == null) {
+      profileDto = profileFactory.create(session, organization, qualityProfile.getQProfileName(), qualityProfile.isDefault());
+      for (org.sonar.api.rules.ActiveRule activeRule : qualityProfile.getActiveRules()) {
+        RuleKey ruleKey = RuleKey.of(activeRule.getRepositoryKey(), activeRule.getRuleKey());
+        RuleActivation activation = new RuleActivation(ruleKey);
+        activation.setSeverity(activeRule.getSeverity() != null ? activeRule.getSeverity().name() : null);
+        for (ActiveRuleParam param : activeRule.getActiveRuleParams()) {
+          activation.setParameter(param.getKey(), param.getValue());
+        }
+        changes.addAll(ruleActivator.activate(session, activation, profileDto));
       }
-      changes.addAll(ruleActivator.activate(session, activation, newQProfileDto));
     }
 
     LoadedTemplateDto template = new LoadedTemplateDto(organization.getUuid(), qualityProfile.getLoadedTemplateType());
index 1f157db531946c26feccc33ac294b1865228cf73..f5427e1ba5856595f81e48ae13a86a96184267d1 100644 (file)
  */
 package org.sonar.server.qualityprofile;
 
-import com.google.common.collect.Lists;
-import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Date;
-import java.util.List;
 import java.util.Objects;
 import javax.annotation.Nullable;
 import org.apache.commons.lang.StringUtils;
@@ -32,12 +29,10 @@ import org.sonar.core.util.UuidFactory;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.organization.OrganizationDto;
-import org.sonar.db.qualityprofile.ActiveRuleDto;
 import org.sonar.db.qualityprofile.QualityProfileDto;
 import org.sonar.server.exceptions.BadRequestException;
 import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
 
-import static org.sonar.server.qualityprofile.ActiveRuleChange.Type.DEACTIVATED;
 import static org.sonar.server.ws.WsUtils.checkRequest;
 
 /**
@@ -111,42 +106,6 @@ public class QProfileFactory {
 
   // ------------- DELETION
 
-  /**
-   * Delete the profile with specified key and all its descendants from database. Elasticsearch
-   * is not touched and still needs to be clean-up.
-   *
-   * Session is NOT committed. Profiles marked as "default" for a language can't be deleted,
-   * except if the parameter <code>force</code> is true.
-   */
-  public List<ActiveRuleChange> deleteTree(DbSession session, String key, boolean force) {
-    QualityProfileDto profile = db.qualityProfileDao().selectOrFailByKey(session, key);
-    List<QualityProfileDto> descendants = db.qualityProfileDao().selectDescendants(session, key);
-    if (!force) {
-      checkNotDefault(profile);
-      for (QualityProfileDto descendant : descendants) {
-        checkNotDefault(descendant);
-      }
-    }
-    // delete bottom-up
-    List<ActiveRuleChange> changes = new ArrayList<>();
-    for (QualityProfileDto descendant : Lists.reverse(descendants)) {
-      changes.addAll(doDelete(session, descendant));
-    }
-    changes.addAll(doDelete(session, profile));
-    return changes;
-  }
-
-  private List<ActiveRuleChange> doDelete(DbSession session, QualityProfileDto profile) {
-    db.qualityProfileDao().deleteAllProjectProfileAssociation(profile.getKey(), session);
-    List<ActiveRuleChange> changes = new ArrayList<>();
-    for (ActiveRuleDto activeRule : db.activeRuleDao().selectByProfileKey(session, profile.getKey())) {
-      db.activeRuleDao().delete(session, activeRule.getKey());
-      changes.add(ActiveRuleChange.createFor(DEACTIVATED, activeRule.getKey()));
-    }
-    db.qualityProfileDao().delete(session, profile.getId());
-    return changes;
-  }
-
   /**
    * Deletes the profiles with specified keys from database and Elasticsearch.
    * All related information are deleted. The profiles marked as "default"
@@ -165,11 +124,4 @@ public class QProfileFactory {
     }
   }
 
-  // ------------- DEFAULT PROFILE
-
-  private static void checkNotDefault(QualityProfileDto p) {
-    if (p.isDefault()) {
-      throw BadRequestException.create("The profile marked as default can not be deleted: " + p.getKey());
-    }
-  }
 }
index 28050992bf373238b93116073bbc653133f38c3e..7622a437294b2f91a4649f717135b4117f77bc4f 100644 (file)
@@ -133,7 +133,7 @@ public class DefinedQProfileCreationImplTest {
   }
 
   @Test
-  public void create_deletes_qp_if_already_exists() {
+  public void create_does_not_update_existing_profile_if_it_already_exists() {
     OrganizationDto organization = dbTester.organizations().insert();
     DefinedQProfile definedQProfile = definedQProfileRepositoryRule.create(FOO_LANGUAGE, "foo1", false);
     long date = 2_456_789L;
@@ -149,8 +149,7 @@ public class DefinedQProfileCreationImplTest {
     dbSession.commit();
 
     QualityProfileDto dto = getPersistedQP(organization, FOO_LANGUAGE, "foo1");
-    assertThat(dto.getId()).isNotEqualTo(existing.getId());
-    assertThat(dto.getKey()).isNotEqualTo(existing.getKey());
+    assertThat(dto.getId()).isEqualTo(existing.getId());
     assertThat(dbTester.countRowsOfTable(dbTester.getSession(), TABLE_RULES_PROFILES)).isEqualTo(1);
   }
 
index 613ca756b84b6de7d36a5cadec15b5baed304326..08c4c9a04af019445ffb32197be0d36acb4ad8fb 100644 (file)
  */
 package org.sonar.server.qualityprofile;
 
-import java.util.List;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.ClassRule;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
-import org.sonar.api.server.rule.RuleParamType;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
-import org.sonar.db.RowNotFoundException;
 import org.sonar.db.organization.OrganizationDto;
 import org.sonar.db.organization.OrganizationTesting;
 import org.sonar.db.qualityprofile.QualityProfileDto;
-import org.sonar.db.qualityprofile.QualityProfileTesting;
-import org.sonar.db.rule.RuleDto;
-import org.sonar.db.rule.RuleParamDto;
-import org.sonar.db.rule.RuleTesting;
 import org.sonar.server.exceptions.BadRequestException;
-import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
-import org.sonar.server.rule.index.RuleIndex;
-import org.sonar.server.rule.index.RuleIndexer;
-import org.sonar.server.rule.index.RuleQuery;
 import org.sonar.server.tester.ServerTester;
 import org.sonar.server.tester.UserSessionRule;
 
 import static org.assertj.core.api.Assertions.assertThat;
-import static org.junit.Assert.fail;
-import static org.sonar.server.qualityprofile.QProfileTesting.XOO_P1_KEY;
-import static org.sonar.server.qualityprofile.QProfileTesting.XOO_P2_KEY;
-import static org.sonar.server.qualityprofile.QProfileTesting.XOO_P3_KEY;
-import static org.sonar.server.qualityprofile.QProfileTesting.getDefaultOrganization;
 
 public class QProfileFactoryMediumTest {
 
@@ -63,8 +47,6 @@ public class QProfileFactoryMediumTest {
 
   private DbClient db;
   private DbSession dbSession;
-  private ActiveRuleIndexer activeRuleIndexer;
-  private RuleIndexer ruleIndexer;
   private QProfileFactory factory;
   private OrganizationDto organization;
 
@@ -74,8 +56,6 @@ public class QProfileFactoryMediumTest {
     db = tester.get(DbClient.class);
     dbSession = db.openSession(false);
     factory = tester.get(QProfileFactory.class);
-    activeRuleIndexer = tester.get(ActiveRuleIndexer.class);
-    ruleIndexer = tester.get(RuleIndexer.class);
     organization = OrganizationTesting.newOrganizationDto();
     db.organizationDao().insert(dbSession, organization);
   }
@@ -185,129 +165,6 @@ public class QProfileFactoryMediumTest {
     assertThat(factory.create(dbSession, organization, name, true)).isNotNull();
   }
 
-  @Test
-  public void delete() {
-    initRules();
-    db.qualityProfileDao().insert(dbSession, QProfileTesting.newXooP1(organization.getUuid()));
-    tester.get(RuleActivator.class).activate(dbSession, new RuleActivation(RuleTesting.XOO_X1), XOO_P1_KEY);
-    dbSession.commit();
-    dbSession.clearCache();
-
-    List<ActiveRuleChange> changes = factory.deleteTree(dbSession, XOO_P1_KEY, false);
-    dbSession.commit();
-    activeRuleIndexer.index(changes);
-
-    dbSession.clearCache();
-    assertThat(db.qualityProfileDao().selectAll(dbSession, getDefaultOrganization(tester, db, dbSession))).isEmpty();
-    assertThat(db.activeRuleDao().selectAll(dbSession)).isEmpty();
-    assertThat(db.activeRuleDao().selectAllParams(dbSession)).isEmpty();
-    assertThat(db.activeRuleDao().selectByProfileKey(dbSession, XOO_P1_KEY)).isEmpty();
-    assertThat(tester.get(RuleIndex.class).searchAll(new RuleQuery().setQProfileKey(XOO_P1_KEY).setActivation(true))).isEmpty();
-  }
-
-  @Test
-  public void delete_descendants() {
-    initRules();
-
-    // create parent and child profiles
-    db.qualityProfileDao().insert(dbSession,
-      QProfileTesting.newXooP1(organization),
-      QProfileTesting.newXooP2(organization),
-      QProfileTesting.newXooP3(organization));
-    List<ActiveRuleChange> changes = tester.get(RuleActivator.class).setParent(dbSession, XOO_P2_KEY, XOO_P1_KEY);
-    changes.addAll(tester.get(RuleActivator.class).setParent(dbSession, XOO_P3_KEY, XOO_P1_KEY));
-    changes.addAll(tester.get(RuleActivator.class).activate(dbSession, new RuleActivation(RuleTesting.XOO_X1), XOO_P1_KEY));
-    dbSession.commit();
-    dbSession.clearCache();
-    activeRuleIndexer.index(changes);
-
-    assertThat(db.qualityProfileDao().selectAll(dbSession, organization)).hasSize(3);
-    assertThat(db.activeRuleDao().selectAll(dbSession)).hasSize(3);
-
-    changes = factory.deleteTree(dbSession, XOO_P1_KEY, false);
-    dbSession.commit();
-    activeRuleIndexer.index(changes);
-
-    dbSession.clearCache();
-    assertThat(db.qualityProfileDao().selectAll(dbSession, organization)).isEmpty();
-    assertThat(db.activeRuleDao().selectAll(dbSession)).isEmpty();
-    assertThat(db.activeRuleDao().selectAllParams(dbSession)).isEmpty();
-    assertThat(db.activeRuleDao().selectByProfileKey(dbSession, XOO_P1_KEY)).isEmpty();
-    assertThat(db.activeRuleDao().selectByProfileKey(dbSession, XOO_P2_KEY)).isEmpty();
-    assertThat(db.activeRuleDao().selectByProfileKey(dbSession, XOO_P3_KEY)).isEmpty();
-  }
-
-  @Test
-  public void do_not_delete_default_profile() {
-    QualityProfileDto profile = QualityProfileTesting.newQualityProfileDto()
-      .setOrganizationUuid(organization.getUuid())
-      .setDefault(true);
-    db.qualityProfileDao().insert(dbSession, profile);
-
-    dbSession.commit();
-
-    try {
-      List<ActiveRuleChange> changes = factory.deleteTree(dbSession, profile.getKey(), false);
-      dbSession.commit();
-      activeRuleIndexer.index(changes);
-      fail();
-    } catch (BadRequestException e) {
-      assertThat(e).hasMessage("The profile marked as default can not be deleted: " + profile.getKey());
-      assertThat(db.qualityProfileDao().selectAll(dbSession, organization)).hasSize(1);
-    }
-  }
-
-  @Test
-  public void do_not_delete_if_default_descendant() {
-    QualityProfileDto parent = QualityProfileTesting.newQualityProfileDto()
-      .setOrganizationUuid(organization.getUuid());
-    QualityProfileDto childNonDefault = QualityProfileTesting.newQualityProfileDto()
-      .setOrganizationUuid(organization.getUuid());
-    QualityProfileDto childDefault = QualityProfileTesting.newQualityProfileDto()
-      .setOrganizationUuid(organization.getUuid())
-      .setDefault(true);
-    db.qualityProfileDao().insert(dbSession, parent, childNonDefault, childDefault);
-
-    List<ActiveRuleChange> changes = tester.get(RuleActivator.class).setParent(dbSession, childNonDefault.getKey(), parent.getKey());
-    changes.addAll(tester.get(RuleActivator.class).setParent(dbSession, childDefault.getKey(), parent.getKey()));
-    dbSession.commit();
-    dbSession.clearCache();
-    activeRuleIndexer.index(changes);
-
-    try {
-      changes = factory.deleteTree(dbSession, parent.getKey(), false);
-      dbSession.commit();
-      activeRuleIndexer.index(changes);
-      fail();
-    } catch (BadRequestException e) {
-      assertThat(e).hasMessage("The profile marked as default can not be deleted: " + childDefault.getKey());
-      assertThat(db.qualityProfileDao().selectAll(dbSession, organization)).hasSize(3);
-    }
-  }
-
-  @Test
-  public void fail_if_unknown_profile_to_be_deleted() {
-    thrown.expect(RowNotFoundException.class);
-    thrown.expectMessage("Quality profile not found: XOO_P1");
-
-    List<ActiveRuleChange> changes = factory.deleteTree(dbSession, XOO_P1_KEY, false);
-    dbSession.commit();
-    activeRuleIndexer.index(changes);
-  }
-
-  private void initRules() {
-    // create pre-defined rules
-    RuleDto xooRule1 = RuleTesting.newXooX1();
-    RuleDto xooRule2 = RuleTesting.newXooX2();
-    db.ruleDao().insert(dbSession, xooRule1);
-    db.ruleDao().insert(dbSession, xooRule2);
-    db.ruleDao().insertRuleParam(dbSession, xooRule1, RuleParamDto.createFor(xooRule1)
-      .setName("max").setDefaultValue("10").setType(RuleParamType.INTEGER.type()));
-    dbSession.commit();
-    dbSession.clearCache();
-    ruleIndexer.index();
-  }
-
   private void expectBadRequestException(String message) {
     thrown.expect(BadRequestException.class);
     thrown.expectMessage(message);