3 * Copyright (C) 2009-2022 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.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
28 import org.sonar.ce.task.projectanalysis.metric.Metric;
29 import org.sonar.ce.task.projectanalysis.metric.MetricImpl;
30 import org.sonar.ce.task.projectanalysis.qualitygate.Condition;
31 import org.sonar.ce.task.projectanalysis.qualitygate.MutableQualityGateHolderRule;
32 import org.sonar.ce.task.projectanalysis.qualitygate.QualityGate;
33 import org.sonar.ce.task.projectanalysis.qualitygate.QualityGateService;
34 import org.sonar.ce.task.step.TestComputationStepContext;
35 import org.sonar.server.project.Project;
37 import static java.util.Collections.emptyList;
38 import static org.assertj.core.api.Assertions.assertThat;
39 import static org.mockito.ArgumentMatchers.any;
40 import static org.mockito.Mockito.mock;
41 import static org.mockito.Mockito.when;
43 public class LoadQualityGateStepTest {
46 public MutableQualityGateHolderRule mutableQualityGateHolder = new MutableQualityGateHolderRule();
48 private final AnalysisMetadataHolder analysisMetadataHolder = mock(AnalysisMetadataHolder.class);
49 private final QualityGateService qualityGateService = mock(QualityGateService.class);
51 private final LoadQualityGateStep underTest = new LoadQualityGateStep(qualityGateService, mutableQualityGateHolder, analysisMetadataHolder);
58 public void filter_conditions_on_pull_request() {
59 Metric newMetric = new MetricImpl("1", "new_key", "name", Metric.MetricType.INT);
60 Metric metric = new MetricImpl("2", "key", "name", Metric.MetricType.INT);
61 Condition variation = new Condition(newMetric, Condition.Operator.GREATER_THAN.getDbValue(), "1.0");
62 Condition condition = new Condition(metric, Condition.Operator.GREATER_THAN.getDbValue(), "1.0");
64 when(analysisMetadataHolder.isPullRequest()).thenReturn(true);
65 QualityGate defaultGate = new QualityGate("1", "qg", Arrays.asList(variation, condition));
66 when(qualityGateService.findDefaultQualityGate()).thenReturn(defaultGate);
68 underTest.execute(new TestComputationStepContext());
70 assertThat(mutableQualityGateHolder.getQualityGate().get().getConditions()).containsExactly(variation);
74 public void execute_sets_default_QualityGate_when_project_has_no_settings() {
75 QualityGate defaultGate = mock(QualityGate.class);
76 when(qualityGateService.findDefaultQualityGate()).thenReturn(defaultGate);
78 underTest.execute(new TestComputationStepContext());
80 assertThat(mutableQualityGateHolder.getQualityGate().get()).isSameAs(defaultGate);
84 public void execute_sets_QualityGate_if_it_can_be_found_by_service() {
85 QualityGate qualityGate = new QualityGate("10", "name", emptyList());
87 when(analysisMetadataHolder.getProject()).thenReturn(mock(Project.class));
88 when(qualityGateService.findQualityGate(any(Project.class))).thenReturn(Optional.of(qualityGate));
90 underTest.execute(new TestComputationStepContext());
92 assertThat(mutableQualityGateHolder.getQualityGate().get()).isSameAs(qualityGate);