2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2014 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * SonarQube 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.
11 * SonarQube 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.
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.
21 package org.sonar.server.computation.step;
23 import org.junit.Before;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.junit.experimental.categories.Category;
27 import org.sonar.api.utils.System2;
28 import org.sonar.db.DbClient;
29 import org.sonar.db.DbSession;
30 import org.sonar.db.DbTester;
31 import org.sonar.db.component.ComponentDto;
32 import org.sonar.db.component.ComponentTesting;
33 import org.sonar.db.component.SnapshotDto;
34 import org.sonar.db.measure.MeasureDto;
35 import org.sonar.server.computation.batch.BatchReportReaderRule;
36 import org.sonar.server.computation.batch.TreeRootHolderRule;
37 import org.sonar.server.computation.component.Component;
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;
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;
52 @Category(DbTests.class)
53 public class ReportComputeMeasureVariationsStepTest {
55 static final Metric ISSUES_METRIC = new MetricImpl(1, "violations", "violations", Metric.MetricType.INT);
56 static final Metric DEBT_METRIC = new MetricImpl(2, "sqale_index", "sqale_index", Metric.MetricType.WORK_DUR);
57 static final Metric FILE_COMPLEXITY_METRIC = new MetricImpl(3, "file_complexity", "file_complexity", Metric.MetricType.FLOAT);
58 static final Metric BUILD_BREAKER_METRIC = new MetricImpl(4, "build_breaker", "build_breaker", Metric.MetricType.BOOL);
60 static final ComponentDto PROJECT_DTO = ComponentTesting.newProjectDto();
62 static final Component PROJECT = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid(PROJECT_DTO.uuid()).build();
65 public DbTester dbTester = DbTester.create(System2.INSTANCE);
68 public BatchReportReaderRule reportReader = new BatchReportReaderRule();
71 public PeriodsHolderRule periodsHolder = new PeriodsHolderRule();
74 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
77 public MetricRepositoryRule metricRepository = new MetricRepositoryRule()
80 .add(FILE_COMPLEXITY_METRIC)
81 .add(BUILD_BREAKER_METRIC);
84 public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
86 DbSession session = dbTester.getSession();
88 DbClient dbClient = dbTester.getDbClient();
90 ComputeMeasureVariationsStep underTest;
94 dbTester.truncateTables();
95 dbClient.componentDao().insert(session, PROJECT_DTO);
98 underTest = new ComputeMeasureVariationsStep(dbClient, treeRootHolder, periodsHolder, metricRepository, measureRepository);
102 public void do_nothing_when_no_raw_measure() {
103 SnapshotDto period1ProjectSnapshot = newSnapshotForProject(PROJECT_DTO);
104 dbClient.snapshotDao().insert(session, period1ProjectSnapshot);
105 dbClient.measureDao().insert(session, newMeasureDto(ISSUES_METRIC.getId(), PROJECT_DTO.getId(), period1ProjectSnapshot.getId(), 60d));
108 periodsHolder.setPeriods(newPeriod(1, period1ProjectSnapshot));
110 treeRootHolder.setRoot(PROJECT);
114 assertThat(measureRepository.getRawMeasures(PROJECT).keys()).isEmpty();
118 public void do_nothing_when_no_period() {
119 Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid(PROJECT_DTO.uuid()).build();
120 treeRootHolder.setRoot(project);
121 periodsHolder.setPeriods();
125 assertThat(measureRepository.getRawMeasures(project).keys()).isEmpty();
129 public void set_variation() {
131 SnapshotDto period1ProjectSnapshot = newSnapshotForProject(PROJECT_DTO);
132 dbClient.snapshotDao().insert(session, period1ProjectSnapshot);
133 dbClient.measureDao().insert(session, newMeasureDto(ISSUES_METRIC.getId(), PROJECT_DTO.getId(), period1ProjectSnapshot.getId(), 60d));
136 ComponentDto directoryDto = ComponentTesting.newDirectory(PROJECT_DTO, "dir");
137 dbClient.componentDao().insert(session, directoryDto);
138 SnapshotDto period1DirectorySnapshot = createForComponent(directoryDto, period1ProjectSnapshot);
139 dbClient.snapshotDao().insert(session, period1DirectorySnapshot);
140 dbClient.measureDao().insert(session, newMeasureDto(ISSUES_METRIC.getId(), directoryDto.getId(), period1DirectorySnapshot.getId(), 10d));
143 periodsHolder.setPeriods(newPeriod(1, period1ProjectSnapshot));
145 Component directory = ReportComponent.builder(Component.Type.DIRECTORY, 2).setUuid(directoryDto.uuid()).build();
146 Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid(PROJECT_DTO.uuid()).addChildren(directory).build();
147 treeRootHolder.setRoot(project);
149 addRawMeasure(project, ISSUES_METRIC, Measure.newMeasureBuilder().create(80, null));
150 addRawMeasure(directory, ISSUES_METRIC, Measure.newMeasureBuilder().create(20, null));
154 assertThat(measureRepository.getRawMeasure(project, ISSUES_METRIC).get().getVariations().getVariation1()).isEqualTo(20d);
155 assertThat(measureRepository.getRawMeasure(directory, ISSUES_METRIC).get().getVariations().getVariation1()).isEqualTo(10d);
159 public void set_variations_on_all_periods() {
160 SnapshotDto period1ProjectSnapshot = newSnapshotForProject(PROJECT_DTO).setLast(false);
161 SnapshotDto period2ProjectSnapshot = newSnapshotForProject(PROJECT_DTO).setLast(false);
162 SnapshotDto period3ProjectSnapshot = newSnapshotForProject(PROJECT_DTO).setLast(false);
163 SnapshotDto period4ProjectSnapshot = newSnapshotForProject(PROJECT_DTO).setLast(false);
164 SnapshotDto period5ProjectSnapshot = newSnapshotForProject(PROJECT_DTO).setLast(false);
165 dbClient.snapshotDao().insert(session, period1ProjectSnapshot, period2ProjectSnapshot, period3ProjectSnapshot, period4ProjectSnapshot, period5ProjectSnapshot);
167 dbClient.measureDao().insert(session,
168 newMeasureDto(ISSUES_METRIC.getId(), PROJECT_DTO.getId(), period1ProjectSnapshot.getId(), 0d),
169 newMeasureDto(ISSUES_METRIC.getId(), PROJECT_DTO.getId(), period2ProjectSnapshot.getId(), 20d),
170 newMeasureDto(ISSUES_METRIC.getId(), PROJECT_DTO.getId(), period3ProjectSnapshot.getId(), 40d),
171 newMeasureDto(ISSUES_METRIC.getId(), PROJECT_DTO.getId(), period4ProjectSnapshot.getId(), 80d),
172 newMeasureDto(ISSUES_METRIC.getId(), PROJECT_DTO.getId(), period5ProjectSnapshot.getId(), 100d));
175 periodsHolder.setPeriods(newPeriod(1, period1ProjectSnapshot),
176 newPeriod(2, period2ProjectSnapshot),
177 newPeriod(3, period3ProjectSnapshot),
178 newPeriod(4, period4ProjectSnapshot),
179 newPeriod(5, period5ProjectSnapshot));
181 treeRootHolder.setRoot(PROJECT);
183 addRawMeasure(PROJECT, ISSUES_METRIC, Measure.newMeasureBuilder().create(80, null));
187 assertThat(measureRepository.getRawMeasures(PROJECT).keys()).hasSize(1);
189 Measure measure = measureRepository.getRawMeasure(PROJECT, ISSUES_METRIC).get();
190 assertThat(measure.hasVariations()).isTrue();
191 assertThat(measure.getVariations().getVariation1()).isEqualTo(80d);
192 assertThat(measure.getVariations().getVariation2()).isEqualTo(60d);
193 assertThat(measure.getVariations().getVariation3()).isEqualTo(40d);
194 assertThat(measure.getVariations().getVariation4()).isEqualTo(0d);
195 assertThat(measure.getVariations().getVariation5()).isEqualTo(-20d);
199 public void set_variation_on_all_numeric_metrics() {
200 SnapshotDto period1ProjectSnapshot = newSnapshotForProject(PROJECT_DTO);
201 dbClient.snapshotDao().insert(session, period1ProjectSnapshot);
202 dbClient.measureDao().insert(session,
203 newMeasureDto(ISSUES_METRIC.getId(), PROJECT_DTO.getId(), period1ProjectSnapshot.getId(), 60d),
204 newMeasureDto(DEBT_METRIC.getId(), PROJECT_DTO.getId(), period1ProjectSnapshot.getId(), 10d),
205 newMeasureDto(FILE_COMPLEXITY_METRIC.getId(), PROJECT_DTO.getId(), period1ProjectSnapshot.getId(), 2d),
206 newMeasureDto(BUILD_BREAKER_METRIC.getId(), PROJECT_DTO.getId(), period1ProjectSnapshot.getId(), 1d)
210 periodsHolder.setPeriods(newPeriod(1, period1ProjectSnapshot));
212 treeRootHolder.setRoot(PROJECT);
214 addRawMeasure(PROJECT, ISSUES_METRIC, Measure.newMeasureBuilder().create(80, null));
215 addRawMeasure(PROJECT, DEBT_METRIC, Measure.newMeasureBuilder().create(5L, null));
216 addRawMeasure(PROJECT, FILE_COMPLEXITY_METRIC, Measure.newMeasureBuilder().create(3d, null));
217 addRawMeasure(PROJECT, BUILD_BREAKER_METRIC, Measure.newMeasureBuilder().create(false, null));
221 assertThat(measureRepository.getRawMeasures(PROJECT).keys()).hasSize(4);
223 assertThat(measureRepository.getRawMeasure(PROJECT, ISSUES_METRIC).get().getVariations().getVariation1()).isEqualTo(20d);
224 assertThat(measureRepository.getRawMeasure(PROJECT, DEBT_METRIC).get().getVariations().getVariation1()).isEqualTo(-5d);
225 assertThat(measureRepository.getRawMeasure(PROJECT, FILE_COMPLEXITY_METRIC).get().getVariations().getVariation1()).isEqualTo(1d);
226 assertThat(measureRepository.getRawMeasure(PROJECT, BUILD_BREAKER_METRIC).get().getVariations().getVariation1()).isEqualTo(-1d);
229 private static MeasureDto newMeasureDto(int metricId, long projectId, long snapshotId, double value) {
230 return new MeasureDto().setMetricId(metricId).setComponentId(projectId).setSnapshotId(snapshotId).setValue(value);
233 private static Period newPeriod(int index, SnapshotDto snapshotDto) {
234 return new Period(index, "mode", null, snapshotDto.getCreatedAt(), snapshotDto.getId());
237 private void addRawMeasure(Component component, Metric metric, Measure measure) {
238 measureRepository.addRawMeasure(component.getReportAttributes().getRef(), metric.getKey(), measure);