Search in sources :

Example 56 with PutMethod

use of org.apache.commons.httpclient.methods.PutMethod in project xwiki-platform by xwiki.

the class ObjectsResourceTest method testGETObjectAtPageVersion.

@Test
public void testGETObjectAtPageVersion() throws Exception {
    Object objectToBePut = createObjectIfDoesNotExists("XWiki.TagClass", this.spaces, this.pageName);
    Map<String, String> versionToValueMap = new HashMap<String, String>();
    for (int i = 0; i < 5; i++) {
        String value = String.format("Value%d", i);
        Property property = getProperty(objectToBePut, "tags");
        property.setValue(value);
        PutMethod putMethod = executePutXml(buildURI(ObjectResource.class, getWiki(), this.spaces, this.pageName, objectToBePut.getClassName(), objectToBePut.getNumber()).toString(), objectToBePut, TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(), TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword());
        Assert.assertEquals(getHttpMethodInfo(putMethod), HttpStatus.SC_ACCEPTED, putMethod.getStatusCode());
        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());
        versionToValueMap.put(page.getVersion(), value);
    }
    for (String version : versionToValueMap.keySet()) {
        GetMethod getMethod = executeGet(buildURI(ObjectAtPageVersionResource.class, getWiki(), this.spaces, this.pageName, version, objectToBePut.getClassName(), objectToBePut.getNumber()).toString());
        Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
        Object currentObject = (Object) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
        Property property = getProperty(currentObject, "tags");
        Assert.assertEquals(versionToValueMap.get(version), property.getValue());
        checkLinks(currentObject);
        for (Property p : currentObject.getProperties()) {
            checkLinks(p);
        }
    }
}
Also used : HashMap(java.util.HashMap) GetMethod(org.apache.commons.httpclient.methods.GetMethod) PutMethod(org.apache.commons.httpclient.methods.PutMethod) Object(org.xwiki.rest.model.jaxb.Object) Page(org.xwiki.rest.model.jaxb.Page) Property(org.xwiki.rest.model.jaxb.Property) AbstractHttpTest(org.xwiki.test.rest.framework.AbstractHttpTest) Test(org.junit.Test)

Example 57 with PutMethod

use of org.apache.commons.httpclient.methods.PutMethod in project xwiki-platform by xwiki.

the class ObjectsResourceTest method testPUTObject.

@Test
public void testPUTObject() throws Exception {
    final String TAG_VALUE = UUID.randomUUID().toString();
    Object objectToBePut = createObjectIfDoesNotExists("XWiki.TagClass", this.spaces, this.pageName);
    GetMethod getMethod = executeGet(buildURI(ObjectResource.class, getWiki(), this.spaces, this.pageName, objectToBePut.getClassName(), objectToBePut.getNumber()).toString());
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
    Object objectSummary = (Object) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    getProperty(objectSummary, "tags").setValue(TAG_VALUE);
    PutMethod putMethod = executePutXml(buildURI(ObjectResource.class, getWiki(), this.spaces, this.pageName, objectToBePut.getClassName(), objectToBePut.getNumber()).toString(), objectSummary, TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(), TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword());
    Assert.assertEquals(getHttpMethodInfo(putMethod), HttpStatus.SC_ACCEPTED, putMethod.getStatusCode());
    Object updatedObjectSummary = (Object) unmarshaller.unmarshal(putMethod.getResponseBodyAsStream());
    Assert.assertEquals(TAG_VALUE, getProperty(updatedObjectSummary, "tags").getValue());
    Assert.assertEquals(objectSummary.getClassName(), updatedObjectSummary.getClassName());
    Assert.assertEquals(objectSummary.getNumber(), updatedObjectSummary.getNumber());
}
Also used : GetMethod(org.apache.commons.httpclient.methods.GetMethod) PutMethod(org.apache.commons.httpclient.methods.PutMethod) Object(org.xwiki.rest.model.jaxb.Object) AbstractHttpTest(org.xwiki.test.rest.framework.AbstractHttpTest) Test(org.junit.Test)

Example 58 with PutMethod

use of org.apache.commons.httpclient.methods.PutMethod 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 59 with PutMethod

use of org.apache.commons.httpclient.methods.PutMethod in project xwiki-platform by xwiki.

the class PageResourceTest method testPUTGETPage.

@Test
public void testPUTGETPage() throws Exception {
    final String title = String.format("Title (%s)", UUID.randomUUID().toString());
    final String content = String.format("This is a content (%d)", System.currentTimeMillis());
    final String comment = String.format("Updated title and content (%d)", System.currentTimeMillis());
    Page originalPage = getFirstPage();
    Page newPage = this.objectFactory.createPage();
    newPage.setTitle(title);
    newPage.setContent(content);
    newPage.setComment(comment);
    Link link = getFirstLinkByRelation(originalPage, Relations.SELF);
    Assert.assertNotNull(link);
    // PUT
    PutMethod putMethod = executePutXml(link.getHref(), newPage, TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(), TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword());
    Assert.assertEquals(getHttpMethodInfo(putMethod), HttpStatus.SC_ACCEPTED, putMethod.getStatusCode());
    Page modifiedPage = (Page) this.unmarshaller.unmarshal(putMethod.getResponseBodyAsStream());
    Assert.assertEquals(title, modifiedPage.getTitle());
    Assert.assertEquals(content, modifiedPage.getContent());
    Assert.assertEquals(comment, modifiedPage.getComment());
    // GET
    GetMethod getMethod = executeGet(link.getHref());
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
    modifiedPage = (Page) this.unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    Assert.assertEquals(title, modifiedPage.getTitle());
    Assert.assertEquals(content, modifiedPage.getContent());
    Assert.assertEquals(comment, modifiedPage.getComment());
}
Also used : GetMethod(org.apache.commons.httpclient.methods.GetMethod) PutMethod(org.apache.commons.httpclient.methods.PutMethod) Page(org.xwiki.rest.model.jaxb.Page) Link(org.xwiki.rest.model.jaxb.Link) AbstractHttpTest(org.xwiki.test.rest.framework.AbstractHttpTest) Test(org.junit.Test)

Example 60 with PutMethod

use of org.apache.commons.httpclient.methods.PutMethod in project xwiki-platform by xwiki.

the class PageResourceTest method testPUTGETTranslation.

@Test
public void testPUTGETTranslation() throws Exception {
    createPageIfDoesntExist(TestConstants.TEST_SPACE_NAME, TestConstants.TRANSLATIONS_PAGE_NAME, "Translations");
    // PUT
    String[] languages = Locale.getISOLanguages();
    final String languageId = languages[random.nextInt(languages.length)];
    Page page = objectFactory.createPage();
    page.setContent(languageId);
    PutMethod putMethod = executePutXml(buildURI(PageTranslationResource.class, getWiki(), TestConstants.TEST_SPACE_NAME, TestConstants.TRANSLATIONS_PAGE_NAME, languageId).toString(), page, TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(), TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword());
    Assert.assertEquals(getHttpMethodInfo(putMethod), HttpStatus.SC_CREATED, putMethod.getStatusCode());
    // GET
    GetMethod getMethod = executeGet(buildURI(PageTranslationResource.class, getWiki(), TestConstants.TEST_SPACE_NAME, TestConstants.TRANSLATIONS_PAGE_NAME, languageId).toString());
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
    Page modifiedPage = (Page) this.unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    // Some of the language codes returned by Locale#getISOLanguages() are deprecated and Locale's constructors map
    // the new codes to the old ones which means the language code we have submitted can be different than the
    // actual language code used when the Locale object is created on the server side. Let's go through the Locale
    // constructor to be safe.
    String expectedLanguage = LocaleUtils.toLocale(languageId).getLanguage();
    Assert.assertEquals(expectedLanguage, modifiedPage.getLanguage());
    Assert.assertTrue(modifiedPage.getTranslations().getTranslations().size() > 0);
    for (Translation translation : modifiedPage.getTranslations().getTranslations()) {
        getMethod = executeGet(getFirstLinkByRelation(translation, Relations.PAGE).getHref());
        Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
        modifiedPage = (Page) this.unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
        Assert.assertEquals(modifiedPage.getLanguage(), translation.getLanguage());
        checkLinks(translation);
    }
}
Also used : Translation(org.xwiki.rest.model.jaxb.Translation) GetMethod(org.apache.commons.httpclient.methods.GetMethod) PutMethod(org.apache.commons.httpclient.methods.PutMethod) Page(org.xwiki.rest.model.jaxb.Page) AbstractHttpTest(org.xwiki.test.rest.framework.AbstractHttpTest) Test(org.junit.Test)

Aggregations

PutMethod (org.apache.commons.httpclient.methods.PutMethod)94 Test (org.junit.Test)49 GetMethod (org.apache.commons.httpclient.methods.GetMethod)29 AbstractHttpTest (org.xwiki.test.rest.framework.AbstractHttpTest)21 StringRequestEntity (org.apache.commons.httpclient.methods.StringRequestEntity)19 Page (org.xwiki.rest.model.jaxb.Page)15 HttpClient (org.apache.commons.httpclient.HttpClient)14 IOException (java.io.IOException)13 RequestEntity (org.apache.commons.httpclient.methods.RequestEntity)13 DeleteMethod (org.apache.commons.httpclient.methods.DeleteMethod)12 PostMethod (org.apache.commons.httpclient.methods.PostMethod)10 InputStreamRequestEntity (org.apache.commons.httpclient.methods.InputStreamRequestEntity)7 DeleteMethod (org.apache.jackrabbit.webdav.client.methods.DeleteMethod)7 Link (org.xwiki.rest.model.jaxb.Link)7 Header (org.apache.commons.httpclient.Header)6 HttpMethod (org.apache.commons.httpclient.HttpMethod)6 FileRequestEntity (org.apache.commons.httpclient.methods.FileRequestEntity)6 File (java.io.File)5 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)5 Object (org.xwiki.rest.model.jaxb.Object)5