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 org.junit.Before;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.mockito.ArgumentCaptor;
27 import org.sonar.api.notifications.Notification;
28 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
29 import org.sonar.server.computation.task.projectanalysis.analysis.Project;
30 import org.sonar.server.computation.task.projectanalysis.component.Component;
31 import org.sonar.server.computation.task.projectanalysis.component.DefaultBranchImpl;
32 import org.sonar.server.computation.task.projectanalysis.component.ReportComponent;
33 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolderRule;
34 import org.sonar.server.computation.task.projectanalysis.event.Event;
35 import org.sonar.server.computation.task.projectanalysis.event.EventRepository;
36 import org.sonar.server.computation.task.projectanalysis.measure.Measure;
37 import org.sonar.server.computation.task.projectanalysis.measure.MeasureRepository;
38 import org.sonar.server.computation.task.projectanalysis.measure.QualityGateStatus;
39 import org.sonar.server.computation.task.projectanalysis.metric.Metric;
40 import org.sonar.server.computation.task.projectanalysis.metric.MetricRepository;
41 import org.sonar.server.notification.NotificationService;
43 import static org.assertj.core.api.Assertions.assertThat;
44 import static org.mockito.Matchers.eq;
45 import static org.mockito.Mockito.mock;
46 import static org.mockito.Mockito.reset;
47 import static org.mockito.Mockito.verify;
48 import static org.mockito.Mockito.verifyNoMoreInteractions;
49 import static org.mockito.Mockito.when;
50 import static org.sonar.api.measures.CoreMetrics.ALERT_STATUS_KEY;
51 import static org.sonar.server.computation.task.projectanalysis.measure.Measure.Level.ERROR;
52 import static org.sonar.server.computation.task.projectanalysis.measure.Measure.Level.OK;
53 import static org.sonar.server.computation.task.projectanalysis.measure.Measure.Level.WARN;
55 public class QualityGateEventsStepTest {
56 private static final ReportComponent PROJECT_COMPONENT = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("uuid 1").setKey("key 1")
57 .addChildren(ReportComponent.builder(Component.Type.MODULE, 2).setVersion("V1.9").build())
59 private static final String INVALID_ALERT_STATUS = "trololo";
60 private static final String ALERT_TEXT = "alert text";
61 private static final QualityGateStatus OK_QUALITY_GATE_STATUS = new QualityGateStatus(OK, ALERT_TEXT);
62 private static final QualityGateStatus WARN_QUALITY_GATE_STATUS = new QualityGateStatus(WARN, ALERT_TEXT);
63 private static final QualityGateStatus ERROR_QUALITY_GATE_STATUS = new QualityGateStatus(ERROR, ALERT_TEXT);
66 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
69 public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
71 private ArgumentCaptor<Event> eventArgumentCaptor = ArgumentCaptor.forClass(Event.class);
72 private ArgumentCaptor<Notification> notificationArgumentCaptor = ArgumentCaptor.forClass(Notification.class);
74 private Metric alertStatusMetric = mock(Metric.class);
76 private MetricRepository metricRepository = mock(MetricRepository.class);
77 private MeasureRepository measureRepository = mock(MeasureRepository.class);
78 private EventRepository eventRepository = mock(EventRepository.class);
79 private NotificationService notificationService = mock(NotificationService.class);
80 private QualityGateEventsStep underTest = new QualityGateEventsStep(treeRootHolder, metricRepository, measureRepository, eventRepository, notificationService, analysisMetadataHolder);
84 when(metricRepository.getByKey(ALERT_STATUS_KEY)).thenReturn(alertStatusMetric);
85 analysisMetadataHolder.setProject(new Project(PROJECT_COMPONENT.getUuid(), PROJECT_COMPONENT.getKey(), PROJECT_COMPONENT.getName()));
86 analysisMetadataHolder.setBranch(null);
87 treeRootHolder.setRoot(PROJECT_COMPONENT);
91 public void no_event_if_no_raw_ALERT_STATUS_measure() {
92 when(measureRepository.getRawMeasure(PROJECT_COMPONENT, alertStatusMetric)).thenReturn(Optional.<Measure>absent());
96 verify(measureRepository).getRawMeasure(PROJECT_COMPONENT, alertStatusMetric);
97 verifyNoMoreInteractions(measureRepository, eventRepository);
101 public void no_event_created_if_raw_ALERT_STATUS_measure_is_null() {
102 when(measureRepository.getRawMeasure(PROJECT_COMPONENT, alertStatusMetric)).thenReturn(of(Measure.newMeasureBuilder().createNoValue()));
106 verify(measureRepository).getRawMeasure(PROJECT_COMPONENT, alertStatusMetric);
107 verifyNoMoreInteractions(measureRepository, eventRepository);
110 private static Optional<Measure> of(Measure measure) {
111 return Optional.of((Measure) measure);
115 public void no_event_created_if_raw_ALERT_STATUS_measure_is_unsupported_value() {
116 when(measureRepository.getRawMeasure(PROJECT_COMPONENT, alertStatusMetric)).thenReturn(of(Measure.newMeasureBuilder().create(INVALID_ALERT_STATUS)));
120 verify(measureRepository).getRawMeasure(PROJECT_COMPONENT, alertStatusMetric);
121 verifyNoMoreInteractions(measureRepository, eventRepository);
125 public void no_event_created_if_no_base_ALERT_STATUS_and_raw_is_OK() {
126 QualityGateStatus someQGStatus = new QualityGateStatus(Measure.Level.OK);
128 when(measureRepository.getRawMeasure(PROJECT_COMPONENT, alertStatusMetric)).thenReturn(of(Measure.newMeasureBuilder().setQualityGateStatus(someQGStatus).createNoValue()));
129 when(measureRepository.getBaseMeasure(PROJECT_COMPONENT, alertStatusMetric)).thenReturn(of(Measure.newMeasureBuilder().createNoValue()));
133 verify(measureRepository).getRawMeasure(PROJECT_COMPONENT, alertStatusMetric);
134 verify(measureRepository).getBaseMeasure(PROJECT_COMPONENT, alertStatusMetric);
135 verifyNoMoreInteractions(measureRepository, eventRepository);
139 public void event_created_if_no_base_ALERT_STATUS_and_raw_is_WARN() {
140 verify_event_created_if_no_base_ALERT_STATUS_measure(WARN, "Orange");
144 public void event_created_if_base_ALERT_STATUS_and_raw_is_ERROR() {
145 verify_event_created_if_no_base_ALERT_STATUS_measure(ERROR, "Red");
149 public void event_created_if_base_ALERT_STATUS_has_no_alertStatus_and_raw_is_ERROR() {
150 verify_event_created_if_no_base_ALERT_STATUS_measure(ERROR, "Red");
154 public void event_created_if_base_ALERT_STATUS_has_no_alertStatus_and_raw_is_WARN() {
155 verify_event_created_if_no_base_ALERT_STATUS_measure(WARN, "Orange");
159 public void event_created_if_base_ALERT_STATUS_has_invalid_alertStatus_and_raw_is_ERROR() {
160 verify_event_created_if_no_base_ALERT_STATUS_measure(ERROR, "Red");
164 public void event_created_if_base_ALERT_STATUS_has_invalid_alertStatus_and_raw_is_WARN() {
165 verify_event_created_if_no_base_ALERT_STATUS_measure(WARN, "Orange");
168 private void verify_event_created_if_no_base_ALERT_STATUS_measure(Measure.Level rawAlterStatus, String expectedLabel) {
169 QualityGateStatus someQGStatus = new QualityGateStatus(rawAlterStatus, ALERT_TEXT);
171 when(measureRepository.getRawMeasure(PROJECT_COMPONENT, alertStatusMetric)).thenReturn(of(Measure.newMeasureBuilder().setQualityGateStatus(someQGStatus).createNoValue()));
172 when(measureRepository.getBaseMeasure(PROJECT_COMPONENT, alertStatusMetric)).thenReturn(of(Measure.newMeasureBuilder().createNoValue()));
176 verify(measureRepository).getRawMeasure(PROJECT_COMPONENT, alertStatusMetric);
177 verify(measureRepository).getBaseMeasure(PROJECT_COMPONENT, alertStatusMetric);
178 verify(eventRepository).add(eq(PROJECT_COMPONENT), eventArgumentCaptor.capture());
179 verifyNoMoreInteractions(measureRepository, eventRepository);
181 Event event = eventArgumentCaptor.getValue();
182 assertThat(event.getCategory()).isEqualTo(Event.Category.ALERT);
183 assertThat(event.getName()).isEqualTo(expectedLabel);
184 assertThat(event.getDescription()).isEqualTo(ALERT_TEXT);
185 assertThat(event.getData()).isNull();
187 verify(notificationService).deliver(notificationArgumentCaptor.capture());
188 Notification notification = notificationArgumentCaptor.getValue();
189 assertThat(notification.getType()).isEqualTo("alerts");
190 assertThat(notification.getFieldValue("projectKey")).isEqualTo(PROJECT_COMPONENT.getPublicKey());
191 assertThat(notification.getFieldValue("projectUuid")).isEqualTo(PROJECT_COMPONENT.getUuid());
192 assertThat(notification.getFieldValue("projectName")).isEqualTo(PROJECT_COMPONENT.getName());
193 assertThat(notification.getFieldValue("projectVersion")).isEqualTo(PROJECT_COMPONENT.getReportAttributes().getVersion());
194 assertThat(notification.getFieldValue("branch")).isNull();
195 assertThat(notification.getFieldValue("alertLevel")).isEqualTo(rawAlterStatus.name());
196 assertThat(notification.getFieldValue("alertName")).isEqualTo(expectedLabel);
200 public void no_event_created_if_base_ALERT_STATUS_measure_but_status_is_the_same() {
201 when(measureRepository.getRawMeasure(PROJECT_COMPONENT, alertStatusMetric)).thenReturn(of(Measure.newMeasureBuilder().setQualityGateStatus(OK_QUALITY_GATE_STATUS).createNoValue()));
202 when(measureRepository.getBaseMeasure(PROJECT_COMPONENT, alertStatusMetric)).thenReturn(of(Measure.newMeasureBuilder().setQualityGateStatus(OK_QUALITY_GATE_STATUS).createNoValue()));
206 verify(measureRepository).getRawMeasure(PROJECT_COMPONENT, alertStatusMetric);
207 verify(measureRepository).getBaseMeasure(PROJECT_COMPONENT, alertStatusMetric);
208 verifyNoMoreInteractions(measureRepository, eventRepository);
212 public void event_created_if_base_ALERT_STATUS_measure_exists_and_status_has_changed() {
213 verify_event_created_if_base_ALERT_STATUS_measure_exists_and_status_has_changed(OK, WARN_QUALITY_GATE_STATUS, "Orange (was Green)");
214 verify_event_created_if_base_ALERT_STATUS_measure_exists_and_status_has_changed(OK, ERROR_QUALITY_GATE_STATUS, "Red (was Green)");
215 verify_event_created_if_base_ALERT_STATUS_measure_exists_and_status_has_changed(WARN, OK_QUALITY_GATE_STATUS, "Green (was Orange)");
216 verify_event_created_if_base_ALERT_STATUS_measure_exists_and_status_has_changed(WARN, ERROR_QUALITY_GATE_STATUS, "Red (was Orange)");
217 verify_event_created_if_base_ALERT_STATUS_measure_exists_and_status_has_changed(ERROR, OK_QUALITY_GATE_STATUS, "Green (was Red)");
218 verify_event_created_if_base_ALERT_STATUS_measure_exists_and_status_has_changed(ERROR, WARN_QUALITY_GATE_STATUS, "Orange (was Red)");
221 private void verify_event_created_if_base_ALERT_STATUS_measure_exists_and_status_has_changed(Measure.Level previousAlertStatus,
222 QualityGateStatus newQualityGateStatus, String expectedLabel) {
223 when(measureRepository.getRawMeasure(PROJECT_COMPONENT, alertStatusMetric)).thenReturn(of(Measure.newMeasureBuilder().setQualityGateStatus(newQualityGateStatus).createNoValue()));
224 when(measureRepository.getBaseMeasure(PROJECT_COMPONENT, alertStatusMetric)).thenReturn(
225 of(Measure.newMeasureBuilder().setQualityGateStatus(new QualityGateStatus(previousAlertStatus)).createNoValue()));
229 verify(measureRepository).getRawMeasure(PROJECT_COMPONENT, alertStatusMetric);
230 verify(measureRepository).getBaseMeasure(PROJECT_COMPONENT, alertStatusMetric);
231 verify(eventRepository).add(eq(PROJECT_COMPONENT), eventArgumentCaptor.capture());
232 verifyNoMoreInteractions(measureRepository, eventRepository);
234 Event event = eventArgumentCaptor.getValue();
235 assertThat(event.getCategory()).isEqualTo(Event.Category.ALERT);
236 assertThat(event.getName()).isEqualTo(expectedLabel);
237 assertThat(event.getDescription()).isEqualTo(ALERT_TEXT);
238 assertThat(event.getData()).isNull();
240 verify(notificationService).deliver(notificationArgumentCaptor.capture());
241 Notification notification = notificationArgumentCaptor.getValue();
242 assertThat(notification.getType()).isEqualTo("alerts");
243 assertThat(notification.getFieldValue("projectKey")).isEqualTo(PROJECT_COMPONENT.getPublicKey());
244 assertThat(notification.getFieldValue("projectUuid")).isEqualTo(PROJECT_COMPONENT.getUuid());
245 assertThat(notification.getFieldValue("projectName")).isEqualTo(PROJECT_COMPONENT.getName());
246 assertThat(notification.getFieldValue("projectVersion")).isEqualTo(PROJECT_COMPONENT.getReportAttributes().getVersion());
247 assertThat(notification.getFieldValue("branch")).isNull();
248 assertThat(notification.getFieldValue("alertLevel")).isEqualTo(newQualityGateStatus.getStatus().name());
249 assertThat(notification.getFieldValue("alertName")).isEqualTo(expectedLabel);
251 reset(measureRepository, eventRepository, notificationService);
255 public void verify_branch_name_is_set_in_notification_when_not_main() {
256 String branchName = "feature1";
257 analysisMetadataHolder.setBranch(new DefaultBranchImpl(branchName) {
259 public boolean isMain() {
264 when(measureRepository.getRawMeasure(PROJECT_COMPONENT, alertStatusMetric)).thenReturn(of(Measure.newMeasureBuilder().setQualityGateStatus(WARN_QUALITY_GATE_STATUS).createNoValue()));
265 when(measureRepository.getBaseMeasure(PROJECT_COMPONENT, alertStatusMetric)).thenReturn(
266 of(Measure.newMeasureBuilder().setQualityGateStatus(new QualityGateStatus(ERROR)).createNoValue()));
270 verify(notificationService).deliver(notificationArgumentCaptor.capture());
271 Notification notification = notificationArgumentCaptor.getValue();
272 assertThat(notification.getType()).isEqualTo("alerts");
273 assertThat(notification.getFieldValue("projectKey")).isEqualTo(PROJECT_COMPONENT.getPublicKey());
274 assertThat(notification.getFieldValue("projectUuid")).isEqualTo(PROJECT_COMPONENT.getUuid());
275 assertThat(notification.getFieldValue("projectName")).isEqualTo(PROJECT_COMPONENT.getName());
276 assertThat(notification.getFieldValue("projectVersion")).isEqualTo(PROJECT_COMPONENT.getReportAttributes().getVersion());
277 assertThat(notification.getFieldValue("branch")).isEqualTo(branchName);
279 reset(measureRepository, eventRepository, notificationService);
283 public void verify_branch_name_is_not_set_in_notification_when_main() {
284 analysisMetadataHolder.setBranch(new DefaultBranchImpl());
286 when(measureRepository.getRawMeasure(PROJECT_COMPONENT, alertStatusMetric)).thenReturn(of(Measure.newMeasureBuilder().setQualityGateStatus(WARN_QUALITY_GATE_STATUS).createNoValue()));
287 when(measureRepository.getBaseMeasure(PROJECT_COMPONENT, alertStatusMetric)).thenReturn(
288 of(Measure.newMeasureBuilder().setQualityGateStatus(new QualityGateStatus(ERROR)).createNoValue()));
292 verify(notificationService).deliver(notificationArgumentCaptor.capture());
293 Notification notification = notificationArgumentCaptor.getValue();
294 assertThat(notification.getType()).isEqualTo("alerts");
295 assertThat(notification.getFieldValue("projectKey")).isEqualTo(PROJECT_COMPONENT.getPublicKey());
296 assertThat(notification.getFieldValue("projectUuid")).isEqualTo(PROJECT_COMPONENT.getUuid());
297 assertThat(notification.getFieldValue("projectName")).isEqualTo(PROJECT_COMPONENT.getName());
298 assertThat(notification.getFieldValue("projectVersion")).isEqualTo(PROJECT_COMPONENT.getReportAttributes().getVersion());
299 assertThat(notification.getFieldValue("branch")).isEqualTo(null);
301 reset(measureRepository, eventRepository, notificationService);