Search in sources :

Example 21 with BinaryReference

use of com.enonic.xp.util.BinaryReference in project xp by enonic.

the class ApplicationNodeTransformerTest method app_binary_updated.

@Test
public void app_binary_updated() throws Exception {
    final PropertyTree data = new PropertyTree();
    final BinaryReference appReference = BinaryReference.from(ApplicationNodeTransformer.APPLICATION_BINARY_REF);
    data.addBinaryReference(ApplicationNodeTransformer.APPLICATION_BINARY_REF, appReference);
    final Node existingNode = Node.create().id(NodeId.from("myNode")).parentPath(NodePath.ROOT).name("myNode").data(data).attachedBinaries(AttachedBinaries.create().add(new AttachedBinary(appReference, "abc")).build()).build();
    final Application app = Mockito.mock(Application.class);
    Mockito.when(app.getKey()).thenReturn(ApplicationKey.from("myApp"));
    Mockito.when(app.getVersion()).thenReturn(Version.valueOf("1.0.0"));
    Mockito.when(app.getMaxSystemVersion()).thenReturn("1.0.0");
    Mockito.when(app.getMinSystemVersion()).thenReturn("1.0.0.");
    Mockito.when(app.getDisplayName()).thenReturn("displayName");
    final ByteSource updatedSource = ByteSource.wrap(ByteStreams.toByteArray(newBundle("myBundleUpdated", true).build()));
    final UpdateNodeParams updateNodeParams = ApplicationNodeTransformer.toUpdateNodeParams(app, updatedSource, existingNode);
    final BinaryAttachments binaryAttachments = updateNodeParams.getBinaryAttachments();
    assertEquals(updatedSource, binaryAttachments.get(appReference).getByteSource());
}
Also used : BinaryAttachments(com.enonic.xp.node.BinaryAttachments) PropertyTree(com.enonic.xp.data.PropertyTree) Node(com.enonic.xp.node.Node) UpdateNodeParams(com.enonic.xp.node.UpdateNodeParams) ByteSource(com.google.common.io.ByteSource) BinaryReference(com.enonic.xp.util.BinaryReference) Application(com.enonic.xp.app.Application) AttachedBinary(com.enonic.xp.node.AttachedBinary) Test(org.junit.jupiter.api.Test)

Example 22 with BinaryReference

use of com.enonic.xp.util.BinaryReference in project xp by enonic.

the class ContentDataSerializer method extractAttachments.

private void extractAttachments(final PropertySet contentAsSet, final Content.Builder<?> builder) {
    final Attachments attachments = dataToAttachments(contentAsSet.getSets(ATTACHMENT));
    builder.attachments(attachments);
    final Attachment thumbnailAttachment = attachments.byName(AttachmentNames.THUMBNAIL);
    if (thumbnailAttachment != null) {
        final BinaryReference thumbnailBinaryRef = thumbnailAttachment.getBinaryReference();
        final Thumbnail thumbnail = Thumbnail.from(thumbnailBinaryRef, thumbnailAttachment.getMimeType(), thumbnailAttachment.getSize());
        builder.thumbnail(thumbnail);
    }
}
Also used : CreateAttachment(com.enonic.xp.attachment.CreateAttachment) Attachment(com.enonic.xp.attachment.Attachment) BinaryReference(com.enonic.xp.util.BinaryReference) Thumbnail(com.enonic.xp.icon.Thumbnail) CreateAttachments(com.enonic.xp.attachment.CreateAttachments) Attachments(com.enonic.xp.attachment.Attachments)

Example 23 with BinaryReference

use of com.enonic.xp.util.BinaryReference in project xp by enonic.

the class ContentDataSerializer method mergeExistingAndUpdatedAttachments.

private Attachments mergeExistingAndUpdatedAttachments(final Attachments existingAttachments, final UpdateContentTranslatorParams params) {
    CreateAttachments createAttachments = params.getCreateAttachments();
    BinaryReferences removeAttachments = params.getRemoveAttachments();
    if (createAttachments == null && removeAttachments == null && !params.isClearAttachments()) {
        return existingAttachments;
    }
    createAttachments = createAttachments == null ? CreateAttachments.empty() : createAttachments;
    removeAttachments = removeAttachments == null ? BinaryReferences.empty() : removeAttachments;
    final Map<BinaryReference, Attachment> attachments = new LinkedHashMap<>();
    if (!params.isClearAttachments()) {
        existingAttachments.stream().forEach((a) -> attachments.put(a.getBinaryReference(), a));
    }
    removeAttachments.stream().forEach(attachments::remove);
    // added attachments with same BinaryReference will replace existing ones
    for (final CreateAttachment createAttachment : createAttachments) {
        final Attachment attachment = Attachment.create().name(createAttachment.getName()).label(createAttachment.getLabel()).mimeType(createAttachment.getMimeType()).size(attachmentSize(createAttachment)).textContent(createAttachment.getTextContent()).build();
        attachments.put(attachment.getBinaryReference(), attachment);
    }
    return Attachments.from(attachments.values());
}
Also used : CreateAttachments(com.enonic.xp.attachment.CreateAttachments) CreateAttachment(com.enonic.xp.attachment.CreateAttachment) BinaryReferences(com.enonic.xp.util.BinaryReferences) BinaryReference(com.enonic.xp.util.BinaryReference) CreateAttachment(com.enonic.xp.attachment.CreateAttachment) Attachment(com.enonic.xp.attachment.Attachment) LinkedHashMap(java.util.LinkedHashMap)

Example 24 with BinaryReference

use of com.enonic.xp.util.BinaryReference in project xp by enonic.

the class NodeExporter method exportNodeBinaries.

private void exportNodeBinaries(final Node relativeNode, final Path nodeDataFolder) {
    for (final AttachedBinary attachedBinary : relativeNode.getAttachedBinaries()) {
        final BinaryReference reference = attachedBinary.getBinaryReference();
        final ByteSource byteSource = this.nodeService.getBinary(relativeNode.id(), relativeNode.getNodeVersionId(), reference);
        if (!dryRun) {
            this.exportWriter.writeSource(nodeDataFolder.resolve(NodeExportPathResolver.BINARY_FOLDER).resolve(reference.toString()), byteSource);
        }
        result.addBinary(relativeNode.path(), reference);
    }
}
Also used : ByteSource(com.google.common.io.ByteSource) BinaryReference(com.enonic.xp.util.BinaryReference) AttachedBinary(com.enonic.xp.node.AttachedBinary)

Example 25 with BinaryReference

use of com.enonic.xp.util.BinaryReference in project xp by enonic.

the class NodeExportIntegrationTest method single_node_with_binary.

@Test
public void single_node_with_binary() throws Exception {
    final BinaryReference binaryRef = BinaryReference.from("myFile");
    final PropertyTree data = new PropertyTree();
    data.addBinaryReference("myBinary", binaryRef);
    final Node myNode = createNode(CreateNodeParams.create().parent(NodePath.ROOT).name("myNode").data(data).attachBinary(binaryRef, ByteSource.wrap("this is a binary file".getBytes())).build());
    final NodeExportResult result = doExportRoot(false);
    assertEquals(2, result.size());
    assertEquals(1, result.getExportedBinaries().size());
    assertExported(myNode);
    assertBinaryExported(myNode, binaryRef);
}
Also used : NodeExportResult(com.enonic.xp.export.NodeExportResult) PropertyTree(com.enonic.xp.data.PropertyTree) Node(com.enonic.xp.node.Node) BinaryReference(com.enonic.xp.util.BinaryReference) Test(org.junit.jupiter.api.Test) AbstractNodeTest(com.enonic.xp.repo.impl.node.AbstractNodeTest)

Aggregations

BinaryReference (com.enonic.xp.util.BinaryReference)38 Test (org.junit.jupiter.api.Test)26 PropertyTree (com.enonic.xp.data.PropertyTree)25 Node (com.enonic.xp.node.Node)23 ByteSource (com.google.common.io.ByteSource)17 CreateNodeParams (com.enonic.xp.node.CreateNodeParams)13 UpdateNodeParams (com.enonic.xp.node.UpdateNodeParams)12 AbstractNodeTest (com.enonic.xp.repo.impl.node.AbstractNodeTest)8 AttachedBinary (com.enonic.xp.node.AttachedBinary)7 NodePath (com.enonic.xp.node.NodePath)7 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)6 BeforeEach (org.junit.jupiter.api.BeforeEach)6 Attachment (com.enonic.xp.attachment.Attachment)5 BinaryAttachment (com.enonic.xp.node.BinaryAttachment)5 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)5 Assertions.assertThrows (org.junit.jupiter.api.Assertions.assertThrows)4 NodeExportResult (com.enonic.xp.export.NodeExportResult)3 NodeBinaryReferenceException (com.enonic.xp.node.NodeBinaryReferenceException)3 NodeHelper (com.enonic.xp.repo.impl.node.NodeHelper)3 ByteStreams (com.google.common.io.ByteStreams)3