]> source.dussan.org Git - sonarqube.git/blob
a892fc57f9d3fe778c8f02d577573c1aca58fc18
[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 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;
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_PREVIOUS_ANALYSIS;
50
51 public class ReportPersistAnalysisStepTest extends BaseStepTest {
52
53   private static final String PROJECT_KEY = "PROJECT_KEY";
54   private static final String ANALYSIS_UUID = "U1";
55
56   @Rule
57   public DbTester dbTester = DbTester.create(System2.INSTANCE);
58   @Rule
59   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
60   @Rule
61   public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
62   @Rule
63   public PeriodHolderRule periodsHolder = new PeriodHolderRule();
64
65   private System2 system2 = mock(System2.class);
66
67   private DbIdsRepositoryImpl dbIdsRepository;
68
69   private DbClient dbClient = dbTester.getDbClient();
70
71   private long analysisDate;
72
73   private long now;
74
75   PersistAnalysisStep underTest;
76
77   @Before
78   public void setup() {
79     analysisDate = DateUtils.parseDateQuietly("2015-06-01").getTime();
80     analysisMetadataHolder.setUuid(ANALYSIS_UUID);
81     analysisMetadataHolder.setAnalysisDate(analysisDate);
82     dbIdsRepository = new DbIdsRepositoryImpl();
83
84     now = DateUtils.parseDateQuietly("2015-06-02").getTime();
85
86     when(system2.now()).thenReturn(now);
87
88     underTest = new PersistAnalysisStep(system2, dbClient, treeRootHolder, analysisMetadataHolder, periodsHolder);
89
90     // initialize PeriodHolder to empty by default
91     periodsHolder.setPeriod(null);
92   }
93
94   @Override
95   protected ComputationStep step() {
96     return underTest;
97   }
98
99   @Test
100   public void persist_analysis() {
101     analysisMetadataHolder.setIncrementalAnalysis(false);
102     OrganizationDto organizationDto = dbTester.organizations().insert();
103     ComponentDto projectDto = ComponentTesting.newPrivateProjectDto(organizationDto, "ABCD").setDbKey(PROJECT_KEY).setName("Project");
104     dbClient.componentDao().insert(dbTester.getSession(), projectDto);
105     ComponentDto moduleDto = ComponentTesting.newModuleDto("BCDE", projectDto).setDbKey("MODULE_KEY").setName("Module");
106     dbClient.componentDao().insert(dbTester.getSession(), moduleDto);
107     ComponentDto directoryDto = ComponentTesting.newDirectory(moduleDto, "CDEF", "MODULE_KEY:src/main/java/dir").setDbKey("MODULE_KEY:src/main/java/dir");
108     dbClient.componentDao().insert(dbTester.getSession(), directoryDto);
109     ComponentDto fileDto = ComponentTesting.newFileDto(moduleDto, directoryDto, "DEFG").setDbKey("MODULE_KEY:src/main/java/dir/Foo.java");
110     dbClient.componentDao().insert(dbTester.getSession(), fileDto);
111     dbTester.getSession().commit();
112
113     Component file = ReportComponent.builder(Component.Type.FILE, 4).setUuid("DEFG").setKey("MODULE_KEY:src/main/java/dir/Foo.java").build();
114     Component directory = ReportComponent.builder(Component.Type.DIRECTORY, 3).setUuid("CDEF").setKey("MODULE_KEY:src/main/java/dir").addChildren(file).build();
115     Component module = ReportComponent.builder(Component.Type.MODULE, 2).setUuid("BCDE").setKey("MODULE_KEY").setVersion("1.1").addChildren(directory).build();
116     Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).setVersion("1.0").addChildren(module).build();
117     treeRootHolder.setRoot(project);
118
119     dbIdsRepository.setComponentId(project, projectDto.getId());
120     dbIdsRepository.setComponentId(module, moduleDto.getId());
121     dbIdsRepository.setComponentId(directory, directoryDto.getId());
122     dbIdsRepository.setComponentId(file, fileDto.getId());
123
124     underTest.execute();
125
126     assertThat(dbTester.countRowsOfTable("snapshots")).isEqualTo(1);
127
128     SnapshotDto projectSnapshot = getUnprocessedSnapshot(projectDto.uuid());
129     assertThat(projectSnapshot.getUuid()).isEqualTo(ANALYSIS_UUID);
130     assertThat(projectSnapshot.getComponentUuid()).isEqualTo(project.getUuid());
131     assertThat(projectSnapshot.getVersion()).isEqualTo("1.0");
132     assertThat(projectSnapshot.getLast()).isFalse();
133     assertThat(projectSnapshot.getStatus()).isEqualTo("U");
134     assertThat(projectSnapshot.getCreatedAt()).isEqualTo(analysisDate);
135     assertThat(projectSnapshot.getBuildDate()).isEqualTo(now);
136     assertThat(projectSnapshot.getIncremental()).isFalse();
137
138     assertThat(dbIdsRepository.getComponentId(module)).isEqualTo(moduleDto.getId());
139     assertThat(dbIdsRepository.getComponentId(directory)).isEqualTo(directoryDto.getId());
140     assertThat(dbIdsRepository.getComponentId(file)).isEqualTo(fileDto.getId());
141   }
142
143   @Test
144   public void persist_snapshots_with_leak_period() {
145     analysisMetadataHolder.setIncrementalAnalysis(false);
146     OrganizationDto organizationDto = dbTester.organizations().insert();
147     ComponentDto projectDto = ComponentTesting.newPrivateProjectDto(organizationDto, "ABCD").setDbKey(PROJECT_KEY).setName("Project");
148     dbClient.componentDao().insert(dbTester.getSession(), projectDto);
149     SnapshotDto snapshotDto = SnapshotTesting.newAnalysis(projectDto).setCreatedAt(DateUtils.parseDateQuietly("2015-01-01").getTime());
150     dbClient.snapshotDao().insert(dbTester.getSession(), snapshotDto);
151     dbTester.getSession().commit();
152     periodsHolder.setPeriod(new Period(LEAK_PERIOD_MODE_DATE, "2015-01-01", analysisDate, "u1"));
153
154     Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).build();
155     treeRootHolder.setRoot(project);
156     dbIdsRepository.setComponentId(project, projectDto.getId());
157
158     underTest.execute();
159
160     SnapshotDto projectSnapshot = getUnprocessedSnapshot(projectDto.uuid());
161     assertThat(projectSnapshot.getPeriodMode()).isEqualTo(LEAK_PERIOD_MODE_DATE);
162     assertThat(projectSnapshot.getPeriodDate()).isEqualTo(analysisDate);
163     assertThat(projectSnapshot.getPeriodModeParameter()).isNotNull();
164   }
165
166   @Test
167   public void only_persist_snapshots_with_leak_period_on_project_and_module() {
168     analysisMetadataHolder.setIncrementalAnalysis(false);
169     periodsHolder.setPeriod(new Period(LEAK_PERIOD_MODE_PREVIOUS_ANALYSIS, null, analysisDate, "u1"));
170
171     OrganizationDto organizationDto = dbTester.organizations().insert();
172     ComponentDto projectDto = ComponentTesting.newPrivateProjectDto(organizationDto, "ABCD").setDbKey(PROJECT_KEY).setName("Project");
173     dbClient.componentDao().insert(dbTester.getSession(), projectDto);
174     SnapshotDto projectSnapshot = SnapshotTesting.newAnalysis(projectDto);
175     dbClient.snapshotDao().insert(dbTester.getSession(), projectSnapshot);
176
177     ComponentDto moduleDto = ComponentTesting.newModuleDto("BCDE", projectDto).setDbKey("MODULE_KEY").setName("Module");
178     dbClient.componentDao().insert(dbTester.getSession(), moduleDto);
179
180     ComponentDto directoryDto = ComponentTesting.newDirectory(moduleDto, "CDEF", "MODULE_KEY:src/main/java/dir").setDbKey("MODULE_KEY:src/main/java/dir");
181     dbClient.componentDao().insert(dbTester.getSession(), directoryDto);
182
183     ComponentDto fileDto = ComponentTesting.newFileDto(moduleDto, directoryDto, "DEFG").setDbKey("MODULE_KEY:src/main/java/dir/Foo.java");
184     dbClient.componentDao().insert(dbTester.getSession(), fileDto);
185
186     dbTester.getSession().commit();
187
188     Component file = ReportComponent.builder(Component.Type.FILE, 4).setUuid("DEFG").setKey("MODULE_KEY:src/main/java/dir/Foo.java").build();
189     Component directory = ReportComponent.builder(Component.Type.DIRECTORY, 3).setUuid("CDEF").setKey("MODULE_KEY:src/main/java/dir").addChildren(file).build();
190     Component module = ReportComponent.builder(Component.Type.MODULE, 2).setUuid("BCDE").setKey("MODULE_KEY").addChildren(directory).build();
191     Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).addChildren(module).build();
192     treeRootHolder.setRoot(project);
193
194     dbIdsRepository.setComponentId(project, projectDto.getId());
195     dbIdsRepository.setComponentId(module, moduleDto.getId());
196     dbIdsRepository.setComponentId(directory, directoryDto.getId());
197     dbIdsRepository.setComponentId(file, fileDto.getId());
198
199     underTest.execute();
200
201     SnapshotDto newProjectSnapshot = getUnprocessedSnapshot(projectDto.uuid());
202     assertThat(newProjectSnapshot.getPeriodMode()).isEqualTo(LEAK_PERIOD_MODE_PREVIOUS_ANALYSIS);
203   }
204
205   @Test
206   public void set_no_period_on_snapshots_when_no_period() {
207     analysisMetadataHolder.setIncrementalAnalysis(false);
208     ComponentDto projectDto = ComponentTesting.newPrivateProjectDto(dbTester.organizations().insert(), "ABCD").setDbKey(PROJECT_KEY).setName("Project");
209     dbClient.componentDao().insert(dbTester.getSession(), projectDto);
210     SnapshotDto snapshotDto = SnapshotTesting.newAnalysis(projectDto);
211     dbClient.snapshotDao().insert(dbTester.getSession(), snapshotDto);
212     dbTester.getSession().commit();
213
214     Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).build();
215     treeRootHolder.setRoot(project);
216     dbIdsRepository.setComponentId(project, projectDto.getId());
217
218     underTest.execute();
219
220     SnapshotDto projectSnapshot = getUnprocessedSnapshot(projectDto.uuid());
221     assertThat(projectSnapshot.getPeriodMode()).isNull();
222     assertThat(projectSnapshot.getPeriodDate()).isNull();
223     assertThat(projectSnapshot.getPeriodModeParameter()).isNull();
224   }
225
226   private SnapshotDto getUnprocessedSnapshot(String componentUuid) {
227     List<SnapshotDto> projectSnapshots = dbClient.snapshotDao().selectAnalysesByQuery(dbTester.getSession(),
228       new SnapshotQuery().setComponentUuid(componentUuid).setIsLast(false).setStatus(SnapshotDto.STATUS_UNPROCESSED));
229     assertThat(projectSnapshots).hasSize(1);
230     return projectSnapshots.get(0);
231   }
232
233 }