]> source.dussan.org Git - sonarqube.git/blob
73ca7f593f904a4a599c1defef4863408282a511
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2019 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.qualitygate.changeevent;
21
22 import com.google.common.collect.ImmutableList;
23 import com.google.common.collect.ImmutableSet;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.Collections;
27 import java.util.List;
28 import java.util.Random;
29 import java.util.Set;
30 import java.util.stream.Collectors;
31 import java.util.stream.Stream;
32 import org.apache.commons.lang.RandomStringUtils;
33 import org.assertj.core.groups.Tuple;
34 import org.junit.Rule;
35 import org.junit.Test;
36 import org.mockito.ArgumentCaptor;
37 import org.mockito.InOrder;
38 import org.mockito.Mockito;
39 import org.sonar.api.issue.Issue;
40 import org.sonar.api.rules.RuleType;
41 import org.sonar.api.utils.log.LogTester;
42 import org.sonar.api.utils.log.LoggerLevel;
43 import org.sonar.core.issue.DefaultIssue;
44 import org.sonar.db.component.ComponentDto;
45 import org.sonar.server.qualitygate.changeevent.QGChangeEventListener.ChangedIssue;
46 import org.sonar.server.qualitygate.changeevent.QGChangeEventListenersImpl.ChangedIssueImpl;
47
48 import static java.util.Arrays.asList;
49 import static java.util.Collections.emptyList;
50 import static java.util.Collections.emptySet;
51 import static java.util.Collections.singletonList;
52 import static org.assertj.core.api.Assertions.assertThat;
53 import static org.assertj.core.api.Assertions.tuple;
54 import static org.junit.Assert.fail;
55 import static org.mockito.ArgumentMatchers.any;
56 import static org.mockito.ArgumentMatchers.same;
57 import static org.mockito.Mockito.doThrow;
58 import static org.mockito.Mockito.mock;
59 import static org.mockito.Mockito.verify;
60 import static org.mockito.Mockito.verifyNoMoreInteractions;
61 import static org.mockito.Mockito.verifyZeroInteractions;
62 import static org.mockito.Mockito.when;
63
64 public class QGChangeEventListenersImplTest {
65   @Rule
66   public LogTester logTester = new LogTester();
67
68   private QGChangeEventListener listener1 = mock(QGChangeEventListener.class);
69   private QGChangeEventListener listener2 = mock(QGChangeEventListener.class);
70   private QGChangeEventListener listener3 = mock(QGChangeEventListener.class);
71   private List<QGChangeEventListener> listeners = Arrays.asList(listener1, listener2, listener3);
72
73   private String component1Uuid = RandomStringUtils.randomAlphabetic(6);
74   private ComponentDto component1 = newComponentDto(component1Uuid);
75   private DefaultIssue component1Issue = newDefaultIssue(component1Uuid);
76   private List<DefaultIssue> oneIssueOnComponent1 = singletonList(component1Issue);
77   private QGChangeEvent component1QGChangeEvent = newQGChangeEvent(component1);
78
79   private InOrder inOrder = Mockito.inOrder(listener1, listener2, listener3);
80
81   private QGChangeEventListenersImpl underTest = new QGChangeEventListenersImpl(new QGChangeEventListener[] {listener1, listener2, listener3});
82
83   @Test
84   public void broadcastOnIssueChange_has_no_effect_when_issues_are_empty() {
85     underTest.broadcastOnIssueChange(emptyList(), singletonList(component1QGChangeEvent));
86
87     verifyZeroInteractions(listener1, listener2, listener3);
88   }
89
90   @Test
91   public void broadcastOnIssueChange_has_no_effect_when_no_changeEvent() {
92     underTest.broadcastOnIssueChange(oneIssueOnComponent1, emptySet());
93
94     verifyZeroInteractions(listener1, listener2, listener3);
95   }
96
97   @Test
98   public void broadcastOnIssueChange_passes_same_arguments_to_all_listeners_in_order_of_addition_to_constructor() {
99     underTest.broadcastOnIssueChange(oneIssueOnComponent1, singletonList(component1QGChangeEvent));
100
101     ArgumentCaptor<Set<ChangedIssue>> changedIssuesCaptor = newSetCaptor();
102     inOrder.verify(listener1).onIssueChanges(same(component1QGChangeEvent), changedIssuesCaptor.capture());
103     Set<ChangedIssue> changedIssues = changedIssuesCaptor.getValue();
104     inOrder.verify(listener2).onIssueChanges(same(component1QGChangeEvent), same(changedIssues));
105     inOrder.verify(listener3).onIssueChanges(same(component1QGChangeEvent), same(changedIssues));
106     inOrder.verifyNoMoreInteractions();
107   }
108
109   @Test
110   public void broadcastOnIssueChange_calls_all_listeners_even_if_one_throws_an_exception() {
111     QGChangeEventListener failingListener = new QGChangeEventListener[] {listener1, listener2, listener3}[new Random().nextInt(3)];
112     doThrow(new RuntimeException("Faking an exception thrown by onChanges"))
113       .when(failingListener)
114       .onIssueChanges(any(), any());
115
116     underTest.broadcastOnIssueChange(oneIssueOnComponent1, singletonList(component1QGChangeEvent));
117
118     ArgumentCaptor<Set<ChangedIssue>> changedIssuesCaptor = newSetCaptor();
119     inOrder.verify(listener1).onIssueChanges(same(component1QGChangeEvent), changedIssuesCaptor.capture());
120     Set<ChangedIssue> changedIssues = changedIssuesCaptor.getValue();
121     inOrder.verify(listener2).onIssueChanges(same(component1QGChangeEvent), same(changedIssues));
122     inOrder.verify(listener3).onIssueChanges(same(component1QGChangeEvent), same(changedIssues));
123     inOrder.verifyNoMoreInteractions();
124     assertThat(logTester.logs()).hasSize(4);
125     assertThat(logTester.logs(LoggerLevel.WARN)).hasSize(1);
126   }
127
128   @Test
129   public void broadcastOnIssueChange_stops_calling_listeners_when_one_throws_an_ERROR() {
130     doThrow(new Error("Faking an error thrown by a listener"))
131       .when(listener2)
132       .onIssueChanges(any(), any());
133
134     underTest.broadcastOnIssueChange(oneIssueOnComponent1, singletonList(component1QGChangeEvent));
135
136     ArgumentCaptor<Set<ChangedIssue>> changedIssuesCaptor = newSetCaptor();
137     inOrder.verify(listener1).onIssueChanges(same(component1QGChangeEvent), changedIssuesCaptor.capture());
138     Set<ChangedIssue> changedIssues = changedIssuesCaptor.getValue();
139     inOrder.verify(listener2).onIssueChanges(same(component1QGChangeEvent), same(changedIssues));
140     inOrder.verifyNoMoreInteractions();
141     assertThat(logTester.logs()).hasSize(3);
142     assertThat(logTester.logs(LoggerLevel.WARN)).hasSize(1);
143   }
144
145   @Test
146   public void broadcastOnIssueChange_logs_each_listener_call_at_TRACE_level() {
147     underTest.broadcastOnIssueChange(oneIssueOnComponent1, singletonList(component1QGChangeEvent));
148
149     assertThat(logTester.logs()).hasSize(3);
150     List<String> traceLogs = logTester.logs(LoggerLevel.TRACE);
151     assertThat(traceLogs).hasSize(3)
152       .containsOnly(
153         "calling onChange() on listener " + listener1.getClass().getName() + " for events " + component1QGChangeEvent.toString() + "...",
154         "calling onChange() on listener " + listener2.getClass().getName() + " for events " + component1QGChangeEvent.toString() + "...",
155         "calling onChange() on listener " + listener3.getClass().getName() + " for events " + component1QGChangeEvent.toString() + "...");
156   }
157
158   @Test
159   public void broadcastOnIssueChange_passes_immutable_set_of_ChangedIssues() {
160     QGChangeEventListenersImpl underTest = new QGChangeEventListenersImpl(new QGChangeEventListener[] {listener1});
161
162     underTest.broadcastOnIssueChange(oneIssueOnComponent1, singletonList(component1QGChangeEvent));
163
164     ArgumentCaptor<Set<ChangedIssue>> changedIssuesCaptor = newSetCaptor();
165     inOrder.verify(listener1).onIssueChanges(same(component1QGChangeEvent), changedIssuesCaptor.capture());
166     assertThat(changedIssuesCaptor.getValue()).isInstanceOf(ImmutableSet.class);
167   }
168
169   @Test
170   public void broadcastOnIssueChange_has_no_effect_when_no_listener() {
171     QGChangeEventListenersImpl underTest = new QGChangeEventListenersImpl();
172
173     underTest.broadcastOnIssueChange(oneIssueOnComponent1, singletonList(component1QGChangeEvent));
174
175     verifyZeroInteractions(listener1, listener2, listener3);
176   }
177
178   @Test
179   public void broadcastOnIssueChange_calls_listener_for_each_component_uuid_with_at_least_one_QGChangeEvent() {
180     // component2 has multiple issues
181     ComponentDto component2 = newComponentDto(component1Uuid + "2");
182     DefaultIssue[] component2Issues = {newDefaultIssue(component2.uuid()), newDefaultIssue(component2.uuid())};
183     QGChangeEvent component2QGChangeEvent = newQGChangeEvent(component2);
184
185     // component 3 has multiple QGChangeEvent and only one issue
186     ComponentDto component3 = newComponentDto(component1Uuid + "3");
187     DefaultIssue component3Issue = newDefaultIssue(component3.uuid());
188     QGChangeEvent[] component3QGChangeEvents = {newQGChangeEvent(component3), newQGChangeEvent(component3)};
189
190     // component 4 has multiple QGChangeEvent and multiples issues
191     ComponentDto component4 = newComponentDto(component1Uuid + "4");
192     DefaultIssue[] component4Issues = {newDefaultIssue(component4.uuid()), newDefaultIssue(component4.uuid())};
193     QGChangeEvent[] component4QGChangeEvents = {newQGChangeEvent(component4), newQGChangeEvent(component4)};
194
195     // component 5 has no QGChangeEvent but one issue
196     ComponentDto component5 = newComponentDto(component1Uuid + "5");
197     DefaultIssue component5Issue = newDefaultIssue(component5.uuid());
198
199     List<DefaultIssue> issues = Stream.of(
200       Stream.of(component1Issue),
201       Arrays.stream(component2Issues),
202       Stream.of(component3Issue),
203       Arrays.stream(component4Issues),
204       Stream.of(component5Issue))
205       .flatMap(s -> s)
206       .collect(Collectors.toList());
207
208     List<DefaultIssue> changedIssues = randomizedList(issues);
209     List<QGChangeEvent> qgChangeEvents = Stream.of(
210       Stream.of(component1QGChangeEvent),
211       Stream.of(component2QGChangeEvent),
212       Arrays.stream(component3QGChangeEvents),
213       Arrays.stream(component4QGChangeEvents))
214       .flatMap(s -> s)
215       .collect(Collectors.toList());
216
217     underTest.broadcastOnIssueChange(changedIssues, randomizedList(qgChangeEvents));
218
219     listeners.forEach(listener -> {
220       verifyListenerCalled(listener, component1QGChangeEvent, component1Issue);
221       verifyListenerCalled(listener, component2QGChangeEvent, component2Issues);
222       Arrays.stream(component3QGChangeEvents)
223         .forEach(component3QGChangeEvent -> verifyListenerCalled(listener, component3QGChangeEvent, component3Issue));
224       Arrays.stream(component4QGChangeEvents)
225         .forEach(component4QGChangeEvent -> verifyListenerCalled(listener, component4QGChangeEvent, component4Issues));
226     });
227     verifyNoMoreInteractions(listener1, listener2, listener3);
228   }
229
230   @Test
231   public void isNotClosed_returns_true_if_issue_in_one_of_opened_states() {
232     DefaultIssue defaultIssue = new DefaultIssue();
233     defaultIssue.setStatus(Issue.STATUS_REOPENED);
234     defaultIssue.setKey("abc");
235     defaultIssue.setType(RuleType.BUG);
236     defaultIssue.setSeverity("BLOCKER");
237
238     ChangedIssue changedIssue = new ChangedIssueImpl(defaultIssue);
239
240     assertThat(changedIssue.isNotClosed()).isTrue();
241   }
242
243   @Test
244   public void isNotClosed_returns_false_if_issue_in_one_of_closed_states() {
245     DefaultIssue defaultIssue = new DefaultIssue();
246     defaultIssue.setStatus(Issue.STATUS_CONFIRMED);
247     defaultIssue.setKey("abc");
248     defaultIssue.setType(RuleType.BUG);
249     defaultIssue.setSeverity("BLOCKER");
250
251     ChangedIssue changedIssue = new ChangedIssueImpl(defaultIssue);
252
253     assertThat(changedIssue.isNotClosed()).isFalse();
254   }
255
256   @Test
257   public void test_status_mapping() {
258     assertThat(ChangedIssueImpl.statusOf(new DefaultIssue().setStatus(Issue.STATUS_OPEN))).isEqualTo(QGChangeEventListener.Status.OPEN);
259     assertThat(ChangedIssueImpl.statusOf(new DefaultIssue().setStatus(Issue.STATUS_REOPENED))).isEqualTo(QGChangeEventListener.Status.REOPENED);
260     assertThat(ChangedIssueImpl.statusOf(new DefaultIssue().setStatus(Issue.STATUS_CONFIRMED))).isEqualTo(QGChangeEventListener.Status.CONFIRMED);
261     assertThat(ChangedIssueImpl.statusOf(new DefaultIssue().setStatus(Issue.STATUS_RESOLVED).setResolution(Issue.RESOLUTION_FALSE_POSITIVE)))
262       .isEqualTo(QGChangeEventListener.Status.RESOLVED_FP);
263     assertThat(ChangedIssueImpl.statusOf(new DefaultIssue().setStatus(Issue.STATUS_RESOLVED).setResolution(Issue.RESOLUTION_WONT_FIX)))
264       .isEqualTo(QGChangeEventListener.Status.RESOLVED_WF);
265     assertThat(ChangedIssueImpl.statusOf(new DefaultIssue().setStatus(Issue.STATUS_RESOLVED).setResolution(Issue.RESOLUTION_FIXED)))
266       .isEqualTo(QGChangeEventListener.Status.RESOLVED_FIXED);
267     try {
268       ChangedIssueImpl.statusOf(new DefaultIssue().setStatus(Issue.STATUS_CLOSED));
269       fail("Expected exception");
270     } catch (Exception e) {
271       assertThat(e).hasMessage("Unexpected status: CLOSED");
272     }
273     try {
274       ChangedIssueImpl.statusOf(new DefaultIssue().setStatus(Issue.STATUS_RESOLVED));
275       fail("Expected exception");
276     } catch (Exception e) {
277       assertThat(e).hasMessage("A resolved issue should have a resolution");
278     }
279     try {
280       ChangedIssueImpl.statusOf(new DefaultIssue().setStatus(Issue.STATUS_RESOLVED).setResolution(Issue.RESOLUTION_REMOVED));
281       fail("Expected exception");
282     } catch (Exception e) {
283       assertThat(e).hasMessage("Unexpected resolution for a resolved issue: REMOVED");
284     }
285   }
286
287   private void verifyListenerCalled(QGChangeEventListener listener, QGChangeEvent changeEvent, DefaultIssue... issues) {
288     ArgumentCaptor<Set<ChangedIssue>> changedIssuesCaptor = newSetCaptor();
289     verify(listener).onIssueChanges(same(changeEvent), changedIssuesCaptor.capture());
290     Set<ChangedIssue> changedIssues = changedIssuesCaptor.getValue();
291     Tuple[] expected = Arrays.stream(issues)
292       .map(issue -> tuple(issue.key(), ChangedIssueImpl.statusOf(issue), issue.type()))
293       .toArray(Tuple[]::new);
294     assertThat(changedIssues)
295       .hasSize(issues.length)
296       .extracting(ChangedIssue::getKey, ChangedIssue::getStatus, ChangedIssue::getType)
297       .containsOnly(expected);
298   }
299
300   private static final String[] POSSIBLE_STATUSES = asList(Issue.STATUS_CONFIRMED, Issue.STATUS_REOPENED, Issue.STATUS_RESOLVED).stream().toArray(String[]::new);
301   private static int issueIdCounter = 0;
302
303   private static DefaultIssue newDefaultIssue(String projectUuid) {
304     DefaultIssue defaultIssue = new DefaultIssue();
305     defaultIssue.setKey("issue_" + issueIdCounter++);
306     defaultIssue.setProjectUuid(projectUuid);
307     defaultIssue.setType(RuleType.values()[new Random().nextInt(RuleType.values().length)]);
308     defaultIssue.setStatus(POSSIBLE_STATUSES[new Random().nextInt(POSSIBLE_STATUSES.length)]);
309     String[] possibleResolutions = possibleResolutions(defaultIssue.getStatus());
310     if (possibleResolutions.length > 0) {
311       defaultIssue.setResolution(possibleResolutions[new Random().nextInt(possibleResolutions.length)]);
312     }
313     return defaultIssue;
314   }
315
316   private static String[] possibleResolutions(String status) {
317     switch (status) {
318       case Issue.STATUS_RESOLVED:
319         return new String[] {Issue.RESOLUTION_FALSE_POSITIVE, Issue.RESOLUTION_WONT_FIX};
320       default:
321         return new String[0];
322     }
323   }
324
325   private static ComponentDto newComponentDto(String uuid) {
326     ComponentDto componentDto = new ComponentDto();
327     componentDto.setUuid(uuid);
328     return componentDto;
329   }
330
331   private static QGChangeEvent newQGChangeEvent(ComponentDto componentDto) {
332     QGChangeEvent res = mock(QGChangeEvent.class);
333     when(res.getProject()).thenReturn(componentDto);
334     return res;
335   }
336
337   private static <T> ArgumentCaptor<Set<T>> newSetCaptor() {
338     Class<Set<T>> clazz = (Class<Set<T>>) (Class) Set.class;
339     return ArgumentCaptor.forClass(clazz);
340   }
341
342   private static <T> List<T> randomizedList(List<T> issues) {
343     ArrayList<T> res = new ArrayList<>(issues);
344     Collections.shuffle(res);
345     return ImmutableList.copyOf(res);
346   }
347
348 }