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