1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (49) |
Complexity: 5 |
Complexity Density: 0.11 |
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
|
65 |
4 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 1 |
Complexity Density: 0.08 |
1PASS
|
|
82 |
1 |
@Test... |
83 |
|
public void upgradeWorkspace() throws Exception |
84 |
|
{ |
85 |
|
|
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 |
|
|
92 |
1 |
BaseObject oldWorkspaceObject = mock(BaseObject.class); |
93 |
1 |
when(oldDescriptorDocument.getXObject(eq(new DocumentReference("mainWiki", "WorkspaceManager", |
94 |
|
"WorkspaceClass")))).thenReturn(oldWorkspaceObject); |
95 |
|
|
96 |
|
|
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 |
|
|
104 |
1 |
mocker.getComponentUnderTest().hibernateMigrate(); |
105 |
|
|
106 |
|
|
107 |
1 |
verify(documentRestorerFromAttachedXAR).restoreDocumentFromAttachedXAR(eq(new DocumentReference("mainWiki", |
108 |
|
"WorkspaceManager", "Install")), eq("workspace-template.xar"), any(List.class)); |
109 |
|
|
110 |
|
|
111 |
1 |
verify(xwiki).copyDocument(eq(documentToRestore2), |
112 |
|
eq(new DocumentReference("workspace", "XWiki", "RegistrationConfig")), any(XWikiContext.class)); |
113 |
|
|
114 |
|
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
121 |
1 |
@Test... |
122 |
|
public void upgradeWorkspaceTemplate() throws Exception |
123 |
|
{ |
124 |
|
|
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 |
|
|
131 |
1 |
DocumentReference workspacePageReference = new DocumentReference("workspacetemplate", "XWiki", |
132 |
|
"ManageWorkspace"); |
133 |
1 |
when(xwiki.exists(eq(workspacePageReference), any(XWikiContext.class))).thenReturn(true); |
134 |
|
|
135 |
|
|
136 |
1 |
mocker.getComponentUnderTest().hibernateMigrate(); |
137 |
|
|
138 |
|
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
146 |
1 |
@Test... |
147 |
|
public void upgradeRegularSubwiki() throws Exception |
148 |
|
{ |
149 |
|
|
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 |
|
|
156 |
1 |
mocker.getComponentUnderTest().hibernateMigrate(); |
157 |
|
|
158 |
|
|
159 |
1 |
verify(xwiki, never()).exists(eq(new DocumentReference("subwiki", "XWiki", "AdminRegistrationSheet")), |
160 |
|
any(XWikiContext.class)); |
161 |
|
} |
162 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
1PASS
|
|
163 |
1 |
@Test... |
164 |
|
public void errorWhenRestoringFromXAR() throws Exception |
165 |
|
{ |
166 |
|
|
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 |
|
|
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 |
|
|
179 |
1 |
mocker.getComponentUnderTest().hibernateMigrate(); |
180 |
|
|
181 |
|
|
182 |
1 |
verify(mocker.getMockedLogger()).error(eq("Error while restoring documents from the Workspace XAR"), |
183 |
|
any(XWikiException.class)); |
184 |
|
} |
185 |
|
|
186 |
|
|
187 |
|
} |