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

File AutomaticWatchModeListenerTest.java

 

Code metrics

0
30
5
1
152
89
5
0.17
6
5
1

Classes

Class Line # Actions
AutomaticWatchModeListenerTest 58 30 0% 5 0
1.0100%
 

Contributing tests

This file is covered by 4 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.watchlist.internal;
21   
22    import org.junit.Before;
23    import org.junit.Rule;
24    import org.junit.Test;
25    import org.xwiki.bridge.event.DocumentCreatedEvent;
26    import org.xwiki.bridge.event.WikiCreatingEvent;
27    import org.xwiki.context.Execution;
28    import org.xwiki.context.ExecutionContext;
29    import org.xwiki.context.internal.DefaultExecution;
30    import org.xwiki.model.reference.DocumentReference;
31    import org.xwiki.observation.EventListener;
32    import org.xwiki.observation.internal.DefaultObservationContext;
33    import org.xwiki.observation.internal.ObservationContextListener;
34    import org.xwiki.test.annotation.ComponentList;
35    import org.xwiki.test.mockito.MockitoComponentMockingRule;
36    import org.xwiki.watchlist.WatchListConfiguration;
37    import org.xwiki.watchlist.internal.api.AutomaticWatchMode;
38    import org.xwiki.watchlist.internal.api.WatchListStore;
39    import org.xwiki.watchlist.internal.api.WatchedElementType;
40   
41    import com.xpn.xwiki.XWiki;
42    import com.xpn.xwiki.XWikiContext;
43    import com.xpn.xwiki.doc.XWikiDocument;
44    import com.xpn.xwiki.internal.event.XARImportingEvent;
45   
46    import static org.mockito.ArgumentMatchers.any;
47    import static org.mockito.Mockito.mock;
48    import static org.mockito.Mockito.never;
49    import static org.mockito.Mockito.verify;
50    import static org.mockito.Mockito.when;
51   
52    /**
53    * Unit tests for {@link AutomaticWatchModeListener}.
54    *
55    * @version $Id: 173f1e5fe4ef0bb5f2717753c2f3049a344e387d $
56    */
57    @ComponentList({ObservationContextListener.class, DefaultExecution.class, DefaultObservationContext.class})
 
58    public class AutomaticWatchModeListenerTest
59    {
60    private WatchListStore mockStore;
61   
62    private EventListener observationContextListener;
63   
64    @Rule
65    public final MockitoComponentMockingRule<EventListener> mocker = new MockitoComponentMockingRule<EventListener>(
66    AutomaticWatchModeListener.class);
67   
68    private WatchListConfiguration configuration;
69   
 
70  4 toggle @Before
71    public void setUp() throws Exception
72    {
73  4 this.mockStore = mocker.getInstance(WatchListStore.class);
74   
75    // Make sure we have an Execution Context since the observationContextListener will store current events in it
76  4 Execution execution = mocker.getInstance(Execution.class);
77  4 execution.setContext(new ExecutionContext());
78   
79  4 this.observationContextListener = mocker.getInstance(EventListener.class, "ObservationContextListener");
80   
81  4 this.configuration = mocker.getInstance(WatchListConfiguration.class);
82  4 when(this.configuration.isEnabled()).thenReturn(true);
83    }
84   
85    /**
86    * Verify that we don't do anything when the current event is inside a WikiCreatingEvent.
87    */
 
88  1 toggle @Test
89    public void onEventWhenInContextOfWikiCreatingEvent() throws Exception
90    {
91    // We simulate a WikiCreatingEvent in the Execution Context
92  1 this.observationContextListener.onEvent(new WikiCreatingEvent(), null, null);
93   
94  1 mocker.getComponentUnderTest().onEvent(new DocumentCreatedEvent(), null, null);
95   
96  1 verify(mockStore, never()).getAutomaticWatchMode(any());
97  1 verify(mockStore, never()).addWatchedElement(any(), any(), any(WatchedElementType.class));
98    }
99   
100    /**
101    * Verify that we don't do anything when the current event is inside a XARImportingEvent.
102    */
 
103  1 toggle @Test
104    public void onEventWhenInContextOXARImportingEvent() throws Exception
105    {
106    // We simulate a XARImportingEvent in the Execution Context
107  1 this.observationContextListener.onEvent(new XARImportingEvent(), null, null);
108   
109  1 mocker.getComponentUnderTest().onEvent(new DocumentCreatedEvent(), null, null);
110   
111  1 verify(mockStore, never()).getAutomaticWatchMode(any());
112  1 verify(mockStore, never()).addWatchedElement(any(), any(), any(WatchedElementType.class));
113    }
114   
115    /**
116    * Verify that we don't do anything when the watchlist is disabled.
117    */
 
118  1 toggle @Test
119    public void onEventWhenWatchListDisabled() throws Exception
120    {
121  1 when(this.configuration.isEnabled()).thenReturn(false);
122  1 mocker.getComponentUnderTest().onEvent(new DocumentCreatedEvent(), null, null);
123   
124  1 verify(mockStore, never()).getAutomaticWatchMode(any());
125  1 verify(mockStore, never()).addWatchedElement(any(), any(), any(WatchedElementType.class));
126    }
127   
 
128  1 toggle @Test
129    public void onEventWhenDocumentCreatedEvent() throws Exception
130    {
131  1 XWikiContext context = mock(XWikiContext.class);
132   
133  1 XWikiDocument document = mock(XWikiDocument.class);
134  1 DocumentReference documentReference = new DocumentReference("authorWiki", "authorSpace", "authorPage");
135  1 when(document.getContentAuthor()).thenReturn("content author");
136  1 when(document.getContentAuthorReference()).thenReturn(documentReference);
137  1 when(document.getPrefixedFullName()).thenReturn("authorSpace.authorPage");
138   
139  1 when(this.mockStore.getAutomaticWatchMode("content author")).thenReturn(AutomaticWatchMode.ALL);
140   
141  1 XWiki xwiki = mock(XWiki.class);
142  1 when(xwiki.exists(documentReference, context)).thenReturn(true);
143   
144  1 when(context.getWiki()).thenReturn(xwiki);
145   
146  1 mocker.getComponentUnderTest().onEvent(new DocumentCreatedEvent(), document, context);
147   
148    // Verify that the document is added to the watchlist
149  1 verify(this.mockStore).addWatchedElement("content author", "authorSpace.authorPage",
150    org.xwiki.watchlist.internal.api.WatchedElementType.DOCUMENT);
151    }
152    }