1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.search.solr.internal.metadata

File DocumentSolrMetadataExtractorTest.java

 

Code metrics

0
254
20
1
633
448
23
0.09
12.7
20
1.15

Classes

Class Line # Actions
DocumentSolrMetadataExtractorTest 93 254 0% 23 1
0.9963503599.6%
 

Contributing tests

This file is covered by 13 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.search.solr.internal.metadata;
21   
22    import java.io.ByteArrayInputStream;
23    import java.io.InputStream;
24    import java.util.ArrayList;
25    import java.util.Arrays;
26    import java.util.Collection;
27    import java.util.Collections;
28    import java.util.Date;
29    import java.util.List;
30    import java.util.Locale;
31   
32    import javax.inject.Provider;
33   
34    import org.apache.commons.io.IOUtils;
35    import org.apache.solr.common.SolrInputDocument;
36    import org.hamcrest.MatcherAssert;
37    import org.hamcrest.Matchers;
38    import org.junit.Before;
39    import org.junit.Rule;
40    import org.junit.Test;
41    import org.mockito.AdditionalAnswers;
42    import org.mockito.invocation.InvocationOnMock;
43    import org.mockito.stubbing.Answer;
44    import org.xwiki.bridge.DocumentAccessBridge;
45    import org.xwiki.context.Execution;
46    import org.xwiki.context.ExecutionContext;
47    import org.xwiki.model.EntityType;
48    import org.xwiki.model.reference.AttachmentReference;
49    import org.xwiki.model.reference.DocumentReference;
50    import org.xwiki.model.reference.EntityReference;
51    import org.xwiki.model.reference.EntityReferenceSerializer;
52    import org.xwiki.rendering.block.Block;
53    import org.xwiki.rendering.renderer.BlockRenderer;
54    import org.xwiki.rendering.renderer.printer.WikiPrinter;
55    import org.xwiki.search.solr.internal.api.FieldUtils;
56    import org.xwiki.search.solr.internal.api.SolrFieldNameEncoder;
57    import org.xwiki.search.solr.internal.api.SolrIndexerException;
58    import org.xwiki.search.solr.internal.reference.SolrReferenceResolver;
59    import org.xwiki.test.mockito.MockitoComponentMockingRule;
60   
61    import com.xpn.xwiki.XWiki;
62    import com.xpn.xwiki.XWikiContext;
63    import com.xpn.xwiki.XWikiException;
64    import com.xpn.xwiki.doc.XWikiAttachment;
65    import com.xpn.xwiki.doc.XWikiDocument;
66    import com.xpn.xwiki.objects.BaseObject;
67    import com.xpn.xwiki.objects.BaseProperty;
68    import com.xpn.xwiki.objects.classes.BaseClass;
69    import com.xpn.xwiki.objects.classes.BooleanClass;
70    import com.xpn.xwiki.objects.classes.ListItem;
71    import com.xpn.xwiki.objects.classes.PasswordClass;
72    import com.xpn.xwiki.objects.classes.StaticListClass;
73    import com.xpn.xwiki.objects.classes.StringClass;
74    import com.xpn.xwiki.objects.classes.TextAreaClass;
75   
76    import static org.junit.Assert.assertEquals;
77    import static org.junit.Assert.assertNull;
78    import static org.junit.Assert.assertSame;
79    import static org.junit.Assert.assertTrue;
80    import static org.junit.Assert.fail;
81    import static org.mockito.ArgumentMatchers.any;
82    import static org.mockito.ArgumentMatchers.isNull;
83    import static org.mockito.ArgumentMatchers.same;
84    import static org.mockito.Mockito.doAnswer;
85    import static org.mockito.Mockito.mock;
86    import static org.mockito.Mockito.when;
87   
88    /**
89    * Unit tests for document meta data extraction.
90    *
91    * @version $Id: e2cb188c3c052c3ee48ba63e8307a83151ec1938 $
92    */
 
93    public class DocumentSolrMetadataExtractorTest
94    {
95    @Rule
96    public final MockitoComponentMockingRule<SolrMetadataExtractor> mocker =
97    new MockitoComponentMockingRule<SolrMetadataExtractor>(DocumentSolrMetadataExtractor.class);
98   
99    private XWikiContext xcontext;
100   
101    /**
102    * The document from which we extract the meta data.
103    */
104    private XWikiDocument document;
105   
106    private DocumentReference documentReference =
107    new DocumentReference("wiki", Arrays.asList("Path", "To", "Page"), "WebHome");
108   
 
109  13 toggle @Before
110    public void setUp() throws Exception
111    {
112  13 this.mocker.registerMockComponent(SolrReferenceResolver.class, "document");
113  13 this.xcontext = mock(XWikiContext.class);
114   
115    // XWikiContext Provider
116  13 Provider<XWikiContext> xcontextProvider = this.mocker.registerMockComponent(XWikiContext.TYPE_PROVIDER);
117  13 when(xcontextProvider.get()).thenReturn(this.xcontext);
118   
119    // XWikiContext trough Execution
120  13 ExecutionContext executionContext = new ExecutionContext();
121  13 executionContext.setProperty(XWikiContext.EXECUTIONCONTEXT_KEY, this.xcontext);
122  13 Execution execution = this.mocker.registerMockComponent(Execution.class);
123  13 when(execution.getContext()).thenReturn(executionContext);
124   
125    // XWiki
126  13 XWiki wiki = mock(XWiki.class);
127  13 when(this.xcontext.getWiki()).thenReturn(wiki);
128   
129    // XWikiDocument
130  13 this.document = mock(XWikiDocument.class);
131  13 when(wiki.getDocument(this.documentReference, this.xcontext)).thenReturn(this.document);
132  13 when(this.document.getDocumentReference()).thenReturn(this.documentReference);
133  13 when(this.document.isHidden()).thenReturn(false);
134  13 when(this.document.getLocale()).thenReturn(Locale.ROOT);
135  13 when(this.document.getRealLocale()).thenReturn(Locale.US);
136  13 when(this.document.getTranslatedDocument((Locale) isNull(), same(this.xcontext))).thenReturn(this.document);
137   
138  13 DocumentAccessBridge dab = this.mocker.registerMockComponent(DocumentAccessBridge.class);
139  13 when(dab.getDocument(this.documentReference)).thenReturn(this.document);
140   
141    // Field Name Serializer
142  13 EntityReferenceSerializer<String> fieldNameSerializer =
143    this.mocker.getInstance(EntityReferenceSerializer.TYPE_STRING, "solr");
144  13 when(fieldNameSerializer.serialize(any())).then(new Answer<String>()
145    {
 
146  24 toggle @Override
147    public String answer(InvocationOnMock invocation) throws Throwable
148    {
149  24 EntityReference reference = (EntityReference) invocation.getArguments()[0];
150  24 StringBuilder result = new StringBuilder();
151  24 for (EntityReference parent : reference.getReversedReferenceChain()) {
152  60 result.append('.').append(parent.getName());
153    }
154  24 return result.substring(1);
155    }
156    });
157   
158    // Field Name Encoder
159  13 SolrFieldNameEncoder fieldNameEncoder = this.mocker.getInstance(SolrFieldNameEncoder.class);
160  13 when(fieldNameEncoder.encode(any())).then(AdditionalAnswers.returnsFirstArg());
161    }
162   
 
163  1 toggle @Test
164    public void getSimpleDocument() throws Exception
165    {
166    //
167    // Mock
168    //
169   
170    // ID
171  1 String id = "wiki:Space.Name_" + Locale.ROOT.toString();
172  1 SolrReferenceResolver documentSolrReferenceResolver =
173    this.mocker.getInstance(SolrReferenceResolver.class, "document");
174  1 when(documentSolrReferenceResolver.getId(documentReference)).thenReturn(id);
175   
176    // Full Name
177  1 String fullName = "Space.Name";
178  1 EntityReferenceSerializer<String> localEntityReferenceSerializer =
179    this.mocker.registerMockComponent(EntityReferenceSerializer.TYPE_STRING, "local");
180  1 when(localEntityReferenceSerializer.serialize(this.documentReference)).thenReturn(fullName);
181   
182    // Hierarchy
183  1 when(localEntityReferenceSerializer.serialize(this.documentReference.getParent())).thenReturn("Path.To.Page");
184  1 when(localEntityReferenceSerializer.serialize(this.documentReference.getParent().getParent()))
185    .thenReturn("Path.To");
186  1 when(localEntityReferenceSerializer.serialize(this.documentReference.getParent().getParent().getParent()))
187    .thenReturn("Path");
188   
189    // Creator.
190  1 DocumentReference creatorReference = new DocumentReference("wiki", "Space", "Creator");
191  1 when(this.document.getCreatorReference()).thenReturn(creatorReference);
192   
193  1 String creatorStringReference = "wiki:Space.Creator";
194  1 EntityReferenceSerializer<String> entityReferenceSerializer =
195    this.mocker.registerMockComponent(EntityReferenceSerializer.TYPE_STRING, "default");
196  1 when(entityReferenceSerializer.serialize(creatorReference)).thenReturn(creatorStringReference);
197   
198  1 String creatorDisplayName = "Crea Tor";
199  1 when(this.xcontext.getWiki().getPlainUserName(creatorReference, this.xcontext)).thenReturn(creatorDisplayName);
200   
201    // Author.
202  1 DocumentReference authorReference = new DocumentReference("wiki", "Space", "Author");
203  1 when(this.document.getAuthorReference()).thenReturn(authorReference);
204   
205  1 String authorStringReference = "wiki:Space.Author";
206  1 when(entityReferenceSerializer.serialize(authorReference)).thenReturn(authorStringReference);
207   
208  1 String authorDisplayName = "Au Thor";
209  1 when(this.xcontext.getWiki().getPlainUserName(authorReference, this.xcontext)).thenReturn(authorDisplayName);
210   
211    // Creation Date
212  1 Date creationDate = new Date();
213  1 when(this.document.getCreationDate()).thenReturn(creationDate);
214   
215    // Date
216  1 Date date = new Date();
217  1 when(this.document.getContentUpdateDate()).thenReturn(date);
218   
219    // Version
220  1 String version = "1.1";
221  1 when(this.document.getVersion()).thenReturn(version);
222   
223    // Version summary
224  1 String comment = "1.1 comment";
225  1 when(this.document.getComment()).thenReturn(comment);
226   
227    // XObjects.
228  1 when(this.document.getXObjects()).thenReturn(Collections.<DocumentReference, List<BaseObject>>emptyMap());
229   
230    // Title
231  1 String title = "title";
232  1 when(this.document.getRenderedTitle(any(), same(this.xcontext))).thenReturn(title);
233   
234    // Rendered Content
235  1 final String renderedContent = "rendered content";
236  1 BlockRenderer plainRenderer = this.mocker.registerMockComponent(BlockRenderer.class, "plain/1.0");
237  1 doAnswer(new Answer<Void>()
238    {
 
239  1 toggle @Override
240    public Void answer(InvocationOnMock invocation)
241    {
242  1 Object[] args = invocation.getArguments();
243   
244  1 WikiPrinter printer = (WikiPrinter) args[1];
245  1 printer.print(renderedContent);
246   
247  1 return null;
248    }
249    }).when(plainRenderer).render((Block) any(), any());
250   
251    // Raw Content
252  1 String rawContent = "raw content";
253  1 when(this.document.getContent()).thenReturn(rawContent);
254   
255    //
256    // Call
257    //
258  1 SolrInputDocument solrDocument = this.mocker.getComponentUnderTest().getSolrDocument(this.documentReference);
259   
260    //
261    // Assert and verify
262    //
263  1 assertEquals(id, solrDocument.getFieldValue(FieldUtils.ID));
264   
265  1 assertEquals(this.documentReference.getWikiReference().getName(), solrDocument.getFieldValue(FieldUtils.WIKI));
266  1 assertEquals(Arrays.asList("Path", "To", "Page"), solrDocument.getFieldValues(FieldUtils.SPACES));
267  1 assertEquals(this.documentReference.getName(), solrDocument.getFieldValue(FieldUtils.NAME));
268   
269  1 assertEquals(Arrays.asList("0/Path.", "1/Path.To.", "2/Path.To.Page."),
270    solrDocument.getFieldValues(FieldUtils.SPACE_FACET));
271  1 assertEquals(Arrays.asList("Path", "Path.To", "Path.To.Page"),
272    solrDocument.getFieldValues(FieldUtils.SPACE_PREFIX));
273   
274  1 assertEquals(Locale.US.toString(), solrDocument.getFieldValue(FieldUtils.LOCALE));
275  1 assertEquals(Locale.US.getLanguage(), solrDocument.getFieldValue(FieldUtils.LANGUAGE));
276  1 Collection<?> actualLocales = solrDocument.getFieldValues(FieldUtils.LOCALES);
277    // The order of the locales in the returned collection is nondeterministic.
278  1 assertTrue(
279    actualLocales.size() == 2 && actualLocales.contains("") && actualLocales.contains(Locale.US.toString()));
280  1 assertEquals(this.document.isHidden(), solrDocument.getFieldValue(FieldUtils.HIDDEN));
281  1 assertEquals(EntityType.DOCUMENT.name(), solrDocument.getFieldValue(FieldUtils.TYPE));
282   
283  1 assertEquals(fullName, solrDocument.getFieldValue(FieldUtils.FULLNAME));
284   
285  1 assertEquals(title, solrDocument.getFieldValue(FieldUtils.getFieldName(FieldUtils.TITLE, Locale.US)));
286  1 assertEquals(rawContent,
287    solrDocument.getFieldValue(FieldUtils.getFieldName(FieldUtils.DOCUMENT_RAW_CONTENT, Locale.US)));
288  1 assertEquals(renderedContent,
289    solrDocument.getFieldValue(FieldUtils.getFieldName(FieldUtils.DOCUMENT_RENDERED_CONTENT, Locale.US)));
290   
291  1 assertEquals(version, solrDocument.getFieldValue(FieldUtils.VERSION));
292  1 assertEquals(comment, solrDocument.getFieldValue(FieldUtils.COMMENT));
293   
294  1 assertEquals(authorStringReference, solrDocument.getFieldValue(FieldUtils.AUTHOR));
295  1 assertEquals(authorDisplayName, solrDocument.getFieldValue(FieldUtils.AUTHOR_DISPLAY));
296  1 assertEquals(creatorStringReference, solrDocument.getFieldValue(FieldUtils.CREATOR));
297  1 assertEquals(creatorDisplayName, solrDocument.getFieldValue(FieldUtils.CREATOR_DISPLAY));
298   
299  1 assertEquals(creationDate, solrDocument.getFieldValue(FieldUtils.CREATIONDATE));
300  1 assertEquals(date, solrDocument.get(FieldUtils.DATE).getValue());
301    }
302   
 
303  1 toggle @Test
304    public void getDocumentThrowingException() throws Exception
305    {
306  1 DocumentReference frenchDocumentReference = new DocumentReference(this.documentReference, Locale.FRENCH);
307  1 XWikiException thrown = new XWikiException(XWikiException.MODULE_XWIKI_STORE,
308    XWikiException.ERROR_XWIKI_STORE_HIBERNATE_READING_DOC, "Unreadable document");
309  1 when(this.xcontext.getWiki().getDocument(frenchDocumentReference, this.xcontext)).thenThrow(thrown);
310   
311  1 try {
312  1 this.mocker.getComponentUnderTest().getSolrDocument(frenchDocumentReference);
313  0 fail("An exception was expected.");
314    } catch (SolrIndexerException ex) {
315  1 assertEquals("Failed to get input Solr document for entity '" + frenchDocumentReference + "'",
316    ex.getMessage());
317  1 assertSame(thrown, ex.getCause());
318    }
319    }
320   
 
321  1 toggle @Test
322    public void getDocumentWithObjects() throws Exception
323    {
324    //
325    // Mock
326    //
327  1 BaseObject comment = mock(BaseObject.class);
328  1 List<BaseProperty<EntityReference>> commentFields = new ArrayList<BaseProperty<EntityReference>>();
329   
330  1 String commentContent = "This is a comment";
331  1 BaseProperty<EntityReference> contentField = mock(BaseProperty.class);
332  1 when(contentField.getName()).thenReturn("comment");
333  1 when(contentField.getValue()).thenReturn(commentContent);
334  1 when(contentField.getObject()).thenReturn(comment);
335  1 commentFields.add(contentField);
336   
337  1 String commentSummary = "summary";
338  1 BaseProperty<EntityReference> summaryField = mock(BaseProperty.class);
339  1 when(summaryField.getName()).thenReturn("summary");
340  1 when(summaryField.getValue()).thenReturn(commentSummary);
341  1 when(summaryField.getObject()).thenReturn(comment);
342  1 commentFields.add(summaryField);
343   
344  1 String commentAuthor = "wiki:space.commentAuthor";
345  1 BaseProperty<EntityReference> authorField = mock(BaseProperty.class);
346  1 when(authorField.getName()).thenReturn("author");
347  1 when(authorField.getValue()).thenReturn(commentAuthor);
348  1 when(authorField.getObject()).thenReturn(comment);
349  1 commentFields.add(authorField);
350   
351  1 Date commentDate = new Date();
352  1 BaseProperty<EntityReference> dateField = mock(BaseProperty.class);
353  1 when(dateField.getName()).thenReturn("date");
354  1 when(dateField.getValue()).thenReturn(commentDate);
355  1 when(dateField.getObject()).thenReturn(comment);
356  1 commentFields.add(dateField);
357   
358    // Adding a fake password field to the comments class just to test the branch in the code.
359  1 String commentPassword = "password";
360  1 BaseProperty<EntityReference> passwordField = mock(BaseProperty.class);
361  1 when(passwordField.getName()).thenReturn("password");
362  1 when(passwordField.getValue()).thenReturn(commentPassword);
363  1 commentFields.add(passwordField);
364   
365  1 List<String> commentList = Arrays.asList("a", "list");
366  1 BaseProperty<EntityReference> listField = mock(BaseProperty.class);
367  1 when(listField.getName()).thenReturn("list");
368  1 when(listField.getValue()).thenReturn(commentList);
369  1 when(listField.getObject()).thenReturn(comment);
370  1 commentFields.add(listField);
371   
372  1 Long commentLikes = 13L;
373  1 BaseProperty<EntityReference> numberField = mock(BaseProperty.class);
374  1 when(numberField.getName()).thenReturn("likes");
375  1 when(numberField.getValue()).thenReturn(commentLikes);
376  1 when(numberField.getObject()).thenReturn(comment);
377  1 commentFields.add(numberField);
378   
379  1 BaseProperty<EntityReference> booleanField = mock(BaseProperty.class);
380  1 when(booleanField.getName()).thenReturn("enabled");
381  1 when(booleanField.getValue()).thenReturn(1);
382  1 when(booleanField.getObject()).thenReturn(comment);
383  1 commentFields.add(booleanField);
384   
385  1 DocumentReference commentsClassReference = new DocumentReference("wiki", "space", "commentsClass");
386  1 when(this.document.getXObjects())
387    .thenReturn(Collections.singletonMap(commentsClassReference, Arrays.asList(comment)));
388   
389  1 EntityReferenceSerializer<String> localEntityReferenceSerializer =
390    this.mocker.getInstance(EntityReferenceSerializer.TYPE_STRING, "local");
391  1 when(localEntityReferenceSerializer.serialize(commentsClassReference)).thenReturn("space.commentsClass");
392   
393  1 BaseClass xclass = mock(BaseClass.class);
394  1 when(comment.getXClass(this.xcontext)).thenReturn(xclass);
395  1 when(comment.getFieldList()).thenReturn(commentFields);
396  1 when(comment.getRelativeXClassReference())
397    .thenReturn(commentsClassReference.removeParent(commentsClassReference.getWikiReference()));
398   
399  1 StringClass stringClass = mock(StringClass.class);
400  1 when(stringClass.getClassType()).thenReturn("String");
401   
402  1 when(xclass.get("comment")).thenReturn(mock(TextAreaClass.class));
403  1 when(xclass.get("summary")).thenReturn(stringClass);
404  1 when(xclass.get("password")).thenReturn(mock(PasswordClass.class));
405  1 when(xclass.get("enabled")).thenReturn(mock(BooleanClass.class));
406   
407    //
408    // Call
409    //
410  1 SolrInputDocument solrDocument = this.mocker.getComponentUnderTest().getSolrDocument(this.documentReference);
411   
412    //
413    // Assert and verify
414    //
415  1 assertEquals(Arrays.asList("space.commentsClass"), solrDocument.getFieldValues(FieldUtils.CLASS));
416   
417    // A TextArea property must be indexed as a localized text.
418  1 assertSame(commentContent,
419    solrDocument.getFieldValue(FieldUtils.getFieldName("property.space.commentsClass.comment", Locale.US)));
420  1 assertNull(solrDocument.getFieldValue("property.space.commentsClass.comment_string"));
421   
422    // A String property must be indexed both as localized text and as raw (unanalyzed) text.
423  1 assertSame(commentSummary,
424    solrDocument.getFieldValue(FieldUtils.getFieldName("property.space.commentsClass.summary", Locale.US)));
425  1 assertSame(commentSummary, solrDocument.getFieldValue("property.space.commentsClass.summary_string"));
426   
427  1 assertSame(commentAuthor, solrDocument.getFieldValue("property.space.commentsClass.author_string"));
428  1 assertSame(commentDate, solrDocument.getFieldValue("property.space.commentsClass.date_date"));
429  1 assertEquals(commentList, solrDocument.getFieldValues("property.space.commentsClass.list_string"));
430  1 assertSame(commentLikes, solrDocument.getFieldValue("property.space.commentsClass.likes_long"));
431  1 assertTrue((Boolean) solrDocument.getFieldValue("property.space.commentsClass.enabled_boolean"));
432   
433    // Make sure the password is not indexed (neither as a string nor as a localized text).
434  1 assertNull(solrDocument.getFieldValue("property.space.commentsClass.password_string"));
435  1 assertNull(
436    solrDocument.getFieldValue(FieldUtils.getFieldName("property.space.commentsClass.password", Locale.US)));
437   
438    // Check the sort fields.
439  1 assertSame(commentAuthor, solrDocument.getFieldValue("property.space.commentsClass.author_sortString"));
440  1 assertSame(commentDate, solrDocument.getFieldValue("property.space.commentsClass.date_sortDate"));
441    // The last value is used for sorting because we cannot sort on fields with multiple values.
442  1 assertEquals("list", solrDocument.getFieldValue("property.space.commentsClass.list_sortString"));
443  1 assertSame(commentLikes, solrDocument.getFieldValue("property.space.commentsClass.likes_sortLong"));
444  1 assertTrue((Boolean) solrDocument.getFieldValue("property.space.commentsClass.enabled_sortBoolean"));
445   
446    // Localized texts are sorted as strings if they are not too large.
447  1 assertSame(commentContent, solrDocument.getFieldValue("property.space.commentsClass.comment_sortString"));
448   
449  1 Collection<Object> objectProperties =
450    solrDocument.getFieldValues(FieldUtils.getFieldName("object.space.commentsClass", Locale.US));
451  1 MatcherAssert.assertThat(objectProperties, Matchers.<Object>containsInAnyOrder(commentContent, commentSummary,
452    commentAuthor, commentDate, commentList.get(0), commentList.get(1), commentLikes, true));
453  1 assertEquals(8, objectProperties.size());
454   
455  1 objectProperties = solrDocument.getFieldValues(FieldUtils.getFieldName(FieldUtils.OBJECT_CONTENT, Locale.US));
456  1 MatcherAssert.assertThat(objectProperties,
457    Matchers.<Object>containsInAnyOrder("comment : " + commentContent, "summary : " + commentSummary,
458    "author : " + commentAuthor, "date : " + commentDate, "list : " + commentList.get(0),
459    "list : " + commentList.get(1), "likes : " + commentLikes, "enabled : true"));
460  1 assertEquals(8, objectProperties.size());
461    }
462   
463    /**
464    * @see "XWIKI-9417: Search does not return any results for Static List values"
465    */
 
466  1 toggle @Test
467    public void setStaticListPropertyValue() throws Exception
468    {
469  1 BaseObject xobject = mock(BaseObject.class);
470   
471  1 @SuppressWarnings("unchecked")
472    BaseProperty<EntityReference> listProperty = mock(BaseProperty.class);
473  1 when(listProperty.getName()).thenReturn("color");
474  1 when(listProperty.getValue()).thenReturn(Arrays.asList("red", "green"));
475  1 when(listProperty.getObject()).thenReturn(xobject);
476   
477  1 DocumentReference classReference = new DocumentReference("wiki", "Space", "MyClass");
478  1 when(this.document.getXObjects()).thenReturn(Collections.singletonMap(classReference, Arrays.asList(xobject)));
479   
480  1 BaseClass xclass = mock(BaseClass.class);
481  1 when(xobject.getXClass(this.xcontext)).thenReturn(xclass);
482  1 when(xobject.getFieldList()).thenReturn(Arrays.<Object>asList(listProperty));
483  1 when(xobject.getRelativeXClassReference())
484    .thenReturn(classReference.removeParent(classReference.getWikiReference()));
485   
486  1 StaticListClass staticListClass = mock(StaticListClass.class);
487  1 when(xclass.get("color")).thenReturn(staticListClass);
488  1 when(staticListClass.getMap(xcontext))
489    .thenReturn(Collections.singletonMap("red", new ListItem("red", "Dark Red")));
490   
491  1 SolrInputDocument solrDocument = this.mocker.getComponentUnderTest().getSolrDocument(this.documentReference);
492   
493    // Make sure both the raw value (which is saved in the database) and the display value (specified in the XClass)
494    // are indexed. The raw values are indexed as strings in order to be able to perform exact matches.
495  1 assertEquals(Arrays.asList("red", "green"), solrDocument.getFieldValues("property.Space.MyClass.color_string"));
496  1 assertEquals(Collections.singletonList("Dark Red"),
497    solrDocument.getFieldValues(FieldUtils.getFieldName("property.Space.MyClass.color", Locale.US)));
498   
499    // Check the sort field. Only the last value we set is used for sorting because we cannot sort on fields that
500    // have multiple values.
501  1 assertEquals(Collections.singletonList("green"),
502    solrDocument.getFieldValues("property.Space.MyClass.color_sortString"));
503    }
504   
 
505  2 toggle private XWikiAttachment createMockAttachment(String fileName, String mimeType, Date date, String content,
506    String authorAlias, String authorDisplayName) throws Exception
507    {
508  2 return createMockAttachment(fileName, mimeType, date, content.getBytes(), authorAlias, authorDisplayName);
509    }
510   
 
511  8 toggle private XWikiAttachment createMockAttachment(String fileName, String mimeType, Date date, InputStream content,
512    String authorAlias, String authorDisplayName) throws Exception
513    {
514  8 return createMockAttachment(fileName, mimeType, date, IOUtils.toByteArray(content), authorAlias,
515    authorDisplayName);
516    }
517   
 
518  10 toggle private XWikiAttachment createMockAttachment(String fileName, String mimeType, Date date, byte[] content,
519    String authorAlias, String authorDisplayName) throws Exception
520    {
521  10 XWikiAttachment attachment = mock(XWikiAttachment.class, fileName);
522  10 when(attachment.getReference()).thenReturn(new AttachmentReference(fileName, this.documentReference));
523  10 when(attachment.getFilename()).thenReturn(fileName);
524  10 when(attachment.getMimeType(this.xcontext)).thenReturn(mimeType);
525  10 when(attachment.getDate()).thenReturn(date);
526  10 when(attachment.getFilesize()).thenReturn(content.length);
527  10 when(attachment.getContentInputStream(this.xcontext)).thenReturn(new ByteArrayInputStream(content));
528   
529  10 String authorFullName = "XWiki." + authorAlias;
530  10 DocumentReference authorReference = new DocumentReference("wiki", "XWiki", authorAlias);
531  10 when(attachment.getAuthorReference()).thenReturn(authorReference);
532   
533  10 EntityReferenceSerializer<String> serializer = this.mocker.getInstance(EntityReferenceSerializer.TYPE_STRING);
534  10 String authorStringReference = "wiki:" + authorFullName;
535  10 when(serializer.serialize(authorReference)).thenReturn(authorStringReference);
536   
537  10 when(this.xcontext.getWiki().getPlainUserName(authorReference, this.xcontext)).thenReturn(authorDisplayName);
538   
539  10 return attachment;
540    }
541   
542    // Attachments
543   
 
544  8 toggle private void assertAttachmentExtract(String expect, String filename) throws Exception
545    {
546  8 XWikiAttachment logo;
547  8 try (InputStream stream = getClass().getResourceAsStream("/files/" + filename)) {
548  8 logo = createMockAttachment(filename, "image/png", new Date(), stream, "Alice", "Shy Alice");
549  8 when(this.document.getAttachmentList()).thenReturn(Arrays.<XWikiAttachment>asList(logo));
550    }
551   
552  8 SolrInputDocument solrDocument = this.mocker.getComponentUnderTest().getSolrDocument(this.documentReference);
553   
554  8 assertEquals("Wrong attachment content indexed", expect, solrDocument.getFieldValue("attcontent_en_US"));
555   
556    }
557   
 
558  1 toggle @Test
559    public void getDocumentWithAttachments() throws Exception
560    {
561  1 Date logoDate = new Date(123);
562  1 XWikiAttachment logo = createMockAttachment("logo.png", "image/png", logoDate, "foo", "Alice", "Shy Alice");
563  1 Date todoDate = new Date(456);
564  1 XWikiAttachment todo = createMockAttachment("todo.txt", "text/plain", todoDate, "bar bar", "Bob", "Angry Bob");
565  1 when(this.document.getAttachmentList()).thenReturn(Arrays.<XWikiAttachment>asList(logo, todo));
566   
567  1 SolrInputDocument solrDocument = this.mocker.getComponentUnderTest().getSolrDocument(this.documentReference);
568   
569  1 assertEquals(Arrays.asList("logo.png", "todo.txt"), solrDocument.getFieldValues(FieldUtils.FILENAME));
570  1 assertEquals(Arrays.asList("image/png", "text/plain"), solrDocument.getFieldValues(FieldUtils.MIME_TYPE));
571  1 assertEquals(Arrays.asList(logoDate, todoDate), solrDocument.getFieldValues(FieldUtils.ATTACHMENT_DATE));
572  1 assertEquals(Arrays.asList(3, 7), solrDocument.getFieldValues(FieldUtils.ATTACHMENT_SIZE));
573  1 assertEquals(Arrays.asList("foo\n", "bar bar\n"), solrDocument.getFieldValues("attcontent_en_US"));
574  1 assertEquals(Arrays.asList("wiki:XWiki.Alice", "wiki:XWiki.Bob"),
575    solrDocument.getFieldValues(FieldUtils.ATTACHMENT_AUTHOR));
576  1 assertEquals(Arrays.asList("Shy Alice", "Angry Bob"),
577    solrDocument.getFieldValues(FieldUtils.ATTACHMENT_AUTHOR_DISPLAY));
578    }
579   
 
580  1 toggle @Test
581    public void testAttachmentExtractFromTxt() throws Exception
582    {
583  1 assertAttachmentExtract("text content\n", "txt.txt");
584    }
585   
 
586  1 toggle @Test
587    public void testAttachmentExtractFromMSOffice97() throws Exception
588    {
589  1 assertAttachmentExtract("MS Office 97 content\n\n", "msoffice97.doc");
590   
591    }
592   
 
593  1 toggle @Test
594    public void testAttachmentExtractFromOpenXML() throws Exception
595    {
596  1 assertAttachmentExtract("OpenXML content\n", "openxml.docx");
597    }
598   
 
599  1 toggle @Test
600    public void testAttachmentExtractFromOpenDocument() throws Exception
601    {
602  1 assertAttachmentExtract("OpenDocument content\n", "opendocument.odt");
603    }
604   
 
605  1 toggle @Test
606    public void testAttachmentExtractFromPDF() throws Exception
607    {
608  1 assertAttachmentExtract("\nPDF content\n\n\n", "pdf.pdf");
609    }
610   
 
611  1 toggle @Test
612    public void testAttachmentExtractFromZIP() throws Exception
613    {
614  1 assertAttachmentExtract("\nzip.txt\nzip content\n\n\n\n", "zip.zip");
615    }
616   
 
617  1 toggle @Test
618    public void testAttachmentExtractFromHTML() throws Exception
619    {
620  1 assertAttachmentExtract("something\n", "html.html");
621    }
622   
 
623  1 toggle @Test
624    public void testAttachmentExtractFromClass() throws Exception
625    {
626  1 String expectedContent =
627    "public synchronized class HelloWorld {\n" +
628    " public void HelloWorld();\n" +
629    " public static void main(String[]);\n" +
630    "}\n\n";
631  1 assertAttachmentExtract(expectedContent, "HelloWorld.class");
632    }
633    }