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;
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));
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) {
}
}
+ 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";
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;
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 {
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);
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();
treeRootHolder.setRoot(PROJECT);
- addRawMeasure(PROJECT, ISSUES_METRIC, Measure.newMeasureBuilder().create(80, null));
+ addRawMeasure(PROJECT, ISSUES_METRIC, newMeasureBuilder().create(80, null));
underTest.execute();
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();
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);
}