1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.wiki.internal.descriptor.builder

File DefaultWikiDescriptorBuilderTest.java

 

Code metrics

0
67
4
1
201
129
4
0.06
16.75
4
1

Classes

Class Line # Actions
DefaultWikiDescriptorBuilderTest 61 67 0% 4 0
1.0100%
 

Contributing tests

This file is covered by 3 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.builder;
21   
22    import static org.junit.Assert.assertEquals;
23    import static org.junit.Assert.assertNull;
24    import static org.mockito.ArgumentMatchers.any;
25    import static org.mockito.Mockito.doThrow;
26    import static org.mockito.Mockito.mock;
27    import static org.mockito.Mockito.verify;
28    import static org.mockito.Mockito.when;
29   
30    import java.util.ArrayList;
31    import java.util.List;
32   
33    import javax.inject.Provider;
34   
35    import org.junit.Before;
36    import org.junit.Rule;
37    import org.junit.Test;
38    import org.xwiki.model.reference.DocumentReference;
39    import org.xwiki.model.reference.DocumentReferenceResolver;
40    import org.xwiki.model.reference.EntityReferenceSerializer;
41    import org.xwiki.model.reference.WikiReference;
42    import org.xwiki.test.mockito.MockitoComponentMockingRule;
43    import org.xwiki.wiki.descriptor.WikiDescriptor;
44    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
45    import org.xwiki.wiki.internal.descriptor.document.WikiDescriptorDocumentHelper;
46    import org.xwiki.wiki.internal.descriptor.document.XWikiServerClassDocumentInitializer;
47    import org.xwiki.wiki.internal.descriptor.properties.WikiPropertyGroupManager;
48    import org.xwiki.wiki.properties.WikiPropertyGroupException;
49   
50    import com.xpn.xwiki.XWiki;
51    import com.xpn.xwiki.XWikiContext;
52    import com.xpn.xwiki.doc.XWikiDocument;
53    import com.xpn.xwiki.objects.BaseObject;
54   
55    /**
56    * Unit tests for {@link org.xwiki.wiki.internal.descriptor.builder.DefaultWikiDescriptorBuilder}.
57    *
58    * @version $Id: c194c65272409326aa2e1b06e6560fafdb67894f $
59    * @since 6.0M1
60    */
 
61    public class DefaultWikiDescriptorBuilderTest
62    {
63    @Rule
64    public org.xwiki.test.mockito.MockitoComponentMockingRule<DefaultWikiDescriptorBuilder> mocker =
65    new MockitoComponentMockingRule<>(DefaultWikiDescriptorBuilder.class);
66   
67    private Provider<XWikiContext> xcontextProvider;
68   
69    private EntityReferenceSerializer<String> referenceSerializer;
70   
71    private DocumentReferenceResolver<String> referenceResolver;
72   
73    private DocumentReferenceResolver<String> userReferenceResolver;
74   
75    private WikiDescriptorManager wikiDescriptorManager;
76   
77    private WikiPropertyGroupManager wikiPropertyGroupManager;
78   
79    private WikiDescriptorDocumentHelper wikiDescriptorDocumentHelper;
80   
81    private XWikiContext context;
82   
83    private XWiki xwiki;
84   
 
85  3 toggle @Before
86    public void setUp() throws Exception
87    {
88  3 xcontextProvider = mocker.registerMockComponent(XWikiContext.TYPE_PROVIDER);
89  3 context = mock(XWikiContext.class);
90  3 when(xcontextProvider.get()).thenReturn(context);
91  3 xwiki = mock(XWiki.class);
92  3 when(context.getWiki()).thenReturn(xwiki);
93   
94  3 referenceSerializer = mocker.getInstance(EntityReferenceSerializer.TYPE_STRING);
95  3 referenceResolver = mocker.getInstance(DocumentReferenceResolver.TYPE_STRING);
96  3 userReferenceResolver = mocker.getInstance(DocumentReferenceResolver.TYPE_STRING, "user");
97   
98  3 wikiDescriptorDocumentHelper = mocker.getInstance(WikiDescriptorDocumentHelper.class);
99   
100  3 wikiDescriptorManager = mocker.registerMockComponent(WikiDescriptorManager.class);
101  3 wikiPropertyGroupManager = mocker.registerMockComponent(WikiPropertyGroupManager.class);
102    }
103   
 
104  1 toggle @Test
105    public void buildDescriptorObject() throws Exception
106    {
107    // Mocks
108  1 List<BaseObject> objects = new ArrayList<>();
109  1 BaseObject object1 = mock(BaseObject.class);
110  1 BaseObject object2 = mock(BaseObject.class);
111  1 BaseObject object3 = mock(BaseObject.class);
112    // Make sure that the first object is null to also verify this case since it can happen that we get holes
113    // with the XWikiDocument.getXObjects() API...
114  1 objects.add(null);
115  1 objects.add(object1);
116  1 objects.add(object2);
117  1 objects.add(null);
118  1 objects.add(object3);
119   
120  1 XWikiDocument document = mock(XWikiDocument.class);
121  1 DocumentReference documentReference = new DocumentReference("mainWiki", "XWiki", "XWikiServerSubwiki1");
122  1 when(document.getDocumentReference()).thenReturn(documentReference);
123   
124  1 when(object1.getStringValue(XWikiServerClassDocumentInitializer.FIELD_SERVER)).thenReturn("subwiki1");
125  1 when(object2.getStringValue(XWikiServerClassDocumentInitializer.FIELD_SERVER)).thenReturn("alias1");
126  1 when(object3.getStringValue(XWikiServerClassDocumentInitializer.FIELD_SERVER)).thenReturn("alias2");
127   
128  1 DocumentReference mainPageReference = new DocumentReference("subwiki1", "Space", "MainPage");
129   
130  1 when(object1.getStringValue(XWikiServerClassDocumentInitializer.FIELD_HOMEPAGE)).thenReturn("Space.MainPage");
131  1 when(referenceResolver.resolve("Space.MainPage")).thenReturn(mainPageReference);
132  1 when(object1.getStringValue(XWikiServerClassDocumentInitializer.FIELD_WIKIPRETTYNAME)).thenReturn(
133    "myPrettyName");
134  1 when(object1.getStringValue(XWikiServerClassDocumentInitializer.FIELD_OWNER)).thenReturn("myOwner");
135  1 when(object1.getStringValue(XWikiServerClassDocumentInitializer.FIELD_DESCRIPTION)).thenReturn("myDescription");
136   
137  1 DocumentReference ownerRef = new DocumentReference("subwiki1", "XWiki", "myOwner");
138  1 when(userReferenceResolver.resolve("myOwner", new WikiReference("subwiki1"))).thenReturn(ownerRef);
139  1 when(referenceSerializer.serialize(ownerRef)).thenReturn("subwiki1:XWiki.myOwner");
140   
141    // Test
142  1 WikiDescriptor result = mocker.getComponentUnderTest().buildDescriptorObject(objects, document);
143   
144  1 assertEquals("subwiki1", result.getId());
145  1 assertEquals(3, result.getAliases().size());
146  1 assertEquals("subwiki1", result.getAliases().get(0));
147  1 assertEquals("alias1", result.getAliases().get(1));
148  1 assertEquals("alias2", result.getAliases().get(2));
149  1 assertEquals(mainPageReference, result.getMainPageReference());
150  1 assertEquals("myPrettyName", result.getPrettyName());
151  1 assertEquals("subwiki1:XWiki.myOwner", result.getOwnerId());
152  1 assertEquals("myDescription", result.getDescription());
153   
154    // Verify
155  1 wikiPropertyGroupManager.loadForDescriptor(any(WikiDescriptor.class));
156    }
157   
 
158  1 toggle @Test
159    public void buildDescriptorObjectWhenInvalidWiki() throws Exception
160    {
161    // Mocks
162  1 List<BaseObject> objects = new ArrayList<>();
163  1 BaseObject object1 = mock(BaseObject.class);
164  1 objects.add(object1);
165  1 when(object1.getStringValue(XWikiServerClassDocumentInitializer.FIELD_SERVER)).thenReturn(" ");
166   
167  1 XWikiDocument document = mock(XWikiDocument.class);
168   
169    // Test
170  1 WikiDescriptor result = mocker.getComponentUnderTest().buildDescriptorObject(objects, document);
171  1 assertNull(result);
172    }
173   
 
174  1 toggle @Test
175    public void buildDescriptorObjectWhenException() throws Exception
176    {
177    // Mocks
178  1 List<BaseObject> objects = new ArrayList<>();
179  1 BaseObject object1 = mock(BaseObject.class);
180  1 objects.add(object1);
181   
182  1 XWikiDocument document = mock(XWikiDocument.class);
183  1 DocumentReference documentReference = new DocumentReference("mainWiki", "XWiki", "XWikiServerSubwiki1");
184  1 when(document.getDocumentReference()).thenReturn(documentReference);
185  1 when(object1.getStringValue(XWikiServerClassDocumentInitializer.FIELD_SERVER)).thenReturn("subwiki1");
186   
187  1 DocumentReference mainPageReference = new DocumentReference("subwiki1", "Space", "MainPage");
188  1 when(object1.getStringValue(XWikiServerClassDocumentInitializer.FIELD_HOMEPAGE)).thenReturn("Space.MainPage");
189  1 when(referenceResolver.resolve("Space.MainPage")).thenReturn(mainPageReference);
190   
191  1 Exception exception = new WikiPropertyGroupException("error in wikiPropertyGroupManager.loadForDescriptor");
192  1 doThrow(exception).when(wikiPropertyGroupManager).loadForDescriptor(any(WikiDescriptor.class));
193   
194    // Test
195  1 mocker.getComponentUnderTest().buildDescriptorObject(objects, document);
196   
197    // Verify
198  1 verify(mocker.getMockedLogger()).error("Failed to load wiki property groups for wiki [{}].", "subwiki1",
199    exception);
200    }
201    }