use of com.xpn.xwiki.objects.BaseObject in project xwiki-platform by xwiki.
the class DefaultSearchSuggestCustomConfigDeleterTest method deleteSearchSuggestCustomConfig.
@Test
public void deleteSearchSuggestCustomConfig() throws Exception {
XWikiDocument searchSuggestConfigDoc = mock(XWikiDocument.class);
when(xwiki.getDocument(eq(new DocumentReference("mainWiki", "XWiki", "SearchSuggestConfig")), any(XWikiContext.class))).thenReturn(searchSuggestConfigDoc);
BaseObject objConfig1 = mock(BaseObject.class);
BaseObject objConfig2 = mock(BaseObject.class);
BaseObject objConfig3 = mock(BaseObject.class);
BaseObject objConfig4 = mock(BaseObject.class);
BaseObject objConfig5 = mock(BaseObject.class);
BaseObject objConfig6 = mock(BaseObject.class);
List<BaseObject> objects = new ArrayList<BaseObject>();
objects.add(objConfig1);
objects.add(objConfig2);
objects.add(objConfig3);
// null objects can be present in the list
objects.add(null);
objects.add(objConfig4);
objects.add(objConfig5);
objects.add(objConfig6);
when(searchSuggestConfigDoc.getXObjects(eq(new DocumentReference("mainWiki", "XWiki", "SearchSuggestSourceClass")))).thenReturn(objects);
// Object 1
when(objConfig1.getStringValue("name")).thenReturn("platform.workspace.searchSuggestSourceWorkspaces");
when(objConfig1.getStringValue("engine")).thenReturn("solr");
when(objConfig1.getStringValue("query")).thenReturn("class:XWiki.XWikiServerClass AND " + "propertyname:wikiprettyname AND propertyvalue__:(__INPUT__*)");
when(objConfig1.getStringValue("url")).thenReturn("xwiki:WorkspaceManager.WorkspacesSuggestSolrService");
// Object 2
when(objConfig2.getStringValue("name")).thenReturn("Bad name");
when(objConfig2.getStringValue("engine")).thenReturn("solr");
when(objConfig2.getStringValue("query")).thenReturn("class:XWiki.XWikiServerClass AND " + "propertyname:wikiprettyname AND propertyvalue__:(__INPUT__*)");
when(objConfig2.getStringValue("url")).thenReturn("xwiki:WorkspaceManager.WorkspacesSuggestSolrService");
// Object 3
when(objConfig3.getStringValue("name")).thenReturn("platform.workspace.searchSuggestSourceWorkspaces");
when(objConfig3.getStringValue("engine")).thenReturn("lucene");
when(objConfig3.getStringValue("query")).thenReturn("XWiki.XWikiServerClass.wikiprettyname:__INPUT__* AND " + "object:WorkspaceManager.WorkspaceClass");
when(objConfig3.getStringValue("url")).thenReturn("xwiki:WorkspaceManager.WorkspacesSuggestLuceneService");
// Object 4
when(objConfig4.getStringValue("name")).thenReturn("platform.workspace.searchSuggestSourceWorkspaces");
when(objConfig4.getStringValue("engine")).thenReturn(null);
when(objConfig4.getStringValue("query")).thenReturn("XWiki.XWikiServerClass.wikiprettyname:__INPUT__* AND " + "object:WorkspaceManager.WorkspaceClass");
when(objConfig4.getStringValue("url")).thenReturn("xwiki:WorkspaceManager.WorkspacesSuggestLuceneService");
// Object 5
when(objConfig5.getStringValue("name")).thenReturn("platform.workspace.searchSuggestSourceWorkspaces");
when(objConfig5.getStringValue("engine")).thenReturn(null);
when(objConfig5.getStringValue("query")).thenReturn("bad query");
when(objConfig5.getStringValue("url")).thenReturn("xwiki:WorkspaceManager.WorkspacesSuggestLuceneService");
// Object 6
when(objConfig6.getStringValue("name")).thenReturn("platform.workspace.searchSuggestSourceWorkspaces");
when(objConfig6.getStringValue("engine")).thenReturn(null);
when(objConfig6.getStringValue("query")).thenReturn("XWiki.XWikiServerClass.wikiprettyname:__INPUT__* AND " + "object:WorkspaceManager.WorkspaceClass");
when(objConfig6.getStringValue("url")).thenReturn("bad URL");
// Run
mocker.getComponentUnderTest().deleteSearchSuggestCustomConfig("mainWiki");
// Verify that the good objects has been removed
verify(searchSuggestConfigDoc).removeXObject(objConfig1);
verify(searchSuggestConfigDoc).removeXObject(objConfig3);
// Verify that the document have been saved
verify(xwiki).saveDocument(searchSuggestConfigDoc, "Remove object previously introduced by WorkspaceManager.Install", xcontext);
}
use of com.xpn.xwiki.objects.BaseObject in project xwiki-platform by xwiki.
the class WorkspaceMigrationTest method errorWhenRestoringFromXAR.
@Test
public void errorWhenRestoringFromXAR() throws Exception {
// Mocks about the descriptor
when(wikiDescriptorManager.getCurrentWikiId()).thenReturn("workspace");
XWikiDocument oldDescriptorDocument = mock(XWikiDocument.class);
when(xwiki.getDocument(eq(new DocumentReference("mainWiki", XWiki.SYSTEM_SPACE, "XWikiServerWorkspace")), any(XWikiContext.class))).thenReturn(oldDescriptorDocument);
// Mocks about the old workspace object
BaseObject oldWorkspaceObject = mock(BaseObject.class);
when(oldDescriptorDocument.getXObject(eq(new DocumentReference("mainWiki", "WorkspaceManager", "WorkspaceClass")))).thenReturn(oldWorkspaceObject);
doThrow(new XWikiException()).when(documentRestorerFromAttachedXAR).restoreDocumentFromAttachedXAR(any(DocumentReference.class), any(String.class), any(List.class));
// Run
mocker.getComponentUnderTest().hibernateMigrate();
// Verify
verify(mocker.getMockedLogger()).error(eq("Error while restoring documents from the Workspace XAR"), any(XWikiException.class));
}
use of com.xpn.xwiki.objects.BaseObject in project xwiki-platform by xwiki.
the class WikiTemplatePropertyGroupProvider method save.
@Override
public void save(WikiPropertyGroup group, String wikiId) throws WikiPropertyGroupException {
XWikiContext context = xcontextProvider.get();
XWiki xwiki = context.getWiki();
WikiTemplatePropertyGroup templateGroup = (WikiTemplatePropertyGroup) group;
try {
XWikiDocument descriptorDocument = wikiDescriptorDocumentHelper.getDocumentFromWikiId(wikiId);
BaseObject object = descriptorDocument.getXObject(WikiTemplateClassDocumentInitializer.SERVER_CLASS, true, context);
object.setIntValue(WikiTemplateClassDocumentInitializer.FIELD_ISWIKITEMPLATE, templateGroup.isTemplate() ? 1 : 0);
// The document must have a creator
if (descriptorDocument.getCreatorReference() == null) {
descriptorDocument.setCreatorReference(context.getUserReference());
}
// The document must have an author
if (descriptorDocument.getAuthorReference() == null) {
descriptorDocument.setAuthorReference(context.getUserReference());
}
xwiki.saveDocument(descriptorDocument, String.format("Changed property group [%s].", GROUP_NAME), context);
} catch (WikiManagerException e) {
throw new WikiPropertyGroupException(String.format(ERROR_MESSAGE_NO_DESCRIPTOR_DOCUMENT, wikiId), e);
} catch (XWikiException e) {
throw new WikiPropertyGroupException("Unable to save descriptor document.", e);
}
}
use of com.xpn.xwiki.objects.BaseObject in project xwiki-platform by xwiki.
the class WikiTemplateMigration method hibernateMigrate.
@Override
protected void hibernateMigrate() throws DataMigrationException, XWikiException {
// XWiki objects
XWikiContext context = getXWikiContext();
XWiki xwiki = context.getWiki();
// WikiManager.WikiTemplateClass reference
DocumentReference templateClassReference = new DocumentReference(wikiDescriptorManager.getCurrentWikiId(), WikiTemplateClassDocumentInitializer.DOCUMENT_SPACE, WikiTemplateClassDocumentInitializer.DOCUMENT_NAME);
// XWiki.XWikiServerClass reference
DocumentReference descriptorClassReference = new DocumentReference(wikiDescriptorManager.getCurrentWikiId(), XWiki.SYSTEM_SPACE, "XWikiServerClass");
// Superadmin reference
DocumentReference superAdmin = new DocumentReference(wikiDescriptorManager.getMainWikiId(), XWiki.SYSTEM_SPACE, "superadmin");
try {
// Get all the descriptor documents
String statement = "select distinct doc.fullName " + "from Document doc, doc.object(XWiki.XWikiServerClass) as obj";
Query query = queryManager.createQuery(statement, Query.XWQL);
List<String> results = query.execute();
for (String wikiPage : results) {
XWikiDocument document = xwiki.getDocument(documentReferenceResolver.resolve(wikiPage), context);
// Get the "iswikitemplate" value
BaseObject descriptorObject = document.getXObject(descriptorClassReference);
int isTemplate = descriptorObject.getIntValue(OLD_TEMPLATE_PROPERTY, 0);
// We remove the deprecated property from the descriptor
descriptorObject.removeField(OLD_TEMPLATE_PROPERTY);
// Add the new WikiManager.WikiTemplateClass object
BaseObject object = document.getXObject(templateClassReference, true, context);
// The new object might already exists and have a template property already set
isTemplate = object.getIntValue(WikiTemplateClassDocumentInitializer.FIELD_ISWIKITEMPLATE, isTemplate);
// Set the (new) value
object.setIntValue(WikiTemplateClassDocumentInitializer.FIELD_ISWIKITEMPLATE, isTemplate);
// The document must have an author
document.setAuthorReference(superAdmin);
// Save the document
xwiki.saveDocument(document, "[UPGRADE] Upgrade the template section.", context);
}
} catch (QueryException e) {
throw new DataMigrationException("Failed to get the list of all existing descriptors.", e);
} catch (XWikiException e) {
throw new DataMigrationException("Failed to upgrade a wiki descriptor.", e);
}
}
use of com.xpn.xwiki.objects.BaseObject in project xwiki-platform by xwiki.
the class WikiTemplatePropertyGroupProviderTest method get.
@Test
public void get() throws Exception {
XWikiDocument descriptorDocument = mock(XWikiDocument.class);
when(wikiDescriptorDocumentHelper.getDocumentFromWikiId("wikiId")).thenReturn(descriptorDocument);
BaseObject object = mock(BaseObject.class);
when(descriptorDocument.getXObject(eq(WikiTemplateClassDocumentInitializer.SERVER_CLASS))).thenReturn(object);
when(object.getIntValue("iswikitemplate", 0)).thenReturn(1);
// Test
WikiPropertyGroup result = mocker.getComponentUnderTest().get("wikiId");
// Verify
assertEquals(true, result.get("isTemplate"));
assertTrue(result instanceof WikiTemplatePropertyGroup);
assertTrue(((WikiTemplatePropertyGroup) result).isTemplate());
XWikiDocument descriptorDocument2 = mock(XWikiDocument.class);
when(wikiDescriptorDocumentHelper.getDocumentFromWikiId("wikiId2")).thenReturn(descriptorDocument2);
BaseObject object2 = mock(BaseObject.class);
when(descriptorDocument2.getXObject(eq(WikiTemplateClassDocumentInitializer.SERVER_CLASS))).thenReturn(object2);
when(object2.getIntValue("iswikitemplate", 0)).thenReturn(0);
// Test
WikiPropertyGroup result2 = mocker.getComponentUnderTest().get("wikiId2");
// Verify
assertEquals(false, result2.get("isTemplate"));
assertTrue(result2 instanceof WikiTemplatePropertyGroup);
assertFalse(((WikiTemplatePropertyGroup) result2).isTemplate());
}
Aggregations