3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.computation.step;
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.ViewsComponent;
37 import org.sonar.server.computation.measure.Measure;
38 import org.sonar.server.computation.measure.MeasureRepositoryRule;
39 import org.sonar.server.computation.metric.Metric;
40 import org.sonar.server.computation.metric.MetricImpl;
41 import org.sonar.server.computation.metric.MetricRepositoryRule;
42 import org.sonar.server.computation.period.Period;
43 import org.sonar.server.computation.period.PeriodsHolderRule;
45 import static org.assertj.core.api.Assertions.assertThat;
46 import static org.sonar.db.component.SnapshotTesting.createForComponent;
47 import static org.sonar.db.component.SnapshotTesting.newSnapshotForProject;
48 import static org.sonar.db.component.SnapshotTesting.newSnapshotForView;
50 public class ViewsComputeMeasureVariationsStepTest {
52 static final Metric ISSUES_METRIC = new MetricImpl(1, "violations", "violations", Metric.MetricType.INT);
53 static final Metric DEBT_METRIC = new MetricImpl(2, "sqale_index", "sqale_index", Metric.MetricType.WORK_DUR);
54 static final Metric FILE_COMPLEXITY_METRIC = new MetricImpl(3, "file_complexity", "file_complexity", Metric.MetricType.FLOAT);
55 static final Metric BUILD_BREAKER_METRIC = new MetricImpl(4, "build_breaker", "build_breaker", Metric.MetricType.BOOL);
57 static final ComponentDto VIEW_DTO = ComponentTesting.newView();
59 static final Component VIEW = ViewsComponent.builder(Component.Type.VIEW, 1).setUuid(VIEW_DTO.uuid()).build();
62 public DbTester dbTester = DbTester.create(System2.INSTANCE);
65 public BatchReportReaderRule reportReader = new BatchReportReaderRule();
68 public PeriodsHolderRule periodsHolder = new PeriodsHolderRule();
71 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
74 public MetricRepositoryRule metricRepository = new MetricRepositoryRule()
77 .add(FILE_COMPLEXITY_METRIC)
78 .add(BUILD_BREAKER_METRIC);
81 public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
83 DbSession session = dbTester.getSession();
85 DbClient dbClient = dbTester.getDbClient();
87 ComputeMeasureVariationsStep underTest;
91 dbClient.componentDao().insert(session, VIEW_DTO);
94 underTest = new ComputeMeasureVariationsStep(dbClient, treeRootHolder, periodsHolder, metricRepository, measureRepository);
98 public void do_nothing_when_no_raw_measure() {
99 SnapshotDto period1ViewSnapshot = newSnapshotForView(VIEW_DTO);
100 dbClient.snapshotDao().insert(session, period1ViewSnapshot);
101 dbClient.measureDao().insert(session, newMeasureDto(ISSUES_METRIC.getId(), VIEW_DTO.uuid(), period1ViewSnapshot.getId(), 60d));
104 periodsHolder.setPeriods(newPeriod(1, period1ViewSnapshot));
106 treeRootHolder.setRoot(VIEW);
110 assertThat(measureRepository.getRawMeasures(VIEW).keys()).isEmpty();
114 public void do_nothing_when_no_period() {
115 Component view = ViewsComponent.builder(Component.Type.VIEW, 1).setUuid(VIEW_DTO.uuid()).build();
116 treeRootHolder.setRoot(view);
117 periodsHolder.setPeriods();
121 assertThat(measureRepository.getRawMeasures(view).keys()).isEmpty();
125 public void set_variation() {
127 SnapshotDto period1ViewSnapshot = newSnapshotForView(VIEW_DTO);
128 dbClient.snapshotDao().insert(session, period1ViewSnapshot);
129 dbClient.measureDao().insert(session, newMeasureDto(ISSUES_METRIC.getId(), VIEW_DTO.uuid(), period1ViewSnapshot.getId(), 60d));
132 ComponentDto subviewDto = ComponentTesting.newSubView(VIEW_DTO, "dir", "key");
133 dbClient.componentDao().insert(session, subviewDto);
134 SnapshotDto period1SubviewSnapshot = createForComponent(subviewDto, period1ViewSnapshot);
135 dbClient.snapshotDao().insert(session, period1SubviewSnapshot);
136 dbClient.measureDao().insert(session, newMeasureDto(ISSUES_METRIC.getId(), subviewDto.uuid(), period1SubviewSnapshot.getId(), 10d));
139 periodsHolder.setPeriods(newPeriod(1, period1ViewSnapshot));
141 Component subview = ViewsComponent.builder(Component.Type.SUBVIEW, 2).setUuid(subviewDto.uuid()).build();
142 Component view = ViewsComponent.builder(Component.Type.VIEW, 1).setUuid(VIEW_DTO.uuid()).addChildren(subview).build();
143 treeRootHolder.setRoot(view);
145 addRawMeasure(view, ISSUES_METRIC, Measure.newMeasureBuilder().create(80, null));
146 addRawMeasure(subview, ISSUES_METRIC, Measure.newMeasureBuilder().create(20, null));
150 assertThat(measureRepository.getRawMeasure(view, ISSUES_METRIC).get().getVariations().getVariation1()).isEqualTo(20d);
151 assertThat(measureRepository.getRawMeasure(subview, ISSUES_METRIC).get().getVariations().getVariation1()).isEqualTo(10d);
155 public void set_variations_on_all_periods() {
156 SnapshotDto period1ViewSnapshot = newSnapshotForProject(VIEW_DTO).setLast(false);
157 SnapshotDto period2ViewSnapshot = newSnapshotForProject(VIEW_DTO).setLast(false);
158 SnapshotDto period3ViewSnapshot = newSnapshotForProject(VIEW_DTO).setLast(false);
159 SnapshotDto period4ViewSnapshot = newSnapshotForProject(VIEW_DTO).setLast(false);
160 SnapshotDto period5ViewSnapshot = newSnapshotForProject(VIEW_DTO).setLast(false);
161 dbClient.snapshotDao().insert(session, period1ViewSnapshot, period2ViewSnapshot, period3ViewSnapshot, period4ViewSnapshot, period5ViewSnapshot);
163 dbClient.measureDao().insert(session,
164 newMeasureDto(ISSUES_METRIC.getId(), VIEW_DTO.uuid(), period1ViewSnapshot.getId(), 0d),
165 newMeasureDto(ISSUES_METRIC.getId(), VIEW_DTO.uuid(), period2ViewSnapshot.getId(), 20d),
166 newMeasureDto(ISSUES_METRIC.getId(), VIEW_DTO.uuid(), period3ViewSnapshot.getId(), 40d),
167 newMeasureDto(ISSUES_METRIC.getId(), VIEW_DTO.uuid(), period4ViewSnapshot.getId(), 80d),
168 newMeasureDto(ISSUES_METRIC.getId(), VIEW_DTO.uuid(), period5ViewSnapshot.getId(), 100d));
171 periodsHolder.setPeriods(newPeriod(1, period1ViewSnapshot),
172 newPeriod(2, period2ViewSnapshot),
173 newPeriod(3, period3ViewSnapshot),
174 newPeriod(4, period4ViewSnapshot),
175 newPeriod(5, period5ViewSnapshot));
177 treeRootHolder.setRoot(VIEW);
179 addRawMeasure(VIEW, ISSUES_METRIC, Measure.newMeasureBuilder().create(80, null));
183 assertThat(measureRepository.getRawMeasures(VIEW).keys()).hasSize(1);
185 Measure measure = measureRepository.getRawMeasure(VIEW, ISSUES_METRIC).get();
186 assertThat(measure.hasVariations()).isTrue();
187 assertThat(measure.getVariations().getVariation1()).isEqualTo(80d);
188 assertThat(measure.getVariations().getVariation2()).isEqualTo(60d);
189 assertThat(measure.getVariations().getVariation3()).isEqualTo(40d);
190 assertThat(measure.getVariations().getVariation4()).isEqualTo(0d);
191 assertThat(measure.getVariations().getVariation5()).isEqualTo(-20d);
195 public void set_variation_on_all_numeric_metrics() {
196 SnapshotDto period1ViewSnapshot = newSnapshotForProject(VIEW_DTO);
197 dbClient.snapshotDao().insert(session, period1ViewSnapshot);
198 dbClient.measureDao().insert(session,
199 newMeasureDto(ISSUES_METRIC.getId(), VIEW_DTO.uuid(), period1ViewSnapshot.getId(), 60d),
200 newMeasureDto(DEBT_METRIC.getId(), VIEW_DTO.uuid(), period1ViewSnapshot.getId(), 10d),
201 newMeasureDto(FILE_COMPLEXITY_METRIC.getId(), VIEW_DTO.uuid(), period1ViewSnapshot.getId(), 2d),
202 newMeasureDto(BUILD_BREAKER_METRIC.getId(), VIEW_DTO.uuid(), period1ViewSnapshot.getId(), 1d)
206 periodsHolder.setPeriods(newPeriod(1, period1ViewSnapshot));
208 treeRootHolder.setRoot(VIEW);
210 addRawMeasure(VIEW, ISSUES_METRIC, Measure.newMeasureBuilder().create(80, null));
211 addRawMeasure(VIEW, DEBT_METRIC, Measure.newMeasureBuilder().create(5L, null));
212 addRawMeasure(VIEW, FILE_COMPLEXITY_METRIC, Measure.newMeasureBuilder().create(3d, 1));
213 addRawMeasure(VIEW, BUILD_BREAKER_METRIC, Measure.newMeasureBuilder().create(false, null));
217 assertThat(measureRepository.getRawMeasures(VIEW).keys()).hasSize(4);
219 assertThat(measureRepository.getRawMeasure(VIEW, ISSUES_METRIC).get().getVariations().getVariation1()).isEqualTo(20d);
220 assertThat(measureRepository.getRawMeasure(VIEW, DEBT_METRIC).get().getVariations().getVariation1()).isEqualTo(-5d);
221 assertThat(measureRepository.getRawMeasure(VIEW, FILE_COMPLEXITY_METRIC).get().getVariations().getVariation1()).isEqualTo(1d);
222 assertThat(measureRepository.getRawMeasure(VIEW, BUILD_BREAKER_METRIC).get().getVariations().getVariation1()).isEqualTo(-1d);
225 private static MeasureDto newMeasureDto(int metricId, String componentUuid, long snapshotId, double value) {
226 return new MeasureDto().setMetricId(metricId).setComponentUuid(componentUuid).setSnapshotId(snapshotId).setValue(value);
229 private static Period newPeriod(int index, SnapshotDto snapshotDto) {
230 return new Period(index, "mode", null, snapshotDto.getCreatedAt(), snapshotDto.getId());
233 private void addRawMeasure(Component component, Metric metric, Measure measure) {
234 measureRepository.add(component, metric, measure);