From 70f24fe988c122c91c96887239c602b43c51ada0 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Wed, 21 Aug 2013 19:34:22 +0200 Subject: [PATCH] SONAR-1835 Sonar should sort alerts based on the functional meaning of the indicators --- .../sonar/core/measure/MeasureFilterSort.java | 5 ++ .../sonar/core/measure/MeasureFilterSql.java | 22 +++++ .../measure/MeasureFilterExecutorTest.java | 53 +++++++++--- .../sort_by_alert.xml | 81 +++++++++++++++++++ 4 files changed, 149 insertions(+), 12 deletions(-) create mode 100644 sonar-core/src/test/resources/org/sonar/core/measure/MeasureFilterExecutorTest/sort_by_alert.xml diff --git a/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterSort.java b/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterSort.java index b3ee0d6f1ae..b9e02f35468 100644 --- a/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterSort.java +++ b/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterSort.java @@ -19,6 +19,7 @@ */ package org.sonar.core.measure; +import org.sonar.api.measures.CoreMetrics; import org.sonar.api.measures.Metric; class MeasureFilterSort { @@ -83,6 +84,10 @@ class MeasureFilterSort { return Field.DATE.equals(field) || Field.PROJECT_CREATION_DATE.equals(field); } + boolean isOnAlert() { + return metric != null && metric.getKey().equals(CoreMetrics.ALERT_STATUS_KEY); + } + boolean isAsc() { return asc; } diff --git a/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterSql.java b/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterSql.java index ea072f115ae..25204064111 100644 --- a/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterSql.java +++ b/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterSql.java @@ -20,6 +20,7 @@ package org.sonar.core.measure; import com.google.common.base.Function; +import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; import com.google.common.collect.Ordering; import org.apache.commons.dbutils.DbUtils; @@ -191,6 +192,8 @@ class MeasureFilterSql { rowProcessor = new NumericSortRowProcessor(); } else if (filter.sort().isOnDate()) { rowProcessor = new DateSortRowProcessor(); + } else if (filter.sort().isOnAlert()) { + rowProcessor = new AlertSortRowProcessor(); } else { rowProcessor = new TextSortRowProcessor(); } @@ -250,7 +253,25 @@ class MeasureFilterSql { } return ordering; } + } + static class AlertSortRowProcessor extends TextSortRowProcessor { + Function sortFieldFunction() { + return new Function() { + public Integer apply(MeasureFilterRow row) { + return ImmutableList.of("OK", "WARN", "ERROR").indexOf(row.getSortText()); + } + }; + } + + @Override + Ordering sortFieldOrdering(boolean ascending) { + Ordering ordering = Ordering.natural().nullsLast(); + if (!ascending) { + ordering = ordering.reverse(); + } + return ordering; + } } static class NumericSortRowProcessor extends RowProcessor { @@ -325,3 +346,4 @@ class MeasureFilterSql { }); } } + diff --git a/sonar-core/src/test/java/org/sonar/core/measure/MeasureFilterExecutorTest.java b/sonar-core/src/test/java/org/sonar/core/measure/MeasureFilterExecutorTest.java index 392ead327cd..71e39f7e897 100644 --- a/sonar-core/src/test/java/org/sonar/core/measure/MeasureFilterExecutorTest.java +++ b/sonar-core/src/test/java/org/sonar/core/measure/MeasureFilterExecutorTest.java @@ -22,6 +22,7 @@ package org.sonar.core.measure; import org.junit.Before; import org.junit.Rule; import org.junit.Test; +import org.sonar.api.measures.CoreMetrics; import org.sonar.api.measures.Metric; import org.sonar.api.utils.DateUtils; import org.sonar.core.persistence.TestDatabase; @@ -359,6 +360,36 @@ public class MeasureFilterExecutorTest { assertThat(DateUtils.formatDate(rows.get(1).getSortDate())).isEqualTo("2008-12-19"); } + @Test + public void sort_by_ascending_alert() throws SQLException { + db.prepareDbUnit(getClass(), "sort_by_alert.xml"); + + Metric alert = new Metric.Builder(CoreMetrics.ALERT_STATUS_KEY, "Alert", Metric.ValueType.LEVEL).create().setId(5); + MeasureFilter filter = new MeasureFilter().setResourceQualifiers(Arrays.asList("TRK")).setSortOnMetric(alert); + List rows = executor.execute(filter, new MeasureFilterContext()); + + // Php Project OK, Java Project WARN then Js Project ERROR + assertThat(rows).hasSize(3); + verifyPhpProject(rows.get(0)); + verifyJavaProject(rows.get(1)); + verifyProject(rows.get(2), 120L, 20L, 20L); + } + + @Test + public void sort_by_descending_alert() throws SQLException { + db.prepareDbUnit(getClass(), "sort_by_alert.xml"); + + Metric alert = new Metric.Builder(CoreMetrics.ALERT_STATUS_KEY, "Alert", Metric.ValueType.LEVEL).create().setId(5); + MeasureFilter filter = new MeasureFilter().setResourceQualifiers(Arrays.asList("TRK")).setSortOnMetric(alert).setSortAsc(false); + List rows = executor.execute(filter, new MeasureFilterContext()); + + // Js Project ERROR, Java Project WARN, then Php Project OK + assertThat(rows).hasSize(3); + verifyProject(rows.get(0), 120L, 20L, 20L); + verifyJavaProject(rows.get(1)); + verifyPhpProject(rows.get(2)); + } + @Test public void condition_on_numeric_measure() throws SQLException { db.prepareDbUnit(getClass(), "shared.xml"); @@ -547,26 +578,24 @@ public class MeasureFilterExecutorTest { private void verifyJavaProject(MeasureFilterRow row) { - assertThat(row.getSnapshotId()).isEqualTo(JAVA_PROJECT_SNAPSHOT_ID); - assertThat(row.getResourceId()).isEqualTo(JAVA_PROJECT_ID); - assertThat(row.getResourceRootId()).isEqualTo(JAVA_PROJECT_ID); + verifyProject(row, JAVA_PROJECT_SNAPSHOT_ID, JAVA_PROJECT_ID, JAVA_PROJECT_ID); } private void verifyJavaBigFile(MeasureFilterRow row) { - assertThat(row.getSnapshotId()).isEqualTo(JAVA_FILE_BIG_SNAPSHOT_ID); - assertThat(row.getResourceId()).isEqualTo(JAVA_FILE_BIG_ID); - assertThat(row.getResourceRootId()).isEqualTo(JAVA_PROJECT_ID); + verifyProject(row, JAVA_FILE_BIG_SNAPSHOT_ID, JAVA_FILE_BIG_ID, JAVA_PROJECT_ID); } private void verifyJavaTinyFile(MeasureFilterRow row) { - assertThat(row.getSnapshotId()).isEqualTo(JAVA_FILE_TINY_SNAPSHOT_ID); - assertThat(row.getResourceId()).isEqualTo(JAVA_FILE_TINY_ID); - assertThat(row.getResourceRootId()).isEqualTo(JAVA_PROJECT_ID); + verifyProject(row, JAVA_FILE_TINY_SNAPSHOT_ID, JAVA_FILE_TINY_ID, JAVA_PROJECT_ID); } private void verifyPhpProject(MeasureFilterRow row) { - assertThat(row.getSnapshotId()).isEqualTo(PHP_SNAPSHOT_ID); - assertThat(row.getResourceId()).isEqualTo(PHP_PROJECT_ID); - assertThat(row.getResourceRootId()).isEqualTo(PHP_PROJECT_ID); + verifyProject(row, PHP_SNAPSHOT_ID, PHP_PROJECT_ID, PHP_PROJECT_ID); + } + + private void verifyProject(MeasureFilterRow row, Long snashotId, Long resourceId, Long resourceRootId) { + assertThat(row.getSnapshotId()).isEqualTo(snashotId); + assertThat(row.getResourceId()).isEqualTo(resourceId); + assertThat(row.getResourceRootId()).isEqualTo(resourceRootId); } } diff --git a/sonar-core/src/test/resources/org/sonar/core/measure/MeasureFilterExecutorTest/sort_by_alert.xml b/sonar-core/src/test/resources/org/sonar/core/measure/MeasureFilterExecutorTest/sort_by_alert.xml new file mode 100644 index 00000000000..292037eac7a --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/measure/MeasureFilterExecutorTest/sort_by_alert.xml @@ -0,0 +1,81 @@ + + \ No newline at end of file -- 2.39.5