3 * Copyright (C) 2009-2017 SonarSource SA
4 * mailto:info 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 java.util.List;
23 import org.junit.Before;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.sonar.api.utils.DateUtils;
27 import org.sonar.api.utils.System2;
28 import org.sonar.db.DbClient;
29 import org.sonar.db.DbTester;
30 import org.sonar.db.component.ComponentDto;
31 import org.sonar.db.component.ComponentTesting;
32 import org.sonar.db.component.SnapshotDto;
33 import org.sonar.db.component.SnapshotQuery;
34 import org.sonar.db.component.SnapshotTesting;
35 import org.sonar.db.organization.OrganizationDto;
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.DbIdsRepositoryImpl;
39 import org.sonar.server.computation.task.projectanalysis.component.ReportComponent;
40 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolderRule;
41 import org.sonar.server.computation.task.projectanalysis.period.Period;
42 import org.sonar.server.computation.task.projectanalysis.period.PeriodHolderRule;
43 import org.sonar.server.computation.task.step.ComputationStep;
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_PREVIOUS_ANALYSIS;
51 public class ReportPersistAnalysisStepTest extends BaseStepTest {
53 private static final String PROJECT_KEY = "PROJECT_KEY";
54 private static final String ANALYSIS_UUID = "U1";
57 public DbTester dbTester = DbTester.create(System2.INSTANCE);
59 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
61 public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
63 public PeriodHolderRule periodsHolder = new PeriodHolderRule();
65 private System2 system2 = mock(System2.class);
67 private DbIdsRepositoryImpl dbIdsRepository;
69 private DbClient dbClient = dbTester.getDbClient();
71 private long analysisDate;
75 PersistAnalysisStep underTest;
79 analysisDate = DateUtils.parseDateQuietly("2015-06-01").getTime();
80 analysisMetadataHolder.setUuid(ANALYSIS_UUID);
81 analysisMetadataHolder.setAnalysisDate(analysisDate);
82 dbIdsRepository = new DbIdsRepositoryImpl();
84 now = DateUtils.parseDateQuietly("2015-06-02").getTime();
86 when(system2.now()).thenReturn(now);
88 underTest = new PersistAnalysisStep(system2, dbClient, treeRootHolder, analysisMetadataHolder, periodsHolder);
90 // initialize PeriodHolder to empty by default
91 periodsHolder.setPeriod(null);
95 protected ComputationStep step() {
100 public void persist_analysis() {
101 OrganizationDto organizationDto = dbTester.organizations().insert();
102 ComponentDto projectDto = ComponentTesting.newPrivateProjectDto(organizationDto, "ABCD").setKey(PROJECT_KEY).setName("Project");
103 dbClient.componentDao().insert(dbTester.getSession(), projectDto);
104 ComponentDto moduleDto = ComponentTesting.newModuleDto("BCDE", projectDto).setKey("MODULE_KEY").setName("Module");
105 dbClient.componentDao().insert(dbTester.getSession(), moduleDto);
106 ComponentDto directoryDto = ComponentTesting.newDirectory(moduleDto, "CDEF", "MODULE_KEY:src/main/java/dir").setKey("MODULE_KEY:src/main/java/dir");
107 dbClient.componentDao().insert(dbTester.getSession(), directoryDto);
108 ComponentDto fileDto = ComponentTesting.newFileDto(moduleDto, directoryDto, "DEFG").setKey("MODULE_KEY:src/main/java/dir/Foo.java");
109 dbClient.componentDao().insert(dbTester.getSession(), fileDto);
110 dbTester.getSession().commit();
112 Component file = ReportComponent.builder(Component.Type.FILE, 4).setUuid("DEFG").setKey("MODULE_KEY:src/main/java/dir/Foo.java").build();
113 Component directory = ReportComponent.builder(Component.Type.DIRECTORY, 3).setUuid("CDEF").setKey("MODULE_KEY:src/main/java/dir").addChildren(file).build();
114 Component module = ReportComponent.builder(Component.Type.MODULE, 2).setUuid("BCDE").setKey("MODULE_KEY").setVersion("1.1").addChildren(directory).build();
115 Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).setVersion("1.0").addChildren(module).build();
116 treeRootHolder.setRoot(project);
118 dbIdsRepository.setComponentId(project, projectDto.getId());
119 dbIdsRepository.setComponentId(module, moduleDto.getId());
120 dbIdsRepository.setComponentId(directory, directoryDto.getId());
121 dbIdsRepository.setComponentId(file, fileDto.getId());
125 assertThat(dbTester.countRowsOfTable("snapshots")).isEqualTo(1);
127 SnapshotDto projectSnapshot = getUnprocessedSnapshot(projectDto.uuid());
128 assertThat(projectSnapshot.getUuid()).isEqualTo(ANALYSIS_UUID);
129 assertThat(projectSnapshot.getComponentUuid()).isEqualTo(project.getUuid());
130 assertThat(projectSnapshot.getVersion()).isEqualTo("1.0");
131 assertThat(projectSnapshot.getLast()).isFalse();
132 assertThat(projectSnapshot.getStatus()).isEqualTo("U");
133 assertThat(projectSnapshot.getCreatedAt()).isEqualTo(analysisDate);
134 assertThat(projectSnapshot.getBuildDate()).isEqualTo(now);
136 assertThat(dbIdsRepository.getComponentId(module)).isEqualTo(moduleDto.getId());
137 assertThat(dbIdsRepository.getComponentId(directory)).isEqualTo(directoryDto.getId());
138 assertThat(dbIdsRepository.getComponentId(file)).isEqualTo(fileDto.getId());
142 public void persist_snapshots_with_leak_period() {
143 OrganizationDto organizationDto = dbTester.organizations().insert();
144 ComponentDto projectDto = ComponentTesting.newPrivateProjectDto(organizationDto, "ABCD").setKey(PROJECT_KEY).setName("Project");
145 dbClient.componentDao().insert(dbTester.getSession(), projectDto);
146 SnapshotDto snapshotDto = SnapshotTesting.newAnalysis(projectDto).setCreatedAt(DateUtils.parseDateQuietly("2015-01-01").getTime());
147 dbClient.snapshotDao().insert(dbTester.getSession(), snapshotDto);
148 dbTester.getSession().commit();
149 periodsHolder.setPeriod(new Period(LEAK_PERIOD_MODE_DATE, "2015-01-01", analysisDate, "u1"));
151 Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).build();
152 treeRootHolder.setRoot(project);
153 dbIdsRepository.setComponentId(project, projectDto.getId());
157 SnapshotDto projectSnapshot = getUnprocessedSnapshot(projectDto.uuid());
158 assertThat(projectSnapshot.getPeriodMode()).isEqualTo(LEAK_PERIOD_MODE_DATE);
159 assertThat(projectSnapshot.getPeriodDate()).isEqualTo(analysisDate);
160 assertThat(projectSnapshot.getPeriodModeParameter()).isNotNull();
164 public void only_persist_snapshots_with_leak_period_on_project_and_module() {
165 periodsHolder.setPeriod(new Period(LEAK_PERIOD_MODE_PREVIOUS_ANALYSIS, null, analysisDate, "u1"));
167 OrganizationDto organizationDto = dbTester.organizations().insert();
168 ComponentDto projectDto = ComponentTesting.newPrivateProjectDto(organizationDto, "ABCD").setKey(PROJECT_KEY).setName("Project");
169 dbClient.componentDao().insert(dbTester.getSession(), projectDto);
170 SnapshotDto projectSnapshot = SnapshotTesting.newAnalysis(projectDto);
171 dbClient.snapshotDao().insert(dbTester.getSession(), projectSnapshot);
173 ComponentDto moduleDto = ComponentTesting.newModuleDto("BCDE", projectDto).setKey("MODULE_KEY").setName("Module");
174 dbClient.componentDao().insert(dbTester.getSession(), moduleDto);
176 ComponentDto directoryDto = ComponentTesting.newDirectory(moduleDto, "CDEF", "MODULE_KEY:src/main/java/dir").setKey("MODULE_KEY:src/main/java/dir");
177 dbClient.componentDao().insert(dbTester.getSession(), directoryDto);
179 ComponentDto fileDto = ComponentTesting.newFileDto(moduleDto, directoryDto, "DEFG").setKey("MODULE_KEY:src/main/java/dir/Foo.java");
180 dbClient.componentDao().insert(dbTester.getSession(), fileDto);
182 dbTester.getSession().commit();
184 Component file = ReportComponent.builder(Component.Type.FILE, 4).setUuid("DEFG").setKey("MODULE_KEY:src/main/java/dir/Foo.java").build();
185 Component directory = ReportComponent.builder(Component.Type.DIRECTORY, 3).setUuid("CDEF").setKey("MODULE_KEY:src/main/java/dir").addChildren(file).build();
186 Component module = ReportComponent.builder(Component.Type.MODULE, 2).setUuid("BCDE").setKey("MODULE_KEY").addChildren(directory).build();
187 Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).addChildren(module).build();
188 treeRootHolder.setRoot(project);
190 dbIdsRepository.setComponentId(project, projectDto.getId());
191 dbIdsRepository.setComponentId(module, moduleDto.getId());
192 dbIdsRepository.setComponentId(directory, directoryDto.getId());
193 dbIdsRepository.setComponentId(file, fileDto.getId());
197 SnapshotDto newProjectSnapshot = getUnprocessedSnapshot(projectDto.uuid());
198 assertThat(newProjectSnapshot.getPeriodMode()).isEqualTo(LEAK_PERIOD_MODE_PREVIOUS_ANALYSIS);
202 public void set_no_period_on_snapshots_when_no_period() {
203 ComponentDto projectDto = ComponentTesting.newPrivateProjectDto(dbTester.organizations().insert(), "ABCD").setKey(PROJECT_KEY).setName("Project");
204 dbClient.componentDao().insert(dbTester.getSession(), projectDto);
205 SnapshotDto snapshotDto = SnapshotTesting.newAnalysis(projectDto);
206 dbClient.snapshotDao().insert(dbTester.getSession(), snapshotDto);
207 dbTester.getSession().commit();
209 Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).build();
210 treeRootHolder.setRoot(project);
211 dbIdsRepository.setComponentId(project, projectDto.getId());
215 SnapshotDto projectSnapshot = getUnprocessedSnapshot(projectDto.uuid());
216 assertThat(projectSnapshot.getPeriodMode()).isNull();
217 assertThat(projectSnapshot.getPeriodDate()).isNull();
218 assertThat(projectSnapshot.getPeriodModeParameter()).isNull();
221 private SnapshotDto getUnprocessedSnapshot(String componentUuid) {
222 List<SnapshotDto> projectSnapshots = dbClient.snapshotDao().selectAnalysesByQuery(dbTester.getSession(),
223 new SnapshotQuery().setComponentUuid(componentUuid).setIsLast(false).setStatus(SnapshotDto.STATUS_UNPROCESSED));
224 assertThat(projectSnapshots).hasSize(1);
225 return projectSnapshots.get(0);