]> source.dussan.org Git - sonarqube.git/blob
42a52d6d3a58e77c88f04bbc985a52bcc0e9d0b4
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2020 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   private DbClient dbClient = dbTester.getDbClient();
68   private ValidateProjectStep underTest = new ValidateProjectStep(dbClient, treeRootHolder, analysisMetadataHolder);
69
70   @Test
71   public void fail_if_pr_is_targeting_branch_with_modules() {
72     ComponentDto masterProject = dbTester.components().insertPublicProject();
73     ComponentDto mergeBranch = dbTester.components().insertProjectBranch(masterProject, b -> b.setKey("mergeBranch"));
74     dbClient.componentDao().insert(dbTester.getSession(), ComponentTesting.newModuleDto(mergeBranch));
75     setBranch(BranchType.PULL_REQUEST, mergeBranch.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 'mergeBranch' before analyzing this pull request.");
84     underTest.execute(new TestComputationStepContext());
85   }
86
87   @Test
88   public void dont_fail_for_long_forked_from_master_with_modules() {
89     ComponentDto masterProject = dbTester.components().insertPublicProject();
90     dbClient.componentDao().insert(dbTester.getSession(), ComponentTesting.newModuleDto(masterProject));
91     setBranch(BranchType.BRANCH, masterProject.uuid());
92     dbTester.getSession().commit();
93
94     treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("DEFG")
95       .setKey("branch")
96       .build());
97
98     underTest.execute(new TestComputationStepContext());
99   }
100
101   private void setBranch(BranchType type, @Nullable String mergeBranchUuid) {
102     Branch branch = mock(Branch.class);
103     when(branch.getType()).thenReturn(type);
104     when(branch.getReferenceBranchUuid()).thenReturn(mergeBranchUuid);
105     analysisMetadataHolder.setBranch(branch);
106   }
107
108   @Test
109   public void not_fail_if_analysis_date_is_after_last_analysis() {
110     ComponentDto project = ComponentTesting.newPrivateProjectDto(dbTester.organizations().insert(), "ABCD").setDbKey(PROJECT_KEY);
111     dbClient.componentDao().insert(dbTester.getSession(), project);
112     dbClient.snapshotDao().insert(dbTester.getSession(), SnapshotTesting.newAnalysis(project).setCreatedAt(1420088400000L)); // 2015-01-01
113     dbTester.getSession().commit();
114
115     treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).build());
116
117     underTest.execute(new TestComputationStepContext());
118   }
119
120   @Test
121   public void fail_if_analysis_date_is_before_last_analysis() {
122     analysisMetadataHolder.setAnalysisDate(DateUtils.parseDate("2015-01-01"));
123
124     ComponentDto project = ComponentTesting.newPrivateProjectDto(dbTester.organizations().insert(), "ABCD").setDbKey(PROJECT_KEY);
125     dbClient.componentDao().insert(dbTester.getSession(), project);
126     dbClient.snapshotDao().insert(dbTester.getSession(), SnapshotTesting.newAnalysis(project).setCreatedAt(1433131200000L)); // 2015-06-01
127     dbTester.getSession().commit();
128
129     treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).build());
130
131     thrown.expect(MessageException.class);
132     thrown.expectMessage("Validation of project failed:");
133     thrown.expectMessage("Date of analysis cannot be older than the date of the last known analysis on this project. Value: ");
134     thrown.expectMessage("Latest analysis: ");
135
136     underTest.execute(new TestComputationStepContext());
137   }
138
139 }