package org.sonar.core.technicaldebt;
+
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rules.Rule;
import org.sonar.api.rules.RuleFinder;
CharacteristicDto requirementDto = dao.selectByRuleId(ruleId);
if (requirementDto != null) {
Rule rule = ruleFinder.findById(ruleId);
+ if (rule == null) {
+ throw new IllegalArgumentException(String.format("Rule with id '%s' do not exists.", ruleId));
+ }
return toCharacteristic(requirementDto, RuleKey.of(rule.getRepositoryKey(), rule.getKey()));
}
return null;
import static com.google.common.collect.Lists.newArrayList;
import static org.fest.assertions.Assertions.assertThat;
+import static org.fest.assertions.Fail.fail;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
assertThat(finder.findRequirementByRuleId(1)).isNull();
}
+
+ @Test
+ public void fail_to_find_requirement_by_rule_id_if_unknown_rule_id() throws Exception {
+ when(dao.selectByRuleId(1)).thenReturn(
+ new CharacteristicDto().setId(3).setRuleId(10).setParentId(2).setRootId(1).setFunction("linear").setFactorValue(30.0).setFactorUnit("mn"));
+ when(ruleFinder.findById(1)).thenReturn(null);
+ try {
+ finder.findRequirementByRuleId(1);
+ fail();
+ } catch (Exception e) {
+ assertThat(e).isInstanceOf(IllegalArgumentException.class);
+ }
+ }
}