]> source.dussan.org Git - sonarqube.git/blob
05e3136e39f01164cd0f6e08f1673ef9fa4ef5cb
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2019 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.junit.rules.ExpectedException;
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.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.mockito.Mockito.mock;
45 import static org.mockito.Mockito.when;
46
47 public class ValidateProjectStepTest {
48
49   static long DEFAULT_ANALYSIS_TIME = 1433131200000L; // 2015-06-01
50   static final String PROJECT_KEY = "PROJECT_KEY";
51   static final Branch DEFAULT_BRANCH = new DefaultBranchImpl();
52
53   @Rule
54   public DbTester dbTester = DbTester.create(System2.INSTANCE);
55
56   @Rule
57   public ExpectedException thrown = ExpectedException.none();
58
59   @Rule
60   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
61
62   @Rule
63   public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule()
64     .setAnalysisDate(new Date(DEFAULT_ANALYSIS_TIME))
65     .setBranch(DEFAULT_BRANCH);
66
67   DbClient dbClient = dbTester.getDbClient();
68
69   ValidateProjectStep underTest = new ValidateProjectStep(dbClient, treeRootHolder, analysisMetadataHolder);
70
71   @Test
72   public void fail_if_slb_is_targeting_master_with_modules() {
73     ComponentDto masterProject = dbTester.components().insertMainBranch();
74     dbClient.componentDao().insert(dbTester.getSession(), ComponentTesting.newModuleDto(masterProject));
75     setBranch(BranchType.SHORT, masterProject.uuid());
76     dbTester.getSession().commit();
77
78     treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("DEFG")
79       .setKey("branch")
80       .build());
81
82     thrown.expect(MessageException.class);
83     thrown.expectMessage("Due to an upgrade, you need first to re-analyze the target branch 'master' before analyzing this short-lived branch.");
84     underTest.execute(new TestComputationStepContext());
85   }
86
87   @Test
88   public void fail_if_pr_is_targeting_branch_with_modules() {
89     ComponentDto masterProject = dbTester.components().insertMainBranch();
90     ComponentDto mergeBranch = dbTester.components().insertProjectBranch(masterProject, b -> b.setKey("mergeBranch"));
91     dbClient.componentDao().insert(dbTester.getSession(), ComponentTesting.newModuleDto(mergeBranch));
92     setBranch(BranchType.PULL_REQUEST, mergeBranch.uuid());
93     dbTester.getSession().commit();
94
95     treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("DEFG")
96       .setKey("branch")
97       .build());
98
99     thrown.expect(MessageException.class);
100     thrown.expectMessage("Due to an upgrade, you need first to re-analyze the target branch 'mergeBranch' before analyzing this pull request.");
101     underTest.execute(new TestComputationStepContext());
102   }
103
104   @Test
105   public void dont_fail_if_slb_is_targeting_branch_without_modules() {
106     ComponentDto masterProject = dbTester.components().insertMainBranch();
107     setBranch(BranchType.SHORT, masterProject.uuid());
108     dbTester.getSession().commit();
109
110     treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("DEFG")
111       .setKey("branch")
112       .build());
113
114     underTest.execute(new TestComputationStepContext());
115   }
116
117   @Test
118   public void dont_fail_for_long_forked_from_master_with_modules() {
119     ComponentDto masterProject = dbTester.components().insertMainBranch();
120     dbClient.componentDao().insert(dbTester.getSession(), ComponentTesting.newModuleDto(masterProject));
121     setBranch(BranchType.LONG, masterProject.uuid());
122     dbTester.getSession().commit();
123
124     treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("DEFG")
125       .setKey("branch")
126       .build());
127
128     underTest.execute(new TestComputationStepContext());
129   }
130
131   private void setBranch(BranchType type, @Nullable String mergeBranchUuid) {
132     Branch branch = mock(Branch.class);
133     when(branch.getType()).thenReturn(type);
134     when(branch.getMergeBranchUuid()).thenReturn(mergeBranchUuid);
135     analysisMetadataHolder.setBranch(branch);
136   }
137
138   @Test
139   public void not_fail_if_analysis_date_is_after_last_analysis() {
140     ComponentDto project = ComponentTesting.newPrivateProjectDto(dbTester.organizations().insert(), "ABCD").setDbKey(PROJECT_KEY);
141     dbClient.componentDao().insert(dbTester.getSession(), project);
142     dbClient.snapshotDao().insert(dbTester.getSession(), SnapshotTesting.newAnalysis(project).setCreatedAt(1420088400000L)); // 2015-01-01
143     dbTester.getSession().commit();
144
145     treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).build());
146
147     underTest.execute(new TestComputationStepContext());
148   }
149
150   @Test
151   public void fail_if_analysis_date_is_before_last_analysis() {
152     analysisMetadataHolder.setAnalysisDate(DateUtils.parseDate("2015-01-01"));
153
154     ComponentDto project = ComponentTesting.newPrivateProjectDto(dbTester.organizations().insert(), "ABCD").setDbKey(PROJECT_KEY);
155     dbClient.componentDao().insert(dbTester.getSession(), project);
156     dbClient.snapshotDao().insert(dbTester.getSession(), SnapshotTesting.newAnalysis(project).setCreatedAt(1433131200000L)); // 2015-06-01
157     dbTester.getSession().commit();
158
159     treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).build());
160
161     thrown.expect(MessageException.class);
162     thrown.expectMessage("Validation of project failed:");
163     thrown.expectMessage("Date of analysis cannot be older than the date of the last known analysis on this project. Value: ");
164     thrown.expectMessage("Latest analysis: ");
165
166     underTest.execute(new TestComputationStepContext());
167   }
168
169 }