3 * Copyright (C) 2009-2017 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 java.util.Arrays;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.List;
27 import java.util.Random;
28 import org.junit.Rule;
29 import org.junit.Test;
30 import org.mockito.ArgumentCaptor;
31 import org.mockito.InOrder;
32 import org.mockito.Mockito;
33 import org.sonar.api.utils.log.LogTester;
34 import org.sonar.api.utils.log.LoggerLevel;
35 import org.sonar.core.issue.DefaultIssue;
36 import org.sonar.db.component.ComponentDto;
38 import static java.util.Collections.emptyList;
39 import static java.util.Collections.singletonList;
40 import static org.assertj.core.api.Assertions.assertThat;
41 import static org.mockito.Matchers.eq;
42 import static org.mockito.Mockito.doThrow;
43 import static org.mockito.Mockito.mock;
44 import static org.mockito.Mockito.verify;
45 import static org.mockito.Mockito.verifyZeroInteractions;
47 public class QGChangeEventListenersImplTest {
49 public LogTester logTester = new LogTester();
51 private QGChangeEventListener listener1 = mock(QGChangeEventListener.class);
52 private QGChangeEventListener listener2 = mock(QGChangeEventListener.class);
53 private QGChangeEventListener listener3 = mock(QGChangeEventListener.class);
54 private QGChangeEventFactory.IssueChangeData issueChangeData = new QGChangeEventFactory.IssueChangeData(singletonList(new DefaultIssue()), singletonList(new ComponentDto()));
55 private InOrder inOrder = Mockito.inOrder(listener1, listener2, listener3);
56 private List<QGChangeEvent> threeChangeEvents = Arrays.asList(mock(QGChangeEvent.class), mock(QGChangeEvent.class));
58 private QGChangeEventListenersImpl underTest = new QGChangeEventListenersImpl(new QGChangeEventListener[] {listener1, listener2, listener3});
61 public void broadcastOnIssueChange_has_no_effect_when_issueChangeData_is_empty() {
62 QGChangeEventFactory.IssueChangeData issueChangeData = new QGChangeEventFactory.IssueChangeData(emptyList(), emptyList());
64 underTest.broadcastOnIssueChange(issueChangeData, threeChangeEvents);
66 verifyZeroInteractions(listener1, listener2, listener3);
70 public void broadcastOnIssueChange_has_no_effect_when_issueChangeData_has_no_issue() {
71 QGChangeEventFactory.IssueChangeData issueChangeData = new QGChangeEventFactory.IssueChangeData(emptyList(), singletonList(new ComponentDto()));
73 underTest.broadcastOnIssueChange(issueChangeData, threeChangeEvents);
75 verifyZeroInteractions(listener1, listener2, listener3);
79 public void broadcastOnIssueChange_has_no_effect_when_issueChangeData_has_no_component() {
80 QGChangeEventFactory.IssueChangeData issueChangeData = new QGChangeEventFactory.IssueChangeData(singletonList(new DefaultIssue()), emptyList());
82 underTest.broadcastOnIssueChange(issueChangeData, threeChangeEvents);
84 verifyZeroInteractions(listener1, listener2, listener3);
88 public void broadcastOnIssueChange_has_no_effect_when_no_changeEvent() {
90 underTest.broadcastOnIssueChange(issueChangeData, Collections.emptySet());
92 verifyZeroInteractions(listener1, listener2, listener3);
96 public void broadcastOnIssueChange_passes_Trigger_and_collection_to_all_listeners_in_order_of_addition_to_constructor() {
97 underTest.broadcastOnIssueChange(issueChangeData, threeChangeEvents);
99 inOrder.verify(listener1).onChanges(Trigger.ISSUE_CHANGE, threeChangeEvents);
100 inOrder.verify(listener2).onChanges(Trigger.ISSUE_CHANGE, threeChangeEvents);
101 inOrder.verify(listener3).onChanges(Trigger.ISSUE_CHANGE, threeChangeEvents);
102 inOrder.verifyNoMoreInteractions();
106 public void broadcastOnIssueChange_calls_all_listeners_even_if_one_throws_an_exception() {
107 QGChangeEventListener failingListener = new QGChangeEventListener[] {listener1, listener2, listener3}[new Random().nextInt(3)];
108 doThrow(new RuntimeException("Faking an exception thrown by onChanges"))
109 .when(failingListener)
110 .onChanges(Trigger.ISSUE_CHANGE, threeChangeEvents);
112 underTest.broadcastOnIssueChange(issueChangeData, threeChangeEvents);
114 inOrder.verify(listener1).onChanges(Trigger.ISSUE_CHANGE, threeChangeEvents);
115 inOrder.verify(listener2).onChanges(Trigger.ISSUE_CHANGE, threeChangeEvents);
116 inOrder.verify(listener3).onChanges(Trigger.ISSUE_CHANGE, threeChangeEvents);
117 inOrder.verifyNoMoreInteractions();
118 assertThat(logTester.logs()).hasSize(4);
119 assertThat(logTester.logs(LoggerLevel.WARN)).hasSize(1);
123 public void broadcastOnIssueChange_stops_calling_listeners_when_one_throws_an_ERROR() {
124 doThrow(new Error("Faking an error thrown by a listener"))
126 .onChanges(Trigger.ISSUE_CHANGE, threeChangeEvents);
128 underTest.broadcastOnIssueChange(issueChangeData, threeChangeEvents);
130 inOrder.verify(listener1).onChanges(Trigger.ISSUE_CHANGE, threeChangeEvents);
131 inOrder.verify(listener2).onChanges(Trigger.ISSUE_CHANGE, threeChangeEvents);
132 inOrder.verifyNoMoreInteractions();
133 assertThat(logTester.logs()).hasSize(3);
134 assertThat(logTester.logs(LoggerLevel.WARN)).hasSize(1);
138 public void broadcastOnIssueChange_logs_each_listener_call_at_TRACE_level() {
139 underTest.broadcastOnIssueChange(issueChangeData, threeChangeEvents);
141 assertThat(logTester.logs()).hasSize(3);
142 List<String> traceLogs = logTester.logs(LoggerLevel.TRACE);
143 assertThat(traceLogs).hasSize(3)
145 "calling onChange() on listener " + listener1.getClass().getName() + " for events " + threeChangeEvents.toString() + "...",
146 "calling onChange() on listener " + listener2.getClass().getName() + " for events " + threeChangeEvents.toString() + "...",
147 "calling onChange() on listener " + listener3.getClass().getName() + " for events " + threeChangeEvents.toString() + "...");
151 public void broadcastOnIssueChange_passes_immutable_list_of_events() {
152 QGChangeEventListenersImpl underTest = new QGChangeEventListenersImpl(new QGChangeEventListener[] {listener1});
154 underTest.broadcastOnIssueChange(issueChangeData, threeChangeEvents);
156 ArgumentCaptor<Collection> collectionCaptor = ArgumentCaptor.forClass(Collection.class);
157 verify(listener1).onChanges(eq(Trigger.ISSUE_CHANGE), collectionCaptor.capture());
158 assertThat(collectionCaptor.getValue()).isInstanceOf(ImmutableList.class);
162 public void broadcastOnIssueChange_has_no_effect_when_no_listener() {
163 QGChangeEventListenersImpl underTest = new QGChangeEventListenersImpl();
165 underTest.broadcastOnIssueChange(issueChangeData, threeChangeEvents);
167 verifyZeroInteractions(listener1, listener2, listener3);