]> source.dussan.org Git - sonarqube.git/blob
644eb7b2d3da8cfdc88823621e5a055504e87f35
[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.task.projectanalysis.step;
21
22 import com.tngtech.java.junit.dataprovider.DataProvider;
23 import com.tngtech.java.junit.dataprovider.DataProviderRunner;
24 import com.tngtech.java.junit.dataprovider.UseDataProvider;
25 import java.text.SimpleDateFormat;
26 import org.junit.Before;
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.sonar.api.config.MapSettings;
31 import org.sonar.api.config.Settings;
32 import org.sonar.api.utils.System2;
33 import org.sonar.api.utils.log.LogTester;
34 import org.sonar.db.DbClient;
35 import org.sonar.db.DbTester;
36 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
37 import org.sonar.server.computation.task.projectanalysis.component.Component;
38 import org.sonar.server.computation.task.projectanalysis.component.ReportComponent;
39 import org.sonar.server.computation.task.projectanalysis.component.SettingsRepository;
40 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolderRule;
41 import org.sonar.server.computation.task.projectanalysis.component.ViewsComponent;
42 import org.sonar.server.computation.task.projectanalysis.period.Period;
43 import org.sonar.server.computation.task.projectanalysis.period.PeriodHolderImpl;
44 import org.sonar.server.computation.task.step.ComputationStep;
45
46 import static org.assertj.core.api.Assertions.assertThat;
47 import static org.mockito.Mockito.mock;
48 import static org.mockito.Mockito.when;
49 import static org.sonar.core.config.CorePropertyDefinitions.TIMEMACHINE_MODE_DATE;
50 import static org.sonar.core.config.CorePropertyDefinitions.TIMEMACHINE_MODE_DAYS;
51 import static org.sonar.core.config.CorePropertyDefinitions.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS;
52 import static org.sonar.core.config.CorePropertyDefinitions.TIMEMACHINE_MODE_PREVIOUS_VERSION;
53 import static org.sonar.core.config.CorePropertyDefinitions.TIMEMACHINE_MODE_VERSION;
54
55 @RunWith(DataProviderRunner.class)
56 public class LoadPeriodsStepTest extends BaseStepTest {
57
58   private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
59   private static final String ROOT_KEY = "ROOT_KEY";
60   private static final ReportComponent PROJECT_ROOT = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(ROOT_KEY).setVersion("1.1").build();
61   private static final ViewsComponent VIEW_ROOT = ViewsComponent.builder(Component.Type.VIEW, ROOT_KEY).setUuid("ABCD").build();
62
63   @Rule
64   public DbTester dbTester = DbTester.create(System2.INSTANCE);
65   @Rule
66   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
67   @Rule
68   public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
69   @Rule
70   public LogTester logTester = new LogTester();
71
72   private PeriodHolderImpl periodsHolder = new PeriodHolderImpl();
73   private DbClient dbClient = dbTester.getDbClient();
74   private Settings settings = new MapSettings();
75   private SettingsRepository settingsRepository = mock(SettingsRepository.class);
76
77   private LoadPeriodsStep underTest = new LoadPeriodsStep(dbClient, settingsRepository, treeRootHolder, analysisMetadataHolder, periodsHolder);
78
79   @Override
80   protected ComputationStep step() {
81     return underTest;
82   }
83
84   @Before
85   public void setUp() throws Exception {
86     analysisMetadataHolder.setAnalysisDate(DATE_FORMAT.parse("2008-11-30").getTime());
87   }
88
89   private void setupRoot(Component root) {
90     treeRootHolder.setRoot(root);
91     when(settingsRepository.getSettings(root)).thenReturn(settings);
92   }
93
94   @DataProvider
95   public static Object[][] projectAndViewRoots() {
96     return new Object[][] {
97       {PROJECT_ROOT},
98       {VIEW_ROOT}
99     };
100   }
101
102   @Test
103   @UseDataProvider("projectAndViewRoots")
104   public void no_period_on_first_analysis(Component root) {
105     setupRoot(root);
106
107     // No project, no snapshot
108     underTest.execute();
109
110     assertThat(periodsHolder.getPeriod()).isNull();
111   }
112
113   @Test
114   @UseDataProvider("projectAndViewRoots")
115   public void feed_one_period(Component root) {
116     setupRoot(root);
117     dbTester.prepareDbUnit(getClass(), "shared.xml");
118     String textDate = "2008-11-22";
119     settings.setProperty("sonar.timemachine.period1", textDate);
120
121     underTest.execute();
122
123     Period period = periodsHolder.getPeriod();
124     assertThat(period).isNotNull();
125     assertThat(period.getMode()).isEqualTo(TIMEMACHINE_MODE_DATE);
126     assertThat(period.getModeParameter()).isEqualTo(textDate);
127     assertThat(period.getSnapshotDate()).isEqualTo(1227358680000L);
128     assertThat(period.getAnalysisUuid()).isEqualTo("u1003");
129   }
130
131   @Test
132   @UseDataProvider("projectAndViewRoots")
133   public void no_period_when_settings_match_no_analysis(Component root) {
134     setupRoot(root);
135     dbTester.prepareDbUnit(getClass(), "shared.xml");
136     settings.setProperty("sonar.timemachine.period1", "UNKNWOWN VERSION");
137
138     underTest.execute();
139
140     assertThat(periodsHolder.getPeriod()).isNull();
141   }
142
143   @Test
144   @UseDataProvider("projectAndViewRoots")
145   public void no_period_when_settings_is_empty(Component root) {
146     setupRoot(root);
147     dbTester.prepareDbUnit(getClass(), "shared.xml");
148     settings.setProperty("sonar.timemachine.period1", "");
149
150     underTest.execute();
151
152     assertThat(periodsHolder.getPeriod()).isNull();
153   }
154
155   @Test
156   @UseDataProvider("projectAndViewRoots")
157   public void ignore_unprocessed_snapshots(Component root) {
158     setupRoot(root);
159     dbTester.prepareDbUnit(getClass(), "unprocessed_snapshots.xml");
160     settings.setProperty("sonar.timemachine.period1", "100");
161
162     underTest.execute();
163
164     assertThat(periodsHolder.getPeriod()).isNull();
165   }
166
167   @Test
168   @UseDataProvider("projectAndViewRoots")
169   public void feed_period_by_date(Component root) {
170     setupRoot(root);
171     dbTester.prepareDbUnit(getClass(), "shared.xml");
172     String textDate = "2008-11-22";
173     settings.setProperty("sonar.timemachine.period1", textDate);
174
175     underTest.execute();
176
177     // Return analysis from given date 2008-11-22
178     Period period = periodsHolder.getPeriod();
179     assertThat(period).isNotNull();
180     assertThat(period.getMode()).isEqualTo(TIMEMACHINE_MODE_DATE);
181     assertThat(period.getModeParameter()).isEqualTo(textDate);
182     assertThat(period.getSnapshotDate()).isEqualTo(1227358680000L);
183     assertThat(period.getAnalysisUuid()).isEqualTo("u1003");
184
185     assertThat(logTester.logs()).hasSize(1);
186     assertThat(logTester.logs().get(0)).startsWith("Compare to date 2008-11-22 (analysis of ");
187   }
188
189   @Test
190   @UseDataProvider("projectAndViewRoots")
191   public void search_by_date_return_nearest_later_analysis(Component root) {
192     setupRoot(root);
193     dbTester.prepareDbUnit(getClass(), "shared.xml");
194     String date = "2008-11-24";
195     settings.setProperty("sonar.timemachine.period1", date);
196
197     underTest.execute();
198
199     // Analysis form 2008-11-29
200     Period period = periodsHolder.getPeriod();
201     assertThat(period).isNotNull();
202     assertThat(period.getMode()).isEqualTo(TIMEMACHINE_MODE_DATE);
203     assertThat(period.getModeParameter()).isEqualTo(date);
204     assertThat(period.getSnapshotDate()).isEqualTo(1227934800000L);
205     assertThat(period.getAnalysisUuid()).isEqualTo("u1004");
206   }
207
208   @Test
209   @UseDataProvider("projectAndViewRoots")
210   public void no_period_by_date(Component root) {
211     setupRoot(root);
212     dbTester.prepareDbUnit(getClass(), "shared.xml");
213     // No analysis at and after this date
214     settings.setProperty("sonar.timemachine.period1", "2008-11-30");
215
216     underTest.execute();
217
218     assertThat(periodsHolder.getPeriod()).isNull();
219   }
220
221   @Test
222   @UseDataProvider("projectAndViewRoots")
223   public void feed_period_by_days(Component root) {
224     setupRoot(root);
225     dbTester.prepareDbUnit(getClass(), "shared.xml");
226     settings.setProperty("sonar.timemachine.period1", "10");
227
228     underTest.execute();
229
230     // return analysis from 2008-11-20
231     Period period = periodsHolder.getPeriod();
232     assertThat(period).isNotNull();
233     assertThat(period.getMode()).isEqualTo(TIMEMACHINE_MODE_DAYS);
234     assertThat(period.getModeParameter()).isEqualTo("10");
235     assertThat(period.getSnapshotDate()).isEqualTo(1227157200000L);
236     assertThat(period.getAnalysisUuid()).isEqualTo("u1002");
237
238     assertThat(logTester.logs()).hasSize(1);
239     assertThat(logTester.logs().get(0)).startsWith("Compare over 10 days (2008-11-20, analysis of ");
240   }
241
242   @Test
243   @UseDataProvider("projectAndViewRoots")
244   public void no_period_by_days(Component root) {
245     setupRoot(root);
246     dbTester.prepareDbUnit(getClass(), "empty.xml");
247     settings.setProperty("sonar.timemachine.period1", "0");
248
249     underTest.execute();
250
251     assertThat(periodsHolder.getPeriod()).isNull();
252   }
253
254   @Test
255   @UseDataProvider("projectAndViewRoots")
256   public void feed_period_by_previous_analysis(Component root) {
257     setupRoot(root);
258     dbTester.prepareDbUnit(getClass(), "shared.xml");
259     settings.setProperty("sonar.timemachine.period1", "previous_analysis");
260
261     underTest.execute();
262
263     // return analysis from 2008-11-29
264     Period period = periodsHolder.getPeriod();
265     assertThat(period).isNotNull();
266     assertThat(period.getMode()).isEqualTo(TIMEMACHINE_MODE_PREVIOUS_ANALYSIS);
267     assertThat(period.getModeParameter()).isNotNull();
268     assertThat(period.getSnapshotDate()).isEqualTo(1227934800000L);
269     assertThat(period.getAnalysisUuid()).isEqualTo("u1004");
270
271     assertThat(logTester.logs()).hasSize(1);
272     assertThat(logTester.logs().get(0)).startsWith("Compare to previous analysis (");
273   }
274
275   @Test
276   @UseDataProvider("projectAndViewRoots")
277   public void no_period_by_previous_analysis(Component root) {
278     setupRoot(root);
279     dbTester.prepareDbUnit(getClass(), "empty.xml");
280     settings.setProperty("sonar.timemachine.period1", "previous_analysis");
281
282     underTest.execute();
283
284     assertThat(periodsHolder.getPeriod()).isNull();
285   }
286
287   @Test
288   public void feed_period_by_previous_version() {
289     setupRoot(PROJECT_ROOT);
290     dbTester.prepareDbUnit(getClass(), "shared.xml");
291     settings.setProperty("sonar.timemachine.period1", "previous_version");
292
293     underTest.execute();
294
295     // Analysis form 2008-11-12
296     Period period = periodsHolder.getPeriod();
297     assertThat(period).isNotNull();
298     assertThat(period.getMode()).isEqualTo(TIMEMACHINE_MODE_PREVIOUS_VERSION);
299     assertThat(period.getModeParameter()).isEqualTo("1.0");
300     assertThat(period.getSnapshotDate()).isEqualTo(1226494680000L);
301     assertThat(period.getAnalysisUuid()).isEqualTo("u1001");
302
303     assertThat(logTester.logs()).hasSize(1);
304     assertThat(logTester.logs().get(0)).startsWith("Compare to previous version (");
305   }
306
307   @Test
308   public void feed_period_by_previous_version_is_not_supported_for_views() {
309     setupRoot(VIEW_ROOT);
310     dbTester.prepareDbUnit(getClass(), "shared.xml");
311     settings.setProperty("sonar.timemachine.period1", "previous_version");
312
313     underTest.execute();
314
315     assertThat(periodsHolder.getPeriod()).isNull();
316   }
317
318   @Test
319   public void feed_period_by_previous_version_with_previous_version_deleted() {
320     setupRoot(PROJECT_ROOT);
321     dbTester.prepareDbUnit(getClass(), "previous_version_deleted.xml");
322     settings.setProperty("sonar.timemachine.period1", "previous_version");
323
324     underTest.execute();
325
326     // Analysis form 2008-11-11
327     Period period = periodsHolder.getPeriod();
328     assertThat(period).isNotNull();
329     assertThat(period.getMode()).isEqualTo(TIMEMACHINE_MODE_PREVIOUS_VERSION);
330     assertThat(period.getModeParameter()).isEqualTo("0.9");
331     assertThat(period.getSnapshotDate()).isEqualTo(1226379600000L);
332     assertThat(period.getAnalysisUuid()).isEqualTo("u1000");
333   }
334
335   @Test
336   @UseDataProvider("projectAndViewRoots")
337   public void no_period_by_previous_version(Component root) {
338     setupRoot(root);
339     dbTester.prepareDbUnit(getClass(), "empty.xml");
340     settings.setProperty("sonar.timemachine.period1", "previous_version");
341
342     underTest.execute();
343
344     assertThat(periodsHolder.getPeriod()).isNull();
345   }
346
347   @Test
348   public void feed_period_by_previous_version_with_first_analysis_when_no_previous_version_found() {
349     setupRoot(PROJECT_ROOT);
350     dbTester.prepareDbUnit(getClass(), "no_previous_version.xml");
351     settings.setProperty("sonar.timemachine.period1", "previous_version");
352
353     underTest.execute();
354
355     Period period = periodsHolder.getPeriod();
356     assertThat(period).isNotNull();
357     assertThat(period.getMode()).isEqualTo(TIMEMACHINE_MODE_PREVIOUS_VERSION);
358     assertThat(period.getModeParameter()).isNull();
359     assertThat(period.getSnapshotDate()).isEqualTo(1226379600000L);
360     assertThat(period.getAnalysisUuid()).isEqualTo("u1000");
361   }
362
363   @Test
364   public void feed_period_by_previous_version_with_first_analysis_when_previous_snapshot_is_the_last_one() {
365     setupRoot(PROJECT_ROOT);
366     dbTester.prepareDbUnit(getClass(), "previous_version_is_last_one.xml");
367     settings.setProperty("sonar.timemachine.period1", "previous_version");
368
369     underTest.execute();
370
371     Period period = periodsHolder.getPeriod();
372     assertThat(period).isNotNull();
373     assertThat(period.getMode()).isEqualTo(TIMEMACHINE_MODE_PREVIOUS_VERSION);
374     assertThat(period.getModeParameter()).isNull();
375     assertThat(period.getSnapshotDate()).isEqualTo(1226379600000L);
376     assertThat(period.getAnalysisUuid()).isEqualTo("u1000");
377   }
378
379   @Test
380   public void no_period_by_previous_version_when_no_event_version_for_views() {
381     setupRoot(VIEW_ROOT);
382     dbTester.prepareDbUnit(getClass(), "no_previous_version.xml");
383     settings.setProperty("sonar.timemachine.period1", "previous_version");
384
385     underTest.execute();
386
387     assertThat(periodsHolder.getPeriod()).isNull();
388   }
389
390   @Test
391   @UseDataProvider("projectAndViewRoots")
392   public void feed_period_by_version(Component root) {
393     setupRoot(root);
394     dbTester.prepareDbUnit(getClass(), "shared.xml");
395     settings.setProperty("sonar.timemachine.period1", "0.9");
396
397     underTest.execute();
398
399     // Analysis form 2008-11-11
400     Period period = periodsHolder.getPeriod();
401     assertThat(period).isNotNull();
402     assertThat(period.getMode()).isEqualTo(TIMEMACHINE_MODE_VERSION);
403     assertThat(period.getModeParameter()).isEqualTo("0.9");
404     assertThat(period.getSnapshotDate()).isEqualTo(1226379600000L);
405     assertThat(period.getAnalysisUuid()).isEqualTo("u1000");
406
407     assertThat(logTester.logs()).hasSize(1);
408     assertThat(logTester.logs().get(0)).startsWith("Compare to version (0.9) (");
409   }
410
411   @Test
412   @UseDataProvider("projectAndViewRoots")
413   public void no_period_by_version(Component root) {
414     setupRoot(root);
415     dbTester.prepareDbUnit(getClass(), "empty.xml");
416     settings.setProperty("sonar.timemachine.period1", "0.8");
417
418     underTest.execute();
419
420     assertThat(periodsHolder.getPeriod()).isNull();
421   }
422
423 }