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

File WikiDescriptorListenerTest.java

 

Code metrics

0
41
3
1
142
84
3
0.07
13.67
3
1

Classes

Class Line # Actions
WikiDescriptorListenerTest 53 41 0% 3 0
1.0100%
 

Contributing tests

This file is covered by 2 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.wiki.internal.descriptor.listener;
21   
22    import java.util.ArrayList;
23    import java.util.List;
24   
25    import org.junit.Before;
26    import org.junit.Rule;
27    import org.junit.Test;
28    import org.xwiki.bridge.event.DocumentDeletedEvent;
29    import org.xwiki.bridge.event.DocumentUpdatedEvent;
30    import org.xwiki.model.reference.DocumentReference;
31    import org.xwiki.observation.event.Event;
32    import org.xwiki.test.mockito.MockitoComponentMockingRule;
33    import org.xwiki.wiki.internal.descriptor.DefaultWikiDescriptor;
34    import org.xwiki.wiki.internal.descriptor.builder.WikiDescriptorBuilder;
35    import org.xwiki.wiki.internal.descriptor.document.WikiDescriptorDocumentHelper;
36    import org.xwiki.wiki.internal.manager.WikiDescriptorCache;
37   
38    import com.xpn.xwiki.doc.XWikiDocument;
39    import com.xpn.xwiki.objects.BaseObject;
40   
41    import static org.mockito.ArgumentMatchers.any;
42    import static org.mockito.Mockito.mock;
43    import static org.mockito.Mockito.never;
44    import static org.mockito.Mockito.verify;
45    import static org.mockito.Mockito.when;
46   
47    /**
48    * Test class for {@link WikiDescriptorListenerTest}.
49    *
50    * @since 6.0M1
51    * @version $Id: dec325e711b255b9f619c979f63f164daa64d4af $
52    */
 
53    public class WikiDescriptorListenerTest
54    {
55    @Rule
56    public org.xwiki.test.mockito.MockitoComponentMockingRule<WikiDescriptorListener> mocker =
57    new MockitoComponentMockingRule(WikiDescriptorListener.class);
58   
59    private WikiDescriptorBuilder builder;
60   
61    private WikiDescriptorCache cache;
62   
63    private WikiDescriptorDocumentHelper wikiDescriptorDocumentHelper;
64   
 
65  2 toggle @Before
66    public void setUp() throws Exception
67    {
68  2 builder = mocker.getInstance(WikiDescriptorBuilder.class);
69  2 cache = mocker.getInstance(WikiDescriptorCache.class);
70  2 wikiDescriptorDocumentHelper = mocker.getInstance(WikiDescriptorDocumentHelper.class);
71    }
72   
 
73  1 toggle @Test
74    public void onDocumentDeletedEvent() throws Exception
75    {
76  1 XWikiDocument document = mock(XWikiDocument.class);
77  1 XWikiDocument originalDocument = mock(XWikiDocument.class);
78  1 when(document.getOriginalDocument()).thenReturn(originalDocument);
79   
80  1 Event event = new DocumentDeletedEvent();
81   
82  1 List<BaseObject> objects = new ArrayList<>();
83  1 BaseObject object = mock(BaseObject.class);
84  1 objects.add(object);
85  1 when(originalDocument.getXObjects(WikiDescriptorListener.SERVER_CLASS)).thenReturn(objects);
86   
87  1 DocumentReference documentReference = new DocumentReference("mainWiki", "XWiki", "XWikiServerSubwikiA");
88  1 when(originalDocument.getDocumentReference()).thenReturn(documentReference);
89   
90  1 when(wikiDescriptorDocumentHelper.getWikiIdFromDocumentReference(documentReference)).thenReturn("subwikia");
91   
92  1 DefaultWikiDescriptor descriptor = new DefaultWikiDescriptor("subwikia", "alias");
93  1 when(cache.getFromId("subwikia")).thenReturn(descriptor);
94   
95    // Test
96  1 mocker.getComponentUnderTest().onEvent(event, document, null);
97   
98    // Verify
99  1 verify(cache).remove(descriptor.getId(), descriptor.getAliases());
100  1 verify(cache, never()).add(any(DefaultWikiDescriptor.class));
101    }
102   
 
103  1 toggle @Test
104    public void onDocumentUpdatedEvent() throws Exception
105    {
106  1 XWikiDocument document = mock(XWikiDocument.class);
107  1 XWikiDocument originalDocument = mock(XWikiDocument.class);
108  1 when(document.getOriginalDocument()).thenReturn(originalDocument);
109   
110  1 Event event = new DocumentUpdatedEvent();
111   
112  1 List<BaseObject> objects = new ArrayList<>();
113  1 BaseObject object = mock(BaseObject.class);
114  1 objects.add(object);
115  1 when(originalDocument.getXObjects(WikiDescriptorListener.SERVER_CLASS)).thenReturn(objects);
116   
117  1 DocumentReference documentReference = new DocumentReference("mainWiki", "XWiki", "XWikiServerSubwikiA");
118  1 when(originalDocument.getDocumentReference()).thenReturn(documentReference);
119   
120  1 when(wikiDescriptorDocumentHelper.getWikiIdFromDocumentReference(documentReference)).thenReturn("subwikia");
121   
122  1 DefaultWikiDescriptor descriptor = new DefaultWikiDescriptor("subwikia", "alias");
123  1 when(cache.getFromId("subwikia")).thenReturn(descriptor);
124   
125    // New objects
126  1 List<BaseObject> newObjects = new ArrayList<>();
127  1 BaseObject newObject = mock(BaseObject.class);
128  1 newObjects.add(newObject);
129   
130  1 when(document.getXObjects(WikiDescriptorListener.SERVER_CLASS)).thenReturn(newObjects);
131  1 DefaultWikiDescriptor newDescriptor = new DefaultWikiDescriptor("subwikia", "newAlias");
132  1 when(builder.buildDescriptorObject(newObjects, document)).thenReturn(newDescriptor);
133   
134    // Test
135  1 mocker.getComponentUnderTest().onEvent(event, document, null);
136   
137    // Verify
138  1 verify(cache).remove(descriptor.getId(), descriptor.getAliases());
139  1 verify(cache).add(newDescriptor);
140    }
141   
142    }