Search in sources :

Example 6 with Objects

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

the class ObjectsResourceTest method testPUTProperty.

@Test
public void testPUTProperty() 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);
    Property newTags = objectFactory.createProperty();
    newTags.setValue(TAG_VALUE);
    PutMethod putMethod = executePutXml(tagsPropertyLink.getHref(), newTags, 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 Objects

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

the class ObjectsResourceTest method createObjectIfDoesNotExists.

private Object createObjectIfDoesNotExists(String className, List<String> spaces, String pageName) throws Exception {
    createPageIfDoesntExist(spaces, pageName, "");
    GetMethod getMethod = executeGet(buildURI(ObjectsResource.class, getWiki(), spaces, pageName).toString());
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
    Objects objects = (Objects) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    for (ObjectSummary objectSummary : objects.getObjectSummaries()) {
        if (objectSummary.getClassName().equals(className)) {
            Link link = getFirstLinkByRelation(objectSummary, Relations.OBJECT);
            Assert.assertNotNull(link);
            getMethod = executeGet(link.getHref());
            Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
            Object object = (Object) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
            return object;
        }
    }
    /* If no object of that class is found, then create a new one */
    Object object = objectFactory.createObject();
    object.setClassName(className);
    PostMethod postMethod = executePostXml(buildURI(ObjectsResource.class, getWiki(), spaces, pageName).toString(), object, TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(), TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword());
    Assert.assertEquals(getHttpMethodInfo(postMethod), HttpStatus.SC_CREATED, postMethod.getStatusCode());
    object = (Object) unmarshaller.unmarshal(postMethod.getResponseBodyAsStream());
    return object;
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) GetMethod(org.apache.commons.httpclient.methods.GetMethod) Objects(org.xwiki.rest.model.jaxb.Objects) Object(org.xwiki.rest.model.jaxb.Object) ObjectSummary(org.xwiki.rest.model.jaxb.ObjectSummary) Link(org.xwiki.rest.model.jaxb.Link)

Example 8 with Objects

use of org.xwiki.rest.model.jaxb.Objects 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 9 with Objects

use of org.xwiki.rest.model.jaxb.Objects 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 10 with Objects

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

the class AbstractHttpTest method setUp.

@Before
public void setUp() throws Exception {
    random = new Random();
    JAXBContext context = JAXBContext.newInstance("org.xwiki.rest.model.jaxb");
    marshaller = context.createMarshaller();
    unmarshaller = context.createUnmarshaller();
    objectFactory = new ObjectFactory();
    // Make sure guest does not have edit right
    Page page = this.testUtils.rest().page(new DocumentReference("xwiki", "XWiki", "XWikiPreferences"));
    org.xwiki.rest.model.jaxb.Object rightObject = this.testUtils.rest().object("XWiki.XWikiGlobalRights");
    rightObject.withProperties(this.testUtils.rest().property("users", "XWiki.XWikiGuest"), this.testUtils.rest().property("levels", "edit"), this.testUtils.rest().property("allow", "0"));
    Objects objects = new Objects();
    objects.withObjectSummaries(rightObject);
    page.setObjects(objects);
    this.testUtils.rest().save(page);
    // Init solr utils
    this.solrUtils = new SolrTestUtils(this.testUtils);
}
Also used : SolrTestUtils(org.xwiki.repository.test.SolrTestUtils) Random(java.util.Random) ObjectFactory(org.xwiki.rest.model.jaxb.ObjectFactory) Objects(org.xwiki.rest.model.jaxb.Objects) JAXBContext(javax.xml.bind.JAXBContext) Page(org.xwiki.rest.model.jaxb.Page) DocumentReference(org.xwiki.model.reference.DocumentReference) Before(org.junit.Before)

Aggregations

Objects (org.xwiki.rest.model.jaxb.Objects)13 Page (org.xwiki.rest.model.jaxb.Page)8 Test (org.junit.Test)6 Object (org.xwiki.rest.model.jaxb.Object)6 ObjectSummary (org.xwiki.rest.model.jaxb.ObjectSummary)6 GetMethod (org.apache.commons.httpclient.methods.GetMethod)5 Link (org.xwiki.rest.model.jaxb.Link)5 Document (com.xpn.xwiki.api.Document)4 XWikiRestException (org.xwiki.rest.XWikiRestException)4 Property (org.xwiki.rest.model.jaxb.Property)4 AbstractHttpTest (org.xwiki.test.rest.framework.AbstractHttpTest)4 XWikiException (com.xpn.xwiki.XWikiException)3 RangeIterable (org.xwiki.rest.internal.RangeIterable)3 ObjectResource (org.xwiki.rest.resources.objects.ObjectResource)3 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)2 BaseObject (com.xpn.xwiki.objects.BaseObject)2 PostMethod (org.apache.commons.httpclient.methods.PostMethod)2 PutMethod (org.apache.commons.httpclient.methods.PutMethod)2 Random (java.util.Random)1 JAXBContext (javax.xml.bind.JAXBContext)1