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

File WorkspaceMigrationTest.java

 

Code metrics

0
44
5
1
187
119
5
0.11
8.8
5
1

Classes

Class Line # Actions
WorkspaceMigrationTest 48 44 0% 5 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.wiki.workspacesmigrator.internal;
21   
22    import java.util.List;
23   
24    import org.junit.Before;
25    import org.junit.Rule;
26    import org.junit.Test;
27    import org.xwiki.context.Execution;
28    import org.xwiki.context.ExecutionContext;
29    import org.xwiki.model.reference.DocumentReference;
30    import org.xwiki.test.mockito.MockitoComponentMockingRule;
31    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
32   
33    import com.xpn.xwiki.XWiki;
34    import com.xpn.xwiki.XWikiContext;
35    import com.xpn.xwiki.XWikiException;
36    import com.xpn.xwiki.doc.XWikiDocument;
37    import com.xpn.xwiki.objects.BaseObject;
38    import com.xpn.xwiki.store.migration.hibernate.HibernateDataMigration;
39   
40    import static org.mockito.ArgumentMatchers.any;
41    import static org.mockito.ArgumentMatchers.eq;
42    import static org.mockito.Mockito.doThrow;
43    import static org.mockito.Mockito.mock;
44    import static org.mockito.Mockito.never;
45    import static org.mockito.Mockito.verify;
46    import static org.mockito.Mockito.when;
47   
 
48    public class WorkspaceMigrationTest
49    {
50    @Rule
51    public MockitoComponentMockingRule<WorkspacesMigration> mocker =
52    new MockitoComponentMockingRule(WorkspacesMigration.class, HibernateDataMigration.class,
53    "R530000WorkspacesMigration");
54   
55    private WikiDescriptorManager wikiDescriptorManager;
56   
57    private DocumentRestorerFromAttachedXAR documentRestorerFromAttachedXAR;
58   
59    private Execution execution;
60   
61    private XWikiContext xcontext;
62   
63    private XWiki xwiki;
64   
 
65  4 toggle @Before
66    public void setUp() throws Exception
67    {
68  4 wikiDescriptorManager = mocker.getInstance(WikiDescriptorManager.class);
69  4 documentRestorerFromAttachedXAR = mocker.getInstance(DocumentRestorerFromAttachedXAR.class);
70  4 execution = mock(Execution.class);
71  4 mocker.registerComponent(Execution.class, execution);
72  4 xcontext = mock(XWikiContext.class);
73  4 xwiki = mock(XWiki.class);
74   
75  4 ExecutionContext executionContext = mock(ExecutionContext.class);
76  4 when(execution.getContext()).thenReturn(executionContext);
77  4 when(executionContext.getProperty("xwikicontext")).thenReturn(xcontext);
78  4 when(xcontext.getWiki()).thenReturn(xwiki);
79  4 when(wikiDescriptorManager.getMainWikiId()).thenReturn("mainWiki");
80    }
81   
 
82  1 toggle @Test
83    public void upgradeWorkspace() throws Exception
84    {
85    // Mocks about the descriptor
86  1 when(wikiDescriptorManager.getCurrentWikiId()).thenReturn("workspace");
87  1 XWikiDocument oldDescriptorDocument = mock(XWikiDocument.class);
88  1 when(xwiki.getDocument(eq(new DocumentReference("mainWiki", XWiki.SYSTEM_SPACE, "XWikiServerWorkspace")),
89    any(XWikiContext.class))).thenReturn(oldDescriptorDocument);
90   
91    // Mocks about the old workspace object
92  1 BaseObject oldWorkspaceObject = mock(BaseObject.class);
93  1 when(oldDescriptorDocument.getXObject(eq(new DocumentReference("mainWiki", "WorkspaceManager",
94    "WorkspaceClass")))).thenReturn(oldWorkspaceObject);
95   
96    // Mocks about the old document to restore form the main wiki
97  1 DocumentReference documentToRestore2 = new DocumentReference("mainWiki", "XWiki", "RegistrationConfig");
98  1 XWikiDocument documentToRestore2FromMainWiki = mock(XWikiDocument.class);
99  1 when(xwiki.getDocument(eq(documentToRestore2), any(XWikiContext.class))).
100    thenReturn(documentToRestore2FromMainWiki);
101  1 when(xwiki.exists(documentToRestore2, xcontext)).thenReturn(true);
102   
103    // Run
104  1 mocker.getComponentUnderTest().hibernateMigrate();
105   
106    // Verify we try to restore the documents from the xar
107  1 verify(documentRestorerFromAttachedXAR).restoreDocumentFromAttachedXAR(eq(new DocumentReference("mainWiki",
108    "WorkspaceManager", "Install")), eq("workspace-template.xar"), any(List.class));
109   
110    // Verify the document to restore has been restored from the main wiki
111  1 verify(xwiki).copyDocument(eq(documentToRestore2),
112    eq(new DocumentReference("workspace", "XWiki", "RegistrationConfig")), any(XWikiContext.class));
113   
114    // Verify that the log contains a warning about the documents that the migration failed to restore
115  1 verify(mocker.getMockedLogger()).warn("Failed to restore some documents: [{}]. You should import manually " +
116    "(1) xwiki-platform-administration-ui.xar and then (2) xwiki-platform-wiki-ui-wiki.xar into your" +
117    " wiki, to restore these documents.", "workspace:XWiki.AdminRegistrationSheet, " +
118    "workspace:XWiki.RegistrationHelp, workspace:XWiki.AdminUsersSheet");
119    }
120   
 
121  1 toggle @Test
122    public void upgradeWorkspaceTemplate() throws Exception
123    {
124    // Mocks about the descriptor
125  1 when(wikiDescriptorManager.getCurrentWikiId()).thenReturn("workspacetemplate");
126  1 XWikiDocument oldDescriptorDocument = mock(XWikiDocument.class);
127  1 when(xwiki.getDocument(eq(new DocumentReference("mainWiki", XWiki.SYSTEM_SPACE,
128    "XWikiServerWorkspacetemplate")), any(XWikiContext.class))).thenReturn(oldDescriptorDocument);
129   
130    // Mock that the workspace special page exists
131  1 DocumentReference workspacePageReference = new DocumentReference("workspacetemplate", "XWiki",
132    "ManageWorkspace");
133  1 when(xwiki.exists(eq(workspacePageReference), any(XWikiContext.class))).thenReturn(true);
134   
135    // Run
136  1 mocker.getComponentUnderTest().hibernateMigrate();
137   
138    // Verify that the log contains a warning about the documents that the migration failed to restore
139  1 verify(mocker.getMockedLogger()).warn("Failed to restore some documents: [{}]. You should import manually " +
140    "(1) xwiki-platform-administration-ui.xar and then (2) xwiki-platform-wiki-ui-wiki.xar into your" +
141    " wiki, to restore these documents.", "workspacetemplate:XWiki.AdminRegistrationSheet, " +
142    "workspacetemplate:XWiki.RegistrationConfig, workspacetemplate:XWiki.RegistrationHelp, " +
143    "workspacetemplate:XWiki.AdminUsersSheet");
144    }
145   
 
146  1 toggle @Test
147    public void upgradeRegularSubwiki() throws Exception
148    {
149    // Mocks about the descriptor
150  1 when(wikiDescriptorManager.getCurrentWikiId()).thenReturn("subwiki");
151  1 XWikiDocument oldDescriptorDocument = mock(XWikiDocument.class);
152  1 when(xwiki.getDocument(eq(new DocumentReference("mainWiki", XWiki.SYSTEM_SPACE,
153    "XWikiServerSubwiki")), any(XWikiContext.class))).thenReturn(oldDescriptorDocument);
154   
155    // Run
156  1 mocker.getComponentUnderTest().hibernateMigrate();
157   
158    // Verify that the migration did not try to restore old documents
159  1 verify(xwiki, never()).exists(eq(new DocumentReference("subwiki", "XWiki", "AdminRegistrationSheet")),
160    any(XWikiContext.class));
161    }
162   
 
163  1 toggle @Test
164    public void errorWhenRestoringFromXAR() throws Exception
165    {
166    // Mocks about the descriptor
167  1 when(wikiDescriptorManager.getCurrentWikiId()).thenReturn("workspace");
168  1 XWikiDocument oldDescriptorDocument = mock(XWikiDocument.class);
169  1 when(xwiki.getDocument(eq(new DocumentReference("mainWiki", XWiki.SYSTEM_SPACE, "XWikiServerWorkspace")),
170    any(XWikiContext.class))).thenReturn(oldDescriptorDocument);
171    // Mocks about the old workspace object
172  1 BaseObject oldWorkspaceObject = mock(BaseObject.class);
173  1 when(oldDescriptorDocument.getXObject(eq(new DocumentReference("mainWiki", "WorkspaceManager",
174    "WorkspaceClass")))).thenReturn(oldWorkspaceObject);
175   
176  1 doThrow(new XWikiException()).when(documentRestorerFromAttachedXAR).restoreDocumentFromAttachedXAR(
177    any(DocumentReference.class), any(String.class), any(List.class));
178    // Run
179  1 mocker.getComponentUnderTest().hibernateMigrate();
180   
181    // Verify
182  1 verify(mocker.getMockedLogger()).error(eq("Error while restoring documents from the Workspace XAR"),
183    any(XWikiException.class));
184    }
185   
186   
187    }