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.component.Component;
30 import org.sonar.server.computation.task.projectanalysis.component.DefaultBranchImpl;
31 import org.sonar.server.computation.task.projectanalysis.component.ReportComponent;
32 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolderRule;
33 import org.sonar.server.computation.task.projectanalysis.event.Event;
34 import org.sonar.server.computation.task.projectanalysis.event.EventRepository;
35 import org.sonar.server.computation.task.projectanalysis.measure.Measure;
36 import org.sonar.server.computation.task.projectanalysis.measure.MeasureRepository;
37 import org.sonar.server.computation.task.projectanalysis.measure.QualityGateStatus;
38 import org.sonar.server.computation.task.projectanalysis.metric.Metric;
39 import org.sonar.server.computation.task.projectanalysis.metric.MetricRepository;
40 import org.sonar.server.notification.NotificationService;
42 import static org.assertj.core.api.Assertions.assertThat;
43 import static org.mockito.Matchers.eq;
44 import static org.mockito.Mockito.mock;
45 import static org.mockito.Mockito.reset;
46 import static org.mockito.Mockito.verify;
47 import static org.mockito.Mockito.verifyNoMoreInteractions;
48 import static org.mockito.Mockito.when;
49 import static org.sonar.api.measures.CoreMetrics.ALERT_STATUS_KEY;
50 import static org.sonar.server.computation.task.projectanalysis.measure.Measure.Level.ERROR;
51 import static org.sonar.server.computation.task.projectanalysis.measure.Measure.Level.OK;
52 import static org.sonar.server.computation.task.projectanalysis.measure.Measure.Level.WARN;
54 public class QualityGateEventsStepTest {
55 private static final ReportComponent PROJECT_COMPONENT = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("uuid 1").setKey("key 1")
56 .addChildren(ReportComponent.builder(Component.Type.MODULE, 2).build())
58 private static final String INVALID_ALERT_STATUS = "trololo";
59 private static final String ALERT_TEXT = "alert text";
60 private static final QualityGateStatus OK_QUALITY_GATE_STATUS = new QualityGateStatus(OK, ALERT_TEXT);
61 private static final QualityGateStatus WARN_QUALITY_GATE_STATUS = new QualityGateStatus(WARN, ALERT_TEXT);
62 private static final QualityGateStatus ERROR_QUALITY_GATE_STATUS = new QualityGateStatus(ERROR, ALERT_TEXT);
65 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
68 public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
70 private ArgumentCaptor<Event> eventArgumentCaptor = ArgumentCaptor.forClass(Event.class);
71 private ArgumentCaptor<Notification> notificationArgumentCaptor = ArgumentCaptor.forClass(Notification.class);
73 private Metric alertStatusMetric = mock(Metric.class);
75 private MetricRepository metricRepository = mock(MetricRepository.class);
76 private MeasureRepository measureRepository = mock(MeasureRepository.class);
77 private EventRepository eventRepository = mock(EventRepository.class);
78 private NotificationService notificationService = mock(NotificationService.class);
79 private QualityGateEventsStep underTest = new QualityGateEventsStep(treeRootHolder, metricRepository, measureRepository, eventRepository, notificationService, analysisMetadataHolder);
83 when(metricRepository.getByKey(ALERT_STATUS_KEY)).thenReturn(alertStatusMetric);
84 analysisMetadataHolder.setBranch(null);
85 treeRootHolder.setRoot(PROJECT_COMPONENT);
89 public void no_event_if_no_raw_ALERT_STATUS_measure() {
90 when(measureRepository.getRawMeasure(PROJECT_COMPONENT, alertStatusMetric)).thenReturn(Optional.<Measure>absent());
94 verify(measureRepository).getRawMeasure(PROJECT_COMPONENT, alertStatusMetric);
95 verifyNoMoreInteractions(measureRepository, eventRepository);
99 public void no_event_created_if_raw_ALERT_STATUS_measure_is_null() {
100 when(measureRepository.getRawMeasure(PROJECT_COMPONENT, alertStatusMetric)).thenReturn(of(Measure.newMeasureBuilder().createNoValue()));
104 verify(measureRepository).getRawMeasure(PROJECT_COMPONENT, alertStatusMetric);
105 verifyNoMoreInteractions(measureRepository, eventRepository);
108 private static Optional<Measure> of(Measure measure) {
109 return Optional.of((Measure) measure);
113 public void no_event_created_if_raw_ALERT_STATUS_measure_is_unsupported_value() {
114 when(measureRepository.getRawMeasure(PROJECT_COMPONENT, alertStatusMetric)).thenReturn(of(Measure.newMeasureBuilder().create(INVALID_ALERT_STATUS)));
118 verify(measureRepository).getRawMeasure(PROJECT_COMPONENT, alertStatusMetric);
119 verifyNoMoreInteractions(measureRepository, eventRepository);
123 public void no_event_created_if_no_base_ALERT_STATUS_and_raw_is_OK() {
124 QualityGateStatus someQGStatus = new QualityGateStatus(Measure.Level.OK);
126 when(measureRepository.getRawMeasure(PROJECT_COMPONENT, alertStatusMetric)).thenReturn(of(Measure.newMeasureBuilder().setQualityGateStatus(someQGStatus).createNoValue()));
127 when(measureRepository.getBaseMeasure(PROJECT_COMPONENT, alertStatusMetric)).thenReturn(of(Measure.newMeasureBuilder().createNoValue()));
131 verify(measureRepository).getRawMeasure(PROJECT_COMPONENT, alertStatusMetric);
132 verify(measureRepository).getBaseMeasure(PROJECT_COMPONENT, alertStatusMetric);
133 verifyNoMoreInteractions(measureRepository, eventRepository);
137 public void event_created_if_no_base_ALERT_STATUS_and_raw_is_WARN() {
138 verify_event_created_if_no_base_ALERT_STATUS_measure(WARN, "Orange");
142 public void event_created_if_base_ALERT_STATUS_and_raw_is_ERROR() {
143 verify_event_created_if_no_base_ALERT_STATUS_measure(ERROR, "Red");
147 public void event_created_if_base_ALERT_STATUS_has_no_alertStatus_and_raw_is_ERROR() {
148 verify_event_created_if_no_base_ALERT_STATUS_measure(ERROR, "Red");
152 public void event_created_if_base_ALERT_STATUS_has_no_alertStatus_and_raw_is_WARN() {
153 verify_event_created_if_no_base_ALERT_STATUS_measure(WARN, "Orange");
157 public void event_created_if_base_ALERT_STATUS_has_invalid_alertStatus_and_raw_is_ERROR() {
158 verify_event_created_if_no_base_ALERT_STATUS_measure(ERROR, "Red");
162 public void event_created_if_base_ALERT_STATUS_has_invalid_alertStatus_and_raw_is_WARN() {
163 verify_event_created_if_no_base_ALERT_STATUS_measure(WARN, "Orange");
166 private void verify_event_created_if_no_base_ALERT_STATUS_measure(Measure.Level rawAlterStatus, String expectedLabel) {
167 QualityGateStatus someQGStatus = new QualityGateStatus(rawAlterStatus, ALERT_TEXT);
169 when(measureRepository.getRawMeasure(PROJECT_COMPONENT, alertStatusMetric)).thenReturn(of(Measure.newMeasureBuilder().setQualityGateStatus(someQGStatus).createNoValue()));
170 when(measureRepository.getBaseMeasure(PROJECT_COMPONENT, alertStatusMetric)).thenReturn(of(Measure.newMeasureBuilder().createNoValue()));
174 verify(measureRepository).getRawMeasure(PROJECT_COMPONENT, alertStatusMetric);
175 verify(measureRepository).getBaseMeasure(PROJECT_COMPONENT, alertStatusMetric);
176 verify(eventRepository).add(eq(PROJECT_COMPONENT), eventArgumentCaptor.capture());
177 verifyNoMoreInteractions(measureRepository, eventRepository);
179 Event event = eventArgumentCaptor.getValue();
180 assertThat(event.getCategory()).isEqualTo(Event.Category.ALERT);
181 assertThat(event.getName()).isEqualTo(expectedLabel);
182 assertThat(event.getDescription()).isEqualTo(ALERT_TEXT);
183 assertThat(event.getData()).isNull();
185 verify(notificationService).deliver(notificationArgumentCaptor.capture());
186 Notification notification = notificationArgumentCaptor.getValue();
187 assertThat(notification.getType()).isEqualTo("alerts");
188 assertThat(notification.getFieldValue("projectKey")).isEqualTo(PROJECT_COMPONENT.getKey());
189 assertThat(notification.getFieldValue("projectUuid")).isEqualTo(PROJECT_COMPONENT.getUuid());
190 assertThat(notification.getFieldValue("projectName")).isEqualTo(PROJECT_COMPONENT.getName());
191 assertThat(notification.getFieldValue("branch")).isNull();
192 assertThat(notification.getFieldValue("alertLevel")).isEqualTo(rawAlterStatus.name());
193 assertThat(notification.getFieldValue("alertName")).isEqualTo(expectedLabel);
197 public void no_event_created_if_base_ALERT_STATUS_measure_but_status_is_the_same() {
198 when(measureRepository.getRawMeasure(PROJECT_COMPONENT, alertStatusMetric)).thenReturn(of(Measure.newMeasureBuilder().setQualityGateStatus(OK_QUALITY_GATE_STATUS).createNoValue()));
199 when(measureRepository.getBaseMeasure(PROJECT_COMPONENT, alertStatusMetric)).thenReturn(of(Measure.newMeasureBuilder().setQualityGateStatus(OK_QUALITY_GATE_STATUS).createNoValue()));
203 verify(measureRepository).getRawMeasure(PROJECT_COMPONENT, alertStatusMetric);
204 verify(measureRepository).getBaseMeasure(PROJECT_COMPONENT, alertStatusMetric);
205 verifyNoMoreInteractions(measureRepository, eventRepository);
209 public void event_created_if_base_ALERT_STATUS_measure_exists_and_status_has_changed() {
210 verify_event_created_if_base_ALERT_STATUS_measure_exists_and_status_has_changed(OK, WARN_QUALITY_GATE_STATUS, "Orange (was Green)");
211 verify_event_created_if_base_ALERT_STATUS_measure_exists_and_status_has_changed(OK, ERROR_QUALITY_GATE_STATUS, "Red (was Green)");
212 verify_event_created_if_base_ALERT_STATUS_measure_exists_and_status_has_changed(WARN, OK_QUALITY_GATE_STATUS, "Green (was Orange)");
213 verify_event_created_if_base_ALERT_STATUS_measure_exists_and_status_has_changed(WARN, ERROR_QUALITY_GATE_STATUS, "Red (was Orange)");
214 verify_event_created_if_base_ALERT_STATUS_measure_exists_and_status_has_changed(ERROR, OK_QUALITY_GATE_STATUS, "Green (was Red)");
215 verify_event_created_if_base_ALERT_STATUS_measure_exists_and_status_has_changed(ERROR, WARN_QUALITY_GATE_STATUS, "Orange (was Red)");
218 private void verify_event_created_if_base_ALERT_STATUS_measure_exists_and_status_has_changed(Measure.Level previousAlertStatus,
219 QualityGateStatus newQualityGateStatus, String expectedLabel) {
220 when(measureRepository.getRawMeasure(PROJECT_COMPONENT, alertStatusMetric)).thenReturn(of(Measure.newMeasureBuilder().setQualityGateStatus(newQualityGateStatus).createNoValue()));
221 when(measureRepository.getBaseMeasure(PROJECT_COMPONENT, alertStatusMetric)).thenReturn(
222 of(Measure.newMeasureBuilder().setQualityGateStatus(new QualityGateStatus(previousAlertStatus)).createNoValue()));
226 verify(measureRepository).getRawMeasure(PROJECT_COMPONENT, alertStatusMetric);
227 verify(measureRepository).getBaseMeasure(PROJECT_COMPONENT, alertStatusMetric);
228 verify(eventRepository).add(eq(PROJECT_COMPONENT), eventArgumentCaptor.capture());
229 verifyNoMoreInteractions(measureRepository, eventRepository);
231 Event event = eventArgumentCaptor.getValue();
232 assertThat(event.getCategory()).isEqualTo(Event.Category.ALERT);
233 assertThat(event.getName()).isEqualTo(expectedLabel);
234 assertThat(event.getDescription()).isEqualTo(ALERT_TEXT);
235 assertThat(event.getData()).isNull();
237 verify(notificationService).deliver(notificationArgumentCaptor.capture());
238 Notification notification = notificationArgumentCaptor.getValue();
239 assertThat(notification.getType()).isEqualTo("alerts");
240 assertThat(notification.getFieldValue("projectKey")).isEqualTo(PROJECT_COMPONENT.getKey());
241 assertThat(notification.getFieldValue("projectUuid")).isEqualTo(PROJECT_COMPONENT.getUuid());
242 assertThat(notification.getFieldValue("projectName")).isEqualTo(PROJECT_COMPONENT.getName());
243 assertThat(notification.getFieldValue("branch")).isNull();
244 assertThat(notification.getFieldValue("alertLevel")).isEqualTo(newQualityGateStatus.getStatus().name());
245 assertThat(notification.getFieldValue("alertName")).isEqualTo(expectedLabel);
247 reset(measureRepository, eventRepository, notificationService);
251 public void verify_branch_name_is_set_in_notification() {
252 String branchName = "feature1";
253 analysisMetadataHolder.setBranch(new DefaultBranchImpl(branchName));
255 when(measureRepository.getRawMeasure(PROJECT_COMPONENT, alertStatusMetric)).thenReturn(of(Measure.newMeasureBuilder().setQualityGateStatus(WARN_QUALITY_GATE_STATUS).createNoValue()));
256 when(measureRepository.getBaseMeasure(PROJECT_COMPONENT, alertStatusMetric)).thenReturn(
257 of(Measure.newMeasureBuilder().setQualityGateStatus(new QualityGateStatus(ERROR)).createNoValue()));
261 verify(notificationService).deliver(notificationArgumentCaptor.capture());
262 Notification notification = notificationArgumentCaptor.getValue();
263 assertThat(notification.getType()).isEqualTo("alerts");
264 assertThat(notification.getFieldValue("projectKey")).isEqualTo(PROJECT_COMPONENT.getKey());
265 assertThat(notification.getFieldValue("projectUuid")).isEqualTo(PROJECT_COMPONENT.getUuid());
266 assertThat(notification.getFieldValue("projectName")).isEqualTo(PROJECT_COMPONENT.getName());
267 assertThat(notification.getFieldValue("branch")).isEqualTo(branchName);
269 reset(measureRepository, eventRepository, notificationService);