]> source.dussan.org Git - sonarqube.git/blob
f0d821eb93b74d75b50fc8b019ef8b9411f15bb3
[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.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;
42
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;
54
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())
58     .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);
64
65   @Rule
66   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
67
68   @Rule
69   public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
70
71   private ArgumentCaptor<Event> eventArgumentCaptor = ArgumentCaptor.forClass(Event.class);
72   private ArgumentCaptor<Notification> notificationArgumentCaptor = ArgumentCaptor.forClass(Notification.class);
73
74   private Metric alertStatusMetric = mock(Metric.class);
75
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);
81
82   @Before
83   public void setUp() {
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);
88   }
89
90   @Test
91   public void no_event_if_no_raw_ALERT_STATUS_measure() {
92     when(measureRepository.getRawMeasure(PROJECT_COMPONENT, alertStatusMetric)).thenReturn(Optional.<Measure>absent());
93
94     underTest.execute();
95
96     verify(measureRepository).getRawMeasure(PROJECT_COMPONENT, alertStatusMetric);
97     verifyNoMoreInteractions(measureRepository, eventRepository);
98   }
99
100   @Test
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()));
103
104     underTest.execute();
105
106     verify(measureRepository).getRawMeasure(PROJECT_COMPONENT, alertStatusMetric);
107     verifyNoMoreInteractions(measureRepository, eventRepository);
108   }
109
110   private static Optional<Measure> of(Measure measure) {
111     return Optional.of((Measure) measure);
112   }
113
114   @Test
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)));
117
118     underTest.execute();
119
120     verify(measureRepository).getRawMeasure(PROJECT_COMPONENT, alertStatusMetric);
121     verifyNoMoreInteractions(measureRepository, eventRepository);
122   }
123
124   @Test
125   public void no_event_created_if_no_base_ALERT_STATUS_and_raw_is_OK() {
126     QualityGateStatus someQGStatus = new QualityGateStatus(Measure.Level.OK);
127
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()));
130
131     underTest.execute();
132
133     verify(measureRepository).getRawMeasure(PROJECT_COMPONENT, alertStatusMetric);
134     verify(measureRepository).getBaseMeasure(PROJECT_COMPONENT, alertStatusMetric);
135     verifyNoMoreInteractions(measureRepository, eventRepository);
136   }
137
138   @Test
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");
141   }
142
143   @Test
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");
146   }
147
148   @Test
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");
151   }
152
153   @Test
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");
156   }
157
158   @Test
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");
161   }
162
163   @Test
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");
166   }
167
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);
170
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()));
173
174     underTest.execute();
175
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);
180
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();
186
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);
197   }
198
199   @Test
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()));
203
204     underTest.execute();
205
206     verify(measureRepository).getRawMeasure(PROJECT_COMPONENT, alertStatusMetric);
207     verify(measureRepository).getBaseMeasure(PROJECT_COMPONENT, alertStatusMetric);
208     verifyNoMoreInteractions(measureRepository, eventRepository);
209   }
210
211   @Test
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)");
219   }
220
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()));
226
227     underTest.execute();
228
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);
233
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();
239
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);
250
251     reset(measureRepository, eventRepository, notificationService);
252   }
253
254   @Test
255   public void verify_branch_name_is_set_in_notification_when_not_main() {
256     String branchName = "feature1";
257     analysisMetadataHolder.setBranch(new DefaultBranchImpl(branchName) {
258       @Override
259       public boolean isMain() {
260         return false;
261       }
262     });
263
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()));
267
268     underTest.execute();
269
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);
278
279     reset(measureRepository, eventRepository, notificationService);
280   }
281
282   @Test
283   public void verify_branch_name_is_not_set_in_notification_when_main() {
284     analysisMetadataHolder.setBranch(new DefaultBranchImpl());
285
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()));
289
290     underTest.execute();
291
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);
300
301     reset(measureRepository, eventRepository, notificationService);
302   }
303
304 }