Search in sources :

Example 16 with BinaryReference

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

the class UpdateNodeCommandTest method try_setting_new_binary_into_existing_property.

@Test
public void try_setting_new_binary_into_existing_property() throws Exception {
    final PropertyTree data = new PropertyTree();
    final BinaryReference binaryRef = BinaryReference.from("my-car.jpg");
    data.setBinaryReference("my-image", binaryRef);
    final CreateNodeParams params = CreateNodeParams.create().parent(NodePath.ROOT).name("my-node").data(data).attachBinary(binaryRef, ByteSource.wrap("my-car-image-source".getBytes())).build();
    final Node node = createNode(params);
    final UpdateNodeParams updateNodeParams = UpdateNodeParams.create().editor(toBeEdited -> {
        final PropertyTree nodeData = toBeEdited.data;
        nodeData.addBinaryReferences("my-image", BinaryReference.from("pirated-reference"));
    }).id(node.id()).build();
    assertThrows(NodeBinaryReferenceException.class, () -> updateNode(updateNodeParams));
}
Also used : PropertyTree(com.enonic.xp.data.PropertyTree) Node(com.enonic.xp.node.Node) UpdateNodeParams(com.enonic.xp.node.UpdateNodeParams) BinaryReference(com.enonic.xp.util.BinaryReference) CreateNodeParams(com.enonic.xp.node.CreateNodeParams) Test(org.junit.jupiter.api.Test)

Example 17 with BinaryReference

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

the class UpdateNodeCommandTest method unreferred_binary_attachment_ignored.

@Test
public void unreferred_binary_attachment_ignored() throws Exception {
    final PropertyTree data = new PropertyTree();
    final BinaryReference binaryRef = BinaryReference.from("my-car.jpg");
    data.setBinaryReference("my-image", binaryRef);
    final CreateNodeParams params = CreateNodeParams.create().parent(NodePath.ROOT).name("my-node").data(data).attachBinary(binaryRef, ByteSource.wrap("my-car-image-source".getBytes())).build();
    final Node node = createNode(params);
    final UpdateNodeParams updateNodeParams = UpdateNodeParams.create().editor(toBeEdited -> {
        final PropertyTree nodeData = toBeEdited.data;
        nodeData.addString("newValue", "hepp");
    }).attachBinary(BinaryReference.from("unreferred binary"), ByteSource.wrap("nothing to see here".getBytes())).id(node.id()).build();
    final Node updatedNode = updateNode(updateNodeParams);
    assertEquals(1, updatedNode.getAttachedBinaries().getSize());
}
Also used : BinaryReference(com.enonic.xp.util.BinaryReference) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) BeforeEach(org.junit.jupiter.api.BeforeEach) CreateNodeParams(com.enonic.xp.node.CreateNodeParams) Node(com.enonic.xp.node.Node) NodePath(com.enonic.xp.node.NodePath) StandardCharsets(java.nio.charset.StandardCharsets) UpdateNodeParams(com.enonic.xp.node.UpdateNodeParams) Test(org.junit.jupiter.api.Test) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) ByteStreams(com.google.common.io.ByteStreams) NodeBinaryReferenceException(com.enonic.xp.node.NodeBinaryReferenceException) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ByteSource(com.google.common.io.ByteSource) PropertyTree(com.enonic.xp.data.PropertyTree) PropertyTree(com.enonic.xp.data.PropertyTree) Node(com.enonic.xp.node.Node) UpdateNodeParams(com.enonic.xp.node.UpdateNodeParams) BinaryReference(com.enonic.xp.util.BinaryReference) CreateNodeParams(com.enonic.xp.node.CreateNodeParams) Test(org.junit.jupiter.api.Test)

Example 18 with BinaryReference

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

the class UpdateNodeCommandTest method update_existing_binary.

@Test
public void update_existing_binary() throws Exception {
    final PropertyTree data = new PropertyTree();
    final BinaryReference binaryRef = BinaryReference.from("my-car.jpg");
    data.setBinaryReference("my-image", binaryRef);
    final CreateNodeParams params = CreateNodeParams.create().parent(NodePath.ROOT).name("my-node").data(data).attachBinary(binaryRef, originalBinaryData()).build();
    final Node node = createNode(params);
    // Update the binary source of the binary reference
    final ByteSource updatedBinaryData = ByteSource.wrap("my-car-image-updated-source".getBytes());
    final UpdateNodeParams updateNodeParams = UpdateNodeParams.create().editor(toBeEdited -> {
    }).attachBinary(binaryRef, updatedBinaryData).id(node.id()).build();
    final Node updatedNode = updateNode(updateNodeParams);
    assertEquals(1, updatedNode.getAttachedBinaries().getSize());
    // Verify that the binary source for binary ref in the updated node is the updated version
    final ByteSource binary = GetBinaryCommand.create().nodeId(updatedNode.id()).binaryReference(binaryRef).indexServiceInternal(this.indexServiceInternal).binaryService(this.binaryService).storageService(this.storageService).searchService(this.searchService).build().execute();
    final byte[] bytes = ByteStreams.toByteArray(binary.openStream());
    assertEquals("my-car-image-updated-source", new String(bytes, StandardCharsets.UTF_8));
}
Also used : BinaryReference(com.enonic.xp.util.BinaryReference) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) BeforeEach(org.junit.jupiter.api.BeforeEach) CreateNodeParams(com.enonic.xp.node.CreateNodeParams) Node(com.enonic.xp.node.Node) NodePath(com.enonic.xp.node.NodePath) StandardCharsets(java.nio.charset.StandardCharsets) UpdateNodeParams(com.enonic.xp.node.UpdateNodeParams) Test(org.junit.jupiter.api.Test) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) ByteStreams(com.google.common.io.ByteStreams) NodeBinaryReferenceException(com.enonic.xp.node.NodeBinaryReferenceException) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ByteSource(com.google.common.io.ByteSource) PropertyTree(com.enonic.xp.data.PropertyTree) 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) CreateNodeParams(com.enonic.xp.node.CreateNodeParams) Test(org.junit.jupiter.api.Test)

Example 19 with BinaryReference

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

the class UpdatedAttachedBinariesResolver method processExistingBinaries.

private void processExistingBinaries(final Map<BinaryReference, AttachedBinary> resolved, final Set<BinaryReference> referencesInEditedNode, final Set<BinaryReference> referencesInPersistedNode) {
    final Set<BinaryReference> unchangedReferences = Sets.intersection(referencesInPersistedNode, referencesInEditedNode);
    final AttachedBinaries attachedBinaries = persistedNode.getAttachedBinaries();
    for (final BinaryReference unchangedReference : unchangedReferences) {
        final AttachedBinary existingAttachedBinary = attachedBinaries.getByBinaryReference(unchangedReference);
        if (existingAttachedBinary != null) {
            resolved.put(unchangedReference, existingAttachedBinary);
        }
    }
}
Also used : BinaryReference(com.enonic.xp.util.BinaryReference) AttachedBinaries(com.enonic.xp.node.AttachedBinaries) AttachedBinary(com.enonic.xp.node.AttachedBinary)

Example 20 with BinaryReference

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

the class GetBinaryByVersionCommandTest method testExecute.

@Test
public void testExecute() {
    final PropertyTree data = new PropertyTree();
    final BinaryReference imageRef = BinaryReference.from("myImage");
    data.addBinaryReferences("myBinary", imageRef);
    final Node node = createNode(CreateNodeParams.create().name("my-node").parent(NodePath.ROOT).data(data).attachBinary(imageRef, ByteSource.wrap("thisIsMyImage".getBytes())).build());
    final ByteSource myImage = GetBinaryByVersionCommand.create().nodeId(node.id()).nodeVersionId(node.getNodeVersionId()).binaryReference(imageRef).indexServiceInternal(this.indexServiceInternal).binaryService(this.binaryService).storageService(this.storageService).searchService(this.searchService).build().execute();
    assertNotNull(myImage);
}
Also used : PropertyTree(com.enonic.xp.data.PropertyTree) Node(com.enonic.xp.node.Node) ByteSource(com.google.common.io.ByteSource) BinaryReference(com.enonic.xp.util.BinaryReference) Test(org.junit.jupiter.api.Test)

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