]> source.dussan.org Git - sonarqube.git/blob
9d4032fcde677c568c94a2a56daef3c512a5ff6d
[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.batch.protocol.output.BatchReport;
26 import org.sonar.server.computation.analysis.MutableAnalysisMetadataHolderRule;
27 import org.sonar.server.computation.batch.BatchReportReaderRule;
28 import org.sonar.server.computation.queue.CeTask;
29
30 import static org.assertj.core.api.Assertions.assertThat;
31 import static org.mockito.Mockito.mock;
32 import static org.mockito.Mockito.when;
33
34 public class LoadReportAnalysisMetadataHolderStepTest {
35
36   public static final String PROJECT_KEY = "project_key";
37   static long ANALYSIS_DATE = 123456789L;
38
39   static String BRANCH = "origin/master";
40
41   @Rule
42   public BatchReportReaderRule reportReader = new BatchReportReaderRule();
43   @Rule
44   public MutableAnalysisMetadataHolderRule analysisMetadataHolder = new MutableAnalysisMetadataHolderRule();
45   @Rule
46   public ExpectedException expectedException = ExpectedException.none();
47
48   private CeTask ceTask = createCeTask(PROJECT_KEY);
49   private ComputationStep underTest = new LoadReportAnalysisMetadataHolderStep(ceTask, reportReader, analysisMetadataHolder);
50
51   @Test
52   public void set_root_component_ref() throws Exception {
53     reportReader.setMetadata(
54       newBatchReportBuilder()
55         .setRootComponentRef(1)
56         .build());
57
58     underTest.execute();
59
60     assertThat(analysisMetadataHolder.getRootComponentRef()).isEqualTo(1);
61   }
62
63   @Test
64   public void set_analysis_date() throws Exception {
65     reportReader.setMetadata(
66       newBatchReportBuilder()
67         .setAnalysisDate(ANALYSIS_DATE)
68         .build());
69
70     underTest.execute();
71
72     assertThat(analysisMetadataHolder.getAnalysisDate()).isEqualTo(ANALYSIS_DATE);
73   }
74
75   @Test
76   public void set_branch() throws Exception {
77     reportReader.setMetadata(
78       newBatchReportBuilder()
79         .setBranch(BRANCH)
80         .build());
81
82     underTest.execute();
83
84     assertThat(analysisMetadataHolder.getBranch()).isEqualTo(BRANCH);
85   }
86
87   @Test
88   public void set_null_branch_when_nothing_in_the_report() throws Exception {
89     reportReader.setMetadata(
90       newBatchReportBuilder()
91         .build());
92
93     underTest.execute();
94
95     assertThat(analysisMetadataHolder.getBranch()).isNull();
96   }
97
98   @Test
99   public void set_cross_project_duplication_to_true() throws Exception {
100     reportReader.setMetadata(
101       newBatchReportBuilder()
102         .setCrossProjectDuplicationActivated(true)
103         .build());
104
105     underTest.execute();
106
107     assertThat(analysisMetadataHolder.isCrossProjectDuplicationEnabled()).isEqualTo(true);
108   }
109
110   @Test
111   public void set_cross_project_duplication_to_false() throws Exception {
112     reportReader.setMetadata(
113       newBatchReportBuilder()
114         .setCrossProjectDuplicationActivated(false)
115         .build());
116
117     underTest.execute();
118
119     assertThat(analysisMetadataHolder.isCrossProjectDuplicationEnabled()).isEqualTo(false);
120   }
121
122   @Test
123   public void set_cross_project_duplication_to_false_when_nothing_in_the_report() throws Exception {
124     reportReader.setMetadata(
125       newBatchReportBuilder()
126         .build());
127
128     underTest.execute();
129
130     assertThat(analysisMetadataHolder.isCrossProjectDuplicationEnabled()).isEqualTo(false);
131   }
132
133   @Test
134   public void execute_fails_with_ISE_when_projectKey_in_report_is_different_from_componentKey_in_CE_task() {
135     reportReader.setMetadata(
136         BatchReport.Metadata.newBuilder()
137             .setProjectKey("some other key")
138             .build());
139
140     expectedException.expect(IllegalStateException.class);
141     expectedException.expectMessage("ProjectKey in report (some other key) is not consistent with projectKey under which the report as been submitted (" + PROJECT_KEY + ")");
142
143     underTest.execute();
144   }
145
146   private static BatchReport.Metadata.Builder newBatchReportBuilder() {
147     return BatchReport.Metadata.newBuilder()
148       .setProjectKey(PROJECT_KEY);
149   }
150
151   private CeTask createCeTask(String projectKey) {
152     CeTask res = mock(CeTask.class);
153     when(res.getComponentKey()).thenReturn(projectKey);
154     return res;
155   }
156
157 }