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

File SaveWikiMetaDataStepTest.java

 

Code metrics

0
49
5
1
154
100
6
0.12
9.8
5
1.2

Classes

Class Line # Actions
SaveWikiMetaDataStepTest 46 49 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 org.junit.Before;
23    import org.junit.Rule;
24    import org.junit.Test;
25    import org.xwiki.extension.ExtensionId;
26    import org.xwiki.platform.wiki.creationjob.WikiCreationException;
27    import org.xwiki.platform.wiki.creationjob.WikiCreationRequest;
28    import org.xwiki.platform.wiki.creationjob.WikiSource;
29    import org.xwiki.test.mockito.MockitoComponentMockingRule;
30    import org.xwiki.wiki.descriptor.WikiDescriptor;
31    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
32    import org.xwiki.wiki.manager.WikiManagerException;
33    import org.xwiki.wiki.template.WikiTemplateManager;
34    import org.xwiki.wiki.user.MembershipType;
35    import org.xwiki.wiki.user.UserScope;
36    import org.xwiki.wiki.user.WikiUserManager;
37   
38    import static org.junit.Assert.assertEquals;
39    import static org.mockito.Mockito.doThrow;
40    import static org.mockito.Mockito.verify;
41    import static org.mockito.Mockito.when;
42   
43    /**
44    * @version $Id: 0047c28dd06bde6f7433aec81193cfbfd5df5d80 $
45    */
 
46    public class SaveWikiMetaDataStepTest
47    {
48    @Rule
49    public MockitoComponentMockingRule<SaveWikiMetaDataStep> mocker =
50    new MockitoComponentMockingRule<>(SaveWikiMetaDataStep.class);
51   
52    private WikiDescriptorManager wikiDescriptorManager;
53   
54    private WikiTemplateManager wikiTemplateManager;
55   
56    private WikiUserManager wikiUserManager;
57   
 
58  4 toggle @Before
59    public void setUp() throws Exception
60    {
61  4 wikiDescriptorManager = mocker.getInstance(WikiDescriptorManager.class);
62  4 wikiTemplateManager = mocker.getInstance(WikiTemplateManager.class);
63  4 wikiUserManager = mocker.getInstance(WikiUserManager.class);
64    }
65   
 
66  1 toggle @Test
67    public void execute() throws Exception
68    {
69  1 WikiCreationRequest request = new WikiCreationRequest();
70  1 request.setWikiId("wikiId");
71  1 request.setDescription("description");
72  1 request.setPrettyName("pretty name");
73  1 request.setOwnerId("ownerId");
74  1 request.setTemplate(false);
75  1 request.setUserScope(UserScope.GLOBAL_ONLY);
76  1 request.setMembershipType(MembershipType.INVITE);
77  1 request.setWikiSource(WikiSource.EXTENSION);
78  1 ExtensionId extensionId = new ExtensionId("id", "version");
79  1 request.setExtensionId(extensionId);
80   
81    // Mock
82  1 WikiDescriptor descriptor = new WikiDescriptor("wikiId", "alias");
83  1 when(wikiDescriptorManager.getById("wikiId")).thenReturn(descriptor);
84   
85   
86    // Test
87  1 mocker.getComponentUnderTest().execute(request);
88   
89    // Verify
90  1 assertEquals("description", descriptor.getDescription());
91  1 assertEquals("pretty name", descriptor.getPrettyName());
92  1 assertEquals("ownerId", descriptor.getOwnerId());
93  1 verify(wikiDescriptorManager).saveDescriptor(descriptor);
94  1 verify(wikiTemplateManager).setTemplate("wikiId", false);
95  1 verify(wikiUserManager).setUserScope("wikiId", UserScope.GLOBAL_ONLY);
96  1 verify(wikiUserManager).setMembershipType("wikiId", MembershipType.INVITE);
97    }
98   
 
99  1 toggle @Test
100    public void executeWithException() throws Exception
101    {
102  1 WikiCreationRequest request = new WikiCreationRequest();
103  1 request.setWikiId("wikiId");
104   
105    // Mock
106  1 WikiDescriptor descriptor = new WikiDescriptor("wikiId", "alias");
107  1 when(wikiDescriptorManager.getById("wikiId")).thenReturn(descriptor);
108   
109  1 Exception exception = new WikiManagerException("Exception on WikiManager.");
110  1 doThrow(exception).when(wikiDescriptorManager).saveDescriptor(descriptor);
111   
112    // Test
113  1 WikiCreationException caughtException = null;
114  1 try {
115  1 mocker.getComponentUnderTest().execute(request);
116    } catch (WikiCreationException e) {
117  1 caughtException = e;
118    }
119   
120    // Verify
121  1 assertEquals("Failed to set metadata to the wiki [wikiId].", caughtException.getMessage());
122  1 assertEquals(exception, caughtException.getCause());
123    }
124   
 
125  1 toggle @Test
126    public void executeWhenSourceIsTemplate() throws Exception
127    {
128  1 WikiCreationRequest request = new WikiCreationRequest();
129  1 request.setWikiId("wikiId");
130  1 request.setTemplate(false);
131  1 request.setUserScope(UserScope.LOCAL_ONLY);
132  1 request.setMembershipType(MembershipType.OPEN);
133  1 request.setWikiSource(WikiSource.TEMPLATE);
134   
135    // Mock
136  1 WikiDescriptor descriptor = new WikiDescriptor("wikiId", "alias");
137  1 when(wikiDescriptorManager.getById("wikiId")).thenReturn(descriptor);
138   
139    // Test
140  1 mocker.getComponentUnderTest().execute(request);
141   
142    // Verify
143  1 verify(wikiDescriptorManager).saveDescriptor(descriptor);
144  1 verify(wikiUserManager).setUserScope("wikiId", UserScope.LOCAL_ONLY);
145  1 verify(wikiUserManager).setMembershipType("wikiId", MembershipType.OPEN);
146    }
147   
 
148  1 toggle @Test
149    public void getOrder() throws Exception
150    {
151  1 assertEquals(2000, mocker.getComponentUnderTest().getOrder());
152    }
153    }
154