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());
}
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());
}
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());
}
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());
}
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());
}
Aggregations