]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-21778 Fix computation of measures when project has not been analyzed yet
authorLéo Geoffroy <leo.geoffroy@sonarsource.com>
Fri, 15 Mar 2024 12:44:00 +0000 (13:44 +0100)
committersonartech <sonartech@sonarsource.com>
Mon, 18 Mar 2024 20:02:30 +0000 (20:02 +0000)
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/formula/ImpactSumFormula.java [deleted file]
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/formula/counter/IntSumCounter.java
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/formula/counter/LongSumCounter.java
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/formula/counter/SumCounter.java
server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/formula/ImpactSumFormulaTest.java [deleted file]

diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/formula/ImpactSumFormula.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/formula/ImpactSumFormula.java
deleted file mode 100644 (file)
index 5c31d78..0000000
+++ /dev/null
@@ -1,92 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2024 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- */
-package org.sonar.ce.task.projectanalysis.formula;
-
-import java.util.Optional;
-import org.sonar.ce.task.projectanalysis.measure.Measure;
-import org.sonar.server.measure.ImpactMeasureBuilder;
-
-import static java.util.Objects.requireNonNull;
-
-public class ImpactSumFormula implements Formula<ImpactSumFormula.ImpactCounter> {
-
-  private final String metricKey;
-
-
-  private ImpactSumFormula(String metricKey) {
-    this.metricKey = requireNonNull(metricKey, "Metric key cannot be null");
-  }
-
-  @Override
-  public ImpactSumFormula.ImpactCounter createNewCounter() {
-    return new ImpactSumFormula.ImpactCounter();
-  }
-
-  public static ImpactSumFormula createImpactSumFormula(String metricKey) {
-    return new ImpactSumFormula(metricKey);
-  }
-
-  @Override
-  public Optional<Measure> createMeasure(ImpactCounter counter, CreateMeasureContext context) {
-    return counter.getValue().map(v -> Measure.newMeasureBuilder().create(v));
-  }
-
-  @Override
-  public String[] getOutputMetricKeys() {
-    return new String[]{metricKey};
-  }
-
-  class ImpactCounter implements Counter<ImpactSumFormula.ImpactCounter> {
-
-    private boolean initialized = false;
-    private boolean hasEmptyValue = false;
-    private final ImpactMeasureBuilder measureImpactBuilder = ImpactMeasureBuilder.createEmpty();
-
-    @Override
-    public void aggregate(ImpactSumFormula.ImpactCounter counter) {
-      Optional<String> value = counter.getValue();
-      if (value.isPresent()) {
-        initialized = true;
-        measureImpactBuilder.add(ImpactMeasureBuilder.fromString(value.get()));
-      } else {
-        hasEmptyValue = true;
-      }
-    }
-
-    @Override
-    public void initialize(CounterInitializationContext context) {
-      Optional<Measure> measureOptional = context.getMeasure(metricKey);
-      String data = measureOptional.map(Measure::getData).orElse(null);
-      if (data != null) {
-        initialized = true;
-        measureImpactBuilder.add(ImpactMeasureBuilder.fromString(data));
-      } else {
-        hasEmptyValue = true;
-      }
-    }
-
-    public Optional<String> getValue() {
-      if (initialized && !hasEmptyValue) {
-        return Optional.ofNullable(measureImpactBuilder.buildAsString());
-      }
-      return Optional.empty();
-    }
-  }
-}
index e552b052b853d0cfd0257b32032d2fbcb4f33257..a004a52d15f260c75327451399f819fd147fa5eb 100644 (file)
@@ -65,7 +65,8 @@ public class IntSumCounter implements SumCounter<Integer, IntSumCounter> {
     }
   }
 
-  private void addValue(int newValue) {
+  @Override
+  public void addValue(Integer newValue) {
     initialized = true;
     value += newValue;
   }
index ae1e53d72472c6cfa7ccbf2f7d7116b5e733dd2e..9e1d70203a06379f10b89c8272d559799efb19f2 100644 (file)
@@ -65,7 +65,8 @@ public class LongSumCounter implements SumCounter<Long, LongSumCounter> {
     }
   }
 
-  private void addValue(long newValue) {
+  @Override
+  public void addValue(Long newValue) {
     initialized = true;
     value += newValue;
   }
index ab7b34ad2fff48f8a600124b284d7e7d0759170b..408d9d19dd63aa3c9a9b27aa43856a21e680a8ec 100644 (file)
@@ -22,7 +22,9 @@ package org.sonar.ce.task.projectanalysis.formula.counter;
 import java.util.Optional;
 import org.sonar.ce.task.projectanalysis.formula.Counter;
 
-public interface SumCounter<T extends Number, COUNTER extends SumCounter<T, COUNTER>> extends Counter<COUNTER> {
+public interface SumCounter<T, COUNTER extends SumCounter<T, COUNTER>> extends Counter<COUNTER> {
 
   Optional<T> getValue();
+
+  void addValue(T value);
 }
diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/formula/ImpactSumFormulaTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/formula/ImpactSumFormulaTest.java
deleted file mode 100644 (file)
index b0eadc3..0000000
+++ /dev/null
@@ -1,147 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2024 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- */
-package org.sonar.ce.task.projectanalysis.formula;
-
-import com.google.gson.Gson;
-import java.util.LinkedHashMap;
-import java.util.Optional;
-import javax.annotation.Nullable;
-import org.junit.Test;
-import org.sonar.api.issue.impact.Severity;
-import org.sonar.ce.task.projectanalysis.measure.Measure;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-public class ImpactSumFormulaTest {
-
-  public static final ImpactSumFormula IMPACT_SUM_FORMULA = ImpactSumFormula.createImpactSumFormula("metricKey");
-  private final Gson gson = new Gson();
-
-  private final CounterInitializationContext counterInitializationContext = mock(CounterInitializationContext.class);
-
-  private final CreateMeasureContext createMeasureContext = mock(CreateMeasureContext.class);
-
-  @Test
-  public void getOutputMetricKeys_shouldReturnCorrectMetrics() {
-    assertThat(IMPACT_SUM_FORMULA.getOutputMetricKeys()).containsExactly("metricKey");
-  }
-
-  @Test
-  public void createMeasure_whenCounterReturnsValue_shouldReturnExpectedMeasure() {
-    ImpactSumFormula.ImpactCounter counter = IMPACT_SUM_FORMULA.createNewCounter();
-    String value = newImpactJson(4, 2, 1, 1);
-    addMeasure("metricKey", value);
-    counter.initialize(counterInitializationContext);
-
-    assertThat(IMPACT_SUM_FORMULA.createMeasure(counter, createMeasureContext)).get().extracting(Measure::getData)
-      .isEqualTo(value);
-  }
-
-  @Test
-  public void createMeasure_whenCounterReturnsNoValue_shouldReturnNoMeasure() {
-    ImpactSumFormula.ImpactCounter counter = IMPACT_SUM_FORMULA.createNewCounter();
-    assertThat(IMPACT_SUM_FORMULA.createMeasure(counter, createMeasureContext)).isEmpty();
-  }
-
-  @Test
-  public void createNewCounter_shouldReturnExpectedCounter() {
-    assertThat(IMPACT_SUM_FORMULA.createNewCounter()).isNotNull()
-      .isInstanceOf(ImpactSumFormula.ImpactCounter.class);
-  }
-
-  @Test
-  public void getValue_whenInitialized_shouldReturnExpectedValue() {
-    ImpactSumFormula.ImpactCounter counter = IMPACT_SUM_FORMULA.createNewCounter();
-    String value = newImpactJson(4, 2, 1, 1);
-    addMeasure("metricKey", value);
-    counter.initialize(counterInitializationContext);
-    assertThat(counter.getValue()).get().isEqualTo(value);
-  }
-
-  @Test
-  public void getValue_whenAggregatingExistingValue_shouldReturnAggregationResult() {
-    ImpactSumFormula.ImpactCounter counter = IMPACT_SUM_FORMULA.createNewCounter();
-    String value = newImpactJson(4, 2, 1, 1);
-    addMeasure("metricKey", value);
-    counter.initialize(counterInitializationContext);
-
-    ImpactSumFormula.ImpactCounter counter2 = IMPACT_SUM_FORMULA.createNewCounter();
-    String value2 = newImpactJson(3, 1, 1, 1);
-    addMeasure("metricKey", value2);
-    counter2.initialize(counterInitializationContext);
-
-    counter.aggregate(counter2);
-    assertThat(counter.getValue()).get().isEqualTo(newImpactJson(7, 3, 2, 2));
-  }
-
-  @Test
-  public void getValue_whenAggregatingUninitializedValue_shouldReturnEmptyValue() {
-    ImpactSumFormula.ImpactCounter counter = IMPACT_SUM_FORMULA.createNewCounter();
-    String value = newImpactJson(4, 2, 1, 1);
-    addMeasure("metricKey", value);
-    counter.initialize(counterInitializationContext);
-
-    ImpactSumFormula.ImpactCounter counter2 = IMPACT_SUM_FORMULA.createNewCounter();
-
-    counter.aggregate(counter2);
-    assertThat(counter.getValue()).isEmpty();
-  }
-
-  @Test
-  public void getValue_whenAggregatingEmptyValue_shouldReturnEmptyValue() {
-
-    ImpactSumFormula.ImpactCounter counter = IMPACT_SUM_FORMULA.createNewCounter();
-    String value = newImpactJson(4, 2, 1, 1);
-    addMeasure("metricKey", value);
-    counter.initialize(counterInitializationContext);
-
-    ImpactSumFormula.ImpactCounter counter2 = IMPACT_SUM_FORMULA.createNewCounter();
-    addMeasure("metricKey", null);
-    counter2.initialize(counterInitializationContext);
-
-    counter.aggregate(counter2);
-
-    assertThat(counter.getValue()).isEmpty();
-  }
-
-  @Test
-  public void getValue_whenNotInitialized_shouldReturnEmpty() {
-    ImpactSumFormula.ImpactCounter counter = IMPACT_SUM_FORMULA.createNewCounter();
-    assertThat(counter.getValue()).isEmpty();
-  }
-
-
-  public String newImpactJson(Integer total, Integer high, Integer medium, Integer low) {
-    LinkedHashMap<Object, Object> map = new LinkedHashMap<>();
-    map.put(Severity.LOW.name(), low);
-    map.put(Severity.MEDIUM.name(), medium);
-    map.put(Severity.HIGH.name(), high);
-    map.put("total", total);
-    return gson.toJson(map);
-  }
-
-  private void addMeasure(String metricKey, @Nullable String value) {
-    when(counterInitializationContext.getMeasure(metricKey)).thenReturn(Optional.ofNullable(value).map(v -> Measure.newMeasureBuilder().create(v)));
-  }
-
-
-}