.setOrder(characteristic.order())
.setParentId(parentId)
.setEnabled(true)
- .setCreatedAt(characteristic.createdAt())
- .setUpdatedAt(characteristic.updatedAt());
+ .setCreatedAt(((DefaultDebtCharacteristic) characteristic).createdAt())
+ .setUpdatedAt(((DefaultDebtCharacteristic) characteristic).updatedAt());
}
private static DebtCharacteristic toDebtCharacteristic(CharacteristicDto characteristic) {
return dto != null ? toCharacteristic(dto) : null;
}
+ @CheckForNull
+ public DebtCharacteristic characteristicByKey(String key) {
+ CharacteristicDto dto = dao.selectByKey(key);
+ return dto != null ? toCharacteristic(dto) : null;
+ }
+
private static List<DebtCharacteristic> toCharacteristics(Collection<CharacteristicDto> dtos) {
return newArrayList(Iterables.transform(dtos, new Function<CharacteristicDto, DebtCharacteristic>() {
@Override
return debtModelLookup.characteristicById(id);
}
+ @CheckForNull
+ public DebtCharacteristic characteristicByKey(String key) {
+ return debtModelLookup.characteristicByKey(key);
+ }
+
public DebtCharacteristic create(String name, @Nullable Integer parentId) {
return debtModelOperations.create(name, parentId);
}
import org.sonar.api.rule.RuleKey;
import org.sonar.api.server.debt.DebtCharacteristic;
import org.sonar.api.server.debt.DebtRemediationFunction;
+import org.sonar.api.server.debt.internal.DefaultDebtCharacteristic;
import org.xml.sax.InputSource;
import javax.annotation.CheckForNull;
xml.append("</" + CHARACTERISTIC_NAME + ">");
}
- if (characteristic.parentId() != null) {
+ if (characteristic.isSub()) {
List<RuleDebt> rules = rules(allRules, characteristic.key());
for (RuleDebt ruleDto : rules) {
processRule(ruleDto, xml);
public static class DebtModel {
- private Multimap<String, DebtCharacteristic> characteristicsByRootKey;
+ private Multimap<String, DebtCharacteristic> characteristicsByKey;
public DebtModel() {
- characteristicsByRootKey = ArrayListMultimap.create();
+ characteristicsByKey = ArrayListMultimap.create();
}
public DebtModel addRootCharacteristic(DebtCharacteristic characteristic) {
- characteristicsByRootKey.put(null, characteristic);
+ characteristicsByKey.put(null, characteristic);
return this;
}
public DebtModel addSubCharacteristic(DebtCharacteristic subCharacteristic, String characteristicKey) {
- characteristicsByRootKey.put(characteristicKey, subCharacteristic);
+ characteristicsByKey.put(characteristicKey, subCharacteristic);
return this;
}
* @return root characteristics sorted by order
*/
public List<DebtCharacteristic> rootCharacteristics() {
- return sortByOrder(newArrayList(characteristicsByRootKey.get(null)));
+ return sortByOrder(newArrayList(characteristicsByKey.get(null)));
}
/**
* @return root characteristics sorted by name
*/
public List<DebtCharacteristic> subCharacteristics(String characteristicKey) {
- return sortByName(newArrayList(characteristicsByRootKey.get(characteristicKey)));
+ return sortByName(newArrayList(characteristicsByKey.get(characteristicKey)));
}
@CheckForNull
public DebtCharacteristic characteristicByKey(final String key) {
- return Iterables.find(characteristicsByRootKey.values(), new Predicate<DebtCharacteristic>() {
+ return Iterables.find(characteristicsByKey.values(), new Predicate<DebtCharacteristic>() {
@Override
public boolean apply(DebtCharacteristic input) {
return key.equals(input.key());
}
public DebtCharacteristic characteristicById(final Integer id) {
- return Iterables.find(characteristicsByRootKey.values(), new Predicate<DebtCharacteristic>() {
+ return Iterables.find(characteristicsByKey.values(), new Predicate<DebtCharacteristic>() {
@Override
public boolean apply(DebtCharacteristic input) {
- return id.equals(input.id());
+ return id.equals(((DefaultDebtCharacteristic) input).id());
}
});
}
import org.sonar.api.issue.internal.DefaultIssue;
import org.sonar.api.issue.internal.FieldDiffs;
import org.sonar.api.server.debt.DebtCharacteristic;
-import org.sonar.api.server.debt.DebtModel;
+import org.sonar.api.server.debt.internal.DefaultDebtCharacteristic;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.RequestHandler;
import org.sonar.api.server.ws.Response;
import org.sonar.core.component.ComponentDto;
import org.sonar.core.issue.workflow.Transition;
import org.sonar.markdown.Markdown;
+import org.sonar.server.debt.DebtModelService;
import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.issue.ActionService;
import org.sonar.server.issue.IssueChangelog;
private final IssueService issueService;
private final IssueChangelogService issueChangelogService;
private final ActionService actionService;
- private final DebtModel debtModel;
+ private final DebtModelService debtModel;
private final I18n i18n;
private final Durations durations;
public IssueShowWsHandler(IssueFinder issueFinder, IssueService issueService, IssueChangelogService issueChangelogService, ActionService actionService,
- DebtModel debtModel, I18n i18n, Durations durations) {
+ DebtModelService debtModel, I18n i18n, Durations durations) {
this.issueFinder = issueFinder;
this.issueService = issueService;
this.issueChangelogService = issueChangelogService;
DebtCharacteristic subCharacteristic = characteristicById(subCharacteristicId);
if (subCharacteristic != null) {
json.prop("subCharacteristic", subCharacteristic.name());
- DebtCharacteristic characteristic = characteristicById(subCharacteristic.parentId());
+ DebtCharacteristic characteristic = characteristicById(((DefaultDebtCharacteristic) subCharacteristic).parentId());
json.prop("characteristic", characteristic != null ? characteristic.name() : null);
}
}
import org.sonar.server.paging.PagedResult;
import org.sonar.server.util.RubyUtils;
+import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import java.util.Map;
}
}
+ @CheckForNull
+ public Rule findByKey(String ruleKey) {
+ return rules.findByKey(RuleKey.parse(ruleKey));
+ }
+
public PagedResult<Rule> find(Map<String, Object> params) {
return rules.find(RuleQuery.builder()
.searchQuery(Strings.emptyToNull((String) params.get("searchQuery")))
import org.sonar.server.util.RubyUtils;
import org.sonar.server.util.Validation;
+import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import java.util.List;
ruleOperations.updateRuleTags(rule, newTags, UserSession.get());
}
+ @CheckForNull
public Rule findByKey(RuleKey key) {
return ruleRegistry.findByKey(key);
}
def rule
verify_ajax_request
require_parameters :id
- rule_key = params[:id].split(':')
- @rule = Rule.first(:conditions => ['plugin_name=? and plugin_rule_key=?', rule_key[0], rule_key[1]])
- characteristic_id = @rule.characteristic_id || @rule.default_characteristic_id
- if characteristic_id
- @characteristic = Internal.debt.characteristicById(characteristic_id)
- @root_characteristic = Internal.debt.characteristicById(@characteristic.parentId())
+
+ @rule = Internal.rules.findByKey(params[:id])
+ if @rule.debtCharacteristicKey()
+ @characteristic = Internal.debt.characteristicByKey(@rule.debtCharacteristicKey())
+ @sub_characteristic = Internal.debt.characteristicByKey(@rule.debtSubCharacteristicKey())
end
render :partial => 'issue/rule'
end
<p class="note">
<span class="spacer-right"><%= h @rule.plugin_name -%>:<%= h @rule.plugin_rule_key -%></span>
<%= image_tag 'sep12.png', :class => 'spacer-right' -%>
- <% if @characteristic %>
- <%= @root_characteristic.name -%> > <%= @characteristic.name -%>
+ <% if @characteristic && @sub_characteristic %>
+ <%= @characteristic.name -%> > <%= @sub_characteristic.name -%>
<% else %>
<%= message 'issue.technical_debt_deleted' %>
<% end %>
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
-import org.sonar.api.server.debt.DebtCharacteristic;
+import org.sonar.api.server.debt.internal.DefaultDebtCharacteristic;
import org.sonar.core.technicaldebt.db.CharacteristicDao;
import org.sonar.core.technicaldebt.db.CharacteristicDto;
public void find_characteristic_by_id() {
when(dao.selectById(1)).thenReturn(characteristicDto);
- DebtCharacteristic characteristic = service.characteristicById(1);
+ DefaultDebtCharacteristic characteristic = (DefaultDebtCharacteristic) service.characteristicById(1);
assertThat(characteristic.id()).isEqualTo(1);
assertThat(characteristic.key()).isEqualTo("MEMORY_EFFICIENCY");
assertThat(characteristic.name()).isEqualTo("Memory use");
assertThat(service.characteristicById(111)).isNull();
}
+ @Test
+ public void find_characteristic_by_key() {
+ when(dao.selectByKey("MEMORY_EFFICIENCY")).thenReturn(characteristicDto);
+
+ DefaultDebtCharacteristic characteristic = (DefaultDebtCharacteristic) service.characteristicByKey("MEMORY_EFFICIENCY");
+ assertThat(characteristic.id()).isEqualTo(1);
+ assertThat(characteristic.key()).isEqualTo("MEMORY_EFFICIENCY");
+ assertThat(characteristic.name()).isEqualTo("Memory use");
+ assertThat(characteristic.order()).isEqualTo(2);
+ assertThat(characteristic.parentId()).isNull();
+
+ assertThat(service.characteristicByKey("UNKNOWN")).isNull();
+ }
+
}
import org.mockito.runners.MockitoJUnitRunner;
import org.mockito.stubbing.Answer;
import org.sonar.api.server.debt.DebtCharacteristic;
+import org.sonar.api.server.debt.internal.DefaultDebtCharacteristic;
import org.sonar.api.utils.DateUtils;
import org.sonar.api.utils.System2;
import org.sonar.core.permission.GlobalPermissions;
public void create_sub_characteristic() {
when(dao.selectById(1, session)).thenReturn(characteristicDto);
- DebtCharacteristic result = service.create("Compilation name", 1);
+ DefaultDebtCharacteristic result = (DefaultDebtCharacteristic) service.create("Compilation name", 1);
assertThat(result.id()).isEqualTo(currentId);
assertThat(result.key()).isEqualTo("COMPILATION_NAME");
public void create_characteristic() {
when(dao.selectMaxCharacteristicOrder(session)).thenReturn(2);
- DebtCharacteristic result = service.create("Portability", null);
+ DefaultDebtCharacteristic result = (DefaultDebtCharacteristic) service.create("Portability", null);
assertThat(result.id()).isEqualTo(currentId);
assertThat(result.key()).isEqualTo("PORTABILITY");
public void create_first_characteristic() {
when(dao.selectMaxCharacteristicOrder(session)).thenReturn(0);
- DebtCharacteristic result = service.create("Portability", null);
+ DefaultDebtCharacteristic result = (DefaultDebtCharacteristic) service.create("Portability", null);
assertThat(result.id()).isEqualTo(currentId);
assertThat(result.key()).isEqualTo("PORTABILITY");
public void rename_characteristic() {
when(dao.selectById(10, session)).thenReturn(subCharacteristicDto);
- DebtCharacteristic result = service.rename(10, "New Efficiency");
+ DefaultDebtCharacteristic result = (DefaultDebtCharacteristic) service.rename(10, "New Efficiency");
assertThat(result.key()).isEqualTo("EFFICIENCY");
assertThat(result.name()).isEqualTo("New Efficiency");
verify(debtModelLookup).characteristicById(111);
}
+ @Test
+ public void find_characteristic_by_key() {
+ service.characteristicByKey("MEMORY_EFFICIENCY");
+ verify(debtModelLookup).characteristicByKey("MEMORY_EFFICIENCY");
+ }
+
@Test
public void create_characteristic() {
service.create("Compilation name", 1);
import org.sonar.api.issue.internal.FieldDiffs;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rules.Rule;
-import org.sonar.api.server.debt.DebtModel;
import org.sonar.api.server.debt.internal.DefaultDebtCharacteristic;
import org.sonar.api.server.ws.WsTester;
import org.sonar.api.user.User;
import org.sonar.core.issue.DefaultIssueQueryResult;
import org.sonar.core.issue.workflow.Transition;
import org.sonar.core.user.DefaultUser;
+import org.sonar.server.debt.DebtModelService;
import org.sonar.server.issue.ActionService;
import org.sonar.server.issue.IssueChangelog;
import org.sonar.server.issue.IssueChangelogService;
ActionService actionService;
@Mock
- DebtModel debtModel;
+ DebtModelService debtModel;
@Mock
I18n i18n;
verify(rules).updateRuleTags(10, ImmutableList.of("tag1", "tag2"));
}
+ @Test
+ public void find_by_key() {
+ facade.findByKey("repo:key");
+ verify(rules).findByKey(RuleKey.of("repo", "key"));
+ }
+
@Test
public void find_by_params() {
Map<String, Object> params = newHashMap();