Search in sources :

Example 1 with SolrReferenceResolver

use of org.xwiki.search.solr.internal.reference.SolrReferenceResolver in project xwiki-platform by xwiki.

the class SolrDocumentIteratorTest method iterate.

@Test
public void iterate() throws Exception {
    SolrDocumentList firstResults = new SolrDocumentList();
    firstResults.add(createSolrDocument("chess", Arrays.asList("A", "B"), "C", "", "1.3"));
    firstResults.add(createSolrDocument("chess", Arrays.asList("M"), "N", "en", "2.4"));
    QueryResponse firstResponse = mock(QueryResponse.class);
    when(firstResponse.getNextCursorMark()).thenReturn("foo");
    when(firstResponse.getResults()).thenReturn(firstResults);
    SolrDocumentList secondResults = new SolrDocumentList();
    secondResults.add(createSolrDocument("tennis", Arrays.asList("X", "Y", "Z"), "V", "fr", "1.1"));
    QueryResponse secondResponse = mock(QueryResponse.class);
    when(secondResponse.getNextCursorMark()).thenReturn("bar");
    when(secondResponse.getResults()).thenReturn(secondResults);
    when(solr.query(any(SolrQuery.class))).thenReturn(firstResponse, secondResponse, secondResponse);
    DocumentIterator<String> iterator = mocker.getComponentUnderTest();
    WikiReference rootReference = new WikiReference("wiki");
    iterator.setRootReference(rootReference);
    List<Pair<DocumentReference, String>> actualResult = new ArrayList<Pair<DocumentReference, String>>();
    while (iterator.hasNext()) {
        actualResult.add(iterator.next());
    }
    SolrReferenceResolver resolver = mocker.getInstance(SolrReferenceResolver.class);
    verify(resolver).getQuery(rootReference);
    List<Pair<DocumentReference, String>> expectedResult = new ArrayList<Pair<DocumentReference, String>>();
    DocumentReference documentReference = new DocumentReference("chess", Arrays.asList("A", "B"), "C");
    expectedResult.add(new ImmutablePair<DocumentReference, String>(documentReference, "1.3"));
    documentReference = new DocumentReference("chess", Arrays.asList("M"), "N", Locale.ENGLISH);
    expectedResult.add(new ImmutablePair<DocumentReference, String>(documentReference, "2.4"));
    documentReference = new DocumentReference("tennis", Arrays.asList("X", "Y", "Z"), "V", Locale.FRENCH);
    expectedResult.add(new ImmutablePair<DocumentReference, String>(documentReference, "1.1"));
    assertEquals(expectedResult, actualResult);
}
Also used : SolrReferenceResolver(org.xwiki.search.solr.internal.reference.SolrReferenceResolver) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) ArrayList(java.util.ArrayList) SolrDocumentList(org.apache.solr.common.SolrDocumentList) WikiReference(org.xwiki.model.reference.WikiReference) SolrQuery(org.apache.solr.client.solrj.SolrQuery) DocumentReference(org.xwiki.model.reference.DocumentReference) Pair(org.apache.commons.lang3.tuple.Pair) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) Test(org.junit.Test)

Example 2 with SolrReferenceResolver

use of org.xwiki.search.solr.internal.reference.SolrReferenceResolver in project xwiki-platform by xwiki.

the class SolrDocumentIteratorTest method size.

@Test
public void size() throws Exception {
    SolrDocumentList results = mock(SolrDocumentList.class);
    when(results.getNumFound()).thenReturn(12L);
    QueryResponse response = mock(QueryResponse.class);
    when(response.getResults()).thenReturn(results);
    when(solr.query(any(SolrQuery.class))).thenReturn(response);
    DocumentIterator<String> iterator = mocker.getComponentUnderTest();
    WikiReference rootReference = new WikiReference("wiki");
    iterator.setRootReference(rootReference);
    assertEquals(12, iterator.size());
    SolrReferenceResolver resolver = mocker.getInstance(SolrReferenceResolver.class);
    verify(resolver).getQuery(rootReference);
}
Also used : SolrReferenceResolver(org.xwiki.search.solr.internal.reference.SolrReferenceResolver) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrDocumentList(org.apache.solr.common.SolrDocumentList) WikiReference(org.xwiki.model.reference.WikiReference) SolrQuery(org.apache.solr.client.solrj.SolrQuery) Test(org.junit.Test)

Example 3 with SolrReferenceResolver

use of org.xwiki.search.solr.internal.reference.SolrReferenceResolver in project xwiki-platform by xwiki.

the class DocumentSolrMetadataExtractorTest method getSimpleDocument.

@Test
public void getSimpleDocument() throws Exception {
    // 
    // Mock
    // 
    // ID
    String id = "wiki:Space.Name_" + Locale.ROOT.toString();
    SolrReferenceResolver documentSolrReferenceResolver = this.mocker.getInstance(SolrReferenceResolver.class, "document");
    when(documentSolrReferenceResolver.getId(documentReference)).thenReturn(id);
    // Full Name
    String fullName = "Space.Name";
    EntityReferenceSerializer<String> localEntityReferenceSerializer = this.mocker.registerMockComponent(EntityReferenceSerializer.TYPE_STRING, "local");
    when(localEntityReferenceSerializer.serialize(this.documentReference)).thenReturn(fullName);
    // Hierarchy
    when(localEntityReferenceSerializer.serialize(this.documentReference.getParent())).thenReturn("Path.To.Page");
    when(localEntityReferenceSerializer.serialize(this.documentReference.getParent().getParent())).thenReturn("Path.To");
    when(localEntityReferenceSerializer.serialize(this.documentReference.getParent().getParent().getParent())).thenReturn("Path");
    // Creator.
    DocumentReference creatorReference = new DocumentReference("wiki", "Space", "Creator");
    when(this.document.getCreatorReference()).thenReturn(creatorReference);
    String creatorStringReference = "wiki:Space.Creator";
    EntityReferenceSerializer<String> entityReferenceSerializer = this.mocker.registerMockComponent(EntityReferenceSerializer.TYPE_STRING, "default");
    when(entityReferenceSerializer.serialize(creatorReference)).thenReturn(creatorStringReference);
    String creatorDisplayName = "Crea Tor";
    when(this.xcontext.getWiki().getPlainUserName(creatorReference, this.xcontext)).thenReturn(creatorDisplayName);
    // Author.
    DocumentReference authorReference = new DocumentReference("wiki", "Space", "Author");
    when(this.document.getAuthorReference()).thenReturn(authorReference);
    String authorStringReference = "wiki:Space.Author";
    when(entityReferenceSerializer.serialize(authorReference)).thenReturn(authorStringReference);
    String authorDisplayName = "Au Thor";
    when(this.xcontext.getWiki().getPlainUserName(authorReference, this.xcontext)).thenReturn(authorDisplayName);
    // Creation Date
    Date creationDate = new Date();
    when(this.document.getCreationDate()).thenReturn(creationDate);
    // Date
    Date date = new Date();
    when(this.document.getContentUpdateDate()).thenReturn(date);
    // Version
    String version = "1.1";
    when(this.document.getVersion()).thenReturn(version);
    // Version summary
    String comment = "1.1 comment";
    when(this.document.getComment()).thenReturn(comment);
    // XObjects.
    when(this.document.getXObjects()).thenReturn(Collections.<DocumentReference, List<BaseObject>>emptyMap());
    // Title
    String title = "title";
    when(this.document.getRenderedTitle(any(), same(this.xcontext))).thenReturn(title);
    // Rendered Content
    final String renderedContent = "rendered content";
    BlockRenderer plainRenderer = this.mocker.registerMockComponent(BlockRenderer.class, "plain/1.0");
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) {
            Object[] args = invocation.getArguments();
            WikiPrinter printer = (WikiPrinter) args[1];
            printer.print(renderedContent);
            return null;
        }
    }).when(plainRenderer).render((Block) any(), any());
    // Raw Content
    String rawContent = "raw content";
    when(this.document.getContent()).thenReturn(rawContent);
    // 
    // Call
    // 
    SolrInputDocument solrDocument = this.mocker.getComponentUnderTest().getSolrDocument(this.documentReference);
    // 
    // Assert and verify
    // 
    assertEquals(id, solrDocument.getFieldValue(FieldUtils.ID));
    assertEquals(this.documentReference.getWikiReference().getName(), solrDocument.getFieldValue(FieldUtils.WIKI));
    assertEquals(Arrays.asList("Path", "To", "Page"), solrDocument.getFieldValues(FieldUtils.SPACES));
    assertEquals(this.documentReference.getName(), solrDocument.getFieldValue(FieldUtils.NAME));
    assertEquals(Arrays.asList("0/Path.", "1/Path.To.", "2/Path.To.Page."), solrDocument.getFieldValues(FieldUtils.SPACE_FACET));
    assertEquals(Arrays.asList("Path", "Path.To", "Path.To.Page"), solrDocument.getFieldValues(FieldUtils.SPACE_PREFIX));
    assertEquals(Locale.US.toString(), solrDocument.getFieldValue(FieldUtils.LOCALE));
    assertEquals(Locale.US.getLanguage(), solrDocument.getFieldValue(FieldUtils.LANGUAGE));
    Collection<?> actualLocales = solrDocument.getFieldValues(FieldUtils.LOCALES);
    // The order of the locales in the returned collection is nondeterministic.
    assertTrue(actualLocales.size() == 2 && actualLocales.contains("") && actualLocales.contains(Locale.US.toString()));
    assertEquals(this.document.isHidden(), solrDocument.getFieldValue(FieldUtils.HIDDEN));
    assertEquals(EntityType.DOCUMENT.name(), solrDocument.getFieldValue(FieldUtils.TYPE));
    assertEquals(fullName, solrDocument.getFieldValue(FieldUtils.FULLNAME));
    assertEquals(title, solrDocument.getFieldValue(FieldUtils.getFieldName(FieldUtils.TITLE, Locale.US)));
    assertEquals(rawContent, solrDocument.getFieldValue(FieldUtils.getFieldName(FieldUtils.DOCUMENT_RAW_CONTENT, Locale.US)));
    assertEquals(renderedContent, solrDocument.getFieldValue(FieldUtils.getFieldName(FieldUtils.DOCUMENT_RENDERED_CONTENT, Locale.US)));
    assertEquals(version, solrDocument.getFieldValue(FieldUtils.VERSION));
    assertEquals(comment, solrDocument.getFieldValue(FieldUtils.COMMENT));
    assertEquals(authorStringReference, solrDocument.getFieldValue(FieldUtils.AUTHOR));
    assertEquals(authorDisplayName, solrDocument.getFieldValue(FieldUtils.AUTHOR_DISPLAY));
    assertEquals(creatorStringReference, solrDocument.getFieldValue(FieldUtils.CREATOR));
    assertEquals(creatorDisplayName, solrDocument.getFieldValue(FieldUtils.CREATOR_DISPLAY));
    assertEquals(creationDate, solrDocument.getFieldValue(FieldUtils.CREATIONDATE));
    assertEquals(date, solrDocument.get(FieldUtils.DATE).getValue());
}
Also used : Date(java.util.Date) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) BaseObject(com.xpn.xwiki.objects.BaseObject) BlockRenderer(org.xwiki.rendering.renderer.BlockRenderer) SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrReferenceResolver(org.xwiki.search.solr.internal.reference.SolrReferenceResolver) InvocationOnMock(org.mockito.invocation.InvocationOnMock) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)3 SolrReferenceResolver (org.xwiki.search.solr.internal.reference.SolrReferenceResolver)3 SolrQuery (org.apache.solr.client.solrj.SolrQuery)2 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)2 SolrDocumentList (org.apache.solr.common.SolrDocumentList)2 DocumentReference (org.xwiki.model.reference.DocumentReference)2 WikiReference (org.xwiki.model.reference.WikiReference)2 BaseObject (com.xpn.xwiki.objects.BaseObject)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)1 Pair (org.apache.commons.lang3.tuple.Pair)1 SolrInputDocument (org.apache.solr.common.SolrInputDocument)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 BlockRenderer (org.xwiki.rendering.renderer.BlockRenderer)1 WikiPrinter (org.xwiki.rendering.renderer.printer.WikiPrinter)1