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));
}
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());
}
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));
}
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);
}
}
}
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);
}
Aggregations