]> source.dussan.org Git - sonarqube.git/commitdiff
FindByProfile should not return removed rules
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 26 Aug 2014 08:18:41 +0000 (10:18 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 26 Aug 2014 08:35:54 +0000 (10:35 +0200)
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleIndex.java
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ActiveRuleBackendMediumTest.java
server/sonar-server/src/test/java/org/sonar/server/rule/RegisterRulesMediumTest.java

index b12c5f8d8cad24098a9191dc464f31ecb736897b..fd7b4034fc061c21a7912cdbb8c5980b681a6310 100644 (file)
@@ -37,11 +37,7 @@ import org.sonar.core.qualityprofile.db.ActiveRuleDto;
 import org.sonar.core.qualityprofile.db.ActiveRuleKey;
 import org.sonar.server.qualityprofile.ActiveRule;
 import org.sonar.server.rule.index.RuleNormalizer;
-import org.sonar.server.search.BaseIndex;
-import org.sonar.server.search.FacetValue;
-import org.sonar.server.search.IndexDefinition;
-import org.sonar.server.search.IndexField;
-import org.sonar.server.search.SearchClient;
+import org.sonar.server.search.*;
 
 import java.io.IOException;
 import java.util.ArrayList;
@@ -138,7 +134,11 @@ public class ActiveRuleIndex extends BaseIndex<ActiveRule, ActiveRuleDto, Active
 
   public List<ActiveRule> findByProfile(String key) {
     SearchRequestBuilder request = getClient().prepareSearch(getIndexName())
-      .setQuery(QueryBuilders.termQuery(ActiveRuleNormalizer.ActiveRuleField.PROFILE_KEY.field(), key))
+      .setQuery(QueryBuilders.filteredQuery(
+        QueryBuilders.termsQuery(ActiveRuleNormalizer.ActiveRuleField.PROFILE_KEY.field(), key),
+        FilterBuilders.boolFilter()
+          .mustNot(FilterBuilders.hasParentFilter(this.getParentType(),
+            FilterBuilders.termFilter(RuleNormalizer.RuleField.STATUS.field(), RuleStatus.REMOVED.name())))))
       .setRouting(key)
         // TODO replace by scrolling
       .setSize(Integer.MAX_VALUE);
index 9c99ce8b716e493aa720f4dbf87a56b148c32151..db00b4efd32eace5d2cf1bbfcd3252ca8b107acc 100644 (file)
@@ -168,11 +168,15 @@ public class ActiveRuleBackendMediumTest extends SearchMediumTest {
 
     RuleDto rule1 = RuleTesting.newXooX1().setSeverity(Severity.MAJOR);
     RuleDto rule2 = RuleTesting.newXooX2().setSeverity(Severity.MAJOR);
-    db.ruleDao().insert(dbSession, rule1, rule2);
+    RuleDto removedRule = RuleTesting.newDto(RuleKey.of("xoo", "removed")).setSeverity(Severity.MAJOR).setStatus(RuleStatus.REMOVED);
+    db.ruleDao().insert(dbSession, rule1, rule2, removedRule);
 
     db.activeRuleDao().insert(dbSession, ActiveRuleDto.createFor(profile1, rule1).setSeverity(Severity.MINOR));
     db.activeRuleDao().insert(dbSession, ActiveRuleDto.createFor(profile1, rule2).setSeverity(Severity.BLOCKER));
     db.activeRuleDao().insert(dbSession, ActiveRuleDto.createFor(profile2, rule2).setSeverity(Severity.CRITICAL));
+    // Removed rule can still be activated for instance when removing the checkstyle plugin, active rules related on checkstyle are not removed
+    // because if the plugin is re-install, quality profiles using these rule are not changed.
+    db.activeRuleDao().insert(dbSession, ActiveRuleDto.createFor(profile2, removedRule).setSeverity(Severity.MAJOR));
     dbSession.commit();
 
     // 1. find by rule key
index 22b0d2e8728bb64dc6a0ee9a0b8f92b87af404fa..790c8c1b6472c1d9d880ec3679033f5015bdb04a 100644 (file)
@@ -348,7 +348,7 @@ public class RegisterRulesMediumTest {
     // Restart without xoo
     register(null);
     assertThat(ruleIndex.getByKey(RuleTesting.XOO_X1).status()).isEqualTo(RuleStatus.REMOVED);
-    assertThat(activeRuleIndex.findByProfile(QProfileTesting.XOO_P1_KEY)).hasSize(1);
+    assertThat(activeRuleIndex.findByProfile(QProfileTesting.XOO_P1_KEY)).isEmpty();
 
     // Re-install
     register(rules);