| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 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.lang.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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (55) |
Complexity: 6 |
Complexity Density: 0.12 |
|
| 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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
|
| 74 |
5 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (18) |
Complexity: 1 |
Complexity Density: 0.06 |
1PASS
|
|
| 89 |
1 |
@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 |
|
|
| 115 |
1 |
mocker.getComponentUnderTest().hibernateMigrate(); |
| 116 |
|
|
| 117 |
|
|
| 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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
| 123 |
1 |
@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 |
|
|
| 133 |
1 |
mocker.getComponentUnderTest().hibernateMigrate(); |
| 134 |
|
|
| 135 |
|
|
| 136 |
1 |
verify(mocker.getMockedLogger()).error("Failed to perform a query on the main wiki.", exception); |
| 137 |
|
} |
| 138 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
1PASS
|
|
| 139 |
1 |
@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 |
|
|
| 156 |
1 |
mocker.getComponentUnderTest().hibernateMigrate(); |
| 157 |
|
|
| 158 |
|
|
| 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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
| 164 |
1 |
@Test... |
| 165 |
|
public void shouldExecuteTrue() throws Exception |
| 166 |
|
{ |
| 167 |
1 |
when(wikiDescriptorManager.getCurrentWikiId()).thenReturn("mainWiki"); |
| 168 |
1 |
when(wikiDescriptorManager.getMainWikiId()).thenReturn("mainWiki"); |
| 169 |
|
|
| 170 |
|
|
| 171 |
1 |
assertTrue(mocker.getComponentUnderTest().shouldExecute(null)); |
| 172 |
|
} |
| 173 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
| 174 |
1 |
@Test... |
| 175 |
|
public void shouldExecuteFalse() throws Exception |
| 176 |
|
{ |
| 177 |
1 |
when(wikiDescriptorManager.getCurrentWikiId()).thenReturn("subwiki"); |
| 178 |
1 |
when(wikiDescriptorManager.getMainWikiId()).thenReturn("mainWiki"); |
| 179 |
|
|
| 180 |
|
|
| 181 |
1 |
assertFalse(mocker.getComponentUnderTest().shouldExecute(null)); |
| 182 |
|
} |
| 183 |
|
} |