]> source.dussan.org Git - sonarqube.git/blob
f96602d71b19ed9ceebe0cc938fdafa87f87aed9
[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.qualitygate.changeevent;
21
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;
37
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;
46
47 public class QGChangeEventListenersImplTest {
48   @Rule
49   public LogTester logTester = new LogTester();
50
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));
57
58   private QGChangeEventListenersImpl underTest = new QGChangeEventListenersImpl(new QGChangeEventListener[] {listener1, listener2, listener3});
59
60   @Test
61   public void broadcastOnIssueChange_has_no_effect_when_issueChangeData_is_empty() {
62     QGChangeEventFactory.IssueChangeData issueChangeData = new QGChangeEventFactory.IssueChangeData(emptyList(), emptyList());
63
64     underTest.broadcastOnIssueChange(issueChangeData, threeChangeEvents);
65
66     verifyZeroInteractions(listener1, listener2, listener3);
67   }
68
69   @Test
70   public void broadcastOnIssueChange_has_no_effect_when_issueChangeData_has_no_issue() {
71     QGChangeEventFactory.IssueChangeData issueChangeData = new QGChangeEventFactory.IssueChangeData(emptyList(), singletonList(new ComponentDto()));
72
73     underTest.broadcastOnIssueChange(issueChangeData, threeChangeEvents);
74
75     verifyZeroInteractions(listener1, listener2, listener3);
76   }
77
78   @Test
79   public void broadcastOnIssueChange_has_no_effect_when_issueChangeData_has_no_component() {
80     QGChangeEventFactory.IssueChangeData issueChangeData = new QGChangeEventFactory.IssueChangeData(singletonList(new DefaultIssue()), emptyList());
81
82     underTest.broadcastOnIssueChange(issueChangeData, threeChangeEvents);
83
84     verifyZeroInteractions(listener1, listener2, listener3);
85   }
86
87   @Test
88   public void broadcastOnIssueChange_has_no_effect_when_no_changeEvent() {
89
90     underTest.broadcastOnIssueChange(issueChangeData, Collections.emptySet());
91
92     verifyZeroInteractions(listener1, listener2, listener3);
93   }
94
95   @Test
96   public void broadcastOnIssueChange_passes_Trigger_and_collection_to_all_listeners_in_order_of_addition_to_constructor() {
97     underTest.broadcastOnIssueChange(issueChangeData, threeChangeEvents);
98
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();
103   }
104
105   @Test
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);
111
112     underTest.broadcastOnIssueChange(issueChangeData, threeChangeEvents);
113
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);
120   }
121
122   @Test
123   public void broadcastOnIssueChange_stops_calling_listeners_when_one_throws_an_ERROR() {
124     doThrow(new Error("Faking an error thrown by a listener"))
125       .when(listener2)
126       .onChanges(Trigger.ISSUE_CHANGE, threeChangeEvents);
127
128     underTest.broadcastOnIssueChange(issueChangeData, threeChangeEvents);
129
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);
135   }
136
137   @Test
138   public void broadcastOnIssueChange_logs_each_listener_call_at_TRACE_level() {
139     underTest.broadcastOnIssueChange(issueChangeData, threeChangeEvents);
140
141     assertThat(logTester.logs()).hasSize(3);
142     List<String> traceLogs = logTester.logs(LoggerLevel.TRACE);
143     assertThat(traceLogs).hasSize(3)
144       .containsOnly(
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() + "...");
148   }
149
150   @Test
151   public void broadcastOnIssueChange_passes_immutable_list_of_events() {
152     QGChangeEventListenersImpl underTest = new QGChangeEventListenersImpl(new QGChangeEventListener[] {listener1});
153
154     underTest.broadcastOnIssueChange(issueChangeData, threeChangeEvents);
155
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);
159   }
160
161   @Test
162   public void broadcastOnIssueChange_has_no_effect_when_no_listener() {
163     QGChangeEventListenersImpl underTest = new QGChangeEventListenersImpl();
164
165     underTest.broadcastOnIssueChange(issueChangeData, threeChangeEvents);
166
167     verifyZeroInteractions(listener1, listener2, listener3);
168   }
169
170 }