From c6706c9934cda2a48007c9f3fd6d3e9abe9365e2 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Fri, 4 Sep 2015 09:38:13 +0200 Subject: [PATCH] SONAR-6827 fix selectBySnapshotAndMetricKeys it should also return rule/characteristic measures --- .../java/org/sonar/db/measure/MeasureDto.java | 1 + .../org/sonar/db/measure/MeasureMapper.xml | 4 +- .../org/sonar/db/measure/MeasureDaoTest.java | 63 ++++++++++++++++++- .../select_by_snapshot_and_metric_keys.xml | 21 +++++++ ...and_metric_keys_with_characteristic_id.xml | 17 +++++ ..._snapshot_and_metric_keys_with_rule_id.xml | 17 +++++ 6 files changed, 120 insertions(+), 3 deletions(-) create mode 100644 sonar-db/src/test/resources/org/sonar/db/measure/MeasureDaoTest/select_by_snapshot_and_metric_keys.xml create mode 100644 sonar-db/src/test/resources/org/sonar/db/measure/MeasureDaoTest/select_by_snapshot_and_metric_keys_with_characteristic_id.xml create mode 100644 sonar-db/src/test/resources/org/sonar/db/measure/MeasureDaoTest/select_by_snapshot_and_metric_keys_with_rule_id.xml diff --git a/sonar-db/src/main/java/org/sonar/db/measure/MeasureDto.java b/sonar-db/src/main/java/org/sonar/db/measure/MeasureDto.java index 986f5d6d588..ccfaf99c4cb 100644 --- a/sonar-db/src/main/java/org/sonar/db/measure/MeasureDto.java +++ b/sonar-db/src/main/java/org/sonar/db/measure/MeasureDto.java @@ -202,6 +202,7 @@ public class MeasureDto { return this; } + @CheckForNull public Integer getCharacteristicId() { return characteristicId; } diff --git a/sonar-db/src/main/resources/org/sonar/db/measure/MeasureMapper.xml b/sonar-db/src/main/resources/org/sonar/db/measure/MeasureMapper.xml index d2439310131..b4449505645 100644 --- a/sonar-db/src/main/resources/org/sonar/db/measure/MeasureMapper.xml +++ b/sonar-db/src/main/resources/org/sonar/db/measure/MeasureMapper.xml @@ -5,6 +5,8 @@ pm.id, pm.metric_id as metricId, + pm.rule_id as ruleId, + pm.characteristic_id as characteristicId, pm.snapshot_id as snapshotId, pm.value as value, pm.text_value as textValue, @@ -69,8 +71,6 @@ metric.name=#{metricKey} - AND pm.rule_id IS NULL - AND pm.characteristic_id IS NULL AND pm.person_id IS NULL diff --git a/sonar-db/src/test/java/org/sonar/db/measure/MeasureDaoTest.java b/sonar-db/src/test/java/org/sonar/db/measure/MeasureDaoTest.java index 8f487d8fc56..dc24bb8a022 100644 --- a/sonar-db/src/test/java/org/sonar/db/measure/MeasureDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/measure/MeasureDaoTest.java @@ -25,6 +25,7 @@ import com.google.common.base.Optional; import com.google.common.base.Predicate; import com.google.common.collect.FluentIterable; import com.google.common.collect.ImmutableSet; +import java.util.Collections; import java.util.List; import java.util.Map; import javax.annotation.Nullable; @@ -36,6 +37,7 @@ import org.sonar.db.DbTester; import org.sonar.test.DbTests; import static com.google.common.collect.Lists.newArrayList; +import static com.google.common.collect.Sets.newHashSet; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.guava.api.Assertions.assertThat; @@ -267,6 +269,55 @@ public class MeasureDaoTest { assertThat(measure1.getPersonId()).isNull(); } + @Test + public void select_by_snapshot_and_metric_keys() throws Exception { + db.prepareDbUnit(getClass(), "select_by_snapshot_and_metric_keys.xml"); + + List results = underTest.selectBySnapshotIdAndMetricKeys(5, newHashSet("ncloc", "authors_by_line"), db.getSession()); + assertThat(results).hasSize(2); + + results = underTest.selectBySnapshotIdAndMetricKeys(5, newHashSet("ncloc"), db.getSession()); + assertThat(results).hasSize(1); + + MeasureDto result = results.get(0); + assertThat(result.getId()).isEqualTo(22); + assertThat(result.getValue()).isEqualTo(10d); + assertThat(result.getVariation(1)).isEqualTo(1d); + assertThat(result.getVariation(2)).isEqualTo(2d); + assertThat(result.getVariation(3)).isEqualTo(3d); + assertThat(result.getVariation(4)).isEqualTo(4d); + assertThat(result.getVariation(5)).isEqualTo(-5d); + + assertThat(underTest.selectBySnapshotIdAndMetricKeys(123, newHashSet("ncloc"), db.getSession())).isEmpty(); + assertThat(underTest.selectBySnapshotIdAndMetricKeys(5, Collections.emptySet(), db.getSession())).isEmpty(); + } + + @Test + public void select_by_snapshot_and_metric_keys_return_measures_with_rule_id() throws Exception { + db.prepareDbUnit(getClass(), "select_by_snapshot_and_metric_keys_with_rule_id.xml"); + + List results = underTest.selectBySnapshotIdAndMetricKeys(5, newHashSet("ncloc"), db.getSession()); + assertThat(results).hasSize(3); + + Map measuresById = measuresById(results); + assertThat(measuresById.get(1L).getRuleId()).isNull(); + assertThat(measuresById.get(2L).getRuleId()).isEqualTo(30); + assertThat(measuresById.get(3L).getRuleId()).isEqualTo(31); + } + + @Test + public void select_by_snapshot_and_metric_keys_return_measures_with_characteristic_id() throws Exception { + db.prepareDbUnit(getClass(), "select_by_snapshot_and_metric_keys_with_characteristic_id.xml"); + + List results = underTest.selectBySnapshotIdAndMetricKeys(5, newHashSet("ncloc"), db.getSession()); + assertThat(results).hasSize(3); + + Map measuresById = measuresById(results); + assertThat(measuresById.get(1L).getCharacteristicId()).isNull(); + assertThat(measuresById.get(2L).getCharacteristicId()).isEqualTo(10); + assertThat(measuresById.get(3L).getCharacteristicId()).isEqualTo(11); + } + @Test public void insert() { db.prepareDbUnit(getClass(), "empty.xml"); @@ -291,7 +342,7 @@ public class MeasureDaoTest { ); db.getSession().commit(); - db.assertDbUnit(getClass(), "insert-result.xml", new String[] {"id"}, "project_measures"); + db.assertDbUnit(getClass(), "insert-result.xml", new String[]{"id"}, "project_measures"); } @Test @@ -323,4 +374,14 @@ public class MeasureDaoTest { } }); } + + private static Map measuresById(List pastMeasures) { + return FluentIterable.from(pastMeasures).uniqueIndex(new Function() { + @Nullable + @Override + public Long apply(MeasureDto input) { + return input.getId(); + } + }); + } } diff --git a/sonar-db/src/test/resources/org/sonar/db/measure/MeasureDaoTest/select_by_snapshot_and_metric_keys.xml b/sonar-db/src/test/resources/org/sonar/db/measure/MeasureDaoTest/select_by_snapshot_and_metric_keys.xml new file mode 100644 index 00000000000..c27311c8d38 --- /dev/null +++ b/sonar-db/src/test/resources/org/sonar/db/measure/MeasureDaoTest/select_by_snapshot_and_metric_keys.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + diff --git a/sonar-db/src/test/resources/org/sonar/db/measure/MeasureDaoTest/select_by_snapshot_and_metric_keys_with_characteristic_id.xml b/sonar-db/src/test/resources/org/sonar/db/measure/MeasureDaoTest/select_by_snapshot_and_metric_keys_with_characteristic_id.xml new file mode 100644 index 00000000000..8f451b78d8b --- /dev/null +++ b/sonar-db/src/test/resources/org/sonar/db/measure/MeasureDaoTest/select_by_snapshot_and_metric_keys_with_characteristic_id.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + diff --git a/sonar-db/src/test/resources/org/sonar/db/measure/MeasureDaoTest/select_by_snapshot_and_metric_keys_with_rule_id.xml b/sonar-db/src/test/resources/org/sonar/db/measure/MeasureDaoTest/select_by_snapshot_and_metric_keys_with_rule_id.xml new file mode 100644 index 00000000000..1398c2447f8 --- /dev/null +++ b/sonar-db/src/test/resources/org/sonar/db/measure/MeasureDaoTest/select_by_snapshot_and_metric_keys_with_rule_id.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + -- 2.39.5