]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5121 Authorize overriding characteristic or function
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 17 Mar 2014 08:27:19 +0000 (09:27 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 17 Mar 2014 08:27:19 +0000 (09:27 +0100)
sonar-batch/src/main/java/org/sonar/batch/rule/RulesProvider.java
sonar-batch/src/test/java/org/sonar/batch/rule/RulesProviderTest.java
sonar-batch/src/test/resources/org/sonar/batch/rule/RulesProviderTest/build_rules_with_default_and_overridden_debt_definitions.xml [new file with mode: 0644]
sonar-batch/src/test/resources/org/sonar/batch/rule/RulesProviderTest/build_rules_with_default_and_user_debt_definitions.xml [deleted file]
sonar-batch/src/test/resources/org/sonar/batch/rule/RulesProviderTest/build_rules_with_default_characteristic_and_overridden_function.xml [new file with mode: 0644]
sonar-batch/src/test/resources/org/sonar/batch/rule/RulesProviderTest/build_rules_with_overridden_characteristic_and_default_function.xml [new file with mode: 0644]
sonar-batch/src/test/resources/org/sonar/batch/rule/RulesProviderTest/build_rules_with_overridden_debt_definitions.xml [new file with mode: 0644]
sonar-batch/src/test/resources/org/sonar/batch/rule/RulesProviderTest/build_rules_with_user_debt_definitions.xml [deleted file]

index 75d7c9ae5876c5d08ffd01b7f9ff0f1337a01da4..0b2332bc4fcafb03dd6c24c11e0d04a1db28f427 100644 (file)
@@ -78,18 +78,12 @@ public class RulesProvider extends ProviderAdapter {
         .setDescription(ruleDto.getDescription())
         .setStatus(RuleStatus.valueOf(ruleDto.getStatus()));
       // TODO should we set metadata ?
+
       if (!ruleDto.isCharacteristicDisabled()) {
-        Integer characteristicId = ruleDto.getCharacteristicId();
-        Integer defaultCharacteristicId = ruleDto.getDefaultCharacteristicId();
-        if (characteristicId != null) {
-          Characteristic characteristic = characteristic(characteristicId, ruleKey, debtModel);
-          updateRuleDebtDefinitions(newRule, ruleKey, characteristic, ruleDto.getRemediationFunction(), ruleDto.getRemediationFactor(), ruleDto.getRemediationOffset(), durations);
-        } else if (defaultCharacteristicId != null) {
-          Characteristic characteristic = characteristic(defaultCharacteristicId, ruleKey, debtModel);
-          updateRuleDebtDefinitions(newRule, ruleKey, characteristic, ruleDto.getDefaultRemediationFunction(), ruleDto.getDefaultRemediationFactor(),
-            ruleDto.getDefaultRemediationOffset(), durations);
-        }
+        newRule.setCharacteristic(characteristic(ruleDto, ruleKey, debtModel).key());
+        setFunction(ruleDto, newRule, ruleKey, durations);
       }
+
       for (RuleParamDto ruleParamDto : paramDtosByRuleId.get(ruleDto.getId())) {
         newRule.addParam(ruleParamDto.getName())
           .setDescription(ruleParamDto.getDescription());
@@ -98,19 +92,33 @@ public class RulesProvider extends ProviderAdapter {
     return rulesBuilder.build();
   }
 
-  private void updateRuleDebtDefinitions(NewRule newRule, RuleKey ruleKey, Characteristic characteristic, @Nullable String function,
-                                         @Nullable String factor, @Nullable String offset,
-                                         Durations durations) {
-    newRule.setCharacteristic(characteristic.key());
-    newRule.setFunction(function(function, ruleKey));
-    newRule.setFactor(factor != null ? durations.decode(factor) : null);
-    newRule.setOffset(offset != null ? durations.decode(offset) : null);
+  private void setFunction(RuleDto ruleDto, NewRule newRule, RuleKey ruleKey, Durations durations) {
+    String function = ruleDto.getRemediationFunction();
+    String factor = ruleDto.getRemediationFactor();
+    String offset = ruleDto.getRemediationOffset();
+
+    String defaultFunction = ruleDto.getDefaultRemediationFunction();
+    String defaultFactor = ruleDto.getDefaultRemediationFactor();
+    String defaultOffset = ruleDto.getDefaultRemediationOffset();
+
+    if (function != null) {
+      newRule.setFunction(function(function, ruleKey));
+      newRule.setFactor(factor != null ? durations.decode(factor) : null);
+      newRule.setOffset(offset != null ? durations.decode(offset) : null);
+    } else {
+      newRule.setFunction(function(defaultFunction, ruleKey));
+      newRule.setFactor(defaultFactor != null ? durations.decode(defaultFactor) : null);
+      newRule.setOffset(defaultOffset != null ? durations.decode(defaultOffset) : null);
+    }
   }
 
-  private Characteristic characteristic(Integer characteristicId, RuleKey ruleKey, TechnicalDebtModel debtModel) {
-    Characteristic characteristic = debtModel.characteristicById(characteristicId);
+  private Characteristic characteristic(RuleDto ruleDto, RuleKey ruleKey, TechnicalDebtModel debtModel) {
+    Integer characteristicId = ruleDto.getCharacteristicId();
+    Integer defaultCharacteristicId = ruleDto.getDefaultCharacteristicId();
+    Integer effectiveCharacteristicId = characteristicId != null ? characteristicId : defaultCharacteristicId;
+    Characteristic characteristic = debtModel.characteristicById(effectiveCharacteristicId);
     if (characteristic == null) {
-      throw new IllegalStateException(String.format("Characteristic id '%s' on rule '%s' has not been found", characteristicId, ruleKey));
+      throw new IllegalStateException(String.format("Characteristic id '%s' on rule '%s' has not been found", effectiveCharacteristicId, ruleKey));
     }
     return characteristic;
   }
index 14be820ef4f15419b2db98e771e8d4d56d71b14d..01108161c10723ce58604e79e82f85625fb656d3 100644 (file)
@@ -118,8 +118,8 @@ public class RulesProviderTest extends AbstractDaoTestCase {
   }
 
   @Test
-  public void build_rules_with_user_debt_definitions() throws Exception {
-    setupData("build_rules_with_user_debt_definitions");
+  public void build_rules_with_overridden_debt_definitions() throws Exception {
+    setupData("build_rules_with_overridden_debt_definitions");
 
     Rules rules = provider.provide(ruleDao, debtModel, durations);
 
@@ -131,8 +131,36 @@ public class RulesProviderTest extends AbstractDaoTestCase {
   }
 
   @Test
-  public void build_rules_with_default_and_user_debt_definitions() throws Exception {
-    setupData("build_rules_with_default_and_user_debt_definitions");
+  public void build_rules_with_default_and_overridden_debt_definitions() throws Exception {
+    setupData("build_rules_with_default_and_overridden_debt_definitions");
+
+    Rules rules = provider.provide(ruleDao, debtModel, durations);
+
+    // As both default columns and user columns on debt are set, user debt columns should be used
+    Rule rule = rules.find(RuleKey.of("checkstyle", "AvoidNull"));
+    assertThat(rule.characteristic()).isEqualTo("PORTABILITY");
+    assertThat(rule.function()).isEqualTo(RemediationFunction.LINEAR);
+    assertThat(rule.factor()).isEqualTo(Duration.decode("2h", 8));
+    assertThat(rule.offset()).isNull();
+  }
+
+  @Test
+  public void build_rules_with_default_characteristic_and_overridden_function() throws Exception {
+    setupData("build_rules_with_default_characteristic_and_overridden_function");
+
+    Rules rules = provider.provide(ruleDao, debtModel, durations);
+
+    // As both default columns and user columns on debt are set, user debt columns should be used
+    Rule rule = rules.find(RuleKey.of("checkstyle", "AvoidNull"));
+    assertThat(rule.characteristic()).isEqualTo("PORTABILITY");
+    assertThat(rule.function()).isEqualTo(RemediationFunction.LINEAR);
+    assertThat(rule.factor()).isEqualTo(Duration.decode("2h", 8));
+    assertThat(rule.offset()).isNull();
+  }
+
+  @Test
+  public void build_rules_with_overridden_characteristic_and_default_function() throws Exception {
+    setupData("build_rules_with_overridden_characteristic_and_default_function");
 
     Rules rules = provider.provide(ruleDao, debtModel, durations);
 
diff --git a/sonar-batch/src/test/resources/org/sonar/batch/rule/RulesProviderTest/build_rules_with_default_and_overridden_debt_definitions.xml b/sonar-batch/src/test/resources/org/sonar/batch/rule/RulesProviderTest/build_rules_with_default_and_overridden_debt_definitions.xml
new file mode 100644 (file)
index 0000000..37c78b1
--- /dev/null
@@ -0,0 +1,9 @@
+<dataset>
+
+  <rules id="1" plugin_rule_key="AvoidNull" plugin_name="checkstyle" name="Avoid Null" description="Should avoid NULL" status="READY" priority="1"
+         characteristic_id="103" default_characteristic_id="101"
+         remediation_function="LINEAR" default_remediation_function="LINEAR_OFFSET"
+         remediation_factor="2h" default_remediation_factor="5d"
+         remediation_offset="[null]" default_remediation_offset="10h"/>
+
+</dataset>
diff --git a/sonar-batch/src/test/resources/org/sonar/batch/rule/RulesProviderTest/build_rules_with_default_and_user_debt_definitions.xml b/sonar-batch/src/test/resources/org/sonar/batch/rule/RulesProviderTest/build_rules_with_default_and_user_debt_definitions.xml
deleted file mode 100644 (file)
index aa0e5c2..0000000
+++ /dev/null
@@ -1,29 +0,0 @@
-<!--
-  ~ SonarQube, open source software quality management tool.
-  ~ Copyright (C) 2008-2014 SonarSource
-  ~ mailto:contact AT sonarsource DOT com
-  ~
-  ~ SonarQube is free software; you can redistribute it and/or
-  ~ modify it under the terms of the GNU Lesser General Public
-  ~ License as published by the Free Software Foundation; either
-  ~ version 3 of the License, or (at your option) any later version.
-  ~
-  ~ SonarQube is distributed in the hope that it will be useful,
-  ~ but WITHOUT ANY WARRANTY; without even the implied warranty of
-  ~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-  ~ Lesser General Public License for more details.
-  ~
-  ~ You should have received a copy of the GNU Lesser General Public License
-  ~ along with this program; if not, write to the Free Software Foundation,
-  ~ Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
-  -->
-
-<dataset>
-
-  <rules id="1" plugin_rule_key="AvoidNull" plugin_name="checkstyle" name="Avoid Null" description="Should avoid NULL" status="READY" priority="1"
-         characteristic_id="103" default_characteristic_id="101"
-         remediation_function="LINEAR" default_remediation_function="LINEAR_OFFSET"
-         remediation_factor="2h" default_remediation_factor="5d"
-         remediation_offset="[null]" default_remediation_offset="10h"/>
-
-</dataset>
diff --git a/sonar-batch/src/test/resources/org/sonar/batch/rule/RulesProviderTest/build_rules_with_default_characteristic_and_overridden_function.xml b/sonar-batch/src/test/resources/org/sonar/batch/rule/RulesProviderTest/build_rules_with_default_characteristic_and_overridden_function.xml
new file mode 100644 (file)
index 0000000..dd8de81
--- /dev/null
@@ -0,0 +1,9 @@
+<dataset>
+
+  <rules id="1" plugin_rule_key="AvoidNull" plugin_name="checkstyle" name="Avoid Null" description="Should avoid NULL" status="READY" priority="1"
+         characteristic_id="[null]" default_characteristic_id="103"
+         remediation_function="LINEAR" default_remediation_function="[null]"
+         remediation_factor="2h" default_remediation_factor="[null]"
+         remediation_offset="[null]" default_remediation_offset="[null]"/>
+
+</dataset>
diff --git a/sonar-batch/src/test/resources/org/sonar/batch/rule/RulesProviderTest/build_rules_with_overridden_characteristic_and_default_function.xml b/sonar-batch/src/test/resources/org/sonar/batch/rule/RulesProviderTest/build_rules_with_overridden_characteristic_and_default_function.xml
new file mode 100644 (file)
index 0000000..ebf9a30
--- /dev/null
@@ -0,0 +1,9 @@
+<dataset>
+
+  <rules id="1" plugin_rule_key="AvoidNull" plugin_name="checkstyle" name="Avoid Null" description="Should avoid NULL" status="READY" priority="1"
+         characteristic_id="103" default_characteristic_id="[null]"
+         remediation_function="[null]" default_remediation_function="LINEAR"
+         remediation_factor="[null]" default_remediation_factor="2h"
+         remediation_offset="[null]" default_remediation_offset="[null]"/>
+
+</dataset>
diff --git a/sonar-batch/src/test/resources/org/sonar/batch/rule/RulesProviderTest/build_rules_with_overridden_debt_definitions.xml b/sonar-batch/src/test/resources/org/sonar/batch/rule/RulesProviderTest/build_rules_with_overridden_debt_definitions.xml
new file mode 100644 (file)
index 0000000..8137148
--- /dev/null
@@ -0,0 +1,29 @@
+<!--
+  ~ SonarQube, open source software quality management tool.
+  ~ Copyright (C) 2008-2014 SonarSource
+  ~ mailto:contact AT sonarsource DOT com
+  ~
+  ~ SonarQube is free software; you can redistribute it and/or
+  ~ modify it under the terms of the GNU Lesser General Public
+  ~ License as published by the Free Software Foundation; either
+  ~ version 3 of the License, or (at your option) any later version.
+  ~
+  ~ SonarQube is distributed in the hope that it will be useful,
+  ~ but WITHOUT ANY WARRANTY; without even the implied warranty of
+  ~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+  ~ Lesser General Public License for more details.
+  ~
+  ~ You should have received a copy of the GNU Lesser General Public License
+  ~ along with this program; if not, write to the Free Software Foundation,
+  ~ Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+  -->
+
+<dataset>
+
+  <rules id="1" plugin_rule_key="AvoidNull" plugin_name="checkstyle" name="Avoid Null" description="Should avoid NULL" status="READY" priority="1"
+         characteristic_id="103" default_characteristic_id="[null]"
+         remediation_function="LINEAR" default_remediation_function="[null]"
+         remediation_factor="2h" default_remediation_factor="[null]"
+         remediation_offset="[null]" default_remediation_offset="[null]"/>
+
+</dataset>
diff --git a/sonar-batch/src/test/resources/org/sonar/batch/rule/RulesProviderTest/build_rules_with_user_debt_definitions.xml b/sonar-batch/src/test/resources/org/sonar/batch/rule/RulesProviderTest/build_rules_with_user_debt_definitions.xml
deleted file mode 100644 (file)
index 8137148..0000000
+++ /dev/null
@@ -1,29 +0,0 @@
-<!--
-  ~ SonarQube, open source software quality management tool.
-  ~ Copyright (C) 2008-2014 SonarSource
-  ~ mailto:contact AT sonarsource DOT com
-  ~
-  ~ SonarQube is free software; you can redistribute it and/or
-  ~ modify it under the terms of the GNU Lesser General Public
-  ~ License as published by the Free Software Foundation; either
-  ~ version 3 of the License, or (at your option) any later version.
-  ~
-  ~ SonarQube is distributed in the hope that it will be useful,
-  ~ but WITHOUT ANY WARRANTY; without even the implied warranty of
-  ~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-  ~ Lesser General Public License for more details.
-  ~
-  ~ You should have received a copy of the GNU Lesser General Public License
-  ~ along with this program; if not, write to the Free Software Foundation,
-  ~ Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
-  -->
-
-<dataset>
-
-  <rules id="1" plugin_rule_key="AvoidNull" plugin_name="checkstyle" name="Avoid Null" description="Should avoid NULL" status="READY" priority="1"
-         characteristic_id="103" default_characteristic_id="[null]"
-         remediation_function="LINEAR" default_remediation_function="[null]"
-         remediation_factor="2h" default_remediation_factor="[null]"
-         remediation_offset="[null]" default_remediation_offset="[null]"/>
-
-</dataset>