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.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.ViewsComponent;
37 import org.sonar.server.computation.period.Period;
38 import org.sonar.server.computation.period.PeriodsHolderRule;
40 import static org.assertj.core.api.Assertions.assertThat;
41 import static org.mockito.Mockito.mock;
42 import static org.mockito.Mockito.when;
43 import static org.sonar.core.config.CorePropertyDefinitions.TIMEMACHINE_MODE_DATE;
44 import static org.sonar.db.component.ComponentTesting.newProjectCopy;
45 import static org.sonar.db.component.ComponentTesting.newProjectDto;
46 import static org.sonar.db.component.ComponentTesting.newSubView;
47 import static org.sonar.db.component.ComponentTesting.newView;
48 import static org.sonar.server.computation.component.Component.Type.PROJECT_VIEW;
49 import static org.sonar.server.computation.component.Component.Type.SUBVIEW;
50 import static org.sonar.server.computation.component.Component.Type.VIEW;
52 public class ViewsPersistAnalysisStepTest extends BaseStepTest {
54 private static final String ANALYSIS_UUID = "U1";
57 public DbTester dbTester = DbTester.create(System2.INSTANCE);
60 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
63 public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
66 public PeriodsHolderRule periodsHolder = new PeriodsHolderRule();
68 System2 system2 = mock(System2.class);
70 DbClient dbClient = dbTester.getDbClient();
76 PersistAnalysisStep underTest;
80 analysisDate = DateUtils.parseDateQuietly("2015-06-01").getTime();
81 analysisMetadataHolder.setUuid(ANALYSIS_UUID);
82 analysisMetadataHolder.setAnalysisDate(analysisDate);
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.setPeriods();
95 protected ComputationStep step() {
100 public void persist_analysis() {
101 ComponentDto viewDto = save(newView("UUID_VIEW").setKey("KEY_VIEW"));
102 ComponentDto subViewDto = save(newSubView(viewDto, "UUID_SUBVIEW", "KEY_SUBVIEW"));
103 ComponentDto projectDto = save(newProjectDto("proj"));
104 ComponentDto projectViewDto = save(newProjectCopy("UUID_PROJECT_COPY", projectDto, subViewDto).setKey("KEY_PROJECT_COPY"));
105 dbTester.getSession().commit();
107 Component projectView = ViewsComponent.builder(PROJECT_VIEW, "KEY_PROJECT_COPY").setUuid("UUID_PROJECT_COPY").build();
108 Component subView = ViewsComponent.builder(SUBVIEW, "KEY_SUBVIEW").setUuid("UUID_SUBVIEW").addChildren(projectView).build();
109 Component view = ViewsComponent.builder(VIEW, "KEY_VIEW").setUuid("UUID_VIEW").addChildren(subView).build();
110 treeRootHolder.setRoot(view);
114 assertThat(dbTester.countRowsOfTable("snapshots")).isEqualTo(1);
116 SnapshotDto viewSnapshot = getUnprocessedSnapshot(viewDto.uuid());
117 assertThat(viewSnapshot.getComponentUuid()).isEqualTo(view.getUuid());
118 assertThat(viewSnapshot.getVersion()).isNull();
119 assertThat(viewSnapshot.getLast()).isFalse();
120 assertThat(viewSnapshot.getStatus()).isEqualTo("U");
121 assertThat(viewSnapshot.getCreatedAt()).isEqualTo(analysisDate);
122 assertThat(viewSnapshot.getBuildDate()).isEqualTo(now);
126 public void persist_snapshots_with_periods() {
127 ComponentDto viewDto = save(newView("UUID_VIEW").setKey("KEY_VIEW"));
128 ComponentDto subViewDto = save(newSubView(viewDto, "UUID_SUBVIEW", "KEY_SUBVIEW"));
129 dbTester.getSession().commit();
131 Component subView = ViewsComponent.builder(SUBVIEW, "KEY_SUBVIEW").setUuid("UUID_SUBVIEW").build();
132 Component view = ViewsComponent.builder(VIEW, "KEY_VIEW").setUuid("UUID_VIEW").addChildren(subView).build();
133 treeRootHolder.setRoot(view);
135 periodsHolder.setPeriods(new Period(1, TIMEMACHINE_MODE_DATE, "2015-01-01", analysisDate, "u1"));
139 SnapshotDto viewSnapshot = getUnprocessedSnapshot(viewDto.uuid());
140 assertThat(viewSnapshot.getPeriodMode(1)).isEqualTo(TIMEMACHINE_MODE_DATE);
141 assertThat(viewSnapshot.getPeriodDate(1)).isEqualTo(analysisDate);
142 assertThat(viewSnapshot.getPeriodModeParameter(1)).isNotNull();
145 private ComponentDto save(ComponentDto componentDto) {
146 dbClient.componentDao().insert(dbTester.getSession(), componentDto);
150 private SnapshotDto getUnprocessedSnapshot(String componentUuid) {
151 List<SnapshotDto> projectSnapshots = dbClient.snapshotDao().selectAnalysesByQuery(dbTester.getSession(),
152 new SnapshotQuery().setComponentUuid(componentUuid).setIsLast(false).setStatus(SnapshotDto.STATUS_UNPROCESSED));
153 assertThat(projectSnapshots).hasSize(1);
154 return projectSnapshots.get(0);