]> source.dussan.org Git - sonarqube.git/blob
a30a26cd2ac69a2812938e5846f3d8b30772a058
[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.step;
21
22 import org.junit.Before;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.sonar.api.utils.System2;
26 import org.sonar.db.DbClient;
27 import org.sonar.db.DbSession;
28 import org.sonar.db.DbTester;
29 import org.sonar.db.component.ComponentDto;
30 import org.sonar.db.component.ComponentTesting;
31 import org.sonar.db.component.SnapshotDto;
32 import org.sonar.db.measure.MeasureDto;
33 import org.sonar.server.computation.batch.BatchReportReaderRule;
34 import org.sonar.server.computation.batch.TreeRootHolderRule;
35 import org.sonar.server.computation.component.Component;
36 import org.sonar.server.computation.component.DumbDeveloper;
37 import org.sonar.server.computation.component.ReportComponent;
38 import org.sonar.server.computation.measure.Measure;
39 import org.sonar.server.computation.measure.MeasureRepositoryRule;
40 import org.sonar.server.computation.measure.MeasureVariations;
41 import org.sonar.server.computation.metric.Metric;
42 import org.sonar.server.computation.metric.MetricImpl;
43 import org.sonar.server.computation.metric.MetricRepositoryRule;
44 import org.sonar.server.computation.period.Period;
45 import org.sonar.server.computation.period.PeriodsHolderRule;
46
47 import static org.assertj.core.api.Assertions.assertThat;
48 import static org.sonar.db.component.SnapshotTesting.createForComponent;
49 import static org.sonar.db.component.SnapshotTesting.newSnapshotForProject;
50 import static org.sonar.server.computation.measure.Measure.newMeasureBuilder;
51
52 public class ReportComputeMeasureVariationsStepTest {
53
54   static final Metric ISSUES_METRIC = new MetricImpl(1, "violations", "violations", Metric.MetricType.INT);
55   static final Metric DEBT_METRIC = new MetricImpl(2, "sqale_index", "sqale_index", Metric.MetricType.WORK_DUR);
56   static final Metric FILE_COMPLEXITY_METRIC = new MetricImpl(3, "file_complexity", "file_complexity", Metric.MetricType.FLOAT);
57   static final Metric BUILD_BREAKER_METRIC = new MetricImpl(4, "build_breaker", "build_breaker", Metric.MetricType.BOOL);
58   static final Metric NEW_DEBT = new MetricImpl(5, "new_debt", "new_debt", Metric.MetricType.WORK_DUR);
59
60   static final ComponentDto PROJECT_DTO = ComponentTesting.newProjectDto();
61
62   static final int PROJECT_REF = 1;
63   static final Component PROJECT = ReportComponent.builder(Component.Type.PROJECT, PROJECT_REF).setUuid(PROJECT_DTO.uuid()).build();
64
65   @Rule
66   public DbTester dbTester = DbTester.create(System2.INSTANCE);
67   @Rule
68   public BatchReportReaderRule reportReader = new BatchReportReaderRule();
69   @Rule
70   public PeriodsHolderRule periodsHolder = new PeriodsHolderRule();
71   @Rule
72   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
73   @Rule
74   public MetricRepositoryRule metricRepository = new MetricRepositoryRule()
75     .add(ISSUES_METRIC)
76     .add(DEBT_METRIC)
77     .add(FILE_COMPLEXITY_METRIC)
78     .add(BUILD_BREAKER_METRIC)
79     .add(NEW_DEBT);
80   @Rule
81   public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
82
83   DbSession session = dbTester.getSession();
84
85   DbClient dbClient = dbTester.getDbClient();
86
87   ComputeMeasureVariationsStep underTest;
88
89   @Before
90   public void setUp() {
91     dbClient.componentDao().insert(session, PROJECT_DTO);
92     session.commit();
93
94     underTest = new ComputeMeasureVariationsStep(dbClient, treeRootHolder, periodsHolder, metricRepository, measureRepository);
95   }
96
97   @Test
98   public void do_nothing_when_no_raw_measure() {
99     SnapshotDto period1ProjectSnapshot = newSnapshotForProject(PROJECT_DTO);
100     dbClient.snapshotDao().insert(session, period1ProjectSnapshot);
101     dbClient.measureDao().insert(session, newMeasureDto(ISSUES_METRIC.getId(), PROJECT_DTO.uuid(), period1ProjectSnapshot.getId(), 60d));
102     session.commit();
103
104     periodsHolder.setPeriods(newPeriod(1, period1ProjectSnapshot));
105
106     treeRootHolder.setRoot(PROJECT);
107
108     underTest.execute();
109
110     assertThat(measureRepository.getRawMeasures(PROJECT).keys()).isEmpty();
111   }
112
113   @Test
114   public void do_nothing_when_no_period() {
115     Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid(PROJECT_DTO.uuid()).build();
116     treeRootHolder.setRoot(project);
117     periodsHolder.setPeriods();
118
119     underTest.execute();
120
121     assertThat(measureRepository.getRawMeasures(project).keys()).isEmpty();
122   }
123
124   @Test
125   public void set_variation() {
126     // Project
127     SnapshotDto period1ProjectSnapshot = newSnapshotForProject(PROJECT_DTO);
128     dbClient.snapshotDao().insert(session, period1ProjectSnapshot);
129     dbClient.measureDao().insert(session, newMeasureDto(ISSUES_METRIC.getId(), PROJECT_DTO.uuid(), period1ProjectSnapshot.getId(), 60d));
130
131     // Directory
132     ComponentDto directoryDto = ComponentTesting.newDirectory(PROJECT_DTO, "dir");
133     dbClient.componentDao().insert(session, directoryDto);
134     SnapshotDto period1DirectorySnapshot = createForComponent(directoryDto, period1ProjectSnapshot);
135     dbClient.snapshotDao().insert(session, period1DirectorySnapshot);
136     dbClient.measureDao().insert(session, newMeasureDto(ISSUES_METRIC.getId(), directoryDto.uuid(), period1DirectorySnapshot.getId(), 10d));
137     session.commit();
138
139     periodsHolder.setPeriods(newPeriod(1, period1ProjectSnapshot));
140
141     Component directory = ReportComponent.builder(Component.Type.DIRECTORY, 2).setUuid(directoryDto.uuid()).build();
142     Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid(PROJECT_DTO.uuid()).addChildren(directory).build();
143     treeRootHolder.setRoot(project);
144
145     addRawMeasure(project, ISSUES_METRIC, newMeasureBuilder().create(80, null));
146     addRawMeasure(directory, ISSUES_METRIC, newMeasureBuilder().create(20, null));
147
148     underTest.execute();
149
150     assertThat(measureRepository.getRawMeasure(project, ISSUES_METRIC).get().getVariations().getVariation1()).isEqualTo(20d);
151     assertThat(measureRepository.getRawMeasure(directory, ISSUES_METRIC).get().getVariations().getVariation1()).isEqualTo(10d);
152   }
153
154   @Test
155   public void set_zero_variation_when_no_change() {
156     // Project
157     SnapshotDto period1ProjectSnapshot = newSnapshotForProject(PROJECT_DTO);
158     dbClient.snapshotDao().insert(session, period1ProjectSnapshot);
159     dbClient.measureDao().insert(session, newMeasureDto(ISSUES_METRIC.getId(), PROJECT_DTO.uuid(), period1ProjectSnapshot.getId(), 60d));
160
161     // Directory
162     ComponentDto directoryDto = ComponentTesting.newDirectory(PROJECT_DTO, "dir");
163     dbClient.componentDao().insert(session, directoryDto);
164     SnapshotDto period1DirectorySnapshot = createForComponent(directoryDto, period1ProjectSnapshot);
165     dbClient.snapshotDao().insert(session, period1DirectorySnapshot);
166     dbClient.measureDao().insert(session, newMeasureDto(ISSUES_METRIC.getId(), directoryDto.uuid(), period1DirectorySnapshot.getId(), 10d));
167     session.commit();
168
169     periodsHolder.setPeriods(newPeriod(1, period1ProjectSnapshot));
170
171     Component directory = ReportComponent.builder(Component.Type.DIRECTORY, 2).setUuid(directoryDto.uuid()).build();
172     Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid(PROJECT_DTO.uuid()).addChildren(directory).build();
173     treeRootHolder.setRoot(project);
174
175     addRawMeasure(project, ISSUES_METRIC, newMeasureBuilder().create(60, null));
176     addRawMeasure(directory, ISSUES_METRIC, newMeasureBuilder().create(10, null));
177
178     underTest.execute();
179
180     assertThat(measureRepository.getRawMeasure(project, ISSUES_METRIC).get().getVariations().getVariation1()).isEqualTo(0d);
181     assertThat(measureRepository.getRawMeasure(directory, ISSUES_METRIC).get().getVariations().getVariation1()).isEqualTo(0d);
182   }
183
184   @Test
185   public void set_variation_to_raw_value_on_new_component() throws Exception {
186     // Project
187     SnapshotDto past1ProjectSnapshot = newSnapshotForProject(PROJECT_DTO).setCreatedAt(1000_000_000L);
188     SnapshotDto currentProjectSnapshot = newSnapshotForProject(PROJECT_DTO).setCreatedAt(2000_000_000L);
189     dbClient.snapshotDao().insert(session, past1ProjectSnapshot);
190     dbClient.snapshotDao().insert(session, currentProjectSnapshot);
191     dbClient.measureDao().insert(session, newMeasureDto(ISSUES_METRIC.getId(), PROJECT_DTO.uuid(), past1ProjectSnapshot.getId(), 60d));
192     dbClient.measureDao().insert(session, newMeasureDto(ISSUES_METRIC.getId(), PROJECT_DTO.uuid(), currentProjectSnapshot.getId(), 60d));
193     session.commit();
194
195     periodsHolder.setPeriods(newPeriod(1, past1ProjectSnapshot));
196
197     // Directory has just been added => no snapshot
198     Component directory = ReportComponent.builder(Component.Type.DIRECTORY, 2).setUuid("DIRECTORY").build();
199     Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid(PROJECT_DTO.uuid()).addChildren(directory).build();
200     treeRootHolder.setRoot(project);
201
202     addRawMeasure(project, ISSUES_METRIC, newMeasureBuilder().create(90, null));
203     addRawMeasure(directory, ISSUES_METRIC, newMeasureBuilder().create(10, null));
204
205     underTest.execute();
206
207     assertThat(measureRepository.getRawMeasure(project, ISSUES_METRIC).get().getVariations().getVariation1()).isEqualTo(30d);
208     // Variation should be the raw value
209     assertThat(measureRepository.getRawMeasure(directory, ISSUES_METRIC).get().getVariations().getVariation1()).isEqualTo(10d);
210   }
211
212   @Test
213   public void set_variations_on_all_periods() {
214     SnapshotDto period1ProjectSnapshot = newSnapshotForProject(PROJECT_DTO).setLast(false);
215     SnapshotDto period2ProjectSnapshot = newSnapshotForProject(PROJECT_DTO).setLast(false);
216     SnapshotDto period3ProjectSnapshot = newSnapshotForProject(PROJECT_DTO).setLast(false);
217     SnapshotDto period4ProjectSnapshot = newSnapshotForProject(PROJECT_DTO).setLast(false);
218     SnapshotDto period5ProjectSnapshot = newSnapshotForProject(PROJECT_DTO).setLast(false);
219     dbClient.snapshotDao().insert(session, period1ProjectSnapshot, period2ProjectSnapshot, period3ProjectSnapshot, period4ProjectSnapshot, period5ProjectSnapshot);
220
221     dbClient.measureDao().insert(session,
222       newMeasureDto(ISSUES_METRIC.getId(), PROJECT_DTO.uuid(), period1ProjectSnapshot.getId(), 0d),
223       newMeasureDto(ISSUES_METRIC.getId(), PROJECT_DTO.uuid(), period2ProjectSnapshot.getId(), 20d),
224       newMeasureDto(ISSUES_METRIC.getId(), PROJECT_DTO.uuid(), period3ProjectSnapshot.getId(), 40d),
225       newMeasureDto(ISSUES_METRIC.getId(), PROJECT_DTO.uuid(), period4ProjectSnapshot.getId(), 80d),
226       newMeasureDto(ISSUES_METRIC.getId(), PROJECT_DTO.uuid(), period5ProjectSnapshot.getId(), 100d));
227     session.commit();
228
229     periodsHolder.setPeriods(newPeriod(1, period1ProjectSnapshot),
230       newPeriod(2, period2ProjectSnapshot),
231       newPeriod(3, period3ProjectSnapshot),
232       newPeriod(4, period4ProjectSnapshot),
233       newPeriod(5, period5ProjectSnapshot));
234
235     treeRootHolder.setRoot(PROJECT);
236
237     addRawMeasure(PROJECT, ISSUES_METRIC, newMeasureBuilder().create(80, null));
238
239     underTest.execute();
240
241     assertThat(measureRepository.getRawMeasures(PROJECT).keys()).hasSize(1);
242
243     Measure measure = measureRepository.getRawMeasure(PROJECT, ISSUES_METRIC).get();
244     assertThat(measure.hasVariations()).isTrue();
245     assertThat(measure.getVariations().getVariation1()).isEqualTo(80d);
246     assertThat(measure.getVariations().getVariation2()).isEqualTo(60d);
247     assertThat(measure.getVariations().getVariation3()).isEqualTo(40d);
248     assertThat(measure.getVariations().getVariation4()).isEqualTo(0d);
249     assertThat(measure.getVariations().getVariation5()).isEqualTo(-20d);
250   }
251
252   @Test
253   public void set_variation_on_all_numeric_metrics() {
254     SnapshotDto period1ProjectSnapshot = newSnapshotForProject(PROJECT_DTO);
255     dbClient.snapshotDao().insert(session, period1ProjectSnapshot);
256     dbClient.measureDao().insert(session,
257       newMeasureDto(ISSUES_METRIC.getId(), PROJECT_DTO.uuid(), period1ProjectSnapshot.getId(), 60d),
258       newMeasureDto(DEBT_METRIC.getId(), PROJECT_DTO.uuid(), period1ProjectSnapshot.getId(), 10d),
259       newMeasureDto(FILE_COMPLEXITY_METRIC.getId(), PROJECT_DTO.uuid(), period1ProjectSnapshot.getId(), 2d),
260       newMeasureDto(BUILD_BREAKER_METRIC.getId(), PROJECT_DTO.uuid(), period1ProjectSnapshot.getId(), 1d));
261     session.commit();
262
263     periodsHolder.setPeriods(newPeriod(1, period1ProjectSnapshot));
264
265     treeRootHolder.setRoot(PROJECT);
266
267     addRawMeasure(PROJECT, ISSUES_METRIC, newMeasureBuilder().create(80, null));
268     addRawMeasure(PROJECT, DEBT_METRIC, newMeasureBuilder().create(5L, null));
269     addRawMeasure(PROJECT, FILE_COMPLEXITY_METRIC, newMeasureBuilder().create(3d, 1, null));
270     addRawMeasure(PROJECT, BUILD_BREAKER_METRIC, newMeasureBuilder().create(false, null));
271
272     underTest.execute();
273
274     assertThat(measureRepository.getRawMeasures(PROJECT).keys()).hasSize(4);
275
276     assertThat(measureRepository.getRawMeasure(PROJECT, ISSUES_METRIC).get().getVariations().getVariation1()).isEqualTo(20d);
277     assertThat(measureRepository.getRawMeasure(PROJECT, DEBT_METRIC).get().getVariations().getVariation1()).isEqualTo(-5d);
278     assertThat(measureRepository.getRawMeasure(PROJECT, FILE_COMPLEXITY_METRIC).get().getVariations().getVariation1()).isEqualTo(1d);
279     assertThat(measureRepository.getRawMeasure(PROJECT, BUILD_BREAKER_METRIC).get().getVariations().getVariation1()).isEqualTo(-1d);
280   }
281
282   @Test
283   public void do_not_set_variations_on_numeric_metric_for_developer() {
284     SnapshotDto period1ProjectSnapshot = newSnapshotForProject(PROJECT_DTO);
285     dbClient.snapshotDao().insert(session, period1ProjectSnapshot);
286     dbClient.measureDao().insert(session,
287       newMeasureDto(ISSUES_METRIC.getId(), PROJECT_DTO.uuid(), period1ProjectSnapshot.getId(), 60d));
288     session.commit();
289
290     periodsHolder.setPeriods(newPeriod(1, period1ProjectSnapshot));
291
292     treeRootHolder.setRoot(PROJECT);
293
294     DumbDeveloper developer = new DumbDeveloper("a");
295     measureRepository.addRawMeasure(PROJECT_REF, ISSUES_METRIC.getKey(), newMeasureBuilder().forDeveloper(developer).create(80, null));
296
297     underTest.execute();
298
299     assertThat(measureRepository.getRawMeasures(PROJECT).keys()).hasSize(1);
300
301     assertThat(measureRepository.getRawMeasure(PROJECT, ISSUES_METRIC, developer).get().hasVariations()).isFalse();
302   }
303
304   @Test
305   public void does_not_update_existing_variations() throws Exception {
306     SnapshotDto period1ProjectSnapshot = newSnapshotForProject(PROJECT_DTO);
307     dbClient.snapshotDao().insert(session, period1ProjectSnapshot);
308     dbClient.measureDao().insert(session, newMeasureDto(NEW_DEBT.getId(), PROJECT_DTO.uuid(), period1ProjectSnapshot.getId(), 60d));
309     session.commit();
310     periodsHolder.setPeriods(newPeriod(1, period1ProjectSnapshot));
311     treeRootHolder.setRoot(PROJECT);
312
313     addRawMeasure(PROJECT, NEW_DEBT, newMeasureBuilder().setVariations(new MeasureVariations(10d)).createNoValue());
314
315     underTest.execute();
316
317     // As the measure has already variations it has been ignored, then variations will be the same
318     assertThat(measureRepository.getRawMeasure(PROJECT, NEW_DEBT).get().getVariations().getVariation1()).isEqualTo(10d);
319   }
320
321   private static MeasureDto newMeasureDto(int metricId, String componentUuid, long snapshotId, double value) {
322     return new MeasureDto().setMetricId(metricId).setComponentUuid(componentUuid).setSnapshotId(snapshotId).setValue(value);
323   }
324
325   private static Period newPeriod(int index, SnapshotDto snapshotDto) {
326     return new Period(index, "mode", null, snapshotDto.getCreatedAt(), snapshotDto.getId());
327   }
328
329   private void addRawMeasure(Component component, Metric metric, Measure measure) {
330     measureRepository.addRawMeasure(component.getReportAttributes().getRef(), metric.getKey(), measure);
331   }
332 }