3 * Copyright (C) 2009-2019 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.qualitygate.changeevent;
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;
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;
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;
64 public class QGChangeEventListenersImplTest {
66 public LogTester logTester = new LogTester();
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);
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);
79 private InOrder inOrder = Mockito.inOrder(listener1, listener2, listener3);
81 private QGChangeEventListenersImpl underTest = new QGChangeEventListenersImpl(new QGChangeEventListener[] {listener1, listener2, listener3});
84 public void broadcastOnIssueChange_has_no_effect_when_issues_are_empty() {
85 underTest.broadcastOnIssueChange(emptyList(), singletonList(component1QGChangeEvent));
87 verifyZeroInteractions(listener1, listener2, listener3);
91 public void broadcastOnIssueChange_has_no_effect_when_no_changeEvent() {
92 underTest.broadcastOnIssueChange(oneIssueOnComponent1, emptySet());
94 verifyZeroInteractions(listener1, listener2, listener3);
98 public void broadcastOnIssueChange_passes_same_arguments_to_all_listeners_in_order_of_addition_to_constructor() {
99 underTest.broadcastOnIssueChange(oneIssueOnComponent1, singletonList(component1QGChangeEvent));
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();
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());
116 underTest.broadcastOnIssueChange(oneIssueOnComponent1, singletonList(component1QGChangeEvent));
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);
129 public void broadcastOnIssueChange_stops_calling_listeners_when_one_throws_an_ERROR() {
130 doThrow(new Error("Faking an error thrown by a listener"))
132 .onIssueChanges(any(), any());
134 underTest.broadcastOnIssueChange(oneIssueOnComponent1, singletonList(component1QGChangeEvent));
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);
146 public void broadcastOnIssueChange_logs_each_listener_call_at_TRACE_level() {
147 underTest.broadcastOnIssueChange(oneIssueOnComponent1, singletonList(component1QGChangeEvent));
149 assertThat(logTester.logs()).hasSize(3);
150 List<String> traceLogs = logTester.logs(LoggerLevel.TRACE);
151 assertThat(traceLogs).hasSize(3)
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() + "...");
159 public void broadcastOnIssueChange_passes_immutable_set_of_ChangedIssues() {
160 QGChangeEventListenersImpl underTest = new QGChangeEventListenersImpl(new QGChangeEventListener[] {listener1});
162 underTest.broadcastOnIssueChange(oneIssueOnComponent1, singletonList(component1QGChangeEvent));
164 ArgumentCaptor<Set<ChangedIssue>> changedIssuesCaptor = newSetCaptor();
165 inOrder.verify(listener1).onIssueChanges(same(component1QGChangeEvent), changedIssuesCaptor.capture());
166 assertThat(changedIssuesCaptor.getValue()).isInstanceOf(ImmutableSet.class);
170 public void broadcastOnIssueChange_has_no_effect_when_no_listener() {
171 QGChangeEventListenersImpl underTest = new QGChangeEventListenersImpl();
173 underTest.broadcastOnIssueChange(oneIssueOnComponent1, singletonList(component1QGChangeEvent));
175 verifyZeroInteractions(listener1, listener2, listener3);
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);
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)};
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)};
195 // component 5 has no QGChangeEvent but one issue
196 ComponentDto component5 = newComponentDto(component1Uuid + "5");
197 DefaultIssue component5Issue = newDefaultIssue(component5.uuid());
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))
206 .collect(Collectors.toList());
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))
215 .collect(Collectors.toList());
217 underTest.broadcastOnIssueChange(changedIssues, randomizedList(qgChangeEvents));
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));
227 verifyNoMoreInteractions(listener1, listener2, listener3);
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");
238 ChangedIssue changedIssue = new ChangedIssueImpl(defaultIssue);
240 assertThat(changedIssue.isNotClosed()).isTrue();
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");
251 ChangedIssue changedIssue = new ChangedIssueImpl(defaultIssue);
253 assertThat(changedIssue.isNotClosed()).isFalse();
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);
268 ChangedIssueImpl.statusOf(new DefaultIssue().setStatus(Issue.STATUS_CLOSED));
269 fail("Expected exception");
270 } catch (Exception e) {
271 assertThat(e).hasMessage("Unexpected status: CLOSED");
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");
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");
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);
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;
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)]);
316 private static String[] possibleResolutions(String status) {
318 case Issue.STATUS_RESOLVED:
319 return new String[] {Issue.RESOLUTION_FALSE_POSITIVE, Issue.RESOLUTION_WONT_FIX};
321 return new String[0];
325 private static ComponentDto newComponentDto(String uuid) {
326 ComponentDto componentDto = new ComponentDto();
327 componentDto.setUuid(uuid);
331 private static QGChangeEvent newQGChangeEvent(ComponentDto componentDto) {
332 QGChangeEvent res = mock(QGChangeEvent.class);
333 when(res.getProject()).thenReturn(componentDto);
337 private static <T> ArgumentCaptor<Set<T>> newSetCaptor() {
338 Class<Set<T>> clazz = (Class<Set<T>>) (Class) Set.class;
339 return ArgumentCaptor.forClass(clazz);
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);