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