]> source.dussan.org Git - sonarqube.git/blob
c1bab1e087507b54006ead82919905bda191aeda
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2016 SonarSource SA
4  * mailto:contact 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.server.computation.step;
21
22 import org.junit.Rule;
23 import org.junit.Test;
24 import org.junit.rules.ExpectedException;
25 import org.sonar.api.utils.MessageException;
26 import org.sonar.scanner.protocol.output.ScannerReport;
27 import org.sonar.server.computation.analysis.MutableAnalysisMetadataHolderRule;
28 import org.sonar.server.computation.batch.BatchReportReaderRule;
29 import org.sonar.server.computation.queue.CeTask;
30
31 import static org.assertj.core.api.Assertions.assertThat;
32 import static org.mockito.Mockito.mock;
33 import static org.mockito.Mockito.when;
34
35 public class LoadReportAnalysisMetadataHolderStepTest {
36
37   public static final String PROJECT_KEY = "project_key";
38   static long ANALYSIS_DATE = 123456789L;
39
40   static String BRANCH = "origin/master";
41
42   @Rule
43   public BatchReportReaderRule reportReader = new BatchReportReaderRule();
44   @Rule
45   public MutableAnalysisMetadataHolderRule analysisMetadataHolder = new MutableAnalysisMetadataHolderRule();
46   @Rule
47   public ExpectedException expectedException = ExpectedException.none();
48
49   private CeTask ceTask = createCeTask(PROJECT_KEY);
50   private ComputationStep underTest = new LoadReportAnalysisMetadataHolderStep(ceTask, reportReader, analysisMetadataHolder);
51
52   @Test
53   public void set_root_component_ref() throws Exception {
54     reportReader.setMetadata(
55       newBatchReportBuilder()
56         .setRootComponentRef(1)
57         .build());
58
59     underTest.execute();
60
61     assertThat(analysisMetadataHolder.getRootComponentRef()).isEqualTo(1);
62   }
63
64   @Test
65   public void set_analysis_date() throws Exception {
66     reportReader.setMetadata(
67       newBatchReportBuilder()
68         .setAnalysisDate(ANALYSIS_DATE)
69         .build());
70
71     underTest.execute();
72
73     assertThat(analysisMetadataHolder.getAnalysisDate()).isEqualTo(ANALYSIS_DATE);
74   }
75
76   @Test
77   public void set_branch() throws Exception {
78     reportReader.setMetadata(
79       newBatchReportBuilder()
80         .setBranch(BRANCH)
81         .build());
82
83     CeTask ceTask = createCeTask(PROJECT_KEY + ":" + BRANCH);
84     ComputationStep underTest = new LoadReportAnalysisMetadataHolderStep(ceTask, reportReader, analysisMetadataHolder);
85
86     underTest.execute();
87
88     assertThat(analysisMetadataHolder.getBranch()).isEqualTo(BRANCH);
89   }
90
91   @Test
92   public void set_null_branch_when_nothing_in_the_report() throws Exception {
93     reportReader.setMetadata(
94       newBatchReportBuilder()
95         .build());
96
97     underTest.execute();
98
99     assertThat(analysisMetadataHolder.getBranch()).isNull();
100   }
101
102   @Test
103   public void set_cross_project_duplication_to_true() throws Exception {
104     reportReader.setMetadata(
105       newBatchReportBuilder()
106         .setCrossProjectDuplicationActivated(true)
107         .build());
108
109     underTest.execute();
110
111     assertThat(analysisMetadataHolder.isCrossProjectDuplicationEnabled()).isEqualTo(true);
112   }
113
114   @Test
115   public void set_cross_project_duplication_to_false() throws Exception {
116     reportReader.setMetadata(
117       newBatchReportBuilder()
118         .setCrossProjectDuplicationActivated(false)
119         .build());
120
121     underTest.execute();
122
123     assertThat(analysisMetadataHolder.isCrossProjectDuplicationEnabled()).isEqualTo(false);
124   }
125
126   @Test
127   public void set_cross_project_duplication_to_false_when_nothing_in_the_report() throws Exception {
128     reportReader.setMetadata(
129       newBatchReportBuilder()
130         .build());
131
132     underTest.execute();
133
134     assertThat(analysisMetadataHolder.isCrossProjectDuplicationEnabled()).isEqualTo(false);
135   }
136
137   @Test
138   public void execute_fails_with_MessageException_if_projectKey_is_null_in_CE_task() {
139     CeTask res = mock(CeTask.class);
140     when(res.getComponentUuid()).thenReturn("prj_uuid");
141     reportReader.setMetadata(ScannerReport.Metadata.newBuilder().build());
142
143     ComputationStep underTest = new LoadReportAnalysisMetadataHolderStep(res, reportReader, analysisMetadataHolder);
144
145     expectedException.expect(MessageException.class);
146     expectedException.expectMessage("Compute Engine task component key is null. Project with UUID prj_uuid must have been deleted since report was uploaded. Can not proceed.");
147
148     underTest.execute();
149   }
150
151   @Test
152   public void execute_fails_with_MessageException_when_projectKey_in_report_is_different_from_componentKey_in_CE_task() {
153     reportReader.setMetadata(
154       ScannerReport.Metadata.newBuilder()
155         .setProjectKey("some other key")
156         .build());
157
158     expectedException.expect(MessageException.class);
159     expectedException.expectMessage("ProjectKey in report (some other key) is not consistent with projectKey under which the report as been submitted (" + PROJECT_KEY + ")");
160
161     underTest.execute();
162   }
163
164   private static ScannerReport.Metadata.Builder newBatchReportBuilder() {
165     return ScannerReport.Metadata.newBuilder()
166       .setProjectKey(PROJECT_KEY);
167   }
168
169   private CeTask createCeTask(String projectKey) {
170     CeTask res = mock(CeTask.class);
171     when(res.getComponentKey()).thenReturn(projectKey);
172     return res;
173   }
174
175 }