From 1147732183f1ca1644d869655a38a7f2d951d6c1 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Tue, 26 Aug 2014 10:18:41 +0200 Subject: [PATCH] FindByProfile should not return removed rules --- .../server/qualityprofile/index/ActiveRuleIndex.java | 12 ++++++------ .../qualityprofile/ActiveRuleBackendMediumTest.java | 6 +++++- .../sonar/server/rule/RegisterRulesMediumTest.java | 2 +- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleIndex.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleIndex.java index b12c5f8d8ca..fd7b4034fc0 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleIndex.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleIndex.java @@ -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 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); diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ActiveRuleBackendMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ActiveRuleBackendMediumTest.java index 9c99ce8b716..db00b4efd32 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ActiveRuleBackendMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ActiveRuleBackendMediumTest.java @@ -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 diff --git a/server/sonar-server/src/test/java/org/sonar/server/rule/RegisterRulesMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/rule/RegisterRulesMediumTest.java index 22b0d2e8728..790c8c1b647 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/rule/RegisterRulesMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/rule/RegisterRulesMediumTest.java @@ -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); -- 2.39.5