1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.wiki.internal.provisioning

File DefaultWikiCopierTest.java

 

Code metrics

0
39
2
1
132
83
2
0.05
19.5
2
1

Classes

Class Line # Actions
DefaultWikiCopierTest 51 39 0% 2 0
1.0100%
 

Contributing tests

This file is covered by 1 test. .

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.internal.provisioning;
21   
22    import java.util.Arrays;
23    import java.util.List;
24   
25    import javax.inject.Provider;
26   
27    import org.junit.Before;
28    import org.junit.Rule;
29    import org.junit.Test;
30    import org.mockito.InOrder;
31    import org.slf4j.Logger;
32    import org.xwiki.job.event.status.JobProgressManager;
33    import org.xwiki.model.reference.DocumentReference;
34    import org.xwiki.model.reference.DocumentReferenceResolver;
35    import org.xwiki.model.reference.WikiReference;
36    import org.xwiki.query.Query;
37    import org.xwiki.query.QueryManager;
38    import org.xwiki.test.mockito.MockitoComponentMockingRule;
39   
40    import com.xpn.xwiki.XWikiContext;
41   
42    import static org.mockito.ArgumentMatchers.eq;
43    import static org.mockito.Mockito.inOrder;
44    import static org.mockito.Mockito.mock;
45    import static org.mockito.Mockito.verify;
46    import static org.mockito.Mockito.when;
47   
48    /**
49    * @version $Id: 19656d1821d01acced598003513cd39293f53f70 $
50    */
 
51    public class DefaultWikiCopierTest
52    {
53    @Rule
54    public MockitoComponentMockingRule<DefaultWikiCopier> mocker = new MockitoComponentMockingRule(
55    DefaultWikiCopier.class);
56   
57    private QueryManager queryManager;
58   
59    private Provider<XWikiContext> xcontextProvider;
60   
61    private DocumentReferenceResolver<String> documentReferenceResolver;
62   
63    private JobProgressManager progress;
64   
65    private Logger logger;
66   
67    private XWikiContext xcontext;
68   
69    private com.xpn.xwiki.XWiki xwiki;
70   
 
71  1 toggle @Before
72    public void setUp() throws Exception
73    {
74  1 queryManager = mocker.getInstance(QueryManager.class);
75  1 progress = mocker.getInstance(JobProgressManager.class);
76  1 xcontextProvider = mocker.registerMockComponent(XWikiContext.TYPE_PROVIDER);
77  1 xcontext = mock(XWikiContext.class);
78  1 when(xcontextProvider.get()).thenReturn(xcontext);
79  1 xwiki = mock(com.xpn.xwiki.XWiki.class);
80  1 when(xcontext.getWiki()).thenReturn(xwiki);
81   
82  1 documentReferenceResolver = mocker.getInstance(DocumentReferenceResolver.TYPE_STRING, "current");
83    }
84   
 
85  1 toggle @Test
86    public void copyDocuments() throws Exception
87    {
88    // Mocks
89  1 Query query = mock(Query.class);
90  1 when(queryManager.createQuery("select distinct doc.fullName from Document as doc", Query.XWQL)).thenReturn(
91    query);
92   
93  1 List<String> documentList = Arrays.asList("Space.Doc1", "Space.Doc2", "Space.Doc3");
94  1 when(query.<String>execute()).thenReturn(documentList);
95   
96  1 WikiReference fromWikiReference = new WikiReference("wikiA");
97  1 DocumentReference docRef1 = new DocumentReference("wikiA", "Space", "Doc1");
98  1 DocumentReference docRef2 = new DocumentReference("wikiA", "Space", "Doc2");
99  1 DocumentReference docRef3 = new DocumentReference("wikiA", "Space", "Doc3");
100  1 DocumentReference copydocRef1 = new DocumentReference("wikiB", "Space", "Doc1");
101  1 DocumentReference copydocRef2 = new DocumentReference("wikiB", "Space", "Doc2");
102  1 DocumentReference copydocRef3 = new DocumentReference("wikiB", "Space", "Doc3");
103  1 when(documentReferenceResolver.resolve(eq("Space.Doc1"), eq(fromWikiReference))).thenReturn(docRef1);
104  1 when(documentReferenceResolver.resolve(eq("Space.Doc2"), eq(fromWikiReference))).thenReturn(docRef2);
105  1 when(documentReferenceResolver.resolve(eq("Space.Doc3"), eq(fromWikiReference))).thenReturn(docRef3);
106   
107    // Test
108  1 mocker.getComponentUnderTest().copyDocuments("wikiA", "wikiB", false);
109   
110    // Verify
111  1 verify(query).setWiki("wikiA");
112  1 InOrder inOrder = inOrder(progress, xwiki, mocker.getMockedLogger());
113  1 inOrder.verify(progress).pushLevelProgress(3, mocker.getComponentUnderTest());
114   
115  1 inOrder.verify(progress).startStep(mocker.getComponentUnderTest());
116  1 inOrder.verify(mocker.getMockedLogger()).info("Copying document [{}] to [{}].", docRef1, copydocRef1);
117  1 inOrder.verify(xwiki).copyDocument(docRef1, copydocRef1, null, true, true, xcontext);
118  1 inOrder.verify(mocker.getMockedLogger()).info("Done copying document [{}] to [{}].", docRef1, copydocRef1);
119   
120  1 inOrder.verify(progress).startStep(mocker.getComponentUnderTest());
121  1 inOrder.verify(mocker.getMockedLogger()).info("Copying document [{}] to [{}].", docRef2, copydocRef2);
122  1 inOrder.verify(xwiki).copyDocument(docRef2, copydocRef2, null, true, true, xcontext);
123  1 inOrder.verify(mocker.getMockedLogger()).info("Done copying document [{}] to [{}].", docRef2, copydocRef2);
124   
125  1 inOrder.verify(progress).startStep(mocker.getComponentUnderTest());
126  1 inOrder.verify(mocker.getMockedLogger()).info("Copying document [{}] to [{}].", docRef3, copydocRef3);
127  1 inOrder.verify(xwiki).copyDocument(docRef3, copydocRef3, null, true, true, xcontext);
128  1 inOrder.verify(mocker.getMockedLogger()).info("Done copying document [{}] to [{}].", docRef3, copydocRef3);
129   
130  1 inOrder.verify(progress).popLevelProgress(mocker.getComponentUnderTest());
131    }
132    }