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.Rule;
25 import org.junit.Test;
26 import org.junit.rules.ExpectedException;
27 import org.sonar.api.config.MapSettings;
28 import org.sonar.server.computation.task.projectanalysis.component.Component;
29 import org.sonar.server.computation.task.projectanalysis.component.ReportComponent;
30 import org.sonar.server.computation.task.projectanalysis.component.SettingsRepository;
31 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolderRule;
32 import org.sonar.server.computation.task.projectanalysis.component.VisitException;
33 import org.sonar.server.computation.task.projectanalysis.qualitygate.Condition;
34 import org.sonar.server.computation.task.projectanalysis.qualitygate.MutableQualityGateHolderRule;
35 import org.sonar.server.computation.task.projectanalysis.qualitygate.QualityGate;
36 import org.sonar.server.computation.task.projectanalysis.qualitygate.QualityGateService;
38 import static java.lang.String.format;
39 import static org.assertj.core.api.Assertions.assertThat;
40 import static org.assertj.guava.api.Assertions.assertThat;
41 import static org.mockito.Mockito.mock;
42 import static org.mockito.Mockito.verify;
43 import static org.mockito.Mockito.verifyNoMoreInteractions;
44 import static org.mockito.Mockito.when;
45 import static org.sonar.test.ExceptionCauseMatcher.hasType;
47 public class QualityGateLoadingStepTest {
48 private static final String PROJECT_KEY = "project key";
49 private static final ReportComponent PROJECT_ALONE = ReportComponent.builder(Component.Type.PROJECT, 1).setKey(PROJECT_KEY).build();
52 public ExpectedException expectedException = ExpectedException.none();
54 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
56 public MutableQualityGateHolderRule mutableQualityGateHolder = new MutableQualityGateHolderRule();
58 private SettingsRepository settingsRepository = mock(SettingsRepository.class);
59 private QualityGateService qualityGateService = mock(QualityGateService.class);
61 private LoadQualityGateStep underTest = new LoadQualityGateStep(treeRootHolder, settingsRepository, qualityGateService, mutableQualityGateHolder);
64 public void execute_sets_default_QualityGate_when_project_has_no_settings() {
65 ReportComponent root = ReportComponent.builder(Component.Type.PROJECT, 1).setKey(PROJECT_KEY).addChildren(ReportComponent.builder(Component.Type.FILE, 2).build()).build();
66 treeRootHolder.setRoot(root);
67 when(settingsRepository.getSettings(root)).thenReturn(new MapSettings());
71 verifyNoQualityGate();
73 // verify only project is processed
74 verify(settingsRepository).getSettings(root);
75 verifyNoMoreInteractions(settingsRepository);
79 public void execute_sets_default_QualityGate_when_property_value_is_not_a_long() {
80 expectedException.expect(VisitException.class);
81 expectedException.expectCause(
82 hasType(IllegalStateException.class)
83 .andMessage(format("Unsupported value (%s) in property sonar.qualitygate", "10 sds")));
85 treeRootHolder.setRoot(PROJECT_ALONE);
86 when(settingsRepository.getSettings(PROJECT_ALONE)).thenReturn(new MapSettings().setProperty("sonar.qualitygate", "10 sds"));
92 public void execute_sets_default_QualityGate_if_it_can_not_be_found_by_service() {
93 treeRootHolder.setRoot(PROJECT_ALONE);
94 when(settingsRepository.getSettings(PROJECT_ALONE)).thenReturn(new MapSettings().setProperty("sonar.qualitygate", 10));
95 when(qualityGateService.findById(10)).thenReturn(Optional.<QualityGate>absent());
99 verifyNoQualityGate();
103 public void execute_sets_QualityGate_if_it_can_be_found_by_service() {
104 QualityGate qualityGate = new QualityGate(465, "name", Collections.<Condition>emptyList());
106 treeRootHolder.setRoot(PROJECT_ALONE);
107 when(settingsRepository.getSettings(PROJECT_ALONE)).thenReturn(new MapSettings().setProperty("sonar.qualitygate", 10));
108 when(qualityGateService.findById(10)).thenReturn(Optional.of(qualityGate));
112 assertThat(mutableQualityGateHolder.getQualityGate().get()).isSameAs(qualityGate);
115 private void verifyNoQualityGate() {
116 assertThat(mutableQualityGateHolder.getQualityGate()).isAbsent();