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

File DefaultWikiCreatorTest.java

 

Code metrics

0
21
2
1
102
61
2
0.1
10.5
2
1

Classes

Class Line # Actions
DefaultWikiCreatorTest 45 21 0% 2 0
1.0100%
 

Contributing tests

This file is covered by 1 test. .

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.manager;
21   
22    import javax.inject.Provider;
23   
24    import org.junit.Before;
25    import org.junit.Rule;
26    import org.junit.Test;
27    import org.xwiki.test.mockito.MockitoComponentMockingRule;
28    import org.xwiki.wiki.descriptor.WikiDescriptor;
29    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
30    import org.xwiki.wiki.internal.descriptor.DefaultWikiDescriptor;
31    import org.xwiki.wiki.internal.descriptor.builder.WikiDescriptorBuilder;
32   
33    import com.xpn.xwiki.XWikiContext;
34    import com.xpn.xwiki.doc.XWikiDocument;
35    import com.xpn.xwiki.store.XWikiStoreInterface;
36   
37    import static org.junit.Assert.assertNotNull;
38    import static org.junit.Assert.assertTrue;
39    import static org.mockito.ArgumentMatchers.any;
40    import static org.mockito.ArgumentMatchers.eq;
41    import static org.mockito.Mockito.mock;
42    import static org.mockito.Mockito.verify;
43    import static org.mockito.Mockito.when;
44   
 
45    public class DefaultWikiCreatorTest
46    {
47    @Rule
48    public MockitoComponentMockingRule<DefaultWikiCreator> mocker =
49    new MockitoComponentMockingRule(DefaultWikiCreator.class);
50   
51    private WikiDescriptorManager wikiDescriptorManager;
52   
53    private Provider<XWikiContext> xcontextProvider;
54   
55    private XWikiContext xcontext;
56   
57    private com.xpn.xwiki.XWiki xwiki;
58   
59    private XWikiStoreInterface store;
60   
61    private WikiDescriptorBuilder wikiDescriptorBuilder;
62   
 
63  1 toggle @Before
64    public void setUp() throws Exception
65    {
66  1 wikiDescriptorManager = mocker.getInstance(WikiDescriptorManager.class);
67  1 xcontextProvider = mocker.registerMockComponent(XWikiContext.TYPE_PROVIDER);
68  1 xcontext = mock(XWikiContext.class);
69  1 when(xcontextProvider.get()).thenReturn(xcontext);
70  1 xwiki = mock(com.xpn.xwiki.XWiki.class);
71  1 when(xcontext.getWiki()).thenReturn(xwiki);
72  1 store = mock(XWikiStoreInterface.class);
73  1 when(xwiki.getStore()).thenReturn(store);
74  1 wikiDescriptorBuilder = mocker.getInstance(WikiDescriptorBuilder.class);
75    }
76   
 
77  1 toggle @Test
78    public void create() throws Exception
79    {
80    // Other mocks
81  1 DefaultWikiDescriptor descriptor = new DefaultWikiDescriptor("wikiid1", "wikialias1");
82  1 XWikiDocument descriptorDocument = mock(XWikiDocument.class);
83  1 when(wikiDescriptorBuilder.save(eq(descriptor))).thenReturn(descriptorDocument);
84  1 when(wikiDescriptorManager.getById("wikiid1")).thenReturn(descriptor);
85  1 when(store.isWikiNameAvailable(any(String.class), any(XWikiContext.class))).thenReturn(true);
86   
87    // Create
88  1 WikiDescriptor newWikiDescriptor = this.mocker.getComponentUnderTest().create("wikiid1", "wikialias1");
89  1 assertNotNull(newWikiDescriptor);
90   
91    // Verify that the wiki descriptor is an instance of DefaultWikiDescriptor
92  1 assertTrue(newWikiDescriptor instanceof DefaultWikiDescriptor);
93    // Verify that the wiki has been created
94  1 verify(store).createWiki(eq("wikiid1"), any(XWikiContext.class));
95    // Verify that the wiki has been updated
96  1 verify(xwiki).initializeWiki(eq("wikiid1"), eq(true), any(XWikiContext.class));
97    // Verify that the descriptor document has been saved
98  1 verify(wikiDescriptorBuilder).save(eq(descriptor));
99    // Verify that the descriptor has been reloaded after being saved
100  1 assertTrue(descriptor == newWikiDescriptor);
101    }
102    }