]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7330 QProfileLoader is now using new ActiveRuleIndex
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 23 Feb 2016 19:55:17 +0000 (20:55 +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/QProfileComparison.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileExporters.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileLoader.java
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileBackuperMediumTest.java
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileExportersTest.java
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileServiceMediumTest.java
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/ExportActionTest.java

index 0deacd4f6693cfeb3a4ec8e029014ae5291c90d3..5fada8ad564dd42f6c093fd8065c60e4bff2db6c 100644 (file)
@@ -23,16 +23,16 @@ import com.google.common.base.Preconditions;
 import com.google.common.collect.MapDifference;
 import com.google.common.collect.Maps;
 import com.google.common.collect.Sets;
-import org.sonar.api.server.ServerSide;
-import org.sonar.api.rule.RuleKey;
-import org.sonar.db.DbSession;
-import org.sonar.db.qualityprofile.QualityProfileDto;
-import org.sonar.core.util.NonNullInputFunction;
-import org.sonar.db.DbClient;
-
 import java.util.Collection;
 import java.util.Map;
 import java.util.Set;
+import org.sonar.api.rule.RuleKey;
+import org.sonar.api.server.ServerSide;
+import org.sonar.core.util.NonNullInputFunction;
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
+import org.sonar.db.qualityprofile.QualityProfileDto;
+import org.sonar.server.qualityprofile.index.ActiveRuleDoc;
 
 @ServerSide
 public class QProfileComparison {
@@ -66,8 +66,8 @@ public class QProfileComparison {
     result.right = dbClient.qualityProfileDao().selectByKey(session, rightKey);
     Preconditions.checkArgument(result.right != null, String.format("Could not find right profile '%s'", leftKey));
 
-    Map<RuleKey, ActiveRule> leftActiveRulesByRuleKey = loadActiveRules(leftKey);
-    Map<RuleKey, ActiveRule> rightActiveRulesByRuleKey = loadActiveRules(rightKey);
+    Map<RuleKey, ActiveRuleDoc> leftActiveRulesByRuleKey = loadActiveRules(leftKey);
+    Map<RuleKey, ActiveRuleDoc> rightActiveRulesByRuleKey = loadActiveRules(rightKey);
 
     Set<RuleKey> allRules = Sets.newHashSet();
     allRules.addAll(leftActiveRulesByRuleKey.keySet());
@@ -99,10 +99,10 @@ public class QProfileComparison {
     }
   }
 
-  private Map<RuleKey, ActiveRule> loadActiveRules(String profileKey) {
-    return Maps.uniqueIndex(profileLoader.findActiveRulesByProfile(profileKey), new NonNullInputFunction<ActiveRule, RuleKey>() {
+  private Map<RuleKey, ActiveRuleDoc> loadActiveRules(String profileKey) {
+    return Maps.uniqueIndex(profileLoader.findActiveRulesByProfile(profileKey), new NonNullInputFunction<ActiveRuleDoc, RuleKey>() {
       @Override
-      protected RuleKey doApply(ActiveRule input) {
+      protected RuleKey doApply(ActiveRuleDoc input) {
         return input.key().ruleKey();
       }
     });
index d154f217697fcf301aac2ca50396b87410937ef0..192349dba9a4d6f0764933a673ac3931466d6542 100644 (file)
@@ -45,6 +45,7 @@ import org.sonar.db.DbSession;
 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;
 
 @ServerSide
 public class QProfileExporters {
@@ -119,7 +120,7 @@ public class QProfileExporters {
 
   private RulesProfile wrap(QualityProfileDto profile) {
     RulesProfile target = new RulesProfile(profile.getName(), profile.getLanguage());
-    for (Iterator<ActiveRule> activeRuleIterator = loader.findActiveRulesByProfile(profile.getKey()); activeRuleIterator.hasNext();) {
+    for (Iterator<ActiveRuleDoc> activeRuleIterator = loader.findActiveRulesByProfile(profile.getKey()); activeRuleIterator.hasNext();) {
       ActiveRule activeRule = activeRuleIterator.next();
       Rule rule = ruleFinder.findByKey(activeRule.key().ruleKey());
       org.sonar.api.rules.ActiveRule wrappedActiveRule = target.activateRule(rule, RulePriority.valueOf(activeRule.severity()));
index 7d1b475e92616fa0100e3fc0730f2c7c30c1b0db..36343f1410c32e87b6cc985956beb7f92ce4928b 100644 (file)
@@ -33,25 +33,24 @@ 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.qualityprofile.index.ActiveRuleIndex;
-import org.sonar.server.rule.index.RuleIndex;
+import org.sonar.server.es.SearchOptions;
+import org.sonar.server.qualityprofile.index.ActiveRuleDoc;
+import org.sonar.server.qualityprofile.index.ActiveRuleIndex2;
+import org.sonar.server.rule.index.RuleIndex2;
 import org.sonar.server.rule.index.RuleQuery;
 import org.sonar.server.search.FacetValue;
-import org.sonar.server.search.IndexClient;
-import org.sonar.server.search.QueryContext;
-import org.sonar.server.user.UserSession;
 
 @ServerSide
 public class QProfileLoader {
 
   private final DbClient dbClient;
-  private final IndexClient index;
-  private final UserSession userSession;
+  private final ActiveRuleIndex2 activeRuleIndex;
+  private final RuleIndex2 ruleIndex;
 
-  public QProfileLoader(DbClient dbClient, IndexClient index, UserSession userSession) {
+  public QProfileLoader(DbClient dbClient, ActiveRuleIndex2 activeRuleIndex, RuleIndex2 ruleIndex) {
     this.dbClient = dbClient;
-    this.index = index;
-    this.userSession = userSession;
+    this.activeRuleIndex = activeRuleIndex;
+    this.ruleIndex = ruleIndex;
   }
 
   /**
@@ -89,20 +88,20 @@ public class QProfileLoader {
 
   @CheckForNull
   public ActiveRule getActiveRule(ActiveRuleKey key) {
-    return index.get(ActiveRuleIndex.class).getNullableByKey(key);
+    return activeRuleIndex.getNullableByKey(key);
   }
 
   public List<ActiveRule> findActiveRulesByRule(RuleKey key) {
-    return index.get(ActiveRuleIndex.class).findByRule(key);
+    return activeRuleIndex.findByRule(key);
   }
 
-  public Iterator<ActiveRule> findActiveRulesByProfile(String key) {
-    return index.get(ActiveRuleIndex.class).findByProfile(key);
+  public Iterator<ActiveRuleDoc> findActiveRulesByProfile(String key) {
+    return activeRuleIndex.findByProfile(key);
   }
 
   public Map<String, Long> countAllActiveRules() {
     Map<String, Long> counts = new HashMap<>();
-    for (Map.Entry<String, Long> entry : index.get(ActiveRuleIndex.class).countAllByQualityProfileKey().entrySet()) {
+    for (Map.Entry<String, Long> entry : activeRuleIndex.countAllByQualityProfileKey().entrySet()) {
       counts.put(entry.getKey(), entry.getValue());
     }
     return counts;
@@ -113,16 +112,16 @@ public class QProfileLoader {
     for (QualityProfileDto profile : this.findAll()) {
       keys.add(profile.getKey());
     }
-    return index.get(ActiveRuleIndex.class).getStatsByProfileKeys(keys);
+    return activeRuleIndex.getStatsByProfileKeys(keys);
   }
 
   public long countDeprecatedActiveRulesByProfile(String key) {
-    return index.get(RuleIndex.class).search(
+    return ruleIndex.search(
       new RuleQuery()
         .setQProfileKey(key)
         .setActivation(true)
         .setStatuses(Lists.newArrayList(RuleStatus.DEPRECATED)),
-      new QueryContext(userSession).setLimit(0)).getTotal();
+      new SearchOptions().setLimit(0)).getTotal();
   }
 
 }
index 1adb893bb524e5fc68a3e6c713a6e6be66583172..1a25af93adf8b1ca8104619a4402faa792685fb0 100644 (file)
@@ -44,6 +44,7 @@ import org.sonar.db.rule.RuleDto;
 import org.sonar.db.rule.RuleParamDto;
 import org.sonar.db.rule.RuleTesting;
 import org.sonar.server.db.DbClient;
+import org.sonar.server.qualityprofile.index.ActiveRuleDoc;
 import org.sonar.server.tester.ServerTester;
 import org.sonar.server.tester.UserSessionRule;
 
@@ -133,7 +134,7 @@ public class QProfileBackuperMediumTest {
     QualityProfileDto profile = db.qualityProfileDao().selectByNameAndLanguage("P1", "xoo", dbSession);
     assertThat(profile).isNotNull();
 
-    List<ActiveRule> activeRules = Lists.newArrayList(tester.get(QProfileLoader.class).findActiveRulesByProfile(profile.getKey()));
+    List<ActiveRuleDoc> activeRules = Lists.newArrayList(tester.get(QProfileLoader.class).findActiveRulesByProfile(profile.getKey()));
     assertThat(activeRules).hasSize(1);
     assertThat(activeRules.get(0).severity()).isEqualTo("BLOCKER");
     assertThat(activeRules.get(0).inheritance()).isEqualTo(ActiveRule.Inheritance.NONE);
@@ -160,7 +161,7 @@ public class QProfileBackuperMediumTest {
     tester.get(QProfileBackuper.class).restore(new StringReader(
       Resources.toString(getClass().getResource("QProfileBackuperMediumTest/restore.xml"), StandardCharsets.UTF_8)), null);
 
-    List<ActiveRule> activeRules = Lists.newArrayList(tester.get(QProfileLoader.class).findActiveRulesByProfile(QProfileTesting.XOO_P1_KEY));
+    List<ActiveRuleDoc> activeRules = Lists.newArrayList(tester.get(QProfileLoader.class).findActiveRulesByProfile(QProfileTesting.XOO_P1_KEY));
     assertThat(activeRules).hasSize(1);
     assertThat(activeRules.get(0).severity()).isEqualTo("BLOCKER");
     assertThat(activeRules.get(0).inheritance()).isEqualTo(ActiveRule.Inheritance.NONE);
@@ -188,7 +189,7 @@ public class QProfileBackuperMediumTest {
       Resources.toString(getClass().getResource("QProfileBackuperMediumTest/restore-child.xml"), StandardCharsets.UTF_8)), null);
 
     // parent profile is unchanged
-    List<ActiveRule> activeRules = Lists.newArrayList(tester.get(QProfileLoader.class).findActiveRulesByProfile(QProfileTesting.XOO_P1_KEY));
+    List<ActiveRuleDoc> activeRules = Lists.newArrayList(tester.get(QProfileLoader.class).findActiveRulesByProfile(QProfileTesting.XOO_P1_KEY));
     assertThat(activeRules).hasSize(1);
     assertThat(activeRules.get(0).severity()).isEqualTo("INFO");
     assertThat(activeRules.get(0).inheritance()).isEqualTo(ActiveRule.Inheritance.NONE);
@@ -223,7 +224,7 @@ public class QProfileBackuperMediumTest {
       Resources.toString(getClass().getResource("QProfileBackuperMediumTest/restore-parent.xml"), StandardCharsets.UTF_8)), null);
 
     // parent profile is updated
-    List<ActiveRule> activeRules = Lists.newArrayList(tester.get(QProfileLoader.class).findActiveRulesByProfile(QProfileTesting.XOO_P1_KEY));
+    List<ActiveRuleDoc> activeRules = Lists.newArrayList(tester.get(QProfileLoader.class).findActiveRulesByProfile(QProfileTesting.XOO_P1_KEY));
     assertThat(activeRules).hasSize(1);
     assertThat(activeRules.get(0).severity()).isEqualTo("BLOCKER");
     assertThat(activeRules.get(0).inheritance()).isEqualTo(ActiveRule.Inheritance.NONE);
@@ -258,7 +259,7 @@ public class QProfileBackuperMediumTest {
       Resources.toString(getClass().getResource("QProfileBackuperMediumTest/keep_other_inherited_rules.xml"), StandardCharsets.UTF_8)), QProfileTesting.XOO_P2_NAME);
 
     // x1 and x2
-    List<ActiveRule> activeRules = Lists.newArrayList(tester.get(QProfileLoader.class).findActiveRulesByProfile(QProfileTesting.XOO_P2_KEY));
+    List<ActiveRuleDoc> activeRules = Lists.newArrayList(tester.get(QProfileLoader.class).findActiveRulesByProfile(QProfileTesting.XOO_P2_KEY));
     assertThat(activeRules).hasSize(2);
   }
 
@@ -302,7 +303,7 @@ public class QProfileBackuperMediumTest {
       Resources.toString(getClass().getResource("QProfileBackuperMediumTest/restore.xml"), StandardCharsets.UTF_8)),
       QProfileTesting.XOO_P3_NAME);
 
-    List<ActiveRule> activeRules = Lists.newArrayList(tester.get(QProfileLoader.class).findActiveRulesByProfile(QProfileTesting.XOO_P1_KEY));
+    List<ActiveRuleDoc> activeRules = Lists.newArrayList(tester.get(QProfileLoader.class).findActiveRulesByProfile(QProfileTesting.XOO_P1_KEY));
     assertThat(activeRules).hasSize(0);
 
     QualityProfileDto target = db.qualityProfileDao().selectByNameAndLanguage("P3", "xoo", dbSession);
index f0d3972bbd95fedd1a878fe968637bb05c46add6..35ef9aba34cd121c35fb1063b47957cef425b21d 100644 (file)
 package org.sonar.server.qualityprofile;
 
 import com.google.common.collect.Lists;
+import java.io.IOException;
+import java.io.Reader;
+import java.io.Writer;
+import java.util.List;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.ClassRule;
@@ -40,12 +44,8 @@ import org.sonar.db.qualityprofile.QualityProfileDto;
 import org.sonar.server.db.DbClient;
 import org.sonar.server.exceptions.BadRequestException;
 import org.sonar.server.exceptions.NotFoundException;
+import org.sonar.server.qualityprofile.index.ActiveRuleDoc;
 import org.sonar.server.tester.ServerTester;
-
-import java.io.IOException;
-import java.io.Reader;
-import java.io.Writer;
-import java.util.List;
 import org.sonar.server.tester.UserSessionRule;
 
 import static org.assertj.core.api.Assertions.assertThat;
@@ -138,7 +138,7 @@ public class QProfileExportersTest {
     exporters.importXml(profileDto, "XooProfileImporter", "<xml/>", dbSession);
     dbSession.commit();
 
-    List<ActiveRule> activeRules = Lists.newArrayList(loader.findActiveRulesByProfile(profileDto.getKey()));
+    List<ActiveRuleDoc> activeRules = Lists.newArrayList(loader.findActiveRulesByProfile(profileDto.getKey()));
     assertThat(activeRules).hasSize(1);
     ActiveRule activeRule = activeRules.get(0);
     assertThat(activeRule.key().ruleKey()).isEqualTo(RuleKey.of("xoo", "R1"));
index aca431d0b8466d8efe41ffaad9d4f78ecc769c86..dbb703896421fe04beb2ea4592788317d89d743a 100644 (file)
@@ -45,13 +45,14 @@ import org.sonar.db.DbSession;
 import org.sonar.db.qualityprofile.ActiveRuleKey;
 import org.sonar.db.qualityprofile.QualityProfileDto;
 import org.sonar.db.rule.RuleDto;
+import org.sonar.db.rule.RuleTesting;
 import org.sonar.db.user.UserDto;
 import org.sonar.server.activity.Activity;
 import org.sonar.server.activity.ActivityService;
 import org.sonar.server.db.DbClient;
 import org.sonar.server.es.SearchOptions;
+import org.sonar.server.qualityprofile.index.ActiveRuleDoc;
 import org.sonar.server.qualityprofile.index.ActiveRuleNormalizer;
-import org.sonar.db.rule.RuleTesting;
 import org.sonar.server.search.FacetValue;
 import org.sonar.server.search.Result;
 import org.sonar.server.tester.ServerTester;
@@ -123,7 +124,7 @@ public class QProfileServiceMediumTest {
     assertThat(loader.getByKey(profile.getKey()).getLanguage()).isEqualTo("xoo");
     assertThat(loader.getByKey(profile.getKey()).getName()).isEqualTo("New Profile");
 
-    List<ActiveRule> activeRules = Lists.newArrayList(loader.findActiveRulesByProfile(profile.getKey()));
+    List<ActiveRuleDoc> activeRules = Lists.newArrayList(loader.findActiveRulesByProfile(profile.getKey()));
     assertThat(activeRules).hasSize(1);
   }
 
index 7123e6da4355abbbd18728b48a55530e73845323..d2cbc287147951b6b66ee7c40f6f386f5f1598d8 100644 (file)
@@ -41,15 +41,14 @@ import org.sonar.db.qualityprofile.QualityProfileDto;
 import org.sonar.server.db.DbClient;
 import org.sonar.server.exceptions.NotFoundException;
 import org.sonar.server.language.LanguageTesting;
-import org.sonar.server.qualityprofile.ActiveRule;
 import org.sonar.server.qualityprofile.QProfileBackuper;
 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.ActiveRuleIndex;
-import org.sonar.server.search.IndexClient;
-import org.sonar.server.tester.UserSessionRule;
+import org.sonar.server.qualityprofile.index.ActiveRuleDoc;
+import org.sonar.server.qualityprofile.index.ActiveRuleIndex2;
+import org.sonar.server.rule.index.RuleIndex2;
 import org.sonar.server.ws.WsTester;
 import org.sonar.server.ws.WsTester.Result;
 
@@ -62,8 +61,6 @@ public class ExportActionTest {
 
   @Rule
   public DbTester db = DbTester.create(System2.INSTANCE);
-  @Rule
-  public UserSessionRule userSessionRule = UserSessionRule.standalone();
 
   WsTester wsTester;
 
@@ -89,12 +86,10 @@ public class ExportActionTest {
     ProfileExporter exporter1 = newExporter("polop");
     ProfileExporter exporter2 = newExporter("palap");
 
-    IndexClient indexClient = mock(IndexClient.class);
-    ActiveRuleIndex activeRuleIndex = mock(ActiveRuleIndex.class);
-    when(activeRuleIndex.findByProfile(Matchers.anyString())).thenReturn(Sets.<ActiveRule>newHashSet().iterator());
+    ActiveRuleIndex2 activeRuleIndex = mock(ActiveRuleIndex2.class);
+    when(activeRuleIndex.findByProfile(Matchers.anyString())).thenReturn(Sets.<ActiveRuleDoc>newHashSet().iterator());
 
-    when(indexClient.get(ActiveRuleIndex.class)).thenReturn(activeRuleIndex);
-    exporters = new QProfileExporters(new QProfileLoader(dbClient, indexClient, userSessionRule), null, null, new ProfileExporter[] {exporter1, exporter2}, null);
+    exporters = new QProfileExporters(new QProfileLoader(dbClient, activeRuleIndex, mock(RuleIndex2.class)), null, null, new ProfileExporter[] {exporter1, exporter2}, null);
     wsTester = new WsTester(new QProfilesWs(mock(RuleActivationActions.class),
       mock(BulkRuleActivationActions.class),
       mock(ProjectAssociationActions.class),