]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7400 Remove no more used code
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 13 May 2016 07:57:27 +0000 (09:57 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 16 May 2016 10:08:18 +0000 (12:08 +0200)
13 files changed:
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileLoader.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleIndex.java
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileBackuperMediumTest.java
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileCopierMediumTest.java
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileExportersTest.java
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileFactoryMediumTest.java
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileResetMediumTest.java
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileServiceMediumTest.java
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/RuleActivatorMediumTest.java
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/index/ActiveRuleIndexTest.java
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/ExportActionTest.java
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsMediumTest.java
server/sonar-server/src/test/java/org/sonar/server/rule/RuleDeleterMediumTest.java

index ff63491c89198278f91bca5dc0838d777c578e2f..a48b7dbca745b0680e7cc23474b6c4fa6a9fc074 100644 (file)
@@ -21,19 +21,15 @@ package org.sonar.server.qualityprofile;
 
 import com.google.common.collect.Lists;
 import com.google.common.collect.Multimap;
-import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import javax.annotation.CheckForNull;
-import org.sonar.api.rule.RuleKey;
 import org.sonar.api.rule.RuleStatus;
 import org.sonar.api.server.ServerSide;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
-import org.sonar.db.qualityprofile.ActiveRuleKey;
 import org.sonar.db.qualityprofile.QualityProfileDto;
 import org.sonar.server.es.SearchOptions;
-import org.sonar.server.qualityprofile.index.ActiveRuleDoc;
 import org.sonar.server.qualityprofile.index.ActiveRuleIndex;
 import org.sonar.server.rule.index.RuleIndex;
 import org.sonar.server.rule.index.RuleQuery;
@@ -85,19 +81,6 @@ public class QProfileLoader {
     }
   }
 
-  @CheckForNull
-  public ActiveRule getActiveRule(ActiveRuleKey key) {
-    return activeRuleIndex.getNullableByKey(key);
-  }
-
-  public List<ActiveRule> findActiveRulesByRule(RuleKey key) {
-    return activeRuleIndex.findByRule(key);
-  }
-
-  public Iterator<ActiveRuleDoc> findActiveRulesByProfile(String key) {
-    return activeRuleIndex.findByProfile(key);
-  }
-
   public Map<String, Long> countAllActiveRules() {
     return activeRuleIndex.countAllByQualityProfileKey();
   }
index 9e7964ae2989387b4c8aef2c558b2948c250d150..1e7c9c16f56411b1d7a9a9d38cb8149391cee70d 100644 (file)
  */
 package org.sonar.server.qualityprofile.index;
 
-import com.google.common.base.Function;
 import com.google.common.collect.ArrayListMultimap;
 import com.google.common.collect.Multimap;
-import java.util.ArrayList;
 import java.util.HashMap;
-import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
-import javax.annotation.CheckForNull;
-import javax.annotation.Nonnull;
-import org.elasticsearch.action.get.GetRequestBuilder;
-import org.elasticsearch.action.get.GetResponse;
+import javax.annotation.Nullable;
 import org.elasticsearch.action.search.SearchRequestBuilder;
 import org.elasticsearch.action.search.SearchResponse;
-import org.elasticsearch.action.search.SearchType;
-import org.elasticsearch.common.unit.TimeValue;
 import org.elasticsearch.index.query.FilterBuilder;
 import org.elasticsearch.index.query.FilterBuilders;
 import org.elasticsearch.index.query.QueryBuilders;
-import org.elasticsearch.search.SearchHit;
 import org.elasticsearch.search.aggregations.Aggregation;
 import org.elasticsearch.search.aggregations.AggregationBuilders;
 import org.elasticsearch.search.aggregations.Aggregations;
 import org.elasticsearch.search.aggregations.bucket.terms.StringTerms;
 import org.elasticsearch.search.aggregations.bucket.terms.Terms;
 import org.elasticsearch.search.aggregations.metrics.valuecount.InternalValueCount;
-import org.sonar.api.rule.RuleKey;
 import org.sonar.api.rule.RuleStatus;
-import org.sonar.db.qualityprofile.ActiveRuleKey;
 import org.sonar.server.es.BaseIndex;
 import org.sonar.server.es.EsClient;
-import org.sonar.server.qualityprofile.ActiveRule;
 import org.sonar.server.rule.index.RuleIndexDefinition;
 import org.sonar.server.search.FacetValue;
 
-import static org.sonar.server.es.EsUtils.SCROLL_TIME_IN_MINUTES;
-import static org.sonar.server.es.EsUtils.scroll;
 import static org.sonar.server.rule.index.RuleIndexDefinition.FIELD_ACTIVE_RULE_INHERITANCE;
 import static org.sonar.server.rule.index.RuleIndexDefinition.FIELD_ACTIVE_RULE_PROFILE_KEY;
 import static org.sonar.server.rule.index.RuleIndexDefinition.FIELD_ACTIVE_RULE_SEVERITY;
@@ -76,68 +62,6 @@ public class ActiveRuleIndex extends BaseIndex {
     super(client);
   }
 
-  /**
-   * @deprecated since 5.5, use {@link org.sonar.db.qualityprofile.ActiveRuleDao instead}
-   */
-  @Deprecated
-  @CheckForNull
-  public ActiveRule getNullableByKey(ActiveRuleKey key) {
-    GetRequestBuilder request = getClient().prepareGet()
-      .setIndex(RuleIndexDefinition.INDEX)
-      .setType(RuleIndexDefinition.TYPE_ACTIVE_RULE)
-      .setId(key.toString())
-      .setFetchSource(true)
-      .setRouting(key.ruleKey().repository());
-
-    GetResponse response = request.get();
-    if (response.isExists()) {
-      return new ActiveRuleDoc(response.getSource());
-    }
-    return null;
-  }
-
-  /**
-   * @deprecated since 5.5, use {@link org.sonar.db.qualityprofile.ActiveRuleDao instead}
-   */
-  @Deprecated
-  public List<ActiveRule> findByRule(RuleKey key) {
-    SearchRequestBuilder request = getClient().prepareSearch(RuleIndexDefinition.INDEX)
-      .setQuery(QueryBuilders
-        .hasParentQuery(RuleIndexDefinition.TYPE_RULE,
-          QueryBuilders.idsQuery(RuleIndexDefinition.TYPE_RULE)
-            .addIds(key.toString())
-        ))
-      .setRouting(key.repository())
-      .setSize(Integer.MAX_VALUE);
-
-    SearchResponse response = request.get();
-
-    List<ActiveRule> activeRules = new ArrayList<>();
-    for (SearchHit hit : response.getHits()) {
-      activeRules.add(new ActiveRuleDoc(hit.getSource()));
-    }
-    return activeRules;
-  }
-
-  /**
-   * @deprecated since 5.5, use {@link org.sonar.db.qualityprofile.ActiveRuleDao instead}
-   */
-  @Deprecated
-  public Iterator<ActiveRuleDoc> findByProfile(String key) {
-    SearchRequestBuilder request = getClient().prepareSearch(RuleIndexDefinition.INDEX)
-      .setTypes(RuleIndexDefinition.TYPE_ACTIVE_RULE)
-      .setSearchType(SearchType.SCAN)
-      .setScroll(TimeValue.timeValueMinutes(SCROLL_TIME_IN_MINUTES))
-      .setSize(100)
-      .setQuery(QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(), FilterBuilders.boolFilter()
-        .must(FilterBuilders.termFilter(RuleIndexDefinition.FIELD_ACTIVE_RULE_PROFILE_KEY, key))
-        .mustNot(FilterBuilders.hasParentFilter(RuleIndexDefinition.TYPE_RULE,
-          FilterBuilders.termFilter(RuleIndexDefinition.FIELD_RULE_STATUS, RuleStatus.REMOVED.name())))));
-
-    SearchResponse response = request.get();
-    return scroll(getClient(), response.getScrollId(), ToDoc.INSTANCE);
-  }
-
   public Map<String, Long> countAllByQualityProfileKey() {
     return countByField(FIELD_ACTIVE_RULE_PROFILE_KEY,
       FilterBuilders.hasParentFilter(TYPE_RULE,
@@ -163,8 +87,7 @@ public class ActiveRuleIndex extends BaseIndex {
 
     SearchResponse response = request.get();
 
-    Terms values =
-      response.getAggregations().get(indexField);
+    Terms values = response.getAggregations().get(indexField);
 
     for (Terms.Bucket value : values.getBuckets()) {
       counts.put(value.getKey(), value.getDocCount());
@@ -198,7 +121,7 @@ public class ActiveRuleIndex extends BaseIndex {
     return stats;
   }
 
-  private Multimap<String, FacetValue> processAggregations(Aggregations aggregations) {
+  private Multimap<String, FacetValue> processAggregations(@Nullable Aggregations aggregations) {
     Multimap<String, FacetValue> stats = ArrayListMultimap.create();
     if (aggregations != null) {
       for (Aggregation aggregation : aggregations.asList()) {
@@ -217,13 +140,4 @@ public class ActiveRuleIndex extends BaseIndex {
     return stats;
   }
 
-  private enum ToDoc implements Function<Map<String, Object>, ActiveRuleDoc> {
-    INSTANCE;
-
-    @Override
-    public ActiveRuleDoc apply(@Nonnull Map<String, Object> input) {
-      return new ActiveRuleDoc(input);
-    }
-  }
-
 }
index b7cd25938044bb365a1d27d6139841985fe0f046..cab4790cd1ccc364f0f70a26d342094cdfe0070e 100644 (file)
@@ -19,7 +19,6 @@
  */
 package org.sonar.server.qualityprofile;
 
-import com.google.common.collect.Lists;
 import com.google.common.io.Resources;
 import java.io.StringReader;
 import java.io.StringWriter;
@@ -46,15 +45,29 @@ import org.sonar.db.qualityprofile.ActiveRuleParamDto;
 import org.sonar.db.qualityprofile.QualityProfileDto;
 import org.sonar.db.rule.RuleDto;
 import org.sonar.db.rule.RuleParamDto;
-import org.sonar.db.rule.RuleTesting;
-import org.sonar.server.qualityprofile.index.ActiveRuleDoc;
 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.db.qualityprofile.ActiveRuleDto.INHERITED;
+import static org.sonar.db.qualityprofile.ActiveRuleDto.OVERRIDES;
+import static org.sonar.db.rule.RuleTesting.XOO_X1;
+import static org.sonar.db.rule.RuleTesting.XOO_X2;
+import static org.sonar.db.rule.RuleTesting.newDto;
+import static org.sonar.db.rule.RuleTesting.newXooX1;
+import static org.sonar.db.rule.RuleTesting.newXooX2;
+import static org.sonar.server.qualityprofile.QProfileTesting.XOO_P1_KEY;
+import static org.sonar.server.qualityprofile.QProfileTesting.XOO_P1_NAME;
+import static org.sonar.server.qualityprofile.QProfileTesting.XOO_P2_KEY;
+import static org.sonar.server.qualityprofile.QProfileTesting.XOO_P2_NAME;
+import static org.sonar.server.qualityprofile.QProfileTesting.XOO_P3_NAME;
+import static org.sonar.server.qualityprofile.QProfileTesting.newXooP1;
+import static org.sonar.server.qualityprofile.QProfileTesting.newXooP2;
 
 public class QProfileBackuperMediumTest {
 
@@ -81,8 +94,8 @@ public class QProfileBackuperMediumTest {
     activeRuleIndexer.setEnabled(true);
 
     // create pre-defined rules
-    RuleDto xooRule1 = RuleTesting.newXooX1().setSeverity("MINOR").setLanguage("xoo");
-    RuleDto xooRule2 = RuleTesting.newXooX2().setSeverity("MAJOR").setLanguage("xoo");
+    RuleDto xooRule1 = newXooX1().setSeverity("MINOR").setLanguage("xoo");
+    RuleDto xooRule2 = newXooX2().setSeverity("MAJOR").setLanguage("xoo");
     db.ruleDao().insert(dbSession, xooRule1);
     db.ruleDao().insert(dbSession, xooRule2);
     db.ruleDao().insertRuleParam(dbSession, xooRule1, RuleParamDto.createFor(xooRule1)
@@ -100,28 +113,28 @@ public class QProfileBackuperMediumTest {
   @Test
   public void backup() throws Exception {
     RuleKey blahRuleKey = RuleKey.of("blah", "my-rule");
-    RuleDto blahRule = RuleTesting.newDto(blahRuleKey).setSeverity("INFO").setLanguage("xoo");
+    RuleDto blahRule = newDto(blahRuleKey).setSeverity("INFO").setLanguage("xoo");
     db.ruleDao().insert(dbSession, blahRule);
     dbSession.commit();
     dbSession.clearCache();
     ruleIndexer.index();
 
     // create profile P1 with rules x2 and x1 activated
-    db.qualityProfileDao().insert(dbSession, QProfileTesting.newXooP1());
-    RuleActivation activation1 = new RuleActivation(RuleTesting.XOO_X2).setSeverity("MINOR");
-    RuleActivation activation2 = new RuleActivation(RuleTesting.XOO_X1);
+    db.qualityProfileDao().insert(dbSession, newXooP1());
+    RuleActivation activation1 = new RuleActivation(XOO_X2).setSeverity("MINOR");
+    RuleActivation activation2 = new RuleActivation(XOO_X1);
     RuleActivation activation3 = new RuleActivation(blahRuleKey);
     activation2.setSeverity(Severity.BLOCKER);
     activation2.setParameter("max", "7");
-    tester.get(RuleActivator.class).activate(dbSession, activation1, QProfileTesting.XOO_P1_NAME);
-    tester.get(RuleActivator.class).activate(dbSession, activation2, QProfileTesting.XOO_P1_NAME);
-    tester.get(RuleActivator.class).activate(dbSession, activation3, QProfileTesting.XOO_P1_NAME);
+    tester.get(RuleActivator.class).activate(dbSession, activation1, XOO_P1_NAME);
+    tester.get(RuleActivator.class).activate(dbSession, activation2, XOO_P1_NAME);
+    tester.get(RuleActivator.class).activate(dbSession, activation3, XOO_P1_NAME);
     dbSession.commit();
     dbSession.clearCache();
     activeRuleIndexer.index();
 
     StringWriter output = new StringWriter();
-    tester.get(QProfileBackuper.class).backup(QProfileTesting.XOO_P1_KEY, output);
+    tester.get(QProfileBackuper.class).backup(XOO_P1_KEY, output);
 
     XMLUnit.setIgnoreWhitespace(true);
     XMLUnit.setIgnoreComments(true);
@@ -146,34 +159,38 @@ public class QProfileBackuperMediumTest {
       Resources.toString(getClass().getResource("QProfileBackuperMediumTest/restore.xml"), StandardCharsets.UTF_8)),
       null);
 
+    // Check in db
     QualityProfileDto profile = db.qualityProfileDao().selectByNameAndLanguage("P1", "xoo", dbSession);
     assertThat(profile).isNotNull();
 
-    List<ActiveRuleDoc> activeRules = Lists.newArrayList(tester.get(QProfileLoader.class).findActiveRulesByProfile(profile.getKey()));
+    List<ActiveRuleDto> activeRules = db.activeRuleDao().selectByProfileKey(dbSession, profile.getKey());
     assertThat(activeRules).hasSize(1);
-    ActiveRuleDoc activeRuleDoc = activeRules.get(0);
-    assertThat(activeRuleDoc.severity()).isEqualTo("BLOCKER");
-    assertThat(activeRuleDoc.inheritance()).isEqualTo(ActiveRule.Inheritance.NONE);
+    ActiveRuleDto activeRuleDoc = activeRules.get(0);
+    assertThat(activeRuleDoc.getSeverityString()).isEqualTo("BLOCKER");
+    assertThat(activeRuleDoc.getInheritance()).isNull();
 
-    ActiveRuleDto activeRuleDto = db.activeRuleDao().selectOrFailByKey(dbSession, activeRuleDoc.key());
+    ActiveRuleDto activeRuleDto = db.activeRuleDao().selectOrFailByKey(dbSession, activeRuleDoc.getKey());
     List<ActiveRuleParamDto> params = tester.get(ActiveRuleDao.class).selectParamsByActiveRuleId(dbSession, activeRuleDto.getId());
     assertThat(params).hasSize(1);
     assertThat(params.get(0).getKey()).isEqualTo("max");
     assertThat(params.get(0).getValue()).isEqualTo("7");
+
+    // Check in es
+    assertThat(tester.get(RuleIndex.class).searchAll(new RuleQuery().setQProfileKey(profile.getKey()).setActivation(true))).containsOnly(XOO_X1);
   }
 
   @Test
   public void restore_and_update_profile() throws Exception {
     // create profile P1 with rules x1 and x2 activated
-    db.qualityProfileDao().insert(dbSession, QProfileTesting.newXooP1());
-    RuleActivation activation = new RuleActivation(RuleTesting.XOO_X1);
+    db.qualityProfileDao().insert(dbSession, newXooP1());
+    RuleActivation activation = new RuleActivation(XOO_X1);
     activation.setSeverity(Severity.INFO);
     activation.setParameter("max", "10");
-    tester.get(RuleActivator.class).activate(dbSession, activation, QProfileTesting.XOO_P1_NAME);
+    tester.get(RuleActivator.class).activate(dbSession, activation, XOO_P1_NAME);
 
-    activation = new RuleActivation(RuleTesting.XOO_X2);
+    activation = new RuleActivation(XOO_X2);
     activation.setSeverity(Severity.INFO);
-    tester.get(RuleActivator.class).activate(dbSession, activation, QProfileTesting.XOO_P1_NAME);
+    tester.get(RuleActivator.class).activate(dbSession, activation, XOO_P1_NAME);
     dbSession.commit();
     dbSession.clearCache();
     activeRuleIndexer.index();
@@ -183,32 +200,36 @@ public class QProfileBackuperMediumTest {
     tester.get(QProfileBackuper.class).restore(new StringReader(
       Resources.toString(getClass().getResource("QProfileBackuperMediumTest/restore.xml"), StandardCharsets.UTF_8)), null);
 
-    List<ActiveRuleDoc> activeRules = Lists.newArrayList(tester.get(QProfileLoader.class).findActiveRulesByProfile(QProfileTesting.XOO_P1_KEY));
+    // Check in db
+    List<ActiveRuleDto> activeRules = db.activeRuleDao().selectByProfileKey(dbSession, XOO_P1_KEY);
     assertThat(activeRules).hasSize(1);
-    ActiveRuleDoc activeRuleDoc = activeRules.get(0);
-    assertThat(activeRuleDoc.severity()).isEqualTo("BLOCKER");
-    assertThat(activeRuleDoc.inheritance()).isEqualTo(ActiveRule.Inheritance.NONE);
+    ActiveRuleDto activeRuleDoc = activeRules.get(0);
+    assertThat(activeRuleDoc.getSeverityString()).isEqualTo("BLOCKER");
+    assertThat(activeRuleDoc.getInheritance()).isNull();
 
-    ActiveRuleDto activeRuleDto = db.activeRuleDao().selectOrFailByKey(dbSession, activeRuleDoc.key());
+    ActiveRuleDto activeRuleDto = db.activeRuleDao().selectOrFailByKey(dbSession, activeRuleDoc.getKey());
     List<ActiveRuleParamDto> params = tester.get(ActiveRuleDao.class).selectParamsByActiveRuleId(dbSession, activeRuleDto.getId());
     assertThat(params).hasSize(1);
     assertThat(params.get(0).getKey()).isEqualTo("max");
     assertThat(params.get(0).getValue()).isEqualTo("7");
+
+    // Check in es
+    assertThat(tester.get(RuleIndex.class).searchAll(new RuleQuery().setQProfileKey(XOO_P1_KEY).setActivation(true))).containsOnly(XOO_X1);
   }
 
   @Test
   public void restore_child_profile() throws Exception {
     // define two parent/child profiles
     db.qualityProfileDao().insert(dbSession,
-      QProfileTesting.newXooP1(),
-      QProfileTesting.newXooP2().setParentKee(QProfileTesting.XOO_P1_KEY));
+      newXooP1(),
+      newXooP2().setParentKee(XOO_P1_KEY));
     dbSession.commit();
 
     // rule x1 is activated on parent profile (so inherited by child profile)
-    RuleActivation activation = new RuleActivation(RuleTesting.XOO_X1);
+    RuleActivation activation = new RuleActivation(XOO_X1);
     activation.setSeverity(Severity.INFO);
     activation.setParameter("max", "10");
-    tester.get(RuleActivator.class).activate(dbSession, activation, QProfileTesting.XOO_P1_KEY);
+    tester.get(RuleActivator.class).activate(dbSession, activation, XOO_P1_KEY);
     dbSession.commit();
     dbSession.clearCache();
     activeRuleIndexer.index();
@@ -218,26 +239,26 @@ public class QProfileBackuperMediumTest {
       Resources.toString(getClass().getResource("QProfileBackuperMediumTest/restore-child.xml"), StandardCharsets.UTF_8)), null);
 
     // parent profile is unchanged
-    List<ActiveRuleDoc> activeRules = Lists.newArrayList(tester.get(QProfileLoader.class).findActiveRulesByProfile(QProfileTesting.XOO_P1_KEY));
+    List<ActiveRuleDto> activeRules = db.activeRuleDao().selectByProfileKey(dbSession, XOO_P1_KEY);
     assertThat(activeRules).hasSize(1);
-    ActiveRuleDoc activeRuleDoc = activeRules.get(0);
-    assertThat(activeRuleDoc.severity()).isEqualTo("INFO");
-    assertThat(activeRuleDoc.inheritance()).isEqualTo(ActiveRule.Inheritance.NONE);
+    ActiveRuleDto activeRuleDoc = activeRules.get(0);
+    assertThat(activeRuleDoc.getSeverityString()).isEqualTo("INFO");
+    assertThat(activeRuleDoc.getInheritance()).isNull();
 
-    ActiveRuleDto activeRuleDto = db.activeRuleDao().selectOrFailByKey(dbSession, activeRuleDoc.key());
+    ActiveRuleDto activeRuleDto = db.activeRuleDao().selectOrFailByKey(dbSession, activeRuleDoc.getKey());
     List<ActiveRuleParamDto> params = tester.get(ActiveRuleDao.class).selectParamsByActiveRuleId(dbSession, activeRuleDto.getId());
     assertThat(params).hasSize(1);
     assertThat(params.get(0).getKey()).isEqualTo("max");
     assertThat(params.get(0).getValue()).isEqualTo("10");
 
     // child profile overrides parent
-    activeRules = Lists.newArrayList(tester.get(QProfileLoader.class).findActiveRulesByProfile(QProfileTesting.XOO_P2_KEY));
+    activeRules = db.activeRuleDao().selectByProfileKey(dbSession, XOO_P2_KEY);
     assertThat(activeRules).hasSize(1);
     activeRuleDoc = activeRules.get(0);
-    assertThat(activeRuleDoc.severity()).isEqualTo("BLOCKER");
-    assertThat(activeRuleDoc.inheritance()).isEqualTo(ActiveRule.Inheritance.OVERRIDES);
+    assertThat(activeRuleDoc.getSeverityString()).isEqualTo("BLOCKER");
+    assertThat(activeRuleDoc.getInheritance()).isEqualTo(OVERRIDES);
 
-    activeRuleDto = db.activeRuleDao().selectOrFailByKey(dbSession, activeRuleDoc.key());
+    activeRuleDto = db.activeRuleDao().selectOrFailByKey(dbSession, activeRuleDoc.getKey());
     params = tester.get(ActiveRuleDao.class).selectParamsByActiveRuleId(dbSession, activeRuleDto.getId());
     assertThat(params).hasSize(1);
     assertThat(params.get(0).getKey()).isEqualTo("max");
@@ -248,15 +269,15 @@ public class QProfileBackuperMediumTest {
   public void restore_parent_profile() throws Exception {
     // define two parent/child profiles
     db.qualityProfileDao().insert(dbSession,
-      QProfileTesting.newXooP1(),
-      QProfileTesting.newXooP2().setParentKee(QProfileTesting.XOO_P1_KEY));
+      newXooP1(),
+      newXooP2().setParentKee(XOO_P1_KEY));
     dbSession.commit();
 
     // rule x1 is activated on parent profile (so inherited by child profile)
-    RuleActivation activation = new RuleActivation(RuleTesting.XOO_X1);
+    RuleActivation activation = new RuleActivation(XOO_X1);
     activation.setSeverity(Severity.INFO);
     activation.setParameter("max", "10");
-    tester.get(RuleActivator.class).activate(dbSession, activation, QProfileTesting.XOO_P1_KEY);
+    tester.get(RuleActivator.class).activate(dbSession, activation, XOO_P1_KEY);
     dbSession.commit();
     dbSession.clearCache();
     activeRuleIndexer.index();
@@ -266,27 +287,27 @@ public class QProfileBackuperMediumTest {
       Resources.toString(getClass().getResource("QProfileBackuperMediumTest/restore-parent.xml"), StandardCharsets.UTF_8)), null);
 
     // parent profile is updated
-    List<ActiveRuleDoc> activeRules = Lists.newArrayList(tester.get(QProfileLoader.class).findActiveRulesByProfile(QProfileTesting.XOO_P1_KEY));
+    List<ActiveRuleDto> activeRules = db.activeRuleDao().selectByProfileKey(dbSession, XOO_P1_KEY);
     assertThat(activeRules).hasSize(1);
 
-    ActiveRuleDoc activeRuleDoc = activeRules.get(0);
-    assertThat(activeRuleDoc.severity()).isEqualTo("BLOCKER");
-    assertThat(activeRuleDoc.inheritance()).isEqualTo(ActiveRule.Inheritance.NONE);
+    ActiveRuleDto activeRuleDoc = activeRules.get(0);
+    assertThat(activeRuleDoc.getSeverityString()).isEqualTo("BLOCKER");
+    assertThat(activeRuleDoc.getInheritance()).isNull();
 
-    ActiveRuleDto activeRuleDto = db.activeRuleDao().selectOrFailByKey(dbSession, activeRuleDoc.key());
+    ActiveRuleDto activeRuleDto = db.activeRuleDao().selectOrFailByKey(dbSession, activeRuleDoc.getKey());
     List<ActiveRuleParamDto> params = tester.get(ActiveRuleDao.class).selectParamsByActiveRuleId(dbSession, activeRuleDto.getId());
     assertThat(params).hasSize(1);
     assertThat(params.get(0).getKey()).isEqualTo("max");
     assertThat(params.get(0).getValue()).isEqualTo("7");
 
     // child profile is inherited
-    activeRules = Lists.newArrayList(tester.get(QProfileLoader.class).findActiveRulesByProfile(QProfileTesting.XOO_P2_KEY));
+    activeRules = db.activeRuleDao().selectByProfileKey(dbSession, XOO_P2_KEY);
     assertThat(activeRules).hasSize(1);
     activeRuleDoc = activeRules.get(0);
-    assertThat(activeRuleDoc.severity()).isEqualTo("BLOCKER");
-    assertThat(activeRuleDoc.inheritance()).isEqualTo(ActiveRule.Inheritance.INHERITED);
+    assertThat(activeRuleDoc.getSeverityString()).isEqualTo("BLOCKER");
+    assertThat(activeRuleDoc.getInheritance()).isEqualTo(INHERITED);
 
-    activeRuleDto = db.activeRuleDao().selectOrFailByKey(dbSession, activeRuleDoc.key());
+    activeRuleDto = db.activeRuleDao().selectOrFailByKey(dbSession, activeRuleDoc.getKey());
     params = tester.get(ActiveRuleDao.class).selectParamsByActiveRuleId(dbSession, activeRuleDto.getId());
     assertThat(params).hasSize(1);
     assertThat(params.get(0).getKey()).isEqualTo("max");
@@ -297,26 +318,25 @@ public class QProfileBackuperMediumTest {
   public void keep_other_inherited_rules() throws Exception {
     // define two parent/child profiles
     db.qualityProfileDao().insert(dbSession,
-      QProfileTesting.newXooP1(),
-      QProfileTesting.newXooP2().setParentKee(QProfileTesting.XOO_P1_KEY));
+      newXooP1(),
+      newXooP2().setParentKee(XOO_P1_KEY));
     dbSession.commit();
 
     // rule x1 is activated on parent profile and is inherited by child profile
-    RuleActivation activation = new RuleActivation(RuleTesting.XOO_X1);
+    RuleActivation activation = new RuleActivation(XOO_X1);
     activation.setSeverity(Severity.INFO);
     activation.setParameter("max", "10");
-    tester.get(RuleActivator.class).activate(dbSession, activation, QProfileTesting.XOO_P1_KEY);
+    tester.get(RuleActivator.class).activate(dbSession, activation, XOO_P1_KEY);
     dbSession.commit();
     dbSession.clearCache();
     activeRuleIndexer.index();
 
     // backup of child profile contains x2 but not x1
     tester.get(QProfileBackuper.class).restore(new StringReader(
-      Resources.toString(getClass().getResource("QProfileBackuperMediumTest/keep_other_inherited_rules.xml"), StandardCharsets.UTF_8)), QProfileTesting.XOO_P2_NAME);
+      Resources.toString(getClass().getResource("QProfileBackuperMediumTest/keep_other_inherited_rules.xml"), StandardCharsets.UTF_8)), XOO_P2_NAME);
 
     // x1 and x2
-    List<ActiveRuleDoc> activeRules = Lists.newArrayList(tester.get(QProfileLoader.class).findActiveRulesByProfile(QProfileTesting.XOO_P2_KEY));
-    assertThat(activeRules).hasSize(2);
+    assertThat(db.activeRuleDao().selectByProfileKey(dbSession, XOO_P2_KEY)).hasSize(2);
   }
 
   @Test
@@ -357,15 +377,14 @@ public class QProfileBackuperMediumTest {
   public void restore_and_override_profile_name() throws Exception {
     tester.get(QProfileBackuper.class).restore(new StringReader(
       Resources.toString(getClass().getResource("QProfileBackuperMediumTest/restore.xml"), StandardCharsets.UTF_8)),
-      QProfileTesting.XOO_P3_NAME);
+      XOO_P3_NAME);
 
-    List<ActiveRuleDoc> activeRules = Lists.newArrayList(tester.get(QProfileLoader.class).findActiveRulesByProfile(QProfileTesting.XOO_P1_KEY));
+    List<ActiveRuleDto> activeRules = db.activeRuleDao().selectByProfileKey(dbSession, XOO_P1_KEY);
     assertThat(activeRules).hasSize(0);
 
     QualityProfileDto target = db.qualityProfileDao().selectByNameAndLanguage("P3", "xoo", dbSession);
     assertThat(target).isNotNull();
-    activeRules = Lists.newArrayList(tester.get(QProfileLoader.class).findActiveRulesByProfile(target.getKey()));
-    assertThat(activeRules).hasSize(1);
+    assertThat(db.activeRuleDao().selectByProfileKey(dbSession, target.getKey())).hasSize(1);
   }
 
   @Test
index 3ea8a2477d05a11d705d21e8647761c0852f4f97..b7b1131dc20f1e40154b69d84b9ebd484c9538ed 100644 (file)
@@ -20,7 +20,6 @@
 package org.sonar.server.qualityprofile;
 
 import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.Lists;
 import java.util.List;
 import java.util.Map;
 import javax.annotation.Nullable;
@@ -39,10 +38,11 @@ import org.sonar.db.qualityprofile.QualityProfileDto;
 import org.sonar.db.rule.RuleDto;
 import org.sonar.db.rule.RuleParamDto;
 import org.sonar.db.rule.RuleTesting;
-import org.sonar.server.qualityprofile.index.ActiveRuleDoc;
 import org.sonar.server.qualityprofile.index.ActiveRuleIndex;
 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;
 
@@ -190,18 +190,18 @@ public class QProfileCopierMediumTest {
     verifyOneActiveRule(dto.getKey(), expectedSeverity, expectedInheritance, expectedParams);
   }
 
-  private void verifyOneActiveRule(String profileKey, String expectedSeverity,
-    @Nullable String expectedInheritance, Map<String, String> expectedParams) {
+  private void verifyOneActiveRule(String profileKey, String expectedSeverity, @Nullable String expectedInheritance, Map<String, String> expectedParams) {
 
-    List<ActiveRuleDoc> activeRules = Lists.newArrayList(index.findByProfile(profileKey));
+    // check in db
+    List<ActiveRuleDto> activeRules = db.activeRuleDao().selectByProfileKey(dbSession, profileKey);
     assertThat(activeRules).hasSize(1);
 
-    ActiveRuleDoc activeRule = activeRules.get(0);
-    assertThat(activeRule.severity()).isEqualTo(expectedSeverity);
-    assertThat(activeRule.inheritance()).isEqualTo(expectedInheritance == null ? ActiveRule.Inheritance.NONE : ActiveRule.Inheritance.valueOf(expectedInheritance));
+    ActiveRuleDto activeRule = activeRules.get(0);
+    assertThat(activeRule.getSeverityString()).isEqualTo(expectedSeverity);
+    assertThat(activeRule.getInheritance()).isEqualTo(expectedInheritance);
 
     // verify parameters
-    ActiveRuleDto activeRuleDto = db.activeRuleDao().selectOrFailByKey(dbSession, activeRule.key());
+    ActiveRuleDto activeRuleDto = db.activeRuleDao().selectOrFailByKey(dbSession, activeRule.getKey());
     List<ActiveRuleParamDto> params = db.activeRuleDao().selectParamsByActiveRuleId(dbSession, activeRuleDto.getId());
     assertThat(params).hasSize(expectedParams.size());
     Map<String, ActiveRuleParamDto> paramsByKey = ActiveRuleParamDto.groupByKey(params);
@@ -209,5 +209,8 @@ public class QProfileCopierMediumTest {
       String value = paramsByKey.get(entry.getKey()).getValue();
       assertThat(value).isEqualTo(entry.getValue());
     }
+
+    // check in es
+    assertThat(tester.get(RuleIndex.class).searchAll(new RuleQuery().setQProfileKey(profileKey).setActivation(true))).hasSize(1);
   }
 }
index b958e49f1df41d3d039b10f1d1e23157b820fcac..9fed6b82926c69be80fc8fcee678f3a9cd7e936e 100644 (file)
@@ -19,7 +19,6 @@
  */
 package org.sonar.server.qualityprofile;
 
-import com.google.common.collect.Lists;
 import java.io.IOException;
 import java.io.Reader;
 import java.io.Writer;
@@ -41,11 +40,13 @@ import org.sonar.api.server.rule.RulesDefinition;
 import org.sonar.api.utils.ValidationMessages;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
+import org.sonar.db.qualityprofile.ActiveRuleDto;
 import org.sonar.db.qualityprofile.QualityProfileDto;
 import org.sonar.server.exceptions.BadRequestException;
 import org.sonar.server.exceptions.NotFoundException;
-import org.sonar.server.qualityprofile.index.ActiveRuleDoc;
 import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
+import org.sonar.server.rule.index.RuleIndex;
+import org.sonar.server.rule.index.RuleQuery;
 import org.sonar.server.tester.ServerTester;
 import org.sonar.server.tester.UserSessionRule;
 
@@ -140,17 +141,21 @@ public class QProfileExportersTest {
     db.qualityProfileDao().insert(dbSession, profileDto);
     dbSession.commit();
 
-    assertThat(loader.findActiveRulesByProfile(profileDto.getKey())).isEmpty();
+    assertThat(db.activeRuleDao().selectByProfileKey(dbSession, profileDto.getKey())).isEmpty();
 
     QProfileResult result = exporters.importXml(profileDto, "XooProfileImporter", "<xml/>", dbSession);
     dbSession.commit();
     activeRuleIndexer.index(result.getChanges());
 
-    List<ActiveRuleDoc> activeRules = Lists.newArrayList(loader.findActiveRulesByProfile(profileDto.getKey()));
+    // Check in db
+    List<ActiveRuleDto> activeRules = db.activeRuleDao().selectByProfileKey(dbSession, profileDto.getKey());
     assertThat(activeRules).hasSize(1);
-    ActiveRule activeRule = activeRules.get(0);
-    assertThat(activeRule.key().ruleKey()).isEqualTo(RuleKey.of("xoo", "R1"));
-    assertThat(activeRule.severity()).isEqualTo(Severity.CRITICAL);
+    ActiveRuleDto activeRule = activeRules.get(0);
+    assertThat(activeRule.getKey().ruleKey()).isEqualTo(RuleKey.of("xoo", "R1"));
+    assertThat(activeRule.getSeverityString()).isEqualTo(Severity.CRITICAL);
+
+    // Check in es
+    assertThat(tester.get(RuleIndex.class).searchAll(new RuleQuery().setQProfileKey(profileDto.getKey()).setActivation(true))).containsOnly(RuleKey.of("xoo", "R1"));
   }
 
   @Test
index 245e133f75a3e22ba3a5e3310a782618ccf8c8cc..ede524fe811be9adcd5a8b75ec2d7de1d989be99 100644 (file)
@@ -40,7 +40,9 @@ import org.sonar.server.exceptions.BadRequestException;
 import org.sonar.server.exceptions.NotFoundException;
 import org.sonar.server.qualityprofile.index.ActiveRuleIndex;
 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.MockUserSession;
 import org.sonar.server.tester.ServerTester;
 import org.sonar.server.tester.UserSessionRule;
@@ -224,7 +226,8 @@ public class QProfileFactoryMediumTest {
     assertThat(db.qualityProfileDao().selectAll(dbSession)).isEmpty();
     assertThat(db.activeRuleDao().selectAll(dbSession)).isEmpty();
     assertThat(db.activeRuleDao().selectAllParams(dbSession)).isEmpty();
-    assertThat(activeRuleIndex.findByProfile(XOO_P1_KEY)).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
@@ -251,9 +254,9 @@ public class QProfileFactoryMediumTest {
     assertThat(db.qualityProfileDao().selectAll(dbSession)).isEmpty();
     assertThat(db.activeRuleDao().selectAll(dbSession)).isEmpty();
     assertThat(db.activeRuleDao().selectAllParams(dbSession)).isEmpty();
-    assertThat(activeRuleIndex.findByProfile(XOO_P1_KEY)).isEmpty();
-    assertThat(activeRuleIndex.findByProfile(XOO_P2_KEY)).isEmpty();
-    assertThat(activeRuleIndex.findByProfile(XOO_P3_KEY)).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
index 10a537d8ee002f7e5a4304925d8a3eed05d97034..42fb2579417cbb443be347238e12a00b5f2bc8b1 100644 (file)
@@ -43,7 +43,6 @@ import org.sonar.db.qualityprofile.ActiveRuleParamDto;
 import org.sonar.db.qualityprofile.QualityProfileDao;
 import org.sonar.db.qualityprofile.QualityProfileDto;
 import org.sonar.server.platform.Platform;
-import org.sonar.server.qualityprofile.index.ActiveRuleIndex;
 import org.sonar.server.tester.ServerTester;
 import org.sonar.server.tester.UserSessionRule;
 
@@ -105,23 +104,22 @@ public class QProfileResetMediumTest {
     RulesProfile defProfile = RulesProfile.create("Basic", ServerTester.Xoo.KEY);
     defProfile.activateRule(
       org.sonar.api.rules.Rule.create("xoo", "x1").setParams(newArrayList(new RuleParam().setKey("acceptWhitespace"))),
-      RulePriority.CRITICAL
-    ).setParameter("acceptWhitespace", "true");
+      RulePriority.CRITICAL).setParameter("acceptWhitespace", "true");
 
     register(new Rules() {
-    @Override
-    public void init(RulesDefinition.NewRepository repository) {
-      RulesDefinition.NewRule x1 = repository.createRule("x1")
-        .setName("x1 name")
-        .setHtmlDescription("x1 desc")
-        .setSeverity(MINOR);
-      x1.createParam("acceptWhitespace")
-        .setDefaultValue("false")
-        .setType(RuleParamType.BOOLEAN)
-        .setDescription("Accept whitespaces on the line");
-    }},
-      defProfile
-    );
+      @Override
+      public void init(RulesDefinition.NewRepository repository) {
+        RulesDefinition.NewRule x1 = repository.createRule("x1")
+          .setName("x1 name")
+          .setHtmlDescription("x1 desc")
+          .setSeverity(MINOR);
+        x1.createParam("acceptWhitespace")
+          .setDefaultValue("false")
+          .setType(RuleParamType.BOOLEAN)
+          .setDescription("Accept whitespaces on the line");
+      }
+    },
+      defProfile);
 
     RuleKey ruleKey = RuleKey.of("xoo", "x1");
     QualityProfileDto profile = tester.get(QualityProfileDao.class).selectByNameAndLanguage("Basic", ServerTester.Xoo.KEY, dbSession);
@@ -131,11 +129,9 @@ public class QProfileResetMediumTest {
     tester.get(RuleActivator.class).activate(dbSession,
       new RuleActivation(ruleKey).setSeverity(BLOCKER)
         .setParameter("acceptWhitespace", "false"),
-      profile.getKey()
-    );
+      profile.getKey());
     dbSession.commit();
 
-
     // Verify severity and param has changed
     ActiveRuleDto activeRuleDto = tester.get(ActiveRuleDao.class).selectOrFailByKey(dbSession, activeRuleKey);
     assertThat(activeRuleDto.getSeverityString()).isEqualTo(BLOCKER);
@@ -149,8 +145,6 @@ public class QProfileResetMediumTest {
     // Severity and parameter value come back to origin after reset
     activeRuleDto = tester.get(ActiveRuleDao.class).selectOrFailByKey(dbSession, activeRuleKey);
     assertThat(activeRuleDto.getSeverityString()).isEqualTo(CRITICAL);
-    ActiveRule activeRule = tester.get(ActiveRuleIndex.class).getNullableByKey(activeRuleKey);
-    assertThat(activeRule.severity()).isEqualTo(CRITICAL);
 
     activeRuleParamDtos = tester.get(ActiveRuleDao.class).selectParamsByActiveRuleId(dbSession, activeRuleDto.getId());
     assertThat(activeRuleParamDtos.get(0).getKey()).isEqualTo("acceptWhitespace");
@@ -173,7 +167,8 @@ public class QProfileResetMediumTest {
           .setDefaultValue("false")
           .setType(RuleParamType.BOOLEAN)
           .setDescription("Accept whitespaces on the line");
-      }}, defProfile);
+      }
+    }, defProfile);
 
     QualityProfileDto profile = tester.get(QualityProfileDao.class).selectByNameAndLanguage("Basic", ServerTester.Xoo.KEY, dbSession);
     ActiveRuleKey activeRuleKey = ActiveRuleKey.of(profile.getKey(), RuleKey.of("xoo", "x1"));
@@ -190,7 +185,8 @@ public class QProfileResetMediumTest {
           .setDefaultValue("true")
           .setType(RuleParamType.BOOLEAN)
           .setDescription("Accept whitespaces on the line");
-      }}, defProfile);
+      }
+    }, defProfile);
 
     reset.resetLanguage(ServerTester.Xoo.KEY);
 
index 0d8cf59ff96be8ea9553755c32428e993bbad1be..9ac80b28c969eb34ad2fd678b54b364e46cd6f2e 100644 (file)
@@ -20,7 +20,6 @@
 package org.sonar.server.qualityprofile;
 
 import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.Lists;
 import com.google.common.collect.Multimap;
 import java.io.IOException;
 import java.io.Reader;
@@ -51,10 +50,11 @@ import org.sonar.db.user.UserDto;
 import org.sonar.server.activity.Activity;
 import org.sonar.server.activity.ActivityService;
 import org.sonar.server.es.SearchOptions;
-import org.sonar.server.qualityprofile.index.ActiveRuleDoc;
 import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
+import org.sonar.server.rule.index.RuleIndex;
 import org.sonar.server.rule.index.RuleIndexDefinition;
 import org.sonar.server.rule.index.RuleIndexer;
+import org.sonar.server.rule.index.RuleQuery;
 import org.sonar.server.search.FacetValue;
 import org.sonar.server.search.Result;
 import org.sonar.server.tester.ServerTester;
@@ -134,8 +134,8 @@ public class QProfileServiceMediumTest {
     assertThat(loader.getByKey(profile.getKey()).getLanguage()).isEqualTo("xoo");
     assertThat(loader.getByKey(profile.getKey()).getName()).isEqualTo("New Profile");
 
-    List<ActiveRuleDoc> activeRules = Lists.newArrayList(loader.findActiveRulesByProfile(profile.getKey()));
-    assertThat(activeRules).hasSize(1);
+    assertThat(db.activeRuleDao().selectByProfileKey(dbSession, profile.getKey())).hasSize(1);
+    assertThat(tester.get(RuleIndex.class).searchAll(new RuleQuery().setQProfileKey( profile.getKey()).setActivation(true))).hasSize(1);
   }
 
   @Test
@@ -229,8 +229,7 @@ public class QProfileServiceMediumTest {
     RuleKey ruleKey = RuleKey.of("xoo", "deleted_rule");
 
     tester.get(ActivityService.class).save(ActiveRuleChange.createFor(ActiveRuleChange.Type.UPDATED, ActiveRuleKey.of(XOO_P1_KEY, ruleKey))
-      .setParameter("max", "10").toActivity()
-      );
+      .setParameter("max", "10").toActivity());
 
     Result<QProfileActivity> activities = service.searchActivities(new QProfileActivityQuery(), new SearchOptions());
     assertThat(activities.getHits()).hasSize(1);
@@ -254,8 +253,7 @@ public class QProfileServiceMediumTest {
       ActiveRuleChange.createFor(ActiveRuleChange.Type.ACTIVATED, ActiveRuleKey.of(XOO_P1_KEY, RuleTesting.XOO_X1))
         .setSeverity(Severity.MAJOR)
         .setParameter("max", "10")
-        .toActivity()
-      );
+        .toActivity());
 
     Result<QProfileActivity> activities = service.searchActivities(new QProfileActivityQuery(), new SearchOptions());
     assertThat(activities.getHits()).hasSize(1);
@@ -274,8 +272,7 @@ public class QProfileServiceMediumTest {
     tester.get(ActivityService.class).save(ActiveRuleChange.createFor(ActiveRuleChange.Type.ACTIVATED, ActiveRuleKey.of(XOO_P1_KEY, ruleKey))
       .setSeverity(Severity.MAJOR)
       .setParameter("max", "10")
-      .toActivity()
-      );
+      .toActivity());
     dbSession.commit();
 
     Result<QProfileActivity> activities = service.searchActivities(new QProfileActivityQuery(), new SearchOptions());
index fc500642101cbbfbe89c03078417e81b3668b280..9d4fa754ed079f6a2c6b2bba97ccf17ea2514876 100644 (file)
@@ -20,7 +20,6 @@
 package org.sonar.server.qualityprofile;
 
 import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.Lists;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
@@ -45,7 +44,6 @@ import org.sonar.db.rule.RuleParamDto;
 import org.sonar.server.es.SearchOptions;
 import org.sonar.server.exceptions.BadRequestException;
 import org.sonar.server.exceptions.Message;
-import org.sonar.server.qualityprofile.index.ActiveRuleDoc;
 import org.sonar.server.qualityprofile.index.ActiveRuleIndex;
 import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
 import org.sonar.server.rule.index.RuleIndex;
@@ -55,6 +53,8 @@ import org.sonar.server.search.QueryContext;
 import org.sonar.server.tester.ServerTester;
 import org.sonar.server.tester.UserSessionRule;
 
+import static com.google.common.collect.Lists.newArrayList;
+import static java.util.Collections.singleton;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.junit.Assert.fail;
 import static org.sonar.api.rule.Severity.BLOCKER;
@@ -871,7 +871,7 @@ public class RuleActivatorMediumTest {
     // 2. assert that all activation has been commit to DB and ES
     dbSession.clearCache();
     assertThat(db.activeRuleDao().selectByProfileKey(dbSession, XOO_P1_KEY)).hasSize(bulkSize);
-    assertThat(activeRuleIndex.findByProfile(XOO_P1_KEY)).hasSize(bulkSize);
+    assertThat(db.activeRuleDao().selectByProfileKey(dbSession, XOO_P1_KEY)).hasSize(bulkSize);
     assertThat(result.countSucceeded()).isEqualTo(bulkSize);
     assertThat(result.countFailed()).isEqualTo(0);
   }
@@ -885,7 +885,7 @@ public class RuleActivatorMediumTest {
     // -> xoo rules x1, x2 and custom1
     dbSession.clearCache();
     assertThat(db.activeRuleDao().selectByProfileKey(dbSession, XOO_P1_KEY)).hasSize(3);
-    assertThat(activeRuleIndex.findByProfile(XOO_P1_KEY)).hasSize(3);
+    assertThat(db.activeRuleDao().selectByProfileKey(dbSession, XOO_P1_KEY)).hasSize(3);
     assertThat(result.countSucceeded()).isEqualTo(3);
     assertThat(result.countFailed()).isGreaterThan(0);
   }
@@ -1094,23 +1094,16 @@ public class RuleActivatorMediumTest {
     assertThat(found).as("Rule is not activated in db").isTrue();
   }
 
-  private void verifyHasActiveRuleInIndex(ActiveRuleKey activeRuleKey, String expectedSeverity,
-    @Nullable String expectedInheritance) {
+  private void verifyHasActiveRuleInIndex(ActiveRuleKey activeRuleKey, String expectedSeverity, @Nullable String expectedInheritance) {
     // verify es
-    List<ActiveRuleDoc> activeRules = Lists.newArrayList(activeRuleIndex.findByProfile(activeRuleKey.qProfile()));
-    boolean found = false;
-    for (ActiveRuleDoc activeRule : activeRules) {
-      if (activeRule.key().equals(activeRuleKey)) {
-        found = true;
-        assertThat(activeRule.severity()).isEqualTo(expectedSeverity);
-        assertThat(activeRule.inheritance()).isEqualTo(expectedInheritance == null ? ActiveRule.Inheritance.NONE : ActiveRule.Inheritance.valueOf(expectedInheritance));
-
-        // Dates should be set
-        assertThat(activeRule.createdAt()).isNotNull();
-        assertThat(activeRule.updatedAt()).isNotNull();
-      }
-    }
-    assertThat(found).as("Rule is not activated in index").isTrue();
+    List<RuleKey> ruleKeys = newArrayList(tester.get(RuleIndex.class).searchAll(
+      new RuleQuery()
+        .setKey(activeRuleKey.ruleKey().toString())
+        .setQProfileKey(activeRuleKey.qProfile())
+        .setActivation(true)
+        .setInheritance(singleton(expectedInheritance == null ? ActiveRule.Inheritance.NONE.name() : ActiveRule.Inheritance.valueOf(expectedInheritance).name()))
+        .setActiveSeverities(singleton(expectedSeverity))));
+    assertThat(ruleKeys).as("Rule is not activated in index").hasSize(1);
   }
 
   private void verifyHasActiveRuleInDbAndIndex(ActiveRuleKey activeRuleKey, String expectedSeverity,
@@ -1140,7 +1133,9 @@ public class RuleActivatorMediumTest {
     assertThat(activeRuleDtos).isEmpty();
 
     // verify es
-    List<ActiveRuleDoc> activeRules = Lists.newArrayList(activeRuleIndex.findByProfile(key));
-    assertThat(activeRules).isEmpty();
+    assertThat(tester.get(RuleIndex.class).searchAll(
+      new RuleQuery()
+        .setQProfileKey(key)
+        .setActivation(true))).isEmpty();
   }
 }
index 2f47190f8a9e83ae287dd84d9bc7fed7a8c612a6..a74b001aafe32266f92065cb11e17e6420269c85 100644 (file)
@@ -20,7 +20,6 @@
 package org.sonar.server.qualityprofile.index;
 
 import com.google.common.collect.ImmutableList;
-import com.google.common.collect.Lists;
 import com.google.common.collect.Multimap;
 import java.util.ArrayList;
 import java.util.List;
@@ -30,11 +29,9 @@ import org.junit.ClassRule;
 import org.junit.Test;
 import org.sonar.api.config.Settings;
 import org.sonar.api.rule.RuleKey;
-import org.sonar.api.rule.RuleStatus;
 import org.sonar.db.qualityprofile.ActiveRuleKey;
 import org.sonar.db.rule.RuleTesting;
 import org.sonar.server.es.EsTester;
-import org.sonar.server.qualityprofile.ActiveRule;
 import org.sonar.server.rule.index.RuleDoc;
 import org.sonar.server.rule.index.RuleDocTesting;
 import org.sonar.server.rule.index.RuleIndexDefinition;
@@ -141,92 +138,6 @@ public class ActiveRuleIndexTest {
     assertThat(stats).hasSize(30);
   }
 
-  @Test
-  public void get_by_key() {
-    indexRules(RuleDocTesting.newDoc(RULE_KEY_1));
-    ActiveRuleKey key = ActiveRuleKey.of(QUALITY_PROFILE_KEY1, RULE_KEY_1);
-    indexActiveRules(ActiveRuleDocTesting.newDoc(key));
-
-    assertThat(index.getNullableByKey(key)).isNotNull();
-    assertThat(index.getNullableByKey(ActiveRuleKey.of(QUALITY_PROFILE_KEY1, RULE_KEY_2))).isNull();
-  }
-
-  @Test
-  public void find_active_rules() {
-    indexRules(
-      RuleDocTesting.newDoc(RULE_KEY_1),
-      RuleDocTesting.newDoc(RULE_KEY_2),
-      RuleDocTesting.newDoc(RuleKey.of("xoo", "removed")).setStatus(RuleStatus.REMOVED.name()));
-
-    indexActiveRules(
-      ActiveRuleDocTesting.newDoc(ActiveRuleKey.of(QUALITY_PROFILE_KEY1, RULE_KEY_1)),
-      ActiveRuleDocTesting.newDoc(ActiveRuleKey.of(QUALITY_PROFILE_KEY1, RULE_KEY_2)),
-      ActiveRuleDocTesting.newDoc(ActiveRuleKey.of(QUALITY_PROFILE_KEY2, RULE_KEY_2)),
-      // 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.
-      ActiveRuleDocTesting.newDoc(ActiveRuleKey.of(QUALITY_PROFILE_KEY2, RuleKey.of("xoo", "removed"))));
-
-    // 1. find by rule key
-
-    // in es
-    List<ActiveRule> activeRules = index.findByRule(RULE_KEY_1);
-    assertThat(activeRules).hasSize(1);
-    assertThat(activeRules.get(0).key().ruleKey()).isEqualTo(RULE_KEY_1);
-
-    activeRules = index.findByRule(RULE_KEY_2);
-    assertThat(activeRules).hasSize(2);
-    assertThat(activeRules.get(0).key().ruleKey()).isEqualTo(RULE_KEY_2);
-
-    activeRules = index.findByRule(RuleKey.of("unknown", "unknown"));
-    assertThat(activeRules).isEmpty();
-
-    // 2. find by profile
-    List<ActiveRuleDoc> activeRuleDocs = Lists.newArrayList(index.findByProfile(QUALITY_PROFILE_KEY1));
-    assertThat(activeRuleDocs).hasSize(2);
-    assertThat(activeRuleDocs.get(0).key().qProfile()).isEqualTo(QUALITY_PROFILE_KEY1);
-    assertThat(activeRuleDocs.get(1).key().qProfile()).isEqualTo(QUALITY_PROFILE_KEY1);
-
-    activeRuleDocs = Lists.newArrayList(index.findByProfile(QUALITY_PROFILE_KEY2));
-    assertThat(activeRuleDocs).hasSize(1);
-    assertThat(activeRuleDocs.get(0).key().qProfile()).isEqualTo(QUALITY_PROFILE_KEY2);
-
-    activeRuleDocs = Lists.newArrayList(index.findByProfile("unknown"));
-    assertThat(activeRuleDocs).isEmpty();
-  }
-
-  @Test
-  public void find_many_active_rules_by_profile() {
-    int nb = 150;
-    RuleDoc[] ruleDocs = new RuleDoc[nb];
-    ActiveRuleDoc[] activeRuleDocs = new ActiveRuleDoc[nb];
-    for (int i = 0; i < nb; i++) {
-      RuleKey ruleKey = RuleKey.of("xoo", "S00" + i);
-      ruleDocs[i] = RuleDocTesting.newDoc(ruleKey);
-      activeRuleDocs[i] = ActiveRuleDocTesting.newDoc(ActiveRuleKey.of(QUALITY_PROFILE_KEY1, ruleKey));
-    }
-    indexRules(ruleDocs);
-    indexActiveRules(activeRuleDocs);
-
-    // verify index
-    assertThat(index.findByProfile(QUALITY_PROFILE_KEY1)).hasSize(nb);
-  }
-
-  @Test
-  public void find_many_active_rules_by_rule() {
-    indexRules(RuleDocTesting.newDoc(RULE_KEY_1));
-
-    int nb = 150;
-    ActiveRuleDoc[] activeRuleDocs = new ActiveRuleDoc[nb];
-    for (int i = 0; i < nb; i++) {
-      activeRuleDocs[i] = ActiveRuleDocTesting.newDoc(ActiveRuleKey.of("qp" + i, RULE_KEY_1));
-    }
-    indexActiveRules(activeRuleDocs);
-
-    // verify index
-    assertThat(index.findByRule(RULE_KEY_1)).hasSize(nb);
-  }
-
   private void indexActiveRules(ActiveRuleDoc... docs) {
     activeRuleIndexer.index(asList(docs).iterator());
   }
index 5d56c0ed40f2bb30bc7fbeb136a3e234963e67e1..7d02c4524b18585ca1b6bfaf151982a15ef0cb87 100644 (file)
@@ -19,7 +19,6 @@
  */
 package org.sonar.server.qualityprofile.ws;
 
-import com.google.common.collect.Sets;
 import java.io.IOException;
 import java.io.Writer;
 import org.apache.commons.lang.StringUtils;
@@ -45,8 +44,6 @@ import org.sonar.server.qualityprofile.QProfileExporters;
 import org.sonar.server.qualityprofile.QProfileFactory;
 import org.sonar.server.qualityprofile.QProfileLoader;
 import org.sonar.server.qualityprofile.QProfileTesting;
-import org.sonar.server.qualityprofile.index.ActiveRuleDoc;
-import org.sonar.server.qualityprofile.index.ActiveRuleIndex;
 import org.sonar.server.rule.index.RuleIndex;
 import org.sonar.server.ws.WsTester;
 import org.sonar.server.ws.WsTester.Result;
@@ -54,7 +51,6 @@ import org.sonar.server.ws.WsTester.Result;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.mockito.Mockito.doAnswer;
 import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
 
 public class ExportActionTest {
 
@@ -82,10 +78,7 @@ public class ExportActionTest {
     ProfileExporter exporter1 = newExporter("polop");
     ProfileExporter exporter2 = newExporter("palap");
 
-    ActiveRuleIndex activeRuleIndex = mock(ActiveRuleIndex.class);
-    when(activeRuleIndex.findByProfile(Matchers.anyString())).thenReturn(Sets.<ActiveRuleDoc>newHashSet().iterator());
-
-    exporters = new QProfileExporters(dbClient, new QProfileLoader(dbClient, activeRuleIndex, mock(RuleIndex.class)), null, null, new ProfileExporter[] {exporter1, exporter2},
+    exporters = new QProfileExporters(dbClient, new QProfileLoader(dbClient, null, mock(RuleIndex.class)), null, null, new ProfileExporter[] {exporter1, exporter2},
       null);
     wsTester = new WsTester(new QProfilesWs(mock(RuleActivationActions.class),
       mock(BulkRuleActivationActions.class),
index 975d3fc9f80ed07c07286da982037bbdf62c7ee4..5a59174436993343588b5bebb5f50b334deed7fe 100644 (file)
@@ -20,6 +20,7 @@
 package org.sonar.server.qualityprofile.ws;
 
 import com.google.common.collect.ImmutableSet;
+import java.util.Collections;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.ClassRule;
@@ -33,6 +34,7 @@ import org.sonar.core.permission.GlobalPermissions;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.component.ComponentDto;
+import org.sonar.db.qualityprofile.ActiveRuleDao;
 import org.sonar.db.qualityprofile.ActiveRuleDto;
 import org.sonar.db.qualityprofile.ActiveRuleKey;
 import org.sonar.db.qualityprofile.QualityProfileDto;
@@ -44,7 +46,6 @@ import org.sonar.server.exceptions.NotFoundException;
 import org.sonar.server.qualityprofile.QProfileFactory;
 import org.sonar.server.qualityprofile.QProfileName;
 import org.sonar.server.qualityprofile.QProfileTesting;
-import org.sonar.server.qualityprofile.index.ActiveRuleIndex;
 import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
 import org.sonar.server.rule.index.RuleIndex;
 import org.sonar.server.rule.index.RuleIndexer;
@@ -360,7 +361,6 @@ public class QProfilesWsMediumTest {
 
     // 0. Assert No Active Rule for profile
     assertThat(db.activeRuleDao().selectByProfileKey(session, profile.getKey())).isEmpty();
-    assertThat(db.activeRuleDao().selectByProfileKey(session, profile.getKey())).hasSize(0);
 
     // 2. Assert ActiveRule with BLOCKER severity
     assertThat(tester.get(RuleIndex.class).search(
@@ -375,7 +375,12 @@ public class QProfilesWsMediumTest {
     session.commit();
 
     // 2. Assert ActiveRule with MINOR severity
-    assertThat(tester.get(ActiveRuleIndex.class).findByRule(rule0.getKey()).get(0).severity()).isEqualTo("MINOR");
+    assertThat(tester.get(ActiveRuleDao.class).selectByRule(session, rule0).get(0).getSeverityString()).isEqualTo("MINOR");
+    assertThat(tester.get(RuleIndex.class).searchAll(new RuleQuery()
+      .setQProfileKey(profile.getKey())
+      .setKey(rule0.getKey().toString())
+      .setActiveSeverities(Collections.singleton("MINOR"))
+      .setActivation(true))).hasSize(1);
   }
 
   @Test
index 7e9ef210735c30d05938e46a9c633c35445f2211..d9cb10cf025e4292f260e6251c70e756baf6ef33 100644 (file)
@@ -19,8 +19,6 @@
  */
 package org.sonar.server.rule;
 
-import com.google.common.collect.Lists;
-import java.util.List;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.ClassRule;
@@ -34,12 +32,9 @@ import org.sonar.db.qualityprofile.QualityProfileDto;
 import org.sonar.db.rule.RuleDao;
 import org.sonar.db.rule.RuleDto;
 import org.sonar.db.rule.RuleTesting;
-import org.sonar.server.es.SearchOptions;
 import org.sonar.server.qualityprofile.QProfileTesting;
 import org.sonar.server.qualityprofile.RuleActivation;
 import org.sonar.server.qualityprofile.RuleActivator;
-import org.sonar.server.qualityprofile.index.ActiveRuleDoc;
-import org.sonar.server.qualityprofile.index.ActiveRuleIndex;
 import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
 import org.sonar.server.rule.index.RuleIndex;
 import org.sonar.server.rule.index.RuleIndexer;
@@ -117,11 +112,10 @@ public class RuleDeleterMediumTest {
     assertThat(customRuleReloaded.getUpdatedAt()).isNotEqualTo(PAST);
 
     // Verify there's no more active rule from custom rule
-    List<ActiveRuleDoc> activeRules = Lists.newArrayList(tester.get(ActiveRuleIndex.class).findByProfile(profileDto.getKey()));
-    assertThat(activeRules).isEmpty();
+    assertThat(index.searchAll(new RuleQuery().setQProfileKey(profileDto.getKey()).setActivation(true))).isEmpty();
 
     // Verify in index
-    assertThat(index.search(new RuleQuery(), new SearchOptions()).getIds()).containsOnly(templateRule.getKey());
+    assertThat(index.searchAll(new RuleQuery())).containsOnly(templateRule.getKey());
   }
 
   @Test