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