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.Plugin;
7 import com.atlassian.plugin.eventlistener.descriptors.EventListenerModuleDescriptor;
8 import com.atlassian.plugin.module.ModuleFactory;
9 import com.atlassian.plugin.scope.ScopeManager;
10 import org.dom4j.Element;
11 import org.junit.Before;
12 import org.junit.Test;
13 import org.junit.runner.RunWith;
14 import org.mockito.Mock;
15 import org.mockito.junit.MockitoJUnitRunner;
16
17 import java.util.Collections;
18 import java.util.Optional;
19
20 import static java.util.Collections.singleton;
21 import static org.mockito.ArgumentMatchers.any;
22 import static org.mockito.ArgumentMatchers.anyString;
23 import static org.mockito.ArgumentMatchers.isNull;
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(isNull(), any())).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 registerListenerViaModuleWithScope(new UnscopedListener(op), Optional.empty());
121
122 eventPublisher.publish(event);
123
124 verify(op).run();
125 }
126
127 @Test
128 public final void verifyUnscopedListenersRunWhenNoScopesAreNotActive() {
129 eventPublisher.register(new UnscopedListener(op));
130
131 eventPublisher.publish(event);
132
133 verify(op).run();
134 }
135
136 @Test
137 public final void verifyUnscopedListenersNotRunWhenNoScopesAreActive() {
138 when(scopeManager.isScopeActive(anyString())).thenReturn(false);
139
140 eventPublisher.register(new ScopedListener(op));
141
142 eventPublisher.publish(event);
143
144 verify(op, never()).run();
145 }
146
147 @Test
148 public final void verifyUnscopedListenersRunWhenScopesAreActive() {
149 when(scopeManager.isScopeActive(anyString())).thenReturn(true);
150
151 eventPublisher.register(new ScopedListener(op));
152
153 eventPublisher.publish(event);
154
155 verify(op).run();
156 }
157
158 @Test(expected = IllegalArgumentException.class)
159 public final void verifyScopedOverrideFails() {
160 registerListenerViaModuleWithScope(new ScopedListener(op), Optional.of("not-atlassian-event"));
161
162 eventPublisher.publish(event);
163
164 verify(op, never()).run();
165 }
166
167 @Test
168 public final void verifyScopedOverrideOkWhenScopeIsTheSame() {
169 when(scopeManager.isScopeActive(anyString())).thenReturn(true);
170
171 registerListenerViaModuleWithScope(new ScopedListener(op), Optional.of("atlassian-event"));
172
173 eventPublisher.publish(event);
174
175 verify(op).run();
176 }
177 }