]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7330 QProfileReset is now using ActiveRuleIndexer
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 24 Feb 2016 20:18:21 +0000 (21:18 +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/QProfileReset.java
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileResetMediumTest.java

index 9b9bbc64297f81cb86143f98c36f12b4394a3a69..beb198c2891f95034d2495eb54f5d686cccfefc5 100644 (file)
@@ -24,6 +24,7 @@ import com.google.common.collect.ArrayListMultimap;
 import com.google.common.collect.ListMultimap;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Sets;
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
 import java.util.Map;
@@ -35,13 +36,14 @@ import org.sonar.api.rules.ActiveRule;
 import org.sonar.api.rules.ActiveRuleParam;
 import org.sonar.api.server.ServerSide;
 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.ActiveRuleKey;
 import org.sonar.db.qualityprofile.QualityProfileDto;
 import org.sonar.db.rule.RuleParamDto;
-import org.sonar.server.db.DbClient;
 import org.sonar.server.exceptions.BadRequestException;
+import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
 
 @ServerSide
 public class QProfileReset {
@@ -49,19 +51,22 @@ public class QProfileReset {
   private final DbClient db;
   private final QProfileFactory factory;
   private final RuleActivator activator;
+  private final ActiveRuleIndexer activeRuleIndexer;
   private final BuiltInProfiles builtInProfiles;
   private final ProfileDefinition[] definitions;
 
-  public QProfileReset(DbClient db, RuleActivator activator, BuiltInProfiles builtInProfiles, QProfileFactory factory, ProfileDefinition[] definitions) {
+  public QProfileReset(DbClient db, RuleActivator activator, ActiveRuleIndexer activeRuleIndexer, BuiltInProfiles builtInProfiles, QProfileFactory factory,
+    ProfileDefinition[] definitions) {
     this.db = db;
     this.activator = activator;
+    this.activeRuleIndexer = activeRuleIndexer;
     this.builtInProfiles = builtInProfiles;
     this.factory = factory;
     this.definitions = definitions;
   }
 
-  public QProfileReset(DbClient db, RuleActivator activator, BuiltInProfiles builtInProfiles, QProfileFactory factory) {
-    this(db, activator, builtInProfiles, factory, new ProfileDefinition[0]);
+  public QProfileReset(DbClient db, RuleActivator activator, BuiltInProfiles builtInProfiles, QProfileFactory factory, ActiveRuleIndexer activeRuleIndexer) {
+    this(db, activator, activeRuleIndexer, builtInProfiles, factory, new ProfileDefinition[0]);
   }
 
   public Collection<String> builtInProfileNamesForLanguage(String language) {
@@ -89,7 +94,7 @@ public class QProfileReset {
                 activation.setParameter(param.getParamKey(), param.getValue());
               }
             } else {
-              for (RuleParamDto param : db.deprecatedRuleDao().selectRuleParamsByRuleKey(dbSession, activeRule.getRule().ruleKey())) {
+              for (RuleParamDto param : db.ruleDao().selectRuleParamsByRuleKey(dbSession, activeRule.getRule().ruleKey())) {
                 activation.setParameter(param.getName(), param.getDefaultValue());
               }
             }
@@ -97,7 +102,6 @@ public class QProfileReset {
           }
         }
         doReset(dbSession, profile, activations);
-        dbSession.commit();
       }
     } finally {
       dbSession.close();
@@ -112,7 +116,6 @@ public class QProfileReset {
     try {
       QualityProfileDto profile = factory.getOrCreate(dbSession, profileName);
       BulkChangeResult result = doReset(dbSession, profile, activations);
-      dbSession.commit();
       return result;
     } finally {
       dbSession.close();
@@ -147,13 +150,17 @@ public class QProfileReset {
       }
     }
 
+    List<ActiveRuleChange> changes = new ArrayList<>();
+    changes.addAll(result.getChanges());
     for (RuleKey ruleKey : ruleToBeDeactivated) {
       try {
-        activator.deactivate(dbSession, ActiveRuleKey.of(profile.getKee(), ruleKey));
+        changes.addAll(activator.deactivate(dbSession, ActiveRuleKey.of(profile.getKee(), ruleKey)));
       } catch (BadRequestException e) {
         // ignore, probably a rule inherited from parent that can't be deactivated
       }
     }
+    dbSession.commit();
+    activeRuleIndexer.index(changes);
     return result;
   }
 
index 33f11227767d70c2f12d0498425be0ab77d80903..92e052ca286292c4652fc65224fc63e014845c76 100644 (file)
@@ -19,7 +19,8 @@
  */
 package org.sonar.server.qualityprofile;
 
-import com.google.common.collect.ImmutableMap;
+import java.util.List;
+import javax.annotation.Nullable;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.ClassRule;
@@ -28,34 +29,42 @@ import org.junit.Test;
 import org.sonar.api.profiles.ProfileDefinition;
 import org.sonar.api.profiles.RulesProfile;
 import org.sonar.api.rule.RuleKey;
-import org.sonar.api.rule.Severity;
 import org.sonar.api.rules.RuleParam;
 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.ActiveRuleDao;
+import org.sonar.db.qualityprofile.ActiveRuleDto;
 import org.sonar.db.qualityprofile.ActiveRuleKey;
+import org.sonar.db.qualityprofile.ActiveRuleParamDto;
 import org.sonar.db.qualityprofile.QualityProfileDao;
 import org.sonar.db.qualityprofile.QualityProfileDto;
-import org.sonar.server.db.DbClient;
 import org.sonar.server.platform.Platform;
-import org.sonar.server.qualityprofile.index.ActiveRuleIndex;
+import org.sonar.server.qualityprofile.index.ActiveRuleIndex2;
 import org.sonar.server.tester.ServerTester;
-
-import javax.annotation.Nullable;
 import org.sonar.server.tester.UserSessionRule;
 
 import static com.google.common.collect.Lists.newArrayList;
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonar.api.rule.Severity.BLOCKER;
+import static org.sonar.api.rule.Severity.CRITICAL;
+import static org.sonar.api.rule.Severity.MAJOR;
+import static org.sonar.api.rule.Severity.MINOR;
 
+// TODO Replace ServerTester by EsTester and DbTester
 public class QProfileResetMediumTest {
 
   static final XooRulesDefinition RULE_DEFS = new XooRulesDefinition();
   static final XooProfileDefinition PROFILE_DEFS = new XooProfileDefinition();
 
   @ClassRule
-  public static ServerTester tester = new ServerTester().addXoo().addComponents(PROFILE_DEFS, RULE_DEFS);
+  public static ServerTester tester = new ServerTester()
+    .withEsIndexes()
+    .addXoo().addComponents(PROFILE_DEFS, RULE_DEFS);
+
   @Rule
   public UserSessionRule userSessionRule = UserSessionRule.forServerTester(tester);
 
@@ -105,7 +114,7 @@ public class QProfileResetMediumTest {
       RulesDefinition.NewRule x1 = repository.createRule("x1")
         .setName("x1 name")
         .setHtmlDescription("x1 desc")
-        .setSeverity(Severity.MINOR);
+        .setSeverity(MINOR);
       x1.createParam("acceptWhitespace")
         .setDefaultValue("false")
         .setType(RuleParamType.BOOLEAN)
@@ -120,23 +129,32 @@ public class QProfileResetMediumTest {
 
     // Change the severity and the value of the parameter in the active rule
     tester.get(RuleActivator.class).activate(dbSession,
-      new RuleActivation(ruleKey).setSeverity(Severity.BLOCKER)
+      new RuleActivation(ruleKey).setSeverity(BLOCKER)
         .setParameter("acceptWhitespace", "false"),
       profile.getKey()
     );
     dbSession.commit();
 
+
     // Verify severity and param has changed
-    ActiveRule activeRule = tester.get(ActiveRuleIndex.class).getNullableByKey(activeRuleKey);
-    assertThat(activeRule.severity()).isEqualTo(Severity.BLOCKER);
-    assertThat(activeRule.params()).isEqualTo(ImmutableMap.of("acceptWhitespace", "false"));
+    ActiveRuleDto activeRuleDto = tester.get(ActiveRuleDao.class).selectOrFailByKey(dbSession, activeRuleKey);
+    assertThat(activeRuleDto.getSeverityString()).isEqualTo(BLOCKER);
+    List<ActiveRuleParamDto> activeRuleParamDtos = tester.get(ActiveRuleDao.class).selectParamsByActiveRuleKey(dbSession, activeRuleKey);
+    assertThat(activeRuleParamDtos.get(0).getKey()).isEqualTo("acceptWhitespace");
+    assertThat(activeRuleParamDtos.get(0).getValue()).isEqualTo("false");
 
     reset.resetLanguage(ServerTester.Xoo.KEY);
+    dbSession.commit();
 
     // Severity and parameter value come back to origin after reset
-    activeRule = tester.get(ActiveRuleIndex.class).getNullableByKey(activeRuleKey);
-    assertThat(activeRule.severity()).isEqualTo(Severity.CRITICAL);
-    assertThat(activeRule.params()).isEqualTo(ImmutableMap.of("acceptWhitespace", "true"));
+    activeRuleDto = tester.get(ActiveRuleDao.class).selectOrFailByKey(dbSession, activeRuleKey);
+    assertThat(activeRuleDto.getSeverityString()).isEqualTo(CRITICAL);
+    ActiveRule activeRule = tester.get(ActiveRuleIndex2.class).getNullableByKey(activeRuleKey);
+    assertThat(activeRule.severity()).isEqualTo(CRITICAL);
+
+    activeRuleParamDtos = tester.get(ActiveRuleDao.class).selectParamsByActiveRuleKey(dbSession, activeRuleKey);
+    assertThat(activeRuleParamDtos.get(0).getKey()).isEqualTo("acceptWhitespace");
+    assertThat(activeRuleParamDtos.get(0).getValue()).isEqualTo("true");
   }
 
   @Test
@@ -150,7 +168,7 @@ public class QProfileResetMediumTest {
         RulesDefinition.NewRule x1 = repository.createRule("x1")
           .setName("x1 name")
           .setHtmlDescription("x1 desc")
-          .setSeverity(Severity.MAJOR);
+          .setSeverity(MAJOR);
         x1.createParam("acceptWhitespace")
           .setDefaultValue("false")
           .setType(RuleParamType.BOOLEAN)
@@ -167,7 +185,7 @@ public class QProfileResetMediumTest {
         RulesDefinition.NewRule x1 = repository.createRule("x1")
           .setName("x1 name")
           .setHtmlDescription("x1 desc")
-          .setSeverity(Severity.MAJOR);
+          .setSeverity(MAJOR);
         x1.createParam("acceptWhitespace")
           .setDefaultValue("true")
           .setType(RuleParamType.BOOLEAN)
@@ -177,8 +195,10 @@ public class QProfileResetMediumTest {
     reset.resetLanguage(ServerTester.Xoo.KEY);
 
     // Parameter value come back to origin after reset
-    ActiveRule activeRule = tester.get(ActiveRuleIndex.class).getNullableByKey(activeRuleKey);
-    assertThat(activeRule.params()).isEqualTo(ImmutableMap.of("acceptWhitespace", "true"));
+    List<ActiveRuleParamDto> params = tester.get(ActiveRuleDao.class).selectParamsByActiveRuleKey(dbSession, activeRuleKey);
+    assertThat(params).hasSize(1);
+    assertThat(params.get(0).getKey()).isEqualTo("acceptWhitespace");
+    assertThat(params.get(0).getValue()).isEqualTo("true");
   }
 
   interface Rules {