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