]> source.dussan.org Git - sonarqube.git/blob
4f5cef4e49c9c3ec272ea80bb6e0bb44f8fff165
[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.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;
44
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;
49
50 public class ViewsComputeMeasureVariationsStepTest {
51
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);
56
57   static final ComponentDto VIEW_DTO = ComponentTesting.newView();
58
59   static final Component VIEW = ViewsComponent.builder(Component.Type.VIEW, 1).setUuid(VIEW_DTO.uuid()).build();
60
61   @Rule
62   public DbTester dbTester = DbTester.create(System2.INSTANCE);
63
64   @Rule
65   public BatchReportReaderRule reportReader = new BatchReportReaderRule();
66
67   @Rule
68   public PeriodsHolderRule periodsHolder = new PeriodsHolderRule();
69
70   @Rule
71   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
72
73   @Rule
74   public MetricRepositoryRule metricRepository = new MetricRepositoryRule()
75     .add(ISSUES_METRIC)
76     .add(DEBT_METRIC)
77     .add(FILE_COMPLEXITY_METRIC)
78     .add(BUILD_BREAKER_METRIC);
79
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     dbClient.componentDao().insert(session, VIEW_DTO);
92     session.commit();
93
94     underTest = new ComputeMeasureVariationsStep(dbClient, treeRootHolder, periodsHolder, metricRepository, measureRepository);
95   }
96
97   @Test
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));
102     session.commit();
103
104     periodsHolder.setPeriods(newPeriod(1, period1ViewSnapshot));
105
106     treeRootHolder.setRoot(VIEW);
107
108     underTest.execute();
109
110     assertThat(measureRepository.getRawMeasures(VIEW).keys()).isEmpty();
111   }
112
113   @Test
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();
118
119     underTest.execute();
120
121     assertThat(measureRepository.getRawMeasures(view).keys()).isEmpty();
122   }
123
124   @Test
125   public void set_variation() {
126     // View
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));
130
131     // SubView
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));
137     session.commit();
138
139     periodsHolder.setPeriods(newPeriod(1, period1ViewSnapshot));
140
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);
144
145     addRawMeasure(view, ISSUES_METRIC, Measure.newMeasureBuilder().create(80, null));
146     addRawMeasure(subview, ISSUES_METRIC, Measure.newMeasureBuilder().create(20, null));
147
148     underTest.execute();
149
150     assertThat(measureRepository.getRawMeasure(view, ISSUES_METRIC).get().getVariations().getVariation1()).isEqualTo(20d);
151     assertThat(measureRepository.getRawMeasure(subview, ISSUES_METRIC).get().getVariations().getVariation1()).isEqualTo(10d);
152   }
153
154   @Test
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);
162
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));
169     session.commit();
170
171     periodsHolder.setPeriods(newPeriod(1, period1ViewSnapshot),
172       newPeriod(2, period2ViewSnapshot),
173       newPeriod(3, period3ViewSnapshot),
174       newPeriod(4, period4ViewSnapshot),
175       newPeriod(5, period5ViewSnapshot));
176
177     treeRootHolder.setRoot(VIEW);
178
179     addRawMeasure(VIEW, ISSUES_METRIC, Measure.newMeasureBuilder().create(80, null));
180
181     underTest.execute();
182
183     assertThat(measureRepository.getRawMeasures(VIEW).keys()).hasSize(1);
184
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);
192   }
193
194   @Test
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)
203       );
204     session.commit();
205
206     periodsHolder.setPeriods(newPeriod(1, period1ViewSnapshot));
207
208     treeRootHolder.setRoot(VIEW);
209
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));
214
215     underTest.execute();
216
217     assertThat(measureRepository.getRawMeasures(VIEW).keys()).hasSize(4);
218
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);
223   }
224
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);
227   }
228
229   private static Period newPeriod(int index, SnapshotDto snapshotDto) {
230     return new Period(index, "mode", null, snapshotDto.getCreatedAt(), snapshotDto.getId());
231   }
232
233   private void addRawMeasure(Component component, Metric metric, Measure measure) {
234     measureRepository.add(component, metric, measure);
235   }
236 }