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).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.getKey());
191 assertThat(notification.getFieldValue("projectUuid")).isEqualTo(PROJECT_COMPONENT.getUuid());
192 assertThat(notification.getFieldValue("projectName")).isEqualTo(PROJECT_COMPONENT.getName());
193 assertThat(notification.getFieldValue("branch")).isNull();
194 assertThat(notification.getFieldValue("alertLevel")).isEqualTo(rawAlterStatus.name());
195 assertThat(notification.getFieldValue("alertName")).isEqualTo(expectedLabel);
199 public void no_event_created_if_base_ALERT_STATUS_measure_but_status_is_the_same() {
200 when(measureRepository.getRawMeasure(PROJECT_COMPONENT, alertStatusMetric)).thenReturn(of(Measure.newMeasureBuilder().setQualityGateStatus(OK_QUALITY_GATE_STATUS).createNoValue()));
201 when(measureRepository.getBaseMeasure(PROJECT_COMPONENT, alertStatusMetric)).thenReturn(of(Measure.newMeasureBuilder().setQualityGateStatus(OK_QUALITY_GATE_STATUS).createNoValue()));
205 verify(measureRepository).getRawMeasure(PROJECT_COMPONENT, alertStatusMetric);
206 verify(measureRepository).getBaseMeasure(PROJECT_COMPONENT, alertStatusMetric);
207 verifyNoMoreInteractions(measureRepository, eventRepository);
211 public void event_created_if_base_ALERT_STATUS_measure_exists_and_status_has_changed() {
212 verify_event_created_if_base_ALERT_STATUS_measure_exists_and_status_has_changed(OK, WARN_QUALITY_GATE_STATUS, "Orange (was Green)");
213 verify_event_created_if_base_ALERT_STATUS_measure_exists_and_status_has_changed(OK, ERROR_QUALITY_GATE_STATUS, "Red (was Green)");
214 verify_event_created_if_base_ALERT_STATUS_measure_exists_and_status_has_changed(WARN, OK_QUALITY_GATE_STATUS, "Green (was Orange)");
215 verify_event_created_if_base_ALERT_STATUS_measure_exists_and_status_has_changed(WARN, ERROR_QUALITY_GATE_STATUS, "Red (was Orange)");
216 verify_event_created_if_base_ALERT_STATUS_measure_exists_and_status_has_changed(ERROR, OK_QUALITY_GATE_STATUS, "Green (was Red)");
217 verify_event_created_if_base_ALERT_STATUS_measure_exists_and_status_has_changed(ERROR, WARN_QUALITY_GATE_STATUS, "Orange (was Red)");
220 private void verify_event_created_if_base_ALERT_STATUS_measure_exists_and_status_has_changed(Measure.Level previousAlertStatus,
221 QualityGateStatus newQualityGateStatus, String expectedLabel) {
222 when(measureRepository.getRawMeasure(PROJECT_COMPONENT, alertStatusMetric)).thenReturn(of(Measure.newMeasureBuilder().setQualityGateStatus(newQualityGateStatus).createNoValue()));
223 when(measureRepository.getBaseMeasure(PROJECT_COMPONENT, alertStatusMetric)).thenReturn(
224 of(Measure.newMeasureBuilder().setQualityGateStatus(new QualityGateStatus(previousAlertStatus)).createNoValue()));
228 verify(measureRepository).getRawMeasure(PROJECT_COMPONENT, alertStatusMetric);
229 verify(measureRepository).getBaseMeasure(PROJECT_COMPONENT, alertStatusMetric);
230 verify(eventRepository).add(eq(PROJECT_COMPONENT), eventArgumentCaptor.capture());
231 verifyNoMoreInteractions(measureRepository, eventRepository);
233 Event event = eventArgumentCaptor.getValue();
234 assertThat(event.getCategory()).isEqualTo(Event.Category.ALERT);
235 assertThat(event.getName()).isEqualTo(expectedLabel);
236 assertThat(event.getDescription()).isEqualTo(ALERT_TEXT);
237 assertThat(event.getData()).isNull();
239 verify(notificationService).deliver(notificationArgumentCaptor.capture());
240 Notification notification = notificationArgumentCaptor.getValue();
241 assertThat(notification.getType()).isEqualTo("alerts");
242 assertThat(notification.getFieldValue("projectKey")).isEqualTo(PROJECT_COMPONENT.getKey());
243 assertThat(notification.getFieldValue("projectUuid")).isEqualTo(PROJECT_COMPONENT.getUuid());
244 assertThat(notification.getFieldValue("projectName")).isEqualTo(PROJECT_COMPONENT.getName());
245 assertThat(notification.getFieldValue("branch")).isNull();
246 assertThat(notification.getFieldValue("alertLevel")).isEqualTo(newQualityGateStatus.getStatus().name());
247 assertThat(notification.getFieldValue("alertName")).isEqualTo(expectedLabel);
249 reset(measureRepository, eventRepository, notificationService);
253 public void verify_branch_name_is_set_in_notification() {
254 String branchName = "feature1";
255 analysisMetadataHolder.setBranch(new DefaultBranchImpl(branchName));
257 when(measureRepository.getRawMeasure(PROJECT_COMPONENT, alertStatusMetric)).thenReturn(of(Measure.newMeasureBuilder().setQualityGateStatus(WARN_QUALITY_GATE_STATUS).createNoValue()));
258 when(measureRepository.getBaseMeasure(PROJECT_COMPONENT, alertStatusMetric)).thenReturn(
259 of(Measure.newMeasureBuilder().setQualityGateStatus(new QualityGateStatus(ERROR)).createNoValue()));
263 verify(notificationService).deliver(notificationArgumentCaptor.capture());
264 Notification notification = notificationArgumentCaptor.getValue();
265 assertThat(notification.getType()).isEqualTo("alerts");
266 assertThat(notification.getFieldValue("projectKey")).isEqualTo(PROJECT_COMPONENT.getKey());
267 assertThat(notification.getFieldValue("projectUuid")).isEqualTo(PROJECT_COMPONENT.getUuid());
268 assertThat(notification.getFieldValue("projectName")).isEqualTo(PROJECT_COMPONENT.getName());
269 assertThat(notification.getFieldValue("branch")).isEqualTo(branchName);
271 reset(measureRepository, eventRepository, notificationService);