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

File WikiTemplateMigrationTest.java

 

Code metrics

0
91
4
1
226
149
4
0.04
22.75
4
1

Classes

Class Line # Actions
WikiTemplateMigrationTest 59 91 0% 4 0
1.0100%
 

Contributing tests

This file is covered by 3 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.template.internal.migration;
21   
22    import java.util.ArrayList;
23    import java.util.List;
24   
25    import org.junit.Before;
26    import org.junit.Rule;
27    import org.junit.Test;
28    import org.xwiki.component.util.DefaultParameterizedType;
29    import org.xwiki.context.Execution;
30    import org.xwiki.context.ExecutionContext;
31    import org.xwiki.model.reference.DocumentReference;
32    import org.xwiki.model.reference.DocumentReferenceResolver;
33    import org.xwiki.query.Query;
34    import org.xwiki.query.QueryManager;
35    import org.xwiki.test.mockito.MockitoComponentMockingRule;
36    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
37   
38    import com.xpn.xwiki.XWiki;
39    import com.xpn.xwiki.XWikiContext;
40    import com.xpn.xwiki.doc.XWikiDocument;
41    import com.xpn.xwiki.objects.BaseObject;
42    import com.xpn.xwiki.store.migration.XWikiDBVersion;
43    import com.xpn.xwiki.store.migration.hibernate.HibernateDataMigration;
44   
45    import static org.junit.Assert.assertFalse;
46    import static org.junit.Assert.assertTrue;
47    import static org.mockito.ArgumentMatchers.any;
48    import static org.mockito.ArgumentMatchers.eq;
49    import static org.mockito.Mockito.mock;
50    import static org.mockito.Mockito.verify;
51    import static org.mockito.Mockito.when;
52   
53    /**
54    * Test class for {@link WikiTemplateMigration}.
55    *
56    * @since 5.4RC1
57    * @version $Id: 54b7c4d0b8917b0bf32c39ed6eeec81b4f281c49 $
58    */
 
59    public class WikiTemplateMigrationTest
60    {
61    @Rule
62    public MockitoComponentMockingRule<WikiTemplateMigration> mocker =
63    new MockitoComponentMockingRule(WikiTemplateMigration.class, HibernateDataMigration.class,
64    "R54000WikiTemplateMigration");
65   
66    private WikiDescriptorManager wikiDescriptorManager;
67   
68    private QueryManager queryManager;
69   
70    private DocumentReferenceResolver<String> documentReferenceResolver;
71   
72    private Execution execution;
73   
74    private XWikiContext xcontext;
75   
76    private XWiki xwiki;
77   
78    private Query query;
79   
 
80  3 toggle @Before
81    public void setUp() throws Exception
82    {
83  3 wikiDescriptorManager = mocker.getInstance(WikiDescriptorManager.class);
84  3 queryManager = mocker.getInstance(QueryManager.class);
85    //documentReferenceResolver = mocker.getInstance(DocumentReferenceResolver.TYPE_STRING, "current");
86  3 documentReferenceResolver = mocker.getInstance(new DefaultParameterizedType(null, DocumentReferenceResolver.class, String.class));
87  3 execution = mock(Execution.class);
88  3 mocker.registerComponent(Execution.class, execution);
89  3 xcontext = mock(XWikiContext.class);
90  3 xwiki = mock(XWiki.class);
91   
92  3 ExecutionContext executionContext = mock(ExecutionContext.class);
93  3 when(execution.getContext()).thenReturn(executionContext);
94  3 when(executionContext.getProperty("xwikicontext")).thenReturn(xcontext);
95  3 when(xcontext.getWiki()).thenReturn(xwiki);
96  3 when(wikiDescriptorManager.getMainWikiId()).thenReturn("mainWiki");
97   
98  3 query = mock(Query.class);
99  3 when(queryManager.createQuery(any(), eq(Query.XWQL))).thenReturn(query);
100    }
101   
 
102  1 toggle @Test
103    public void upgrade() throws Exception
104    {
105    // Mocks
106  1 when(wikiDescriptorManager.getCurrentWikiId()).thenReturn("mainWiki");
107   
108  1 String document1 = "XWiki.XWikiServerTemplate";
109  1 String document2 = "XWiki.XWikiServerNotATemplateAnymore";
110  1 String document3 = "XWiki.XWikiServerNotATemplate";
111  1 String document4 = "XWiki.XWikiServerTemplateNow";
112   
113  1 DocumentReference documentReference1 = new DocumentReference("mainWiki", "XWiki", "XWikiServerTemplate");
114  1 DocumentReference documentReference2 = new DocumentReference("mainWiki", "XWiki", "XWikiServerNotATemplateAnymore");
115  1 DocumentReference documentReference3 = new DocumentReference("mainWiki", "XWiki", "XWikiServerNotATemplate");
116  1 DocumentReference documentReference4 = new DocumentReference("mainWiki", "XWiki", "XWikiServerTemplateNow");
117   
118  1 XWikiDocument doc1 = mock(XWikiDocument.class);
119  1 XWikiDocument doc2 = mock(XWikiDocument.class);
120  1 XWikiDocument doc3 = mock(XWikiDocument.class);
121  1 XWikiDocument doc4 = mock(XWikiDocument.class);
122   
123    // Return the document
124  1 List<String> documents = new ArrayList<String>();
125  1 documents.add(document1);
126  1 documents.add(document2);
127  1 documents.add(document3);
128  1 documents.add(document4);
129  1 when(query.<String>execute()).thenReturn(documents);
130   
131    // Document Reference resolver
132  1 when(documentReferenceResolver.resolve(document1)).thenReturn(documentReference1);
133  1 when(documentReferenceResolver.resolve(document2)).thenReturn(documentReference2);
134  1 when(documentReferenceResolver.resolve(document3)).thenReturn(documentReference3);
135  1 when(documentReferenceResolver.resolve(document4)).thenReturn(documentReference4);
136   
137    // Document getter
138  1 when(xwiki.getDocument(documentReference1, xcontext)).thenReturn(doc1);
139  1 when(xwiki.getDocument(documentReference2, xcontext)).thenReturn(doc2);
140  1 when(xwiki.getDocument(documentReference3, xcontext)).thenReturn(doc3);
141  1 when(xwiki.getDocument(documentReference4, xcontext)).thenReturn(doc4);
142   
143    // WikiManager.WikiTemplateClass reference
144  1 DocumentReference templateClassReference = new DocumentReference("mainWiki", "WikiManager", "WikiTemplateClass");
145   
146    // XWiki.XWikiServerClass reference
147  1 DocumentReference descriptorClassReference = new DocumentReference("mainWiki", "XWiki", "XWikiServerClass");
148   
149  1 BaseObject descriptorObj1 = mock(BaseObject.class);
150  1 BaseObject descriptorObj2 = mock(BaseObject.class);
151  1 BaseObject descriptorObj3 = mock(BaseObject.class);
152  1 BaseObject descriptorObj4 = mock(BaseObject.class);
153  1 when(doc1.getXObject(eq(descriptorClassReference))).thenReturn(descriptorObj1);
154  1 when(doc2.getXObject(eq(descriptorClassReference))).thenReturn(descriptorObj2);
155  1 when(doc3.getXObject(eq(descriptorClassReference))).thenReturn(descriptorObj3);
156  1 when(doc4.getXObject(eq(descriptorClassReference))).thenReturn(descriptorObj4);
157   
158  1 when(descriptorObj1.getIntValue("iswikitemplate", 0)).thenReturn(1);
159  1 when(descriptorObj2.getIntValue("iswikitemplate", 0)).thenReturn(1);
160  1 when(descriptorObj3.getIntValue("iswikitemplate", 0)).thenReturn(0);
161  1 when(descriptorObj4.getIntValue("iswikitemplate", 0)).thenReturn(0);
162   
163  1 BaseObject wikitemplateObj1 = mock(BaseObject.class);
164  1 BaseObject wikitemplateObj2 = mock(BaseObject.class);
165  1 BaseObject wikitemplateObj3 = mock(BaseObject.class);
166  1 BaseObject wikitemplateObj4 = mock(BaseObject.class);
167  1 when(doc1.getXObject(eq(templateClassReference), eq(true), any(XWikiContext.class))).thenReturn(wikitemplateObj1);
168  1 when(doc2.getXObject(eq(templateClassReference), eq(true), any(XWikiContext.class))).thenReturn(wikitemplateObj2);
169  1 when(doc3.getXObject(eq(templateClassReference), eq(true), any(XWikiContext.class))).thenReturn(wikitemplateObj3);
170  1 when(doc4.getXObject(eq(templateClassReference), eq(true), any(XWikiContext.class))).thenReturn(wikitemplateObj4);
171   
172  1 when(wikitemplateObj1.getIntValue("iswikitemplate", 1)).thenReturn(1);
173  1 when(wikitemplateObj2.getIntValue("iswikitemplate", 1)).thenReturn(0);
174  1 when(wikitemplateObj3.getIntValue("iswikitemplate", 0)).thenReturn(0);
175  1 when(wikitemplateObj4.getIntValue("iswikitemplate", 0)).thenReturn(1);
176   
177    // Run
178  1 mocker.getComponentUnderTest().hibernateMigrate();
179   
180    // Verify
181   
182    // doc1 is a template
183  1 verify(wikitemplateObj1).setIntValue("iswikitemplate", 1);
184    // doc2 is a not a template anymore
185  1 verify(wikitemplateObj2).setIntValue("iswikitemplate", 0);
186    // doc3 has never been a template
187  1 verify(wikitemplateObj3).setIntValue("iswikitemplate", 0);
188    // doc4 has became a template
189  1 verify(wikitemplateObj4).setIntValue("iswikitemplate", 1);
190   
191    // the old properties has been removed
192  1 verify(descriptorObj1).removeField("iswikitemplate");
193  1 verify(descriptorObj2).removeField("iswikitemplate");
194  1 verify(descriptorObj3).removeField("iswikitemplate");
195  1 verify(descriptorObj4).removeField("iswikitemplate");
196   
197    // superadmin is the author
198  1 DocumentReference superadmin = new DocumentReference("mainWiki", "XWiki", "superadmin");
199  1 verify(doc1).setAuthorReference(eq(superadmin));
200  1 verify(doc2).setAuthorReference(eq(superadmin));
201  1 verify(doc3).setAuthorReference(eq(superadmin));
202  1 verify(doc4).setAuthorReference(eq(superadmin));
203   
204    // all the documents has been saved
205  1 verify(xwiki).saveDocument(doc1, "[UPGRADE] Upgrade the template section.", xcontext);
206  1 verify(xwiki).saveDocument(doc2, "[UPGRADE] Upgrade the template section.", xcontext);
207  1 verify(xwiki).saveDocument(doc3, "[UPGRADE] Upgrade the template section.", xcontext);
208  1 verify(xwiki).saveDocument(doc4, "[UPGRADE] Upgrade the template section.", xcontext);
209    }
210   
 
211  1 toggle @Test
212    public void shouldExecuteTrue() throws Exception
213    {
214  1 when(wikiDescriptorManager.getCurrentWikiId()).thenReturn("mainWiki");
215  1 XWikiDBVersion version = new XWikiDBVersion(52000);
216  1 assertTrue(mocker.getComponentUnderTest().shouldExecute(version));
217    }
218   
 
219  1 toggle @Test
220    public void shouldExecuteFalse() throws Exception
221    {
222  1 when(wikiDescriptorManager.getCurrentWikiId()).thenReturn("subwiki");
223  1 XWikiDBVersion version = new XWikiDBVersion(52000);
224  1 assertFalse(mocker.getComponentUnderTest().shouldExecute(version));
225    }
226    }