Search in sources :

Example 11 with CreateNodeParams

use of com.enonic.xp.node.CreateNodeParams in project xp by enonic.

the class UpdateNodeCommandTest method keep_existing_binaries.

@Test
public void keep_existing_binaries() 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");
    }).id(node.id()).build();
    final Node updatedNode = updateNode(updateNodeParams);
    assertEquals(1, updatedNode.getAttachedBinaries().getSize());
}
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 12 with CreateNodeParams

use of com.enonic.xp.node.CreateNodeParams in project xp by enonic.

the class UpdateNodeCommandTest method add_new_binary.

@Test
public void add_new_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, 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;
        final BinaryReference newBinaryRef = BinaryReference.from("my-other-car.jpg");
        nodeData.addBinaryReferences("new-binary", newBinaryRef);
    }).attachBinary(BinaryReference.from("my-other-car.jpg"), ByteSource.wrap("my-new-binary".getBytes())).id(node.id()).build();
    final Node updatedNode = updateNode(updateNodeParams);
    assertEquals(2, 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 13 with CreateNodeParams

use of com.enonic.xp.node.CreateNodeParams in project xp by enonic.

the class UpdateNodeCommandTest method try_add_new_without_source.

@Test
public void try_add_new_without_source() 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("piratedValue", BinaryReference.from("new-binary-ref"));
    }).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 14 with CreateNodeParams

use of com.enonic.xp.node.CreateNodeParams 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 15 with CreateNodeParams

use of com.enonic.xp.node.CreateNodeParams 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)

Aggregations

CreateNodeParams (com.enonic.xp.node.CreateNodeParams)46 Node (com.enonic.xp.node.Node)32 Test (org.junit.jupiter.api.Test)28 PropertyTree (com.enonic.xp.data.PropertyTree)18 BinaryReference (com.enonic.xp.util.BinaryReference)9 UpdateNodeParams (com.enonic.xp.node.UpdateNodeParams)8 NodeAlreadyExistAtPathException (com.enonic.xp.node.NodeAlreadyExistAtPathException)7 NodeId (com.enonic.xp.node.NodeId)4 AbstractNodeTest (com.enonic.xp.repo.impl.node.AbstractNodeTest)4 AccessControlList (com.enonic.xp.security.acl.AccessControlList)4 ByteSource (com.google.common.io.ByteSource)4 BeforeEach (org.junit.jupiter.api.BeforeEach)4 NodeBinaryReferenceException (com.enonic.xp.node.NodeBinaryReferenceException)3 NodeIdExistsException (com.enonic.xp.node.NodeIdExistsException)3 NodePath (com.enonic.xp.node.NodePath)3 PrincipalAlreadyExistsException (com.enonic.xp.security.PrincipalAlreadyExistsException)3 ByteStreams (com.google.common.io.ByteStreams)3 StandardCharsets (java.nio.charset.StandardCharsets)3 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)3 Assertions.assertThrows (org.junit.jupiter.api.Assertions.assertThrows)3