3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.computation.step;
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;
31 import static org.assertj.core.api.Assertions.assertThat;
32 import static org.mockito.Mockito.mock;
33 import static org.mockito.Mockito.when;
35 public class LoadReportAnalysisMetadataHolderStepTest {
37 public static final String PROJECT_KEY = "project_key";
38 static long ANALYSIS_DATE = 123456789L;
40 static String BRANCH = "origin/master";
43 public BatchReportReaderRule reportReader = new BatchReportReaderRule();
45 public MutableAnalysisMetadataHolderRule analysisMetadataHolder = new MutableAnalysisMetadataHolderRule();
47 public ExpectedException expectedException = ExpectedException.none();
49 private CeTask ceTask = createCeTask(PROJECT_KEY);
50 private ComputationStep underTest = new LoadReportAnalysisMetadataHolderStep(ceTask, reportReader, analysisMetadataHolder);
53 public void set_root_component_ref() throws Exception {
54 reportReader.setMetadata(
55 newBatchReportBuilder()
56 .setRootComponentRef(1)
61 assertThat(analysisMetadataHolder.getRootComponentRef()).isEqualTo(1);
65 public void set_analysis_date() throws Exception {
66 reportReader.setMetadata(
67 newBatchReportBuilder()
68 .setAnalysisDate(ANALYSIS_DATE)
73 assertThat(analysisMetadataHolder.getAnalysisDate()).isEqualTo(ANALYSIS_DATE);
77 public void set_branch() throws Exception {
78 reportReader.setMetadata(
79 newBatchReportBuilder()
83 CeTask ceTask = createCeTask(PROJECT_KEY + ":" + BRANCH);
84 ComputationStep underTest = new LoadReportAnalysisMetadataHolderStep(ceTask, reportReader, analysisMetadataHolder);
88 assertThat(analysisMetadataHolder.getBranch()).isEqualTo(BRANCH);
92 public void set_null_branch_when_nothing_in_the_report() throws Exception {
93 reportReader.setMetadata(
94 newBatchReportBuilder()
99 assertThat(analysisMetadataHolder.getBranch()).isNull();
103 public void set_cross_project_duplication_to_true() throws Exception {
104 reportReader.setMetadata(
105 newBatchReportBuilder()
106 .setCrossProjectDuplicationActivated(true)
111 assertThat(analysisMetadataHolder.isCrossProjectDuplicationEnabled()).isEqualTo(true);
115 public void set_cross_project_duplication_to_false() throws Exception {
116 reportReader.setMetadata(
117 newBatchReportBuilder()
118 .setCrossProjectDuplicationActivated(false)
123 assertThat(analysisMetadataHolder.isCrossProjectDuplicationEnabled()).isEqualTo(false);
127 public void set_cross_project_duplication_to_false_when_nothing_in_the_report() throws Exception {
128 reportReader.setMetadata(
129 newBatchReportBuilder()
134 assertThat(analysisMetadataHolder.isCrossProjectDuplicationEnabled()).isEqualTo(false);
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());
143 ComputationStep underTest = new LoadReportAnalysisMetadataHolderStep(res, reportReader, analysisMetadataHolder);
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.");
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")
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 + ")");
164 private static ScannerReport.Metadata.Builder newBatchReportBuilder() {
165 return ScannerReport.Metadata.newBuilder()
166 .setProjectKey(PROJECT_KEY);
169 private CeTask createCeTask(String projectKey) {
170 CeTask res = mock(CeTask.class);
171 when(res.getComponentKey()).thenReturn(projectKey);