Search in sources :

Example 21 with AttachmentReference

use of org.xwiki.model.reference.AttachmentReference in project xwiki-platform by xwiki.

the class UsersClassPropertyValuesProviderTest method getValuesLocal.

@Test
public void getValuesLocal() throws Exception {
    when(this.wikiUserManager.getUserScope(this.classReference.getWikiReference().getName())).thenReturn(UserScope.LOCAL_ONLY);
    DocumentReference aliceReference = new DocumentReference("wiki", "Users", "Alice");
    when(this.allowedValuesQuery.execute()).thenReturn(Collections.singletonList(new Object[] { aliceReference, " Alice One " }));
    when(this.xcontext.getWiki().getDocument(aliceReference, this.xcontext)).thenReturn(mock(XWikiDocument.class, "alice"));
    QueryFilter documentFilter = this.mocker.getInstance(QueryFilter.class, "document");
    QueryFilter viewableFilter = this.mocker.getInstance(QueryFilter.class, "viewable");
    List<QueryFilter> filters = mock(List.class);
    DocumentReference bobReference = new DocumentReference("wiki", "Users", "Bob");
    when(this.usedValuesQuery.getFilters()).thenReturn(filters);
    when(this.usedValuesQuery.execute()).thenReturn(Arrays.asList(new Object[] { bobReference, 17L }, new Object[] { aliceReference, 3L }));
    EntityReferenceSerializer<String> compactSerializer = this.mocker.getInstance(EntityReferenceSerializer.TYPE_STRING, "compact");
    when(compactSerializer.serialize(aliceReference, this.classReference.getWikiReference())).thenReturn("Users.Alice");
    when(compactSerializer.serialize(bobReference, this.classReference.getWikiReference())).thenReturn("Users.Bob");
    when(this.xcontext.getWiki().getURL(aliceReference, this.xcontext)).thenReturn("url/to/alice");
    when(this.xcontext.getWiki().getURL(bobReference, this.xcontext)).thenReturn("url/to/bob");
    XWikiDocument bobProfile = mock(XWikiDocument.class);
    XWikiAttachment bobAvatar = mock(XWikiAttachment.class);
    AttachmentReference bobAvatarReference = new AttachmentReference("somePhoto.png", bobReference);
    when(this.xcontext.getWiki().getDocument(bobReference, this.xcontext)).thenReturn(bobProfile);
    when(bobProfile.getStringValue("avatar")).thenReturn(bobAvatarReference.getName());
    when(bobProfile.getAttachment("somePhoto.png")).thenReturn(bobAvatar);
    when(bobAvatar.isImage(this.xcontext)).thenReturn(true);
    when(bobAvatar.getReference()).thenReturn(bobAvatarReference);
    when(this.xcontext.getWiki().getURL(bobAvatarReference, "download", "width=30&height=30&keepAspectRatio=true", null, this.xcontext)).thenReturn("url/to/bob/avatar");
    PropertyValues values = this.mocker.getComponentUnderTest().getValues(this.propertyReference, 5, "foo");
    assertEquals(2, values.getPropertyValues().size());
    assertEquals("Users.Alice", values.getPropertyValues().get(0).getValue());
    assertEquals("Alice One", values.getPropertyValues().get(0).getMetaData().get("label"));
    assertEquals(3L, values.getPropertyValues().get(0).getMetaData().get("count"));
    assertEquals("url/to/noavatar.png", values.getPropertyValues().get(0).getMetaData().get("icon"));
    assertEquals("url/to/alice", values.getPropertyValues().get(0).getMetaData().get("url"));
    assertEquals("Users.Bob", values.getPropertyValues().get(1).getValue());
    assertEquals("Bob", values.getPropertyValues().get(1).getMetaData().get("label"));
    assertEquals(17L, values.getPropertyValues().get(1).getMetaData().get("count"));
    assertEquals("url/to/bob/avatar", values.getPropertyValues().get(1).getMetaData().get("icon"));
    assertEquals("url/to/bob", values.getPropertyValues().get(1).getMetaData().get("url"));
    verify(this.allowedValuesQuery, never()).setWiki(any(String.class));
    verify(this.allowedValuesQuery, times(1)).execute();
    verify(filters).clear();
    verify(this.usedValuesQuery).addFilter(documentFilter);
    verify(this.usedValuesQuery).addFilter(viewableFilter);
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) QueryFilter(org.xwiki.query.QueryFilter) PropertyValues(org.xwiki.rest.model.jaxb.PropertyValues) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 22 with AttachmentReference

use of org.xwiki.model.reference.AttachmentReference in project xwiki-platform by xwiki.

the class SpacesResourceTest method testAttachments.

@Test
public void testAttachments() throws Exception {
    DocumentReference reference = new DocumentReference(getWiki(), getTestClassName(), getTestMethodName());
    this.testUtils.rest().delete(reference);
    this.testUtils.rest().savePage(reference);
    this.testUtils.rest().attachFile(new AttachmentReference("attachment.txt", reference), new ReaderInputStream(new StringReader("content")), true);
    // Matches Sandbox.WebHome@XWikLogo.png
    GetMethod getMethod = executeGet(String.format("%s", buildURI(SpaceAttachmentsResource.class, getWiki(), Arrays.asList(getTestClassName()))));
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
    Attachments attachments = (Attachments) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    Assert.assertEquals(getAttachmentsInfo(attachments), 1, attachments.getAttachments().size());
    for (Attachment attachment : attachments.getAttachments()) {
        checkLinks(attachment);
    }
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) ReaderInputStream(org.restlet.engine.io.ReaderInputStream) StringReader(java.io.StringReader) GetMethod(org.apache.commons.httpclient.methods.GetMethod) Attachment(org.xwiki.rest.model.jaxb.Attachment) Attachments(org.xwiki.rest.model.jaxb.Attachments) DocumentReference(org.xwiki.model.reference.DocumentReference) AbstractHttpTest(org.xwiki.test.rest.framework.AbstractHttpTest) Test(org.junit.Test)

Example 23 with AttachmentReference

use of org.xwiki.model.reference.AttachmentReference in project xwiki-platform by xwiki.

the class WikisResourceTest method testAttachments.

@Test
public void testAttachments() throws Exception {
    this.testUtils.rest().delete(reference);
    this.testUtils.rest().attachFile(new AttachmentReference(getTestClassName() + ".txt", reference), new ReaderInputStream(new StringReader("attachment content")), true);
    // Verify there are attachments in the whole wiki
    GetMethod getMethod = executeGet(buildURI(WikiAttachmentsResource.class, getWiki()).toString());
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
    Attachments attachments = (Attachments) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    Assert.assertTrue(attachments.getAttachments().size() > 0);
    for (Attachment attachment : attachments.getAttachments()) {
        checkLinks(attachment);
    }
    // Verify we can search for a specific attachment name in the whole wiki
    getMethod = executeGet(String.format("%s?name=" + getTestClassName(), buildURI(WikiAttachmentsResource.class, getWiki())));
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
    attachments = (Attachments) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    Assert.assertEquals(getAttachmentsInfo(attachments), 1, attachments.getAttachments().size());
    for (Attachment attachment : attachments.getAttachments()) {
        checkLinks(attachment);
    }
    // Verify we can search for all attachments in a given space (sandbox)
    // Also verify that a space can be looked up independtly of its case ("sandbox" will match the "Sandbox" space)
    getMethod = executeGet(String.format("%s?space=" + getTestClassName(), buildURI(WikiAttachmentsResource.class, getWiki())));
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
    attachments = (Attachments) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    Assert.assertEquals(getAttachmentsInfo(attachments), 1, attachments.getAttachments().size());
    for (Attachment attachment : attachments.getAttachments()) {
        checkLinks(attachment);
    }
    // Verify we can search for an attachment in a given space (sandbox)
    getMethod = executeGet(String.format("%s?name=" + getTestClassName() + "&space=" + getTestClassName(), buildURI(WikiAttachmentsResource.class, getWiki())));
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
    attachments = (Attachments) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    Assert.assertEquals(getAttachmentsInfo(attachments), 1, attachments.getAttachments().size());
    for (Attachment attachment : attachments.getAttachments()) {
        checkLinks(attachment);
    }
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) ReaderInputStream(org.restlet.engine.io.ReaderInputStream) StringReader(java.io.StringReader) GetMethod(org.apache.commons.httpclient.methods.GetMethod) Attachment(org.xwiki.rest.model.jaxb.Attachment) Attachments(org.xwiki.rest.model.jaxb.Attachments) WikiAttachmentsResource(org.xwiki.rest.resources.wikis.WikiAttachmentsResource) AbstractHttpTest(org.xwiki.test.rest.framework.AbstractHttpTest) Test(org.junit.Test)

Example 24 with AttachmentReference

use of org.xwiki.model.reference.AttachmentReference in project xwiki-platform by xwiki.

the class AttachmentSolrMetadataExtractor method setFieldsInternal.

@Override
public boolean setFieldsInternal(LengthSolrInputDocument solrDocument, EntityReference entityReference) throws Exception {
    AttachmentReference attachmentReference = new AttachmentReference(entityReference);
    XWikiDocument originalDocument = getDocument(attachmentReference.getDocumentReference());
    XWikiAttachment attachment = originalDocument.getAttachment(attachmentReference.getName());
    if (attachment == null) {
        return false;
    }
    XWikiContext xcontext = xcontextProvider.get();
    solrDocument.setField(FieldUtils.FILENAME, attachment.getFilename());
    solrDocument.setField(FieldUtils.FILENAME_SORT, attachment.getFilename());
    solrDocument.setField(FieldUtils.MIME_TYPE, attachment.getMimeType(xcontext));
    solrDocument.setField(FieldUtils.ATTACHMENT_DATE, attachment.getDate());
    // We need to add a dedicated sort field because the corresponding field is multiValued and thus cannot be used
    // for sorting (the reason it is multiValued is because it is 'reused' on document rows and documents can have
    // multiple attachments).
    solrDocument.setField(FieldUtils.ATTACHMENT_DATE_SORT, attachment.getDate());
    solrDocument.setField(FieldUtils.ATTACHMENT_SIZE, attachment.getLongSize());
    solrDocument.setField(FieldUtils.ATTACHMENT_SIZE_SORT, attachment.getLongSize());
    // We need to index the attachment version (revision) to be able to detect when the search index is out of date
    // (not in sync with the database).
    solrDocument.setField(FieldUtils.ATTACHMENT_VERSION, attachment.getVersion());
    // Index the full author reference for exact matching (faceting).
    String authorStringReference = entityReferenceSerializer.serialize(attachment.getAuthorReference());
    solrDocument.setField(FieldUtils.ATTACHMENT_AUTHOR, authorStringReference);
    try {
        // Index the author display name for free text search and results sorting.
        String authorDisplayName = xcontext.getWiki().getPlainUserName(attachment.getAuthorReference(), xcontext);
        solrDocument.setField(FieldUtils.ATTACHMENT_AUTHOR_DISPLAY, authorDisplayName);
        solrDocument.setField(FieldUtils.ATTACHMENT_AUTHOR_DISPLAY_SORT, authorDisplayName);
    } catch (Exception e) {
        this.logger.error("Failed to get author display name for attachment [{}]", attachment.getReference(), e);
    }
    setLocaleAndContentFields(attachment, solrDocument);
    return true;
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment)

Example 25 with AttachmentReference

use of org.xwiki.model.reference.AttachmentReference in project xwiki-platform by xwiki.

the class TestUtils method attachFile.

/**
 * @since 7.2M2
 */
public void attachFile(List<String> spaces, String page, String name, InputStream is, boolean failIfExists) throws Exception {
    AttachmentReference reference = new AttachmentReference(name, new DocumentReference(getCurrentWiki(), spaces, page));
    attachFile(reference, is, failIfExists);
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) DocumentReference(org.xwiki.model.reference.DocumentReference)

Aggregations

AttachmentReference (org.xwiki.model.reference.AttachmentReference)64 DocumentReference (org.xwiki.model.reference.DocumentReference)44 Test (org.junit.Test)31 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)17 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)14 ByteArrayInputStream (java.io.ByteArrayInputStream)10 ResourceReference (org.xwiki.rendering.listener.reference.ResourceReference)9 XWikiContext (com.xpn.xwiki.XWikiContext)8 EntityReference (org.xwiki.model.reference.EntityReference)7 Expectations (org.jmock.Expectations)6 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)4 XWikiException (com.xpn.xwiki.XWikiException)3 InputStream (java.io.InputStream)3 URL (java.net.URL)3 Mockery (org.jmock.Mockery)3 JUnit4Mockery (org.jmock.integration.junit4.JUnit4Mockery)3 XDOMOfficeDocument (org.xwiki.officeimporter.document.XDOMOfficeDocument)3 ImageBlock (org.xwiki.rendering.block.ImageBlock)3