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.step;
22 import java.util.List;
23 import org.junit.Before;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.junit.experimental.categories.Category;
27 import org.sonar.api.utils.DateUtils;
28 import org.sonar.api.utils.System2;
29 import org.sonar.db.DbClient;
30 import org.sonar.db.DbTester;
31 import org.sonar.db.component.ComponentDto;
32 import org.sonar.db.component.ComponentTesting;
33 import org.sonar.db.component.SnapshotDto;
34 import org.sonar.db.component.SnapshotQuery;
35 import org.sonar.db.component.SnapshotTesting;
36 import org.sonar.server.computation.analysis.AnalysisMetadataHolderRule;
37 import org.sonar.server.computation.batch.TreeRootHolderRule;
38 import org.sonar.server.computation.component.Component;
39 import org.sonar.server.computation.component.DbIdsRepositoryImpl;
40 import org.sonar.server.computation.component.FileAttributes;
41 import org.sonar.server.computation.component.ReportComponent;
42 import org.sonar.server.computation.period.Period;
43 import org.sonar.server.computation.period.PeriodsHolderRule;
44 import org.sonar.test.DbTests;
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_PREVIOUS_ANALYSIS;
52 @Category(DbTests.class)
53 public class ReportPersistSnapshotsStepTest extends BaseStepTest {
55 private static final String PROJECT_KEY = "PROJECT_KEY";
58 public DbTester dbTester = DbTester.create(System2.INSTANCE);
61 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
64 public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
67 public PeriodsHolderRule periodsHolder = new PeriodsHolderRule();
69 System2 system2 = mock(System2.class);
71 DbIdsRepositoryImpl dbIdsRepository;
73 DbClient dbClient = dbTester.getDbClient();
79 PersistSnapshotsStep underTest;
83 dbTester.truncateTables();
84 analysisDate = DateUtils.parseDateQuietly("2015-06-01").getTime();
85 analysisMetadataHolder.setAnalysisDate(analysisDate);
86 dbIdsRepository = new DbIdsRepositoryImpl();
88 now = DateUtils.parseDateQuietly("2015-06-02").getTime();
90 when(system2.now()).thenReturn(now);
92 underTest = new PersistSnapshotsStep(system2, dbClient, treeRootHolder, analysisMetadataHolder, dbIdsRepository, periodsHolder);
94 // initialize PeriodHolder to empty by default
95 periodsHolder.setPeriods();
99 protected ComputationStep step() {
104 public void persist_snapshots() {
105 ComponentDto projectDto = ComponentTesting.newProjectDto("ABCD").setKey(PROJECT_KEY).setName("Project");
106 dbClient.componentDao().insert(dbTester.getSession(), projectDto);
107 ComponentDto moduleDto = ComponentTesting.newModuleDto("BCDE", projectDto).setKey("MODULE_KEY").setName("Module");
108 dbClient.componentDao().insert(dbTester.getSession(), moduleDto);
109 ComponentDto directoryDto = ComponentTesting.newDirectory(moduleDto, "CDEF", "MODULE_KEY:src/main/java/dir").setKey("MODULE_KEY:src/main/java/dir");
110 dbClient.componentDao().insert(dbTester.getSession(), directoryDto);
111 ComponentDto fileDto = ComponentTesting.newFileDto(moduleDto, "DEFG").setKey("MODULE_KEY:src/main/java/dir/Foo.java");
112 dbClient.componentDao().insert(dbTester.getSession(), fileDto);
113 dbTester.getSession().commit();
115 Component file = ReportComponent.builder(Component.Type.FILE, 4).setUuid("DEFG").setKey("MODULE_KEY:src/main/java/dir/Foo.java").build();
116 Component directory = ReportComponent.builder(Component.Type.DIRECTORY, 3).setUuid("CDEF").setKey("MODULE_KEY:src/main/java/dir").addChildren(file).build();
117 Component module = ReportComponent.builder(Component.Type.MODULE, 2).setUuid("BCDE").setKey("MODULE_KEY").setVersion("1.1").addChildren(directory).build();
118 Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).setVersion("1.0").addChildren(module).build();
119 treeRootHolder.setRoot(project);
121 dbIdsRepository.setComponentId(project, projectDto.getId());
122 dbIdsRepository.setComponentId(module, moduleDto.getId());
123 dbIdsRepository.setComponentId(directory, directoryDto.getId());
124 dbIdsRepository.setComponentId(file, fileDto.getId());
128 assertThat(dbTester.countRowsOfTable("snapshots")).isEqualTo(4);
130 SnapshotDto projectSnapshot = getUnprocessedSnapshot(projectDto.getId());
131 assertThat(projectSnapshot.getComponentId()).isEqualTo(projectDto.getId());
132 assertThat(projectSnapshot.getRootProjectId()).isEqualTo(projectDto.getId());
133 assertThat(projectSnapshot.getRootId()).isNull();
134 assertThat(projectSnapshot.getParentId()).isNull();
135 assertThat(projectSnapshot.getDepth()).isEqualTo(0);
136 assertThat(projectSnapshot.getPath()).isNullOrEmpty();
137 assertThat(projectSnapshot.getQualifier()).isEqualTo("TRK");
138 assertThat(projectSnapshot.getScope()).isEqualTo("PRJ");
139 assertThat(projectSnapshot.getVersion()).isEqualTo("1.0");
140 assertThat(projectSnapshot.getLast()).isFalse();
141 assertThat(projectSnapshot.getStatus()).isEqualTo("U");
142 assertThat(projectSnapshot.getCreatedAt()).isEqualTo(analysisDate);
143 assertThat(projectSnapshot.getBuildDate()).isEqualTo(now);
145 SnapshotDto moduleSnapshot = getUnprocessedSnapshot(moduleDto.getId());
146 assertThat(moduleSnapshot.getComponentId()).isEqualTo(moduleDto.getId());
147 assertThat(moduleSnapshot.getRootProjectId()).isEqualTo(projectDto.getId());
148 assertThat(moduleSnapshot.getRootId()).isEqualTo(projectSnapshot.getId());
149 assertThat(moduleSnapshot.getParentId()).isEqualTo(projectSnapshot.getId());
150 assertThat(moduleSnapshot.getDepth()).isEqualTo(1);
151 assertThat(moduleSnapshot.getPath()).isEqualTo(projectSnapshot.getId() + ".");
152 assertThat(moduleSnapshot.getQualifier()).isEqualTo("BRC");
153 assertThat(moduleSnapshot.getScope()).isEqualTo("PRJ");
154 assertThat(moduleSnapshot.getVersion()).isEqualTo("1.1");
155 assertThat(moduleSnapshot.getLast()).isFalse();
156 assertThat(moduleSnapshot.getStatus()).isEqualTo("U");
157 assertThat(moduleSnapshot.getCreatedAt()).isEqualTo(analysisDate);
158 assertThat(moduleSnapshot.getBuildDate()).isEqualTo(now);
160 SnapshotDto directorySnapshot = getUnprocessedSnapshot(directoryDto.getId());
161 assertThat(directorySnapshot.getComponentId()).isEqualTo(directoryDto.getId());
162 assertThat(directorySnapshot.getRootProjectId()).isEqualTo(projectDto.getId());
163 assertThat(directorySnapshot.getRootId()).isEqualTo(projectSnapshot.getId());
164 assertThat(directorySnapshot.getParentId()).isEqualTo(moduleSnapshot.getId());
165 assertThat(directorySnapshot.getDepth()).isEqualTo(2);
166 assertThat(directorySnapshot.getPath()).isEqualTo(projectSnapshot.getId() + "." + moduleSnapshot.getId() + ".");
167 assertThat(directorySnapshot.getQualifier()).isEqualTo("DIR");
168 assertThat(directorySnapshot.getScope()).isEqualTo("DIR");
169 assertThat(directorySnapshot.getVersion()).isNull();
170 assertThat(directorySnapshot.getLast()).isFalse();
171 assertThat(directorySnapshot.getStatus()).isEqualTo("U");
172 assertThat(directorySnapshot.getCreatedAt()).isEqualTo(analysisDate);
173 assertThat(directorySnapshot.getBuildDate()).isEqualTo(now);
175 SnapshotDto fileSnapshot = getUnprocessedSnapshot(fileDto.getId());
176 assertThat(fileSnapshot.getComponentId()).isEqualTo(fileDto.getId());
177 assertThat(fileSnapshot.getRootProjectId()).isEqualTo(projectDto.getId());
178 assertThat(fileSnapshot.getRootId()).isEqualTo(projectSnapshot.getId());
179 assertThat(fileSnapshot.getParentId()).isEqualTo(directorySnapshot.getId());
180 assertThat(fileSnapshot.getDepth()).isEqualTo(3);
181 assertThat(fileSnapshot.getPath()).isEqualTo(projectSnapshot.getId() + "." + moduleSnapshot.getId() + "." + directorySnapshot.getId() + ".");
182 assertThat(fileSnapshot.getQualifier()).isEqualTo("FIL");
183 assertThat(fileSnapshot.getScope()).isEqualTo("FIL");
184 assertThat(fileSnapshot.getVersion()).isNull();
185 assertThat(fileSnapshot.getLast()).isFalse();
186 assertThat(fileSnapshot.getStatus()).isEqualTo("U");
187 assertThat(fileSnapshot.getCreatedAt()).isEqualTo(analysisDate);
188 assertThat(fileSnapshot.getBuildDate()).isEqualTo(now);
190 assertThat(dbIdsRepository.getSnapshotId(project)).isEqualTo(projectSnapshot.getId());
191 assertThat(dbIdsRepository.getComponentId(module)).isEqualTo(moduleDto.getId());
192 assertThat(dbIdsRepository.getComponentId(directory)).isEqualTo(directoryDto.getId());
193 assertThat(dbIdsRepository.getComponentId(file)).isEqualTo(fileDto.getId());
197 public void persist_unit_test() {
198 ComponentDto projectDto = ComponentTesting.newProjectDto("ABCD").setKey(PROJECT_KEY).setName("Project");
199 dbClient.componentDao().insert(dbTester.getSession(), projectDto);
200 ComponentDto moduleDto = ComponentTesting.newModuleDto("BCDE", projectDto).setKey("MODULE_KEY").setName("Module");
201 dbClient.componentDao().insert(dbTester.getSession(), moduleDto);
202 ComponentDto directoryDto = ComponentTesting.newDirectory(moduleDto, "CDEF", "MODULE_KEY:src/test/java/dir").setKey("MODULE_KEY:src/test/java/dir");
203 dbClient.componentDao().insert(dbTester.getSession(), directoryDto);
204 ComponentDto fileDto = ComponentTesting.newFileDto(moduleDto, "DEFG").setKey("MODULE_KEY:src/test/java/dir/FooTest.java").setQualifier("UTS");
205 dbClient.componentDao().insert(dbTester.getSession(), fileDto);
206 dbTester.getSession().commit();
208 Component file = ReportComponent.builder(Component.Type.FILE, 3).setUuid("DEFG").setKey(PROJECT_KEY + ":src/main/java/dir/Foo.java")
209 .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.newSnapshotForProject(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, 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(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, 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.newSnapshotForProject(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(TIMEMACHINE_MODE_PREVIOUS_ANALYSIS);
344 SnapshotDto newModuleSnapshot = getUnprocessedSnapshot(moduleDto.getId());
345 assertThat(newModuleSnapshot.getPeriodMode(1)).isEqualTo(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.newSnapshotForProject(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);