]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6827 fix selectBySnapshotAndMetricKeys 493/head
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 4 Sep 2015 07:38:13 +0000 (09:38 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 7 Sep 2015 15:56:17 +0000 (17:56 +0200)
it should also return rule/characteristic measures

sonar-db/src/main/java/org/sonar/db/measure/MeasureDto.java
sonar-db/src/main/resources/org/sonar/db/measure/MeasureMapper.xml
sonar-db/src/test/java/org/sonar/db/measure/MeasureDaoTest.java
sonar-db/src/test/resources/org/sonar/db/measure/MeasureDaoTest/select_by_snapshot_and_metric_keys.xml [new file with mode: 0644]
sonar-db/src/test/resources/org/sonar/db/measure/MeasureDaoTest/select_by_snapshot_and_metric_keys_with_characteristic_id.xml [new file with mode: 0644]
sonar-db/src/test/resources/org/sonar/db/measure/MeasureDaoTest/select_by_snapshot_and_metric_keys_with_rule_id.xml [new file with mode: 0644]

index 986f5d6d588be0b0936fa82f6519184e78f47743..ccfaf99c4cbff4d62af99ebb89c63fda2fd49d7d 100644 (file)
@@ -202,6 +202,7 @@ public class MeasureDto {
     return this;
   }
 
+  @CheckForNull
   public Integer getCharacteristicId() {
     return characteristicId;
   }
index d243931013144aa55227b4ae44cbd713453c2ddc..b4449505645c2c98d4575128c2403a408079c1ff 100644 (file)
@@ -5,6 +5,8 @@
   <sql id="measureColumns">
     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 @@
       <foreach item="metricKey" index="index" collection="metricKeys" open="(" separator=" or " close=")">
         metric.name=#{metricKey}
       </foreach>
-      AND pm.rule_id IS NULL
-      AND pm.characteristic_id IS NULL
       AND pm.person_id IS NULL
     </where>
   </select>
index 8f487d8fc5626a46721ba97b9e51c7d39e1b938e..dc24bb8a0221a871a02ca313860257c0e0d3db00 100644 (file)
@@ -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<MeasureDto> 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.<String>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<MeasureDto> results = underTest.selectBySnapshotIdAndMetricKeys(5, newHashSet("ncloc"), db.getSession());
+    assertThat(results).hasSize(3);
+
+    Map<Long, MeasureDto> 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<MeasureDto> results = underTest.selectBySnapshotIdAndMetricKeys(5, newHashSet("ncloc"), db.getSession());
+    assertThat(results).hasSize(3);
+
+    Map<Long, MeasureDto> 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<Long, MeasureDto> measuresById(List<MeasureDto> pastMeasures) {
+    return FluentIterable.from(pastMeasures).uniqueIndex(new Function<MeasureDto, Long>() {
+      @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 (file)
index 0000000..c27311c
--- /dev/null
@@ -0,0 +1,21 @@
+<dataset>
+
+  <metrics id="10" name="authors_by_line"/>
+  <metrics id="11" name="coverage_line_hits_data"/>
+  <metrics id="12" name="ncloc"/>
+
+  <projects id="1" kee="org.struts:struts-core:src/org/struts/RequestContext.java" enabled="[true]"/>
+
+  <snapshots id="5" project_id="1" islast="[true]" />
+
+  <project_measures id="20" snapshot_id="5" metric_id="10" value="[null]" text_value="0123456789012345678901234567890123456789" measure_data="[null]"
+                    variation_value_1="[null]" variation_value_2="[null]" variation_value_3="[null]" variation_value_4="[null]" variation_value_5="[null]"
+                    alert_status="[null]" alert_text="[null]" />
+  <project_measures id="21" snapshot_id="5" metric_id="11" value="[null]" text_value="36=1;37=1;38=1;39=1;43=1;48=1;53=1" measure_data="[null]"
+                    variation_value_1="[null]" variation_value_2="[null]" variation_value_3="[null]" variation_value_4="[null]" variation_value_5="[null]"
+                    alert_status="[null]" alert_text="[null]" />
+  <project_measures id="22" snapshot_id="5" metric_id="12" value="10" text_value="[null]" measure_data="[null]"
+                    variation_value_1="1" variation_value_2="2" variation_value_3="3" variation_value_4="4" variation_value_5="-5"
+                    alert_status="OK" alert_text="Green"/>
+
+</dataset>
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 (file)
index 0000000..8f451b7
--- /dev/null
@@ -0,0 +1,17 @@
+<dataset>
+
+  <metrics id="10" name="authors_by_line"/>
+  <metrics id="11" name="coverage_line_hits_data"/>
+  <metrics id="12" name="ncloc"/>
+
+  <projects id="1" kee="org.struts:struts-core:src/org/struts/RequestContext.java" enabled="[true]"/>
+
+  <snapshots id="5" project_id="1" islast="[true]" />
+
+  <project_measures id="1" VALUE="60" METRIC_ID="12" SNAPSHOT_ID="5" RULE_ID="[null]" characteristic_id="[null]" person_id="[null]"/>
+
+  <project_measures id="2" VALUE="20" METRIC_ID="12" SNAPSHOT_ID="5" RULE_ID="[null]" characteristic_id="10" person_id="[null]"/>
+
+  <project_measures id="3" VALUE="40" METRIC_ID="12" SNAPSHOT_ID="5" RULE_ID="[null]" characteristic_id="11" person_id="[null]"/>
+
+</dataset>
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 (file)
index 0000000..1398c24
--- /dev/null
@@ -0,0 +1,17 @@
+<dataset>
+
+  <metrics id="10" name="authors_by_line"/>
+  <metrics id="11" name="coverage_line_hits_data"/>
+  <metrics id="12" name="ncloc"/>
+
+  <projects id="1" kee="org.struts:struts-core:src/org/struts/RequestContext.java" enabled="[true]"/>
+
+  <snapshots id="5" project_id="1" islast="[true]" />
+
+  <project_measures id="1" VALUE="60" METRIC_ID="12" SNAPSHOT_ID="5" RULE_ID="[null]" characteristic_id="[null]" person_id="[null]"/>
+
+  <project_measures id="2" VALUE="20" METRIC_ID="12" SNAPSHOT_ID="5" RULE_ID="30" characteristic_id="[null]" person_id="[null]"/>
+
+  <project_measures id="3" VALUE="40" METRIC_ID="12" SNAPSHOT_ID="5" RULE_ID="31" characteristic_id="[null]" person_id="[null]"/>
+
+</dataset>