Search in sources :

Example 31 with WikiDescriptor

use of org.xwiki.wiki.descriptor.WikiDescriptor in project xwiki-platform by xwiki.

the class DefaultWikiDescriptorBuilderTest method buildDescriptorObject.

@Test
public void buildDescriptorObject() throws Exception {
    // Mocks
    List<BaseObject> objects = new ArrayList<>();
    BaseObject object1 = mock(BaseObject.class);
    BaseObject object2 = mock(BaseObject.class);
    BaseObject object3 = mock(BaseObject.class);
    // Make sure that the first object is null to also verify this case since it can happen that we get holes
    // with the XWikiDocument.getXObjects() API...
    objects.add(null);
    objects.add(object1);
    objects.add(object2);
    objects.add(null);
    objects.add(object3);
    XWikiDocument document = mock(XWikiDocument.class);
    DocumentReference documentReference = new DocumentReference("mainWiki", "XWiki", "XWikiServerSubwiki1");
    when(document.getDocumentReference()).thenReturn(documentReference);
    when(object1.getStringValue(XWikiServerClassDocumentInitializer.FIELD_SERVER)).thenReturn("subwiki1");
    when(object2.getStringValue(XWikiServerClassDocumentInitializer.FIELD_SERVER)).thenReturn("alias1");
    when(object3.getStringValue(XWikiServerClassDocumentInitializer.FIELD_SERVER)).thenReturn("alias2");
    DocumentReference mainPageReference = new DocumentReference("subwiki1", "Space", "MainPage");
    when(object1.getStringValue(XWikiServerClassDocumentInitializer.FIELD_HOMEPAGE)).thenReturn("Space.MainPage");
    when(referenceResolver.resolve("Space.MainPage")).thenReturn(mainPageReference);
    when(object1.getStringValue(XWikiServerClassDocumentInitializer.FIELD_WIKIPRETTYNAME)).thenReturn("myPrettyName");
    when(object1.getStringValue(XWikiServerClassDocumentInitializer.FIELD_OWNER)).thenReturn("myOwner");
    when(object1.getStringValue(XWikiServerClassDocumentInitializer.FIELD_DESCRIPTION)).thenReturn("myDescription");
    DocumentReference ownerRef = new DocumentReference("subwiki1", "XWiki", "myOwner");
    when(userReferenceResolver.resolve("myOwner", new WikiReference("subwiki1"))).thenReturn(ownerRef);
    when(referenceSerializer.serialize(ownerRef)).thenReturn("subwiki1:XWiki.myOwner");
    // Test
    WikiDescriptor result = mocker.getComponentUnderTest().buildDescriptorObject(objects, document);
    assertEquals("subwiki1", result.getId());
    assertEquals(3, result.getAliases().size());
    assertEquals("subwiki1", result.getAliases().get(0));
    assertEquals("alias1", result.getAliases().get(1));
    assertEquals("alias2", result.getAliases().get(2));
    assertEquals(mainPageReference, result.getMainPageReference());
    assertEquals("myPrettyName", result.getPrettyName());
    assertEquals("subwiki1:XWiki.myOwner", result.getOwnerId());
    assertEquals("myDescription", result.getDescription());
    // Verify
    wikiPropertyGroupManager.loadForDescriptor(any(WikiDescriptor.class));
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ArrayList(java.util.ArrayList) WikiReference(org.xwiki.model.reference.WikiReference) DocumentReference(org.xwiki.model.reference.DocumentReference) WikiDescriptor(org.xwiki.wiki.descriptor.WikiDescriptor) BaseObject(com.xpn.xwiki.objects.BaseObject) Test(org.junit.Test)

Example 32 with WikiDescriptor

use of org.xwiki.wiki.descriptor.WikiDescriptor in project xwiki-platform by xwiki.

the class DefaultWikiCreatorTest method create.

@Test
public void create() throws Exception {
    // Other mocks
    DefaultWikiDescriptor descriptor = new DefaultWikiDescriptor("wikiid1", "wikialias1");
    XWikiDocument descriptorDocument = mock(XWikiDocument.class);
    when(wikiDescriptorBuilder.save(eq(descriptor))).thenReturn(descriptorDocument);
    when(wikiDescriptorManager.getById("wikiid1")).thenReturn(descriptor);
    when(store.isWikiNameAvailable(any(String.class), any(XWikiContext.class))).thenReturn(true);
    // Create
    WikiDescriptor newWikiDescriptor = this.mocker.getComponentUnderTest().create("wikiid1", "wikialias1");
    assertNotNull(newWikiDescriptor);
    // Verify that the wiki descriptor is an instance of DefaultWikiDescriptor
    assertTrue(newWikiDescriptor instanceof DefaultWikiDescriptor);
    // Verify that the wiki has been created
    verify(store).createWiki(eq("wikiid1"), any(XWikiContext.class));
    // Verify that the wiki has been updated
    verify(xwiki).initializeWiki(eq("wikiid1"), eq(true), any(XWikiContext.class));
    // Verify that the descriptor document has been saved
    verify(wikiDescriptorBuilder).save(eq(descriptor));
    // Verify that the descriptor has been reloaded after being saved
    assertTrue(descriptor == newWikiDescriptor);
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiContext(com.xpn.xwiki.XWikiContext) DefaultWikiDescriptor(org.xwiki.wiki.internal.descriptor.DefaultWikiDescriptor) DefaultWikiDescriptor(org.xwiki.wiki.internal.descriptor.DefaultWikiDescriptor) WikiDescriptor(org.xwiki.wiki.descriptor.WikiDescriptor) Test(org.junit.Test)

Example 33 with WikiDescriptor

use of org.xwiki.wiki.descriptor.WikiDescriptor in project xwiki-platform by xwiki.

the class DefaultWikiManagerTest method copyWhenWikiAvailable.

@Test
public void copyWhenWikiAvailable() throws Exception {
    // The wiki does not already exist
    when(wikiDescriptorManager.exists("wikiid1")).thenReturn(false);
    // The new id is valid
    when(xwiki.Param("xwiki.virtual.reserved_wikis")).thenReturn("forbidden");
    // The wiki name is available
    when(store.isWikiNameAvailable(eq("wikiid1"), any(XWikiContext.class))).thenReturn(true);
    // Other mocks
    DefaultWikiDescriptor descriptor = new DefaultWikiDescriptor("wikiid1", "wikialias1");
    when(wikiCreator.create("wikiid1", "wikialias1")).thenReturn(descriptor);
    // Copy
    WikiDescriptor newWikiDescriptor = this.mocker.getComponentUnderTest().copy("wikiid", "wikiid1", "wikialias1", true, true, true);
    assertNotNull(newWikiDescriptor);
    // Verify that the wiki has been created
    verify(wikiCreator).create("wikiid1", "wikialias1");
    // Verify that the wiki has been copied
    verify(wikiCopier).copyDocuments(eq("wikiid"), eq("wikiid1"), eq(true));
    // Verify that deleted documents has been copied too
    verify(wikiCopier).copyDeletedDocuments(eq("wikiid"), eq("wikiid1"));
    // Verify that events has been sent
    verify(observationManager).notify(new WikiCopiedEvent("wikiid", "wikiid1"), "wikiid", xcontext);
}
Also used : WikiCopiedEvent(org.xwiki.bridge.event.WikiCopiedEvent) XWikiContext(com.xpn.xwiki.XWikiContext) DefaultWikiDescriptor(org.xwiki.wiki.internal.descriptor.DefaultWikiDescriptor) DefaultWikiDescriptor(org.xwiki.wiki.internal.descriptor.DefaultWikiDescriptor) WikiDescriptor(org.xwiki.wiki.descriptor.WikiDescriptor) Test(org.junit.Test)

Example 34 with WikiDescriptor

use of org.xwiki.wiki.descriptor.WikiDescriptor in project xwiki-platform by xwiki.

the class DefaultWikiManagerTest method createWhenWikiIdIsValid.

@Test
public void createWhenWikiIdIsValid() throws Exception {
    // The wiki does not already exist
    when(wikiDescriptorManager.exists("wikiid1")).thenReturn(false);
    // The wiki id is valid
    when(xwiki.Param("xwiki.virtual.reserved_wikis")).thenReturn("forbidden");
    // The wiki name is available
    when(store.isWikiNameAvailable(eq("wikiid1"), any(XWikiContext.class))).thenReturn(true);
    DefaultWikiDescriptor descriptor = new DefaultWikiDescriptor("wikiid1", "wikialias1");
    when(wikiCreator.create("wikiid1", "wikialias1")).thenReturn(descriptor);
    // Create
    WikiDescriptor newWikiDescriptor = this.mocker.getComponentUnderTest().create("wikiid1", "wikialias1", true);
    // Verify a descriptor has been returned
    assertNotNull(newWikiDescriptor);
    // Verify that the wiki descriptor is an instance of DefaultWikiDescriptor
    assertTrue(newWikiDescriptor instanceof DefaultWikiDescriptor);
    // Verify that the wiki has been created
    verify(wikiCreator).create("wikiid1", "wikialias1");
    // Verify the events has been sent
    verify(observationManager).notify(new WikiCreatingEvent("wikiid1"), "wikiid1", xcontext);
    verify(observationManager).notify(new WikiCreatedEvent("wikiid1"), "wikiid1", xcontext);
}
Also used : WikiCreatingEvent(org.xwiki.bridge.event.WikiCreatingEvent) WikiCreatedEvent(org.xwiki.bridge.event.WikiCreatedEvent) XWikiContext(com.xpn.xwiki.XWikiContext) DefaultWikiDescriptor(org.xwiki.wiki.internal.descriptor.DefaultWikiDescriptor) DefaultWikiDescriptor(org.xwiki.wiki.internal.descriptor.DefaultWikiDescriptor) WikiDescriptor(org.xwiki.wiki.descriptor.WikiDescriptor) Test(org.junit.Test)

Example 35 with WikiDescriptor

use of org.xwiki.wiki.descriptor.WikiDescriptor in project xwiki-platform by xwiki.

the class DefaultWikiManagerREST method createWiki.

@Override
@POST
public Response createWiki(@QueryParam("template") String template, Wiki wiki) throws XWikiRestException {
    XWikiContext xcontext = getXWikiContext();
    WikiDescriptor descriptor = null;
    try {
        // Create the wiki
        descriptor = wikiManager.create(wiki.getId(), wiki.getId(), true);
        // Change the descriptor
        if (wiki.getOwner() != null) {
            descriptor.setOwnerId(wiki.getOwner());
        } else {
            descriptor.setOwnerId(entityReferenceSerializer.serialize(xcontext.getUserReference()));
        }
        descriptor.setPrettyName(wiki.getName());
        descriptor.setDescription(wiki.getDescription());
        // Save the descriptor
        wikiDescriptorManager.saveDescriptor(descriptor);
        // Apply a template (if needed)
        if (template != null) {
            WikiProvisioningJob job = wikiTemplateManager.applyTemplate(descriptor.getId(), template);
            job.join();
        }
        // Build the response
        Wiki result = createWiki(objectFactory, uriInfo.getBaseUri(), wiki.getId(), wiki.getOwner(), wiki.getDescription());
        // Add a link to the WebHome of the newly created wiki.
        Link home = objectFactory.createLink();
        home.setRel(Relations.HOME);
        home.setHref(xcontext.getWiki().getURL(descriptor.getMainPageReference(), "view", xcontext));
        result.getLinks().add(home);
        // Return the final response
        return Response.created(UriBuilder.fromUri(uriInfo.getBaseUri()).path(WikiResource.class).build(wiki.getId())).entity(result).build();
    } catch (Exception e) {
        throw new XWikiRestException(e);
    }
}
Also used : XWikiRestException(org.xwiki.rest.XWikiRestException) XWikiContext(com.xpn.xwiki.XWikiContext) Wiki(org.xwiki.rest.model.jaxb.Wiki) WikiDescriptor(org.xwiki.wiki.descriptor.WikiDescriptor) Link(org.xwiki.rest.model.jaxb.Link) XWikiRestException(org.xwiki.rest.XWikiRestException) WikiProvisioningJob(org.xwiki.wiki.provisioning.WikiProvisioningJob) POST(javax.ws.rs.POST)

Aggregations

WikiDescriptor (org.xwiki.wiki.descriptor.WikiDescriptor)60 Test (org.junit.Test)40 WikiManagerException (org.xwiki.wiki.manager.WikiManagerException)28 AccessDeniedException (org.xwiki.security.authorization.AccessDeniedException)17 WikiReference (org.xwiki.model.reference.WikiReference)11 XWikiContext (com.xpn.xwiki.XWikiContext)9 WikiTemplateManagerException (org.xwiki.wiki.template.WikiTemplateManagerException)9 ArrayList (java.util.ArrayList)7 DocumentReference (org.xwiki.model.reference.DocumentReference)6 WikiDescriptorManager (org.xwiki.wiki.descriptor.WikiDescriptorManager)6 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)5 BaseObject (com.xpn.xwiki.objects.BaseObject)5 DefaultWikiDescriptor (org.xwiki.wiki.internal.descriptor.DefaultWikiDescriptor)5 WikiCreationRequest (org.xwiki.platform.wiki.creationjob.WikiCreationRequest)3 AuthorizationException (org.xwiki.security.authorization.AuthorizationException)3 WikiCopiedEvent (org.xwiki.bridge.event.WikiCopiedEvent)2 WikiCreatedEvent (org.xwiki.bridge.event.WikiCreatedEvent)2 WikiCreatingEvent (org.xwiki.bridge.event.WikiCreatingEvent)2 WikiCreationException (org.xwiki.platform.wiki.creationjob.WikiCreationException)2 WikiDescriptorBuilderException (org.xwiki.wiki.internal.descriptor.builder.WikiDescriptorBuilderException)2