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.Arrays;
23 import java.util.Optional;
24 import org.junit.Before;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.rules.ExpectedException;
28 import org.sonar.api.config.internal.MapSettings;
29 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
30 import org.sonar.ce.task.projectanalysis.component.ConfigurationRepository;
31 import org.sonar.ce.task.projectanalysis.metric.Metric;
32 import org.sonar.ce.task.projectanalysis.metric.MetricImpl;
33 import org.sonar.ce.task.projectanalysis.qualitygate.Condition;
34 import org.sonar.ce.task.projectanalysis.qualitygate.MutableQualityGateHolderRule;
35 import org.sonar.ce.task.projectanalysis.qualitygate.QualityGate;
36 import org.sonar.ce.task.projectanalysis.qualitygate.QualityGateService;
37 import org.sonar.ce.task.step.TestComputationStepContext;
39 import static java.util.Collections.emptyList;
40 import static org.assertj.core.api.Assertions.assertThat;
41 import static org.mockito.ArgumentMatchers.any;
42 import static org.mockito.Mockito.mock;
43 import static org.mockito.Mockito.when;
45 public class LoadQualityGateStepTest {
48 public ExpectedException expectedException = ExpectedException.none();
50 public MutableQualityGateHolderRule mutableQualityGateHolder = new MutableQualityGateHolderRule();
52 private AnalysisMetadataHolder analysisMetadataHolder = mock(AnalysisMetadataHolder.class);
53 private ConfigurationRepository settingsRepository = mock(ConfigurationRepository.class);
54 private QualityGateService qualityGateService = mock(QualityGateService.class);
56 private LoadQualityGateStep underTest = new LoadQualityGateStep(settingsRepository, qualityGateService, mutableQualityGateHolder, analysisMetadataHolder);
60 when(analysisMetadataHolder.isShortLivingBranch()).thenReturn(false);
64 public void filter_conditions_on_short_living_branch() {
65 when(settingsRepository.getConfiguration()).thenReturn(new MapSettings().asConfig());
67 Metric newMetric = new MetricImpl(1, "new_key", "name", Metric.MetricType.INT);
68 Metric metric = new MetricImpl(2, "key", "name", Metric.MetricType.INT);
69 Condition variation = new Condition(newMetric, Condition.Operator.GREATER_THAN.getDbValue(), "1.0");
70 Condition condition = new Condition(metric, Condition.Operator.GREATER_THAN.getDbValue(), "1.0");
72 when(analysisMetadataHolder.isSLBorPR()).thenReturn(true);
73 QualityGate defaultGate = new QualityGate(1, "qg", Arrays.asList(variation, condition));
74 when(qualityGateService.findDefaultQualityGate(any())).thenReturn(defaultGate);
76 underTest.execute(new TestComputationStepContext());
78 assertThat(mutableQualityGateHolder.getQualityGate().get().getConditions()).containsExactly(variation);
82 public void filter_conditions_on_pull_request() {
83 when(settingsRepository.getConfiguration()).thenReturn(new MapSettings().asConfig());
85 Metric newMetric = new MetricImpl(1, "new_key", "name", Metric.MetricType.INT);
86 Metric metric = new MetricImpl(2, "key", "name", Metric.MetricType.INT);
87 Condition variation = new Condition(newMetric, Condition.Operator.GREATER_THAN.getDbValue(), "1.0");
88 Condition condition = new Condition(metric, Condition.Operator.GREATER_THAN.getDbValue(), "1.0");
90 when(analysisMetadataHolder.isSLBorPR()).thenReturn(true);
91 QualityGate defaultGate = new QualityGate(1, "qg", Arrays.asList(variation, condition));
92 when(qualityGateService.findDefaultQualityGate(any())).thenReturn(defaultGate);
94 underTest.execute(new TestComputationStepContext());
96 assertThat(mutableQualityGateHolder.getQualityGate().get().getConditions()).containsExactly(variation);
100 public void execute_sets_default_QualityGate_when_project_has_no_settings() {
101 when(settingsRepository.getConfiguration()).thenReturn(new MapSettings().asConfig());
102 QualityGate defaultGate = mock(QualityGate.class);
103 when(qualityGateService.findDefaultQualityGate(any())).thenReturn(defaultGate);
105 underTest.execute(new TestComputationStepContext());
107 assertThat(mutableQualityGateHolder.getQualityGate().get()).isSameAs(defaultGate);
111 public void execute_sets_default_QualityGate_when_property_value_is_not_a_long() {
112 expectedException.expect(IllegalStateException.class);
113 expectedException.expectMessage("Unsupported value (10 sds) in property sonar.qualitygate");
115 when(settingsRepository.getConfiguration()).thenReturn(new MapSettings().setProperty("sonar.qualitygate", "10 sds").asConfig());
117 underTest.execute(new TestComputationStepContext());
121 public void execute_sets_QualityGate_if_it_can_be_found_by_service() {
122 QualityGate qualityGate = new QualityGate(10, "name", emptyList());
124 when(settingsRepository.getConfiguration()).thenReturn(new MapSettings().setProperty("sonar.qualitygate", 10).asConfig());
125 when(qualityGateService.findById(10)).thenReturn(Optional.of(qualityGate));
127 underTest.execute(new TestComputationStepContext());
129 assertThat(mutableQualityGateHolder.getQualityGate().get()).isSameAs(qualityGate);