3 * Copyright (C) 2009-2017 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.server.computation.task.projectanalysis.step;
22 import com.google.common.base.Optional;
23 import java.util.Collections;
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.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder;
30 import org.sonar.server.computation.task.projectanalysis.component.ConfigurationRepository;
31 import org.sonar.server.computation.task.projectanalysis.qualitygate.MutableQualityGateHolderRule;
32 import org.sonar.server.computation.task.projectanalysis.qualitygate.QualityGate;
33 import org.sonar.server.computation.task.projectanalysis.qualitygate.QualityGateService;
34 import org.sonar.server.qualitygate.ShortLivingBranchQualityGate;
36 import static java.lang.String.format;
37 import static org.assertj.core.api.Assertions.assertThat;
38 import static org.assertj.guava.api.Assertions.assertThat;
39 import static org.mockito.Mockito.mock;
40 import static org.mockito.Mockito.verify;
41 import static org.mockito.Mockito.verifyNoMoreInteractions;
42 import static org.mockito.Mockito.when;
44 public class LoadQualityGateStepTest {
47 public ExpectedException expectedException = ExpectedException.none();
49 public MutableQualityGateHolderRule mutableQualityGateHolder = new MutableQualityGateHolderRule();
51 private AnalysisMetadataHolder analysisMetadataHolder = mock(AnalysisMetadataHolder.class);
52 private ConfigurationRepository settingsRepository = mock(ConfigurationRepository.class);
53 private QualityGateService qualityGateService = mock(QualityGateService.class);
55 private LoadQualityGateStep underTest = new LoadQualityGateStep(settingsRepository, qualityGateService, mutableQualityGateHolder, analysisMetadataHolder);
59 when(analysisMetadataHolder.isShortLivingBranch()).thenReturn(false);
63 public void add_hardcoded_QG_on_short_living_branch() {
64 when(analysisMetadataHolder.isShortLivingBranch()).thenReturn(true);
65 QualityGate qualityGate = mock(QualityGate.class);
66 when(qualityGateService.findById(ShortLivingBranchQualityGate.ID)).thenReturn(Optional.of(qualityGate));
70 assertThat(mutableQualityGateHolder.getQualityGate().get()).isSameAs(qualityGate);
74 public void execute_sets_default_QualityGate_when_project_has_no_settings() {
75 when(settingsRepository.getConfiguration()).thenReturn(new MapSettings().asConfig());
79 verifyNoQualityGate();
81 // verify only project is processed
82 verify(settingsRepository).getConfiguration();
83 verifyNoMoreInteractions(settingsRepository);
87 public void execute_sets_default_QualityGate_when_property_value_is_not_a_long() {
88 expectedException.expect(IllegalStateException.class);
89 expectedException.expectMessage(format("Unsupported value (%s) in property sonar.qualitygate", "10 sds"));
91 when(settingsRepository.getConfiguration()).thenReturn(new MapSettings().setProperty("sonar.qualitygate", "10 sds").asConfig());
97 public void execute_sets_default_QualityGate_if_it_can_not_be_found_by_service() {
98 when(settingsRepository.getConfiguration()).thenReturn(new MapSettings().setProperty("sonar.qualitygate", 10).asConfig());
99 when(qualityGateService.findById(10)).thenReturn(Optional.absent());
103 verifyNoQualityGate();
107 public void execute_sets_QualityGate_if_it_can_be_found_by_service() {
108 QualityGate qualityGate = new QualityGate(465, "name", Collections.emptyList());
110 when(settingsRepository.getConfiguration()).thenReturn(new MapSettings().setProperty("sonar.qualitygate", 10).asConfig());
111 when(qualityGateService.findById(10)).thenReturn(Optional.of(qualityGate));
115 assertThat(mutableQualityGateHolder.getQualityGate().get()).isSameAs(qualityGate);
118 private void verifyNoQualityGate() {
119 assertThat(mutableQualityGateHolder.getQualityGate()).isAbsent();