]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7330 RegisterQualityProfiles is now using ActiveRuleIndexer
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 25 Feb 2016 11:04:23 +0000 (12:04 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 29 Feb 2016 12:26:54 +0000 (13:26 +0100)
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/RegisterQualityProfiles.java
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/RegisterQualityProfilesMediumTest.java

index b9089ffce2cfbcc4f93bc02a410c155d02b638c6..3591d48dc30b53d6d2111f0125f09b846548ed7a 100644 (file)
@@ -24,6 +24,7 @@ import com.google.common.collect.ArrayListMultimap;
 import com.google.common.collect.ListMultimap;
 import com.google.common.collect.Multimaps;
 import com.google.common.collect.Sets;
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
@@ -88,6 +89,7 @@ public class RegisterQualityProfiles {
     Profiler profiler = Profiler.create(Loggers.get(getClass())).startInfo("Register quality profiles");
     DbSession session = dbClient.openSession(false);
     try {
+      activeRuleIndexer.setEnabled(true);
       ListMultimap<String, RulesProfile> profilesByLanguage = profilesByLanguage();
       for (String language : profilesByLanguage.keySet()) {
         List<RulesProfile> defs = profilesByLanguage.get(language);
@@ -95,7 +97,7 @@ public class RegisterQualityProfiles {
           registerProfilesForLanguage(session, language, defs);
         }
       }
-      activeRuleIndexer.setEnabled(true).index();
+      activeRuleIndexer.index();
       profiler.stopDebug();
 
     } finally {
@@ -125,7 +127,6 @@ public class RegisterQualityProfiles {
       QProfileName profileName = new QProfileName(language, name);
       if (shouldRegister(profileName, session)) {
         register(profileName, entry.getValue(), session);
-        session.commit();
       }
       builtInProfiles.put(language, name);
     }
@@ -138,10 +139,15 @@ public class RegisterQualityProfiles {
 
     QualityProfileDto profileDto = dbClient.qualityProfileDao().selectByNameAndLanguage(name.getName(), name.getLanguage(), session);
     if (profileDto != null) {
-      profileFactory.delete(session, profileDto.getKey(), true);
+      // When deleting the profile, we also remove active rule from index in order to have to deal with conflicts when activating new rules
+      // (for instance, if same rule is removed then activated)
+      List<ActiveRuleChange> deleteChanges = profileFactory.delete(session, profileDto.getKey(), true);
+      session.commit();
+      activeRuleIndexer.index(deleteChanges);
     }
     profileFactory.create(session, name);
 
+    List<ActiveRuleChange> changes = new ArrayList<>();
     for (RulesProfile profile : profiles) {
       for (org.sonar.api.rules.ActiveRule activeRule : profile.getActiveRules()) {
         RuleKey ruleKey = RuleKey.of(activeRule.getRepositoryKey(), activeRule.getRuleKey());
@@ -150,12 +156,14 @@ public class RegisterQualityProfiles {
         for (ActiveRuleParam param : activeRule.getActiveRuleParams()) {
           activation.setParameter(param.getKey(), param.getValue());
         }
-        ruleActivator.activate(session, activation, name);
+        changes.addAll(ruleActivator.activate(session, activation, name));
       }
     }
 
     LoadedTemplateDto template = new LoadedTemplateDto(templateKey(name), LoadedTemplateDto.QUALITY_PROFILE_TYPE);
     dbClient.loadedTemplateDao().insert(template, session);
+    session.commit();
+    activeRuleIndexer.index(changes);
   }
 
   private void setDefault(String language, List<RulesProfile> profileDefs, DbSession session) {
index 1c0ccb8d0158f8267fbc9ed914fa7d09389fbde5..6c892422c05bbefb00d98389faf319cb00389ae9 100644 (file)
@@ -51,6 +51,7 @@ import static com.google.common.collect.Lists.newArrayList;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.guava.api.Assertions.assertThat;
 
+// TODO replace this MediumTest by DbTester and EsTester
 public class RegisterQualityProfilesMediumTest {
 
   ServerTester tester;
@@ -68,7 +69,7 @@ public class RegisterQualityProfilesMediumTest {
 
   @Test
   public void register_existing_profile_definitions() {
-    tester = new ServerTester().withStartupTasks().addXoo().addComponents(XooRulesDefinition.class, XooProfileDefinition.class);
+    tester = new ServerTester().withEsIndexes().withStartupTasks().addXoo().addComponents(XooRulesDefinition.class, XooProfileDefinition.class);
     tester.start();
     dbSession = dbClient().openSession(false);
 
@@ -87,7 +88,7 @@ public class RegisterQualityProfilesMediumTest {
     assertThat(activeRuleDao.selectByKey(dbSession, activeRuleKey)).isPresent();
 
     // Check in ES
-    assertThat(tester.get(RuleIndex2.class).search(new RuleQuery().setActivation(true), new SearchOptions()).getIds()).containsOnly(ruleKey);
+    assertThat(tester.get(RuleIndex2.class).search(new RuleQuery().setActivation(true), new SearchOptions()).getIds()).containsOnly(ruleKey, RuleKey.of("xoo", "x2"));
 
     tester.get(Platform.class).restart();
 
@@ -100,12 +101,11 @@ public class RegisterQualityProfilesMediumTest {
     assertThat(activeRule.getSeverityString()).isEqualTo(Severity.CRITICAL);
 
     // Check in ES
-    assertThat(tester.get(RuleIndex2.class).search(new RuleQuery().setActivation(true), new SearchOptions()).getIds()).containsOnly(ruleKey);
+    assertThat(tester.get(RuleIndex2.class).search(new RuleQuery().setActivation(true), new SearchOptions()).getIds()).containsOnly(ruleKey, RuleKey.of("xoo", "x2"));
 
     // TODO
     // Check ActiveRuleParameters in DB
-    Map<String, ActiveRuleParamDto> params =
-      ActiveRuleParamDto.groupByKey(activeRuleDao.selectParamsByActiveRuleKey(dbSession, activeRule.getKey()));
+    Map<String, ActiveRuleParamDto> params = ActiveRuleParamDto.groupByKey(activeRuleDao.selectParamsByActiveRuleKey(dbSession, activeRule.getKey()));
     assertThat(params).hasSize(2);
     // set by profile
     assertThat(params.get("acceptWhitespace").getValue()).isEqualTo("true");
@@ -115,7 +115,7 @@ public class RegisterQualityProfilesMediumTest {
 
   @Test
   public void register_profile_definitions() {
-    tester = new ServerTester().withStartupTasks().addXoo().addComponents(XooRulesDefinition.class, XooProfileDefinition.class);
+    tester = new ServerTester().withEsIndexes().withStartupTasks().addXoo().addComponents(XooRulesDefinition.class, XooProfileDefinition.class);
     tester.start();
     dbSession = dbClient().openSession(false);
 
@@ -151,7 +151,7 @@ public class RegisterQualityProfilesMediumTest {
   @Test
   public void do_not_register_profile_if_missing_language() {
     // xoo language is not installed
-    tester = new ServerTester().addComponents(XooRulesDefinition.class, XooProfileDefinition.class);
+    tester = new ServerTester().withEsIndexes().addComponents(XooRulesDefinition.class, XooProfileDefinition.class);
     tester.start();
     dbSession = dbClient().openSession(false);
 
@@ -162,7 +162,7 @@ public class RegisterQualityProfilesMediumTest {
 
   @Test
   public void fail_if_two_definitions_are_marked_as_default_on_the_same_language() {
-    tester = new ServerTester().addXoo().addComponents(new SimpleProfileDefinition("one", true), new SimpleProfileDefinition("two", true));
+    tester = new ServerTester().withEsIndexes().addXoo().addComponents(new SimpleProfileDefinition("one", true), new SimpleProfileDefinition("two", true));
 
     try {
       tester.start();
@@ -173,7 +173,7 @@ public class RegisterQualityProfilesMediumTest {
 
   @Test
   public void mark_profile_as_default() {
-    tester = new ServerTester().withStartupTasks().addXoo().addComponents(new SimpleProfileDefinition("one", false), new SimpleProfileDefinition("two", true));
+    tester = new ServerTester().withEsIndexes().withStartupTasks().addXoo().addComponents(new SimpleProfileDefinition("one", false), new SimpleProfileDefinition("two", true));
 
     tester.start();
     verifyDefaultProfile("xoo", "two");
@@ -181,7 +181,7 @@ public class RegisterQualityProfilesMediumTest {
 
   @Test
   public void use_sonar_way_as_default_profile_if_none_are_marked_as_default() {
-    tester = new ServerTester().withStartupTasks().addXoo().addComponents(new SimpleProfileDefinition("Sonar way", false), new SimpleProfileDefinition("Other way", false));
+    tester = new ServerTester().withEsIndexes().withStartupTasks().addXoo().addComponents(new SimpleProfileDefinition("Sonar way", false), new SimpleProfileDefinition("Other way", false));
 
     tester.start();
     verifyDefaultProfile("xoo", "Sonar way");
@@ -189,7 +189,7 @@ public class RegisterQualityProfilesMediumTest {
 
   @Test
   public void do_not_reset_default_profile_if_still_valid() {
-    tester = new ServerTester().withStartupTasks().addXoo().addComponents(new SimpleProfileDefinition("one", true), new SimpleProfileDefinition("two", false));
+    tester = new ServerTester().withEsIndexes().withStartupTasks().addXoo().addComponents(new SimpleProfileDefinition("one", true), new SimpleProfileDefinition("two", false));
     tester.start();
 
     QualityProfileDao profileDao = dbClient().qualityProfileDao();
@@ -210,7 +210,7 @@ public class RegisterQualityProfilesMediumTest {
    */
   @Test
   public void clean_up_profiles_if_missing_loaded_template() {
-    tester = new ServerTester().addXoo().addComponents(XooRulesDefinition.class, XooProfileDefinition.class);
+    tester = new ServerTester().withEsIndexes().addXoo().addComponents(XooRulesDefinition.class, XooProfileDefinition.class);
     tester.start();
 
     dbSession = dbClient().openSession(false);