]> source.dussan.org Git - sonarqube.git/blob
5f85e850677cb8b51cc2e19f146047a306baeeb8
[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.junit.experimental.categories.Category;
26 import org.sonar.api.utils.System2;
27 import org.sonar.db.DbClient;
28 import org.sonar.db.DbSession;
29 import org.sonar.db.DbTester;
30 import org.sonar.db.component.ComponentDto;
31 import org.sonar.db.component.ComponentTesting;
32 import org.sonar.db.component.SnapshotDto;
33 import org.sonar.db.measure.MeasureDto;
34 import org.sonar.server.computation.batch.BatchReportReaderRule;
35 import org.sonar.server.computation.batch.TreeRootHolderRule;
36 import org.sonar.server.computation.component.Component;
37 import org.sonar.server.computation.component.DumbDeveloper;
38 import org.sonar.server.computation.component.ReportComponent;
39 import org.sonar.server.computation.measure.Measure;
40 import org.sonar.server.computation.measure.MeasureRepositoryRule;
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 import org.sonar.test.DbTests;
47
48 import static org.assertj.core.api.Assertions.assertThat;
49 import static org.sonar.db.component.SnapshotTesting.createForComponent;
50 import static org.sonar.db.component.SnapshotTesting.newSnapshotForProject;
51 import static org.sonar.server.computation.measure.Measure.newMeasureBuilder;
52
53 @Category(DbTests.class)
54 public class ReportComputeMeasureVariationsStepTest {
55
56   static final Metric ISSUES_METRIC = new MetricImpl(1, "violations", "violations", Metric.MetricType.INT);
57   static final Metric DEBT_METRIC = new MetricImpl(2, "sqale_index", "sqale_index", Metric.MetricType.WORK_DUR);
58   static final Metric FILE_COMPLEXITY_METRIC = new MetricImpl(3, "file_complexity", "file_complexity", Metric.MetricType.FLOAT);
59   static final Metric BUILD_BREAKER_METRIC = new MetricImpl(4, "build_breaker", "build_breaker", Metric.MetricType.BOOL);
60
61   static final ComponentDto PROJECT_DTO = ComponentTesting.newProjectDto();
62
63   static final int PROJECT_REF = 1;
64   static final Component PROJECT = ReportComponent.builder(Component.Type.PROJECT, PROJECT_REF).setUuid(PROJECT_DTO.uuid()).build();
65
66   @Rule
67   public DbTester dbTester = DbTester.create(System2.INSTANCE);
68   @Rule
69   public BatchReportReaderRule reportReader = new BatchReportReaderRule();
70   @Rule
71   public PeriodsHolderRule periodsHolder = new PeriodsHolderRule();
72   @Rule
73   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
74   @Rule
75   public MetricRepositoryRule metricRepository = new MetricRepositoryRule()
76     .add(ISSUES_METRIC)
77     .add(DEBT_METRIC)
78     .add(FILE_COMPLEXITY_METRIC)
79     .add(BUILD_BREAKER_METRIC);
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     dbTester.truncateTables();
92     dbClient.componentDao().insert(session, PROJECT_DTO);
93     session.commit();
94
95     underTest = new ComputeMeasureVariationsStep(dbClient, treeRootHolder, periodsHolder, metricRepository, measureRepository);
96   }
97
98   @Test
99   public void do_nothing_when_no_raw_measure() {
100     SnapshotDto period1ProjectSnapshot = newSnapshotForProject(PROJECT_DTO);
101     dbClient.snapshotDao().insert(session, period1ProjectSnapshot);
102     dbClient.measureDao().insert(session, newMeasureDto(ISSUES_METRIC.getId(), PROJECT_DTO.getId(), period1ProjectSnapshot.getId(), 60d));
103     session.commit();
104
105     periodsHolder.setPeriods(newPeriod(1, period1ProjectSnapshot));
106
107     treeRootHolder.setRoot(PROJECT);
108
109     underTest.execute();
110
111     assertThat(measureRepository.getRawMeasures(PROJECT).keys()).isEmpty();
112   }
113
114   @Test
115   public void do_nothing_when_no_period() {
116     Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid(PROJECT_DTO.uuid()).build();
117     treeRootHolder.setRoot(project);
118     periodsHolder.setPeriods();
119
120     underTest.execute();
121
122     assertThat(measureRepository.getRawMeasures(project).keys()).isEmpty();
123   }
124
125   @Test
126   public void set_variation() {
127     // Project
128     SnapshotDto period1ProjectSnapshot = newSnapshotForProject(PROJECT_DTO);
129     dbClient.snapshotDao().insert(session, period1ProjectSnapshot);
130     dbClient.measureDao().insert(session, newMeasureDto(ISSUES_METRIC.getId(), PROJECT_DTO.getId(), period1ProjectSnapshot.getId(), 60d));
131
132     // Directory
133     ComponentDto directoryDto = ComponentTesting.newDirectory(PROJECT_DTO, "dir");
134     dbClient.componentDao().insert(session, directoryDto);
135     SnapshotDto period1DirectorySnapshot = createForComponent(directoryDto, period1ProjectSnapshot);
136     dbClient.snapshotDao().insert(session, period1DirectorySnapshot);
137     dbClient.measureDao().insert(session, newMeasureDto(ISSUES_METRIC.getId(), directoryDto.getId(), period1DirectorySnapshot.getId(), 10d));
138     session.commit();
139
140     periodsHolder.setPeriods(newPeriod(1, period1ProjectSnapshot));
141
142     Component directory = ReportComponent.builder(Component.Type.DIRECTORY, 2).setUuid(directoryDto.uuid()).build();
143     Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid(PROJECT_DTO.uuid()).addChildren(directory).build();
144     treeRootHolder.setRoot(project);
145
146     addRawMeasure(project, ISSUES_METRIC, newMeasureBuilder().create(80, null));
147     addRawMeasure(directory, ISSUES_METRIC, newMeasureBuilder().create(20, null));
148
149     underTest.execute();
150
151     assertThat(measureRepository.getRawMeasure(project, ISSUES_METRIC).get().getVariations().getVariation1()).isEqualTo(20d);
152     assertThat(measureRepository.getRawMeasure(directory, ISSUES_METRIC).get().getVariations().getVariation1()).isEqualTo(10d);
153   }
154
155   @Test
156   public void set_variations_on_all_periods() {
157     SnapshotDto period1ProjectSnapshot = newSnapshotForProject(PROJECT_DTO).setLast(false);
158     SnapshotDto period2ProjectSnapshot = newSnapshotForProject(PROJECT_DTO).setLast(false);
159     SnapshotDto period3ProjectSnapshot = newSnapshotForProject(PROJECT_DTO).setLast(false);
160     SnapshotDto period4ProjectSnapshot = newSnapshotForProject(PROJECT_DTO).setLast(false);
161     SnapshotDto period5ProjectSnapshot = newSnapshotForProject(PROJECT_DTO).setLast(false);
162     dbClient.snapshotDao().insert(session, period1ProjectSnapshot, period2ProjectSnapshot, period3ProjectSnapshot, period4ProjectSnapshot, period5ProjectSnapshot);
163
164     dbClient.measureDao().insert(session,
165       newMeasureDto(ISSUES_METRIC.getId(), PROJECT_DTO.getId(), period1ProjectSnapshot.getId(), 0d),
166       newMeasureDto(ISSUES_METRIC.getId(), PROJECT_DTO.getId(), period2ProjectSnapshot.getId(), 20d),
167       newMeasureDto(ISSUES_METRIC.getId(), PROJECT_DTO.getId(), period3ProjectSnapshot.getId(), 40d),
168       newMeasureDto(ISSUES_METRIC.getId(), PROJECT_DTO.getId(), period4ProjectSnapshot.getId(), 80d),
169       newMeasureDto(ISSUES_METRIC.getId(), PROJECT_DTO.getId(), period5ProjectSnapshot.getId(), 100d));
170     session.commit();
171
172     periodsHolder.setPeriods(newPeriod(1, period1ProjectSnapshot),
173       newPeriod(2, period2ProjectSnapshot),
174       newPeriod(3, period3ProjectSnapshot),
175       newPeriod(4, period4ProjectSnapshot),
176       newPeriod(5, period5ProjectSnapshot));
177
178     treeRootHolder.setRoot(PROJECT);
179
180     addRawMeasure(PROJECT, ISSUES_METRIC, newMeasureBuilder().create(80, null));
181
182     underTest.execute();
183
184     assertThat(measureRepository.getRawMeasures(PROJECT).keys()).hasSize(1);
185
186     Measure measure = measureRepository.getRawMeasure(PROJECT, ISSUES_METRIC).get();
187     assertThat(measure.hasVariations()).isTrue();
188     assertThat(measure.getVariations().getVariation1()).isEqualTo(80d);
189     assertThat(measure.getVariations().getVariation2()).isEqualTo(60d);
190     assertThat(measure.getVariations().getVariation3()).isEqualTo(40d);
191     assertThat(measure.getVariations().getVariation4()).isEqualTo(0d);
192     assertThat(measure.getVariations().getVariation5()).isEqualTo(-20d);
193   }
194
195   @Test
196   public void set_variation_on_all_numeric_metrics() {
197     SnapshotDto period1ProjectSnapshot = newSnapshotForProject(PROJECT_DTO);
198     dbClient.snapshotDao().insert(session, period1ProjectSnapshot);
199     dbClient.measureDao().insert(session,
200       newMeasureDto(ISSUES_METRIC.getId(), PROJECT_DTO.getId(), period1ProjectSnapshot.getId(), 60d),
201       newMeasureDto(DEBT_METRIC.getId(), PROJECT_DTO.getId(), period1ProjectSnapshot.getId(), 10d),
202       newMeasureDto(FILE_COMPLEXITY_METRIC.getId(), PROJECT_DTO.getId(), period1ProjectSnapshot.getId(), 2d),
203       newMeasureDto(BUILD_BREAKER_METRIC.getId(), PROJECT_DTO.getId(), period1ProjectSnapshot.getId(), 1d)
204       );
205     session.commit();
206
207     periodsHolder.setPeriods(newPeriod(1, period1ProjectSnapshot));
208
209     treeRootHolder.setRoot(PROJECT);
210
211     addRawMeasure(PROJECT, ISSUES_METRIC, newMeasureBuilder().create(80, null));
212     addRawMeasure(PROJECT, DEBT_METRIC, newMeasureBuilder().create(5L, null));
213     addRawMeasure(PROJECT, FILE_COMPLEXITY_METRIC, newMeasureBuilder().create(3d, 1, null));
214     addRawMeasure(PROJECT, BUILD_BREAKER_METRIC, newMeasureBuilder().create(false, null));
215
216     underTest.execute();
217
218     assertThat(measureRepository.getRawMeasures(PROJECT).keys()).hasSize(4);
219
220     assertThat(measureRepository.getRawMeasure(PROJECT, ISSUES_METRIC).get().getVariations().getVariation1()).isEqualTo(20d);
221     assertThat(measureRepository.getRawMeasure(PROJECT, DEBT_METRIC).get().getVariations().getVariation1()).isEqualTo(-5d);
222     assertThat(measureRepository.getRawMeasure(PROJECT, FILE_COMPLEXITY_METRIC).get().getVariations().getVariation1()).isEqualTo(1d);
223     assertThat(measureRepository.getRawMeasure(PROJECT, BUILD_BREAKER_METRIC).get().getVariations().getVariation1()).isEqualTo(-1d);
224   }
225
226   @Test
227   public void do_not_set_variations_on_numeric_metric_for_developer() {
228     SnapshotDto period1ProjectSnapshot = newSnapshotForProject(PROJECT_DTO);
229     dbClient.snapshotDao().insert(session, period1ProjectSnapshot);
230     dbClient.measureDao().insert(session,
231       newMeasureDto(ISSUES_METRIC.getId(), PROJECT_DTO.getId(), period1ProjectSnapshot.getId(), 60d)
232       );
233     session.commit();
234
235     periodsHolder.setPeriods(newPeriod(1, period1ProjectSnapshot));
236
237     treeRootHolder.setRoot(PROJECT);
238
239     DumbDeveloper developer = new DumbDeveloper("a");
240     measureRepository.addRawMeasure(PROJECT_REF, ISSUES_METRIC.getKey(), newMeasureBuilder().forDeveloper(developer).create(80, null));
241
242     underTest.execute();
243
244     assertThat(measureRepository.getRawMeasures(PROJECT).keys()).hasSize(1);
245
246     assertThat(measureRepository.getRawMeasure(PROJECT, ISSUES_METRIC, developer).get().hasVariations()).isFalse();
247   }
248
249   private static MeasureDto newMeasureDto(int metricId, long projectId, long snapshotId, double value) {
250     return new MeasureDto().setMetricId(metricId).setComponentId(projectId).setSnapshotId(snapshotId).setValue(value);
251   }
252
253   private static Period newPeriod(int index, SnapshotDto snapshotDto) {
254     return new Period(index, "mode", null, snapshotDto.getCreatedAt(), snapshotDto.getId());
255   }
256
257   private void addRawMeasure(Component component, Metric metric, Measure measure) {
258     measureRepository.addRawMeasure(component.getReportAttributes().getRef(), metric.getKey(), measure);
259   }
260 }