]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7027 do not try and compute variations of developer measures 669/head
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Wed, 2 Dec 2015 14:49:02 +0000 (15:49 +0100)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 3 Dec 2015 07:43:18 +0000 (08:43 +0100)
server/sonar-server/src/main/java/org/sonar/server/computation/step/ComputeMeasureVariationsStep.java
server/sonar-server/src/test/java/org/sonar/server/computation/step/ReportComputeMeasureVariationsStepTest.java

index 768628ba67fdf1961fea6c8ff3ab7d6d53729b66..4da6dd4190fa8706b7c8d25b433b44b03c695459 100644 (file)
@@ -47,6 +47,7 @@ import org.sonar.server.computation.metric.MetricRepository;
 import org.sonar.server.computation.period.Period;
 import org.sonar.server.computation.period.PeriodsHolder;
 
+import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.collect.FluentIterable.from;
 import static org.sonar.server.computation.component.Component.Type.DIRECTORY;
 import static org.sonar.server.computation.component.Component.Type.SUBVIEW;
@@ -143,7 +144,7 @@ public class ComputeMeasureVariationsStep implements ComputationStep {
 
     private void setVariationMeasures(Component component, List<PastMeasureDto> pastMeasures, int period, MeasuresWithVariationRepository measuresWithVariationRepository) {
       Map<MeasureKey, PastMeasureDto> pastMeasuresByMeasureKey = from(pastMeasures).uniqueIndex(pastMeasureToMeasureKey);
-      for (Map.Entry<String, Measure> entry : measureRepository.getRawMeasures(component).entries()) {
+      for (Map.Entry<String, Measure> entry : from(measureRepository.getRawMeasures(component).entries()).filter(NotDeveloperMeasure.INSTANCE)) {
         String metricKey = entry.getKey();
         Measure measure = entry.getValue();
         PastMeasureDto pastMeasure = pastMeasuresByMeasureKey.get(new MeasureKey(metricKey, measure.getCharacteristicId(), measure.getRuleId(), null));
@@ -175,6 +176,7 @@ public class ComputeMeasureVariationsStep implements ComputationStep {
     private final Map<MeasureKey, MeasureWithVariations> measuresWithVariations = new HashMap<>();
 
     public void add(Metric metric, final Measure measure, int variationIndex, double variationValue) {
+      checkArgument(measure.getDeveloper() == null, "%s does not support computing variations of Measures for Developer", getClass().getSimpleName());
       MeasureKey measureKey = new MeasureKey(metric.getKey(), measure.getCharacteristicId(), measure.getRuleId(), null);
       MeasureWithVariations measureWithVariations = measuresWithVariations.get(measureKey);
       if (measureWithVariations == null) {
@@ -250,6 +252,15 @@ public class ComputeMeasureVariationsStep implements ComputationStep {
     }
   }
 
+  private enum NotDeveloperMeasure implements Predicate<Map.Entry<String, Measure>> {
+    INSTANCE;
+
+    @Override
+    public boolean apply(@Nonnull Map.Entry<String, Measure> input) {
+      return input.getValue().getDeveloper() == null;
+    }
+  }
+
   @Override
   public String getDescription() {
     return "Compute measure variations";
index 1b74b2c70ca08c5b7cf1aa9d082099e8f9e2b888..07f848adc1fd8573c3cb2f9c8a3f075d97685869 100644 (file)
@@ -35,6 +35,7 @@ import org.sonar.db.measure.MeasureDto;
 import org.sonar.server.computation.batch.BatchReportReaderRule;
 import org.sonar.server.computation.batch.TreeRootHolderRule;
 import org.sonar.server.computation.component.Component;
+import org.sonar.server.computation.component.DumbDeveloper;
 import org.sonar.server.computation.component.ReportComponent;
 import org.sonar.server.computation.measure.Measure;
 import org.sonar.server.computation.measure.MeasureRepositoryRule;
@@ -48,6 +49,7 @@ import org.sonar.test.DbTests;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.sonar.db.component.SnapshotTesting.createForComponent;
 import static org.sonar.db.component.SnapshotTesting.newSnapshotForProject;
+import static org.sonar.server.computation.measure.Measure.newMeasureBuilder;
 
 @Category(DbTests.class)
 public class ReportComputeMeasureVariationsStepTest {
@@ -59,27 +61,23 @@ public class ReportComputeMeasureVariationsStepTest {
 
   static final ComponentDto PROJECT_DTO = ComponentTesting.newProjectDto();
 
-  static final Component PROJECT = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid(PROJECT_DTO.uuid()).build();
+  static final int PROJECT_REF = 1;
+  static final Component PROJECT = ReportComponent.builder(Component.Type.PROJECT, PROJECT_REF).setUuid(PROJECT_DTO.uuid()).build();
 
   @Rule
   public DbTester dbTester = DbTester.create(System2.INSTANCE);
-
   @Rule
   public BatchReportReaderRule reportReader = new BatchReportReaderRule();
-
   @Rule
   public PeriodsHolderRule periodsHolder = new PeriodsHolderRule();
-
   @Rule
   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
-
   @Rule
   public MetricRepositoryRule metricRepository = new MetricRepositoryRule()
     .add(ISSUES_METRIC)
     .add(DEBT_METRIC)
     .add(FILE_COMPLEXITY_METRIC)
     .add(BUILD_BREAKER_METRIC);
-
   @Rule
   public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
 
@@ -146,8 +144,8 @@ public class ReportComputeMeasureVariationsStepTest {
     Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid(PROJECT_DTO.uuid()).addChildren(directory).build();
     treeRootHolder.setRoot(project);
 
-    addRawMeasure(project, ISSUES_METRIC, Measure.newMeasureBuilder().create(80, null));
-    addRawMeasure(directory, ISSUES_METRIC, Measure.newMeasureBuilder().create(20, null));
+    addRawMeasure(project, ISSUES_METRIC, newMeasureBuilder().create(80, null));
+    addRawMeasure(directory, ISSUES_METRIC, newMeasureBuilder().create(20, null));
 
     underTest.execute();
 
@@ -180,7 +178,7 @@ public class ReportComputeMeasureVariationsStepTest {
 
     treeRootHolder.setRoot(PROJECT);
 
-    addRawMeasure(PROJECT, ISSUES_METRIC, Measure.newMeasureBuilder().create(80, null));
+    addRawMeasure(PROJECT, ISSUES_METRIC, newMeasureBuilder().create(80, null));
 
     underTest.execute();
 
@@ -204,17 +202,17 @@ public class ReportComputeMeasureVariationsStepTest {
       newMeasureDto(DEBT_METRIC.getId(), PROJECT_DTO.getId(), period1ProjectSnapshot.getId(), 10d),
       newMeasureDto(FILE_COMPLEXITY_METRIC.getId(), PROJECT_DTO.getId(), period1ProjectSnapshot.getId(), 2d),
       newMeasureDto(BUILD_BREAKER_METRIC.getId(), PROJECT_DTO.getId(), period1ProjectSnapshot.getId(), 1d)
-    );
+      );
     session.commit();
 
     periodsHolder.setPeriods(newPeriod(1, period1ProjectSnapshot));
 
     treeRootHolder.setRoot(PROJECT);
 
-    addRawMeasure(PROJECT, ISSUES_METRIC, Measure.newMeasureBuilder().create(80, null));
-    addRawMeasure(PROJECT, DEBT_METRIC, Measure.newMeasureBuilder().create(5L, null));
-    addRawMeasure(PROJECT, FILE_COMPLEXITY_METRIC, Measure.newMeasureBuilder().create(3d, null));
-    addRawMeasure(PROJECT, BUILD_BREAKER_METRIC, Measure.newMeasureBuilder().create(false, null));
+    addRawMeasure(PROJECT, ISSUES_METRIC, newMeasureBuilder().create(80, null));
+    addRawMeasure(PROJECT, DEBT_METRIC, newMeasureBuilder().create(5L, null));
+    addRawMeasure(PROJECT, FILE_COMPLEXITY_METRIC, newMeasureBuilder().create(3d, null));
+    addRawMeasure(PROJECT, BUILD_BREAKER_METRIC, newMeasureBuilder().create(false, null));
 
     underTest.execute();
 
@@ -226,6 +224,29 @@ public class ReportComputeMeasureVariationsStepTest {
     assertThat(measureRepository.getRawMeasure(PROJECT, BUILD_BREAKER_METRIC).get().getVariations().getVariation1()).isEqualTo(-1d);
   }
 
+  @Test
+  public void do_not_set_variations_on_numeric_metric_for_developer() {
+    SnapshotDto period1ProjectSnapshot = newSnapshotForProject(PROJECT_DTO);
+    dbClient.snapshotDao().insert(session, period1ProjectSnapshot);
+    dbClient.measureDao().insert(session,
+      newMeasureDto(ISSUES_METRIC.getId(), PROJECT_DTO.getId(), period1ProjectSnapshot.getId(), 60d)
+      );
+    session.commit();
+
+    periodsHolder.setPeriods(newPeriod(1, period1ProjectSnapshot));
+
+    treeRootHolder.setRoot(PROJECT);
+
+    DumbDeveloper developer = new DumbDeveloper("a");
+    measureRepository.addRawMeasure(PROJECT_REF, ISSUES_METRIC.getKey(), newMeasureBuilder().forDeveloper(developer).create(80, null));
+
+    underTest.execute();
+
+    assertThat(measureRepository.getRawMeasures(PROJECT).keys()).hasSize(1);
+
+    assertThat(measureRepository.getRawMeasure(PROJECT, ISSUES_METRIC, developer).get().hasVariations()).isFalse();
+  }
+
   private static MeasureDto newMeasureDto(int metricId, long projectId, long snapshotId, double value) {
     return new MeasureDto().setMetricId(metricId).setComponentId(projectId).setSnapshotId(snapshotId).setValue(value);
   }