2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2014 SonarSource
4 * mailto:contact AT sonarsource DOT com
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.
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.
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.
21 package org.sonar.server.computation.step;
23 import java.util.Date;
24 import java.util.List;
25 import org.junit.Before;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.junit.experimental.categories.Category;
29 import org.sonar.api.CoreProperties;
30 import org.sonar.api.utils.DateUtils;
31 import org.sonar.api.utils.System2;
32 import org.sonar.db.DbClient;
33 import org.sonar.db.DbTester;
34 import org.sonar.db.component.ComponentDto;
35 import org.sonar.db.component.SnapshotDto;
36 import org.sonar.db.component.SnapshotQuery;
37 import org.sonar.db.component.ComponentTesting;
38 import org.sonar.server.component.SnapshotTesting;
39 import org.sonar.server.computation.analysis.MutableAnalysisMetadataHolderRule;
40 import org.sonar.server.computation.batch.TreeRootHolderRule;
41 import org.sonar.server.computation.component.Component;
42 import org.sonar.server.computation.component.DbIdsRepositoryImpl;
43 import org.sonar.server.computation.component.ReportComponent;
44 import org.sonar.server.computation.component.FileAttributes;
45 import org.sonar.server.computation.period.Period;
46 import org.sonar.server.computation.period.PeriodsHolderRule;
47 import org.sonar.test.DbTests;
49 import static org.assertj.core.api.Assertions.assertThat;
50 import static org.mockito.Mockito.mock;
51 import static org.mockito.Mockito.when;
53 @Category(DbTests.class)
54 public class ReportPersistSnapshotsStepTest extends BaseStepTest {
56 private static final String PROJECT_KEY = "PROJECT_KEY";
59 public DbTester dbTester = DbTester.create(System2.INSTANCE);
62 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
65 public MutableAnalysisMetadataHolderRule analysisMetadataHolder = new MutableAnalysisMetadataHolderRule();
68 public PeriodsHolderRule periodsHolder = new PeriodsHolderRule();
70 System2 system2 = mock(System2.class);
72 DbIdsRepositoryImpl dbIdsRepository;
74 DbClient dbClient = dbTester.getDbClient();
80 PersistSnapshotsStep underTest;
84 dbTester.truncateTables();
85 analysisDate = DateUtils.parseDateQuietly("2015-06-01").getTime();
86 analysisMetadataHolder.setAnalysisDate(new Date(analysisDate));
87 dbIdsRepository = new DbIdsRepositoryImpl();
89 now = DateUtils.parseDateQuietly("2015-06-02").getTime();
91 when(system2.now()).thenReturn(now);
93 underTest = new PersistSnapshotsStep(system2, dbClient, treeRootHolder, analysisMetadataHolder, dbIdsRepository, periodsHolder);
95 // initialize PeriodHolder to empty by default
96 periodsHolder.setPeriods();
100 protected ComputationStep step() {
105 public void persist_snapshots() {
106 ComponentDto projectDto = ComponentTesting.newProjectDto("ABCD").setKey(PROJECT_KEY).setName("Project");
107 dbClient.componentDao().insert(dbTester.getSession(), projectDto);
108 ComponentDto moduleDto = ComponentTesting.newModuleDto("BCDE", projectDto).setKey("MODULE_KEY").setName("Module");
109 dbClient.componentDao().insert(dbTester.getSession(), moduleDto);
110 ComponentDto directoryDto = ComponentTesting.newDirectory(moduleDto, "CDEF", "MODULE_KEY:src/main/java/dir").setKey("MODULE_KEY:src/main/java/dir");
111 dbClient.componentDao().insert(dbTester.getSession(), directoryDto);
112 ComponentDto fileDto = ComponentTesting.newFileDto(moduleDto, "DEFG").setKey("MODULE_KEY:src/main/java/dir/Foo.java");
113 dbClient.componentDao().insert(dbTester.getSession(), fileDto);
114 dbTester.getSession().commit();
116 Component file = ReportComponent.builder(Component.Type.FILE, 4).setUuid("DEFG").setKey("MODULE_KEY:src/main/java/dir/Foo.java").build();
117 Component directory = ReportComponent.builder(Component.Type.DIRECTORY, 3).setUuid("CDEF").setKey("MODULE_KEY:src/main/java/dir").addChildren(file).build();
118 Component module = ReportComponent.builder(Component.Type.MODULE, 2).setUuid("BCDE").setKey("MODULE_KEY").setVersion("1.1").addChildren(directory).build();
119 Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).setVersion("1.0").addChildren(module).build();
120 treeRootHolder.setRoot(project);
122 dbIdsRepository.setComponentId(project, projectDto.getId());
123 dbIdsRepository.setComponentId(module, moduleDto.getId());
124 dbIdsRepository.setComponentId(directory, directoryDto.getId());
125 dbIdsRepository.setComponentId(file, fileDto.getId());
129 assertThat(dbTester.countRowsOfTable("snapshots")).isEqualTo(4);
131 SnapshotDto projectSnapshot = getUnprocessedSnapshot(projectDto.getId());
132 assertThat(projectSnapshot.getComponentId()).isEqualTo(projectDto.getId());
133 assertThat(projectSnapshot.getRootProjectId()).isEqualTo(projectDto.getId());
134 assertThat(projectSnapshot.getRootId()).isNull();
135 assertThat(projectSnapshot.getParentId()).isNull();
136 assertThat(projectSnapshot.getDepth()).isEqualTo(0);
137 assertThat(projectSnapshot.getPath()).isNullOrEmpty();
138 assertThat(projectSnapshot.getQualifier()).isEqualTo("TRK");
139 assertThat(projectSnapshot.getScope()).isEqualTo("PRJ");
140 assertThat(projectSnapshot.getVersion()).isEqualTo("1.0");
141 assertThat(projectSnapshot.getLast()).isFalse();
142 assertThat(projectSnapshot.getStatus()).isEqualTo("U");
143 assertThat(projectSnapshot.getCreatedAt()).isEqualTo(analysisDate);
144 assertThat(projectSnapshot.getBuildDate()).isEqualTo(now);
146 SnapshotDto moduleSnapshot = getUnprocessedSnapshot(moduleDto.getId());
147 assertThat(moduleSnapshot.getComponentId()).isEqualTo(moduleDto.getId());
148 assertThat(moduleSnapshot.getRootProjectId()).isEqualTo(projectDto.getId());
149 assertThat(moduleSnapshot.getRootId()).isEqualTo(projectSnapshot.getId());
150 assertThat(moduleSnapshot.getParentId()).isEqualTo(projectSnapshot.getId());
151 assertThat(moduleSnapshot.getDepth()).isEqualTo(1);
152 assertThat(moduleSnapshot.getPath()).isEqualTo(projectSnapshot.getId() + ".");
153 assertThat(moduleSnapshot.getQualifier()).isEqualTo("BRC");
154 assertThat(moduleSnapshot.getScope()).isEqualTo("PRJ");
155 assertThat(moduleSnapshot.getVersion()).isEqualTo("1.1");
156 assertThat(moduleSnapshot.getLast()).isFalse();
157 assertThat(moduleSnapshot.getStatus()).isEqualTo("U");
158 assertThat(moduleSnapshot.getCreatedAt()).isEqualTo(analysisDate);
159 assertThat(moduleSnapshot.getBuildDate()).isEqualTo(now);
161 SnapshotDto directorySnapshot = getUnprocessedSnapshot(directoryDto.getId());
162 assertThat(directorySnapshot.getComponentId()).isEqualTo(directoryDto.getId());
163 assertThat(directorySnapshot.getRootProjectId()).isEqualTo(projectDto.getId());
164 assertThat(directorySnapshot.getRootId()).isEqualTo(projectSnapshot.getId());
165 assertThat(directorySnapshot.getParentId()).isEqualTo(moduleSnapshot.getId());
166 assertThat(directorySnapshot.getDepth()).isEqualTo(2);
167 assertThat(directorySnapshot.getPath()).isEqualTo(projectSnapshot.getId() + "." + moduleSnapshot.getId() + ".");
168 assertThat(directorySnapshot.getQualifier()).isEqualTo("DIR");
169 assertThat(directorySnapshot.getScope()).isEqualTo("DIR");
170 assertThat(directorySnapshot.getVersion()).isNull();
171 assertThat(directorySnapshot.getLast()).isFalse();
172 assertThat(directorySnapshot.getStatus()).isEqualTo("U");
173 assertThat(directorySnapshot.getCreatedAt()).isEqualTo(analysisDate);
174 assertThat(directorySnapshot.getBuildDate()).isEqualTo(now);
176 SnapshotDto fileSnapshot = getUnprocessedSnapshot(fileDto.getId());
177 assertThat(fileSnapshot.getComponentId()).isEqualTo(fileDto.getId());
178 assertThat(fileSnapshot.getRootProjectId()).isEqualTo(projectDto.getId());
179 assertThat(fileSnapshot.getRootId()).isEqualTo(projectSnapshot.getId());
180 assertThat(fileSnapshot.getParentId()).isEqualTo(directorySnapshot.getId());
181 assertThat(fileSnapshot.getDepth()).isEqualTo(3);
182 assertThat(fileSnapshot.getPath()).isEqualTo(projectSnapshot.getId() + "." + moduleSnapshot.getId() + "." + directorySnapshot.getId() + ".");
183 assertThat(fileSnapshot.getQualifier()).isEqualTo("FIL");
184 assertThat(fileSnapshot.getScope()).isEqualTo("FIL");
185 assertThat(fileSnapshot.getVersion()).isNull();
186 assertThat(fileSnapshot.getLast()).isFalse();
187 assertThat(fileSnapshot.getStatus()).isEqualTo("U");
188 assertThat(fileSnapshot.getCreatedAt()).isEqualTo(analysisDate);
189 assertThat(fileSnapshot.getBuildDate()).isEqualTo(now);
191 assertThat(dbIdsRepository.getSnapshotId(project)).isEqualTo(projectSnapshot.getId());
192 assertThat(dbIdsRepository.getComponentId(module)).isEqualTo(moduleDto.getId());
193 assertThat(dbIdsRepository.getComponentId(directory)).isEqualTo(directoryDto.getId());
194 assertThat(dbIdsRepository.getComponentId(file)).isEqualTo(fileDto.getId());
198 public void persist_unit_test() {
199 ComponentDto projectDto = ComponentTesting.newProjectDto("ABCD").setKey(PROJECT_KEY).setName("Project");
200 dbClient.componentDao().insert(dbTester.getSession(), projectDto);
201 ComponentDto moduleDto = ComponentTesting.newModuleDto("BCDE", projectDto).setKey("MODULE_KEY").setName("Module");
202 dbClient.componentDao().insert(dbTester.getSession(), moduleDto);
203 ComponentDto directoryDto = ComponentTesting.newDirectory(moduleDto, "CDEF", "MODULE_KEY:src/test/java/dir").setKey("MODULE_KEY:src/test/java/dir");
204 dbClient.componentDao().insert(dbTester.getSession(), directoryDto);
205 ComponentDto fileDto = ComponentTesting.newFileDto(moduleDto, "DEFG").setKey("MODULE_KEY:src/test/java/dir/FooTest.java").setQualifier("UTS");
206 dbClient.componentDao().insert(dbTester.getSession(), fileDto);
207 dbTester.getSession().commit();
209 Component file = ReportComponent.builder(Component.Type.FILE, 3).setUuid("DEFG").setKey(PROJECT_KEY + ":src/main/java/dir/Foo.java").setFileAttributes(new FileAttributes(true, null)).build();
210 Component directory = ReportComponent.builder(Component.Type.DIRECTORY, 2).setUuid("CDEF").setKey(PROJECT_KEY + ":src/main/java/dir").addChildren(file).build();
211 Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).addChildren(directory).build();
212 treeRootHolder.setRoot(project);
214 dbIdsRepository.setComponentId(project, projectDto.getId());
215 dbIdsRepository.setComponentId(directory, directoryDto.getId());
216 dbIdsRepository.setComponentId(file, fileDto.getId());
220 SnapshotDto fileSnapshot = getUnprocessedSnapshot(fileDto.getId());
221 assertThat(fileSnapshot.getQualifier()).isEqualTo("UTS");
222 assertThat(fileSnapshot.getScope()).isEqualTo("FIL");
226 public void persist_snapshots_on_multi_modules() {
227 ComponentDto projectDto = ComponentTesting.newProjectDto("ABCD").setKey(PROJECT_KEY);
228 dbClient.componentDao().insert(dbTester.getSession(), projectDto);
229 ComponentDto moduleADto = ComponentTesting.newModuleDto("BCDE", projectDto).setKey("MODULE_A");
230 dbClient.componentDao().insert(dbTester.getSession(), moduleADto);
231 ComponentDto subModuleADto = ComponentTesting.newModuleDto("CDEF", moduleADto).setKey("SUB_MODULE_A");
232 dbClient.componentDao().insert(dbTester.getSession(), subModuleADto);
233 ComponentDto moduleBDto = ComponentTesting.newModuleDto("DEFG", projectDto).setKey("MODULE_B");
234 dbClient.componentDao().insert(dbTester.getSession(), moduleBDto);
235 dbTester.getSession().commit();
237 Component moduleB = ReportComponent.builder(Component.Type.MODULE, 4).setUuid("DEFG").setKey("MODULE_B").build();
238 Component subModuleA = ReportComponent.builder(Component.Type.MODULE, 3).setUuid("CDEF").setKey("SUB_MODULE_A").build();
239 Component moduleA = ReportComponent.builder(Component.Type.MODULE, 2).setUuid("BCDE").setKey("MODULE_A").addChildren(subModuleA).build();
240 Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).addChildren(moduleA, moduleB).build();
241 treeRootHolder.setRoot(project);
243 dbIdsRepository.setComponentId(project, projectDto.getId());
244 dbIdsRepository.setComponentId(moduleA, moduleADto.getId());
245 dbIdsRepository.setComponentId(subModuleA, subModuleADto.getId());
246 dbIdsRepository.setComponentId(moduleB, moduleBDto.getId());
250 assertThat(dbTester.countRowsOfTable("snapshots")).isEqualTo(4);
252 SnapshotDto projectSnapshot = getUnprocessedSnapshot(projectDto.getId());
253 assertThat(projectSnapshot.getRootProjectId()).isEqualTo(projectDto.getId());
254 assertThat(projectSnapshot.getRootId()).isNull();
255 assertThat(projectSnapshot.getParentId()).isNull();
256 assertThat(projectSnapshot.getDepth()).isEqualTo(0);
257 assertThat(projectSnapshot.getPath()).isNullOrEmpty();
259 SnapshotDto moduleASnapshot = getUnprocessedSnapshot(moduleADto.getId());
260 assertThat(moduleASnapshot.getRootProjectId()).isEqualTo(projectDto.getId());
261 assertThat(moduleASnapshot.getRootId()).isEqualTo(projectSnapshot.getId());
262 assertThat(moduleASnapshot.getParentId()).isEqualTo(projectSnapshot.getId());
263 assertThat(moduleASnapshot.getDepth()).isEqualTo(1);
264 assertThat(moduleASnapshot.getPath()).isEqualTo(projectSnapshot.getId() + ".");
266 SnapshotDto subModuleASnapshot = getUnprocessedSnapshot(subModuleADto.getId());
267 assertThat(subModuleASnapshot.getRootProjectId()).isEqualTo(projectDto.getId());
268 assertThat(subModuleASnapshot.getRootId()).isEqualTo(projectSnapshot.getId());
269 assertThat(subModuleASnapshot.getParentId()).isEqualTo(moduleASnapshot.getId());
270 assertThat(subModuleASnapshot.getDepth()).isEqualTo(2);
271 assertThat(subModuleASnapshot.getPath()).isEqualTo(projectSnapshot.getId() + "." + moduleASnapshot.getId() + ".");
273 SnapshotDto moduleBSnapshot = getUnprocessedSnapshot(moduleBDto.getId());
274 assertThat(moduleBSnapshot.getRootProjectId()).isEqualTo(projectDto.getId());
275 assertThat(moduleBSnapshot.getRootId()).isEqualTo(projectSnapshot.getId());
276 assertThat(moduleBSnapshot.getParentId()).isEqualTo(projectSnapshot.getId());
277 assertThat(moduleBSnapshot.getDepth()).isEqualTo(1);
278 assertThat(moduleBSnapshot.getPath()).isEqualTo(projectSnapshot.getId() + ".");
282 public void persist_snapshots_with_periods() {
283 ComponentDto projectDto = ComponentTesting.newProjectDto("ABCD").setKey(PROJECT_KEY).setName("Project");
284 dbClient.componentDao().insert(dbTester.getSession(), projectDto);
285 SnapshotDto snapshotDto = SnapshotTesting.createForProject(projectDto).setCreatedAt(DateUtils.parseDateQuietly("2015-01-01").getTime());
286 dbClient.snapshotDao().insert(dbTester.getSession(), snapshotDto);
287 dbTester.getSession().commit();
288 periodsHolder.setPeriods(new Period(1, CoreProperties.TIMEMACHINE_MODE_DATE, "2015-01-01", analysisDate, 123L));
290 Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).build();
291 treeRootHolder.setRoot(project);
292 dbIdsRepository.setComponentId(project, projectDto.getId());
296 SnapshotDto projectSnapshot = getUnprocessedSnapshot(projectDto.getId());
297 assertThat(projectSnapshot.getPeriodMode(1)).isEqualTo(CoreProperties.TIMEMACHINE_MODE_DATE);
298 assertThat(projectSnapshot.getPeriodDate(1)).isEqualTo(analysisDate);
299 assertThat(projectSnapshot.getPeriodModeParameter(1)).isNotNull();
303 public void only_persist_snapshots_with_periods_on_project_and_module() {
304 periodsHolder.setPeriods(new Period(1, CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS, null, analysisDate, 123L));
306 ComponentDto projectDto = ComponentTesting.newProjectDto("ABCD").setKey(PROJECT_KEY).setName("Project");
307 dbClient.componentDao().insert(dbTester.getSession(), projectDto);
308 SnapshotDto projectSnapshot = SnapshotTesting.createForProject(projectDto);
309 dbClient.snapshotDao().insert(dbTester.getSession(), projectSnapshot);
311 ComponentDto moduleDto = ComponentTesting.newModuleDto("BCDE", projectDto).setKey("MODULE_KEY").setName("Module");
312 dbClient.componentDao().insert(dbTester.getSession(), moduleDto);
313 SnapshotDto moduleSnapshot = SnapshotTesting.createForComponent(moduleDto, projectSnapshot);
314 dbClient.snapshotDao().insert(dbTester.getSession(), moduleSnapshot);
316 ComponentDto directoryDto = ComponentTesting.newDirectory(moduleDto, "CDEF", "MODULE_KEY:src/main/java/dir").setKey("MODULE_KEY:src/main/java/dir");
317 dbClient.componentDao().insert(dbTester.getSession(), directoryDto);
318 SnapshotDto directorySnapshot = SnapshotTesting.createForComponent(directoryDto, moduleSnapshot);
319 dbClient.snapshotDao().insert(dbTester.getSession(), directorySnapshot);
321 ComponentDto fileDto = ComponentTesting.newFileDto(moduleDto, "DEFG").setKey("MODULE_KEY:src/main/java/dir/Foo.java");
322 dbClient.componentDao().insert(dbTester.getSession(), fileDto);
323 SnapshotDto fileSnapshot = SnapshotTesting.createForComponent(fileDto, directorySnapshot);
324 dbClient.snapshotDao().insert(dbTester.getSession(), fileSnapshot);
326 dbTester.getSession().commit();
328 Component file = ReportComponent.builder(Component.Type.FILE, 4).setUuid("DEFG").setKey("MODULE_KEY:src/main/java/dir/Foo.java").build();
329 Component directory = ReportComponent.builder(Component.Type.DIRECTORY, 3).setUuid("CDEF").setKey("MODULE_KEY:src/main/java/dir").addChildren(file).build();
330 Component module = ReportComponent.builder(Component.Type.MODULE, 2).setUuid("BCDE").setKey("MODULE_KEY").addChildren(directory).build();
331 Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).addChildren(module).build();
332 treeRootHolder.setRoot(project);
334 dbIdsRepository.setComponentId(project, projectDto.getId());
335 dbIdsRepository.setComponentId(module, moduleDto.getId());
336 dbIdsRepository.setComponentId(directory, directoryDto.getId());
337 dbIdsRepository.setComponentId(file, fileDto.getId());
341 SnapshotDto newProjectSnapshot = getUnprocessedSnapshot(projectDto.getId());
342 assertThat(newProjectSnapshot.getPeriodMode(1)).isEqualTo(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS);
344 SnapshotDto newModuleSnapshot = getUnprocessedSnapshot(moduleDto.getId());
345 assertThat(newModuleSnapshot.getPeriodMode(1)).isEqualTo(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS);
347 SnapshotDto newDirectorySnapshot = getUnprocessedSnapshot(directoryDto.getId());
348 assertThat(newDirectorySnapshot.getPeriodMode(1)).isNull();
350 SnapshotDto newFileSnapshot = getUnprocessedSnapshot(fileDto.getId());
351 assertThat(newFileSnapshot.getPeriodMode(1)).isNull();
355 public void set_no_period_on_snapshots_when_no_period() {
356 ComponentDto projectDto = ComponentTesting.newProjectDto("ABCD").setKey(PROJECT_KEY).setName("Project");
357 dbClient.componentDao().insert(dbTester.getSession(), projectDto);
358 SnapshotDto snapshotDto = SnapshotTesting.createForProject(projectDto);
359 dbClient.snapshotDao().insert(dbTester.getSession(), snapshotDto);
360 dbTester.getSession().commit();
362 Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).build();
363 treeRootHolder.setRoot(project);
364 dbIdsRepository.setComponentId(project, projectDto.getId());
368 SnapshotDto projectSnapshot = getUnprocessedSnapshot(projectDto.getId());
369 assertThat(projectSnapshot.getPeriodMode(1)).isNull();
370 assertThat(projectSnapshot.getPeriodDate(1)).isNull();
371 assertThat(projectSnapshot.getPeriodModeParameter(1)).isNull();
374 private SnapshotDto getUnprocessedSnapshot(long componentId) {
375 List<SnapshotDto> projectSnapshots = dbClient.snapshotDao().selectSnapshotsByQuery(dbTester.getSession(),
376 new SnapshotQuery().setComponentId(componentId).setIsLast(false).setStatus(SnapshotDto.STATUS_UNPROCESSED));
377 assertThat(projectSnapshots).hasSize(1);
378 return projectSnapshots.get(0);