Example usage for org.apache.commons.httpclient HttpStatus SC_CREATED

List of usage examples for org.apache.commons.httpclient HttpStatus SC_CREATED

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpStatus SC_CREATED.

Prototype

int SC_CREATED

To view the source code for org.apache.commons.httpclient HttpStatus SC_CREATED.

Click Source Link

Document

<tt>201 Created</tt> (HTTP/1.0 - RFC 1945)

Usage

From source file:org.xwiki.test.rest.CommentsResourceTest.java

@Test
public void testPOSTComment() throws Exception {
    String commentsUri = buildURI(CommentsResource.class, getWiki(), SPACE_NAME, PAGE_NAME).toString();

    GetMethod getMethod = executeGet(commentsUri);
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());

    Comments comments = (Comments) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());

    int numberOfComments = comments.getComments().size();

    Comment comment = objectFactory.createComment();
    comment.setText("Comment");

    PostMethod postMethod = executePostXml(commentsUri, comment, TestUtils.ADMIN_CREDENTIALS.getUserName(),
            TestUtils.ADMIN_CREDENTIALS.getPassword());
    Assert.assertEquals(getHttpMethodInfo(postMethod), HttpStatus.SC_CREATED, postMethod.getStatusCode());

    getMethod = executeGet(commentsUri);
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());

    comments = (Comments) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());

    Assert.assertEquals(numberOfComments + 1, comments.getComments().size());
}

From source file:org.xwiki.test.rest.CommentsResourceTest.java

@Test
public void testPOSTCommentWithTextPlain() throws Exception {
    String commentsUri = buildURI(CommentsResource.class, getWiki(), SPACE_NAME, PAGE_NAME).toString();

    GetMethod getMethod = executeGet(commentsUri);
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());

    Comments comments = (Comments) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());

    int numberOfComments = comments.getComments().size();

    PostMethod postMethod = executePost(commentsUri, "Comment", MediaType.TEXT_PLAIN,
            TestUtils.ADMIN_CREDENTIALS.getUserName(), TestUtils.ADMIN_CREDENTIALS.getPassword());
    Assert.assertEquals(getHttpMethodInfo(postMethod), HttpStatus.SC_CREATED, postMethod.getStatusCode());

    getMethod = executeGet(commentsUri);
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());

    comments = (Comments) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());

    Assert.assertEquals(numberOfComments + 1, comments.getComments().size());
}

From source file:org.xwiki.test.rest.CommentsResourceTest.java

@Test
public void testPOSTCommentFormUrlEncoded() throws Exception {
    String commentsUri = buildURI(CommentsResource.class, getWiki(), SPACE_NAME, PAGE_NAME).toString();

    GetMethod getMethod = executeGet(commentsUri);
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());

    Comments comments = (Comments) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());

    int numberOfComments = comments.getComments().size();

    NameValuePair[] nameValuePairs = new NameValuePair[1];
    nameValuePairs[0] = new NameValuePair("text", "Comment");

    PostMethod postMethod = executePostForm(commentsUri, nameValuePairs,
            TestUtils.ADMIN_CREDENTIALS.getUserName(), TestUtils.ADMIN_CREDENTIALS.getPassword());
    Assert.assertEquals(getHttpMethodInfo(postMethod), HttpStatus.SC_CREATED, postMethod.getStatusCode());

    getMethod = executeGet(commentsUri);
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());

    comments = (Comments) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());

    Assert.assertEquals(numberOfComments + 1, comments.getComments().size());
}

From source file:org.xwiki.test.rest.framework.AbstractHttpTest.java

protected int setPageContent(String wikiName, List<String> spaceName, String pageName, String content)
        throws Exception {
    String uri = buildURI(PageResource.class, wikiName, spaceName, pageName).toString();

    PutMethod putMethod = executePut(uri, content, javax.ws.rs.core.MediaType.TEXT_PLAIN,
            TestUtils.ADMIN_CREDENTIALS.getUserName(), TestUtils.ADMIN_CREDENTIALS.getPassword());

    int code = putMethod.getStatusCode();
    Assert.assertTrue(String.format("Failed to set page content, %s", getHttpMethodInfo(putMethod)),
            code == HttpStatus.SC_ACCEPTED || code == HttpStatus.SC_CREATED);

    return code;// ww  w .j av  a  2  s . c o m
}

From source file:org.xwiki.test.rest.framework.AbstractHttpTest.java

protected void createPage(List<String> spaces, String pageName, String content) throws Exception {
    String uri = buildURI(PageResource.class, getWiki(), spaces, pageName);

    Page page = this.objectFactory.createPage();
    page.setContent(content);/*from w w w  .ja v a  2 s .  co m*/

    PutMethod putMethod = executePutXml(uri, page, TestUtils.ADMIN_CREDENTIALS.getUserName(),
            TestUtils.ADMIN_CREDENTIALS.getPassword());
    Assert.assertEquals(getHttpMethodInfo(putMethod), HttpStatus.SC_CREATED, putMethod.getStatusCode());
}

From source file:org.xwiki.test.rest.ObjectsResourceTest.java

@Override
@Before/*www .j  ava 2s  .  c o m*/
public void setUp() throws Exception {
    super.setUp();

    GetMethod getMethod = executeGet(buildURI(PageResource.class, getWiki(), MAIN_SPACE, "WebHome").toString());
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());

    Page page = (Page) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    Link link = getFirstLinkByRelation(page, Relations.OBJECTS);

    /* Create a tag object if it doesn't exist yet */
    if (link == null) {
        Object object = objectFactory.createObject();
        object.setClassName("XWiki.TagClass");

        PostMethod postMethod = executePostXml(
                buildURI(ObjectsResource.class, getWiki(), MAIN_SPACE, "WebHome").toString(), object,
                TestUtils.ADMIN_CREDENTIALS.getUserName(), TestUtils.ADMIN_CREDENTIALS.getPassword());
        Assert.assertEquals(getHttpMethodInfo(postMethod), HttpStatus.SC_CREATED, postMethod.getStatusCode());
    }
}

From source file:org.xwiki.test.rest.ObjectsResourceTest.java

@Test
public void testPOSTObject() throws Exception {
    final String TAG_VALUE = "TAG";

    Property property = new Property();
    property.setName("tags");
    property.setValue(TAG_VALUE);/*from w w w .  j ava  2 s.  co  m*/
    Object object = objectFactory.createObject();
    object.setClassName("XWiki.TagClass");
    object.getProperties().add(property);

    PostMethod postMethod = executePostXml(
            buildURI(ObjectsResource.class, getWiki(), MAIN_SPACE, "WebHome").toString(), object,
            TestUtils.ADMIN_CREDENTIALS.getUserName(), TestUtils.ADMIN_CREDENTIALS.getPassword());
    Assert.assertEquals(getHttpMethodInfo(postMethod), HttpStatus.SC_CREATED, postMethod.getStatusCode());

    object = (Object) unmarshaller.unmarshal(postMethod.getResponseBodyAsStream());

    Assert.assertEquals(TAG_VALUE, getProperty(object, "tags").getValue());

    GetMethod getMethod = executeGet(buildURI(ObjectResource.class, getWiki(), MAIN_SPACE, "WebHome",
            object.getClassName(), object.getNumber()).toString());
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());

    object = (Object) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());

    Assert.assertEquals(TAG_VALUE, getProperty(object, "tags").getValue());
}

From source file:org.xwiki.test.rest.ObjectsResourceTest.java

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);//w  w  w  .  j  a  va  2s .c  om
            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.ADMIN_CREDENTIALS.getUserName(), TestUtils.ADMIN_CREDENTIALS.getPassword());
    Assert.assertEquals(getHttpMethodInfo(postMethod), HttpStatus.SC_CREATED, postMethod.getStatusCode());

    object = (Object) unmarshaller.unmarshal(postMethod.getResponseBodyAsStream());

    return object;
}

From source file:org.xwiki.test.rest.ObjectsResourceTest.java

@Test
public void testPOSTObjectFormUrlEncoded() throws Exception {
    final String TAG_VALUE = "TAG";

    NameValuePair[] nameValuePairs = new NameValuePair[2];
    nameValuePairs[0] = new NameValuePair("className", "XWiki.TagClass");
    nameValuePairs[1] = new NameValuePair("property#tags", TAG_VALUE);

    PostMethod postMethod = executePostForm(
            buildURI(ObjectsResource.class, getWiki(), MAIN_SPACE, "WebHome").toString(), nameValuePairs,
            TestUtils.ADMIN_CREDENTIALS.getUserName(), TestUtils.ADMIN_CREDENTIALS.getPassword());
    Assert.assertEquals(getHttpMethodInfo(postMethod), HttpStatus.SC_CREATED, postMethod.getStatusCode());

    Object object = (Object) unmarshaller.unmarshal(postMethod.getResponseBodyAsStream());

    Assert.assertEquals(TAG_VALUE, getProperty(object, "tags").getValue());

    GetMethod getMethod = executeGet(buildURI(ObjectResource.class, getWiki(), MAIN_SPACE, "WebHome",
            object.getClassName(), object.getNumber()).toString());
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());

    object = (Object) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());

    Assert.assertEquals(TAG_VALUE, getProperty(object, "tags").getValue());
}

From source file:org.xwiki.test.rest.PageResourceTest.java

@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 = objectFactory.createPage();
    newPage.setTitle(title);/*from   ww w  . j av  a  2  s.c  o m*/
    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.ADMIN_CREDENTIALS.getUserName(),
            TestUtils.ADMIN_CREDENTIALS.getPassword());
    assertThat(getHttpMethodInfo(putMethod), putMethod.getStatusCode(),
            isIn(Arrays.asList(HttpStatus.SC_ACCEPTED, HttpStatus.SC_CREATED)));

    Page modifiedPage = (Page) 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) 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.ADMIN_CREDENTIALS.getUserName(),
            TestUtils.ADMIN_CREDENTIALS.getPassword());
    assertThat(getHttpMethodInfo(putMethod), putMethod.getStatusCode(),
            isIn(Arrays.asList(HttpStatus.SC_ACCEPTED)));

    modifiedPage = (Page) 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) 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());
}