]> source.dussan.org Git - sonarqube.git/commitdiff
Update server DebtModel API
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 3 Apr 2014 15:52:22 +0000 (17:52 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 3 Apr 2014 15:52:32 +0000 (17:52 +0200)
14 files changed:
sonar-server/src/main/java/org/sonar/server/debt/DebtModelBackup.java
sonar-server/src/main/java/org/sonar/server/debt/DebtModelLookup.java
sonar-server/src/main/java/org/sonar/server/debt/DebtModelService.java
sonar-server/src/main/java/org/sonar/server/debt/DebtModelXMLExporter.java
sonar-server/src/main/java/org/sonar/server/issue/ws/IssueShowWsHandler.java
sonar-server/src/main/java/org/sonar/server/rule/RubyRuleService.java
sonar-server/src/main/java/org/sonar/server/rule/Rules.java
sonar-server/src/main/webapp/WEB-INF/app/controllers/issue_controller.rb
sonar-server/src/main/webapp/WEB-INF/app/views/issue/_rule.html.erb
sonar-server/src/test/java/org/sonar/server/debt/DebtModelLookupTest.java
sonar-server/src/test/java/org/sonar/server/debt/DebtModelOperationsTest.java
sonar-server/src/test/java/org/sonar/server/debt/DebtModelServiceTest.java
sonar-server/src/test/java/org/sonar/server/issue/ws/IssueShowWsHandlerTest.java
sonar-server/src/test/java/org/sonar/server/rule/RubyRuleServiceTest.java

index bf0e9c7ca7695edd62aa7759444814edb619714f..aeae487b8b9bf7f15849b4b7b1845047b0584594 100644 (file)
@@ -408,8 +408,8 @@ public class DebtModelBackup implements ServerComponent {
       .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) {
index 6d74c63641c1cff64a2734f9fc302f97851e0b90..fbfeb0855a6caafbbbe89b2e0fddb4eadf33cbf6 100644 (file)
@@ -58,6 +58,12 @@ public class DebtModelLookup implements ServerComponent {
     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
index a3c85737a0b55328eb844269a0d605578462337c..db4cd78bbe4d00b5c38ac2015ccd87ff68db7216 100644 (file)
@@ -58,6 +58,11 @@ public class DebtModelService implements DebtModel {
     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);
   }
index 8a39f48fcb27c859934c06c6432f6d43985622a9..a10081174e96a51045453e1dcc6099da25dab7b0 100644 (file)
@@ -30,6 +30,7 @@ import org.sonar.api.ServerComponent;
 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;
@@ -93,7 +94,7 @@ public class DebtModelXMLExporter implements ServerComponent {
       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);
@@ -188,19 +189,19 @@ public class DebtModelXMLExporter implements ServerComponent {
 
   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;
     }
 
@@ -208,19 +209,19 @@ public class DebtModelXMLExporter implements ServerComponent {
      * @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());
@@ -229,10 +230,10 @@ public class DebtModelXMLExporter implements ServerComponent {
     }
 
     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());
         }
       });
     }
index e41a035ea08150fc99e9c2f7cfd7fc307a25b826..d32cf0202af07d80450221b35dcc6c855e9cbacd 100644 (file)
@@ -28,7 +28,7 @@ import org.sonar.api.issue.action.Action;
 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;
@@ -41,6 +41,7 @@ import org.sonar.api.web.UserRole;
 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;
@@ -63,12 +64,12 @@ public class IssueShowWsHandler implements RequestHandler {
   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;
@@ -193,7 +194,7 @@ public class IssueShowWsHandler implements RequestHandler {
     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);
     }
   }
index 4ced6efd70cd5c237ecca2756b8531dcf00fef6f..6d4d445fc9f39cd43d043720946f00f8f9c52993 100644 (file)
@@ -28,6 +28,7 @@ import org.sonar.api.rule.RuleKey;
 import org.sonar.server.paging.PagedResult;
 import org.sonar.server.util.RubyUtils;
 
+import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
 
 import java.util.Map;
@@ -63,6 +64,11 @@ public class RubyRuleService implements ServerComponent, Startable {
     }
   }
 
+  @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")))
index 25405d662074d7989ba3bea9c9a6b2f7b8ad1e81..e4840358e739667b0f9c593103dd53d2f84aadd4 100644 (file)
@@ -33,6 +33,7 @@ import org.sonar.server.user.UserSession;
 import org.sonar.server.util.RubyUtils;
 import org.sonar.server.util.Validation;
 
+import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
 
 import java.util.List;
@@ -107,6 +108,7 @@ public class Rules implements ServerExtension {
     ruleOperations.updateRuleTags(rule, newTags, UserSession.get());
   }
 
+  @CheckForNull
   public Rule findByKey(RuleKey key) {
     return ruleRegistry.findByKey(key);
   }
index 43542de752048f66b038af6f6b6252b3e94da8c3..9d37e5ac83e059ea7c748e6081518cda68e2adca 100644 (file)
@@ -185,12 +185,11 @@ class IssueController < ApplicationController
   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
index f466660d508f4dcbd5c9bbf80ea6372bb3590663..9214ce50513db944cc3cdab12e232a8ceb6fd300 100644 (file)
@@ -17,8 +17,8 @@
 <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 -%>&nbsp;&gt;&nbsp;<%= @characteristic.name -%>
+  <% if @characteristic && @sub_characteristic %>
+    <%= @characteristic.name -%>&nbsp;&gt;&nbsp;<%= @sub_characteristic.name -%>
   <% else %>
     <%= message 'issue.technical_debt_deleted' %>
   <% end %>
index eedc79f38affdef8e3a59042e10a6646834aff52..a0390191cc99a1f67c07afd42a7853a8b140e123 100644 (file)
@@ -25,7 +25,7 @@ import org.junit.Test;
 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;
 
@@ -69,7 +69,7 @@ public class DebtModelLookupTest {
   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");
@@ -79,4 +79,18 @@ public class DebtModelLookupTest {
     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();
+  }
+
 }
index baf1f8bd0603439c67e9151639ec04fa8a58c976..8b871dce3f29c8d275aab1cf79a657943fcfbdc9 100644 (file)
@@ -31,6 +31,7 @@ import org.mockito.invocation.InvocationOnMock;
 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;
@@ -126,7 +127,7 @@ public class DebtModelOperationsTest {
   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");
@@ -189,7 +190,7 @@ public class DebtModelOperationsTest {
   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");
@@ -202,7 +203,7 @@ public class DebtModelOperationsTest {
   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");
@@ -215,7 +216,7 @@ public class DebtModelOperationsTest {
   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");
index 404c1e83c3b54bb65bab1341920cd9089615a407..e5309e9f8d8856f89ff49a4f3d752f53bb6eb4fa 100644 (file)
@@ -64,6 +64,12 @@ public class DebtModelServiceTest {
     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);
index 1624b8704f0bc71f5cb1f9c4b18c5252c32091d4..9f21f8646ccce9e61df4aef24fc4ce63fe965aaf 100644 (file)
@@ -37,7 +37,6 @@ import org.sonar.api.issue.internal.DefaultIssueComment;
 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;
@@ -50,6 +49,7 @@ import org.sonar.core.issue.DefaultActionPlan;
 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;
@@ -84,7 +84,7 @@ public class IssueShowWsHandlerTest {
   ActionService actionService;
 
   @Mock
-  DebtModel debtModel;
+  DebtModelService debtModel;
 
   @Mock
   I18n i18n;
index 89b12ef17d33d37312e7a223a77926e19d6f7f8b..59c1cdcc04e3920d385ca47846af180184266335 100644 (file)
@@ -129,6 +129,12 @@ public class RubyRuleServiceTest {
     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();