]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6019 Sort debt characteristics in api/rules/app WS
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Wed, 21 Jan 2015 13:26:26 +0000 (14:26 +0100)
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Wed, 21 Jan 2015 13:26:26 +0000 (14:26 +0100)
server/sonar-server/src/main/java/org/sonar/server/rule/ws/AppAction.java
server/sonar-server/src/test/java/org/sonar/server/rule/ws/AppActionTest.java
server/sonar-server/src/test/java/org/sonar/server/rule/ws/RulesWebServiceMediumTest.java
server/sonar-server/src/test/resources/org/sonar/server/rule/ws/AppActionTest/app.json

index 25564e3a618493148ed29fc64e22fc682b27c180..36351a152ae1a01d0b650f7ae0caa1f3f11816cf 100644 (file)
@@ -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<Integer, DefaultDebtCharacteristic> caracById = Maps.newHashMap();
-    for (DebtCharacteristic carac : debtModel.allCharacteristics()) {
-      DefaultDebtCharacteristic fullCarac = (DefaultDebtCharacteristic) carac;
-      caracById.put(fullCarac.id(), fullCarac);
-    }
+
+    List<DebtCharacteristic> rootCharacs = sortRootCaracs();
+
+    Multimap<Integer, DefaultDebtCharacteristic> 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<DebtCharacteristic> sortRootCaracs() {
+    List<DebtCharacteristic> rootCharacs = Lists.newArrayList(debtModel.characteristics());
+    Collections.sort(rootCharacs, new Comparator<DebtCharacteristic>() {
+      @Override
+      public int compare(DebtCharacteristic o1, DebtCharacteristic o2) {
+        return o1.order().compareTo(o2.order());
+      }
+    });
+    return rootCharacs;
+
+  }
+
+  private Multimap<Integer, DefaultDebtCharacteristic> ventilateSubCaracs(List<DebtCharacteristic> rootCharacs) {
+    Multimap<Integer, DefaultDebtCharacteristic> 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<DefaultDebtCharacteristic> sortSubCaracs(Multimap<Integer, DefaultDebtCharacteristic> subByCaracId, DebtCharacteristic rootCarac) {
+    List<DefaultDebtCharacteristic> subCaracs = Lists.newArrayList(subByCaracId.get(((DefaultDebtCharacteristic) rootCarac).id()));
+    Collections.sort(subCaracs, new Comparator<DefaultDebtCharacteristic>() {
+      @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")
index c54a13586f5f8cd8bf55ffdedd5249f7206cc149..fee153a4d3afddcaded7531f1bc5c4f586555d59 100644 (file)
@@ -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.<DebtCharacteristic>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.<DebtCharacteristic>of(char1, char2));
+    when(debtModel.allCharacteristics()).thenReturn(ImmutableList.<DebtCharacteristic>of(char1sub1, char1sub2, char1sub3, char1, char2sub1, char2));
 
     tester.newGetRequest("api/rules", "app").execute().assertJson(this.getClass(), "app.json");
   }
index 782e4b2f52622b898eb475c346012ab2db59382e..3df679acc8c169560b264eb2478b06fc2ed44e2f 100644 (file)
@@ -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";
index 2fd983ef78c5063a71b060e51d368b9a1d64140e..d66c86823471a92452ce223185cfb0f5060b844c 100644 (file)
   },
   "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"}
   ]}