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.task.projectanalysis.step;
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;
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;
55 @RunWith(DataProviderRunner.class)
56 public class LoadPeriodsStepTest extends BaseStepTest {
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();
64 public DbTester dbTester = DbTester.create(System2.INSTANCE);
66 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
68 public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
70 public LogTester logTester = new LogTester();
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);
77 private LoadPeriodsStep underTest = new LoadPeriodsStep(dbClient, settingsRepository, treeRootHolder, analysisMetadataHolder, periodsHolder);
80 protected ComputationStep step() {
85 public void setUp() throws Exception {
86 analysisMetadataHolder.setAnalysisDate(DATE_FORMAT.parse("2008-11-30").getTime());
89 private void setupRoot(Component root) {
90 treeRootHolder.setRoot(root);
91 when(settingsRepository.getSettings(root)).thenReturn(settings);
95 public static Object[][] projectAndViewRoots() {
96 return new Object[][] {
103 @UseDataProvider("projectAndViewRoots")
104 public void no_period_on_first_analysis(Component root) {
107 // No project, no snapshot
110 assertThat(periodsHolder.getPeriod()).isNull();
114 @UseDataProvider("projectAndViewRoots")
115 public void feed_one_period(Component root) {
117 dbTester.prepareDbUnit(getClass(), "shared.xml");
118 String textDate = "2008-11-22";
119 settings.setProperty("sonar.timemachine.period1", textDate);
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");
132 @UseDataProvider("projectAndViewRoots")
133 public void no_period_when_settings_match_no_analysis(Component root) {
135 dbTester.prepareDbUnit(getClass(), "shared.xml");
136 settings.setProperty("sonar.timemachine.period1", "UNKNWOWN VERSION");
140 assertThat(periodsHolder.getPeriod()).isNull();
144 @UseDataProvider("projectAndViewRoots")
145 public void no_period_when_settings_is_empty(Component root) {
147 dbTester.prepareDbUnit(getClass(), "shared.xml");
148 settings.setProperty("sonar.timemachine.period1", "");
152 assertThat(periodsHolder.getPeriod()).isNull();
156 @UseDataProvider("projectAndViewRoots")
157 public void ignore_unprocessed_snapshots(Component root) {
159 dbTester.prepareDbUnit(getClass(), "unprocessed_snapshots.xml");
160 settings.setProperty("sonar.timemachine.period1", "100");
164 assertThat(periodsHolder.getPeriod()).isNull();
168 @UseDataProvider("projectAndViewRoots")
169 public void feed_period_by_date(Component root) {
171 dbTester.prepareDbUnit(getClass(), "shared.xml");
172 String textDate = "2008-11-22";
173 settings.setProperty("sonar.timemachine.period1", textDate);
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");
185 assertThat(logTester.logs()).hasSize(1);
186 assertThat(logTester.logs().get(0)).startsWith("Compare to date 2008-11-22 (analysis of ");
190 @UseDataProvider("projectAndViewRoots")
191 public void search_by_date_return_nearest_later_analysis(Component root) {
193 dbTester.prepareDbUnit(getClass(), "shared.xml");
194 String date = "2008-11-24";
195 settings.setProperty("sonar.timemachine.period1", date);
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");
209 @UseDataProvider("projectAndViewRoots")
210 public void no_period_by_date(Component root) {
212 dbTester.prepareDbUnit(getClass(), "shared.xml");
213 // No analysis at and after this date
214 settings.setProperty("sonar.timemachine.period1", "2008-11-30");
218 assertThat(periodsHolder.getPeriod()).isNull();
222 @UseDataProvider("projectAndViewRoots")
223 public void feed_period_by_days(Component root) {
225 dbTester.prepareDbUnit(getClass(), "shared.xml");
226 settings.setProperty("sonar.timemachine.period1", "10");
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");
238 assertThat(logTester.logs()).hasSize(1);
239 assertThat(logTester.logs().get(0)).startsWith("Compare over 10 days (2008-11-20, analysis of ");
243 @UseDataProvider("projectAndViewRoots")
244 public void no_period_by_days(Component root) {
246 dbTester.prepareDbUnit(getClass(), "empty.xml");
247 settings.setProperty("sonar.timemachine.period1", "0");
251 assertThat(periodsHolder.getPeriod()).isNull();
255 @UseDataProvider("projectAndViewRoots")
256 public void feed_period_by_previous_analysis(Component root) {
258 dbTester.prepareDbUnit(getClass(), "shared.xml");
259 settings.setProperty("sonar.timemachine.period1", "previous_analysis");
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");
271 assertThat(logTester.logs()).hasSize(1);
272 assertThat(logTester.logs().get(0)).startsWith("Compare to previous analysis (");
276 @UseDataProvider("projectAndViewRoots")
277 public void no_period_by_previous_analysis(Component root) {
279 dbTester.prepareDbUnit(getClass(), "empty.xml");
280 settings.setProperty("sonar.timemachine.period1", "previous_analysis");
284 assertThat(periodsHolder.getPeriod()).isNull();
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");
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");
303 assertThat(logTester.logs()).hasSize(1);
304 assertThat(logTester.logs().get(0)).startsWith("Compare to previous version (");
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");
315 assertThat(periodsHolder.getPeriod()).isNull();
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");
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");
336 @UseDataProvider("projectAndViewRoots")
337 public void no_period_by_previous_version(Component root) {
339 dbTester.prepareDbUnit(getClass(), "empty.xml");
340 settings.setProperty("sonar.timemachine.period1", "previous_version");
344 assertThat(periodsHolder.getPeriod()).isNull();
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");
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");
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");
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");
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");
387 assertThat(periodsHolder.getPeriod()).isNull();
391 @UseDataProvider("projectAndViewRoots")
392 public void feed_period_by_version(Component root) {
394 dbTester.prepareDbUnit(getClass(), "shared.xml");
395 settings.setProperty("sonar.timemachine.period1", "0.9");
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");
407 assertThat(logTester.logs()).hasSize(1);
408 assertThat(logTester.logs().get(0)).startsWith("Compare to version (0.9) (");
412 @UseDataProvider("projectAndViewRoots")
413 public void no_period_by_version(Component root) {
415 dbTester.prepareDbUnit(getClass(), "empty.xml");
416 settings.setProperty("sonar.timemachine.period1", "0.8");
420 assertThat(periodsHolder.getPeriod()).isNull();