Search in sources :

Example 71 with PutMethod

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

the class WikiManagerRestTest method testMultiwikiSearch.

// FIXME: Test is disabled for the moment. It works if tested against MySQL but with HSQLDB it seems that the
// Lucene plugin is not triggered. Anyway this should be better to rewrite it, if possible, as a unit test.
@Ignore("This test doesn't seem to work correctly with HSQLDB but it actually works if run against MySQL.")
@Test
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(WikiManagerREST.class), "superadmin", "pass", wiki);
    Assert.assertEquals(HttpStatus.SC_CREATED, postMethod.getStatusCode());
    wiki = objectFactory.createWiki();
    wiki.setId(WIKI2_ID);
    postMethod = executePost(getFullUri(WikiManagerREST.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());
    }
}
Also used : PageResource(org.xwiki.rest.resources.pages.PageResource) PostMethod(org.apache.commons.httpclient.methods.PostMethod) GetMethod(org.apache.commons.httpclient.methods.GetMethod) Wiki(org.xwiki.rest.model.jaxb.Wiki) WikiManagerREST(org.xwiki.wiki.rest.WikiManagerREST) PutMethod(org.apache.commons.httpclient.methods.PutMethod) Page(org.xwiki.rest.model.jaxb.Page) SearchResult(org.xwiki.rest.model.jaxb.SearchResult) SearchResults(org.xwiki.rest.model.jaxb.SearchResults) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 72 with PutMethod

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

the class AttachmentsResourceTest method testDELETEAttachment.

@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.SUPER_ADMIN_CREDENTIALS.getUserName(), TestUtils.SUPER_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.SUPER_ADMIN_CREDENTIALS.getUserName(), TestUtils.SUPER_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());
}
Also used : DeleteMethod(org.apache.commons.httpclient.methods.DeleteMethod) GetMethod(org.apache.commons.httpclient.methods.GetMethod) PutMethod(org.apache.commons.httpclient.methods.PutMethod) AbstractHttpTest(org.xwiki.test.rest.framework.AbstractHttpTest) Test(org.junit.Test)

Example 73 with PutMethod

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

the class AttachmentsResourceTest method testDELETEAttachmentNoRights.

@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.SUPER_ADMIN_CREDENTIALS.getUserName(), TestUtils.SUPER_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());
}
Also used : DeleteMethod(org.apache.commons.httpclient.methods.DeleteMethod) GetMethod(org.apache.commons.httpclient.methods.GetMethod) PutMethod(org.apache.commons.httpclient.methods.PutMethod) AbstractHttpTest(org.xwiki.test.rest.framework.AbstractHttpTest) Test(org.junit.Test)

Example 74 with PutMethod

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

the class AttachmentsResourceTest method putAttachmentFilename.

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.SUPER_ADMIN_CREDENTIALS.getUserName(), TestUtils.SUPER_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());
}
Also used : GetMethod(org.apache.commons.httpclient.methods.GetMethod) PutMethod(org.apache.commons.httpclient.methods.PutMethod)

Example 75 with PutMethod

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

the class AttachmentsResourceTest method testPUTAttachmentNoRights.

@Test
public void testPUTAttachmentNoRights() throws Exception {
    String attachmentName = String.format("%s.txt", UUID.randomUUID());
    String attachmentURI = buildURIForThisPage(AttachmentResource.class, attachmentName);
    String content = "ATTACHMENT CONTENT";
    GetMethod getMethod = executeGet(attachmentURI);
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_NOT_FOUND, getMethod.getStatusCode());
    PutMethod putMethod = executePut(attachmentURI, content, MediaType.TEXT_PLAIN);
    Assert.assertEquals(getHttpMethodInfo(putMethod), HttpStatus.SC_UNAUTHORIZED, putMethod.getStatusCode());
}
Also used : GetMethod(org.apache.commons.httpclient.methods.GetMethod) PutMethod(org.apache.commons.httpclient.methods.PutMethod) 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