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.wso2.mdm.integration.user.UserManagement.java

@Test(description = "Test update user.", dependsOnMethods = { "testAddUser" })
public void testUpdateUser() throws Exception {
    String url = GetURL(Constants.UserManagement.USER_ENDPOINT);
    HttpResponse response = client.put(url,
            PayloadGenerator//from  w w w .  j  ava  2 s  . c  om
                    .getJsonPayload(Constants.UserManagement.USER_PAYLOAD_FILE_NAME, Constants.HTTP_METHOD_PUT)
                    .toString());
    Assert.assertEquals(HttpStatus.SC_CREATED, response.getResponseCode());
    AssertUtil.jsonPayloadCompare(PayloadGenerator
            .getJsonPayload(Constants.UserManagement.USER_RESPONSE_PAYLOAD_FILE_NAME, Constants.HTTP_METHOD_PUT)
            .toString(), response.getData().toString(), true);

}

From source file:org.xwiki.manager.rest.test.WikiManagerRestTest.java

@Test
public void testCreateWiki() throws Exception {
    String WIKI_ID = "foo";

    Wiki wiki = objectFactory.createWiki();
    wiki.setId(WIKI_ID);//from w ww. j  ava 2s . c  o m

    PostMethod postMethod = executePost(getFullUri(WikiManagerResource.class), "superadmin", "pass", wiki);
    Assert.assertEquals(HttpStatus.SC_CREATED, postMethod.getStatusCode());

    wiki = (Wiki) unmarshaller.unmarshal(postMethod.getResponseBodyAsStream());
    Assert.assertEquals(WIKI_ID, wiki.getId());

    GetMethod getMethod = executeGet(getFullUri(WikisResource.class));
    Assert.assertEquals(HttpStatus.SC_OK, getMethod.getStatusCode());

    Wikis wikis = (Wikis) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());

    boolean found = false;
    for (Wiki w : wikis.getWikis()) {
        if (WIKI_ID.equals(w.getId())) {
            found = true;
            break;
        }
    }

    Assert.assertTrue(found);
}

From source file:org.xwiki.manager.rest.test.WikiManagerRestTest.java

@Ignore("This test doesn't seem to work correctly with HSQLDB but it actually works if run against MySQL.")
@Test// w w w.j a  v  a  2 s  .  c  om
public void testMultiwikiSearch() throws Exception {
    String WIKI1_ID = "w1";
    String WIKI2_ID = "w2";
    String PAGE_SPACE = "Main";
    String PAGE_NAME = "Test";
    String PAGE1_STRING = "foo";
    String PAGE2_STRING = "bar";

    Wiki wiki = objectFactory.createWiki();
    wiki.setId(WIKI1_ID);

    PostMethod postMethod = executePost(getFullUri(WikiManagerResource.class), "superadmin", "pass", wiki);
    Assert.assertEquals(HttpStatus.SC_CREATED, postMethod.getStatusCode());

    wiki = objectFactory.createWiki();
    wiki.setId(WIKI2_ID);

    postMethod = executePost(getFullUri(WikiManagerResource.class), "superadmin", "pass", wiki);
    Assert.assertEquals(HttpStatus.SC_CREATED, postMethod.getStatusCode());

    /* Store the page */
    Page page1 = objectFactory.createPage();
    page1.setTitle(PAGE1_STRING);
    page1.setContent(PAGE1_STRING);
    PutMethod putMethod = executePut(
            getUriBuilder(PageResource.class).build(WIKI1_ID, PAGE_SPACE, PAGE_NAME).toString(), "superadmin",
            "pass", page1);
    Assert.assertEquals(HttpStatus.SC_CREATED, putMethod.getStatusCode());
    page1 = (Page) unmarshaller.unmarshal(putMethod.getResponseBodyAsStream());

    /* Retrieve the page to check that it exists */
    GetMethod getMethod = executeGet(
            getUriBuilder(PageResource.class).build(WIKI1_ID, PAGE_SPACE, PAGE_NAME).toString());
    Assert.assertEquals(HttpStatus.SC_OK, getMethod.getStatusCode());
    Page page = (Page) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    Assert.assertEquals(WIKI1_ID, page.getWiki());
    Assert.assertEquals(PAGE_SPACE, page.getSpace());
    Assert.assertEquals(PAGE_NAME, page.getName());
    Assert.assertEquals(PAGE1_STRING, page.getTitle());
    Assert.assertEquals(PAGE1_STRING, page.getContent());
    Assert.assertEquals(page1.getCreated(), page.getCreated());
    Assert.assertEquals(page1.getModified(), page.getModified());

    /* Store the page */
    Page page2 = objectFactory.createPage();
    page2.setTitle(PAGE2_STRING);
    page2.setContent(PAGE2_STRING);
    putMethod = executePut(getUriBuilder(PageResource.class).build(WIKI2_ID, PAGE_SPACE, PAGE_NAME).toString(),
            "superadmin", "pass", page2);
    Assert.assertEquals(HttpStatus.SC_CREATED, putMethod.getStatusCode());
    page2 = (Page) unmarshaller.unmarshal(putMethod.getResponseBodyAsStream());

    /* Retrieve the page to check that it exists */
    getMethod = executeGet(getUriBuilder(PageResource.class).build(WIKI2_ID, PAGE_SPACE, PAGE_NAME).toString());
    Assert.assertEquals(HttpStatus.SC_OK, getMethod.getStatusCode());
    page = (Page) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    Assert.assertEquals(WIKI2_ID, page.getWiki());
    Assert.assertEquals(PAGE_SPACE, page.getSpace());
    Assert.assertEquals(PAGE_NAME, page.getName());
    Assert.assertEquals(PAGE2_STRING, page.getTitle());
    Assert.assertEquals(PAGE2_STRING, page.getContent());
    Assert.assertEquals(page2.getCreated(), page.getCreated());
    Assert.assertEquals(page2.getModified(), page.getModified());

    /* Wait a bit that the Lucene Indexer indexes the pages. */
    Thread.sleep(5000);

    getMethod = executeGet(URIUtil.encodeQuery(
            String.format("%s?q=\"%s\"&wikis=w1,w2", getFullUri(WikisSearchQueryResource.class), PAGE_NAME)));
    Assert.assertEquals(HttpStatus.SC_OK, getMethod.getStatusCode());

    SearchResults searchResults = (SearchResults) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());

    Assert.assertEquals(2, searchResults.getSearchResults().size());

    for (SearchResult searchResult : searchResults.getSearchResults()) {
        Page pageToBeCheckedAgainst = null;
        if (searchResult.getWiki().equals(WIKI1_ID)) {
            pageToBeCheckedAgainst = page1;
        } else {
            pageToBeCheckedAgainst = page2;
        }

        Assert.assertEquals(pageToBeCheckedAgainst.getWiki(), searchResult.getWiki());
        Assert.assertEquals(pageToBeCheckedAgainst.getTitle(), searchResult.getTitle());
        Assert.assertEquals(pageToBeCheckedAgainst.getAuthor(), searchResult.getAuthor());
        Assert.assertEquals(pageToBeCheckedAgainst.getModified(), searchResult.getModified());
        Assert.assertEquals(pageToBeCheckedAgainst.getVersion(), searchResult.getVersion());
    }
}

From source file:org.xwiki.test.cluster.DocumentCacheTest.java

@Test
public void testDocumentCacheSyncForAttachments() throws Exception {
    // 1) Edit a page on XWiki 0
    switchXWiki(0);//from   w  w  w .j  a  va2  s  .  com
    setPageContent(getWiki(), "Test", "AttachementCacheSync", "content");

    // 2) Add attachment to the page on XWiki 1
    switchXWiki(1);
    String attachmentUri = getUriBuilder(AttachmentResource.class)
            .build(getWiki(), "Test", "AttachementCacheSync", "file.ext").toString();
    PutMethod putMethod = executePut(attachmentUri, "content", MediaType.TEXT_PLAIN, "Admin", "admin");
    Assert.assertEquals(getHttpMethodInfo(putMethod), HttpStatus.SC_CREATED, putMethod.getStatusCode());

    // ASSERT) The content in XWiki 0 should be the one set than in XWiki 1
    // Since it can take time for the Cluster to propagate the change, we need to wait and set up a timeout.
    switchXWiki(0);
    String attachmentsUri = getUriBuilder(AttachmentsResource.class)
            .build(getWiki(), "Test", "AttachementCacheSync").toString();

    long t1 = System.currentTimeMillis();
    long t2;
    while (!hasAttachment(attachmentsUri)) {
        t2 = System.currentTimeMillis();
        if (t2 - t1 > 10000L) {
            Assert.fail("Failed to find attachment");
        }
        Thread.sleep(100);
    }
}

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

protected void putAttachmentFilename(String attachmentName, String type) throws Exception {
    String content = "ATTACHMENT CONTENT";
    String attachmentURI = buildURIForThisPage(AttachmentResource.class, attachmentName);

    GetMethod getMethod = executeGet(attachmentURI);
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_NOT_FOUND, getMethod.getStatusCode());

    PutMethod putMethod = executePut(attachmentURI, content, MediaType.TEXT_PLAIN,
            TestUtils.ADMIN_CREDENTIALS.getUserName(), TestUtils.ADMIN_CREDENTIALS.getPassword());
    Assert.assertEquals(getHttpMethodInfo(putMethod), HttpStatus.SC_CREATED, putMethod.getStatusCode());

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

    Assert.assertEquals(content, getMethod.getResponseBodyAsString());
}

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

@Test
public void testDELETEAttachment() throws Exception {
    String attachmentName = String.format("%d.txt", System.currentTimeMillis());
    String attachmentURI = buildURIForThisPage(AttachmentResource.class, attachmentName);
    String content = "ATTACHMENT CONTENT";

    PutMethod putMethod = executePut(attachmentURI, content, MediaType.TEXT_PLAIN,
            TestUtils.ADMIN_CREDENTIALS.getUserName(), TestUtils.ADMIN_CREDENTIALS.getPassword());
    Assert.assertEquals(getHttpMethodInfo(putMethod), HttpStatus.SC_CREATED, putMethod.getStatusCode());

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

    DeleteMethod deleteMethod = executeDelete(attachmentURI, TestUtils.ADMIN_CREDENTIALS.getUserName(),
            TestUtils.ADMIN_CREDENTIALS.getPassword());
    Assert.assertEquals(getHttpMethodInfo(deleteMethod), HttpStatus.SC_NO_CONTENT,
            deleteMethod.getStatusCode());

    getMethod = executeGet(attachmentURI);
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_NOT_FOUND, getMethod.getStatusCode());
}

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

@Test
public void testDELETEAttachmentNoRights() throws Exception {
    String attachmentName = String.format("%d.txt", System.currentTimeMillis());
    String attachmentURI = buildURIForThisPage(AttachmentResource.class, attachmentName);

    String content = "ATTACHMENT CONTENT";

    PutMethod putMethod = executePut(attachmentURI, content, MediaType.TEXT_PLAIN,
            TestUtils.ADMIN_CREDENTIALS.getUserName(), TestUtils.ADMIN_CREDENTIALS.getPassword());
    Assert.assertEquals(getHttpMethodInfo(putMethod), HttpStatus.SC_CREATED, putMethod.getStatusCode());

    DeleteMethod deleteMethod = executeDelete(attachmentURI);
    Assert.assertEquals(getHttpMethodInfo(deleteMethod), HttpStatus.SC_UNAUTHORIZED,
            deleteMethod.getStatusCode());

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

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

@Test
public void testGETAttachmentsAtPageVersion() throws Exception {
    final int NUMBER_OF_ATTACHMENTS = 4;
    String[] attachmentNames = new String[NUMBER_OF_ATTACHMENTS];
    String[] pageVersions = new String[NUMBER_OF_ATTACHMENTS];

    for (int i = 0; i < NUMBER_OF_ATTACHMENTS; i++) {
        attachmentNames[i] = String.format("%s.txt", UUID.randomUUID());
    }/*w w w.  j  a  v a 2  s .  co  m*/

    String content = "ATTACHMENT CONTENT";

    /* Create NUMBER_OF_ATTACHMENTS attachments */
    for (int i = 0; i < NUMBER_OF_ATTACHMENTS; i++) {
        String attachmentURI = buildURIForThisPage(AttachmentResource.class, attachmentNames[i]);

        PutMethod putMethod = executePut(attachmentURI, content, MediaType.TEXT_PLAIN,
                TestUtils.ADMIN_CREDENTIALS.getUserName(), TestUtils.ADMIN_CREDENTIALS.getPassword());
        Assert.assertEquals(getHttpMethodInfo(putMethod), HttpStatus.SC_CREATED, putMethod.getStatusCode());

        Attachment attachment = (Attachment) this.unmarshaller.unmarshal(putMethod.getResponseBodyAsStream());
        pageVersions[i] = attachment.getPageVersion();
    }

    // For each page version generated, check that the attachments that are supposed to be there are actually there.
    // We do the following: at pageVersion[i] we check that all attachmentNames[0..i] are there.
    for (int i = 0; i < NUMBER_OF_ATTACHMENTS; i++) {
        String attachmentsUri = buildURIForThisPage(AttachmentsAtPageVersionResource.class, pageVersions[i]);
        GetMethod getMethod = executeGet(attachmentsUri);
        Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());

        Attachments attachments = (Attachments) this.unmarshaller
                .unmarshal(getMethod.getResponseBodyAsStream());

        /*
         * Check that all attachmentNames[0..i] are present in the list of attachments of page at version
         * pageVersions[i]
         */
        for (int j = 0; j <= i; j++) {
            boolean found = false;
            for (Attachment attachment : attachments.getAttachments()) {
                if (attachment.getName().equals(attachmentNames[j])) {
                    if (attachment.getPageVersion().equals(pageVersions[i])) {
                        found = true;
                        break;
                    }
                }
            }
            Assert.assertTrue(String.format("%s is not present in attachments list of the page at version %s",
                    attachmentNames[j], pageVersions[i]), found);
        }

        /* Check links */
        for (Attachment attachment : attachments.getAttachments()) {
            checkLinks(attachment);
        }
    }
}

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

@Test
public void testGETAttachmentVersions() throws Exception {
    final int NUMBER_OF_VERSIONS = 4;
    String attachmentName = String.format("%s.txt", UUID.randomUUID().toString());

    Map<String, String> versionToContentMap = new HashMap<String, String>();

    /* Create NUMBER_OF_ATTACHMENTS attachments */
    for (int i = 0; i < NUMBER_OF_VERSIONS; i++) {
        String attachmentURI = buildURIForThisPage(AttachmentResource.class, attachmentName);
        String content = String.format("CONTENT %d", i);
        PutMethod putMethod = executePut(attachmentURI, content, MediaType.TEXT_PLAIN,
                TestUtils.ADMIN_CREDENTIALS.getUserName(), TestUtils.ADMIN_CREDENTIALS.getPassword());
        if (i == 0) {
            Assert.assertEquals(getHttpMethodInfo(putMethod), HttpStatus.SC_CREATED, putMethod.getStatusCode());
        } else {// ww  w .j a v a 2 s .  co  m
            Assert.assertEquals(getHttpMethodInfo(putMethod), HttpStatus.SC_ACCEPTED,
                    putMethod.getStatusCode());
        }

        Attachment attachment = (Attachment) this.unmarshaller.unmarshal(putMethod.getResponseBodyAsStream());

        versionToContentMap.put(attachment.getVersion(), content);
    }

    String attachmentsUri = buildURIForThisPage(AttachmentHistoryResource.class, attachmentName);
    GetMethod getMethod = executeGet(attachmentsUri);
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());

    Attachments attachments = (Attachments) this.unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    Assert.assertEquals(NUMBER_OF_VERSIONS, attachments.getAttachments().size());

    for (Attachment attachment : attachments.getAttachments()) {
        getMethod = executeGet(getFirstLinkByRelation(attachment, Relations.ATTACHMENT_DATA).getHref());
        Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());

        Assert.assertEquals(versionToContentMap.get(attachment.getVersion()),
                getMethod.getResponseBodyAsString());
    }
}

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

@Test
public void testPOSTAttachment() throws Exception {
    final String attachmentName = String.format("%s.txt", UUID.randomUUID());
    final String content = "ATTACHMENT CONTENT";

    String attachmentsUri = buildURIForThisPage(AttachmentsResource.class, attachmentName);

    HttpClient httpClient = new HttpClient();
    httpClient.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(
            TestUtils.ADMIN_CREDENTIALS.getUserName(), TestUtils.ADMIN_CREDENTIALS.getPassword()));
    httpClient.getParams().setAuthenticationPreemptive(true);

    Part[] parts = new Part[1];

    ByteArrayPartSource baps = new ByteArrayPartSource(attachmentName, content.getBytes());
    parts[0] = new FilePart(attachmentName, baps);

    PostMethod postMethod = new PostMethod(attachmentsUri);
    MultipartRequestEntity mpre = new MultipartRequestEntity(parts, postMethod.getParams());
    postMethod.setRequestEntity(mpre);//from w  w  w .j  ava2  s .  co  m
    httpClient.executeMethod(postMethod);
    Assert.assertEquals(getHttpMethodInfo(postMethod), HttpStatus.SC_CREATED, postMethod.getStatusCode());

    this.unmarshaller.unmarshal(postMethod.getResponseBodyAsStream());

    Header location = postMethod.getResponseHeader("location");

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

    Assert.assertEquals(content, getMethod.getResponseBodyAsString());
}