]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7135 WS api/measures/component_tree sort by metric consistent for component... 784/head
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Thu, 18 Feb 2016 15:05:24 +0000 (16:05 +0100)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Thu, 18 Feb 2016 15:05:24 +0000 (16:05 +0100)
server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeSort.java
server/sonar-server/src/test/java/org/sonar/server/measure/ws/ComponentTreeSortTest.java

index b99ec27a3b316c090ca14a60dd6bda2b04089165..6c136b2f4904a0aa6c00574bdc762741b462db2e 100644 (file)
@@ -87,13 +87,12 @@ class ComponentTreeSort {
   }
 
   private static Ordering<ComponentDtoWithSnapshotId> stringOrdering(boolean isAscending, Function<ComponentDtoWithSnapshotId, String> function) {
-    Ordering<String> ordering = Ordering.from(CASE_INSENSITIVE_ORDER)
-      .nullsLast();
+    Ordering<String> ordering = Ordering.from(CASE_INSENSITIVE_ORDER);
     if (!isAscending) {
       ordering = ordering.reverse();
     }
 
-    return ordering.onResultOf(function);
+    return ordering.nullsLast().onResultOf(function);
   }
 
   /**
@@ -129,14 +128,13 @@ class ComponentTreeSort {
 
   private static Ordering<ComponentDtoWithSnapshotId> numericalMetricOrdering(boolean isAscending, @Nullable MetricDto metric,
     Table<String, MetricDto, MeasureDto> measuresByComponentUuidAndMetric) {
-    Ordering<Double> ordering = Ordering.natural()
-      .nullsLast();
+    Ordering<Double> ordering = Ordering.natural();
 
     if (!isAscending) {
       ordering = ordering.reverse();
     }
 
-    return ordering.onResultOf(new ComponentDtoWithSnapshotIdToNumericalMeasureValue(metric, measuresByComponentUuidAndMetric));
+    return ordering.nullsLast().onResultOf(new ComponentDtoWithSnapshotIdToNumericalMeasureValue(metric, measuresByComponentUuidAndMetric));
   }
 
   private static class ComponentDtoWithSnapshotIdToNumericalMeasureValue implements Function<ComponentDtoWithSnapshotId, Double> {
index eed4d78fc97632dd0b91e82c0e5dfa7d54edfbe2..13439ef9655761aeb8c6245e0ffa0051cd9086a2 100644 (file)
@@ -111,23 +111,47 @@ public class ComponentTreeSortTest {
   }
 
   @Test
-  public void sort_by_numerical_metric_key() {
+  public void sort_by_numerical_metric_key_ascending() {
+    components.add(newComponentWithoutSnapshotId("name-without-measure", "qualifier-without-measure", "path-without-measure"));
     ComponentTreeWsRequest wsRequest = newRequest(singletonList(METRIC_SORT), true, NUM_METRIC_KEY);
 
     List<ComponentDtoWithSnapshotId> result = sortComponents(wsRequest);
 
     assertThat(result).extracting("path")
-      .containsExactly("path-1", "path-2", "path-3", "path-4", "path-5", "path-6", "path-7", "path-8", "path-9");
+      .containsExactly("path-1", "path-2", "path-3", "path-4", "path-5", "path-6", "path-7", "path-8", "path-9", "path-without-measure");
+  }
+
+  @Test
+  public void sort_by_numerical_metric_key_descending() {
+    components.add(newComponentWithoutSnapshotId("name-without-measure", "qualifier-without-measure", "path-without-measure"));
+    ComponentTreeWsRequest wsRequest = newRequest(singletonList(METRIC_SORT), false, NUM_METRIC_KEY);
+
+    List<ComponentDtoWithSnapshotId> result = sortComponents(wsRequest);
+
+    assertThat(result).extracting("path")
+      .containsExactly("path-9", "path-8", "path-7", "path-6", "path-5", "path-4", "path-3", "path-2", "path-1", "path-without-measure");
   }
 
   @Test
-  public void sort_by_textual_metric_key() {
+  public void sort_by_textual_metric_key_ascending() {
+    components.add(newComponentWithoutSnapshotId("name-without-measure", "qualifier-without-measure", "path-without-measure"));
     ComponentTreeWsRequest wsRequest = newRequest(singletonList(METRIC_SORT), true, TEXT_METRIC_KEY);
 
     List<ComponentDtoWithSnapshotId> result = sortComponents(wsRequest);
 
     assertThat(result).extracting("path")
-      .containsExactly("path-1", "path-2", "path-3", "path-4", "path-5", "path-6", "path-7", "path-8", "path-9");
+      .containsExactly("path-1", "path-2", "path-3", "path-4", "path-5", "path-6", "path-7", "path-8", "path-9", "path-without-measure");
+  }
+
+  @Test
+  public void sort_by_textual_metric_key_descending() {
+    components.add(newComponentWithoutSnapshotId("name-without-measure", "qualifier-without-measure", "path-without-measure"));
+    ComponentTreeWsRequest wsRequest = newRequest(singletonList(METRIC_SORT), false, TEXT_METRIC_KEY);
+
+    List<ComponentDtoWithSnapshotId> result = sortComponents(wsRequest);
+
+    assertThat(result).extracting("path")
+      .containsExactly("path-9", "path-8", "path-7", "path-6", "path-5", "path-4", "path-3", "path-2", "path-1", "path-without-measure");
   }
 
   @Test