@Test
  public void getSimpleDocument() throws Exception {
    // Mock

    // No objects (and no comments).
    when(mockDocument.getComments()).thenReturn(new Vector<BaseObject>());
    when(mockDocument.getXObjects()).thenReturn(new HashMap<DocumentReference, List<BaseObject>>());

    // Call

    DocumentSolrMetadataExtractor extractor =
        (DocumentSolrMetadataExtractor) mocker.getComponentUnderTest();
    SolrInputDocument solrDocument = extractor.getSolrDocument(documentReference);

    // Assert and verify

    Assert.assertEquals(
        String.format("%s_%s", documentReferenceString, language),
        solrDocument.getFieldValue(Fields.ID));

    Assert.assertEquals(
        documentReference.getWikiReference().getName(), solrDocument.getFieldValue(Fields.WIKI));
    Assert.assertEquals(
        documentReference.getLastSpaceReference().getName(),
        solrDocument.getFieldValue(Fields.SPACE));
    Assert.assertEquals(documentReference.getName(), solrDocument.getFieldValue(Fields.NAME));

    Assert.assertEquals(language, solrDocument.getFieldValue(Fields.LANGUAGE));
    Assert.assertEquals(hidden, solrDocument.getFieldValue(Fields.HIDDEN));
    Assert.assertEquals(EntityType.DOCUMENT.name(), solrDocument.getFieldValue(Fields.TYPE));

    Assert.assertEquals(documentReferenceLocalString, solrDocument.getFieldValue(Fields.FULLNAME));

    Assert.assertEquals(
        title,
        solrDocument.getFieldValue(
            String.format(Fields.MULTILIGNUAL_FORMAT, Fields.TITLE, language)));
    Assert.assertEquals(
        renderedContent,
        solrDocument.getFieldValue(
            String.format(Fields.MULTILIGNUAL_FORMAT, Fields.DOCUMENT_CONTENT, language)));

    Assert.assertEquals(version, solrDocument.getFieldValue(Fields.VERSION));

    Assert.assertEquals(authorString, solrDocument.getFieldValue(Fields.AUTHOR));
    Assert.assertEquals(authorDisplay, solrDocument.getFieldValue(Fields.AUTHOR_DISPLAY));
    Assert.assertEquals(creatorString, solrDocument.getFieldValue(Fields.CREATOR));
    Assert.assertEquals(creatorDisplay, solrDocument.getFieldValue(Fields.CREATOR_DISPLAY));

    Assert.assertEquals(creationDate, solrDocument.getFieldValue(Fields.CREATIONDATE));
    Assert.assertEquals(date, solrDocument.get(Fields.DATE).getValue());
  }
  /**
   * Adds the document comments using the multiValued field {@link Fields#COMMENT}.
   *
   * @param solrDocument the Solr document where to add the comments.
   * @param originalDocument the XWiki document from which to extract the comments.
   */
  protected void addComments(SolrInputDocument solrDocument, XWikiDocument originalDocument) {
    List<BaseObject> comments = originalDocument.getComments();
    if (comments == null) {
      return;
    }

    for (BaseObject comment : comments) {
      // Yes, objects can be null at this point...
      if (comment != null) {
        String commentString = comment.getStringValue("comment");
        String author = comment.getStringValue("author");
        Date date = comment.getDateValue("date");
        solrDocument.addField(
            Fields.COMMENT, String.format("%s by %s on %s", commentString, author, date));
      }
    }
  }
  @Test
  public void getDocumentWithObjects() throws Exception {
    DocumentReference commentsClassReference =
        new DocumentReference("wiki", "space", "commentsClass");
    String commentContent = "This is a comment";
    String commentAuthor = "wiki:space.commentAuthor";
    Date commentDate = new Date();
    // Adding a fake password field to the comments class just to test the branch in the code.
    String commentPassword = "******";
    List<String> commentList = Arrays.asList("a", "list");

    List<BaseProperty<EntityReference>> commentFields =
        new ArrayList<BaseProperty<EntityReference>>();

    // Mock

    BaseProperty<EntityReference> mockCommentField = mock(BaseProperty.class);
    when(mockCommentField.getName()).thenReturn("comment");
    when(mockCommentField.getValue()).thenReturn(commentContent);
    commentFields.add(mockCommentField);

    BaseProperty<EntityReference> mockAuthorField = mock(BaseProperty.class);
    when(mockAuthorField.getName()).thenReturn("author");
    when(mockAuthorField.getValue()).thenReturn(commentAuthor);
    commentFields.add(mockAuthorField);

    BaseProperty<EntityReference> mockDateField = mock(BaseProperty.class);
    when(mockDateField.getName()).thenReturn("date");
    when(mockDateField.getValue()).thenReturn(commentDate);
    commentFields.add(mockDateField);

    BaseProperty<EntityReference> mockPasswordField = mock(BaseProperty.class);
    when(mockPasswordField.getName()).thenReturn("password");
    when(mockPasswordField.getValue()).thenReturn(commentPassword);
    commentFields.add(mockPasswordField);

    BaseProperty<EntityReference> mockListField = mock(BaseProperty.class);
    when(mockListField.getName()).thenReturn("list");
    when(mockListField.getValue()).thenReturn(commentList);
    commentFields.add(mockListField);

    BaseClass mockXClass = mock(BaseClass.class);

    BaseObject mockComment = mock(BaseObject.class);

    // When handled as a comment
    Vector<BaseObject> comments = new Vector<BaseObject>();
    comments.add(mockComment);
    when(mockDocument.getComments()).thenReturn(comments);

    when(mockComment.getStringValue("comment")).thenReturn(commentContent);
    when(mockComment.getStringValue("author")).thenReturn(commentAuthor);
    when(mockComment.getDateValue("date")).thenReturn(commentDate);

    // When handled as a general object
    HashMap<DocumentReference, List<BaseObject>> xObjects =
        new HashMap<DocumentReference, List<BaseObject>>();
    xObjects.put(commentsClassReference, Arrays.asList(mockComment));
    when(mockDocument.getXObjects()).thenReturn(xObjects);

    when(mockComment.getXClass(mockContext)).thenReturn(mockXClass);
    when(mockComment.getFieldList()).thenReturn(commentFields);

    PropertyClass passwordClass = mock(PasswordClass.class);
    when(mockXClass.get("password")).thenReturn(passwordClass);
    when(passwordClass.getClassType()).thenReturn("Password");

    // Call

    DocumentSolrMetadataExtractor extractor =
        (DocumentSolrMetadataExtractor) mocker.getComponentUnderTest();
    SolrInputDocument solrDocument = extractor.getSolrDocument(documentReference);

    // Assert and verify

    Assert.assertEquals(
        String.format("%s by %s on %s", commentContent, commentAuthor, commentDate),
        solrDocument.getFieldValue(
            String.format(Fields.MULTILIGNUAL_FORMAT, Fields.COMMENT, language)));

    Collection<Object> objectProperties =
        solrDocument.getFieldValues(
            String.format(Fields.MULTILIGNUAL_FORMAT, Fields.OBJECT_CONTENT, language));
    MatcherAssert.assertThat(
        objectProperties,
        Matchers.containsInAnyOrder(
            (Object) ("comment:" + commentContent),
            (Object) ("author:" + commentAuthor),
            (Object) ("date:" + commentDate.toString()),
            (Object) ("list:" + commentList.get(0)),
            (Object) ("list:" + commentList.get(1))));
    Assert.assertEquals(5, objectProperties.size());
  }