]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5380 Manage custom rule activation
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 11 Jun 2014 14:01:26 +0000 (16:01 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 11 Jun 2014 14:48:34 +0000 (16:48 +0200)
sonar-server/src/main/java/org/sonar/server/qualityprofile/RuleActivator.java
sonar-server/src/main/java/org/sonar/server/rule/RuleUpdater.java
sonar-server/src/test/java/org/sonar/server/qualityprofile/RuleActivatorMediumTest.java

index 0187ceb36447a16c13ec694b026eb207f00297cf..0e390f03c1d346bd4b5daec8e7c6fdc4f69f7a1c 100644 (file)
@@ -30,11 +30,7 @@ import org.sonar.api.server.rule.RuleParamType;
 import org.sonar.core.log.Log;
 import org.sonar.core.persistence.DbSession;
 import org.sonar.core.preview.PreviewCache;
-import org.sonar.core.qualityprofile.db.ActiveRuleDto;
-import org.sonar.core.qualityprofile.db.ActiveRuleKey;
-import org.sonar.core.qualityprofile.db.ActiveRuleParamDto;
-import org.sonar.core.qualityprofile.db.QualityProfileDto;
-import org.sonar.core.qualityprofile.db.QualityProfileKey;
+import org.sonar.core.qualityprofile.db.*;
 import org.sonar.core.rule.RuleDto;
 import org.sonar.core.rule.RuleParamDto;
 import org.sonar.server.db.DbClient;
@@ -51,6 +47,7 @@ import org.sonar.server.search.QueryOptions;
 import org.sonar.server.util.TypeValidations;
 
 import javax.annotation.Nullable;
+
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
@@ -162,6 +159,7 @@ public class RuleActivator implements ServerComponent {
    */
   private void applySeverityAndParamToChange(RuleActivation activation, RuleActivatorContext context, ActiveRuleChange change) {
     change.setSeverity(StringUtils.defaultIfEmpty(activation.getSeverity(), context.defaultSeverity()));
+    verifyParametersAreNotSetOnCustomRule(context, activation, change);
     for (RuleParamDto ruleParamDto : context.ruleParams()) {
       String value = StringUtils.defaultIfEmpty(
         activation.getParameters().get(ruleParamDto.getName()),
@@ -459,4 +457,11 @@ public class RuleActivator implements ServerComponent {
     }
     return false;
   }
+
+  private void verifyParametersAreNotSetOnCustomRule(RuleActivatorContext context, RuleActivation activation, ActiveRuleChange change){
+    if (!activation.getParameters().isEmpty() && context.rule().getParentId() != null) {
+      throw new IllegalStateException(String.format("Parameters cannot be set when activating the custom rule '%s'", activation.getKey().ruleKey()));
+    }
+  }
+
 }
index 2087f840c6e3d6e181705e45368cf0eee65ec040..11310a294c8b9db50f6cbfbf5be731db6d9461c1 100644 (file)
@@ -30,12 +30,11 @@ import org.sonar.api.server.debt.DebtRemediationFunction;
 import org.sonar.api.utils.System2;
 import org.sonar.core.persistence.DbSession;
 import org.sonar.core.qualityprofile.db.ActiveRuleDto;
+import org.sonar.core.qualityprofile.db.ActiveRuleParamDto;
 import org.sonar.core.rule.RuleDto;
 import org.sonar.core.rule.RuleParamDto;
 import org.sonar.core.technicaldebt.db.CharacteristicDto;
 import org.sonar.server.db.DbClient;
-import org.sonar.server.qualityprofile.RuleActivation;
-import org.sonar.server.qualityprofile.RuleActivator;
 import org.sonar.server.user.UserSession;
 
 import java.util.Collections;
@@ -47,12 +46,10 @@ import static com.google.common.collect.Lists.newArrayList;
 
 public class RuleUpdater implements ServerComponent {
 
-  private final RuleActivator ruleActivator;
   private final DbClient dbClient;
   private final System2 system;
 
-  public RuleUpdater(RuleActivator ruleActivator, DbClient dbClient, System2 system) {
-    this.ruleActivator = ruleActivator;
+  public RuleUpdater(DbClient dbClient, System2 system) {
     this.dbClient = dbClient;
     this.system = system;
   }
@@ -84,10 +81,13 @@ public class RuleUpdater implements ServerComponent {
   private void updateActiveRule(DbSession dbSession, RuleUpdate update, RuleDto rule) {
     if (update.isCustomRule() && update.isChangeParameters()) {
       for (ActiveRuleDto activeRuleDto : dbClient.activeRuleDao().findByRule(dbSession, rule)) {
-        RuleActivation ruleActivation = new RuleActivation(activeRuleDto.getKey())
-          .setSeverity(activeRuleDto.getSeverityString())
-          .setParameters(update.getParameters());
-        ruleActivator.activate(dbSession, ruleActivation);
+        for (ActiveRuleParamDto activeRuleParamDto : dbClient.activeRuleDao().findParamsByActiveRuleKey(dbSession, activeRuleDto.getKey())) {
+          String newValue = update.getParameters().get(activeRuleParamDto.getKey());
+          if (!Strings.isNullOrEmpty(newValue)) {
+            activeRuleParamDto.setValue(newValue);
+            dbClient.activeRuleDao().updateParam(dbSession, activeRuleDto, activeRuleParamDto);
+          }
+        }
       }
     }
   }
index 16c1e339a35b524339b0fb496eafdd83e910e7e9..ff8ec304c9f18403850279627220167235932d88 100644 (file)
@@ -28,13 +28,8 @@ import org.sonar.api.rule.RuleKey;
 import org.sonar.api.rule.RuleStatus;
 import org.sonar.api.rule.Severity;
 import org.sonar.api.server.rule.RuleParamType;
-import org.sonar.check.Cardinality;
 import org.sonar.core.persistence.DbSession;
-import org.sonar.core.qualityprofile.db.ActiveRuleDto;
-import org.sonar.core.qualityprofile.db.ActiveRuleKey;
-import org.sonar.core.qualityprofile.db.ActiveRuleParamDto;
-import org.sonar.core.qualityprofile.db.QualityProfileDto;
-import org.sonar.core.qualityprofile.db.QualityProfileKey;
+import org.sonar.core.qualityprofile.db.*;
 import org.sonar.core.rule.RuleDto;
 import org.sonar.core.rule.RuleParamDto;
 import org.sonar.server.db.DbClient;
@@ -48,6 +43,7 @@ import org.sonar.server.search.QueryOptions;
 import org.sonar.server.tester.ServerTester;
 
 import javax.annotation.Nullable;
+
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
@@ -62,6 +58,8 @@ public class RuleActivatorMediumTest {
   static final QualityProfileKey XOO_CHILD_PROFILE_KEY = QualityProfileKey.of("P2", "xoo");
   static final QualityProfileKey XOO_GRAND_CHILD_PROFILE_KEY = QualityProfileKey.of("P3", "xoo");
   static final RuleKey MANUAL_RULE_KEY = RuleKey.of(Rule.MANUAL_REPOSITORY_KEY, "m1");
+  static final RuleKey TEMPLATE_RULE_KEY = RuleKey.of("xoo", "template1");
+  static final RuleKey CUSTOM_RULE_KEY = RuleKey.of(TEMPLATE_RULE_KEY.repository(), "custom1");
   static final RuleKey XOO_RULE_1 = RuleKey.of("xoo", "x1");
   static final RuleKey XOO_RULE_2 = RuleKey.of("xoo", "x2");
 
@@ -87,14 +85,22 @@ public class RuleActivatorMediumTest {
     RuleDto xooRule1 = RuleTesting.newDto(XOO_RULE_1)
       .setSeverity("MINOR").setLanguage("xoo");
     RuleDto xooRule2 = RuleTesting.newDto(XOO_RULE_2).setLanguage("xoo");
-    RuleDto xooTemplateRule1 = RuleTesting.newDto(RuleKey.of("xoo", "template1"))
-      .setSeverity("MINOR").setLanguage("xoo").setCardinality(Cardinality.MULTIPLE);
+    RuleDto xooTemplateRule1 = RuleTesting.newTemplateRule(TEMPLATE_RULE_KEY)
+      .setSeverity("MINOR").setLanguage("xoo");
     RuleDto manualRule = RuleTesting.newDto(MANUAL_RULE_KEY);
     db.ruleDao().insert(dbSession, javaRule, xooRule1, xooRule2, xooTemplateRule1, manualRule);
     db.ruleDao().addRuleParam(dbSession, xooRule1, RuleParamDto.createFor(xooRule1)
       .setName("max").setDefaultValue("10").setType(RuleParamType.INTEGER.type()));
     db.ruleDao().addRuleParam(dbSession, xooRule1, RuleParamDto.createFor(xooRule1)
       .setName("min").setType(RuleParamType.INTEGER.type()));
+    db.ruleDao().addRuleParam(dbSession, xooTemplateRule1, RuleParamDto.createFor(xooTemplateRule1)
+      .setName("format").setType(RuleParamType.STRING.type()));
+
+    RuleDto xooCustomRule1 = RuleTesting.newCustomRule(xooTemplateRule1).setRuleKey(CUSTOM_RULE_KEY.rule())
+      .setSeverity("MINOR").setLanguage("xoo");
+    db.ruleDao().insert(dbSession, xooCustomRule1);
+    db.ruleDao().addRuleParam(dbSession, xooTemplateRule1, RuleParamDto.createFor(xooTemplateRule1)
+      .setName("format").setDefaultValue("txt").setType(RuleParamType.STRING.type()));
 
     // create pre-defined profile
     db.qualityProfileDao().insert(dbSession, QualityProfileDto.createFor(XOO_PROFILE_KEY));
@@ -203,7 +209,7 @@ public class RuleActivatorMediumTest {
 
   @Test
   public void fail_to_activate_if_template() throws Exception {
-    RuleActivation activation = new RuleActivation(ActiveRuleKey.of(XOO_PROFILE_KEY, RuleKey.of("xoo", "template1")));
+    RuleActivation activation = new RuleActivation(ActiveRuleKey.of(XOO_PROFILE_KEY, TEMPLATE_RULE_KEY));
 
     try {
       ruleActivator.activate(activation);
@@ -301,6 +307,20 @@ public class RuleActivatorMediumTest {
     }
   }
 
+  @Test
+  public void fail_to_activate_if_custom_rule_template_and_parameters_are_set() throws Exception {
+    RuleActivation activation = new RuleActivation(ActiveRuleKey.of(XOO_PROFILE_KEY, CUSTOM_RULE_KEY))
+      .setParameter("format", "xls");
+
+    try {
+      ruleActivator.activate(activation);
+      fail();
+    } catch (Exception e) {
+      assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("Parameters cannot be set when activating the custom rule 'xoo:custom1'");
+      verifyZeroActiveRules(XOO_PROFILE_KEY);
+    }
+  }
+
   @Test
   public void deactivate() throws Exception {
     // activation