]> source.dussan.org Git - sonarqube.git/blob
be5f57befc78a87fb0863688192877cde76e3e1a
[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.ViewsComponent;
38 import org.sonar.server.computation.measure.Measure;
39 import org.sonar.server.computation.measure.MeasureRepositoryRule;
40 import org.sonar.server.computation.metric.Metric;
41 import org.sonar.server.computation.metric.MetricImpl;
42 import org.sonar.server.computation.metric.MetricRepositoryRule;
43 import org.sonar.server.computation.period.Period;
44 import org.sonar.server.computation.period.PeriodsHolderRule;
45 import org.sonar.test.DbTests;
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.db.component.SnapshotTesting.newSnapshotForView;
51
52 @Category(DbTests.class)
53 public class ViewsComputeMeasureVariationsStepTest {
54
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);
59
60   static final ComponentDto VIEW_DTO = ComponentTesting.newView();
61
62   static final Component VIEW = ViewsComponent.builder(Component.Type.VIEW, 1).setUuid(VIEW_DTO.uuid()).build();
63
64   @Rule
65   public DbTester dbTester = DbTester.create(System2.INSTANCE);
66
67   @Rule
68   public BatchReportReaderRule reportReader = new BatchReportReaderRule();
69
70   @Rule
71   public PeriodsHolderRule periodsHolder = new PeriodsHolderRule();
72
73   @Rule
74   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
75
76   @Rule
77   public MetricRepositoryRule metricRepository = new MetricRepositoryRule()
78     .add(ISSUES_METRIC)
79     .add(DEBT_METRIC)
80     .add(FILE_COMPLEXITY_METRIC)
81     .add(BUILD_BREAKER_METRIC);
82
83   @Rule
84   public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
85
86   DbSession session = dbTester.getSession();
87
88   DbClient dbClient = dbTester.getDbClient();
89
90   ComputeMeasureVariationsStep underTest;
91
92   @Before
93   public void setUp() {
94     dbTester.truncateTables();
95     dbClient.componentDao().insert(session, VIEW_DTO);
96     session.commit();
97
98     underTest = new ComputeMeasureVariationsStep(dbClient, treeRootHolder, periodsHolder, metricRepository, measureRepository);
99   }
100
101   @Test
102   public void do_nothing_when_no_raw_measure() {
103     SnapshotDto period1ViewSnapshot = newSnapshotForView(VIEW_DTO);
104     dbClient.snapshotDao().insert(session, period1ViewSnapshot);
105     dbClient.measureDao().insert(session, newMeasureDto(ISSUES_METRIC.getId(), VIEW_DTO.getId(), period1ViewSnapshot.getId(), 60d));
106     session.commit();
107
108     periodsHolder.setPeriods(newPeriod(1, period1ViewSnapshot));
109
110     treeRootHolder.setRoot(VIEW);
111
112     underTest.execute();
113
114     assertThat(measureRepository.getRawMeasures(VIEW).keys()).isEmpty();
115   }
116
117   @Test
118   public void do_nothing_when_no_period() {
119     Component view = ViewsComponent.builder(Component.Type.VIEW, 1).setUuid(VIEW_DTO.uuid()).build();
120     treeRootHolder.setRoot(view);
121     periodsHolder.setPeriods();
122
123     underTest.execute();
124
125     assertThat(measureRepository.getRawMeasures(view).keys()).isEmpty();
126   }
127
128   @Test
129   public void set_variation() {
130     // View
131     SnapshotDto period1ViewSnapshot = newSnapshotForView(VIEW_DTO);
132     dbClient.snapshotDao().insert(session, period1ViewSnapshot);
133     dbClient.measureDao().insert(session, newMeasureDto(ISSUES_METRIC.getId(), VIEW_DTO.getId(), period1ViewSnapshot.getId(), 60d));
134
135     // SubView
136     ComponentDto subviewDto = ComponentTesting.newSubView(VIEW_DTO, "dir", "key");
137     dbClient.componentDao().insert(session, subviewDto);
138     SnapshotDto period1SubviewSnapshot = createForComponent(subviewDto, period1ViewSnapshot);
139     dbClient.snapshotDao().insert(session, period1SubviewSnapshot);
140     dbClient.measureDao().insert(session, newMeasureDto(ISSUES_METRIC.getId(), subviewDto.getId(), period1SubviewSnapshot.getId(), 10d));
141     session.commit();
142
143     periodsHolder.setPeriods(newPeriod(1, period1ViewSnapshot));
144
145     Component subview = ViewsComponent.builder(Component.Type.SUBVIEW, 2).setUuid(subviewDto.uuid()).build();
146     Component view = ViewsComponent.builder(Component.Type.VIEW, 1).setUuid(VIEW_DTO.uuid()).addChildren(subview).build();
147     treeRootHolder.setRoot(view);
148
149     addRawMeasure(view, ISSUES_METRIC, Measure.newMeasureBuilder().create(80, null));
150     addRawMeasure(subview, ISSUES_METRIC, Measure.newMeasureBuilder().create(20, null));
151
152     underTest.execute();
153
154     assertThat(measureRepository.getRawMeasure(view, ISSUES_METRIC).get().getVariations().getVariation1()).isEqualTo(20d);
155     assertThat(measureRepository.getRawMeasure(subview, ISSUES_METRIC).get().getVariations().getVariation1()).isEqualTo(10d);
156   }
157
158   @Test
159   public void set_variations_on_all_periods() {
160     SnapshotDto period1ViewSnapshot = newSnapshotForProject(VIEW_DTO).setLast(false);
161     SnapshotDto period2ViewSnapshot = newSnapshotForProject(VIEW_DTO).setLast(false);
162     SnapshotDto period3ViewSnapshot = newSnapshotForProject(VIEW_DTO).setLast(false);
163     SnapshotDto period4ViewSnapshot = newSnapshotForProject(VIEW_DTO).setLast(false);
164     SnapshotDto period5ViewSnapshot = newSnapshotForProject(VIEW_DTO).setLast(false);
165     dbClient.snapshotDao().insert(session, period1ViewSnapshot, period2ViewSnapshot, period3ViewSnapshot, period4ViewSnapshot, period5ViewSnapshot);
166
167     dbClient.measureDao().insert(session,
168       newMeasureDto(ISSUES_METRIC.getId(), VIEW_DTO.getId(), period1ViewSnapshot.getId(), 0d),
169       newMeasureDto(ISSUES_METRIC.getId(), VIEW_DTO.getId(), period2ViewSnapshot.getId(), 20d),
170       newMeasureDto(ISSUES_METRIC.getId(), VIEW_DTO.getId(), period3ViewSnapshot.getId(), 40d),
171       newMeasureDto(ISSUES_METRIC.getId(), VIEW_DTO.getId(), period4ViewSnapshot.getId(), 80d),
172       newMeasureDto(ISSUES_METRIC.getId(), VIEW_DTO.getId(), period5ViewSnapshot.getId(), 100d));
173     session.commit();
174
175     periodsHolder.setPeriods(newPeriod(1, period1ViewSnapshot),
176       newPeriod(2, period2ViewSnapshot),
177       newPeriod(3, period3ViewSnapshot),
178       newPeriod(4, period4ViewSnapshot),
179       newPeriod(5, period5ViewSnapshot));
180
181     treeRootHolder.setRoot(VIEW);
182
183     addRawMeasure(VIEW, ISSUES_METRIC, Measure.newMeasureBuilder().create(80, null));
184
185     underTest.execute();
186
187     assertThat(measureRepository.getRawMeasures(VIEW).keys()).hasSize(1);
188
189     Measure measure = measureRepository.getRawMeasure(VIEW, 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);
196   }
197
198   @Test
199   public void set_variation_on_all_numeric_metrics() {
200     SnapshotDto period1ViewSnapshot = newSnapshotForProject(VIEW_DTO);
201     dbClient.snapshotDao().insert(session, period1ViewSnapshot);
202     dbClient.measureDao().insert(session,
203       newMeasureDto(ISSUES_METRIC.getId(), VIEW_DTO.getId(), period1ViewSnapshot.getId(), 60d),
204       newMeasureDto(DEBT_METRIC.getId(), VIEW_DTO.getId(), period1ViewSnapshot.getId(), 10d),
205       newMeasureDto(FILE_COMPLEXITY_METRIC.getId(), VIEW_DTO.getId(), period1ViewSnapshot.getId(), 2d),
206       newMeasureDto(BUILD_BREAKER_METRIC.getId(), VIEW_DTO.getId(), period1ViewSnapshot.getId(), 1d)
207     );
208     session.commit();
209
210     periodsHolder.setPeriods(newPeriod(1, period1ViewSnapshot));
211
212     treeRootHolder.setRoot(VIEW);
213
214     addRawMeasure(VIEW, ISSUES_METRIC, Measure.newMeasureBuilder().create(80, null));
215     addRawMeasure(VIEW, DEBT_METRIC, Measure.newMeasureBuilder().create(5L, null));
216     addRawMeasure(VIEW, FILE_COMPLEXITY_METRIC, Measure.newMeasureBuilder().create(3d, 1));
217     addRawMeasure(VIEW, BUILD_BREAKER_METRIC, Measure.newMeasureBuilder().create(false, null));
218
219     underTest.execute();
220
221     assertThat(measureRepository.getRawMeasures(VIEW).keys()).hasSize(4);
222
223     assertThat(measureRepository.getRawMeasure(VIEW, ISSUES_METRIC).get().getVariations().getVariation1()).isEqualTo(20d);
224     assertThat(measureRepository.getRawMeasure(VIEW, DEBT_METRIC).get().getVariations().getVariation1()).isEqualTo(-5d);
225     assertThat(measureRepository.getRawMeasure(VIEW, FILE_COMPLEXITY_METRIC).get().getVariations().getVariation1()).isEqualTo(1d);
226     assertThat(measureRepository.getRawMeasure(VIEW, BUILD_BREAKER_METRIC).get().getVariations().getVariation1()).isEqualTo(-1d);
227   }
228
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);
231   }
232
233   private static Period newPeriod(int index, SnapshotDto snapshotDto) {
234     return new Period(index, "mode", null, snapshotDto.getCreatedAt(), snapshotDto.getId());
235   }
236
237   private void addRawMeasure(Component component, Metric metric, Measure measure) {
238     measureRepository.add(component, metric, measure);
239   }
240 }