]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8743 Handle only leak period when computing measure variations
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 7 Feb 2017 12:49:53 +0000 (13:49 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 9 Feb 2017 11:15:11 +0000 (12:15 +0100)
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/ComputeMeasureVariationsStep.java
server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/ReportComputeMeasureVariationsStepTest.java
server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/ViewsComputeMeasureVariationsStepTest.java

index 26593d0ffc5d29ac3158f1c6da6f119545ee9298..348642b058ea6f1ab2c0be248b0567b96fa42abf 100644 (file)
  */
 package org.sonar.server.computation.task.projectanalysis.step;
 
-import com.google.common.base.Function;
 import com.google.common.base.Optional;
-import com.google.common.base.Predicate;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
-import java.util.stream.Collectors;
+import java.util.function.Predicate;
 import java.util.stream.StreamSupport;
 import javax.annotation.CheckForNull;
-import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
+import org.sonar.core.util.stream.Collectors;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.measure.PastMeasureDto;
@@ -43,7 +41,6 @@ import org.sonar.server.computation.task.projectanalysis.component.TypeAwareVisi
 import org.sonar.server.computation.task.projectanalysis.measure.Measure;
 import org.sonar.server.computation.task.projectanalysis.measure.MeasureKey;
 import org.sonar.server.computation.task.projectanalysis.measure.MeasureRepository;
-import org.sonar.server.computation.task.projectanalysis.measure.MeasureVariations;
 import org.sonar.server.computation.task.projectanalysis.metric.Metric;
 import org.sonar.server.computation.task.projectanalysis.metric.MetricRepository;
 import org.sonar.server.computation.task.projectanalysis.period.Period;
@@ -73,7 +70,7 @@ public class ComputeMeasureVariationsStep implements ComputationStep {
   private final MeasureRepository measureRepository;
 
   public ComputeMeasureVariationsStep(DbClient dbClient, TreeRootHolder treeRootHolder, PeriodsHolder periodsHolder, MetricRepository metricRepository,
-    MeasureRepository measureRepository) {
+                                      MeasureRepository measureRepository) {
     this.dbClient = dbClient;
     this.treeRootHolder = treeRootHolder;
     this.periodsHolder = periodsHolder;
@@ -85,7 +82,7 @@ public class ComputeMeasureVariationsStep implements ComputationStep {
   public void execute() {
     DbSession dbSession = dbClient.openSession(false);
     try {
-      List<Metric> metrics = StreamSupport.stream(metricRepository.getAll().spliterator(), false).filter(NumericMetric.INSTANCE::apply).collect(Collectors.toList());
+      List<Metric> metrics = StreamSupport.stream(metricRepository.getAll().spliterator(), false).filter(isNumeric()).collect(Collectors.toList());
       new DepthTraversalTypeAwareCrawler(new VariationMeasuresVisitor(dbSession, metrics))
         .visit(treeRootHolder.getRoot());
     } finally {
@@ -103,7 +100,7 @@ public class ComputeMeasureVariationsStep implements ComputationStep {
       // measures on files are currently purged, so past measures are not available on files
       super(CrawlerDepthLimit.reportMaxDepth(DIRECTORY).withViewsMaxDepth(SUBVIEW), PRE_ORDER);
       this.session = session;
-      this.metricIds = metrics.stream().map(MetricDtoToMetricId.INSTANCE::apply).collect(Collectors.toSet());
+      this.metricIds = metrics.stream().map(Metric::getId).collect(Collectors.toSet());
       this.metrics = metrics;
     }
 
@@ -115,24 +112,25 @@ public class ComputeMeasureVariationsStep implements ComputationStep {
 
     private MeasuresWithVariationRepository computeMeasuresWithVariations(Component component) {
       MeasuresWithVariationRepository measuresWithVariationRepository = new MeasuresWithVariationRepository();
-      for (Period period : periodsHolder.getPeriods()) {
+      if (periodsHolder.hasPeriod()) {
+        Period period = periodsHolder.getPeriod();
         List<PastMeasureDto> pastMeasures = dbClient.measureDao()
           .selectPastMeasures(session, component.getUuid(), period.getAnalysisUuid(), metricIds);
-        setVariationMeasures(component, pastMeasures, period.getIndex(), measuresWithVariationRepository);
+        setVariationMeasures(component, pastMeasures, measuresWithVariationRepository);
       }
       return measuresWithVariationRepository;
     }
 
-    private void setVariationMeasures(Component component, List<PastMeasureDto> pastMeasures, int period, MeasuresWithVariationRepository measuresWithVariationRepository) {
+    private void setVariationMeasures(Component component, List<PastMeasureDto> pastMeasures, MeasuresWithVariationRepository measuresWithVariationRepository) {
       Map<MeasureKey, PastMeasureDto> pastMeasuresByMeasureKey = pastMeasures
         .stream()
         .collect(uniqueIndex(m -> new MeasureKey(metricRepository.getById((long) m.getMetricId()).getKey(), null), identity()));
       for (Metric metric : metrics) {
         Optional<Measure> measure = measureRepository.getRawMeasure(component, metric);
-        if (measure.isPresent() && !measure.get().hasVariations()) {
+        if (measure.isPresent() && !measure.get().hasVariation()) {
           PastMeasureDto pastMeasure = pastMeasuresByMeasureKey.get(new MeasureKey(metric.getKey(), null));
           double pastValue = (pastMeasure != null && pastMeasure.hasValue()) ? pastMeasure.getValue() : 0d;
-          measuresWithVariationRepository.add(metric, measure.get(), period, computeVariation(measure.get(), pastValue));
+          measuresWithVariationRepository.add(metric, measure.get(), computeVariation(measure.get(), pastValue));
         }
       }
     }
@@ -153,15 +151,10 @@ public class ComputeMeasureVariationsStep implements ComputationStep {
     }
 
     private void processMeasuresWithVariation(Component component, MeasuresWithVariationRepository measuresWithVariationRepository) {
-      for (MeasureWithVariations measureWithVariations : measuresWithVariationRepository.measures()) {
-        Metric metric = measureWithVariations.getMetric();
-        Measure measure = Measure.updatedMeasureBuilder(measureWithVariations.getMeasure())
-          .setVariations(new MeasureVariations(
-            measureWithVariations.getVariation(1),
-            measureWithVariations.getVariation(2),
-            measureWithVariations.getVariation(3),
-            measureWithVariations.getVariation(4),
-            measureWithVariations.getVariation(5)))
+      for (MeasureWithVariation measureWithVariation : measuresWithVariationRepository.measures()) {
+        Metric metric = measureWithVariation.getMetric();
+        Measure measure = Measure.updatedMeasureBuilder(measureWithVariation.getMeasure())
+          .setVariation(measureWithVariation.getVariation())
           .create();
         measureRepository.update(component, metric, measure);
       }
@@ -170,30 +163,26 @@ public class ComputeMeasureVariationsStep implements ComputationStep {
 
   private static final class MeasuresWithVariationRepository {
 
-    private final Map<MeasureKey, MeasureWithVariations> measuresWithVariations = new HashMap<>();
+    private final Map<MeasureKey, MeasureWithVariation> measuresWithVariations = new HashMap<>();
 
-    public void add(Metric metric, final Measure measure, int variationIndex, double variationValue) {
+    public void add(Metric metric, final Measure measure, double variationValue) {
       checkArgument(measure.getDeveloper() == null, "%s does not support computing variations of Measures for Developer", getClass().getSimpleName());
       MeasureKey measureKey = new MeasureKey(metric.getKey(), null);
-      MeasureWithVariations measureWithVariations = measuresWithVariations.get(measureKey);
-      if (measureWithVariations == null) {
-        measureWithVariations = new MeasureWithVariations(metric, measure);
-        measuresWithVariations.put(measureKey, measureWithVariations);
-      }
-      measureWithVariations.setVariation(variationIndex, variationValue);
+      MeasureWithVariation measureWithVariation = measuresWithVariations.computeIfAbsent(measureKey, k -> new MeasureWithVariation(metric, measure));
+      measureWithVariation.setVariation(variationValue);
     }
 
-    public Collection<MeasureWithVariations> measures() {
+    public Collection<MeasureWithVariation> measures() {
       return measuresWithVariations.values();
     }
   }
 
-  private static final class MeasureWithVariations {
+  private static final class MeasureWithVariation {
     private final Metric metric;
     private final Measure measure;
-    private final Double[] variations = new Double[5];
+    private Double variation;
 
-    MeasureWithVariations(Metric metric, Measure measure) {
+    MeasureWithVariation(Metric metric, Measure measure) {
       this.metric = metric;
       this.measure = measure;
     }
@@ -206,37 +195,24 @@ public class ComputeMeasureVariationsStep implements ComputationStep {
       return metric;
     }
 
-    public void setVariation(int index, @Nullable Double value) {
-      variations[index - 1] = value;
+    public void setVariation(@Nullable Double value) {
+      this.variation = value;
     }
 
     @CheckForNull
-    public Double getVariation(int index) {
-      return variations[index - 1];
+    public Double getVariation() {
+      return variation;
     }
   }
 
-  private enum MetricDtoToMetricId implements Function<Metric, Integer> {
-    INSTANCE;
-
-    @Nullable
-    @Override
-    public Integer apply(@Nonnull Metric metric) {
-      return metric.getId();
-    }
-  }
-
-  private enum NumericMetric implements Predicate<Metric> {
-    INSTANCE;
-
-    @Override
-    public boolean apply(@Nonnull Metric metric) {
+  private static Predicate<Metric> isNumeric() {
+    return metric -> {
       Measure.ValueType valueType = metric.getType().getValueType();
       return Measure.ValueType.INT.equals(valueType)
         || Measure.ValueType.LONG.equals(valueType)
         || Measure.ValueType.DOUBLE.equals(valueType)
         || Measure.ValueType.BOOLEAN.equals(valueType);
-    }
+    };
   }
 
   @Override
index ee7bab1d88678e469289d649a60c640f2ec707de..ead320828a8e5ef8d57384caa1e3b5d64f107d66 100644 (file)
@@ -37,7 +37,6 @@ import org.sonar.server.computation.task.projectanalysis.component.ReportCompone
 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolderRule;
 import org.sonar.server.computation.task.projectanalysis.measure.Measure;
 import org.sonar.server.computation.task.projectanalysis.measure.MeasureRepositoryRule;
-import org.sonar.server.computation.task.projectanalysis.measure.MeasureVariations;
 import org.sonar.server.computation.task.projectanalysis.metric.Metric;
 import org.sonar.server.computation.task.projectanalysis.metric.MetricImpl;
 import org.sonar.server.computation.task.projectanalysis.metric.MetricRepositoryRule;
@@ -59,7 +58,6 @@ public class ReportComputeMeasureVariationsStepTest {
   private static final int PROJECT_REF = 1;
   private static final Component PROJECT = ReportComponent.builder(Component.Type.PROJECT, PROJECT_REF).setUuid(PROJECT_UUID).build();
 
-
   @Rule
   public DbTester dbTester = DbTester.create(System2.INSTANCE);
   @Rule
@@ -80,17 +78,15 @@ public class ReportComputeMeasureVariationsStepTest {
 
   private ComponentDto project;
 
-  DbSession session = dbTester.getSession();
+  private DbSession session = dbTester.getSession();
 
-  DbClient dbClient = dbTester.getDbClient();
+  private DbClient dbClient = dbTester.getDbClient();
 
-  ComputeMeasureVariationsStep underTest;
+  private ComputeMeasureVariationsStep underTest = new ComputeMeasureVariationsStep(dbClient, treeRootHolder, periodsHolder, metricRepository, measureRepository);
 
   @Before
   public void setUp() {
     project = dbTester.components().insertProject(dbTester.organizations().insert(), PROJECT_UUID);
-
-    underTest = new ComputeMeasureVariationsStep(dbClient, treeRootHolder, periodsHolder, metricRepository, measureRepository);
   }
 
   @Test
@@ -100,7 +96,7 @@ public class ReportComputeMeasureVariationsStepTest {
     dbClient.measureDao().insert(session, newMeasureDto(ISSUES_METRIC.getId(), PROJECT_UUID, period1ProjectSnapshot.getUuid(), 60d));
     session.commit();
 
-    periodsHolder.setPeriods(newPeriod(1, period1ProjectSnapshot));
+    periodsHolder.setPeriod(newPeriod(1, period1ProjectSnapshot));
 
     treeRootHolder.setRoot(PROJECT);
 
@@ -113,7 +109,7 @@ public class ReportComputeMeasureVariationsStepTest {
   public void do_nothing_when_no_period() {
     Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid(PROJECT_UUID).build();
     treeRootHolder.setRoot(project);
-    periodsHolder.setPeriods();
+    periodsHolder.setPeriod(null);
 
     underTest.execute();
 
@@ -133,7 +129,7 @@ public class ReportComputeMeasureVariationsStepTest {
     dbClient.measureDao().insert(session, newMeasureDto(ISSUES_METRIC.getId(), directoryDto.uuid(), period1Snapshot.getUuid(), 10d));
     session.commit();
 
-    periodsHolder.setPeriods(newPeriod(1, period1Snapshot));
+    periodsHolder.setPeriod(newPeriod(1, period1Snapshot));
 
     Component directory = ReportComponent.builder(Component.Type.DIRECTORY, 2).setUuid(directoryDto.uuid()).build();
     Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid(PROJECT_UUID).addChildren(directory).build();
@@ -144,8 +140,8 @@ public class ReportComputeMeasureVariationsStepTest {
 
     underTest.execute();
 
-    assertThat(measureRepository.getRawMeasure(project, ISSUES_METRIC).get().getVariations().getVariation1()).isEqualTo(20d);
-    assertThat(measureRepository.getRawMeasure(directory, ISSUES_METRIC).get().getVariations().getVariation1()).isEqualTo(10d);
+    assertThat(measureRepository.getRawMeasure(project, ISSUES_METRIC).get().getVariation()).isEqualTo(20d);
+    assertThat(measureRepository.getRawMeasure(directory, ISSUES_METRIC).get().getVariation()).isEqualTo(10d);
   }
 
   @Test
@@ -161,7 +157,7 @@ public class ReportComputeMeasureVariationsStepTest {
     dbClient.measureDao().insert(session, newMeasureDto(ISSUES_METRIC.getId(), directoryDto.uuid(), period1Snapshot.getUuid(), 10d));
     session.commit();
 
-    periodsHolder.setPeriods(newPeriod(1, period1Snapshot));
+    periodsHolder.setPeriod(newPeriod(1, period1Snapshot));
 
     Component directory = ReportComponent.builder(Component.Type.DIRECTORY, 2).setUuid(directoryDto.uuid()).build();
     Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid(PROJECT_UUID).addChildren(directory).build();
@@ -172,8 +168,8 @@ public class ReportComputeMeasureVariationsStepTest {
 
     underTest.execute();
 
-    assertThat(measureRepository.getRawMeasure(project, ISSUES_METRIC).get().getVariations().getVariation1()).isEqualTo(0d);
-    assertThat(measureRepository.getRawMeasure(directory, ISSUES_METRIC).get().getVariations().getVariation1()).isEqualTo(0d);
+    assertThat(measureRepository.getRawMeasure(project, ISSUES_METRIC).get().getVariation()).isEqualTo(0d);
+    assertThat(measureRepository.getRawMeasure(directory, ISSUES_METRIC).get().getVariation()).isEqualTo(0d);
   }
 
   @Test
@@ -187,7 +183,7 @@ public class ReportComputeMeasureVariationsStepTest {
     dbClient.measureDao().insert(session, newMeasureDto(ISSUES_METRIC.getId(), PROJECT_UUID, currentProjectSnapshot.getUuid(), 60d));
     session.commit();
 
-    periodsHolder.setPeriods(newPeriod(1, past1ProjectSnapshot));
+    periodsHolder.setPeriod(newPeriod(1, past1ProjectSnapshot));
 
     // Directory has just been added => no snapshot
     Component directory = ReportComponent.builder(Component.Type.DIRECTORY, 2).setUuid("DIRECTORY").build();
@@ -199,49 +195,9 @@ public class ReportComputeMeasureVariationsStepTest {
 
     underTest.execute();
 
-    assertThat(measureRepository.getRawMeasure(project, ISSUES_METRIC).get().getVariations().getVariation1()).isEqualTo(30d);
+    assertThat(measureRepository.getRawMeasure(project, ISSUES_METRIC).get().getVariation()).isEqualTo(30d);
     // Variation should be the raw value
-    assertThat(measureRepository.getRawMeasure(directory, ISSUES_METRIC).get().getVariations().getVariation1()).isEqualTo(10d);
-  }
-
-  @Test
-  public void set_variations_on_all_periods() {
-    SnapshotDto period1ProjectSnapshot = newAnalysis(project).setLast(false);
-    SnapshotDto period2ProjectSnapshot = newAnalysis(project).setLast(false);
-    SnapshotDto period3ProjectSnapshot = newAnalysis(project).setLast(false);
-    SnapshotDto period4ProjectSnapshot = newAnalysis(project).setLast(false);
-    SnapshotDto period5ProjectSnapshot = newAnalysis(project).setLast(false);
-    dbClient.snapshotDao().insert(session, period1ProjectSnapshot, period2ProjectSnapshot, period3ProjectSnapshot, period4ProjectSnapshot, period5ProjectSnapshot);
-
-    dbClient.measureDao().insert(session,
-      newMeasureDto(ISSUES_METRIC.getId(), PROJECT_UUID, period1ProjectSnapshot.getUuid(), 0d),
-      newMeasureDto(ISSUES_METRIC.getId(), PROJECT_UUID, period2ProjectSnapshot.getUuid(), 20d),
-      newMeasureDto(ISSUES_METRIC.getId(), PROJECT_UUID, period3ProjectSnapshot.getUuid(), 40d),
-      newMeasureDto(ISSUES_METRIC.getId(), PROJECT_UUID, period4ProjectSnapshot.getUuid(), 80d),
-      newMeasureDto(ISSUES_METRIC.getId(), PROJECT_UUID, period5ProjectSnapshot.getUuid(), 100d));
-    session.commit();
-
-    periodsHolder.setPeriods(newPeriod(1, period1ProjectSnapshot),
-      newPeriod(2, period2ProjectSnapshot),
-      newPeriod(3, period3ProjectSnapshot),
-      newPeriod(4, period4ProjectSnapshot),
-      newPeriod(5, period5ProjectSnapshot));
-
-    treeRootHolder.setRoot(PROJECT);
-
-    addRawMeasure(PROJECT, ISSUES_METRIC, newMeasureBuilder().create(80, null));
-
-    underTest.execute();
-
-    assertThat(measureRepository.getRawMeasures(PROJECT).keys()).hasSize(1);
-
-    Measure measure = measureRepository.getRawMeasure(PROJECT, ISSUES_METRIC).get();
-    assertThat(measure.hasVariations()).isTrue();
-    assertThat(measure.getVariations().getVariation1()).isEqualTo(80d);
-    assertThat(measure.getVariations().getVariation2()).isEqualTo(60d);
-    assertThat(measure.getVariations().getVariation3()).isEqualTo(40d);
-    assertThat(measure.getVariations().getVariation4()).isEqualTo(0d);
-    assertThat(measure.getVariations().getVariation5()).isEqualTo(-20d);
+    assertThat(measureRepository.getRawMeasure(directory, ISSUES_METRIC).get().getVariation()).isEqualTo(10d);
   }
 
   @Test
@@ -255,7 +211,7 @@ public class ReportComputeMeasureVariationsStepTest {
       newMeasureDto(BUILD_BREAKER_METRIC.getId(), PROJECT_UUID, period1ProjectSnapshot.getUuid(), 1d));
     session.commit();
 
-    periodsHolder.setPeriods(newPeriod(1, period1ProjectSnapshot));
+    periodsHolder.setPeriod(newPeriod(1, period1ProjectSnapshot));
 
     treeRootHolder.setRoot(PROJECT);
 
@@ -268,21 +224,21 @@ public class ReportComputeMeasureVariationsStepTest {
 
     assertThat(measureRepository.getRawMeasures(PROJECT).keys()).hasSize(4);
 
-    assertThat(measureRepository.getRawMeasure(PROJECT, ISSUES_METRIC).get().getVariations().getVariation1()).isEqualTo(20d);
-    assertThat(measureRepository.getRawMeasure(PROJECT, DEBT_METRIC).get().getVariations().getVariation1()).isEqualTo(-5d);
-    assertThat(measureRepository.getRawMeasure(PROJECT, FILE_COMPLEXITY_METRIC).get().getVariations().getVariation1()).isEqualTo(1d);
-    assertThat(measureRepository.getRawMeasure(PROJECT, BUILD_BREAKER_METRIC).get().getVariations().getVariation1()).isEqualTo(-1d);
+    assertThat(measureRepository.getRawMeasure(PROJECT, ISSUES_METRIC).get().getVariation()).isEqualTo(20d);
+    assertThat(measureRepository.getRawMeasure(PROJECT, DEBT_METRIC).get().getVariation()).isEqualTo(-5d);
+    assertThat(measureRepository.getRawMeasure(PROJECT, FILE_COMPLEXITY_METRIC).get().getVariation()).isEqualTo(1d);
+    assertThat(measureRepository.getRawMeasure(PROJECT, BUILD_BREAKER_METRIC).get().getVariation()).isEqualTo(-1d);
   }
 
   @Test
-  public void do_not_set_variations_on_numeric_metric_for_developer() {
+  public void do_not_set_variation_on_numeric_metric_for_developer() {
     SnapshotDto period1ProjectSnapshot = newAnalysis(project);
     dbClient.snapshotDao().insert(session, period1ProjectSnapshot);
     dbClient.measureDao().insert(session,
       newMeasureDto(ISSUES_METRIC.getId(), PROJECT_UUID, period1ProjectSnapshot.getUuid(), 60d));
     session.commit();
 
-    periodsHolder.setPeriods(newPeriod(1, period1ProjectSnapshot));
+    periodsHolder.setPeriod(newPeriod(1, period1ProjectSnapshot));
 
     treeRootHolder.setRoot(PROJECT);
 
@@ -293,24 +249,24 @@ public class ReportComputeMeasureVariationsStepTest {
 
     assertThat(measureRepository.getRawMeasures(PROJECT).keys()).hasSize(1);
 
-    assertThat(measureRepository.getRawMeasure(PROJECT, ISSUES_METRIC, developer).get().hasVariations()).isFalse();
+    assertThat(measureRepository.getRawMeasure(PROJECT, ISSUES_METRIC, developer).get().hasVariation()).isFalse();
   }
 
   @Test
-  public void does_not_update_existing_variations() throws Exception {
+  public void does_not_update_existing_variation() throws Exception {
     SnapshotDto period1ProjectSnapshot = newAnalysis(project);
     dbClient.snapshotDao().insert(session, period1ProjectSnapshot);
     dbClient.measureDao().insert(session, newMeasureDto(NEW_DEBT.getId(), PROJECT_UUID, period1ProjectSnapshot.getUuid(), 60d));
     session.commit();
-    periodsHolder.setPeriods(newPeriod(1, period1ProjectSnapshot));
+    periodsHolder.setPeriod(newPeriod(1, period1ProjectSnapshot));
     treeRootHolder.setRoot(PROJECT);
 
-    addRawMeasure(PROJECT, NEW_DEBT, newMeasureBuilder().setVariations(new MeasureVariations(10d)).createNoValue());
+    addRawMeasure(PROJECT, NEW_DEBT, newMeasureBuilder().setVariation(10d).createNoValue());
 
     underTest.execute();
 
     // As the measure has already variations it has been ignored, then variations will be the same
-    assertThat(measureRepository.getRawMeasure(PROJECT, NEW_DEBT).get().getVariations().getVariation1()).isEqualTo(10d);
+    assertThat(measureRepository.getRawMeasure(PROJECT, NEW_DEBT).get().getVariation()).isEqualTo(10d);
   }
 
   private static MeasureDto newMeasureDto(int metricId, String componentUuid, String analysisUuid, double value) {
index 48ed2e47c2b5db9f6ff85b7a3bcaeb8978914f7a..c8461707d5f8f7fcde493c804a696f508289657a 100644 (file)
@@ -31,8 +31,8 @@ import org.sonar.db.component.ComponentTesting;
 import org.sonar.db.component.SnapshotDto;
 import org.sonar.db.measure.MeasureDto;
 import org.sonar.server.computation.task.projectanalysis.batch.BatchReportReaderRule;
-import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolderRule;
 import org.sonar.server.computation.task.projectanalysis.component.Component;
+import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolderRule;
 import org.sonar.server.computation.task.projectanalysis.component.ViewsComponent;
 import org.sonar.server.computation.task.projectanalysis.measure.Measure;
 import org.sonar.server.computation.task.projectanalysis.measure.MeasureRepositoryRule;
@@ -76,19 +76,17 @@ public class ViewsComputeMeasureVariationsStepTest {
   @Rule
   public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
 
-  DbSession session = dbTester.getSession();
+  private DbSession session = dbTester.getSession();
 
-  DbClient dbClient = dbTester.getDbClient();
+  private DbClient dbClient = dbTester.getDbClient();
 
   private ComponentDto view;
 
-  ComputeMeasureVariationsStep underTest;
+  private ComputeMeasureVariationsStep underTest = new ComputeMeasureVariationsStep(dbClient, treeRootHolder, periodsHolder, metricRepository, measureRepository);
 
   @Before
   public void setUp() {
     view = dbTester.components().insertView(dbTester.organizations().insert(), VIEW_UUID);
-
-    underTest = new ComputeMeasureVariationsStep(dbClient, treeRootHolder, periodsHolder, metricRepository, measureRepository);
   }
 
   @Test
@@ -98,7 +96,7 @@ public class ViewsComputeMeasureVariationsStepTest {
     dbClient.measureDao().insert(session, newMeasureDto(ISSUES_METRIC.getId(), VIEW_UUID, period1ViewSnapshot.getUuid(), 60d));
     session.commit();
 
-    periodsHolder.setPeriods(newPeriod(1, period1ViewSnapshot));
+    periodsHolder.setPeriod(newPeriod(1, period1ViewSnapshot));
 
     treeRootHolder.setRoot(VIEW);
 
@@ -111,7 +109,7 @@ public class ViewsComputeMeasureVariationsStepTest {
   public void do_nothing_when_no_period() {
     Component view = ViewsComponent.builder(Component.Type.VIEW, 1).setUuid(VIEW_UUID).build();
     treeRootHolder.setRoot(view);
-    periodsHolder.setPeriods();
+    periodsHolder.setPeriod(null);
 
     underTest.execute();
 
@@ -131,7 +129,7 @@ public class ViewsComputeMeasureVariationsStepTest {
     dbClient.measureDao().insert(session, newMeasureDto(ISSUES_METRIC.getId(), subviewDto.uuid(), period1Snapshot.getUuid(), 10d));
     session.commit();
 
-    periodsHolder.setPeriods(newPeriod(1, period1Snapshot));
+    periodsHolder.setPeriod(newPeriod(1, period1Snapshot));
 
     Component subview = ViewsComponent.builder(Component.Type.SUBVIEW, 2).setUuid(subviewDto.uuid()).build();
     Component view = ViewsComponent.builder(Component.Type.VIEW, 1).setUuid(VIEW_UUID).addChildren(subview).build();
@@ -142,48 +140,8 @@ public class ViewsComputeMeasureVariationsStepTest {
 
     underTest.execute();
 
-    assertThat(measureRepository.getRawMeasure(view, ISSUES_METRIC).get().getVariations().getVariation1()).isEqualTo(20d);
-    assertThat(measureRepository.getRawMeasure(subview, ISSUES_METRIC).get().getVariations().getVariation1()).isEqualTo(10d);
-  }
-
-  @Test
-  public void set_variations_on_all_periods() {
-    SnapshotDto period1ViewSnapshot = newAnalysis(view).setLast(false);
-    SnapshotDto period2ViewSnapshot = newAnalysis(view).setLast(false);
-    SnapshotDto period3ViewSnapshot = newAnalysis(view).setLast(false);
-    SnapshotDto period4ViewSnapshot = newAnalysis(view).setLast(false);
-    SnapshotDto period5ViewSnapshot = newAnalysis(view).setLast(false);
-    dbClient.snapshotDao().insert(session, period1ViewSnapshot, period2ViewSnapshot, period3ViewSnapshot, period4ViewSnapshot, period5ViewSnapshot);
-
-    dbClient.measureDao().insert(session,
-      newMeasureDto(ISSUES_METRIC.getId(), VIEW_UUID, period1ViewSnapshot.getUuid(), 0d),
-      newMeasureDto(ISSUES_METRIC.getId(), VIEW_UUID, period2ViewSnapshot.getUuid(), 20d),
-      newMeasureDto(ISSUES_METRIC.getId(), VIEW_UUID, period3ViewSnapshot.getUuid(), 40d),
-      newMeasureDto(ISSUES_METRIC.getId(), VIEW_UUID, period4ViewSnapshot.getUuid(), 80d),
-      newMeasureDto(ISSUES_METRIC.getId(), VIEW_UUID, period5ViewSnapshot.getUuid(), 100d));
-    session.commit();
-
-    periodsHolder.setPeriods(newPeriod(1, period1ViewSnapshot),
-      newPeriod(2, period2ViewSnapshot),
-      newPeriod(3, period3ViewSnapshot),
-      newPeriod(4, period4ViewSnapshot),
-      newPeriod(5, period5ViewSnapshot));
-
-    treeRootHolder.setRoot(VIEW);
-
-    addRawMeasure(VIEW, ISSUES_METRIC, Measure.newMeasureBuilder().create(80, null));
-
-    underTest.execute();
-
-    assertThat(measureRepository.getRawMeasures(VIEW).keys()).hasSize(1);
-
-    Measure measure = measureRepository.getRawMeasure(VIEW, ISSUES_METRIC).get();
-    assertThat(measure.hasVariations()).isTrue();
-    assertThat(measure.getVariations().getVariation1()).isEqualTo(80d);
-    assertThat(measure.getVariations().getVariation2()).isEqualTo(60d);
-    assertThat(measure.getVariations().getVariation3()).isEqualTo(40d);
-    assertThat(measure.getVariations().getVariation4()).isEqualTo(0d);
-    assertThat(measure.getVariations().getVariation5()).isEqualTo(-20d);
+    assertThat(measureRepository.getRawMeasure(view, ISSUES_METRIC).get().getVariation()).isEqualTo(20d);
+    assertThat(measureRepository.getRawMeasure(subview, ISSUES_METRIC).get().getVariation()).isEqualTo(10d);
   }
 
   @Test
@@ -194,11 +152,10 @@ public class ViewsComputeMeasureVariationsStepTest {
       newMeasureDto(ISSUES_METRIC.getId(), VIEW_UUID, period1ViewSnapshot.getUuid(), 60d),
       newMeasureDto(DEBT_METRIC.getId(), VIEW_UUID, period1ViewSnapshot.getUuid(), 10d),
       newMeasureDto(FILE_COMPLEXITY_METRIC.getId(), VIEW_UUID, period1ViewSnapshot.getUuid(), 2d),
-      newMeasureDto(BUILD_BREAKER_METRIC.getId(), VIEW_UUID, period1ViewSnapshot.getUuid(), 1d)
-      );
+      newMeasureDto(BUILD_BREAKER_METRIC.getId(), VIEW_UUID, period1ViewSnapshot.getUuid(), 1d));
     session.commit();
 
-    periodsHolder.setPeriods(newPeriod(1, period1ViewSnapshot));
+    periodsHolder.setPeriod(newPeriod(1, period1ViewSnapshot));
 
     treeRootHolder.setRoot(VIEW);
 
@@ -211,10 +168,10 @@ public class ViewsComputeMeasureVariationsStepTest {
 
     assertThat(measureRepository.getRawMeasures(VIEW).keys()).hasSize(4);
 
-    assertThat(measureRepository.getRawMeasure(VIEW, ISSUES_METRIC).get().getVariations().getVariation1()).isEqualTo(20d);
-    assertThat(measureRepository.getRawMeasure(VIEW, DEBT_METRIC).get().getVariations().getVariation1()).isEqualTo(-5d);
-    assertThat(measureRepository.getRawMeasure(VIEW, FILE_COMPLEXITY_METRIC).get().getVariations().getVariation1()).isEqualTo(1d);
-    assertThat(measureRepository.getRawMeasure(VIEW, BUILD_BREAKER_METRIC).get().getVariations().getVariation1()).isEqualTo(-1d);
+    assertThat(measureRepository.getRawMeasure(VIEW, ISSUES_METRIC).get().getVariation()).isEqualTo(20d);
+    assertThat(measureRepository.getRawMeasure(VIEW, DEBT_METRIC).get().getVariation()).isEqualTo(-5d);
+    assertThat(measureRepository.getRawMeasure(VIEW, FILE_COMPLEXITY_METRIC).get().getVariation()).isEqualTo(1d);
+    assertThat(measureRepository.getRawMeasure(VIEW, BUILD_BREAKER_METRIC).get().getVariation()).isEqualTo(-1d);
   }
 
   private static MeasureDto newMeasureDto(int metricId, String componentUuid, String analysisUuid, double value) {