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

File DefaultDocumentRestorerFromAttachedXARTest.java

 

Code metrics

0
44
5
1
169
105
5
0.11
8.8
5
1

Classes

Class Line # Actions
DefaultDocumentRestorerFromAttachedXARTest 51 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.io.IOException;
23    import java.io.InputStream;
24    import java.util.ArrayList;
25    import java.util.List;
26   
27    import javax.inject.Provider;
28   
29    import org.junit.Before;
30    import org.junit.Rule;
31    import org.junit.Test;
32    import org.xwiki.model.reference.DocumentReference;
33    import org.xwiki.test.mockito.MockitoComponentMockingRule;
34   
35    import com.xpn.xwiki.XWikiContext;
36    import com.xpn.xwiki.doc.XWikiAttachment;
37    import com.xpn.xwiki.doc.XWikiDocument;
38   
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.times;
43    import static org.mockito.Mockito.verify;
44    import static org.mockito.Mockito.when;
45   
46    /**
47    * Tests for {@link DefaultDocumentRestorerFromAttachedXAR}.
48    *
49    * @since 5.3RC1
50    */
 
51    public class DefaultDocumentRestorerFromAttachedXARTest
52    {
53    @Rule
54    public MockitoComponentMockingRule<DefaultDocumentRestorerFromAttachedXAR> mocker =
55    new MockitoComponentMockingRule(DefaultDocumentRestorerFromAttachedXAR.class);
56   
57    private Provider<XWikiContext> xcontextProvider;
58   
59    private XWikiContext xcontext;
60   
61    private com.xpn.xwiki.XWiki xwiki;
62   
63    private XWikiDocument docToRestore1;
64   
65    private XWikiDocument docToRestore2;
66   
67    private List<DocumentReference> docsToRestore;
68   
 
69  4 toggle @Before
70    public void setUp() throws Exception
71    {
72  4 xcontextProvider = mocker.registerMockComponent(XWikiContext.TYPE_PROVIDER);
73  4 xcontext = mock(XWikiContext.class);
74  4 when(xcontextProvider.get()).thenReturn(xcontext);
75  4 xwiki = mock(com.xpn.xwiki.XWiki.class);
76  4 when(xcontext.getWiki()).thenReturn(xwiki);
77   
78    // Inputs
79  4 docToRestore1 = mock(XWikiDocument.class);
80  4 DocumentReference docToRestoreRef1 = new DocumentReference("workspace", "XWiki", "AdminRegistrationSheet");
81  4 when(xwiki.getDocument(eq(docToRestoreRef1), any(XWikiContext.class))).thenReturn(docToRestore1);
82  4 docToRestore2 = mock(XWikiDocument.class);
83  4 DocumentReference docToRestoreRef2 = new DocumentReference("workspace", "XWiki", "RegistrationHelp");
84  4 when(xwiki.getDocument(eq(docToRestoreRef2), any(XWikiContext.class))).thenReturn(docToRestore2);
85  4 docsToRestore = new ArrayList<DocumentReference>();
86  4 docsToRestore.add(docToRestoreRef1);
87  4 docsToRestore.add(docToRestoreRef2);
88    }
89   
 
90  1 toggle @Test
91    public void restoreDocumentFromAttachedXAR() throws Exception
92    {
93    // Mocks
94  1 XWikiDocument workspaceInstallDoc = mock(XWikiDocument.class);
95  1 when(xwiki.getDocument(eq(new DocumentReference("mainWiki", "WorkspaceManager", "Install")),
96    any(XWikiContext.class))).thenReturn(workspaceInstallDoc);
97  1 XWikiAttachment xeXar = mock(XWikiAttachment.class);
98  1 when(workspaceInstallDoc.getAttachment("workspace-template.xar")).thenReturn(xeXar);
99  1 when(xeXar.getContentInputStream(any(XWikiContext.class))).thenReturn(
100    getClass().getResourceAsStream("/test-restore-documents.xar"));
101   
102    // Run
103  1 mocker.getComponentUnderTest().restoreDocumentFromAttachedXAR(new DocumentReference("mainWiki",
104    "WorkspaceManager", "Install"), "workspace-template.xar", docsToRestore);
105   
106    // Verify the document to restore has been restored from the xar
107  1 verify(docToRestore1).fromXML(any(InputStream.class));
108  1 verify(xwiki, times(1)).saveDocument(docToRestore1, xcontext);
109  1 verify(docToRestore2).fromXML(any(InputStream.class));
110  1 verify(xwiki, times(1)).saveDocument(docToRestore2, xcontext);
111    }
112   
 
113  1 toggle @Test
114    public void errorWhenNoDocument() throws Exception
115    {
116    // Mocks
117  1 XWikiDocument workspaceInstallDoc = mock(XWikiDocument.class);
118  1 DocumentReference workspaceInstallDocRef = new DocumentReference("mainWiki", "WorkspaceManager", "Install");
119  1 when(xwiki.getDocument(eq(workspaceInstallDocRef), any(XWikiContext.class))).thenReturn(workspaceInstallDoc);
120  1 when(workspaceInstallDoc.isNew()).thenReturn(true);
121   
122    // Run
123  1 mocker.getComponentUnderTest().restoreDocumentFromAttachedXAR(new DocumentReference("mainWiki",
124    "WorkspaceManager", "Install"), "workspace-template.xar", docsToRestore);
125   
126    // Verify
127  1 verify(mocker.getMockedLogger()).warn("[{}] does not exist", workspaceInstallDocRef);
128    }
129   
 
130  1 toggle @Test
131    public void errorWhenNoAttachment() throws Exception
132    {
133    // Mocks
134  1 XWikiDocument workspaceInstallDoc = mock(XWikiDocument.class);
135  1 DocumentReference workspaceInstallDocRef = new DocumentReference("mainWiki", "WorkspaceManager", "Install");
136  1 when(xwiki.getDocument(eq(workspaceInstallDocRef), any(XWikiContext.class))).thenReturn(workspaceInstallDoc);
137  1 when(workspaceInstallDoc.getAttachment("workspace-template.xar")).thenReturn(null);
138   
139    // Run
140  1 mocker.getComponentUnderTest().restoreDocumentFromAttachedXAR(new DocumentReference("mainWiki",
141    "WorkspaceManager", "Install"), "workspace-template.xar", docsToRestore);
142   
143    // Verify
144  1 verify(mocker.getMockedLogger()).warn("[{}] has no attachment named [{}].", workspaceInstallDocRef,
145    "workspace-template.xar");
146    }
147   
 
148  1 toggle @Test
149    public void errorZipInvalid() throws Exception
150    {
151    // Mocks
152  1 XWikiDocument workspaceInstallDoc = mock(XWikiDocument.class);
153  1 DocumentReference workspaceInstallDocRef = new DocumentReference("mainWiki", "WorkspaceManager", "Install");
154  1 when(xwiki.getDocument(eq(workspaceInstallDocRef), any(XWikiContext.class))).thenReturn(workspaceInstallDoc);
155  1 XWikiAttachment xeXar = mock(XWikiAttachment.class);
156  1 when(workspaceInstallDoc.getAttachment("workspace-template.xar")).thenReturn(xeXar);
157  1 when(xeXar.getContentInputStream(any(XWikiContext.class))).thenReturn(
158    getClass().getResourceAsStream("/invalid-xar.xar"));
159   
160    // Run
161  1 mocker.getComponentUnderTest().restoreDocumentFromAttachedXAR(new DocumentReference("mainWiki",
162    "WorkspaceManager", "Install"), "workspace-template.xar", docsToRestore);
163   
164    // Verify
165  1 verify(mocker.getMockedLogger()).error(eq("Error during the decompression of [{}]."),
166    eq("workspace-template.xar"), any(IOException.class));
167    }
168   
169    }