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

File WikiDescriptorMigratorTest.java

 

Code metrics

0
49
6
1
183
116
6
0.12
8.17
6
1

Classes

Class Line # Actions
WikiDescriptorMigratorTest 55 49 0% 6 0
1.0100%
 

Contributing tests

This file is covered by 5 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.internal.descriptor.migrator;
21   
22    import java.util.ArrayList;
23    import java.util.List;
24   
25    import javax.inject.Provider;
26   
27    import org.apache.commons.lang3.exception.ExceptionUtils;
28    import org.junit.Before;
29    import org.junit.Rule;
30    import org.junit.Test;
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.QueryException;
35    import org.xwiki.query.QueryManager;
36    import org.xwiki.test.mockito.MockitoComponentMockingRule;
37    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
38    import org.xwiki.wiki.internal.descriptor.document.XWikiServerClassDocumentInitializer;
39   
40    import com.xpn.xwiki.XWiki;
41    import com.xpn.xwiki.XWikiContext;
42    import com.xpn.xwiki.XWikiException;
43    import com.xpn.xwiki.doc.XWikiDocument;
44    import com.xpn.xwiki.objects.BaseObject;
45    import com.xpn.xwiki.store.migration.hibernate.HibernateDataMigration;
46   
47    import static org.junit.Assert.assertFalse;
48    import static org.junit.Assert.assertTrue;
49    import static org.mockito.ArgumentMatchers.any;
50    import static org.mockito.ArgumentMatchers.eq;
51    import static org.mockito.Mockito.mock;
52    import static org.mockito.Mockito.verify;
53    import static org.mockito.Mockito.when;
54   
 
55    public class WikiDescriptorMigratorTest
56    {
57    @Rule
58    public MockitoComponentMockingRule<WikiDescriptorMigrator> mocker =
59    new MockitoComponentMockingRule(WikiDescriptorMigrator.class, HibernateDataMigration.class,
60    "R54300WikiDescriptorMigration");
61   
62    private QueryManager queryManager;
63   
64    private WikiDescriptorManager wikiDescriptorManager;
65   
66    private Provider<XWikiContext> xcontextProvider;
67   
68    private DocumentReferenceResolver<String> documentReferenceResolver;
69   
70    private XWikiContext context;
71   
72    private XWiki xwiki;
73   
 
74  5 toggle @Before
75    public void setUp() throws Exception
76    {
77  5 xcontextProvider = mocker.registerMockComponent(XWikiContext.TYPE_PROVIDER);
78  5 context = mock(XWikiContext.class);
79  5 when(xcontextProvider.get()).thenReturn(context);
80  5 xwiki = mock(XWiki.class);
81  5 when(context.getWiki()).thenReturn(xwiki);
82   
83  5 queryManager = mocker.getInstance(QueryManager.class);
84  5 documentReferenceResolver= mocker.getInstance(DocumentReferenceResolver.TYPE_STRING);
85   
86  5 wikiDescriptorManager = mocker.getInstance(WikiDescriptorManager.class);
87    }
88   
 
89  1 toggle @Test
90    public void hibernateMigrate() throws Exception
91    {
92  1 List<String> documentList = new ArrayList<>();
93  1 documentList.add("XWiki.XWikiServerSubwiki1");
94   
95  1 Query query = mock(Query.class);
96  1 when(queryManager.createQuery(any(), eq(Query.HQL))).thenReturn(query);
97  1 when(query.<String>execute()).thenReturn(documentList);
98   
99  1 DocumentReference documentReference = new DocumentReference("mainWiki", "XWiki", "XWikiServerSubwiki1");
100  1 when(documentReferenceResolver.resolve(documentList.get(0))).thenReturn(documentReference);
101   
102  1 XWikiDocument document = mock(XWikiDocument.class);
103  1 when(xwiki.getDocument(documentReference, context)).thenReturn(document);
104   
105  1 List<BaseObject> objects = new ArrayList<>();
106  1 objects.add(null);
107   
108  1 BaseObject object = mock(BaseObject.class);
109  1 objects.add(object);
110   
111  1 when(document.getXObjects(XWikiServerClassDocumentInitializer.SERVER_CLASS)).thenReturn(objects);
112  1 when(object.getStringValue(XWikiServerClassDocumentInitializer.FIELD_WIKIPRETTYNAME)).thenReturn("");
113   
114    // Test
115  1 mocker.getComponentUnderTest().hibernateMigrate();
116   
117    // Verify
118  1 verify(object).setStringValue(XWikiServerClassDocumentInitializer.FIELD_WIKIPRETTYNAME, "Subwiki1");
119  1 verify(xwiki).saveDocument(document, "[UPGRADE] Set a default pretty name.", context);
120   
121    }
122   
 
123  1 toggle @Test
124    public void hibernateMigrateWhenQueryException() throws Exception
125    {
126  1 List<String> documentList = new ArrayList<>();
127  1 documentList.add("XWiki.XWikiServerSubwiki1");
128   
129  1 Exception exception = new QueryException("error in queryManager.createQuery()", null, null);
130  1 when(queryManager.createQuery(any(), eq(Query.HQL))).thenThrow(exception);
131   
132    // Test
133  1 mocker.getComponentUnderTest().hibernateMigrate();
134   
135    // Verify
136  1 verify(mocker.getMockedLogger()).error("Failed to perform a query on the main wiki.", exception);
137    }
138   
 
139  1 toggle @Test
140    public void hibernateMigrateWhenXWikiException() throws Exception
141    {
142  1 List<String> documentList = new ArrayList<>();
143  1 documentList.add("XWiki.XWikiServerSubwiki1");
144   
145  1 Query query = mock(Query.class);
146  1 when(queryManager.createQuery(any(), eq(Query.HQL))).thenReturn(query);
147  1 when(query.<String>execute()).thenReturn(documentList);
148   
149  1 DocumentReference documentReference = new DocumentReference("mainWiki", "XWiki", "XWikiServerSubwiki1");
150  1 when(documentReferenceResolver.resolve(documentList.get(0))).thenReturn(documentReference);
151   
152  1 Exception exception = new XWikiException(0, 0, "error in xwiki.getDocument()");
153  1 when(xwiki.getDocument(documentReference, context)).thenThrow(exception);
154   
155    // Test
156  1 mocker.getComponentUnderTest().hibernateMigrate();
157   
158    // Verify
159  1 verify(mocker.getMockedLogger()).warn("Failed to get or save the wiki descriptor document [{}]. You" +
160    " will not see the corresponding wiki in the Wiki Index unless you give it a Pretty Name manually. {}",
161    documentList.get(0), ExceptionUtils.getRootCauseMessage(exception));
162    }
163   
 
164  1 toggle @Test
165    public void shouldExecuteTrue() throws Exception
166    {
167  1 when(wikiDescriptorManager.getCurrentWikiId()).thenReturn("mainWiki");
168  1 when(wikiDescriptorManager.getMainWikiId()).thenReturn("mainWiki");
169   
170    // Test
171  1 assertTrue(mocker.getComponentUnderTest().shouldExecute(null));
172    }
173   
 
174  1 toggle @Test
175    public void shouldExecuteFalse() throws Exception
176    {
177  1 when(wikiDescriptorManager.getCurrentWikiId()).thenReturn("subwiki");
178  1 when(wikiDescriptorManager.getMainWikiId()).thenReturn("mainWiki");
179   
180    // Test
181  1 assertFalse(mocker.getComponentUnderTest().shouldExecute(null));
182    }
183    }