Search in sources :

Example 61 with PutMethod

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

the class PageResourceTest method testPUTGETWithObject.

@Test
public void testPUTGETWithObject() throws Exception {
    String pageURI = buildURI(PageResource.class, getWiki(), Arrays.asList("RESTTest"), "PageWithObject");
    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 newPage = this.objectFactory.createPage();
    newPage.setTitle(title);
    newPage.setContent(content);
    newPage.setComment(comment);
    // Add object
    final String TAG_VALUE = "TAG";
    Property property = new Property();
    property.setName("tags");
    property.setValue(TAG_VALUE);
    Object object = objectFactory.createObject();
    object.setClassName("XWiki.TagClass");
    object.getProperties().add(property);
    newPage.setObjects(objectFactory.createObjects());
    newPage.getObjects().getObjectSummaries().add(object);
    // PUT
    PutMethod putMethod = executePutXml(pageURI, newPage, TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(), TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword());
    assertThat(getHttpMethodInfo(putMethod), putMethod.getStatusCode(), isIn(Arrays.asList(HttpStatus.SC_ACCEPTED, HttpStatus.SC_CREATED)));
    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(pageURI + "?objects=true");
    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());
    Assert.assertEquals(TAG_VALUE, getProperty((Object) modifiedPage.getObjects().getObjectSummaries().get(0), "tags").getValue());
    // Send again but with empty object list
    modifiedPage.getObjects().getObjectSummaries().clear();
    // PUT
    putMethod = executePutXml(pageURI, modifiedPage, TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(), TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword());
    assertThat(getHttpMethodInfo(putMethod), putMethod.getStatusCode(), isIn(Arrays.asList(HttpStatus.SC_ACCEPTED)));
    modifiedPage = (Page) this.unmarshaller.unmarshal(putMethod.getResponseBodyAsStream());
    Assert.assertEquals(title, modifiedPage.getTitle());
    Assert.assertEquals(content, modifiedPage.getContent());
    Assert.assertEquals(comment, modifiedPage.getComment());
    // GET
    getMethod = executeGet(pageURI + "?objects=true");
    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());
    Assert.assertTrue(modifiedPage.getObjects().getObjectSummaries().isEmpty());
}
Also used : GetMethod(org.apache.commons.httpclient.methods.GetMethod) PutMethod(org.apache.commons.httpclient.methods.PutMethod) Page(org.xwiki.rest.model.jaxb.Page) Object(org.xwiki.rest.model.jaxb.Object) Property(org.xwiki.rest.model.jaxb.Property) AbstractHttpTest(org.xwiki.test.rest.framework.AbstractHttpTest) Test(org.junit.Test)

Example 62 with PutMethod

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

the class PageResourceTest method testPUTNonExistingPage.

@Test
public void testPUTNonExistingPage() throws Exception {
    final List<String> SPACE_NAME = Arrays.asList("Test");
    final String PAGE_NAME = String.format("Test-%d", System.currentTimeMillis());
    final String CONTENT = String.format("Content %d", System.currentTimeMillis());
    final String TITLE = String.format("Title %d", System.currentTimeMillis());
    final String PARENT = "Main.WebHome";
    Page page = objectFactory.createPage();
    page.setContent(CONTENT);
    page.setTitle(TITLE);
    page.setParent(PARENT);
    PutMethod putMethod = executePutXml(buildURI(PageResource.class, getWiki(), SPACE_NAME, PAGE_NAME).toString(), page, TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(), TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword());
    Assert.assertEquals(getHttpMethodInfo(putMethod), HttpStatus.SC_CREATED, putMethod.getStatusCode());
    Page modifiedPage = (Page) this.unmarshaller.unmarshal(putMethod.getResponseBodyAsStream());
    Assert.assertEquals(CONTENT, modifiedPage.getContent());
    Assert.assertEquals(TITLE, modifiedPage.getTitle());
    Assert.assertEquals(PARENT, modifiedPage.getParent());
}
Also used : 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)

Example 63 with PutMethod

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

the class PageResourceTest method testPUTWithInvalidRepresentation.

@Test
public void testPUTWithInvalidRepresentation() throws Exception {
    Page page = getFirstPage();
    Link link = getFirstLinkByRelation(page, Relations.SELF);
    PutMethod putMethod = executePut(link.getHref(), "<?xml version=\"1.0\" encoding=\"UTF-8\"?><invalidPage><content/></invalidPage>", MediaType.TEXT_XML);
    Assert.assertEquals(getHttpMethodInfo(putMethod), HttpStatus.SC_BAD_REQUEST, putMethod.getStatusCode());
}
Also used : 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 64 with PutMethod

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

the class PageResourceTest method testPUTPageUnauthorized.

@Test
public void testPUTPageUnauthorized() throws Exception {
    Page page = getFirstPage();
    page.setContent("New content");
    Link link = getFirstLinkByRelation(page, Relations.SELF);
    Assert.assertNotNull(link);
    PutMethod putMethod = executePutXml(link.getHref(), page);
    Assert.assertEquals(getHttpMethodInfo(putMethod), HttpStatus.SC_UNAUTHORIZED, putMethod.getStatusCode());
}
Also used : 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 65 with PutMethod

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

the class PageResourceTest method testPUTPageWithTextPlain.

@Test
public void testPUTPageWithTextPlain() throws Exception {
    final String CONTENT = String.format("This is a content (%d)", System.currentTimeMillis());
    Page originalPage = getFirstPage();
    Link link = getFirstLinkByRelation(originalPage, Relations.SELF);
    Assert.assertNotNull(link);
    PutMethod putMethod = executePut(link.getHref(), CONTENT, MediaType.TEXT_PLAIN, 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(CONTENT, modifiedPage.getContent());
}
Also used : 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)

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