]> source.dussan.org Git - sonarqube.git/blob
a1eaf07b4c2bdc027d11f7b0c90aad3299b76328
[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.Date;
23 import javax.annotation.Nullable;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.sonar.api.utils.DateUtils;
27 import org.sonar.api.utils.MessageException;
28 import org.sonar.api.utils.System2;
29 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
30 import org.sonar.ce.task.projectanalysis.analysis.Branch;
31 import org.sonar.ce.task.projectanalysis.component.Component;
32 import org.sonar.ce.task.projectanalysis.component.DefaultBranchImpl;
33 import org.sonar.ce.task.projectanalysis.component.ReportComponent;
34 import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
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.BranchType;
39 import org.sonar.db.component.ComponentDto;
40 import org.sonar.db.component.ComponentTesting;
41 import org.sonar.db.component.SnapshotTesting;
42
43 import static org.assertj.core.api.Assertions.assertThatThrownBy;
44 import static org.mockito.Mockito.mock;
45 import static org.mockito.Mockito.when;
46 import static org.sonar.db.component.BranchDto.DEFAULT_PROJECT_MAIN_BRANCH_NAME;
47
48 public class ValidateProjectStepTest {
49   static long PAST_ANALYSIS_TIME = 1_420_088_400_000L; // 2015-01-01
50   static long DEFAULT_ANALYSIS_TIME = 1_433_131_200_000L; // 2015-06-01
51
52   static final String PROJECT_KEY = "PROJECT_KEY";
53   static final Branch DEFAULT_BRANCH = new DefaultBranchImpl(DEFAULT_PROJECT_MAIN_BRANCH_NAME);
54
55   @Rule
56   public DbTester db = DbTester.create(System2.INSTANCE);
57   @Rule
58   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
59   @Rule
60   public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule()
61     .setAnalysisDate(new Date(DEFAULT_ANALYSIS_TIME))
62     .setBranch(DEFAULT_BRANCH);
63
64   private final DbClient dbClient = db.getDbClient();
65
66   private final ValidateProjectStep underTest = new ValidateProjectStep(dbClient, treeRootHolder, analysisMetadataHolder);
67
68   @Test
69   public void dont_fail_for_long_forked_from_master_with_modules() {
70     ComponentDto masterProject = db.components().insertPublicProject();
71     dbClient.componentDao().insert(db.getSession(), ComponentTesting.newModuleDto(masterProject));
72     setBranch(BranchType.BRANCH, masterProject.uuid());
73     db.getSession().commit();
74
75     treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("DEFG")
76       .setKey("branch")
77       .build());
78
79     underTest.execute(new TestComputationStepContext());
80   }
81
82   @Test
83   public void not_fail_if_analysis_date_is_after_last_analysis() {
84     ComponentDto project = db.components().insertPrivateProject("ABCD", c -> c.setKey(PROJECT_KEY));
85     dbClient.snapshotDao().insert(db.getSession(), SnapshotTesting.newAnalysis(project).setCreatedAt(PAST_ANALYSIS_TIME));
86     db.getSession().commit();
87
88     treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).build());
89
90     underTest.execute(new TestComputationStepContext());
91   }
92
93   @Test
94   public void fail_if_pr_is_targeting_branch_with_modules() {
95     ComponentDto masterProject = db.components().insertPublicProject();
96     ComponentDto mergeBranch = db.components().insertProjectBranch(masterProject, b -> b.setKey("mergeBranch"));
97     dbClient.componentDao().insert(db.getSession(), ComponentTesting.newModuleDto(mergeBranch));
98     setBranch(BranchType.PULL_REQUEST, mergeBranch.uuid());
99     db.getSession().commit();
100
101     treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("DEFG")
102       .setKey("branch")
103       .build());
104
105     var stepContext = new TestComputationStepContext();
106     assertThatThrownBy(() -> underTest.execute(stepContext))
107       .isInstanceOf(MessageException.class)
108       .hasMessage("Due to an upgrade, you need first to re-analyze the target branch 'mergeBranch' before analyzing this pull request.");
109   }
110
111   @Test
112   public void fail_if_analysis_date_is_before_last_analysis() {
113     analysisMetadataHolder.setAnalysisDate(DateUtils.parseDate("2015-01-01"));
114
115     ComponentDto project = db.components().insertPrivateProject("ABCD", c -> c.setKey(PROJECT_KEY));
116     dbClient.snapshotDao().insert(db.getSession(), SnapshotTesting.newAnalysis(project).setCreatedAt(1433131200000L)); // 2015-06-01
117     db.getSession().commit();
118
119     treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).build());
120
121     var stepContext = new TestComputationStepContext();
122     assertThatThrownBy(() -> underTest.execute(stepContext))
123       .isInstanceOf(MessageException.class)
124       .hasMessageContainingAll("Validation of project failed:",
125         "Date of analysis cannot be older than the date of the last known analysis on this project. Value: ",
126         "Latest analysis: ");
127   }
128
129   @Test
130   public void fail_when_project_key_is_invalid() {
131     ComponentDto project = db.components().insertPrivateProject(p -> p.setKey("inv$lid!"));
132     db.components().insertSnapshot(project, a -> a.setCreatedAt(PAST_ANALYSIS_TIME));
133     treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1)
134       .setUuid(project.uuid())
135       .setKey(project.getKey())
136       .build());
137
138     var stepContext = new TestComputationStepContext();
139     assertThatThrownBy(() -> underTest.execute(stepContext))
140       .isInstanceOf(MessageException.class)
141       .hasMessageContainingAll("Validation of project failed:",
142         "The project key ‘inv$lid!’ contains invalid characters.",
143         "Allowed characters are alphanumeric, '-', '_', '.' and ':', with at least one non-digit.",
144         "You should update the project key with the expected format.");
145   }
146
147   private void setBranch(BranchType type, @Nullable String mergeBranchUuid) {
148     Branch branch = mock(Branch.class);
149     when(branch.getType()).thenReturn(type);
150     when(branch.getReferenceBranchUuid()).thenReturn(mergeBranchUuid);
151     analysisMetadataHolder.setBranch(branch);
152   }
153 }