]> source.dussan.org Git - sonarqube.git/blob
0b812822820c0036780efebf6eaed812573c42f4
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2016 SonarSource SA
4  * mailto:contact 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.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.SnapshotDto;
32 import org.sonar.db.component.SnapshotQuery;
33 import org.sonar.server.computation.analysis.AnalysisMetadataHolderRule;
34 import org.sonar.server.computation.batch.TreeRootHolderRule;
35 import org.sonar.server.computation.component.Component;
36 import org.sonar.server.computation.component.MapBasedDbIdsRepository;
37 import org.sonar.server.computation.component.ViewsComponent;
38 import org.sonar.server.computation.period.Period;
39 import org.sonar.server.computation.period.PeriodsHolderRule;
40
41 import static java.lang.String.valueOf;
42 import static org.assertj.core.api.Assertions.assertThat;
43 import static org.mockito.Mockito.mock;
44 import static org.mockito.Mockito.when;
45 import static org.sonar.core.config.CorePropertyDefinitions.TIMEMACHINE_MODE_DATE;
46 import static org.sonar.db.component.ComponentTesting.newProjectCopy;
47 import static org.sonar.db.component.ComponentTesting.newProjectDto;
48 import static org.sonar.db.component.ComponentTesting.newSubView;
49 import static org.sonar.db.component.ComponentTesting.newView;
50 import static org.sonar.db.component.SnapshotTesting.newSnapshotForProject;
51 import static org.sonar.server.computation.component.Component.Type.PROJECT_VIEW;
52 import static org.sonar.server.computation.component.Component.Type.SUBVIEW;
53 import static org.sonar.server.computation.component.Component.Type.VIEW;
54 import static org.sonar.server.computation.component.ComponentFunctions.toKey;
55
56
57 public class ViewsPersistSnapshotsStepTest extends BaseStepTest {
58
59   private static final int PROJECT_KEY = 1;
60
61   @Rule
62   public DbTester dbTester = DbTester.create(System2.INSTANCE);
63
64   @Rule
65   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
66
67   @Rule
68   public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
69
70   @Rule
71   public PeriodsHolderRule periodsHolder = new PeriodsHolderRule();
72
73   System2 system2 = mock(System2.class);
74
75   MapBasedDbIdsRepository<String> dbIdsRepository = new MapBasedDbIdsRepository<>(toKey());
76
77   DbClient dbClient = dbTester.getDbClient();
78
79   long analysisDate;
80
81   long now;
82
83   PersistSnapshotsStep underTest;
84
85   @Before
86   public void setup() {
87     dbTester.truncateTables();
88     analysisDate = DateUtils.parseDateQuietly("2015-06-01").getTime();
89     analysisMetadataHolder.setAnalysisDate(analysisDate);
90
91     now = DateUtils.parseDateQuietly("2015-06-02").getTime();
92
93     when(system2.now()).thenReturn(now);
94
95     underTest = new PersistSnapshotsStep(system2, dbClient, treeRootHolder, analysisMetadataHolder, dbIdsRepository, periodsHolder);
96
97     // initialize PeriodHolder to empty by default
98     periodsHolder.setPeriods();
99   }
100
101   @Override
102   protected ComputationStep step() {
103     return underTest;
104   }
105
106   @Test
107   public void persist_snapshots() {
108     ComponentDto projectDto = save(newProjectDto("proj"));
109     ComponentDto viewDto = save(newView("ABCD").setKey(valueOf(PROJECT_KEY)).setName("Project"));
110     ComponentDto subViewDto = save(newSubView(viewDto, "CDEF", "key").setKey("2"));
111     ComponentDto projectViewDto = save(newProjectCopy("DEFG", projectDto, subViewDto).setKey("3"));
112     dbTester.getSession().commit();
113
114     Component projectView = ViewsComponent.builder(PROJECT_VIEW, 3).setUuid("DEFG").build();
115     Component subView = ViewsComponent.builder(SUBVIEW, 2).setUuid("CDEF").addChildren(projectView).build();
116     Component view = ViewsComponent.builder(VIEW, 1).setUuid("ABCD").addChildren(subView).build();
117     treeRootHolder.setRoot(view);
118
119     dbIdsRepository.setComponentId(view, viewDto.getId());
120     dbIdsRepository.setComponentId(subView, subViewDto.getId());
121     dbIdsRepository.setComponentId(projectView, projectViewDto.getId());
122
123     underTest.execute();
124
125     assertThat(dbTester.countRowsOfTable("snapshots")).isEqualTo(3);
126
127     SnapshotDto projectSnapshot = getUnprocessedSnapshot(viewDto.getId());
128     assertThat(projectSnapshot.getComponentId()).isEqualTo(viewDto.getId());
129     assertThat(projectSnapshot.getRootProjectId()).isEqualTo(viewDto.getId());
130     assertThat(projectSnapshot.getRootId()).isNull();
131     assertThat(projectSnapshot.getParentId()).isNull();
132     assertThat(projectSnapshot.getDepth()).isEqualTo(0);
133     assertThat(projectSnapshot.getPath()).isNullOrEmpty();
134     assertThat(projectSnapshot.getQualifier()).isEqualTo("VW");
135     assertThat(projectSnapshot.getScope()).isEqualTo("PRJ");
136     assertThat(projectSnapshot.getVersion()).isNull();
137     assertThat(projectSnapshot.getLast()).isFalse();
138     assertThat(projectSnapshot.getStatus()).isEqualTo("U");
139     assertThat(projectSnapshot.getCreatedAt()).isEqualTo(analysisDate);
140     assertThat(projectSnapshot.getBuildDate()).isEqualTo(now);
141
142     SnapshotDto subViewSnapshot = getUnprocessedSnapshot(subViewDto.getId());
143     assertThat(subViewSnapshot.getComponentId()).isEqualTo(subViewDto.getId());
144     assertThat(subViewSnapshot.getRootProjectId()).isEqualTo(viewDto.getId());
145     assertThat(subViewSnapshot.getRootId()).isEqualTo(projectSnapshot.getId());
146     assertThat(subViewSnapshot.getParentId()).isEqualTo(projectSnapshot.getId());
147     assertThat(subViewSnapshot.getDepth()).isEqualTo(1);
148     assertThat(subViewSnapshot.getPath()).isEqualTo(projectSnapshot.getId() + ".");
149     assertThat(subViewSnapshot.getQualifier()).isEqualTo("SVW");
150     assertThat(subViewSnapshot.getScope()).isEqualTo("PRJ");
151     assertThat(subViewSnapshot.getVersion()).isNull();
152     assertThat(subViewSnapshot.getLast()).isFalse();
153     assertThat(subViewSnapshot.getStatus()).isEqualTo("U");
154     assertThat(subViewSnapshot.getCreatedAt()).isEqualTo(analysisDate);
155     assertThat(subViewSnapshot.getBuildDate()).isEqualTo(now);
156
157     SnapshotDto projectViewSnapshot = getUnprocessedSnapshot(projectViewDto.getId());
158     assertThat(projectViewSnapshot.getComponentId()).isEqualTo(projectViewDto.getId());
159     assertThat(projectViewSnapshot.getRootProjectId()).isEqualTo(viewDto.getId());
160     assertThat(projectViewSnapshot.getRootId()).isEqualTo(projectSnapshot.getId());
161     assertThat(projectViewSnapshot.getParentId()).isEqualTo(subViewSnapshot.getId());
162     assertThat(projectViewSnapshot.getDepth()).isEqualTo(2);
163     assertThat(projectViewSnapshot.getPath()).isEqualTo(projectSnapshot.getId() + "." + subViewSnapshot.getId() + ".");
164     assertThat(projectViewSnapshot.getQualifier()).isEqualTo("TRK");
165     assertThat(projectViewSnapshot.getScope()).isEqualTo("FIL");
166     assertThat(projectViewSnapshot.getVersion()).isNull();
167     assertThat(projectViewSnapshot.getLast()).isFalse();
168     assertThat(projectViewSnapshot.getStatus()).isEqualTo("U");
169     assertThat(projectViewSnapshot.getCreatedAt()).isEqualTo(analysisDate);
170     assertThat(projectViewSnapshot.getBuildDate()).isEqualTo(now);
171
172     assertThat(dbIdsRepository.getSnapshotId(view)).isEqualTo(projectSnapshot.getId());
173     assertThat(dbIdsRepository.getComponentId(subView)).isEqualTo(subViewDto.getId());
174     assertThat(dbIdsRepository.getComponentId(projectView)).isEqualTo(projectViewDto.getId());
175   }
176
177   @Test
178   public void persist_snapshots_with_periods() {
179     ComponentDto viewDto = save(newView("ABCD").setKey(valueOf(PROJECT_KEY)).setName("Project"));
180     ComponentDto subViewDto = save(newSubView(viewDto, "CDEF", "key").setKey("2"));
181     SnapshotDto viewSnapshotDto = save(newSnapshotForProject(viewDto).setCreatedAt(DateUtils.parseDateQuietly("2015-01-01").getTime()));
182     SnapshotDto subViewSnapshotDto = save(newSnapshotForProject(subViewDto).setCreatedAt(DateUtils.parseDateQuietly("2015-01-01").getTime()));
183     dbTester.getSession().commit();
184
185     Component subView = ViewsComponent.builder(SUBVIEW, 2).setUuid("ABCD").build();
186     Component view = ViewsComponent.builder(VIEW, PROJECT_KEY).setUuid("ABCD").addChildren(subView).build();
187     treeRootHolder.setRoot(view);
188     dbIdsRepository.setComponentId(view, viewDto.getId());
189     dbIdsRepository.setComponentId(subView, subViewDto.getId());
190
191     periodsHolder.setPeriods(new Period(1, TIMEMACHINE_MODE_DATE, "2015-01-01", analysisDate, 123L));
192
193     underTest.execute();
194
195     SnapshotDto viewSnapshot = getUnprocessedSnapshot(viewDto.getId());
196     assertThat(viewSnapshot.getPeriodMode(1)).isEqualTo(TIMEMACHINE_MODE_DATE);
197     assertThat(viewSnapshot.getPeriodDate(1)).isEqualTo(analysisDate);
198     assertThat(viewSnapshot.getPeriodModeParameter(1)).isNotNull();
199
200     SnapshotDto subViewSnapshot = getUnprocessedSnapshot(subViewDto.getId());
201     assertThat(subViewSnapshot.getPeriodMode(1)).isEqualTo(TIMEMACHINE_MODE_DATE);
202     assertThat(subViewSnapshot.getPeriodDate(1)).isEqualTo(analysisDate);
203     assertThat(subViewSnapshot.getPeriodModeParameter(1)).isNotNull();
204   }
205
206   private ComponentDto save(ComponentDto componentDto) {
207     dbClient.componentDao().insert(dbTester.getSession(), componentDto);
208     return componentDto;
209   }
210
211   private SnapshotDto save(SnapshotDto snapshotDto) {
212     dbClient.snapshotDao().insert(dbTester.getSession(), snapshotDto);
213     return snapshotDto;
214   }
215
216   private SnapshotDto getUnprocessedSnapshot(long componentId) {
217     List<SnapshotDto> projectSnapshots = dbClient.snapshotDao().selectSnapshotsByQuery(dbTester.getSession(),
218       new SnapshotQuery().setComponentId(componentId).setIsLast(false).setStatus(SnapshotDto.STATUS_UNPROCESSED));
219     assertThat(projectSnapshots).hasSize(1);
220     return projectSnapshots.get(0);
221   }
222
223 }