From 219681b5f2007a7ac88528ea78260c536b68e9d4 Mon Sep 17 00:00:00 2001 From: Stephane Gamard Date: Mon, 30 Jun 2014 14:44:48 +0200 Subject: [PATCH] SONAR-5007 - Returning name of effectiveChar in Issue Show Action WS --- .../main/java/org/sonar/api/rules/Rule.java | 48 +++++++++---------- .../server/issue/ws/IssueShowAction.java | 28 +++++++++-- .../sonar/server/rule/DefaultRuleFinder.java | 8 ++-- .../server/issue/ws/IssueShowActionTest.java | 29 ++++++----- 4 files changed, 68 insertions(+), 45 deletions(-) diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/rules/Rule.java b/sonar-plugin-api/src/main/java/org/sonar/api/rules/Rule.java index b783ac11844..0b7ac65d5c7 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/rules/Rule.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/rules/Rule.java @@ -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; } } diff --git a/sonar-server/src/main/java/org/sonar/server/issue/ws/IssueShowAction.java b/sonar-server/src/main/java/org/sonar/server/issue/ws/IssueShowAction.java index 3936daf64f8..3a50e6f8616 100644 --- a/sonar-server/src/main/java/org/sonar/server/issue/ws/IssueShowAction.java +++ b/sonar-server/src/main/java/org/sonar/server/issue/ws/IssueShowAction.java @@ -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; } } diff --git a/sonar-server/src/main/java/org/sonar/server/rule/DefaultRuleFinder.java b/sonar-server/src/main/java/org/sonar/server/rule/DefaultRuleFinder.java index 00fa7a8c72a..d916cb13145 100644 --- a/sonar-server/src/main/java/org/sonar/server/rule/DefaultRuleFinder.java +++ b/sonar-server/src/main/java/org/sonar/server/rule/DefaultRuleFinder.java @@ -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 apiParams = newArrayList(); diff --git a/sonar-server/src/test/java/org/sonar/server/issue/ws/IssueShowActionTest.java b/sonar-server/src/test/java/org/sonar/server/issue/ws/IssueShowActionTest.java index 57ff3ea82a4..67dd5e8c1e7 100644 --- a/sonar-server/src/test/java/org/sonar/server/issue/ws/IssueShowActionTest.java +++ b/sonar-server/src/test/java/org/sonar/server/issue/ws/IssueShowActionTest.java @@ -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()); -- 2.39.5