]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5007 - Returning name of effectiveChar in Issue Show Action WS
authorStephane Gamard <stephane.gamard@searchbox.com>
Mon, 30 Jun 2014 12:44:48 +0000 (14:44 +0200)
committerStephane Gamard <stephane.gamard@searchbox.com>
Mon, 30 Jun 2014 12:46:44 +0000 (14:46 +0200)
sonar-plugin-api/src/main/java/org/sonar/api/rules/Rule.java
sonar-server/src/main/java/org/sonar/server/issue/ws/IssueShowAction.java
sonar-server/src/main/java/org/sonar/server/rule/DefaultRuleFinder.java
sonar-server/src/test/java/org/sonar/server/issue/ws/IssueShowActionTest.java

index b783ac11844e9a0f7d0a2740fd25738b367960a0..0b7ac65d5c7b8ac7d8e80078464675ce5f2554f8 100644 (file)
@@ -151,13 +151,13 @@ public class Rule {
   private Date updatedAt;
 
   @Transient
-  private String defaultCharacteristic;
+  private String defaultCharacteristicKey;
   @Transient
-  private String defaultSubCharacteristic;
+  private String defaultSubCharacteristicKey;
   @Transient
-  private String characteristic;
+  private String characteristicKey;
   @Transient
-  private String subCharacteristic;
+  private String subCharacteristicKey;
 
   private transient String[] tags = DEFAULT_TAGS;
 
@@ -518,7 +518,7 @@ public class Rule {
   /**
    * For internal use only.
    *
-   * @deprecated since 4.4, use {@link #getCharacteristic()}
+   * @deprecated since 4.4, use {@link #getCharacteristicKey()}
    * @since 4.3
    */
   @CheckForNull
@@ -530,7 +530,7 @@ public class Rule {
   /**
    * For internal use only.
    *
-   * @deprecated since 4.4, use {@link #setCharacteristic(@Nullable String characteristic)}
+   * @deprecated since 4.4, use {@link #setCharacteristicKey(@Nullable String characteristicKey)}
    * @since 4.3
    */
   @Deprecated
@@ -542,7 +542,7 @@ public class Rule {
   /**
    * For internal use only.
    *
-   * @deprecated since 4.4, use {@link #getDefaultCharacteristic()}
+   * @deprecated since 4.4, use {@link #getDefaultCharacteristicKey()}
    * @since 4.3
    */
   @CheckForNull
@@ -554,7 +554,7 @@ public class Rule {
   /**
    * For internal use only.
    *
-   * @deprecated since 4.4, use {@link #setDefaultCharacteristic(@Nullable String defaultCharacteristic)}
+   * @deprecated since 4.4, use {@link #setDefaultCharacteristicKey(@Nullable String defaultCharacteristicKey)}
    * @since 4.3
    */
   @Deprecated
@@ -643,15 +643,15 @@ public class Rule {
    * @since 4.4
    */
   @CheckForNull
-  public String getDefaultCharacteristic() {
-    return defaultCharacteristic;
+  public String getDefaultCharacteristicKey() {
+    return defaultCharacteristicKey;
   }
 
   /**
    * @since 4.4
    */
-  public Rule setDefaultCharacteristic(@Nullable String defaultCharacteristic) {
-    this.defaultCharacteristic = defaultCharacteristic;
+  public Rule setDefaultCharacteristicKey(@Nullable String defaultCharacteristicKey) {
+    this.defaultCharacteristicKey = defaultCharacteristicKey;
     return this;
   }
 
@@ -659,15 +659,15 @@ public class Rule {
    * @since 4.4
    */
   @CheckForNull
-  public String getDefaultSubCharacteristic() {
-    return defaultSubCharacteristic;
+  public String getDefaultSubCharacteristicKey() {
+    return defaultSubCharacteristicKey;
   }
 
   /**
    * @since 4.4
    */
-  public Rule setDefaultSubCharacteristic(@Nullable String defaultSubCharacteristic) {
-    this.defaultSubCharacteristic = defaultSubCharacteristic;
+  public Rule setDefaultSubCharacteristicKey(@Nullable String defaultSubCharacteristicKey) {
+    this.defaultSubCharacteristicKey = defaultSubCharacteristicKey;
     return this;
   }
 
@@ -675,15 +675,15 @@ public class Rule {
    * @since 4.4
    */
   @CheckForNull
-  public String getCharacteristic() {
-    return characteristic;
+  public String getCharacteristicKey() {
+    return characteristicKey;
   }
 
   /**
    * @since 4.4
    */
-  public Rule setCharacteristic(@Nullable String characteristic) {
-    this.characteristic = characteristic;
+  public Rule setCharacteristicKey(@Nullable String characteristicKey) {
+    this.characteristicKey = characteristicKey;
     return this;
   }
 
@@ -691,15 +691,15 @@ public class Rule {
    * @since 4.4
    */
   @CheckForNull
-  public String getSubCharacteristic() {
-    return subCharacteristic;
+  public String getSubCharacteristicKey() {
+    return subCharacteristicKey;
   }
 
   /**
    * @since 4.4
    */
-  public Rule setSubCharacteristic(@Nullable String subCharacteristic) {
-    this.subCharacteristic = subCharacteristic;
+  public Rule setSubCharacteristicKey(@Nullable String subCharacteristicKey) {
+    this.subCharacteristicKey = subCharacteristicKey;
     return this;
   }
 }
index 3936daf64f810e93e434f64f3e11e50b4d705f72..3a50e6f8616de5ce115990d99acffab630979135 100644 (file)
@@ -32,6 +32,8 @@ import org.sonar.api.issue.IssueQuery;
 import org.sonar.api.issue.IssueQueryResult;
 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.internal.DefaultDebtCharacteristic;
 import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.RequestHandler;
 import org.sonar.api.server.ws.Response;
@@ -273,12 +275,28 @@ public class IssueShowAction implements RequestHandler {
   }
 
   private void addCharacteristics(IssueQueryResult result, DefaultIssue issue, JsonWriter json) {
-    String subCharacteristic = result.rule(issue).getSubCharacteristic() != null ? result.rule(issue).getSubCharacteristic() : result.rule(issue).getDefaultSubCharacteristic();
-    String characteristic = result.rule(issue).getCharacteristic() != null ? result.rule(issue).getCharacteristic() : result.rule(issue).getDefaultCharacteristic();
-
+    String subCharacteristicKey = result.rule(issue).getCharacteristicKey() != null ? result.rule(issue).getCharacteristicKey() : result.rule(issue).getDefaultCharacteristicKey();
+    DebtCharacteristic subCharacteristic = characteristicByKey(subCharacteristicKey);
     if (subCharacteristic != null) {
-      json.prop("subCharacteristic", subCharacteristic);
-      json.prop("characteristic", characteristic != null ? characteristic : null);
+      json.prop("subCharacteristic", subCharacteristic.name());
+      DebtCharacteristic characteristic = characteristicById(((DefaultDebtCharacteristic) subCharacteristic).parentId());
+      json.prop("characteristic", characteristic != null ? characteristic.name() : null);
+    }
+  }
+
+  @CheckForNull
+  private DebtCharacteristic characteristicById(@Nullable Integer id) {
+    if (id != null) {
+      return debtModel.characteristicById(id);
     }
+    return null;
+  }
+
+  @CheckForNull
+  private DebtCharacteristic characteristicByKey(@Nullable String key) {
+    if (key != null) {
+      return debtModel.characteristicByKey(key);
+    }
+    return null;
   }
 }
index 00fa7a8c72a18070456dfcac344de9e1dedb4d53..d916cb1314595c181d633c18c087a0fa660d6736 100644 (file)
@@ -116,10 +116,10 @@ public class DefaultRuleFinder implements RuleFinder {
       .setRepositoryKey(rule.key().repository())
       .setSeverity(RulePriority.valueOf(rule.severity()))
       .setStatus(rule.status().name())
-      .setDefaultCharacteristic(rule.defaultDebtCharacteristicKey())
-      .setDefaultSubCharacteristic(rule.defaultDebtSubCharacteristicKey())
-      .setCharacteristic(rule.debtCharacteristicKey())
-      .setSubCharacteristic(rule.debtSubCharacteristicKey())
+      .setDefaultCharacteristicKey(rule.defaultDebtCharacteristicKey())
+      .setDefaultSubCharacteristicKey(rule.defaultDebtSubCharacteristicKey())
+      .setCharacteristicKey(rule.debtCharacteristicKey())
+      .setSubCharacteristicKey(rule.debtSubCharacteristicKey())
       .setTags(rule.tags().toArray(new String[rule.tags().size()]));
 
     List<org.sonar.api.rules.RuleParam> apiParams = newArrayList();
index 57ff3ea82a4677ca1d7f860f8621330f5513dda4..67dd5e8c1e7ca9642fe20177c1c0e7a4688b0e86 100644 (file)
@@ -322,9 +322,10 @@ public class IssueShowActionTest {
     Issue issue = createStandardIssue().setDebt(Duration.create(7260L));
     issues.add(issue);
 
-    result.rule(issue).setCharacteristicId(2);
-    when(debtModel.characteristicById(1)).thenReturn(new DefaultDebtCharacteristic().setId(1).setName("Maintainability"));
-    when(debtModel.characteristicById(2)).thenReturn(new DefaultDebtCharacteristic().setId(2).setName("Readability").setParentId(1));
+    result.rule(issue).setCharacteristicKey("K2");
+    when(debtModel.characteristicById(1)).thenReturn(new DefaultDebtCharacteristic().setKey("K1").setId(1).setName("Maintainability"));
+    when(debtModel.characteristicById(2)).thenReturn(new DefaultDebtCharacteristic().setKey("K2").setId(2).setName("Readability").setParentId(1));
+    when(debtModel.characteristicByKey("K2")).thenReturn(new DefaultDebtCharacteristic().setKey("K2").setId(2).setName("Readability").setParentId(1));
 
     MockUserSession.set();
     WsTester.TestRequest request = tester.newGetRequest("api/issues", "show").setParam("key", issue.key());
@@ -336,12 +337,14 @@ public class IssueShowActionTest {
     Issue issue = createStandardIssue().setDebt(Duration.create(7260L));
     issues.add(issue);
 
-    result.rule(issue).setDefaultCharacteristicId(2);
-    when(debtModel.characteristicById(1)).thenReturn(new DefaultDebtCharacteristic().setId(1).setName("Maintainability"));
-    when(debtModel.characteristicById(2)).thenReturn(new DefaultDebtCharacteristic().setId(2).setName("Readability").setParentId(1));
+    result.rule(issue).setDefaultCharacteristicKey("K2");
+    when(debtModel.characteristicById(1)).thenReturn(new DefaultDebtCharacteristic().setKey("K1").setId(1).setName("Maintainability"));
+    when(debtModel.characteristicById(2)).thenReturn(new DefaultDebtCharacteristic().setKey("K2").setId(2).setName("Readability").setParentId(1));
+    when(debtModel.characteristicByKey("K2")).thenReturn(new DefaultDebtCharacteristic().setKey("K2").setId(2).setName("Readability").setParentId(1));
 
     MockUserSession.set();
     WsTester.TestRequest request = tester.newGetRequest("api/issues", "show").setParam("key", issue.key());
+    System.out.println("request.execute().outputAsString() = " + request.execute().outputAsString());
     request.execute().assertJson(getClass(), "show_issue_with_characteristics.json");
   }
 
@@ -350,13 +353,15 @@ public class IssueShowActionTest {
     Issue issue = createStandardIssue().setDebt(Duration.create(7260L));
     issues.add(issue);
 
-    result.rule(issue).setCharacteristicId(2);
-    when(debtModel.characteristicById(1)).thenReturn(new DefaultDebtCharacteristic().setId(1).setName("Maintainability"));
-    when(debtModel.characteristicById(2)).thenReturn(new DefaultDebtCharacteristic().setId(2).setName("Readability").setParentId(1));
+    result.rule(issue).setCharacteristicKey("K2");
+    when(debtModel.characteristicById(1)).thenReturn(new DefaultDebtCharacteristic().setKey("K1").setId(1).setName("Maintainability"));
+    when(debtModel.characteristicById(2)).thenReturn(new DefaultDebtCharacteristic().setKey("K2").setId(2).setName("Readability").setParentId(1));
+    when(debtModel.characteristicByKey("K2")).thenReturn(new DefaultDebtCharacteristic().setKey("K2").setId(2).setName("Readability").setParentId(1));
 
-    result.rule(issue).setDefaultCharacteristicId(20);
-    when(debtModel.characteristicById(10)).thenReturn(new DefaultDebtCharacteristic().setId(10).setName("Default Maintainability"));
-    when(debtModel.characteristicById(20)).thenReturn(new DefaultDebtCharacteristic().setId(20).setName("Default Readability").setParentId(10));
+    result.rule(issue).setDefaultCharacteristicKey("K20");
+    when(debtModel.characteristicById(10)).thenReturn(new DefaultDebtCharacteristic().setKey("K10").setId(10).setName("Default Maintainability"));
+    when(debtModel.characteristicById(20)).thenReturn(new DefaultDebtCharacteristic().setKey("K20").setId(20).setName("Default Readability").setParentId(10));
+    when(debtModel.characteristicByKey("K20")).thenReturn(new DefaultDebtCharacteristic().setKey("K20").setId(20).setName("Default Readability").setParentId(10));
 
     MockUserSession.set();
     WsTester.TestRequest request = tester.newGetRequest("api/issues", "show").setParam("key", issue.key());