]> source.dussan.org Git - sonarqube.git/blob
9a1a708bc6e8544fcb2d45da5d63fa9fd17f7dcc
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 SonarSource SA
4  * mailto:info 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.DataProviderRunner;
23 import java.text.SimpleDateFormat;
24 import org.junit.Before;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.rules.ExpectedException;
28 import org.junit.runner.RunWith;
29 import org.sonar.api.config.internal.MapSettings;
30 import org.sonar.api.utils.System2;
31 import org.sonar.api.utils.log.LogTester;
32 import org.sonar.api.utils.log.LoggerLevel;
33 import org.sonar.db.DbClient;
34 import org.sonar.db.DbTester;
35 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
36 import org.sonar.server.computation.task.projectanalysis.component.Component;
37 import org.sonar.server.computation.task.projectanalysis.component.ConfigurationRepository;
38 import org.sonar.server.computation.task.projectanalysis.component.ReportComponent;
39 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolderRule;
40 import org.sonar.server.computation.task.projectanalysis.component.ViewsComponent;
41 import org.sonar.server.computation.task.projectanalysis.period.Period;
42 import org.sonar.server.computation.task.projectanalysis.period.PeriodHolderImpl;
43 import org.sonar.server.computation.task.step.ComputationStep;
44
45 import static org.assertj.core.api.Assertions.assertThat;
46 import static org.mockito.Mockito.mock;
47 import static org.mockito.Mockito.when;
48 import static org.sonar.core.config.CorePropertyDefinitions.LEAK_PERIOD_MODE_DATE;
49 import static org.sonar.core.config.CorePropertyDefinitions.LEAK_PERIOD_MODE_DAYS;
50 import static org.sonar.core.config.CorePropertyDefinitions.LEAK_PERIOD_MODE_PREVIOUS_VERSION;
51 import static org.sonar.core.config.CorePropertyDefinitions.LEAK_PERIOD_MODE_VERSION;
52
53 @RunWith(DataProviderRunner.class)
54 public class LoadPeriodsStepTest extends BaseStepTest {
55
56   private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
57   private static final String ROOT_KEY = "ROOT_KEY";
58   private static final ReportComponent PROJECT_ROOT = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(ROOT_KEY).setVersion("1.1").build();
59   private static final ViewsComponent VIEW_ROOT = ViewsComponent.builder(Component.Type.VIEW, ROOT_KEY).setUuid("ABCD").build();
60
61   @Rule
62   public DbTester dbTester = DbTester.create(System2.INSTANCE);
63   @Rule
64   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
65   @Rule
66   public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
67   @Rule
68   public LogTester logTester = new LogTester();
69   @Rule
70   public ExpectedException expectedException = ExpectedException.none();
71
72   private PeriodHolderImpl periodsHolder = new PeriodHolderImpl();
73   private DbClient dbClient = dbTester.getDbClient();
74   private MapSettings settings = new MapSettings();
75   private ConfigurationRepository settingsRepository = mock(ConfigurationRepository.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.getConfiguration()).thenReturn(settings.asConfig());
92   }
93
94   @Test
95   public void no_period_on_first_analysis() {
96     setupRoot(PROJECT_ROOT);
97
98     // No project, no snapshot
99     underTest.execute();
100
101     assertThat(periodsHolder.getPeriod()).isNull();
102   }
103
104   @Test
105   public void feed_one_period() {
106     setupRoot(PROJECT_ROOT);
107     dbTester.prepareDbUnit(getClass(), "shared.xml");
108     String textDate = "2008-11-22";
109     settings.setProperty("sonar.leak.period", textDate);
110
111     underTest.execute();
112
113     Period period = periodsHolder.getPeriod();
114     assertThat(period).isNotNull();
115     assertThat(period.getMode()).isEqualTo(LEAK_PERIOD_MODE_DATE);
116     assertThat(period.getModeParameter()).isEqualTo(textDate);
117     assertThat(period.getSnapshotDate()).isEqualTo(1227358680000L);
118     assertThat(period.getAnalysisUuid()).isEqualTo("u1003");
119   }
120
121   @Test
122   public void no_period_when_settings_match_no_analysis() {
123     setupRoot(PROJECT_ROOT);
124     dbTester.prepareDbUnit(getClass(), "shared.xml");
125     settings.setProperty("sonar.leak.period", "UNKNWOWN VERSION");
126
127     underTest.execute();
128
129     assertThat(periodsHolder.getPeriod()).isNull();
130   }
131
132   @Test
133   public void no_period_when_settings_is_empty() {
134     setupRoot(PROJECT_ROOT);
135     dbTester.prepareDbUnit(getClass(), "shared.xml");
136     settings.setProperty("sonar.leak.period", "");
137
138     underTest.execute();
139
140     assertThat(periodsHolder.getPeriod()).isNull();
141   }
142
143   @Test
144   public void ignore_unprocessed_snapshots() {
145     setupRoot(PROJECT_ROOT);
146     dbTester.prepareDbUnit(getClass(), "unprocessed_snapshots.xml");
147     settings.setProperty("sonar.leak.period", "100");
148
149     underTest.execute();
150
151     assertThat(periodsHolder.getPeriod()).isNull();
152   }
153
154   @Test
155   public void feed_period_by_date() {
156     setupRoot(PROJECT_ROOT);
157     dbTester.prepareDbUnit(getClass(), "shared.xml");
158     String textDate = "2008-11-22";
159     settings.setProperty("sonar.leak.period", textDate);
160
161     underTest.execute();
162
163     // Return analysis from given date 2008-11-22
164     Period period = periodsHolder.getPeriod();
165     assertThat(period).isNotNull();
166     assertThat(period.getMode()).isEqualTo(LEAK_PERIOD_MODE_DATE);
167     assertThat(period.getModeParameter()).isEqualTo(textDate);
168     assertThat(period.getSnapshotDate()).isEqualTo(1227358680000L);
169     assertThat(period.getAnalysisUuid()).isEqualTo("u1003");
170
171     assertThat(logTester.logs()).hasSize(1);
172     assertThat(logTester.logs().get(0)).startsWith("Compare to date 2008-11-22 (analysis of ");
173   }
174
175   @Test
176   public void search_by_date_return_nearest_later_analysis() {
177     setupRoot(PROJECT_ROOT);
178     dbTester.prepareDbUnit(getClass(), "shared.xml");
179     String date = "2008-11-24";
180     settings.setProperty("sonar.leak.period", date);
181
182     underTest.execute();
183
184     // Analysis form 2008-11-29
185     Period period = periodsHolder.getPeriod();
186     assertThat(period).isNotNull();
187     assertThat(period.getMode()).isEqualTo(LEAK_PERIOD_MODE_DATE);
188     assertThat(period.getModeParameter()).isEqualTo(date);
189     assertThat(period.getSnapshotDate()).isEqualTo(1227934800000L);
190     assertThat(period.getAnalysisUuid()).isEqualTo("u1004");
191   }
192
193   @Test
194   public void no_period_by_date() {
195     setupRoot(PROJECT_ROOT);
196     dbTester.prepareDbUnit(getClass(), "shared.xml");
197     // No analysis at and after this date
198     settings.setProperty("sonar.leak.period", "2008-11-30");
199
200     underTest.execute();
201
202     assertThat(periodsHolder.getPeriod()).isNull();
203   }
204
205   @Test
206   public void feed_period_by_days() {
207     setupRoot(PROJECT_ROOT);
208     dbTester.prepareDbUnit(getClass(), "shared.xml");
209     settings.setProperty("sonar.leak.period", "10");
210
211     underTest.execute();
212
213     // return analysis from 2008-11-20
214     Period period = periodsHolder.getPeriod();
215     assertThat(period).isNotNull();
216     assertThat(period.getMode()).isEqualTo(LEAK_PERIOD_MODE_DAYS);
217     assertThat(period.getModeParameter()).isEqualTo("10");
218     assertThat(period.getSnapshotDate()).isEqualTo(1227157200000L);
219     assertThat(period.getAnalysisUuid()).isEqualTo("u1002");
220
221     assertThat(logTester.logs()).hasSize(1);
222     assertThat(logTester.logs().get(0)).startsWith("Compare over 10 days (2008-11-20, analysis of ");
223   }
224
225   @Test
226   public void no_period_by_days() {
227     setupRoot(PROJECT_ROOT);
228     dbTester.prepareDbUnit(getClass(), "empty.xml");
229     settings.setProperty("sonar.leak.period", "0");
230
231     underTest.execute();
232
233     assertThat(periodsHolder.getPeriod()).isNull();
234   }
235
236   @Test
237   public void feed_period_by_previous_version() {
238     setupRoot(PROJECT_ROOT);
239     dbTester.prepareDbUnit(getClass(), "shared.xml");
240     settings.setProperty("sonar.leak.period", "previous_version");
241
242     underTest.execute();
243
244     // Analysis form 2008-11-12
245     Period period = periodsHolder.getPeriod();
246     assertThat(period).isNotNull();
247     assertThat(period.getMode()).isEqualTo(LEAK_PERIOD_MODE_PREVIOUS_VERSION);
248     assertThat(period.getModeParameter()).isEqualTo("1.0");
249     assertThat(period.getSnapshotDate()).isEqualTo(1226494680000L);
250     assertThat(period.getAnalysisUuid()).isEqualTo("u1001");
251
252     assertThat(logTester.logs(LoggerLevel.DEBUG)).hasSize(1);
253     assertThat(logTester.logs(LoggerLevel.DEBUG).get(0)).startsWith("Compare to previous version (");
254   }
255
256   @Test
257   public void feed_period_by_previous_version_with_previous_version_deleted() {
258     setupRoot(PROJECT_ROOT);
259     dbTester.prepareDbUnit(getClass(), "previous_version_deleted.xml");
260     settings.setProperty("sonar.leak.period", "previous_version");
261
262     underTest.execute();
263
264     // Analysis form 2008-11-11
265     Period period = periodsHolder.getPeriod();
266     assertThat(period).isNotNull();
267     assertThat(period.getMode()).isEqualTo(LEAK_PERIOD_MODE_PREVIOUS_VERSION);
268     assertThat(period.getModeParameter()).isEqualTo("0.9");
269     assertThat(period.getSnapshotDate()).isEqualTo(1226379600000L);
270     assertThat(period.getAnalysisUuid()).isEqualTo("u1000");
271   }
272
273   @Test
274   public void no_period_by_previous_version() {
275     setupRoot(PROJECT_ROOT);
276     dbTester.prepareDbUnit(getClass(), "empty.xml");
277     settings.setProperty("sonar.leak.period", "previous_version");
278
279     underTest.execute();
280
281     assertThat(periodsHolder.getPeriod()).isNull();
282   }
283
284   @Test
285   public void feed_period_by_previous_version_with_first_analysis_when_no_previous_version_found() {
286     setupRoot(PROJECT_ROOT);
287     dbTester.prepareDbUnit(getClass(), "no_previous_version.xml");
288     settings.setProperty("sonar.leak.period", "previous_version");
289
290     underTest.execute();
291
292     Period period = periodsHolder.getPeriod();
293     assertThat(period).isNotNull();
294     assertThat(period.getMode()).isEqualTo(LEAK_PERIOD_MODE_PREVIOUS_VERSION);
295     assertThat(period.getModeParameter()).isNull();
296     assertThat(period.getSnapshotDate()).isEqualTo(1226379600000L);
297     assertThat(period.getAnalysisUuid()).isEqualTo("u1000");
298   }
299
300   @Test
301   public void feed_period_by_previous_version_with_first_analysis_when_previous_snapshot_is_the_last_one() {
302     setupRoot(PROJECT_ROOT);
303     dbTester.prepareDbUnit(getClass(), "previous_version_is_last_one.xml");
304     settings.setProperty("sonar.leak.period", "previous_version");
305
306     underTest.execute();
307
308     Period period = periodsHolder.getPeriod();
309     assertThat(period).isNotNull();
310     assertThat(period.getMode()).isEqualTo(LEAK_PERIOD_MODE_PREVIOUS_VERSION);
311     assertThat(period.getModeParameter()).isNull();
312     assertThat(period.getSnapshotDate()).isEqualTo(1226379600000L);
313     assertThat(period.getAnalysisUuid()).isEqualTo("u1000");
314   }
315
316   @Test
317   public void feed_period_by_version() {
318     setupRoot(PROJECT_ROOT);
319     dbTester.prepareDbUnit(getClass(), "shared.xml");
320     settings.setProperty("sonar.leak.period", "0.9");
321
322     underTest.execute();
323
324     // Analysis form 2008-11-11
325     Period period = periodsHolder.getPeriod();
326     assertThat(period).isNotNull();
327     assertThat(period.getMode()).isEqualTo(LEAK_PERIOD_MODE_VERSION);
328     assertThat(period.getModeParameter()).isEqualTo("0.9");
329     assertThat(period.getSnapshotDate()).isEqualTo(1226379600000L);
330     assertThat(period.getAnalysisUuid()).isEqualTo("u1000");
331
332     assertThat(logTester.logs()).hasSize(1);
333     assertThat(logTester.logs().get(0)).startsWith("Compare to version (0.9) (");
334   }
335
336   @Test
337   public void no_period_by_version() {
338     setupRoot(PROJECT_ROOT);
339     dbTester.prepareDbUnit(getClass(), "empty.xml");
340     settings.setProperty("sonar.leak.period", "0.8");
341
342     underTest.execute();
343
344     assertThat(periodsHolder.getPeriod()).isNull();
345   }
346
347 }