summaryrefslogtreecommitdiffstats
path: root/sonar-db
diff options
context:
space:
mode:
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>2016-04-06 18:36:39 +0200
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>2016-04-07 13:54:46 +0200
commit616389be7c3eecd9a979aafec6d42c4445a1558a (patch)
tree837260d69fa50bda415c5f06bee1c46095747ab6 /sonar-db
parent3049d9176376e1907980a692c318c9d0299b8bd8 (diff)
downloadsonarqube-616389be7c3eecd9a979aafec6d42c4445a1558a.tar.gz
sonarqube-616389be7c3eecd9a979aafec6d42c4445a1558a.zip
SONAR-7528 WS api/measures/component_tree handles developer measures
Diffstat (limited to 'sonar-db')
-rw-r--r--sonar-db/src/main/java/org/sonar/db/measure/MeasureDao.java13
-rw-r--r--sonar-db/src/main/java/org/sonar/db/measure/MeasureMapper.java4
-rw-r--r--sonar-db/src/main/resources/org/sonar/db/measure/MeasureMapper.xml9
-rw-r--r--sonar-db/src/test/java/org/sonar/db/measure/MeasureDaoTest.java75
4 files changed, 89 insertions, 12 deletions
diff --git a/sonar-db/src/main/java/org/sonar/db/measure/MeasureDao.java b/sonar-db/src/main/java/org/sonar/db/measure/MeasureDao.java
index a73d92b128b..69724405aab 100644
--- a/sonar-db/src/main/java/org/sonar/db/measure/MeasureDao.java
+++ b/sonar-db/src/main/java/org/sonar/db/measure/MeasureDao.java
@@ -26,6 +26,7 @@ import java.util.List;
import java.util.Set;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
import org.sonar.db.Dao;
import org.sonar.db.DatabaseUtils;
import org.sonar.db.DbSession;
@@ -69,7 +70,7 @@ public class MeasureDao implements Dao {
}
public List<PastMeasureDto> selectByComponentUuidAndProjectSnapshotIdAndMetricIds(final DbSession session, final String componentUuid, final long projectSnapshotId,
- Set<Integer> metricIds) {
+ Set<Integer> metricIds) {
return DatabaseUtils.executeLargeInputs(metricIds, new Function<List<Integer>, List<PastMeasureDto>>() {
@Override
public List<PastMeasureDto> apply(List<Integer> ids) {
@@ -82,7 +83,8 @@ 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) {
+ public List<MeasureDto> selectByDeveloperForSnapshotAndMetrics(final DbSession dbSession, @Nullable final Long developerId, final long snapshotId,
+ Collection<Integer> metricIds) {
return DatabaseUtils.executeLargeInputs(metricIds, new Function<List<Integer>, List<MeasureDto>>() {
@Override
@Nonnull
@@ -109,11 +111,16 @@ public class MeasureDao implements Dao {
* Used by plugin Developer Cockpit
*/
public List<MeasureDto> selectBySnapshotIdsAndMetricIds(final DbSession dbSession, List<Long> snapshotIds, final List<Integer> metricIds) {
+ return selectByDeveloperAndSnapshotIdsAndMetricIds(dbSession, null, snapshotIds, metricIds);
+ }
+
+ public List<MeasureDto> selectByDeveloperAndSnapshotIdsAndMetricIds(final DbSession dbSession, @Nullable final Long developerId, List<Long> snapshotIds,
+ final List<Integer> metricIds) {
return DatabaseUtils.executeLargeInputs(snapshotIds, new Function<List<Long>, List<MeasureDto>>() {
@Override
@Nonnull
public List<MeasureDto> apply(@Nonnull List<Long> input) {
- return mapper(dbSession).selectBySnapshotIdsAndMetricIds(input, metricIds);
+ return mapper(dbSession).selectByDeveloperAndSnapshotIdsAndMetricIds(developerId, input, metricIds);
}
});
}
diff --git a/sonar-db/src/main/java/org/sonar/db/measure/MeasureMapper.java b/sonar-db/src/main/java/org/sonar/db/measure/MeasureMapper.java
index 82bf90bd0f1..fb9d7254bcc 100644
--- a/sonar-db/src/main/java/org/sonar/db/measure/MeasureMapper.java
+++ b/sonar-db/src/main/java/org/sonar/db/measure/MeasureMapper.java
@@ -22,6 +22,7 @@ package org.sonar.db.measure;
import java.util.Collection;
import java.util.List;
import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
import org.apache.ibatis.annotations.Param;
public interface MeasureMapper {
@@ -37,7 +38,8 @@ public interface MeasureMapper {
List<MeasureDto> selectBySnapshotAndMetrics(@Param("snapshotId") long snapshotId, @Param("metricIds") List<Integer> input);
- List<MeasureDto> selectBySnapshotIdsAndMetricIds(@Param("snapshotIds") List<Long> snapshotIds, @Param("metricIds") List<Integer> metricIds);
+ List<MeasureDto> selectByDeveloperAndSnapshotIdsAndMetricIds(@Nullable @Param("developerId") Long developerId, @Param("snapshotIds") List<Long> snapshotIds,
+ @Param("metricIds") List<Integer> metricIds);
List<MeasureDto> selectProjectMeasuresByDeveloperForMetrics(@Param("developerId") long developerId, @Param("metricIds") Collection<Integer> metricIds);
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 4f9f8211536..7c97ed532e9 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
@@ -84,7 +84,7 @@
</where>
</select>
- <select id="selectBySnapshotIdsAndMetricIds" parameterType="map" resultType="Measure">
+ <select id="selectByDeveloperAndSnapshotIdsAndMetricIds" parameterType="map" resultType="Measure">
SELECT
<include refid="measureColumns"/>
FROM project_measures pm
@@ -98,7 +98,12 @@
<foreach item="metricId" collection="metricIds" open="(" separator="," close=")">
#{metricId}
</foreach>
- AND pm.person_id is NULL
+ <if test="developerId==null">
+ AND pm.person_id is NULL
+ </if>
+ <if test="developerId!=null">
+ AND pm.person_id=#{developerId}
+ </if>
</where>
</select>
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 d85f6cbbfce..2e7c592e568 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
@@ -35,14 +35,23 @@ import org.sonar.api.utils.System2;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
+import org.sonar.db.component.ComponentDbTester;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.ComponentTesting;
import org.sonar.db.component.SnapshotDto;
+import org.sonar.db.metric.MetricDto;
+import org.sonar.db.metric.MetricTesting;
import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.Sets.newHashSet;
+import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.guava.api.Assertions.assertThat;
+import static org.sonar.db.component.ComponentTesting.newDevProjectCopy;
+import static org.sonar.db.component.ComponentTesting.newDeveloper;
+import static org.sonar.db.component.ComponentTesting.newFileDto;
+import static org.sonar.db.component.ComponentTesting.newProjectDto;
+import static org.sonar.db.measure.MeasureTesting.newMeasureDto;
public class MeasureDaoTest {
@@ -59,6 +68,7 @@ public class MeasureDaoTest {
final DbSession dbSession = db.getSession();
MeasureDao underTest = dbClient.measureDao();
+ ComponentDbTester componentDb = new ComponentDbTester(db);
@Test
public void get_value_by_key() {
@@ -326,7 +336,62 @@ public class MeasureDaoTest {
assertThat(measureDtos).isEmpty();
}
- // TODO add test for selectBySnapshotIdsAndMetricIds
+ @Test
+ public void selectSnapshotIdsAndMetricIds() {
+ MetricDto metric = dbClient.metricDao().insert(dbSession, MetricTesting.newMetricDto());
+ ComponentDto project = newProjectDto();
+ SnapshotDto projectSnapshot = componentDb.insertProjectAndSnapshot(project);
+ SnapshotDto fileSnapshot = componentDb.insertComponentAndSnapshot(newFileDto(project, "file-uuid"), projectSnapshot);
+ ComponentDto developer = newDeveloper("Ray Bradbury");
+ SnapshotDto developerSnapshot = componentDb.insertDeveloperAndSnapshot(developer);
+ componentDb.insertComponentAndSnapshot(newDevProjectCopy("project-copy-uuid", project, developer), developerSnapshot);
+ underTest.insert(dbSession,
+ newMeasureDto(metric, developerSnapshot.getId()).setDeveloperId(developer.getId()),
+ newMeasureDto(metric, projectSnapshot.getId()),
+ newMeasureDto(metric, fileSnapshot.getId()));
+ dbSession.commit();
+
+ List<MeasureDto> result = underTest.selectBySnapshotIdsAndMetricIds(dbSession,
+ newArrayList(developerSnapshot.getId(), projectSnapshot.getId(), fileSnapshot.getId()),
+ singletonList(metric.getId()));
+
+ assertThat(result)
+ .hasSize(2)
+ .extracting("snapshotId")
+ .containsOnly(projectSnapshot.getId(), fileSnapshot.getId())
+ .doesNotContain(developerSnapshot.getId());
+ }
+
+ @Test
+ public void selectDeveloperAndSnapshotIdsAndMetricIds() {
+ MetricDto metric = dbClient.metricDao().insert(dbSession, MetricTesting.newMetricDto());
+ ComponentDto project = newProjectDto();
+ SnapshotDto projectSnapshot = componentDb.insertProjectAndSnapshot(project);
+ SnapshotDto fileSnapshot = componentDb.insertComponentAndSnapshot(newFileDto(project, "file-uuid"), projectSnapshot);
+ ComponentDto developer = newDeveloper("Ray Bradbury");
+ SnapshotDto developerSnapshot = componentDb.insertDeveloperAndSnapshot(developer);
+ componentDb.insertComponentAndSnapshot(newDevProjectCopy("project-copy-uuid", project, developer), developerSnapshot);
+
+ underTest.insert(dbSession,
+ newMeasureDto(metric, developerSnapshot.getId()).setDeveloperId(developer.getId()),
+ newMeasureDto(metric, projectSnapshot.getId()).setDeveloperId(developer.getId()),
+ newMeasureDto(metric, projectSnapshot.getId()).setDeveloperId(null),
+ newMeasureDto(metric, fileSnapshot.getId()).setDeveloperId(developer.getId()));
+ dbSession.commit();
+
+ List<MeasureDto> result = underTest.selectByDeveloperAndSnapshotIdsAndMetricIds(dbSession,
+ developer.getId(),
+ newArrayList(developerSnapshot.getId(), projectSnapshot.getId(), fileSnapshot.getId()),
+ singletonList(metric.getId()));
+
+ assertThat(result)
+ .hasSize(3)
+ .extracting("snapshotId")
+ .containsOnly(developerSnapshot.getId(), projectSnapshot.getId(), fileSnapshot.getId());
+ assertThat(result)
+ .extracting("developerId")
+ .containsOnly(developer.getId());
+ }
@Test
public void selectProjectMeasuresByDeveloperForMetrics_returns_empty_on_empty_db() {
@@ -397,7 +462,7 @@ public class MeasureDaoTest {
}
private ComponentDto insertProject(String uuid) {
- ComponentDto projectDto = ComponentTesting.newProjectDto(uuid);
+ ComponentDto projectDto = newProjectDto(uuid);
return insertComponent(projectDto);
}
@@ -434,8 +499,7 @@ public class MeasureDaoTest {
.setVariation(5, 5.0d)
.setAlertStatus("alert")
.setAlertText("alert-text")
- .setDescription("measure-description")
- );
+ .setDescription("measure-description"));
dbSession.commit();
db.assertDbUnit(getClass(), "insert-result.xml", new String[] {"id"}, "project_measures");
@@ -454,8 +518,7 @@ public class MeasureDaoTest {
.setSnapshotId(3L)
.setMetricId(4)
.setComponentId(6L)
- .setValue(4.0d)
- );
+ .setValue(4.0d));
dbSession.commit();
assertThat(db.countRowsOfTable("project_measures")).isEqualTo(2);