.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());
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;
}
}
@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);
}
@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);
--- /dev/null
+<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>
+++ /dev/null
-<!--
- ~ 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>
--- /dev/null
+<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>
--- /dev/null
+<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>
--- /dev/null
+<!--
+ ~ 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>
+++ /dev/null
-<!--
- ~ 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>