1 package com.atlassian.event.internal;
2
3
4 import com.atlassian.event.api.EventListener;
5 import com.atlassian.event.api.EventPublisher;
6 import com.atlassian.plugin.ModuleDescriptor;
7 import com.atlassian.plugin.Plugin;
8 import com.atlassian.plugin.eventlistener.descriptors.EventListenerModuleDescriptor;
9 import com.atlassian.plugin.module.ModuleFactory;
10 import com.atlassian.plugin.scope.ScopeManager;
11 import org.dom4j.Element;
12 import org.junit.Before;
13 import org.junit.Test;
14 import org.junit.runner.RunWith;
15 import org.mockito.Mock;
16 import org.mockito.runners.MockitoJUnitRunner;
17
18 import java.util.Collections;
19 import java.util.Optional;
20
21 import static java.util.Collections.singleton;
22 import static org.mockito.Matchers.any;
23 import static org.mockito.Matchers.anyString;
24 import static org.mockito.Mockito.never;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
27
28 @RunWith(MockitoJUnitRunner.class)
29 public class EventPublisherImplScopeTest {
30
31 @Mock
32 Object event;
33
34 @Mock
35 Runnable op;
36
37 @Mock
38 Plugin licensedPlugin;
39
40 @Mock
41 Element element;
42
43 @Mock
44 ScopeManager scopeManager;
45
46 @Mock
47 ModuleFactory moduleFactory;
48
49 private static class UnscopedListener {
50 final Runnable runnable;
51
52 UnscopedListener(Runnable runnable) {
53 this.runnable = runnable;
54 }
55
56 @EventListener
57 public void onObjectEvent(final Object event) {
58 runnable.run();
59 }
60 }
61
62 private static class ScopedListener {
63 final Runnable runnable;
64
65 ScopedListener(Runnable runnable) {
66 this.runnable = runnable;
67 }
68
69 @EventListener(scope = "atlassian-event")
70 public void onObjectEvent(final Object event) {
71 runnable.run();
72 }
73 }
74
75 private EventPublisher eventPublisher;
76
77 @Before
78 public final void setUp() {
79 eventPublisher = new EventPublisherImpl(new StubEventDispatcher(),
80 () -> Collections.singletonList(new AnnotatedMethodsListenerHandler())
81 , scopeManager);
82 }
83
84 private void registerListenerViaModuleWithScope(Object listener, Optional<String> scope) {
85 when(moduleFactory.createModule(anyString(), any(ModuleDescriptor.class))).thenReturn(listener);
86 final EventListenerModuleDescriptor licensedDescriptor = new EventListenerModuleDescriptor(moduleFactory, eventPublisher);
87 when(licensedPlugin.getActivePermissions()).thenReturn(singleton("execute_java"));
88
89 when(licensedPlugin.getScopeKey()).thenReturn(scope);
90
91 licensedDescriptor.init(licensedPlugin, element);
92
93 eventPublisher.register(licensedDescriptor);
94 }
95
96 @Test
97 public final void testScopedModuleDescriptorIfNoActiveLicense() {
98 when(scopeManager.isScopeActive(anyString())).thenReturn(false);
99
100 registerListenerViaModuleWithScope(new UnscopedListener(op), Optional.of("events"));
101
102 eventPublisher.publish(event);
103
104 verify(op, never()).run();
105 }
106
107 @Test
108 public final void testScopedModuleDescriptorWithActiveLicense() {
109 when(scopeManager.isScopeActive(anyString())).thenReturn(true);
110
111 registerListenerViaModuleWithScope(new UnscopedListener(op), Optional.of("events"));
112
113 eventPublisher.publish(event);
114
115 verify(op).run();
116 }
117
118 @Test
119 public final void testUnScopedModuleDescriptorWithNoActiveLicense() {
120 when(scopeManager.isScopeActive(anyString())).thenReturn(false);
121
122 registerListenerViaModuleWithScope(new UnscopedListener(op), Optional.empty());
123
124 eventPublisher.publish(event);
125
126 verify(op).run();
127 }
128
129 @Test
130 public final void verifyUnscopedListenersRunWhenNoScopesAreNotActive() {
131 when(scopeManager.isScopeActive(anyString())).thenReturn(false);
132
133 eventPublisher.register(new UnscopedListener(op));
134
135 eventPublisher.publish(event);
136
137 verify(op).run();
138 }
139
140 @Test
141 public final void verifyUnscopedListenersNotRunWhenNoScopesAreActive() {
142 when(scopeManager.isScopeActive(anyString())).thenReturn(false);
143
144 eventPublisher.register(new ScopedListener(op));
145
146 eventPublisher.publish(event);
147
148 verify(op, never()).run();
149 }
150
151 @Test
152 public final void verifyUnscopedListenersRunWhenScopesAreActive() {
153 when(scopeManager.isScopeActive(anyString())).thenReturn(true);
154
155 eventPublisher.register(new ScopedListener(op));
156
157 eventPublisher.publish(event);
158
159 verify(op).run();
160 }
161
162 @Test(expected = IllegalArgumentException.class)
163 public final void verifyScopedOverrideFails() {
164 when(scopeManager.isScopeActive(anyString())).thenReturn(false);
165
166 registerListenerViaModuleWithScope(new ScopedListener(op), Optional.of("not-atlassian-event"));
167
168 eventPublisher.publish(event);
169
170 verify(op, never()).run();
171 }
172
173 @Test
174 public final void verifyScopedOverrideOkWhenScopeIsTheSame() {
175 when(scopeManager.isScopeActive(anyString())).thenReturn(true);
176
177 registerListenerViaModuleWithScope(new ScopedListener(op), Optional.of("atlassian-event"));
178
179 eventPublisher.publish(event);
180
181 verify(op).run();
182 }
183 }