3 * Copyright (C) 2009-2024 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.ce.task.projectanalysis.formula;
22 import com.google.gson.Gson;
23 import java.util.LinkedHashMap;
24 import java.util.Optional;
25 import javax.annotation.Nullable;
26 import org.junit.Test;
27 import org.sonar.api.issue.impact.Severity;
28 import org.sonar.ce.task.projectanalysis.measure.Measure;
30 import static org.assertj.core.api.Assertions.assertThat;
31 import static org.mockito.Mockito.mock;
32 import static org.mockito.Mockito.when;
34 public class ImpactSumFormulaTest {
36 public static final ImpactSumFormula IMPACT_SUM_FORMULA = ImpactSumFormula.createImpactSumFormula("metricKey");
37 private final Gson gson = new Gson();
39 private final CounterInitializationContext counterInitializationContext = mock(CounterInitializationContext.class);
41 private final CreateMeasureContext createMeasureContext = mock(CreateMeasureContext.class);
44 public void getOutputMetricKeys_shouldReturnCorrectMetrics() {
45 assertThat(IMPACT_SUM_FORMULA.getOutputMetricKeys()).containsExactly("metricKey");
49 public void createMeasure_whenCounterReturnsValue_shouldReturnExpectedMeasure() {
50 ImpactSumFormula.ImpactCounter counter = IMPACT_SUM_FORMULA.createNewCounter();
51 String value = newImpactJson(4, 2, 1, 1);
52 addMeasure("metricKey", value);
53 counter.initialize(counterInitializationContext);
55 assertThat(IMPACT_SUM_FORMULA.createMeasure(counter, createMeasureContext)).get().extracting(Measure::getData)
60 public void createMeasure_whenCounterReturnsNoValue_shouldReturnNoMeasure() {
61 ImpactSumFormula.ImpactCounter counter = IMPACT_SUM_FORMULA.createNewCounter();
62 assertThat(IMPACT_SUM_FORMULA.createMeasure(counter, createMeasureContext)).isEmpty();
66 public void createNewCounter_shouldReturnExpectedCounter() {
67 assertThat(IMPACT_SUM_FORMULA.createNewCounter()).isNotNull()
68 .isInstanceOf(ImpactSumFormula.ImpactCounter.class);
72 public void getValue_whenInitialized_shouldReturnExpectedValue() {
73 ImpactSumFormula.ImpactCounter counter = IMPACT_SUM_FORMULA.createNewCounter();
74 String value = newImpactJson(4, 2, 1, 1);
75 addMeasure("metricKey", value);
76 counter.initialize(counterInitializationContext);
77 assertThat(counter.getValue()).get().isEqualTo(value);
81 public void getValue_whenAggregatingExistingValue_shouldReturnAggregationResult() {
82 ImpactSumFormula.ImpactCounter counter = IMPACT_SUM_FORMULA.createNewCounter();
83 String value = newImpactJson(4, 2, 1, 1);
84 addMeasure("metricKey", value);
85 counter.initialize(counterInitializationContext);
87 ImpactSumFormula.ImpactCounter counter2 = IMPACT_SUM_FORMULA.createNewCounter();
88 String value2 = newImpactJson(3, 1, 1, 1);
89 addMeasure("metricKey", value2);
90 counter2.initialize(counterInitializationContext);
92 counter.aggregate(counter2);
93 assertThat(counter.getValue()).get().isEqualTo(newImpactJson(7, 3, 2, 2));
97 public void getValue_whenAggregatingUninitializedValue_shouldReturnEmptyValue() {
98 ImpactSumFormula.ImpactCounter counter = IMPACT_SUM_FORMULA.createNewCounter();
99 String value = newImpactJson(4, 2, 1, 1);
100 addMeasure("metricKey", value);
101 counter.initialize(counterInitializationContext);
103 ImpactSumFormula.ImpactCounter counter2 = IMPACT_SUM_FORMULA.createNewCounter();
105 counter.aggregate(counter2);
106 assertThat(counter.getValue()).isEmpty();
110 public void getValue_whenAggregatingEmptyValue_shouldReturnEmptyValue() {
112 ImpactSumFormula.ImpactCounter counter = IMPACT_SUM_FORMULA.createNewCounter();
113 String value = newImpactJson(4, 2, 1, 1);
114 addMeasure("metricKey", value);
115 counter.initialize(counterInitializationContext);
117 ImpactSumFormula.ImpactCounter counter2 = IMPACT_SUM_FORMULA.createNewCounter();
118 addMeasure("metricKey", null);
119 counter2.initialize(counterInitializationContext);
121 counter.aggregate(counter2);
123 assertThat(counter.getValue()).isEmpty();
127 public void getValue_whenNotInitialized_shouldReturnEmpty() {
128 ImpactSumFormula.ImpactCounter counter = IMPACT_SUM_FORMULA.createNewCounter();
129 assertThat(counter.getValue()).isEmpty();
133 public String newImpactJson(Integer total, Integer high, Integer medium, Integer low) {
134 LinkedHashMap<Object, Object> map = new LinkedHashMap<>();
135 map.put(Severity.LOW.name(), low);
136 map.put(Severity.MEDIUM.name(), medium);
137 map.put(Severity.HIGH.name(), high);
138 map.put("total", total);
139 return gson.toJson(map);
142 private void addMeasure(String metricKey, @Nullable String value) {
143 when(counterInitializationContext.getMeasure(metricKey)).thenReturn(Optional.ofNullable(value).map(v -> Measure.newMeasureBuilder().create(v)));