Search in sources :

Example 11 with SearchResults

use of org.xwiki.rest.model.jaxb.SearchResults in project xwiki-platform by xwiki.

the class WikisResourceTest method testSearchWikis.

@Test
public void testSearchWikis() throws Exception {
    this.testUtils.rest().delete(reference);
    this.testUtils.rest().savePage(reference, "content" + this.pageName, "title" + this.pageName);
    GetMethod getMethod = executeGet(String.format("%s?q=content" + this.pageName, buildURI(WikiSearchResource.class, getWiki())));
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
    SearchResults searchResults = (SearchResults) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    int resultSize = searchResults.getSearchResults().size();
    assertEquals(1, resultSize);
    for (SearchResult searchResult : searchResults.getSearchResults()) {
        checkLinks(searchResult);
    }
    getMethod = executeGet(String.format("%s?q=" + this.pageName + "&scope=name", buildURI(WikiSearchResource.class, getWiki())));
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
    searchResults = (SearchResults) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    resultSize = searchResults.getSearchResults().size();
    assertEquals(1, resultSize);
    for (SearchResult searchResult : searchResults.getSearchResults()) {
        checkLinks(searchResult);
    }
    // Search in titles
    getMethod = executeGet(String.format("%s?q=title" + this.pageName + "&scope=title", buildURI(WikiSearchResource.class, getWiki())));
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
    searchResults = (SearchResults) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    resultSize = searchResults.getSearchResults().size();
    assertEquals(1, resultSize);
    for (SearchResult searchResult : searchResults.getSearchResults()) {
        checkLinks(searchResult);
    }
    // Search for space names
    getMethod = executeGet(String.format("%s?q=" + this.spaces.get(0) + "&scope=spaces", buildURI(WikiSearchResource.class, getWiki())));
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
    searchResults = (SearchResults) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    resultSize = searchResults.getSearchResults().size();
    assertEquals(1, resultSize);
    for (SearchResult searchResult : searchResults.getSearchResults()) {
        checkLinks(searchResult);
    }
}
Also used : WikiSearchResource(org.xwiki.rest.resources.wikis.WikiSearchResource) GetMethod(org.apache.commons.httpclient.methods.GetMethod) SearchResult(org.xwiki.rest.model.jaxb.SearchResult) SearchResults(org.xwiki.rest.model.jaxb.SearchResults) AbstractHttpTest(org.xwiki.test.rest.framework.AbstractHttpTest) Test(org.junit.Test)

Example 12 with SearchResults

use of org.xwiki.rest.model.jaxb.SearchResults in project xwiki-platform by xwiki.

the class WikisResourceTest method testSolrSearch.

@Test
public void testSolrSearch() throws Exception {
    this.testUtils.rest().delete(this.reference);
    this.testUtils.rest().savePage(this.reference);
    this.solrUtils.waitEmpyQueue();
    GetMethod getMethod = executeGet(URIUtil.encodeQuery(String.format("%s?q=\"" + this.pageName + "\"&type=solr", buildURI(WikiSearchQueryResource.class, getWiki()))));
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
    SearchResults searchResults = (SearchResults) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    int resultSize = searchResults.getSearchResults().size();
    assertEquals(1, resultSize);
    assertEquals(this.fullName, searchResults.getSearchResults().get(0).getPageFullName());
}
Also used : WikiSearchQueryResource(org.xwiki.rest.resources.wikis.WikiSearchQueryResource) GetMethod(org.apache.commons.httpclient.methods.GetMethod) SearchResults(org.xwiki.rest.model.jaxb.SearchResults) AbstractHttpTest(org.xwiki.test.rest.framework.AbstractHttpTest) Test(org.junit.Test)

Example 13 with SearchResults

use of org.xwiki.rest.model.jaxb.SearchResults 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 14 with SearchResults

use of org.xwiki.rest.model.jaxb.SearchResults in project xwiki-platform by xwiki.

the class WikiSearchResourceImpl method search.

@Override
public SearchResults search(String wikiName, String keywords, List<String> searchScopeStrings, Integer number, Integer start, String orderField, String order, Boolean withPrettyNames) throws XWikiRestException {
    try {
        SearchResults searchResults = objectFactory.createSearchResults();
        searchResults.setTemplate(String.format("%s?%s", Utils.createURI(uriInfo.getBaseUri(), WikiSearchResource.class, wikiName).toString(), SEARCH_TEMPLATE_INFO));
        if (wikiName != null) {
            Utils.getXWikiContext(componentManager).setWikiId(wikiName);
        }
        List<SearchScope> searchScopes = parseSearchScopeStrings(searchScopeStrings);
        searchResults.getSearchResults().addAll(search(searchScopes, keywords, getXWikiContext().getWikiId(), null, Utils.getXWiki(componentManager).getRightService().hasProgrammingRights(Utils.getXWikiContext(componentManager)), number, start, true, orderField, order, withPrettyNames));
        return searchResults;
    } catch (Exception e) {
        throw new XWikiRestException(e);
    }
}
Also used : XWikiRestException(org.xwiki.rest.XWikiRestException) WikiSearchResource(org.xwiki.rest.resources.wikis.WikiSearchResource) SearchResults(org.xwiki.rest.model.jaxb.SearchResults) XWikiRestException(org.xwiki.rest.XWikiRestException)

Aggregations

SearchResults (org.xwiki.rest.model.jaxb.SearchResults)14 GetMethod (org.apache.commons.httpclient.methods.GetMethod)10 Test (org.junit.Test)10 AbstractHttpTest (org.xwiki.test.rest.framework.AbstractHttpTest)9 WikiSearchQueryResource (org.xwiki.rest.resources.wikis.WikiSearchQueryResource)5 XWikiRestException (org.xwiki.rest.XWikiRestException)4 SearchResult (org.xwiki.rest.model.jaxb.SearchResult)3 SpaceSearchResource (org.xwiki.rest.resources.spaces.SpaceSearchResource)2 WikiSearchResource (org.xwiki.rest.resources.wikis.WikiSearchResource)2 PostMethod (org.apache.commons.httpclient.methods.PostMethod)1 PutMethod (org.apache.commons.httpclient.methods.PutMethod)1 Ignore (org.junit.Ignore)1 DocumentReference (org.xwiki.model.reference.DocumentReference)1 Page (org.xwiki.rest.model.jaxb.Page)1 Wiki (org.xwiki.rest.model.jaxb.Wiki)1 PageResource (org.xwiki.rest.resources.pages.PageResource)1 WikisSearchQueryResource (org.xwiki.rest.resources.wikis.WikisSearchQueryResource)1 WikiManagerREST (org.xwiki.wiki.rest.WikiManagerREST)1