1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.eventstream.internal

File UntypedEventListenerTest.java

 

Code metrics

0
78
13
2
286
182
13
0.17
6
6.5
1

Classes

Class Line # Actions
UntypedEventListenerTest 63 75 0% 11 0
1.0100%
UntypedEventListenerTest.MyAnswer 72 3 0% 2 0
1.0100%
 

Contributing tests

This file is covered by 8 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.eventstream.internal;
21   
22    import java.util.Arrays;
23   
24    import javax.script.ScriptContext;
25   
26    import org.junit.Before;
27    import org.junit.Rule;
28    import org.junit.Test;
29    import org.mockito.invocation.InvocationOnMock;
30    import org.mockito.stubbing.Answer;
31    import org.xwiki.bridge.event.ApplicationReadyEvent;
32    import org.xwiki.bridge.event.DocumentUpdatedEvent;
33    import org.xwiki.component.manager.ComponentManager;
34    import org.xwiki.eventstream.UntypedRecordableEventDescriptor;
35    import org.xwiki.observation.ObservationManager;
36    import org.xwiki.rendering.block.XDOM;
37    import org.xwiki.rendering.renderer.BlockRenderer;
38    import org.xwiki.rendering.renderer.printer.WikiPrinter;
39    import org.xwiki.script.ScriptContextManager;
40    import org.xwiki.template.Template;
41    import org.xwiki.template.TemplateManager;
42    import org.xwiki.test.mockito.MockitoComponentMockingRule;
43   
44    import static org.junit.Assert.assertEquals;
45    import static org.junit.Assert.assertNotNull;
46    import static org.junit.Assert.assertNull;
47    import static org.junit.Assert.assertTrue;
48    import static org.mockito.ArgumentMatchers.any;
49    import static org.mockito.ArgumentMatchers.anyString;
50    import static org.mockito.Mockito.doAnswer;
51    import static org.mockito.Mockito.mock;
52    import static org.mockito.Mockito.never;
53    import static org.mockito.Mockito.times;
54    import static org.mockito.Mockito.verify;
55    import static org.mockito.Mockito.when;
56   
57    /**
58    * Units tests for {@link UntypedEventListener}.
59    *
60    * @version $Id: 86bb823d8ffe2add9fe1f03ac5df27ebd862c28d $
61    * @since 9.6RC1
62    */
 
63    public class UntypedEventListenerTest
64    {
65    @Rule
66    public final MockitoComponentMockingRule<UntypedEventListener> mocker =
67    new MockitoComponentMockingRule<>(UntypedEventListener.class);
68   
69    /**
70    * Inner classed used to verify what is sent to the observation manager
71    */
 
72    class MyAnswer implements Answer
73    {
74    private DefaultUntypedRecordableEvent sentEvent;
75   
 
76  4 toggle @Override
77    public Object answer(InvocationOnMock invocationOnMock) throws Throwable
78    {
79  4 sentEvent = invocationOnMock.getArgument(0);
80  4 return null;
81    }
82   
 
83  16 toggle public DefaultUntypedRecordableEvent getSentEvent()
84    {
85  16 return sentEvent;
86    }
87    }
88   
89    private MyAnswer answer;
90   
91    private ObservationManager observationManager;
92   
93    private ScriptContextManager scriptContextManager;
94   
95    private ComponentManager componentManager;
96   
97    private ModelBridge modelBridge;
98   
99    private TemplateManager templateManager;
100   
101    private Template template;
102   
103    private XDOM xdom;
104   
105    private ScriptContext scriptContext;
106   
107    private BlockRenderer renderer;
108   
 
109  8 toggle @Before
110    public void setUp() throws Exception
111    {
112  8 componentManager = mocker.registerMockComponent(ComponentManager.class, "wiki");
113  8 modelBridge = mocker.registerMockComponent(ModelBridge.class);
114  8 renderer = mocker.getInstance(BlockRenderer.class, "html/5.0");
115   
116  8 scriptContextManager = mocker.registerMockComponent(ScriptContextManager.class);
117  8 scriptContext = mock(ScriptContext.class);
118  8 when(scriptContextManager.getCurrentScriptContext()).thenReturn(scriptContext);
119   
120  8 templateManager = mocker.getInstance(TemplateManager.class);
121  8 template = mock(Template.class);
122  8 xdom = mock(XDOM.class);
123  8 when(templateManager.createStringTemplate(anyString(), any())).thenReturn(template);
124  8 when(templateManager.execute(template)).thenReturn(xdom);
125    }
126   
 
127  8 toggle @Before
128    public void watchObservationManager() throws Exception
129    {
130  8 observationManager = mocker.registerMockComponent(ObservationManager.class);
131  8 answer = new MyAnswer();
132  8 doAnswer(answer).when(observationManager).notify(any(DefaultUntypedRecordableEvent.class), anyString(), any());
133    }
134   
 
135  8 toggle private UntypedRecordableEventDescriptor mockDescriptor() throws Exception
136    {
137  8 UntypedRecordableEventDescriptor descriptor = mock(UntypedRecordableEventDescriptor.class);
138  8 when(descriptor.getEventTriggers()).thenReturn(Arrays.asList(DocumentUpdatedEvent.class.getCanonicalName()));
139  8 when(descriptor.getEventType()).thenReturn("myCustomEvent");
140  8 when(modelBridge.checkXObjectPresence(any(), any())).thenReturn(true);
141  8 when(componentManager.getInstanceList(any())).thenReturn(Arrays.asList(descriptor));
142   
143  8 return descriptor;
144    }
145   
 
146  1 toggle @Test
147    public void onEventWithBlankValidationAndBlankTarget() throws Exception
148    {
149    // Mocks
150  1 mockDescriptor();
151   
152    // Test
153  1 mocker.getComponentUnderTest().onEvent(new DocumentUpdatedEvent(), mock(Object.class), null);
154   
155    // Verify
156  1 verify(this.observationManager, times(1)).notify(any(DefaultUntypedRecordableEvent.class), any(), any());
157   
158  1 assertNotNull(answer.getSentEvent());
159  1 assertTrue(answer.getSentEvent().getTarget().isEmpty());
160  1 assertEquals("myCustomEvent", answer.getSentEvent().getEventType());
161    }
162   
 
163  1 toggle @Test
164    public void onEventWithWrongEvent() throws Exception
165    {
166    // Mocks
167  1 mockDescriptor();
168   
169    // Test
170  1 mocker.getComponentUnderTest().onEvent(new ApplicationReadyEvent(), mock(Object.class), null);
171   
172    // Verify
173  1 verify(this.observationManager, never()).notify(any(), any(), any());
174  1 assertNull(answer.getSentEvent());
175    }
176   
 
177  1 toggle @Test
178    public void onEventWithoutXObject() throws Exception
179    {
180    // Mocks
181  1 mockDescriptor();
182   
183    // Same than onEventWithBlankValidationAndBlankTarget() but with no XObject
184  1 when(modelBridge.checkXObjectPresence(any(), any())).thenReturn(false);
185   
186    // Test
187  1 Object source = mock(Object.class);
188  1 mocker.getComponentUnderTest().onEvent(new DocumentUpdatedEvent(), source, null);
189   
190    // Verify
191  1 verify(this.observationManager, never()).notify(any(), any(), any());
192  1 assertNull(answer.getSentEvent());
193    }
194   
 
195  1 toggle @Test
196    public void onEventWithCorrectValidation() throws Exception
197    {
198    // Mocks
199  1 UntypedRecordableEventDescriptor descriptor = mockDescriptor();
200  1 when(descriptor.getValidationExpression()).thenReturn("someVelocityCode");
201  1 when(scriptContext.getAttribute("xreturn")).thenReturn(true);
202   
203    // Test
204  1 Object source = mock(Object.class);
205  1 mocker.getComponentUnderTest().onEvent(new DocumentUpdatedEvent(), source, null);
206   
207    // Verify
208  1 verify(this.observationManager, times(1)).notify(any(DefaultUntypedRecordableEvent.class), any(), any());
209  1 assertNotNull(answer.getSentEvent());
210  1 assertEquals("myCustomEvent", answer.getSentEvent().getEventType());
211    }
212   
 
213  1 toggle @Test
214    public void onEventWithIncorrectValidation() throws Exception
215    {
216    // Mocks
217  1 UntypedRecordableEventDescriptor descriptor = mockDescriptor();
218  1 when(descriptor.getValidationExpression()).thenReturn("someVelocityCode");
219  1 when(scriptContext.getAttribute("xreturn")).thenReturn(false);
220   
221    // Test
222  1 mocker.getComponentUnderTest().onEvent(new DocumentUpdatedEvent(), mock(Object.class), null);
223   
224    // Verify
225  1 verify(this.observationManager, never()).notify(any(), any(), any());
226  1 assertNull(answer.getSentEvent());
227    }
228   
 
229  1 toggle @Test
230    public void onEventWithCorrectValidation2() throws Exception
231    {
232    // Mocks
233  1 UntypedRecordableEventDescriptor descriptor = mockDescriptor();
234  1 when(descriptor.getValidationExpression()).thenReturn("someVelocityCode");
235  1 doAnswer(invocationOnMock -> {
236  1 WikiPrinter wikiPrinter = invocationOnMock.getArgument(1);
237  1 wikiPrinter.println(" true ");
238  1 return null;
239    }).when(renderer).render(any(XDOM.class), any(WikiPrinter.class));
240   
241    // Test
242  1 mocker.getComponentUnderTest().onEvent(new DocumentUpdatedEvent(), mock(Object.class), null);
243   
244    // Verify
245  1 verify(this.observationManager, times(1)).notify(any(DefaultUntypedRecordableEvent.class), any(), any());
246  1 assertNotNull(answer.getSentEvent());
247  1 assertEquals("myCustomEvent", answer.getSentEvent().getEventType());
248    }
249   
 
250  1 toggle @Test
251    public void onEventWithIncorrectValidation2() throws Exception
252    {
253    // Mocks
254  1 UntypedRecordableEventDescriptor descriptor = mockDescriptor();
255  1 when(descriptor.getValidationExpression()).thenReturn("someVelocityCode");
256   
257    // Test
258  1 mocker.getComponentUnderTest().onEvent(new DocumentUpdatedEvent(), mock(Object.class), null);
259   
260    // Verify
261  1 verify(this.observationManager, never()).notify(any(), any(), any());
262  1 assertNull(answer.getSentEvent());
263    }
264   
 
265  1 toggle @Test
266    public void onEventWithTarget() throws Exception
267    {
268    // Mocks
269  1 UntypedRecordableEventDescriptor descriptor = mockDescriptor();
270  1 when(descriptor.getTargetExpression()).thenReturn("someVelocityCode");
271    // Target velocity
272  1 when(scriptContext.getAttribute("xreturn")).thenReturn(Arrays.asList("UserA", "UserB"));
273   
274    // Test
275  1 mocker.getComponentUnderTest().onEvent(new DocumentUpdatedEvent(), mock(Object.class), null);
276   
277    // Verify
278  1 verify(this.observationManager, times(1)).notify(
279    any(DefaultUntypedRecordableEvent.class), any(), any());
280  1 assertNotNull(answer.getSentEvent());
281  1 assertNotNull(answer.getSentEvent().getTarget());
282  1 assertTrue(answer.getSentEvent().getTarget().contains("UserA"));
283  1 assertTrue(answer.getSentEvent().getTarget().contains("UserB"));
284  1 assertEquals(2, answer.getSentEvent().getTarget().size());
285    }
286    }