3 * Copyright (C) 2009-2019 SonarSource SA
4 * mailto:info 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.ce.task.projectanalysis.step;
22 import java.util.Optional;
23 import org.junit.Before;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.junit.rules.ExpectedException;
27 import org.sonar.api.config.internal.MapSettings;
28 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
29 import org.sonar.ce.task.projectanalysis.component.ConfigurationRepository;
30 import org.sonar.ce.task.projectanalysis.qualitygate.MutableQualityGateHolderRule;
31 import org.sonar.ce.task.projectanalysis.qualitygate.QualityGate;
32 import org.sonar.ce.task.projectanalysis.qualitygate.QualityGateService;
33 import org.sonar.ce.task.step.TestComputationStepContext;
34 import org.sonar.server.qualitygate.ShortLivingBranchQualityGate;
36 import static java.util.Collections.emptyList;
37 import static org.assertj.core.api.Assertions.assertThat;
38 import static org.mockito.ArgumentMatchers.any;
39 import static org.mockito.Mockito.mock;
40 import static org.mockito.Mockito.when;
42 public class LoadQualityGateStepTest {
45 public ExpectedException expectedException = ExpectedException.none();
47 public MutableQualityGateHolderRule mutableQualityGateHolder = new MutableQualityGateHolderRule();
49 private AnalysisMetadataHolder analysisMetadataHolder = mock(AnalysisMetadataHolder.class);
50 private ConfigurationRepository settingsRepository = mock(ConfigurationRepository.class);
51 private QualityGateService qualityGateService = mock(QualityGateService.class);
53 private LoadQualityGateStep underTest = new LoadQualityGateStep(settingsRepository, qualityGateService, mutableQualityGateHolder, analysisMetadataHolder);
57 when(analysisMetadataHolder.isShortLivingBranch()).thenReturn(false);
61 public void add_hardcoded_QG_on_short_living_branch() {
62 when(analysisMetadataHolder.isShortLivingBranch()).thenReturn(true);
63 QualityGate qualityGate = mock(QualityGate.class);
64 when(qualityGateService.findById(ShortLivingBranchQualityGate.ID)).thenReturn(Optional.of(qualityGate));
66 underTest.execute(new TestComputationStepContext());
68 assertThat(mutableQualityGateHolder.getQualityGate().get()).isSameAs(qualityGate);
72 public void add_hardcoded_QG_on_pull_request() {
73 when(analysisMetadataHolder.isPullRequest()).thenReturn(true);
74 QualityGate qualityGate = mock(QualityGate.class);
75 when(qualityGateService.findById(ShortLivingBranchQualityGate.ID)).thenReturn(Optional.of(qualityGate));
77 underTest.execute(new TestComputationStepContext());
79 assertThat(mutableQualityGateHolder.getQualityGate().get()).isSameAs(qualityGate);
83 public void execute_sets_default_QualityGate_when_project_has_no_settings() {
84 when(settingsRepository.getConfiguration()).thenReturn(new MapSettings().asConfig());
85 QualityGate defaultGate = mock(QualityGate.class);
86 when(qualityGateService.findDefaultQualityGate(any())).thenReturn(defaultGate);
88 underTest.execute(new TestComputationStepContext());
90 assertThat(mutableQualityGateHolder.getQualityGate().get()).isSameAs(defaultGate);
94 public void execute_sets_default_QualityGate_when_property_value_is_not_a_long() {
95 expectedException.expect(IllegalStateException.class);
96 expectedException.expectMessage("Unsupported value (10 sds) in property sonar.qualitygate");
98 when(settingsRepository.getConfiguration()).thenReturn(new MapSettings().setProperty("sonar.qualitygate", "10 sds").asConfig());
100 underTest.execute(new TestComputationStepContext());
104 public void execute_sets_QualityGate_if_it_can_be_found_by_service() {
105 QualityGate qualityGate = new QualityGate(10, "name", emptyList());
107 when(settingsRepository.getConfiguration()).thenReturn(new MapSettings().setProperty("sonar.qualitygate", 10).asConfig());
108 when(qualityGateService.findById(10)).thenReturn(Optional.of(qualityGate));
110 underTest.execute(new TestComputationStepContext());
112 assertThat(mutableQualityGateHolder.getQualityGate().get()).isSameAs(qualityGate);