From 1226733455cc9e8082bd48f152cf5e4e64cf51dc Mon Sep 17 00:00:00 2001 From: Teryk Bellahsene Date: Thu, 18 Feb 2016 16:05:24 +0100 Subject: SONAR-7135 WS api/measures/component_tree sort by metric consistent for component measureless --- .../sonar/server/measure/ws/ComponentTreeSort.java | 10 +++---- .../server/measure/ws/ComponentTreeSortTest.java | 32 +++++++++++++++++++--- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeSort.java b/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeSort.java index b99ec27a3b3..6c136b2f490 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeSort.java +++ b/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeSort.java @@ -87,13 +87,12 @@ class ComponentTreeSort { } private static Ordering stringOrdering(boolean isAscending, Function function) { - Ordering ordering = Ordering.from(CASE_INSENSITIVE_ORDER) - .nullsLast(); + Ordering 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 numericalMetricOrdering(boolean isAscending, @Nullable MetricDto metric, Table measuresByComponentUuidAndMetric) { - Ordering ordering = Ordering.natural() - .nullsLast(); + Ordering 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 { diff --git a/server/sonar-server/src/test/java/org/sonar/server/measure/ws/ComponentTreeSortTest.java b/server/sonar-server/src/test/java/org/sonar/server/measure/ws/ComponentTreeSortTest.java index eed4d78fc97..13439ef9655 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/measure/ws/ComponentTreeSortTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/measure/ws/ComponentTreeSortTest.java @@ -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 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 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 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 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 -- cgit v1.2.3