]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7330 QProfileExporters is now using ActiveRuleIndexer
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 25 Feb 2016 10:16:21 +0000 (11:16 +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/QProfileExporters.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileResult.java
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileExportersTest.java

index 192349dba9a4d6f0764933a673ac3931466d6542..3f30e0f3febc2cb184e25efc13ed0bc710c40cd3 100644 (file)
@@ -166,15 +166,18 @@ public class QProfileExporters {
     ValidationMessages messages = ValidationMessages.create();
     ProfileImporter importer = getProfileImporter(importerKey);
     RulesProfile rulesProfile = importer.importProfile(xml, messages);
-    importProfile(profileDto, rulesProfile, dbSession);
+    List<ActiveRuleChange> changes = importProfile(profileDto, rulesProfile, dbSession);
+    result.addChanges(changes);
     processValidationMessages(messages, result);
     return result;
   }
 
-  private void importProfile(QualityProfileDto profileDto, RulesProfile rulesProfile, DbSession dbSession) {
+  private List<ActiveRuleChange> importProfile(QualityProfileDto profileDto, RulesProfile rulesProfile, DbSession dbSession) {
+    List<ActiveRuleChange> changes = new ArrayList<>();
     for (org.sonar.api.rules.ActiveRule activeRule : rulesProfile.getActiveRules()) {
-      ruleActivator.activate(dbSession, toRuleActivation(activeRule), profileDto);
+      changes.addAll(ruleActivator.activate(dbSession, toRuleActivation(activeRule), profileDto));
     }
+    return changes;
   }
 
   private ProfileImporter getProfileImporter(String importerKey) {
index 34b90e34e7552fbbf54672f3db1ca919420249c0..24d0b23caf71e260704db159baec055aff247be5 100644 (file)
  */
 package org.sonar.server.qualityprofile;
 
-import org.sonar.db.qualityprofile.QualityProfileDto;
-
+import java.util.ArrayList;
 import java.util.List;
-
-import static com.google.common.collect.Lists.newArrayList;
+import org.sonar.db.qualityprofile.QualityProfileDto;
 
 public class QProfileResult {
 
@@ -32,9 +30,12 @@ public class QProfileResult {
 
   private QualityProfileDto profile;
 
+  private List<ActiveRuleChange> changes;
+
   public QProfileResult() {
-    warnings = newArrayList();
-    infos = newArrayList();
+    warnings = new ArrayList<>();
+    infos = new ArrayList<>();
+    changes = new ArrayList<>();
   }
 
   public List<String> warnings() {
@@ -64,9 +65,19 @@ public class QProfileResult {
     return this;
   }
 
+  public List<ActiveRuleChange> getChanges() {
+    return changes;
+  }
+
+  public QProfileResult addChanges(List<ActiveRuleChange> changes) {
+    this.changes.addAll(changes);
+    return this;
+  }
+
   public QProfileResult add(QProfileResult result) {
     warnings.addAll(result.warnings());
     infos.addAll(result.infos());
+    changes.addAll(result.getChanges());
     return this;
   }
 
index 35ef9aba34cd121c35fb1063b47957cef425b21d..37639fcb1dc4ab5d3095ec7cb1f4e2a9096dec8e 100644 (file)
@@ -39,12 +39,13 @@ import org.sonar.api.rules.RulePriority;
 import org.sonar.api.server.rule.RuleParamType;
 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.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.qualityprofile.index.ActiveRuleIndexer;
 import org.sonar.server.tester.ServerTester;
 import org.sonar.server.tester.UserSessionRule;
 
@@ -54,10 +55,13 @@ import static org.junit.Assert.fail;
 public class QProfileExportersTest {
 
   @ClassRule
-  public static ServerTester tester = new ServerTester().withStartupTasks().addXoo().addComponents(
-    XooRulesDefinition.class, XooProfileDefinition.class,
-    XooExporter.class, StandardExporter.class,
-    XooProfileImporter.class, XooProfileImporterWithMessages.class, XooProfileImporterWithError.class);
+  public static ServerTester tester = new ServerTester()
+    .withEsIndexes()
+    .withStartupTasks()
+    .addXoo()
+    .addComponents(XooRulesDefinition.class, XooProfileDefinition.class, XooExporter.class, StandardExporter.class,
+      XooProfileImporter.class, XooProfileImporterWithMessages.class, XooProfileImporterWithError.class);
+
   @org.junit.Rule
   public UserSessionRule userSessionRule = UserSessionRule.forServerTester(tester);
 
@@ -65,6 +69,7 @@ public class QProfileExportersTest {
   DbSession dbSession;
   QProfileExporters exporters;
   QProfileLoader loader;
+  ActiveRuleIndexer activeRuleIndexer;
 
   @Before
   public void before() {
@@ -72,6 +77,8 @@ public class QProfileExportersTest {
     dbSession = db.openSession(false);
     exporters = tester.get(QProfileExporters.class);
     loader = tester.get(QProfileLoader.class);
+    activeRuleIndexer = tester.get(ActiveRuleIndexer.class);
+    activeRuleIndexer.setEnabled(true);
   }
 
   @After
@@ -135,8 +142,9 @@ public class QProfileExportersTest {
 
     assertThat(loader.findActiveRulesByProfile(profileDto.getKey())).isEmpty();
 
-    exporters.importXml(profileDto, "XooProfileImporter", "<xml/>", dbSession);
+    QProfileResult result = exporters.importXml(profileDto, "XooProfileImporter", "<xml/>", dbSession);
     dbSession.commit();
+    activeRuleIndexer.index(result.getChanges());
 
     List<ActiveRuleDoc> activeRules = Lists.newArrayList(loader.findActiveRulesByProfile(profileDto.getKey()));
     assertThat(activeRules).hasSize(1);