]> source.dussan.org Git - sonarqube.git/blob
9424cd660bac459044b724867653377775b3100d
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2016 SonarSource SA
4  * mailto:contact 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.server.computation.task.projectanalysis.api.measurecomputer;
21
22 import com.google.common.base.Optional;
23 import java.util.Arrays;
24 import java.util.Collections;
25 import java.util.List;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.junit.rules.ExpectedException;
29 import org.sonar.api.ce.measure.Component;
30 import org.sonar.api.ce.measure.MeasureComputer;
31 import org.sonar.api.measures.CoreMetrics;
32 import org.sonar.api.rule.RuleKey;
33 import org.sonar.api.utils.Duration;
34 import org.sonar.core.issue.DefaultIssue;
35 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolderRule;
36 import org.sonar.server.computation.task.projectanalysis.component.SettingsRepository;
37 import org.sonar.server.computation.task.projectanalysis.issue.ComponentIssuesRepositoryRule;
38 import org.sonar.server.computation.task.projectanalysis.measure.Measure;
39 import org.sonar.server.computation.task.projectanalysis.measure.MeasureRepositoryRule;
40 import org.sonar.server.computation.task.projectanalysis.metric.Metric;
41 import org.sonar.server.computation.task.projectanalysis.metric.MetricImpl;
42 import org.sonar.server.computation.task.projectanalysis.metric.MetricRepositoryRule;
43
44 import static org.assertj.core.api.Assertions.assertThat;
45 import static org.assertj.guava.api.Assertions.assertThat;
46 import static org.mockito.Mockito.mock;
47 import static org.mockito.Mockito.when;
48 import static org.sonar.api.measures.CoreMetrics.COMMENT_LINES_KEY;
49 import static org.sonar.api.measures.CoreMetrics.NCLOC_KEY;
50 import static org.sonar.server.computation.task.projectanalysis.component.ReportComponent.builder;
51 import static org.sonar.server.computation.task.projectanalysis.measure.Measure.newMeasureBuilder;
52
53 public class MeasureComputerContextImplTest {
54
55   @Rule
56   public ExpectedException thrown = ExpectedException.none();
57
58   private static final String INT_METRIC_KEY = "int_metric_key";
59   private static final String DOUBLE_METRIC_KEY = "double_metric_key";
60   private static final String LONG_METRIC_KEY = "long_metric_key";
61   private static final String STRING_METRIC_KEY = "string_metric_key";
62   private static final String BOOLEAN_METRIC_KEY = "boolean_metric_key";
63
64   private static final int PROJECT_REF = 1;
65   private static final int FILE_1_REF = 12341;
66   private static final String FILE_1_KEY = "fileKey";
67   private static final int FILE_2_REF = 12342;
68
69   private static final org.sonar.server.computation.task.projectanalysis.component.Component FILE_1 = builder(org.sonar.server.computation.task.projectanalysis.component.Component.Type.FILE, FILE_1_REF)
70     .setKey(FILE_1_KEY)
71     .build();
72
73   @Rule
74   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule()
75     .setRoot(builder(org.sonar.server.computation.task.projectanalysis.component.Component.Type.PROJECT, PROJECT_REF).setKey("project")
76       .addChildren(
77         FILE_1,
78         builder(org.sonar.server.computation.task.projectanalysis.component.Component.Type.FILE, FILE_2_REF).setKey("fileKey2").build()
79       ).build());
80
81   @Rule
82   public MetricRepositoryRule metricRepository = new MetricRepositoryRule()
83     .add(1, CoreMetrics.NCLOC)
84     .add(new MetricImpl(2, INT_METRIC_KEY, "int metric", Metric.MetricType.INT))
85     .add(new MetricImpl(3, DOUBLE_METRIC_KEY, "double metric", Metric.MetricType.FLOAT))
86     .add(new MetricImpl(4, LONG_METRIC_KEY, "long metric", Metric.MetricType.MILLISEC))
87     .add(new MetricImpl(5, STRING_METRIC_KEY, "string metric", Metric.MetricType.STRING))
88     .add(new MetricImpl(6, BOOLEAN_METRIC_KEY, "boolean metric", Metric.MetricType.BOOL))
89     ;
90
91   @Rule
92   public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
93
94   @Rule
95   public ComponentIssuesRepositoryRule componentIssuesRepository = new ComponentIssuesRepositoryRule(treeRootHolder);
96
97   SettingsRepository settingsRepository = mock(SettingsRepository.class);
98
99   @Test
100   public void get_component() throws Exception {
101     MeasureComputerContextImpl underTest = newContext(FILE_1_REF);
102     assertThat(underTest.getComponent().getType()).isEqualTo(Component.Type.FILE);
103   }
104
105   @Test
106   public void get_string_settings() throws Exception {
107     org.sonar.api.config.Settings serverSettings = new org.sonar.api.config.Settings();
108     serverSettings.setProperty("prop", "value");
109     when(settingsRepository.getSettings(FILE_1)).thenReturn(serverSettings);
110
111     MeasureComputerContextImpl underTest = newContext(FILE_1_REF);
112     assertThat(underTest.getSettings().getString("prop")).isEqualTo("value");
113     assertThat(underTest.getSettings().getString("unknown")).isNull();
114   }
115
116   @Test
117   public void get_string_array_settings() throws Exception {
118     org.sonar.api.config.Settings serverSettings = new org.sonar.api.config.Settings();
119     serverSettings.setProperty("prop", "1,3.4,8,50");
120     when(settingsRepository.getSettings(FILE_1)).thenReturn(serverSettings);
121
122     MeasureComputerContextImpl underTest = newContext(FILE_1_REF);
123     assertThat(underTest.getSettings().getStringArray("prop")).containsExactly("1", "3.4", "8", "50");
124     assertThat(underTest.getSettings().getStringArray("unknown")).isEmpty();
125   }
126
127   @Test
128   public void get_measure() throws Exception {
129     measureRepository.addRawMeasure(FILE_1_REF, NCLOC_KEY, newMeasureBuilder().create(10));
130
131     MeasureComputerContextImpl underTest = newContext(FILE_1_REF, NCLOC_KEY, COMMENT_LINES_KEY);
132     assertThat(underTest.getMeasure(NCLOC_KEY).getIntValue()).isEqualTo(10);
133   }
134
135   @Test
136   public void fail_with_IAE_when_get_measure_is_called_on_metric_not_in_input_list() throws Exception {
137     thrown.expect(IllegalArgumentException.class);
138     thrown.expectMessage("Only metrics in [another metric] can be used to load measures");
139
140     MeasureComputerContextImpl underTest = newContext(PROJECT_REF, "another metric", "debt");
141     underTest.getMeasure(NCLOC_KEY);
142   }
143
144   @Test
145   public void get_children_measures() throws Exception {
146     measureRepository.addRawMeasure(FILE_1_REF, NCLOC_KEY, newMeasureBuilder().create(10));
147     measureRepository.addRawMeasure(FILE_2_REF, NCLOC_KEY, newMeasureBuilder().create(12));
148
149     MeasureComputerContextImpl underTest = newContext(PROJECT_REF, NCLOC_KEY, COMMENT_LINES_KEY);
150     assertThat(underTest.getChildrenMeasures(NCLOC_KEY)).hasSize(2);
151     assertThat(underTest.getChildrenMeasures(NCLOC_KEY)).extracting("intValue").containsOnly(10, 12);
152   }
153
154   @Test
155   public void get_children_measures_when_one_child_has_no_value() throws Exception {
156     measureRepository.addRawMeasure(FILE_1_REF, NCLOC_KEY, newMeasureBuilder().create(10));
157     // No data on file 2
158
159     MeasureComputerContextImpl underTest = newContext(PROJECT_REF, NCLOC_KEY, COMMENT_LINES_KEY);
160     assertThat(underTest.getChildrenMeasures(NCLOC_KEY)).extracting("intValue").containsOnly(10);
161   }
162
163   @Test
164   public void not_fail_to_get_children_measures_on_output_metric() throws Exception {
165     measureRepository.addRawMeasure(FILE_1_REF, INT_METRIC_KEY, newMeasureBuilder().create(10));
166
167     MeasureComputerContextImpl underTest = newContext(PROJECT_REF, NCLOC_KEY, INT_METRIC_KEY);
168     assertThat(underTest.getChildrenMeasures(INT_METRIC_KEY)).hasSize(1);
169     assertThat(underTest.getChildrenMeasures(INT_METRIC_KEY)).extracting("intValue").containsOnly(10);
170   }
171
172   @Test
173   public void fail_with_IAE_when_get_children_measures_is_called_on_metric_not_in_input_list() throws Exception {
174     thrown.expect(IllegalArgumentException.class);
175     thrown.expectMessage("Only metrics in [another metric] can be used to load measures");
176
177     MeasureComputerContextImpl underTest = newContext(PROJECT_REF, "another metric", "debt");
178     underTest.getChildrenMeasures(NCLOC_KEY);
179   }
180
181   @Test
182   public void add_int_measure_create_measure_of_type_int_with_right_value() throws Exception {
183     MeasureComputerContextImpl underTest = newContext(PROJECT_REF, NCLOC_KEY, INT_METRIC_KEY);
184     underTest.addMeasure(INT_METRIC_KEY, 10);
185
186     Optional<Measure> measure = measureRepository.getAddedRawMeasure(PROJECT_REF, INT_METRIC_KEY);
187     assertThat(measure).isPresent();
188     assertThat(measure.get().getIntValue()).isEqualTo(10);
189   }
190
191   @Test
192   public void add_double_measure_create_measure_of_type_double_with_right_value() throws Exception {
193     MeasureComputerContextImpl underTest = newContext(PROJECT_REF, NCLOC_KEY, DOUBLE_METRIC_KEY);
194     underTest.addMeasure(DOUBLE_METRIC_KEY, 10d);
195
196     Optional<Measure> measure = measureRepository.getAddedRawMeasure(PROJECT_REF, DOUBLE_METRIC_KEY);
197     assertThat(measure).isPresent();
198     assertThat(measure.get().getDoubleValue()).isEqualTo(10d);
199   }
200
201   @Test
202   public void add_long_measure_create_measure_of_type_long_with_right_value() throws Exception {
203     MeasureComputerContextImpl underTest = newContext(PROJECT_REF, NCLOC_KEY, LONG_METRIC_KEY);
204     underTest.addMeasure(LONG_METRIC_KEY, 10L);
205
206     Optional<Measure> measure = measureRepository.getAddedRawMeasure(PROJECT_REF, LONG_METRIC_KEY);
207     assertThat(measure).isPresent();
208     assertThat(measure.get().getLongValue()).isEqualTo(10L);
209   }
210
211   @Test
212   public void add_string_measure_create_measure_of_type_string_with_right_value() throws Exception {
213     MeasureComputerContextImpl underTest = newContext(PROJECT_REF, NCLOC_KEY, STRING_METRIC_KEY);
214     underTest.addMeasure(STRING_METRIC_KEY, "data");
215
216     Optional<Measure> measure = measureRepository.getAddedRawMeasure(PROJECT_REF, STRING_METRIC_KEY);
217     assertThat(measure).isPresent();
218     assertThat(measure.get().getStringValue()).isEqualTo("data");
219   }
220
221   @Test
222   public void add_boolean_measure_create_measure_of_type_boolean_with_right_value() throws Exception {
223     MeasureComputerContextImpl underTest = newContext(PROJECT_REF, NCLOC_KEY, BOOLEAN_METRIC_KEY);
224     underTest.addMeasure(BOOLEAN_METRIC_KEY, true);
225
226     Optional<Measure> measure = measureRepository.getAddedRawMeasure(PROJECT_REF, BOOLEAN_METRIC_KEY);
227     assertThat(measure).isPresent();
228     assertThat(measure.get().getBooleanValue()).isTrue();
229   }
230
231   @Test
232   public void fail_with_IAE_when_add_measure_is_called_on_metric_not_in_output_list() throws Exception {
233     thrown.expect(IllegalArgumentException.class);
234     thrown.expectMessage("Only metrics in [int_metric_key] can be used to add measures. Metric 'double_metric_key' is not allowed.");
235
236     MeasureComputerContextImpl underTest = newContext(PROJECT_REF, NCLOC_KEY, INT_METRIC_KEY);
237     underTest.addMeasure(DOUBLE_METRIC_KEY, 10);
238   }
239
240   @Test
241   public void fail_with_unsupported_operation_when_adding_measure_that_already_exists() throws Exception {
242     thrown.expect(UnsupportedOperationException.class);
243     thrown.expectMessage("A measure on metric 'int_metric_key' already exists on component 'fileKey'");
244
245     measureRepository.addRawMeasure(FILE_1_REF, INT_METRIC_KEY, newMeasureBuilder().create(20));
246
247     MeasureComputerContextImpl underTest = newContext(FILE_1_REF, NCLOC_KEY, INT_METRIC_KEY);
248     underTest.addMeasure(INT_METRIC_KEY, 10);
249   }
250
251   @Test
252   public void get_issues() throws Exception {
253     DefaultIssue issue = new DefaultIssue()
254       .setKey("KEY")
255       .setRuleKey(RuleKey.of("xoo", "S01"))
256       .setSeverity("MAJOR")
257       .setStatus("CLOSED")
258       .setResolution("FIXED")
259       .setEffort(Duration.create(10l));
260
261     MeasureComputerContextImpl underTest = newContext(PROJECT_REF, Arrays.asList(issue));
262
263     assertThat(underTest.getIssues()).hasSize(1);
264     org.sonar.api.ce.measure.Issue result = underTest.getIssues().get(0);
265     assertThat(result.key()).isEqualTo("KEY");
266     assertThat(result.ruleKey()).isEqualTo(RuleKey.of("xoo", "S01"));
267     assertThat(result.severity()).isEqualTo("MAJOR");
268     assertThat(result.status()).isEqualTo("CLOSED");
269     assertThat(result.resolution()).isEqualTo("FIXED");
270     assertThat(result.debt()).isEqualTo(Duration.create(10l));
271   }
272
273   private MeasureComputerContextImpl newContext(int componentRef) {
274     return newContext(componentRef, NCLOC_KEY, COMMENT_LINES_KEY, Collections.<DefaultIssue>emptyList());
275   }
276
277   private MeasureComputerContextImpl newContext(int componentRef, List<DefaultIssue> issues) {
278     return newContext(componentRef, NCLOC_KEY, COMMENT_LINES_KEY, issues);
279   }
280
281   private MeasureComputerContextImpl newContext(int componentRef, String inputMetric, String outputMetric) {
282     return newContext(componentRef, inputMetric, outputMetric, Collections.<DefaultIssue>emptyList());
283   }
284
285   private MeasureComputerContextImpl newContext(int componentRef, String inputMetric, String outputMetric, List<DefaultIssue> issues) {
286     componentIssuesRepository.setIssues(componentRef, issues);
287
288     MeasureComputer.MeasureComputerDefinition definition = new MeasureComputerDefinitionImpl.BuilderImpl()
289       .setInputMetrics(new String[] {inputMetric})
290       .setOutputMetrics(new String[] {outputMetric})
291       .build();
292
293     MeasureComputerContextImpl context = new MeasureComputerContextImpl(treeRootHolder.getComponentByRef(componentRef),
294       settingsRepository, measureRepository, metricRepository, componentIssuesRepository);
295     context.setDefinition(definition);
296     return context;
297   }
298 }