]> source.dussan.org Git - sonarqube.git/blob
b0eadc31a14a86ea150140a10add4b6fa2a3e9bb
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2024 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.ce.task.projectanalysis.formula;
21
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;
29
30 import static org.assertj.core.api.Assertions.assertThat;
31 import static org.mockito.Mockito.mock;
32 import static org.mockito.Mockito.when;
33
34 public class ImpactSumFormulaTest {
35
36   public static final ImpactSumFormula IMPACT_SUM_FORMULA = ImpactSumFormula.createImpactSumFormula("metricKey");
37   private final Gson gson = new Gson();
38
39   private final CounterInitializationContext counterInitializationContext = mock(CounterInitializationContext.class);
40
41   private final CreateMeasureContext createMeasureContext = mock(CreateMeasureContext.class);
42
43   @Test
44   public void getOutputMetricKeys_shouldReturnCorrectMetrics() {
45     assertThat(IMPACT_SUM_FORMULA.getOutputMetricKeys()).containsExactly("metricKey");
46   }
47
48   @Test
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);
54
55     assertThat(IMPACT_SUM_FORMULA.createMeasure(counter, createMeasureContext)).get().extracting(Measure::getData)
56       .isEqualTo(value);
57   }
58
59   @Test
60   public void createMeasure_whenCounterReturnsNoValue_shouldReturnNoMeasure() {
61     ImpactSumFormula.ImpactCounter counter = IMPACT_SUM_FORMULA.createNewCounter();
62     assertThat(IMPACT_SUM_FORMULA.createMeasure(counter, createMeasureContext)).isEmpty();
63   }
64
65   @Test
66   public void createNewCounter_shouldReturnExpectedCounter() {
67     assertThat(IMPACT_SUM_FORMULA.createNewCounter()).isNotNull()
68       .isInstanceOf(ImpactSumFormula.ImpactCounter.class);
69   }
70
71   @Test
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);
78   }
79
80   @Test
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);
86
87     ImpactSumFormula.ImpactCounter counter2 = IMPACT_SUM_FORMULA.createNewCounter();
88     String value2 = newImpactJson(3, 1, 1, 1);
89     addMeasure("metricKey", value2);
90     counter2.initialize(counterInitializationContext);
91
92     counter.aggregate(counter2);
93     assertThat(counter.getValue()).get().isEqualTo(newImpactJson(7, 3, 2, 2));
94   }
95
96   @Test
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);
102
103     ImpactSumFormula.ImpactCounter counter2 = IMPACT_SUM_FORMULA.createNewCounter();
104
105     counter.aggregate(counter2);
106     assertThat(counter.getValue()).isEmpty();
107   }
108
109   @Test
110   public void getValue_whenAggregatingEmptyValue_shouldReturnEmptyValue() {
111
112     ImpactSumFormula.ImpactCounter counter = IMPACT_SUM_FORMULA.createNewCounter();
113     String value = newImpactJson(4, 2, 1, 1);
114     addMeasure("metricKey", value);
115     counter.initialize(counterInitializationContext);
116
117     ImpactSumFormula.ImpactCounter counter2 = IMPACT_SUM_FORMULA.createNewCounter();
118     addMeasure("metricKey", null);
119     counter2.initialize(counterInitializationContext);
120
121     counter.aggregate(counter2);
122
123     assertThat(counter.getValue()).isEmpty();
124   }
125
126   @Test
127   public void getValue_whenNotInitialized_shouldReturnEmpty() {
128     ImpactSumFormula.ImpactCounter counter = IMPACT_SUM_FORMULA.createNewCounter();
129     assertThat(counter.getValue()).isEmpty();
130   }
131
132
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);
140   }
141
142   private void addMeasure(String metricKey, @Nullable String value) {
143     when(counterInitializationContext.getMeasure(metricKey)).thenReturn(Optional.ofNullable(value).map(v -> Measure.newMeasureBuilder().create(v)));
144   }
145
146
147 }