1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.platform.wiki.creationjob.internal.steps

File ProvisionWikiStepTest.java

 

Code metrics

0
43
5
1
153
102
6
0.14
8.6
5
1.2

Classes

Class Line # Actions
ProvisionWikiStepTest 54 43 0% 6 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.platform.wiki.creationjob.internal.steps;
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.bridge.event.WikiCopiedEvent;
28    import org.xwiki.bridge.event.WikiProvisionedEvent;
29    import org.xwiki.bridge.event.WikiProvisioningEvent;
30    import org.xwiki.bridge.event.WikiProvisioningFailedEvent;
31    import org.xwiki.extension.ExtensionId;
32    import org.xwiki.observation.ObservationManager;
33    import org.xwiki.platform.wiki.creationjob.WikiCreationException;
34    import org.xwiki.platform.wiki.creationjob.WikiCreationRequest;
35    import org.xwiki.platform.wiki.creationjob.WikiSource;
36    import org.xwiki.platform.wiki.creationjob.internal.ExtensionInstaller;
37    import org.xwiki.test.mockito.MockitoComponentMockingRule;
38    import org.xwiki.wiki.provisioning.WikiCopier;
39   
40    import com.xpn.xwiki.XWikiContext;
41   
42    import static org.junit.Assert.assertEquals;
43    import static org.junit.Assert.assertNotNull;
44    import static org.mockito.ArgumentMatchers.eq;
45    import static org.mockito.Mockito.doThrow;
46    import static org.mockito.Mockito.mock;
47    import static org.mockito.Mockito.verify;
48    import static org.mockito.Mockito.verifyZeroInteractions;
49    import static org.mockito.Mockito.when;
50   
51    /**
52    * @version $Id: 9538a628225a95d4da0e7bc35e88646bace6eee6 $
53    */
 
54    public class ProvisionWikiStepTest
55    {
56    @Rule
57    public MockitoComponentMockingRule<ProvisionWikiStep> mocker =
58    new MockitoComponentMockingRule<>(ProvisionWikiStep.class);
59   
60    private WikiCopier wikiCopier;
61   
62    private ExtensionInstaller extensionInstaller;
63   
64    private ObservationManager observationManager;
65   
66    private Provider<XWikiContext> xcontextProvider;
67   
68    private XWikiContext xcontext;
69   
 
70  4 toggle @Before
71    public void setUp() throws Exception
72    {
73  4 wikiCopier = mocker.getInstance(WikiCopier.class);
74  4 extensionInstaller = mocker.getInstance(ExtensionInstaller.class);
75  4 observationManager = mocker.getInstance(ObservationManager.class);
76  4 xcontextProvider = mocker.registerMockComponent(XWikiContext.TYPE_PROVIDER);
77  4 xcontext = mock(XWikiContext.class);
78  4 when(xcontextProvider.get()).thenReturn(xcontext);
79    }
80   
 
81  1 toggle @Test
82    public void executeWhenSourceIsExtension() throws Exception
83    {
84  1 WikiCreationRequest request = new WikiCreationRequest();
85  1 request.setWikiId("wikiId");
86  1 request.setWikiSource(WikiSource.EXTENSION);
87  1 ExtensionId extensionId = new ExtensionId("id", "version");
88  1 request.setExtensionId(extensionId);
89   
90    // Test
91  1 mocker.getComponentUnderTest().execute(request);
92   
93    // Verify
94  1 verify(extensionInstaller).installExtension(eq("wikiId"), eq(extensionId));
95  1 verify(observationManager).notify(eq(new WikiProvisioningEvent("wikiId")), eq("wikiId"), eq(xcontext));
96  1 verify(observationManager).notify(eq(new WikiProvisionedEvent("wikiId")), eq("wikiId"), eq(xcontext));
97  1 verifyZeroInteractions(wikiCopier);
98    }
99   
 
100  1 toggle @Test
101    public void executeWhenSourceIsTemplate() throws Exception
102    {
103  1 WikiCreationRequest request = new WikiCreationRequest();
104  1 request.setWikiId("wikiId");
105  1 request.setWikiSource(WikiSource.TEMPLATE);
106  1 request.setTemplateId("template");
107   
108    // Test
109  1 mocker.getComponentUnderTest().execute(request);
110   
111    // Verify
112  1 verify(wikiCopier).copyDocuments(eq("template"), eq("wikiId"), eq(false));
113  1 verify(observationManager).notify(eq(new WikiProvisioningEvent("wikiId")), eq("wikiId"), eq(xcontext));
114  1 verify(observationManager).notify(eq(new WikiCopiedEvent("template", "wikiId")), eq("template"), eq(xcontext));
115  1 verify(observationManager).notify(eq(new WikiProvisionedEvent("wikiId")), eq("wikiId"), eq(xcontext));
116  1 verifyZeroInteractions(extensionInstaller);
117    }
118   
 
119  1 toggle @Test
120    public void executeWhenException() throws Exception
121    {
122  1 WikiCreationRequest request = new WikiCreationRequest();
123  1 request.setWikiId("wikiId");
124  1 request.setWikiSource(WikiSource.EXTENSION);
125  1 ExtensionId extensionId = new ExtensionId("id", "version");
126  1 request.setExtensionId(extensionId);
127   
128    // Mocks
129  1 WikiCreationException exception = new WikiCreationException("Exception in ExtensionInstaller");
130  1 doThrow(exception).when(extensionInstaller).installExtension("wikiId", extensionId);
131   
132    // Test
133  1 WikiCreationException caughtException = null;
134  1 try {
135  1 mocker.getComponentUnderTest().execute(request);
136    } catch (WikiCreationException e) {
137  1 caughtException = e;
138    }
139   
140    // Verify
141  1 assertNotNull(caughtException);
142  1 assertEquals("Failed to provision the wiki [wikiId].", caughtException.getMessage());
143  1 assertEquals(exception, caughtException.getCause());
144  1 verify(observationManager).notify(eq(new WikiProvisioningEvent("wikiId")), eq("wikiId"), eq(xcontext));
145  1 verify(observationManager).notify(eq(new WikiProvisioningFailedEvent("wikiId")), eq("wikiId"), eq(xcontext));
146    }
147   
 
148  1 toggle @Test
149    public void getOrder() throws Exception
150    {
151  1 assertEquals(3000, mocker.getComponentUnderTest().getOrder());
152    }
153    }