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.server.computation.analysis.MutableAnalysisMetadataHolderRule;
38 import org.sonar.server.computation.batch.TreeRootHolderRule;
39 import org.sonar.server.computation.component.Component;
40 import org.sonar.server.computation.component.MapBasedDbIdsRepository;
41 import org.sonar.server.computation.component.ViewsComponent;
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 java.lang.String.valueOf;
47 import static org.assertj.core.api.Assertions.assertThat;
48 import static org.mockito.Mockito.mock;
49 import static org.mockito.Mockito.when;
50 import static org.sonar.db.component.ComponentTesting.newProjectCopy;
51 import static org.sonar.db.component.ComponentTesting.newProjectDto;
52 import static org.sonar.db.component.ComponentTesting.newSubView;
53 import static org.sonar.db.component.ComponentTesting.newView;
54 import static org.sonar.server.component.SnapshotTesting.createForProject;
55 import static org.sonar.server.computation.component.Component.Type.PROJECT_VIEW;
56 import static org.sonar.server.computation.component.Component.Type.SUBVIEW;
57 import static org.sonar.server.computation.component.Component.Type.VIEW;
58 import static org.sonar.server.computation.component.ComponentFunctions.toKey;
60 @Category(DbTests.class)
61 public class ViewsPersistSnapshotsStepTest extends BaseStepTest {
63 private static final int PROJECT_KEY = 1;
66 public DbTester dbTester = DbTester.create(System2.INSTANCE);
69 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
72 public MutableAnalysisMetadataHolderRule analysisMetadataHolder = new MutableAnalysisMetadataHolderRule();
75 public PeriodsHolderRule periodsHolder = new PeriodsHolderRule();
77 System2 system2 = mock(System2.class);
79 MapBasedDbIdsRepository<String> dbIdsRepository = new MapBasedDbIdsRepository<>(toKey());
81 DbClient dbClient = dbTester.getDbClient();
87 PersistSnapshotsStep underTest;
91 dbTester.truncateTables();
92 analysisDate = DateUtils.parseDateQuietly("2015-06-01").getTime();
93 analysisMetadataHolder.setAnalysisDate(new Date(analysisDate));
95 now = DateUtils.parseDateQuietly("2015-06-02").getTime();
97 when(system2.now()).thenReturn(now);
99 underTest = new PersistSnapshotsStep(system2, dbClient, treeRootHolder, analysisMetadataHolder, dbIdsRepository, periodsHolder);
101 // initialize PeriodHolder to empty by default
102 periodsHolder.setPeriods();
106 protected ComputationStep step() {
111 public void persist_snapshots() {
112 ComponentDto projectDto = save(newProjectDto("proj"));
113 ComponentDto viewDto = save(newView("ABCD").setKey(valueOf(PROJECT_KEY)).setName("Project"));
114 ComponentDto subViewDto = save(newSubView(viewDto, "CDEF", "key").setKey("2"));
115 ComponentDto projectViewDto = save(newProjectCopy("DEFG", projectDto, subViewDto).setKey("3"));
116 dbTester.getSession().commit();
118 Component projectView = ViewsComponent.builder(PROJECT_VIEW, 3).setUuid("DEFG").build();
119 Component subView = ViewsComponent.builder(SUBVIEW, 2).setUuid("CDEF").addChildren(projectView).build();
120 Component view = ViewsComponent.builder(VIEW, 1).setUuid("ABCD").addChildren(subView).build();
121 treeRootHolder.setRoot(view);
123 dbIdsRepository.setComponentId(view, viewDto.getId());
124 dbIdsRepository.setComponentId(subView, subViewDto.getId());
125 dbIdsRepository.setComponentId(projectView, projectViewDto.getId());
129 assertThat(dbTester.countRowsOfTable("snapshots")).isEqualTo(3);
131 SnapshotDto projectSnapshot = getUnprocessedSnapshot(viewDto.getId());
132 assertThat(projectSnapshot.getComponentId()).isEqualTo(viewDto.getId());
133 assertThat(projectSnapshot.getRootProjectId()).isEqualTo(viewDto.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("VW");
139 assertThat(projectSnapshot.getScope()).isEqualTo("PRJ");
140 assertThat(projectSnapshot.getVersion()).isNull();
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 subViewSnapshot = getUnprocessedSnapshot(subViewDto.getId());
147 assertThat(subViewSnapshot.getComponentId()).isEqualTo(subViewDto.getId());
148 assertThat(subViewSnapshot.getRootProjectId()).isEqualTo(viewDto.getId());
149 assertThat(subViewSnapshot.getRootId()).isEqualTo(projectSnapshot.getId());
150 assertThat(subViewSnapshot.getParentId()).isEqualTo(projectSnapshot.getId());
151 assertThat(subViewSnapshot.getDepth()).isEqualTo(1);
152 assertThat(subViewSnapshot.getPath()).isEqualTo(projectSnapshot.getId() + ".");
153 assertThat(subViewSnapshot.getQualifier()).isEqualTo("SVW");
154 assertThat(subViewSnapshot.getScope()).isEqualTo("PRJ");
155 assertThat(subViewSnapshot.getVersion()).isNull();
156 assertThat(subViewSnapshot.getLast()).isFalse();
157 assertThat(subViewSnapshot.getStatus()).isEqualTo("U");
158 assertThat(subViewSnapshot.getCreatedAt()).isEqualTo(analysisDate);
159 assertThat(subViewSnapshot.getBuildDate()).isEqualTo(now);
161 SnapshotDto projectViewSnapshot = getUnprocessedSnapshot(projectViewDto.getId());
162 assertThat(projectViewSnapshot.getComponentId()).isEqualTo(projectViewDto.getId());
163 assertThat(projectViewSnapshot.getRootProjectId()).isEqualTo(viewDto.getId());
164 assertThat(projectViewSnapshot.getRootId()).isEqualTo(projectSnapshot.getId());
165 assertThat(projectViewSnapshot.getParentId()).isEqualTo(subViewSnapshot.getId());
166 assertThat(projectViewSnapshot.getDepth()).isEqualTo(2);
167 assertThat(projectViewSnapshot.getPath()).isEqualTo(projectSnapshot.getId() + "." + subViewSnapshot.getId() + ".");
168 assertThat(projectViewSnapshot.getQualifier()).isEqualTo("TRK");
169 assertThat(projectViewSnapshot.getScope()).isEqualTo("FIL");
170 assertThat(projectViewSnapshot.getVersion()).isNull();
171 assertThat(projectViewSnapshot.getLast()).isFalse();
172 assertThat(projectViewSnapshot.getStatus()).isEqualTo("U");
173 assertThat(projectViewSnapshot.getCreatedAt()).isEqualTo(analysisDate);
174 assertThat(projectViewSnapshot.getBuildDate()).isEqualTo(now);
176 assertThat(dbIdsRepository.getSnapshotId(view)).isEqualTo(projectSnapshot.getId());
177 assertThat(dbIdsRepository.getComponentId(subView)).isEqualTo(subViewDto.getId());
178 assertThat(dbIdsRepository.getComponentId(projectView)).isEqualTo(projectViewDto.getId());
182 public void persist_snapshots_with_periods() {
183 ComponentDto viewDto = save(newView("ABCD").setKey(valueOf(PROJECT_KEY)).setName("Project"));
184 ComponentDto subViewDto = save(newSubView(viewDto, "CDEF", "key").setKey("2"));
185 SnapshotDto viewSnapshotDto = save(createForProject(viewDto).setCreatedAt(DateUtils.parseDateQuietly("2015-01-01").getTime()));
186 SnapshotDto subViewSnapshotDto = save(createForProject(subViewDto).setCreatedAt(DateUtils.parseDateQuietly("2015-01-01").getTime()));
187 dbTester.getSession().commit();
189 Component subView = ViewsComponent.builder(SUBVIEW, 2).setUuid("ABCD").build();
190 Component view = ViewsComponent.builder(VIEW, PROJECT_KEY).setUuid("ABCD").addChildren(subView).build();
191 treeRootHolder.setRoot(view);
192 dbIdsRepository.setComponentId(view, viewDto.getId());
193 dbIdsRepository.setComponentId(subView, subViewDto.getId());
195 periodsHolder.setPeriods(new Period(1, CoreProperties.TIMEMACHINE_MODE_DATE, "2015-01-01", analysisDate, 123L));
199 SnapshotDto viewSnapshot = getUnprocessedSnapshot(viewDto.getId());
200 assertThat(viewSnapshot.getPeriodMode(1)).isEqualTo(CoreProperties.TIMEMACHINE_MODE_DATE);
201 assertThat(viewSnapshot.getPeriodDate(1)).isEqualTo(analysisDate);
202 assertThat(viewSnapshot.getPeriodModeParameter(1)).isNotNull();
204 SnapshotDto subViewSnapshot = getUnprocessedSnapshot(subViewDto.getId());
205 assertThat(subViewSnapshot.getPeriodMode(1)).isEqualTo(CoreProperties.TIMEMACHINE_MODE_DATE);
206 assertThat(subViewSnapshot.getPeriodDate(1)).isEqualTo(analysisDate);
207 assertThat(subViewSnapshot.getPeriodModeParameter(1)).isNotNull();
210 private ComponentDto save(ComponentDto componentDto) {
211 dbClient.componentDao().insert(dbTester.getSession(), componentDto);
215 private SnapshotDto save(SnapshotDto snapshotDto) {
216 dbClient.snapshotDao().insert(dbTester.getSession(), snapshotDto);
220 private SnapshotDto getUnprocessedSnapshot(long componentId) {
221 List<SnapshotDto> projectSnapshots = dbClient.snapshotDao().selectSnapshotsByQuery(dbTester.getSession(),
222 new SnapshotQuery().setComponentId(componentId).setIsLast(false).setStatus(SnapshotDto.STATUS_UNPROCESSED));
223 assertThat(projectSnapshots).hasSize(1);
224 return projectSnapshots.get(0);