]> source.dussan.org Git - sonarqube.git/blob
33e1c7340074fd7db975e229dedf111a4b81797a
[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.issue;
21
22 import org.junit.Test;
23 import org.sonar.api.utils.Duration;
24 import org.sonar.ce.task.projectanalysis.component.Component;
25 import org.sonar.ce.task.projectanalysis.component.ReportComponent;
26 import org.sonar.ce.task.projectanalysis.measure.MeasureRepositoryRule;
27 import org.sonar.ce.task.projectanalysis.metric.MetricRepositoryRule;
28 import org.sonar.core.issue.DefaultIssue;
29
30 import static org.assertj.core.api.Assertions.assertThat;
31 import static org.sonar.api.issue.Issue.RESOLUTION_FIXED;
32 import static org.sonar.api.measures.CoreMetrics.RELIABILITY_REMEDIATION_EFFORT;
33 import static org.sonar.api.measures.CoreMetrics.RELIABILITY_REMEDIATION_EFFORT_KEY;
34 import static org.sonar.api.measures.CoreMetrics.SECURITY_REMEDIATION_EFFORT;
35 import static org.sonar.api.measures.CoreMetrics.SECURITY_REMEDIATION_EFFORT_KEY;
36 import static org.sonar.api.measures.CoreMetrics.TECHNICAL_DEBT;
37 import static org.sonar.api.measures.CoreMetrics.TECHNICAL_DEBT_KEY;
38 import static org.sonar.api.rules.RuleType.BUG;
39 import static org.sonar.api.rules.RuleType.CODE_SMELL;
40 import static org.sonar.api.rules.RuleType.VULNERABILITY;
41
42 public class EffortAggregatorTest {
43
44   static final Component FILE = ReportComponent.builder(Component.Type.FILE, 1).build();
45   static final Component PROJECT = ReportComponent.builder(Component.Type.PROJECT, 2).addChildren(FILE).build();
46
47   @org.junit.Rule
48   public MetricRepositoryRule metricRepository = new MetricRepositoryRule()
49     .add(TECHNICAL_DEBT)
50     .add(RELIABILITY_REMEDIATION_EFFORT)
51     .add(SECURITY_REMEDIATION_EFFORT);
52
53   @org.junit.Rule
54   public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(PROJECT, metricRepository);
55
56   EffortAggregator underTest = new EffortAggregator(metricRepository, measureRepository);
57
58   @Test
59   public void sum_maintainability_effort_of_unresolved_issues() {
60     DefaultIssue unresolved1 = newCodeSmellIssue(10);
61     DefaultIssue unresolved2 = newCodeSmellIssue(30);
62     DefaultIssue unresolvedWithoutEffort = newCodeSmellIssueWithoutEffort();
63     DefaultIssue resolved = newCodeSmellIssue(50).setResolution(RESOLUTION_FIXED);
64
65     underTest.beforeComponent(FILE);
66     underTest.onIssue(FILE, unresolved1);
67     underTest.onIssue(FILE, unresolved2);
68     underTest.onIssue(FILE, unresolvedWithoutEffort);
69     underTest.onIssue(FILE, resolved);
70     underTest.afterComponent(FILE);
71
72     // total maintainability effort
73     assertMeasure(FILE, TECHNICAL_DEBT_KEY, 10L + 30L);
74   }
75
76   @Test
77   public void maintainability_effort_is_only_computed_using_code_smell_issues() {
78     DefaultIssue codeSmellIssue = newCodeSmellIssue(10);
79     // Issues of type BUG and VULNERABILITY should be ignored
80     DefaultIssue bugIssue = newBugIssue(15);
81     DefaultIssue vulnerabilityIssue = newVulnerabilityIssue(12);
82
83     underTest.beforeComponent(FILE);
84     underTest.onIssue(FILE, codeSmellIssue);
85     underTest.onIssue(FILE, bugIssue);
86     underTest.onIssue(FILE, vulnerabilityIssue);
87     underTest.afterComponent(FILE);
88
89     // Only effort of CODE SMELL issue is used
90     assertMeasure(FILE, TECHNICAL_DEBT_KEY, 10L);
91   }
92
93   @Test
94   public void sum_reliability_effort_of_unresolved_issues() {
95     DefaultIssue unresolved1 = newBugIssue(10);
96     DefaultIssue unresolved2 = newBugIssue(30);
97     DefaultIssue unresolvedWithoutEffort = newBugIssueWithoutEffort();
98     DefaultIssue resolved = newBugIssue(50).setResolution(RESOLUTION_FIXED);
99
100     underTest.beforeComponent(FILE);
101     underTest.onIssue(FILE, unresolved1);
102     underTest.onIssue(FILE, unresolved2);
103     underTest.onIssue(FILE, unresolvedWithoutEffort);
104     underTest.onIssue(FILE, resolved);
105     underTest.afterComponent(FILE);
106
107     assertMeasure(FILE, RELIABILITY_REMEDIATION_EFFORT_KEY, 10L + 30L);
108   }
109
110   @Test
111   public void reliability_effort_is_only_computed_using_bug_issues() {
112     DefaultIssue bugIssue = newBugIssue(10);
113     // Issues of type CODE SMELL and VULNERABILITY should be ignored
114     DefaultIssue codeSmellIssue = newCodeSmellIssue(15);
115     DefaultIssue vulnerabilityIssue = newVulnerabilityIssue(12);
116
117     underTest.beforeComponent(FILE);
118     underTest.onIssue(FILE, bugIssue);
119     underTest.onIssue(FILE, codeSmellIssue);
120     underTest.onIssue(FILE, vulnerabilityIssue);
121     underTest.afterComponent(FILE);
122
123     // Only effort of BUG issue is used
124     assertMeasure(FILE, RELIABILITY_REMEDIATION_EFFORT_KEY, 10L);
125   }
126
127   @Test
128   public void sum_security_effort_of_unresolved_issues() {
129     DefaultIssue unresolved1 = newVulnerabilityIssue(10);
130     DefaultIssue unresolved2 = newVulnerabilityIssue(30);
131     DefaultIssue unresolvedWithoutEffort = newVulnerabilityIssueWithoutEffort();
132     DefaultIssue resolved = newVulnerabilityIssue(50).setResolution(RESOLUTION_FIXED);
133
134     underTest.beforeComponent(FILE);
135     underTest.onIssue(FILE, unresolved1);
136     underTest.onIssue(FILE, unresolved2);
137     underTest.onIssue(FILE, unresolvedWithoutEffort);
138     underTest.onIssue(FILE, resolved);
139     underTest.afterComponent(FILE);
140
141     assertMeasure(FILE, SECURITY_REMEDIATION_EFFORT_KEY, 10L + 30L);
142   }
143
144   @Test
145   public void security_effort_is_only_computed_using_code_smell_issues() {
146     DefaultIssue vulnerabilityIssue = newVulnerabilityIssue(10);
147     // Issues of type BUG and CODE SMELL should be ignored
148     DefaultIssue bugIssue = newBugIssue(15);
149     DefaultIssue codeSmellIssue = newCodeSmellIssue(12);
150
151     underTest.beforeComponent(FILE);
152     underTest.onIssue(FILE, vulnerabilityIssue);
153     underTest.onIssue(FILE, bugIssue);
154     underTest.onIssue(FILE, codeSmellIssue);
155     underTest.afterComponent(FILE);
156
157     // Only effort of VULNERABILITY issue is used
158     assertMeasure(FILE, SECURITY_REMEDIATION_EFFORT_KEY, 10L);
159   }
160
161   @Test
162   public void aggregate_maintainability_measures_of_children() {
163     underTest.beforeComponent(FILE);
164     underTest.onIssue(FILE, newCodeSmellIssue(10));
165     underTest.onIssue(FILE, newBugIssue(8));
166     underTest.onIssue(FILE, newVulnerabilityIssue(12));
167     underTest.afterComponent(FILE);
168     underTest.beforeComponent(PROJECT);
169     underTest.onIssue(PROJECT, newCodeSmellIssue(30));
170     underTest.onIssue(PROJECT, newBugIssue(38));
171     underTest.onIssue(PROJECT, newVulnerabilityIssue(42));
172     underTest.afterComponent(PROJECT);
173
174     assertMeasure(PROJECT, TECHNICAL_DEBT_KEY, 10L + 30L);
175     assertMeasure(PROJECT, RELIABILITY_REMEDIATION_EFFORT_KEY, 8L + 38L);
176     assertMeasure(PROJECT, SECURITY_REMEDIATION_EFFORT_KEY, 12L + 42L);
177   }
178
179   @Test
180   public void sum_characteristic_measures_of_issues_without_effort() {
181     underTest.beforeComponent(FILE);
182     underTest.onIssue(FILE, newCodeSmellIssueWithoutEffort());
183     underTest.onIssue(FILE, newBugIssueWithoutEffort());
184     underTest.onIssue(FILE, newVulnerabilityIssueWithoutEffort());
185     underTest.afterComponent(FILE);
186     underTest.beforeComponent(PROJECT);
187     underTest.onIssue(PROJECT, newCodeSmellIssueWithoutEffort());
188     underTest.afterComponent(PROJECT);
189
190     assertMeasure(PROJECT, TECHNICAL_DEBT_KEY, 0L);
191     assertMeasure(PROJECT, RELIABILITY_REMEDIATION_EFFORT_KEY, 0L);
192     assertMeasure(PROJECT, SECURITY_REMEDIATION_EFFORT_KEY, 0L);
193   }
194
195   private void assertMeasure(Component component, String metricKey, long expectedValue) {
196     assertThat(measureRepository.getAddedRawMeasure(component, metricKey).get().getLongValue()).isEqualTo(expectedValue);
197   }
198
199   private static DefaultIssue newCodeSmellIssue(long effort) {
200     return newCodeSmellIssueWithoutEffort().setEffort(Duration.create(effort)).setType(CODE_SMELL);
201   }
202
203   private static DefaultIssue newBugIssue(long effort) {
204     return newCodeSmellIssueWithoutEffort().setEffort(Duration.create(effort)).setType(BUG);
205   }
206
207   private static DefaultIssue newVulnerabilityIssue(long effort) {
208     return newCodeSmellIssueWithoutEffort().setEffort(Duration.create(effort)).setType(VULNERABILITY);
209   }
210
211   private static DefaultIssue newCodeSmellIssueWithoutEffort() {
212     return new DefaultIssue().setType(CODE_SMELL);
213   }
214
215   private static DefaultIssue newBugIssueWithoutEffort() {
216     return new DefaultIssue().setType(BUG);
217   }
218
219   private static DefaultIssue newVulnerabilityIssueWithoutEffort() {
220     return new DefaultIssue().setType(VULNERABILITY);
221   }
222
223 }