From 1cec8c236983db68a867f27cf94f9f1b8e5338c1 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Lievremont Date: Wed, 21 Jan 2015 14:26:26 +0100 Subject: [PATCH] SONAR-6019 Sort debt characteristics in api/rules/app WS --- .../org/sonar/server/rule/ws/AppAction.java | 69 +++++++++++++++---- .../sonar/server/rule/ws/AppActionTest.java | 22 ++++-- .../rule/ws/RulesWebServiceMediumTest.java | 4 +- .../server/rule/ws/AppActionTest/app.json | 6 +- 4 files changed, 78 insertions(+), 23 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/rule/ws/AppAction.java b/server/sonar-server/src/main/java/org/sonar/server/rule/ws/AppAction.java index 25564e3a618..36351a152ae 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/rule/ws/AppAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/rule/ws/AppAction.java @@ -19,7 +19,9 @@ */ package org.sonar.server.rule.ws; -import com.google.common.collect.Maps; +import com.google.common.collect.LinkedListMultimap; +import com.google.common.collect.Lists; +import com.google.common.collect.Multimap; import org.sonar.api.i18n.I18n; import org.sonar.api.resources.Language; import org.sonar.api.resources.Languages; @@ -38,8 +40,10 @@ import org.sonar.server.rule.RuleRepositories; import org.sonar.server.rule.RuleRepositories.Repository; import org.sonar.server.user.UserSession; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; import java.util.Locale; -import java.util.Map; /** * @since 4.4 @@ -129,24 +133,63 @@ public class AppAction implements RulesAction { } private void addCharacteristics(JsonWriter json) { - Map caracById = Maps.newHashMap(); - for (DebtCharacteristic carac : debtModel.allCharacteristics()) { - DefaultDebtCharacteristic fullCarac = (DefaultDebtCharacteristic) carac; - caracById.put(fullCarac.id(), fullCarac); - } + + List rootCharacs = sortRootCaracs(); + + Multimap subByCaracId = ventilateSubCaracs(rootCharacs); + json.name("characteristics").beginArray(); - for (DefaultDebtCharacteristic carac : caracById.values()) { + for (DebtCharacteristic rootCarac : rootCharacs) { json.beginObject() - .prop("key", carac.key()) - .prop("name", carac.name()); - if (carac.isSub()) { - json.prop("parent", caracById.get(carac.parentId()).key()); + .prop("key", rootCarac.key()) + .prop("name", rootCarac.name()) + .endObject(); + for (DefaultDebtCharacteristic child : sortSubCaracs(subByCaracId, rootCarac)) { + json.beginObject() + .prop("key", child.key()) + .prop("name", child.name()) + .prop("parent", rootCarac.key()) + .endObject(); } - json.endObject(); } json.endArray(); } + private List sortRootCaracs() { + List rootCharacs = Lists.newArrayList(debtModel.characteristics()); + Collections.sort(rootCharacs, new Comparator() { + @Override + public int compare(DebtCharacteristic o1, DebtCharacteristic o2) { + return o1.order().compareTo(o2.order()); + } + }); + return rootCharacs; + + } + + private Multimap ventilateSubCaracs(List rootCharacs) { + Multimap subByCaracId = LinkedListMultimap.create(rootCharacs.size()); + + for (DebtCharacteristic carac : debtModel.allCharacteristics()) { + DefaultDebtCharacteristic fullCarac = (DefaultDebtCharacteristic) carac; + if (carac.isSub()) { + subByCaracId.put(fullCarac.parentId(), fullCarac); + } + } + return subByCaracId; + } + + private List sortSubCaracs(Multimap subByCaracId, DebtCharacteristic rootCarac) { + List subCaracs = Lists.newArrayList(subByCaracId.get(((DefaultDebtCharacteristic) rootCarac).id())); + Collections.sort(subCaracs, new Comparator() { + @Override + public int compare(DefaultDebtCharacteristic o1, DefaultDebtCharacteristic o2) { + return o1.name().compareTo(o2.name()); + } + }); + return subCaracs; + } + @Override public void define(WebService.NewController controller) { controller.createAction("app") diff --git a/server/sonar-server/src/test/java/org/sonar/server/rule/ws/AppActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/rule/ws/AppActionTest.java index c54a13586f5..fee153a4d3a 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/rule/ws/AppActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/rule/ws/AppActionTest.java @@ -102,12 +102,22 @@ public class AppActionTest { } }); - int parentId = 42; - DefaultDebtCharacteristic char1 = new DefaultDebtCharacteristic(); - char1.setId(parentId).setKey("REUSABILITY").setName("Reusability"); - DefaultDebtCharacteristic char2 = new DefaultDebtCharacteristic(); - char2.setId(24).setParentId(parentId).setKey("MODULARITY").setName("Modularity"); - when(debtModel.allCharacteristics()).thenReturn(ImmutableList.of(char1, char2)); + DefaultDebtCharacteristic char1 = new DefaultDebtCharacteristic() + .setId(1).setKey("PORTABILITY").setName("Portability").setOrder(2); + DefaultDebtCharacteristic char1sub1 = new DefaultDebtCharacteristic() + .setId(11).setKey("LANGUAGE").setName("Language").setParentId(1); + DefaultDebtCharacteristic char1sub2 = new DefaultDebtCharacteristic() + .setId(12).setKey("COMPILER").setName("Compiler").setParentId(1); + DefaultDebtCharacteristic char1sub3 = new DefaultDebtCharacteristic() + .setId(13).setKey("HARDWARE").setName("Hardware").setParentId(1); + + DefaultDebtCharacteristic char2 = new DefaultDebtCharacteristic() + .setId(2).setKey("REUSABILITY").setName("Reusability").setOrder(1); + DefaultDebtCharacteristic char2sub1 = new DefaultDebtCharacteristic() + .setId(21).setKey("MODULARITY").setName("Modularity").setParentId(2); + + when(debtModel.characteristics()).thenReturn(ImmutableList.of(char1, char2)); + when(debtModel.allCharacteristics()).thenReturn(ImmutableList.of(char1sub1, char1sub2, char1sub3, char1, char2sub1, char2)); tester.newGetRequest("api/rules", "app").execute().assertJson(this.getClass(), "app.json"); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/rule/ws/RulesWebServiceMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/rule/ws/RulesWebServiceMediumTest.java index 782e4b2f526..3df679acc8c 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/rule/ws/RulesWebServiceMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/rule/ws/RulesWebServiceMediumTest.java @@ -59,9 +59,7 @@ import static org.assertj.core.api.Assertions.assertThat; public class RulesWebServiceMediumTest { @ClassRule - public static ServerTester tester = new ServerTester() - .setProperty("sonar.log.profilingLevel", "FULL") - .setProperty("sonar.search.httpPort", "9010"); + public static ServerTester tester = new ServerTester(); private static final String API_ENDPOINT = "api/rules"; private static final String API_SEARCH_METHOD = "search"; diff --git a/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/AppActionTest/app.json b/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/AppActionTest/app.json index 2fd983ef78c..d66c8682347 100644 --- a/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/AppActionTest/app.json +++ b/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/AppActionTest/app.json @@ -36,6 +36,10 @@ }, "characteristics": [ {"key": "REUSABILITY", "name": "Reusability"}, - {"key": "MODULARITY", "name": "Modularity", "parent": "REUSABILITY"} + {"key": "MODULARITY", "name": "Modularity", "parent": "REUSABILITY"}, + {"key": "PORTABILITY", "name": "Portability"}, + {"key": "COMPILER", "name": "Compiler", "parent": "PORTABILITY"}, + {"key": "HARDWARE", "name": "Hardware", "parent": "PORTABILITY"}, + {"key": "LANGUAGE", "name": "Language", "parent": "PORTABILITY"} ]} -- 2.39.5