import org.sonar.core.debt.db.CharacteristicDto;
import org.sonar.core.persistence.MyBatis;
+import javax.annotation.CheckForNull;
+
import java.io.Reader;
import java.util.Collection;
import java.util.List;
Rule rule = rulesCache.getByRuleKey(pluginRequirement.ruleKey());
if (!isRequirementExists(existingModel, rule)) {
CharacteristicDto characteristicDto = findCharacteristic(existingModel, pluginRequirement.characteristic().key());
- Integer rootId = characteristicDto.getRootId();
- if (rootId == null) {
- throw new IllegalArgumentException("Requirement on rule '" + pluginRequirement.ruleKey() + "' should not be linked on a root characteristic.");
+ if (characteristicDto != null) {
+ Integer rootId = characteristicDto.getRootId();
+ if (rootId == null) {
+ throw new IllegalArgumentException("Requirement on rule '" + pluginRequirement.ruleKey() + "' should not be linked on a root characteristic.");
+ }
+ CharacteristicDto requirementDto = CharacteristicDto.toDto(pluginRequirement, characteristicDto.getId(), rootId, rule.getId());
+ dao.insert(requirementDto, session);
}
- CharacteristicDto requirementDto = CharacteristicDto.toDto(pluginRequirement, characteristicDto.getId(), rootId, rule.getId());
- dao.insert(requirementDto, session);
}
}
}
return pluginList;
}
+ @CheckForNull
private CharacteristicDto findCharacteristic(List<CharacteristicDto> existingModel, final String key) {
return Iterables.find(existingModel, new Predicate<CharacteristicDto>() {
@Override
String characteristicKey = input.getKey();
return input.getRuleId() == null && characteristicKey != null && characteristicKey.equals(key);
}
- });
+ }, null);
}
private boolean isRequirementExists(List<CharacteristicDto> existingModel, final Rule rule) {
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rules.Rule;
import org.sonar.api.server.rule.RuleDefinitions;
+import org.sonar.api.utils.MessageException;
import org.sonar.api.utils.System2;
import org.sonar.api.utils.TimeProfiler;
import org.sonar.check.Cardinality;
LOG.warn(String.format("Characteristic '%s' has not been found, Technical debt definitions on rule '%s:%s' will be ignored",
key, ruleDef.repository().name(), ruleDef.key()));
} else if (characteristicDto.getParentId() == null) {
- LOG.error(String.format("Rule '%s:%s' should not be linked on the root characteristic '%s'. Technical debt definitions on this rule wll be ignored", key,
- ruleDef.repository().name(), ruleDef.key()));
- return null;
+ throw MessageException.of(String.format("Rule '%s:%s' cannot be linked on the root characteristic '%s'",
+ ruleDef.repository().name(), ruleDef.key(), key));
}
return characteristicDto;
}
import org.sonar.api.rule.RuleStatus;
import org.sonar.api.rule.Severity;
import org.sonar.api.server.rule.RuleDefinitions;
+import org.sonar.api.utils.MessageException;
import org.sonar.core.debt.db.CharacteristicDao;
import org.sonar.core.persistence.AbstractDaoTestCase;
import org.sonar.core.persistence.MyBatis;
import org.sonar.server.startup.RegisterDebtCharacteristicModel;
import static org.fest.assertions.Assertions.assertThat;
+import static org.fest.assertions.Fail.fail;
import static org.mockito.Mockito.*;
public class RuleRegistrationTest extends AbstractDaoTestCase {
}
@Test
- public void ignore_rule_debt_definitions_if_rule_is_linked_on_root_characteristic() {
+ public void fail_when_rule_is_linked_on_root_characteristic() {
setupData("ignore_rule_debt_definitions_if_rule_is_linked_on_root_characteristic");
- task.start();
- checkTables("ignore_rule_debt_definitions_if_rule_is_linked_on_root_characteristic", EXCLUDED_COLUMN_NAMES, "rules");
+ try {
+ task.start();
+ fail();
+ } catch (Exception e) {
+ assertThat(e).isInstanceOf(MessageException.class).hasMessage("Rule 'fake:rule1' cannot be linked on the root characteristic 'MEMORY_EFFICIENCY'");
+ }
}
@Test
+++ /dev/null
-<dataset>
-
- <rules id="1" plugin_rule_key="rule1" plugin_name="fake" plugin_config_key="config1" name="One" description="Description of One"
- status="READY" priority="4" cardinality="SINGLE" parent_id="[null]" language="java"
- characteristic_id="[null]" default_characteristic_id="[null]"
- remediation_function="[null]" default_remediation_function="[null]"
- remediation_factor="[null]" default_remediation_factor="[null]"
- remediation_offset="[null]" default_remediation_offset="[null]"
- effort_to_fix_l10n_key="[null]"/>
-
- <rules id="2" plugin_rule_key="rule2" plugin_name="fake" plugin_config_key="[null]" name="Two" description="Description of Two"
- status="DEPRECATED" priority="0" cardinality="SINGLE" parent_id="[null]" language="java"
- characteristic_id="[null]" default_characteristic_id="[null]"
- remediation_function="[null]" default_remediation_function="[null]"
- remediation_factor="[null]" default_remediation_factor="[null]"
- remediation_offset="[null]" default_remediation_offset="[null]"
- effort_to_fix_l10n_key="[null]"/>
-
-</dataset>