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.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;
30 import static org.assertj.core.api.Assertions.assertThat;
31 import static org.mockito.Mockito.mock;
32 import static org.mockito.Mockito.when;
34 public class LoadReportAnalysisMetadataHolderStepTest {
36 public static final String PROJECT_KEY = "project_key";
37 static long ANALYSIS_DATE = 123456789L;
39 static String BRANCH = "origin/master";
42 public BatchReportReaderRule reportReader = new BatchReportReaderRule();
44 public MutableAnalysisMetadataHolderRule analysisMetadataHolder = new MutableAnalysisMetadataHolderRule();
46 public ExpectedException expectedException = ExpectedException.none();
48 private CeTask ceTask = createCeTask(PROJECT_KEY);
49 private ComputationStep underTest = new LoadReportAnalysisMetadataHolderStep(ceTask, reportReader, analysisMetadataHolder);
52 public void set_root_component_ref() throws Exception {
53 reportReader.setMetadata(
54 newBatchReportBuilder()
55 .setRootComponentRef(1)
60 assertThat(analysisMetadataHolder.getRootComponentRef()).isEqualTo(1);
64 public void set_analysis_date() throws Exception {
65 reportReader.setMetadata(
66 newBatchReportBuilder()
67 .setAnalysisDate(ANALYSIS_DATE)
72 assertThat(analysisMetadataHolder.getAnalysisDate()).isEqualTo(ANALYSIS_DATE);
76 public void set_branch() throws Exception {
77 reportReader.setMetadata(
78 newBatchReportBuilder()
84 assertThat(analysisMetadataHolder.getBranch()).isEqualTo(BRANCH);
88 public void set_null_branch_when_nothing_in_the_report() throws Exception {
89 reportReader.setMetadata(
90 newBatchReportBuilder()
95 assertThat(analysisMetadataHolder.getBranch()).isNull();
99 public void set_cross_project_duplication_to_true() throws Exception {
100 reportReader.setMetadata(
101 newBatchReportBuilder()
102 .setCrossProjectDuplicationActivated(true)
107 assertThat(analysisMetadataHolder.isCrossProjectDuplicationEnabled()).isEqualTo(true);
111 public void set_cross_project_duplication_to_false() throws Exception {
112 reportReader.setMetadata(
113 newBatchReportBuilder()
114 .setCrossProjectDuplicationActivated(false)
119 assertThat(analysisMetadataHolder.isCrossProjectDuplicationEnabled()).isEqualTo(false);
123 public void set_cross_project_duplication_to_false_when_nothing_in_the_report() throws Exception {
124 reportReader.setMetadata(
125 newBatchReportBuilder()
130 assertThat(analysisMetadataHolder.isCrossProjectDuplicationEnabled()).isEqualTo(false);
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")
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 + ")");
146 private static BatchReport.Metadata.Builder newBatchReportBuilder() {
147 return BatchReport.Metadata.newBuilder()
148 .setProjectKey(PROJECT_KEY);
151 private CeTask createCeTask(String projectKey) {
152 CeTask res = mock(CeTask.class);
153 when(res.getComponentKey()).thenReturn(projectKey);