]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7027 add MeasureDao#selectByDeveloperForSnapshotAndMetrics
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Tue, 24 Nov 2015 08:44:04 +0000 (09:44 +0100)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Wed, 25 Nov 2015 14:33:07 +0000 (15:33 +0100)
this method is used by Dev Cockpit to copy measures of a specific developer from a project to a copy of this project

sonar-db/src/main/java/org/sonar/db/measure/MeasureDao.java
sonar-db/src/main/java/org/sonar/db/measure/MeasureMapper.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/with_some_measures_for_developer.xml [new file with mode: 0644]

index fa8b93f4348c7581363952be4cb575a61dc28786..13b27d687680eb461c8f5ca31579426342cb35a3 100644 (file)
@@ -26,6 +26,7 @@ import java.util.Collection;
 import java.util.List;
 import java.util.Set;
 import javax.annotation.CheckForNull;
+import javax.annotation.Nonnull;
 import org.sonar.db.Dao;
 import org.sonar.db.DatabaseUtils;
 import org.sonar.db.DbSession;
@@ -79,6 +80,19 @@ public class MeasureDao implements Dao {
     });
   }
 
+  /**
+   * Used by plugin Developer Cockpit
+   */
+  public List<MeasureDto> selectByDeveloperForSnapshotAndMetrics(final DbSession dbSession, final long developerId, final long snapshotId, Collection<Integer> metricIds) {
+    return DatabaseUtils.executeLargeInputs(metricIds, new Function<List<Integer>, List<MeasureDto>>() {
+      @Override
+      @Nonnull
+      public List<MeasureDto> apply(@Nonnull List<Integer> input) {
+        return mapper(dbSession).selectByDeveloperForSnapshotAndMetrics(developerId, snapshotId, input);
+      }
+    });
+  }
+
   public void insert(DbSession session, MeasureDto measureDto) {
     mapper(session).insert(measureDto);
   }
index 301dff9e629ee746787f7c5fe5bb733f85125e05..fc6debc0a62d896eec57403d7d213eb6608fc036 100644 (file)
@@ -32,6 +32,8 @@ public interface MeasureMapper {
 
   List<MeasureDto> selectBySnapshotAndMetricKeys(@Param("snapshotId") long snapshotId, @Param("metricKeys") List<String> metricKeys);
 
+  List<MeasureDto> selectByDeveloperForSnapshotAndMetrics(@Param("developerId") long developerId, @Param("snapshotId") long snapshotId, @Param("metricIds") List<Integer> metricIds);
+
   @CheckForNull
   MeasureDto selectByComponentAndMetric(@Param("componentKey") String componentKey, @Param("metricKey") String metricKey);
 
index 8b104d9049ee0cb70e2b8610e3d94ee533233eb0..7874e1e2d2071f6634f9fd98ae9f935531a0fbf7 100644 (file)
     </where>
   </select>
 
+  <select id="selectByDeveloperForSnapshotAndMetrics" parameterType="map" resultType="Measure">
+    SELECT
+    <include refid="measureColumns"/>
+    FROM project_measures pm
+    <where>
+      pm.snapshot_id = #{snapshotId}
+      AND pm.rule_id is NULL
+      AND pm.characteristic_id is NULL
+      AND pm.person_id = #{developerId}
+      AND
+      <foreach item="metricId" index="index" collection="metricIds" open="(" separator=" or " close=")">
+        pm.metric_id=#{metricId}
+      </foreach>
+    </where>
+  </select>
+
   <select id="countByComponentAndMetric" parameterType="map" resultType="long">
     SELECT count(pm.id)
     FROM project_measures pm
index a714ad0fffb092892b975555daff542960b6e2ac..60538b15b0ec438336edafae6c729b8db3a16cd6 100644 (file)
@@ -24,6 +24,7 @@ import com.google.common.base.Function;
 import com.google.common.base.Optional;
 import com.google.common.base.Predicate;
 import com.google.common.collect.FluentIterable;
+import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableSet;
 import java.util.Collections;
 import java.util.List;
@@ -44,6 +45,12 @@ import static org.assertj.guava.api.Assertions.assertThat;
 @Category(DbTests.class)
 public class MeasureDaoTest {
 
+  private static final int SNAPSHOT_ID = 5;
+  private static final long DEVELOPER_ID = 333L;
+  private static final int AUTHORS_BY_LINE_METRIC_ID = 10;
+  private static final int COVERAGE_LINE_HITS_DATA_METRIC_ID = 11;
+  private static final int NCLOC_METRIC_ID = 12;
+
   @Rule
   public DbTester db = DbTester.create(System2.INSTANCE);
 
@@ -111,7 +118,7 @@ public class MeasureDaoTest {
   public void select_by_snapshotId_and_metrics() {
     db.prepareDbUnit(getClass(), "shared.xml");
 
-    List<MeasureDto> results = underTest.selectBySnapshotIdAndMetricKeys(5l, ImmutableSet.of("ncloc", "authors_by_line"), db.getSession());
+    List<MeasureDto> results = underTest.selectBySnapshotIdAndMetricKeys(SNAPSHOT_ID, ImmutableSet.of("ncloc", "authors_by_line"), db.getSession());
     assertThat(results).hasSize(2);
 
     Optional<MeasureDto> optional = FluentIterable.from(results).filter(new Predicate<MeasureDto>() {
@@ -273,10 +280,10 @@ public class MeasureDaoTest {
   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());
+    List<MeasureDto> results = underTest.selectBySnapshotIdAndMetricKeys(SNAPSHOT_ID, newHashSet("ncloc", "authors_by_line"), db.getSession());
     assertThat(results).hasSize(2);
 
-    results = underTest.selectBySnapshotIdAndMetricKeys(5, newHashSet("ncloc"), db.getSession());
+    results = underTest.selectBySnapshotIdAndMetricKeys(SNAPSHOT_ID, newHashSet("ncloc"), db.getSession());
     assertThat(results).hasSize(1);
 
     MeasureDto result = results.get(0);
@@ -289,14 +296,14 @@ public class MeasureDaoTest {
     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();
+    assertThat(underTest.selectBySnapshotIdAndMetricKeys(SNAPSHOT_ID, 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());
+    List<MeasureDto> results = underTest.selectBySnapshotIdAndMetricKeys(SNAPSHOT_ID, newHashSet("ncloc"), db.getSession());
     assertThat(results).hasSize(3);
 
     Map<Long, MeasureDto> measuresById = measuresById(results);
@@ -309,7 +316,7 @@ public class MeasureDaoTest {
   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());
+    List<MeasureDto> results = underTest.selectBySnapshotIdAndMetricKeys(SNAPSHOT_ID, newHashSet("ncloc"), db.getSession());
     assertThat(results).hasSize(3);
 
     Map<Long, MeasureDto> measuresById = measuresById(results);
@@ -318,6 +325,61 @@ public class MeasureDaoTest {
     assertThat(measuresById.get(3L).getCharacteristicId()).isEqualTo(11);
   }
 
+  @Test
+  public void selectByDeveloperForSnapshotAndMetrics_when_there_is_no_measure_for_developer_returns_empty() {
+    db.prepareDbUnit(getClass(), "shared.xml");
+
+    List<MeasureDto> measureDtos = underTest.selectByDeveloperForSnapshotAndMetrics(db.getSession(),
+        DEVELOPER_ID, SNAPSHOT_ID,
+        ImmutableList.of(AUTHORS_BY_LINE_METRIC_ID, COVERAGE_LINE_HITS_DATA_METRIC_ID, NCLOC_METRIC_ID));
+
+    assertThat(measureDtos).isEmpty();
+  }
+
+  @Test
+  public void selectByDeveloperForSnapshotAndMetrics_returns_only_measures_for_developer() {
+    db.prepareDbUnit(getClass(), "with_some_measures_for_developer.xml");
+
+    List<MeasureDto> measureDtos = underTest.selectByDeveloperForSnapshotAndMetrics(db.getSession(),
+        DEVELOPER_ID, SNAPSHOT_ID,
+        ImmutableList.of(AUTHORS_BY_LINE_METRIC_ID, COVERAGE_LINE_HITS_DATA_METRIC_ID, NCLOC_METRIC_ID));
+
+    assertThat(measureDtos).extracting("id").containsOnly(30L, 31L, 32L);
+  }
+
+  @Test
+  public void selectByDeveloperForSnapshotAndMetrics_returns_only_measures_for_developer_and_specified_metric_id() {
+    db.prepareDbUnit(getClass(), "with_some_measures_for_developer.xml");
+
+    List<MeasureDto> measureDtos = underTest.selectByDeveloperForSnapshotAndMetrics(db.getSession(),
+        DEVELOPER_ID, SNAPSHOT_ID,
+        ImmutableList.of(NCLOC_METRIC_ID));
+
+    assertThat(measureDtos).extracting("id").containsOnly(32L);
+  }
+
+  @Test
+  public void selectByDeveloperForSnapshotAndMetrics_returns_empty_when_single_metric_id_does_not_exist() {
+    db.prepareDbUnit(getClass(), "with_some_measures_for_developer.xml");
+
+    List<MeasureDto> measureDtos = underTest.selectByDeveloperForSnapshotAndMetrics(db.getSession(),
+        DEVELOPER_ID, SNAPSHOT_ID,
+        ImmutableList.of(666));
+
+    assertThat(measureDtos).isEmpty();
+  }
+
+  @Test
+  public void selectByDeveloperForSnapshotAndMetrics_returns_empty_when_snapshotId_does_not_exist() {
+    db.prepareDbUnit(getClass(), "with_some_measures_for_developer.xml");
+
+    List<MeasureDto> measureDtos = underTest.selectByDeveloperForSnapshotAndMetrics(db.getSession(),
+        DEVELOPER_ID, 10,
+        ImmutableList.of(AUTHORS_BY_LINE_METRIC_ID, COVERAGE_LINE_HITS_DATA_METRIC_ID, NCLOC_METRIC_ID));
+
+    assertThat(measureDtos).isEmpty();
+  }
+
   @Test
   public void insert() {
     db.prepareDbUnit(getClass(), "empty.xml");
diff --git a/sonar-db/src/test/resources/org/sonar/db/measure/MeasureDaoTest/with_some_measures_for_developer.xml b/sonar-db/src/test/resources/org/sonar/db/measure/MeasureDaoTest/with_some_measures_for_developer.xml
new file mode 100644 (file)
index 0000000..a6978d6
--- /dev/null
@@ -0,0 +1,38 @@
+<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]"/>
+  <projects id="333" kee="dev:John-Doe" 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]"
+                    person_id="[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]"
+                    person_id="[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"
+                    person_id="[null]"/>
+  <!--measures for developer 333-->
+  <project_measures id="30" 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]"
+                    person_id="333"/>
+  <project_measures id="31" 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]"
+                    person_id="333"/>
+  <project_measures id="32" 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"
+                    person_id="333"/>
+
+</dataset>