]> source.dussan.org Git - sonarqube.git/blob
8f634c9b33e191f1837b8f7c3781d2b46cc8647d
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.server.computation.task.projectanalysis.step;
21
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.component.Component;
29 import org.sonar.server.computation.task.projectanalysis.component.ReportComponent;
30 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolderRule;
31 import org.sonar.server.computation.task.projectanalysis.event.Event;
32 import org.sonar.server.computation.task.projectanalysis.event.EventRepository;
33 import org.sonar.server.computation.task.projectanalysis.measure.Measure;
34 import org.sonar.server.computation.task.projectanalysis.measure.MeasureRepository;
35 import org.sonar.server.computation.task.projectanalysis.measure.QualityGateStatus;
36 import org.sonar.server.computation.task.projectanalysis.metric.Metric;
37 import org.sonar.server.computation.task.projectanalysis.metric.MetricRepository;
38 import org.sonar.server.notification.NotificationService;
39
40 import static org.assertj.core.api.Assertions.assertThat;
41 import static org.mockito.Matchers.eq;
42 import static org.mockito.Mockito.mock;
43 import static org.mockito.Mockito.reset;
44 import static org.mockito.Mockito.verify;
45 import static org.mockito.Mockito.verifyNoMoreInteractions;
46 import static org.mockito.Mockito.when;
47 import static org.sonar.api.measures.CoreMetrics.ALERT_STATUS_KEY;
48 import static org.sonar.server.computation.task.projectanalysis.measure.Measure.Level.ERROR;
49 import static org.sonar.server.computation.task.projectanalysis.measure.Measure.Level.OK;
50 import static org.sonar.server.computation.task.projectanalysis.measure.Measure.Level.WARN;
51
52 public class QualityGateEventsStepTest {
53   private static final ReportComponent PROJECT_COMPONENT = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("uuid 1").setKey("key 1")
54     .addChildren(ReportComponent.builder(Component.Type.MODULE, 2).build())
55     .build();
56   private static final String INVALID_ALERT_STATUS = "trololo";
57   private static final String ALERT_TEXT = "alert text";
58   private static final QualityGateStatus OK_QUALITY_GATE_STATUS = new QualityGateStatus(OK, ALERT_TEXT);
59   private static final QualityGateStatus WARN_QUALITY_GATE_STATUS = new QualityGateStatus(WARN, ALERT_TEXT);
60   private static final QualityGateStatus ERROR_QUALITY_GATE_STATUS = new QualityGateStatus(ERROR, ALERT_TEXT);
61
62   @Rule
63   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
64
65   private ArgumentCaptor<Event> eventArgumentCaptor = ArgumentCaptor.forClass(Event.class);
66   private ArgumentCaptor<Notification> notificationArgumentCaptor = ArgumentCaptor.forClass(Notification.class);
67
68   private Metric alertStatusMetric = mock(Metric.class);
69
70   private MetricRepository metricRepository = mock(MetricRepository.class);
71   private MeasureRepository measureRepository = mock(MeasureRepository.class);
72   private EventRepository eventRepository = mock(EventRepository.class);
73   private NotificationService notificationService = mock(NotificationService.class);
74   private QualityGateEventsStep underTest = new QualityGateEventsStep(treeRootHolder, metricRepository, measureRepository, eventRepository, notificationService);
75
76   @Before
77   public void setUp() {
78     when(metricRepository.getByKey(ALERT_STATUS_KEY)).thenReturn(alertStatusMetric);
79     treeRootHolder.setRoot(PROJECT_COMPONENT);
80   }
81
82   @Test
83   public void no_event_if_no_raw_ALERT_STATUS_measure() {
84     when(measureRepository.getRawMeasure(PROJECT_COMPONENT, alertStatusMetric)).thenReturn(Optional.<Measure>absent());
85
86     underTest.execute();
87
88     verify(measureRepository).getRawMeasure(PROJECT_COMPONENT, alertStatusMetric);
89     verifyNoMoreInteractions(measureRepository, eventRepository);
90   }
91
92   @Test
93   public void no_event_created_if_raw_ALERT_STATUS_measure_is_null() {
94     when(measureRepository.getRawMeasure(PROJECT_COMPONENT, alertStatusMetric)).thenReturn(of(Measure.newMeasureBuilder().createNoValue()));
95
96     underTest.execute();
97
98     verify(measureRepository).getRawMeasure(PROJECT_COMPONENT, alertStatusMetric);
99     verifyNoMoreInteractions(measureRepository, eventRepository);
100   }
101
102   private static Optional<Measure> of(Measure measure) {
103     return Optional.of((Measure) measure);
104   }
105
106   @Test
107   public void no_event_created_if_raw_ALERT_STATUS_measure_is_unsupported_value() {
108     when(measureRepository.getRawMeasure(PROJECT_COMPONENT, alertStatusMetric)).thenReturn(of(Measure.newMeasureBuilder().create(INVALID_ALERT_STATUS)));
109
110     underTest.execute();
111
112     verify(measureRepository).getRawMeasure(PROJECT_COMPONENT, alertStatusMetric);
113     verifyNoMoreInteractions(measureRepository, eventRepository);
114   }
115
116   @Test
117   public void no_event_created_if_no_base_ALERT_STATUS_and_raw_is_OK() {
118     QualityGateStatus someQGStatus = new QualityGateStatus(Measure.Level.OK);
119
120     when(measureRepository.getRawMeasure(PROJECT_COMPONENT, alertStatusMetric)).thenReturn(of(Measure.newMeasureBuilder().setQualityGateStatus(someQGStatus).createNoValue()));
121     when(measureRepository.getBaseMeasure(PROJECT_COMPONENT, alertStatusMetric)).thenReturn(of(Measure.newMeasureBuilder().createNoValue()));
122
123     underTest.execute();
124
125     verify(measureRepository).getRawMeasure(PROJECT_COMPONENT, alertStatusMetric);
126     verify(measureRepository).getBaseMeasure(PROJECT_COMPONENT, alertStatusMetric);
127     verifyNoMoreInteractions(measureRepository, eventRepository);
128   }
129
130   @Test
131   public void event_created_if_no_base_ALERT_STATUS_and_raw_is_WARN() {
132     verify_event_created_if_no_base_ALERT_STATUS_measure(WARN, "Orange");
133   }
134
135   @Test
136   public void event_created_if_base_ALERT_STATUS_and_raw_is_ERROR() {
137     verify_event_created_if_no_base_ALERT_STATUS_measure(ERROR, "Red");
138   }
139
140   @Test
141   public void event_created_if_base_ALERT_STATUS_has_no_alertStatus_and_raw_is_ERROR() {
142     verify_event_created_if_no_base_ALERT_STATUS_measure(ERROR, "Red");
143   }
144
145   @Test
146   public void event_created_if_base_ALERT_STATUS_has_no_alertStatus_and_raw_is_WARN() {
147     verify_event_created_if_no_base_ALERT_STATUS_measure(WARN, "Orange");
148   }
149
150   @Test
151   public void event_created_if_base_ALERT_STATUS_has_invalid_alertStatus_and_raw_is_ERROR() {
152     verify_event_created_if_no_base_ALERT_STATUS_measure(ERROR, "Red");
153   }
154
155   @Test
156   public void event_created_if_base_ALERT_STATUS_has_invalid_alertStatus_and_raw_is_WARN() {
157     verify_event_created_if_no_base_ALERT_STATUS_measure(WARN, "Orange");
158   }
159
160   private void verify_event_created_if_no_base_ALERT_STATUS_measure(Measure.Level rawAlterStatus, String expectedLabel) {
161     QualityGateStatus someQGStatus = new QualityGateStatus(rawAlterStatus, ALERT_TEXT);
162
163     when(measureRepository.getRawMeasure(PROJECT_COMPONENT, alertStatusMetric)).thenReturn(of(Measure.newMeasureBuilder().setQualityGateStatus(someQGStatus).createNoValue()));
164     when(measureRepository.getBaseMeasure(PROJECT_COMPONENT, alertStatusMetric)).thenReturn(of(Measure.newMeasureBuilder().createNoValue()));
165
166     underTest.execute();
167
168     verify(measureRepository).getRawMeasure(PROJECT_COMPONENT, alertStatusMetric);
169     verify(measureRepository).getBaseMeasure(PROJECT_COMPONENT, alertStatusMetric);
170     verify(eventRepository).add(eq(PROJECT_COMPONENT), eventArgumentCaptor.capture());
171     verifyNoMoreInteractions(measureRepository, eventRepository);
172
173     Event event = eventArgumentCaptor.getValue();
174     assertThat(event.getCategory()).isEqualTo(Event.Category.ALERT);
175     assertThat(event.getName()).isEqualTo(expectedLabel);
176     assertThat(event.getDescription()).isEqualTo(ALERT_TEXT);
177     assertThat(event.getData()).isNull();
178
179     verify(notificationService).deliver(notificationArgumentCaptor.capture());
180     Notification notification = notificationArgumentCaptor.getValue();
181     assertThat(notification.getType()).isEqualTo("alerts");
182     assertThat(notification.getFieldValue("projectKey")).isEqualTo(PROJECT_COMPONENT.getKey());
183     assertThat(notification.getFieldValue("projectUuid")).isEqualTo(PROJECT_COMPONENT.getUuid());
184     assertThat(notification.getFieldValue("projectName")).isEqualTo(PROJECT_COMPONENT.getName());
185     assertThat(notification.getFieldValue("alertLevel")).isEqualTo(rawAlterStatus.name());
186     assertThat(notification.getFieldValue("alertName")).isEqualTo(expectedLabel);
187   }
188
189   @Test
190   public void no_event_created_if_base_ALERT_STATUS_measure_but_status_is_the_same() {
191     when(measureRepository.getRawMeasure(PROJECT_COMPONENT, alertStatusMetric)).thenReturn(of(Measure.newMeasureBuilder().setQualityGateStatus(OK_QUALITY_GATE_STATUS).createNoValue()));
192     when(measureRepository.getBaseMeasure(PROJECT_COMPONENT, alertStatusMetric)).thenReturn(of(Measure.newMeasureBuilder().setQualityGateStatus(OK_QUALITY_GATE_STATUS).createNoValue()));
193
194     underTest.execute();
195
196     verify(measureRepository).getRawMeasure(PROJECT_COMPONENT, alertStatusMetric);
197     verify(measureRepository).getBaseMeasure(PROJECT_COMPONENT, alertStatusMetric);
198     verifyNoMoreInteractions(measureRepository, eventRepository);
199   }
200
201   @Test
202   public void event_created_if_base_ALERT_STATUS_measure_exists_and_status_has_changed() {
203     verify_event_created_if_base_ALERT_STATUS_measure_exists_and_status_has_changed(OK, WARN_QUALITY_GATE_STATUS, "Orange (was Green)");
204     verify_event_created_if_base_ALERT_STATUS_measure_exists_and_status_has_changed(OK, ERROR_QUALITY_GATE_STATUS, "Red (was Green)");
205     verify_event_created_if_base_ALERT_STATUS_measure_exists_and_status_has_changed(WARN, OK_QUALITY_GATE_STATUS, "Green (was Orange)");
206     verify_event_created_if_base_ALERT_STATUS_measure_exists_and_status_has_changed(WARN, ERROR_QUALITY_GATE_STATUS, "Red (was Orange)");
207     verify_event_created_if_base_ALERT_STATUS_measure_exists_and_status_has_changed(ERROR, OK_QUALITY_GATE_STATUS, "Green (was Red)");
208     verify_event_created_if_base_ALERT_STATUS_measure_exists_and_status_has_changed(ERROR, WARN_QUALITY_GATE_STATUS, "Orange (was Red)");
209   }
210
211   private void verify_event_created_if_base_ALERT_STATUS_measure_exists_and_status_has_changed(Measure.Level previousAlertStatus,
212     QualityGateStatus newQualityGateStatus, String expectedLabel) {
213     when(measureRepository.getRawMeasure(PROJECT_COMPONENT, alertStatusMetric)).thenReturn(of(Measure.newMeasureBuilder().setQualityGateStatus(newQualityGateStatus).createNoValue()));
214     when(measureRepository.getBaseMeasure(PROJECT_COMPONENT, alertStatusMetric)).thenReturn(
215       of(Measure.newMeasureBuilder().setQualityGateStatus(new QualityGateStatus(previousAlertStatus)).createNoValue()));
216
217     underTest.execute();
218
219     verify(measureRepository).getRawMeasure(PROJECT_COMPONENT, alertStatusMetric);
220     verify(measureRepository).getBaseMeasure(PROJECT_COMPONENT, alertStatusMetric);
221     verify(eventRepository).add(eq(PROJECT_COMPONENT), eventArgumentCaptor.capture());
222     verifyNoMoreInteractions(measureRepository, eventRepository);
223
224     Event event = eventArgumentCaptor.getValue();
225     assertThat(event.getCategory()).isEqualTo(Event.Category.ALERT);
226     assertThat(event.getName()).isEqualTo(expectedLabel);
227     assertThat(event.getDescription()).isEqualTo(ALERT_TEXT);
228     assertThat(event.getData()).isNull();
229
230     verify(notificationService).deliver(notificationArgumentCaptor.capture());
231     Notification notification = notificationArgumentCaptor.getValue();
232     assertThat(notification.getType()).isEqualTo("alerts");
233     assertThat(notification.getFieldValue("projectKey")).isEqualTo(PROJECT_COMPONENT.getKey());
234     assertThat(notification.getFieldValue("projectUuid")).isEqualTo(PROJECT_COMPONENT.getUuid());
235     assertThat(notification.getFieldValue("projectName")).isEqualTo(PROJECT_COMPONENT.getName());
236     assertThat(notification.getFieldValue("alertLevel")).isEqualTo(newQualityGateStatus.getStatus().name());
237     assertThat(notification.getFieldValue("alertName")).isEqualTo(expectedLabel);
238
239     reset(measureRepository, eventRepository, notificationService);
240   }
241
242 }