Search in sources :

Example 6 with ObjectSummary

use of org.xwiki.rest.model.jaxb.ObjectSummary in project xwiki-platform by xwiki.

the class ObjectsResourceTest method testPUTPropertyWithTextPlain.

@Test
public void testPUTPropertyWithTextPlain() throws Exception {
    final String TAG_VALUE = UUID.randomUUID().toString();
    /* Make sure that an Object with the TagClass exists. */
    createObjectIfDoesNotExists("XWiki.TagClass", this.spaces, this.pageName);
    GetMethod getMethod = executeGet(buildURI(PageResource.class, getWiki(), this.spaces, this.pageName).toString());
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
    Page page = (Page) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    Link link = getFirstLinkByRelation(page, Relations.OBJECTS);
    Assert.assertNotNull(link);
    getMethod = executeGet(link.getHref());
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
    Objects objects = (Objects) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    Assert.assertFalse(objects.getObjectSummaries().isEmpty());
    Object currentObject = null;
    for (ObjectSummary objectSummary : objects.getObjectSummaries()) {
        if (objectSummary.getClassName().equals("XWiki.TagClass")) {
            link = getFirstLinkByRelation(objectSummary, Relations.OBJECT);
            Assert.assertNotNull(link);
            getMethod = executeGet(link.getHref());
            Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
            currentObject = (Object) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
            break;
        }
    }
    Assert.assertNotNull(currentObject);
    Property tagsProperty = getProperty(currentObject, "tags");
    Assert.assertNotNull(tagsProperty);
    Link tagsPropertyLink = getFirstLinkByRelation(tagsProperty, Relations.SELF);
    Assert.assertNotNull(tagsPropertyLink);
    PutMethod putMethod = executePut(tagsPropertyLink.getHref(), TAG_VALUE, MediaType.TEXT_PLAIN, TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(), TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword());
    Assert.assertEquals(getHttpMethodInfo(putMethod), HttpStatus.SC_ACCEPTED, putMethod.getStatusCode());
    getMethod = executeGet(buildURI(ObjectResource.class, getWiki(), this.spaces, this.pageName, currentObject.getClassName(), currentObject.getNumber()).toString());
    Assert.assertEquals(HttpStatus.SC_OK, getMethod.getStatusCode());
    currentObject = (Object) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    tagsProperty = getProperty(currentObject, "tags");
    Assert.assertEquals(TAG_VALUE, tagsProperty.getValue());
}
Also used : GetMethod(org.apache.commons.httpclient.methods.GetMethod) Objects(org.xwiki.rest.model.jaxb.Objects) PutMethod(org.apache.commons.httpclient.methods.PutMethod) Page(org.xwiki.rest.model.jaxb.Page) Object(org.xwiki.rest.model.jaxb.Object) ObjectResource(org.xwiki.rest.resources.objects.ObjectResource) ObjectSummary(org.xwiki.rest.model.jaxb.ObjectSummary) Property(org.xwiki.rest.model.jaxb.Property) Link(org.xwiki.rest.model.jaxb.Link) AbstractHttpTest(org.xwiki.test.rest.framework.AbstractHttpTest) Test(org.junit.Test)

Example 7 with ObjectSummary

use of org.xwiki.rest.model.jaxb.ObjectSummary in project xwiki-platform by xwiki.

the class ObjectsResourceTest method testRepresentation.

@Override
@Test
public void testRepresentation() throws Exception {
    GetMethod getMethod = executeGet(buildURI(PageResource.class, getWiki(), this.spaces, this.pageName).toString());
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
    Page page = (Page) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    Link link = getFirstLinkByRelation(page, Relations.OBJECTS);
    Assert.assertNotNull(link);
    getMethod = executeGet(link.getHref());
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
    Objects objects = (Objects) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    Assert.assertFalse(objects.getObjectSummaries().isEmpty());
    for (ObjectSummary objectSummary : objects.getObjectSummaries()) {
        link = getFirstLinkByRelation(objectSummary, Relations.OBJECT);
        getMethod = executeGet(link.getHref());
        Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
        Object object = (Object) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
        checkLinks(objectSummary);
        for (Property property : object.getProperties()) {
            checkLinks(property);
        }
    }
}
Also used : GetMethod(org.apache.commons.httpclient.methods.GetMethod) Objects(org.xwiki.rest.model.jaxb.Objects) Page(org.xwiki.rest.model.jaxb.Page) Object(org.xwiki.rest.model.jaxb.Object) ObjectSummary(org.xwiki.rest.model.jaxb.ObjectSummary) Property(org.xwiki.rest.model.jaxb.Property) Link(org.xwiki.rest.model.jaxb.Link) AbstractHttpTest(org.xwiki.test.rest.framework.AbstractHttpTest) Test(org.junit.Test)

Example 8 with ObjectSummary

use of org.xwiki.rest.model.jaxb.ObjectSummary in project xwiki-platform by xwiki.

the class ModelFactory method toDocument.

public boolean toDocument(Document doc, org.xwiki.rest.model.jaxb.Page restPage) throws XWikiException {
    boolean modified = false;
    if (restPage.getContent() != null) {
        doc.setContent(restPage.getContent());
        modified = true;
    }
    if (restPage.getTitle() != null) {
        doc.setTitle(restPage.getTitle());
        modified = true;
    }
    if (restPage.getParent() != null) {
        doc.setParent(restPage.getParent());
        modified = true;
    }
    if (restPage.getSyntax() != null) {
        doc.setSyntaxId(restPage.getSyntax());
        modified = true;
    }
    doc.setHidden(restPage.isHidden());
    // Set objects
    if (restPage.getObjects() != null) {
        Set<ObjectReference> newReferences = new HashSet<>();
        // Add/update objects
        for (ObjectSummary restObjectSummary : restPage.getObjects().getObjectSummaries()) {
            if (restObjectSummary != null) {
                org.xwiki.rest.model.jaxb.Object restObject = (org.xwiki.rest.model.jaxb.Object) restObjectSummary;
                com.xpn.xwiki.api.Object xwikiObject = doc.getObject(restObject.getClassName(), restObject.getNumber());
                if (xwikiObject == null) {
                    xwikiObject = doc.newObject(restObject.getClassName());
                }
                toObject(xwikiObject, restObject);
                modified = true;
                newReferences.add(xwikiObject.getReference());
            }
        }
        // Remove objects
        List<com.xpn.xwiki.api.Object> toRemove = new ArrayList<>();
        for (Vector<com.xpn.xwiki.api.Object> objects : doc.getxWikiObjects().values()) {
            for (com.xpn.xwiki.api.Object object : objects) {
                if (!newReferences.contains(object.getReference())) {
                    toRemove.add(object);
                }
            }
        }
        for (com.xpn.xwiki.api.Object obj : toRemove) {
            doc.removeObject(obj);
            modified = true;
        }
    }
    return modified;
}
Also used : ArrayList(java.util.ArrayList) ObjectReference(org.xwiki.model.reference.ObjectReference) Object(org.xwiki.rest.model.jaxb.Object) BaseObject(com.xpn.xwiki.objects.BaseObject) Object(org.xwiki.rest.model.jaxb.Object) ObjectSummary(org.xwiki.rest.model.jaxb.ObjectSummary) HashSet(java.util.HashSet)

Aggregations

ObjectSummary (org.xwiki.rest.model.jaxb.ObjectSummary)8 Link (org.xwiki.rest.model.jaxb.Link)6 Object (org.xwiki.rest.model.jaxb.Object)6 Objects (org.xwiki.rest.model.jaxb.Objects)6 GetMethod (org.apache.commons.httpclient.methods.GetMethod)5 Test (org.junit.Test)4 Page (org.xwiki.rest.model.jaxb.Page)4 Property (org.xwiki.rest.model.jaxb.Property)4 AbstractHttpTest (org.xwiki.test.rest.framework.AbstractHttpTest)4 ObjectResource (org.xwiki.rest.resources.objects.ObjectResource)3 BaseObject (com.xpn.xwiki.objects.BaseObject)2 PostMethod (org.apache.commons.httpclient.methods.PostMethod)2 PutMethod (org.apache.commons.httpclient.methods.PutMethod)2 Document (com.xpn.xwiki.api.Document)1 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 NameValuePair (org.apache.commons.httpclient.NameValuePair)1 ObjectReference (org.xwiki.model.reference.ObjectReference)1 XWikiRestException (org.xwiki.rest.XWikiRestException)1