]> source.dussan.org Git - sonarqube.git/blob
8e05800f5edfb439c64534eabef9ac4538552190
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2022 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.ce.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.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
29 import org.sonar.ce.task.projectanalysis.component.Component;
30 import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
31 import org.sonar.ce.task.projectanalysis.component.ViewsComponent;
32 import org.sonar.ce.task.projectanalysis.period.Period;
33 import org.sonar.ce.task.projectanalysis.period.PeriodHolderRule;
34 import org.sonar.ce.task.step.ComputationStep;
35 import org.sonar.ce.task.step.TestComputationStepContext;
36 import org.sonar.db.DbClient;
37 import org.sonar.db.DbTester;
38 import org.sonar.db.component.ComponentDto;
39 import org.sonar.db.component.ComponentTesting;
40 import org.sonar.db.component.SnapshotDto;
41 import org.sonar.db.component.SnapshotQuery;
42
43 import static org.assertj.core.api.Assertions.assertThat;
44 import static org.mockito.Mockito.mock;
45 import static org.mockito.Mockito.when;
46 import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT_VIEW;
47 import static org.sonar.ce.task.projectanalysis.component.Component.Type.SUBVIEW;
48 import static org.sonar.ce.task.projectanalysis.component.Component.Type.VIEW;
49 import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto;
50
51 public class ViewsPersistAnalysisStepTest extends BaseStepTest {
52
53   private static final String ANALYSIS_UUID = "U1";
54
55   @Rule
56   public DbTester dbTester = DbTester.create(System2.INSTANCE);
57   @Rule
58   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
59   @Rule
60   public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
61   @Rule
62   public PeriodHolderRule periodsHolder = new PeriodHolderRule();
63
64   private System2 system2 = mock(System2.class);
65   private DbClient dbClient = dbTester.getDbClient();
66   private long analysisDate;
67   private long now;
68   private PersistAnalysisStep underTest;
69
70   @Before
71   public void setup() {
72     analysisDate = DateUtils.parseDateQuietly("2015-06-01").getTime();
73     analysisMetadataHolder.setUuid(ANALYSIS_UUID);
74     analysisMetadataHolder.setAnalysisDate(analysisDate);
75
76     now = DateUtils.parseDateQuietly("2015-06-02").getTime();
77
78     when(system2.now()).thenReturn(now);
79
80     underTest = new PersistAnalysisStep(system2, dbClient, treeRootHolder, analysisMetadataHolder, periodsHolder);
81
82     // initialize PeriodHolder to empty by default
83     periodsHolder.setPeriod(null);
84   }
85
86   @Override
87   protected ComputationStep step() {
88     return underTest;
89   }
90
91   @Test
92   public void persist_analysis() {
93     ComponentDto viewDto = save(ComponentTesting.newPortfolio("UUID_VIEW").setDbKey("KEY_VIEW"));
94     save(ComponentTesting.newSubPortfolio(viewDto, "UUID_SUBVIEW", "KEY_SUBVIEW"));
95     save(newPrivateProjectDto("proj"));
96     dbTester.getSession().commit();
97
98     Component projectView = ViewsComponent.builder(PROJECT_VIEW, "KEY_PROJECT_COPY").setUuid("UUID_PROJECT_COPY").build();
99     Component subView = ViewsComponent.builder(SUBVIEW, "KEY_SUBVIEW").setUuid("UUID_SUBVIEW").addChildren(projectView).build();
100     Component view = ViewsComponent.builder(VIEW, "KEY_VIEW").setUuid("UUID_VIEW").addChildren(subView).build();
101     treeRootHolder.setRoot(view);
102
103     underTest.execute(new TestComputationStepContext());
104
105     assertThat(dbTester.countRowsOfTable("snapshots")).isOne();
106
107     SnapshotDto viewSnapshot = getUnprocessedSnapshot(viewDto.uuid());
108     assertThat(viewSnapshot.getUuid()).isEqualTo(ANALYSIS_UUID);
109     assertThat(viewSnapshot.getComponentUuid()).isEqualTo(view.getUuid());
110     assertThat(viewSnapshot.getProjectVersion()).isNull();
111     assertThat(viewSnapshot.getLast()).isFalse();
112     assertThat(viewSnapshot.getStatus()).isEqualTo("U");
113     assertThat(viewSnapshot.getCreatedAt()).isEqualTo(analysisDate);
114     assertThat(viewSnapshot.getBuildDate()).isEqualTo(now);
115   }
116
117   @Test
118   public void persist_snapshots_with_new_code_period() {
119     ComponentDto viewDto = save(ComponentTesting.newPortfolio("UUID_VIEW").setDbKey("KEY_VIEW"));
120     ComponentDto subViewDto = save(ComponentTesting.newSubPortfolio(viewDto, "UUID_SUBVIEW", "KEY_SUBVIEW"));
121     dbTester.getSession().commit();
122
123     Component subView = ViewsComponent.builder(SUBVIEW, "KEY_SUBVIEW").setUuid("UUID_SUBVIEW").build();
124     Component view = ViewsComponent.builder(VIEW, "KEY_VIEW").setUuid("UUID_VIEW").addChildren(subView).build();
125     treeRootHolder.setRoot(view);
126
127     periodsHolder.setPeriod(new Period("NUMBER_OF_DAYS", "30", analysisDate));
128
129     underTest.execute(new TestComputationStepContext());
130
131     SnapshotDto viewSnapshot = getUnprocessedSnapshot(viewDto.uuid());
132     assertThat(viewSnapshot.getPeriodMode()).isEqualTo("NUMBER_OF_DAYS");
133     assertThat(viewSnapshot.getPeriodDate()).isEqualTo(analysisDate);
134     assertThat(viewSnapshot.getPeriodModeParameter()).isNotNull();
135   }
136
137   private ComponentDto save(ComponentDto componentDto) {
138     return dbTester.components().insertComponent(componentDto);
139   }
140
141   private SnapshotDto getUnprocessedSnapshot(String componentUuid) {
142     List<SnapshotDto> projectSnapshots = dbClient.snapshotDao().selectAnalysesByQuery(dbTester.getSession(),
143       new SnapshotQuery().setComponentUuid(componentUuid).setIsLast(false).setStatus(SnapshotDto.STATUS_UNPROCESSED));
144     assertThat(projectSnapshots).hasSize(1);
145     return projectSnapshots.get(0);
146   }
147
148 }