Commit debdda38 authored by Sonia Zorba's avatar Sonia Zorba
Browse files

Bugfix getNodePropertyAsListByURI and added NodeUtils tests

parent 8bc31dac
Loading
Loading
Loading
Loading
Loading
+36 −55
Original line number Diff line number Diff line
package it.inaf.oats.vospace.datamodel;

import java.util.List;
import java.util.stream.Collectors;
import net.ivoa.xml.vospace.v2.Node;
import net.ivoa.xml.vospace.v2.Property;

public abstract class NodeProperties {

    
    private NodeProperties() { }
    private NodeProperties() {
    }

    public static final String BASE_URI = "ivo://ivoa.net/vospace/core#";
    public static final String AVAILABLE_SPACE_URI = "ivo://ivoa.net/vospace/core#availableSpace";      // the amount of space available within a container      
@@ -40,25 +39,10 @@ public abstract class NodeProperties {
    public static final String ASYNC_TRANS_URN = "urn:async_trans";
    public static final String STICKY_URN = "urn:sticky";

    

    
    public static String getStandardNodePropertyByName(Node node, String propertyName) {
        return getNodePropertyByURI(node, BASE_URI.concat(propertyName));
    }


    public static String getProperty(Node node, String propertyName) {

        for (Property property : node.getProperties()) {
            if (property.getUri().equals(BASE_URI.concat(propertyName))) {
                return property.getValue();
            }
        }
        return null;
    }
    

    public static String getNodePropertyByURI(Node node, String uri) {

        for (Property property : node.getProperties()) {
@@ -67,22 +51,21 @@ public abstract class NodeProperties {
            }
        }
        return null;
        
    }

    // Returns all properties stored inside the node under the requested
    // property URI.    
    public static List<String> getNodePropertyAsListByURI(Node node, String propertyURI) {
        String property = getNodePropertyByURI(node, propertyURI);
        return parsePropertyStringToList(property);
    }

        List<String> propertyList = node.getProperties().stream()
                .filter((i) -> i.getUri()
                .equals(propertyURI))
                .map((i) -> i.getValue())
                .collect(Collectors.toList());
    private static List<String> parsePropertyStringToList(String property) {

        return propertyList;
        if (property == null) {
            return List.of();
        }

    public static List<String> parsePropertyStringToList(String property) {
        // If separator changes, this method should remain consistent
        // For now it assumes that " " is the separator        
        String separator = " ";
@@ -93,7 +76,5 @@ public abstract class NodeProperties {
        }

        return List.of(trimmedProperty.split(separator));

    }
    
}
+14 −30
Original line number Diff line number Diff line
@@ -110,13 +110,11 @@ public class NodeUtils {
        }

        return resultList;

    }

    public static boolean checkIfWritable(Node myNode, String userName, List<String> userGroups) {

        return checkAccessPropery(myNode, userName, userGroups, NodeProperties.GROUP_WRITE_URI);

        return checkAccessProperty(myNode, userName, userGroups, NodeProperties.GROUP_WRITE_URI);
    }

    public static boolean checkIfReadable(Node myNode, String userName, List<String> userGroups) {
@@ -125,20 +123,18 @@ public class NodeUtils {
            return true;
        }

        return checkAccessPropery(myNode, userName, userGroups, NodeProperties.GROUP_READ_URI);

        return checkAccessProperty(myNode, userName, userGroups, NodeProperties.GROUP_READ_URI);
    }

    public static boolean checkAccessPropery(Node myNode, String userName,
    private static boolean checkAccessProperty(Node myNode, String userName,
            List<String> userGroups, String accessPropertyName) {

        // First check if parent node creator is == userid
        List<String> nodeOwner
                = NodeProperties.getNodePropertyAsListByURI(myNode, NodeProperties.CREATOR_URI);
        String nodeOwner = NodeProperties.getNodePropertyByURI(myNode, NodeProperties.CREATOR_URI);

        if (nodeOwner == null
                || nodeOwner.isEmpty()
                || !nodeOwner.get(0).equals(userName)) {
                || !nodeOwner.equals(userName)) {
            // Node owner check has failed: let's check if user can write
            // due to group privileges

@@ -157,11 +153,8 @@ public class NodeUtils {
                return false;
            }

            List<String> nodeGroups
                    = NodeProperties.parsePropertyStringToList(groupAccessPropValues.get(0));

            if (nodeGroups.isEmpty()
                    || !nodeGroups.stream()
            if (groupAccessPropValues.isEmpty()
                    || !groupAccessPropValues.stream()
                            .anyMatch((i) -> userGroups.contains(i))) {
                return false;
            }
@@ -171,9 +164,6 @@ public class NodeUtils {
        return true;
    }

        
        

    public static String getDbNodeType(Node node) {
        if (node instanceof ContainerNode) {
            return "container";
@@ -183,20 +173,17 @@ public class NodeUtils {
        throw new UnsupportedOperationException("Unable to retrieve database node type for class " + node.getClass().getCanonicalName());
    }


    public static String getNodeName(String path) {
        String[] parsedPath = path.split("/");

        return parsedPath[parsedPath.length - 1];
    }

    
    public static String getNodeName(Node myNode) {
        String uri = myNode.getUri();
        return getNodeName(uri);
    }


    public static boolean getIsBusy(Node myNode) {

        if (myNode instanceof DataNode) {
@@ -208,7 +195,6 @@ public class NodeUtils {
        return false;
    }

    
    public static Node getTypedNode(String type) {
        Node node;
        switch (type) {
@@ -227,7 +213,6 @@ public class NodeUtils {
        return node;
    }

    
    public static String getVosPath(Node myNode) {

        String nodeUri = myNode.getUri();
@@ -235,5 +220,4 @@ public class NodeUtils {
        return nodeUri.replaceAll("vos://[^/]+", "");
    }


}
+28 −0
Original line number Diff line number Diff line
package it.inaf.oats.vospace.datamodel;

import java.util.List;
import net.ivoa.xml.vospace.v2.DataNode;
import net.ivoa.xml.vospace.v2.Node;
import net.ivoa.xml.vospace.v2.Property;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;

public class NodePropertiesTest {

    @Test
    public void testGetNodePropertyAsListByURI() {

        Property groupRead = new Property();
        groupRead.setUri(NodeProperties.GROUP_READ_URI);
        groupRead.setValue("group1 group2");

        Node node = new DataNode();
        node.getProperties().add(groupRead);
        
        List<String> values = NodeProperties.getNodePropertyAsListByURI(node, NodeProperties.GROUP_READ_URI);

        assertEquals(2, values.size());
        assertEquals("group1", values.get(0));
        assertEquals("group2", values.get(1));
    }
}
+170 −38
Original line number Diff line number Diff line
@@ -2,8 +2,14 @@ package it.inaf.oats.vospace.datamodel;

import java.util.ArrayList;
import java.util.List;
import net.ivoa.xml.vospace.v2.ContainerNode;
import net.ivoa.xml.vospace.v2.DataNode;
import net.ivoa.xml.vospace.v2.LinkNode;
import net.ivoa.xml.vospace.v2.Node;
import net.ivoa.xml.vospace.v2.Property;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;

@@ -60,10 +66,9 @@ public class NodeUtilsTest {
        assertTrue(exception);
    }

    //@Test
    @Test
    public void getPathFromRequestURLStringTest() {

        
        String urlForTest = "http://server.example.com/vospace/";
        String result = NodeUtils.getPathFromRequestURLString(urlForTest);
        assertEquals("/", result);
@@ -81,56 +86,183 @@ public class NodeUtilsTest {
        assertEquals("/mydata1/uffi/nonso/pappa.txt", result);
    }

    /* Is it a possible case?
    // Is it a possible case?
    @Test
    public void subPathComponentsTest1() {
        
        //assertArrayEquals(expected, actual)
        String pathForTest = "";
        List result = NodeUtils.subPathComponents(pathForTest);
        List expected = new ArrayList();     // expected empty
        assertArrayEquals(expected.toArray(), result.toArray());
        
        List<String> result = NodeUtils.subPathComponents(pathForTest);
        assertTrue(result.isEmpty());
    }
    */

    @Test
    public void subPathComponentsTest2() {

        String pathForTest = "/";
        List result = NodeUtils.subPathComponents(pathForTest);
        List expected = new ArrayList();
        List<String> result = NodeUtils.subPathComponents(pathForTest);
        List<String> expected = new ArrayList<>();
        expected.add("/");
        assertArrayEquals(expected.toArray(), result.toArray());
        
                
    }

        
    @Test
    public void subPathComponentsTest3() {

        String pathForTest = "/mynode1";
        List result = NodeUtils.subPathComponents(pathForTest);
        List expected = new ArrayList();
        List<String> result = NodeUtils.subPathComponents(pathForTest);
        List<String> expected = new ArrayList<>();
        expected.add("/mynode1");
        assertArrayEquals(expected.toArray(), result.toArray());
        
    }

        
    @Test
    public void subPathComponentsTest4() {

        //assertArrayEquals(expected, actual)
        String pathForTest = "/mydata1/uffi/nonso/pappa.txt";
        List result = NodeUtils.subPathComponents(pathForTest);
        List expected = new ArrayList();
        List<String> result = NodeUtils.subPathComponents(pathForTest);
        List<String> expected = new ArrayList<>();
        expected.add("/mydata1");
        expected.add("/mydata1/uffi");
        expected.add("/mydata1/uffi/nonso");
        expected.add("/mydata1/uffi/nonso/pappa.txt");
        assertArrayEquals(expected.toArray(), result.toArray());
    }

    @Test
    public void testCheckReadablePublicNode() {

        Property creator = getProperty(NodeProperties.CREATOR_URI, "user1");
        Property publicRead = getProperty(NodeProperties.PUBLIC_READ_URI, "true");

        Node node = new DataNode();
        node.getProperties().add(creator);
        node.getProperties().add(publicRead);

        assertTrue(NodeUtils.checkIfReadable(node, "user3", List.of()));
    }

    @Test
    public void testCheckReadableCreatorOnly() {

        Property creator = getProperty(NodeProperties.CREATOR_URI, "user1");

        Node node = new DataNode();
        node.getProperties().add(creator);

        assertTrue(NodeUtils.checkIfReadable(node, "user1", List.of()));
    }

    @Test
    public void testCheckReadableGroupsTrue() {

        Property creator = getProperty(NodeProperties.CREATOR_URI, "user1");
        Property groupWrite = getProperty(NodeProperties.GROUP_READ_URI, "group3 group4");

        Node node = new DataNode();
        node.getProperties().add(creator);
        node.getProperties().add(groupWrite);

        assertTrue(NodeUtils.checkIfReadable(node, "user2", List.of("group2", "group3")));
    }

    @Test
    public void testCheckReadableGroupsFalse() {

        Property creator = getProperty(NodeProperties.CREATOR_URI, "user1");
        Property groupWrite = getProperty(NodeProperties.GROUP_READ_URI, "group3 group4");

        Node node = new DataNode();
        node.getProperties().add(creator);
        node.getProperties().add(groupWrite);

        assertFalse(NodeUtils.checkIfReadable(node, "user2", List.of("group5", "group6")));
    }

    @Test
    public void testCheckWritableCreatorOnly() {

        Property creator = getProperty(NodeProperties.CREATOR_URI, "user1");

        Node node = new DataNode();
        node.getProperties().add(creator);

        assertTrue(NodeUtils.checkIfWritable(node, "user1", List.of()));
    }

    @Test
    public void testCheckWritableGroupsTrue() {

        Property creator = getProperty(NodeProperties.CREATOR_URI, "user1");
        Property groupWrite = getProperty(NodeProperties.GROUP_WRITE_URI, "group3 group4");

        Node node = new DataNode();
        node.getProperties().add(creator);
        node.getProperties().add(groupWrite);

        assertTrue(NodeUtils.checkIfWritable(node, "user2", List.of("group2", "group3")));
    }

    @Test
    public void testCheckWritableGroupsFalse() {

        Property creator = getProperty(NodeProperties.CREATOR_URI, "user1");
        Property groupWrite = getProperty(NodeProperties.GROUP_WRITE_URI, "group3 group4");

        Node node = new DataNode();
        node.getProperties().add(creator);
        node.getProperties().add(groupWrite);

        assertFalse(NodeUtils.checkIfWritable(node, "user2", List.of("group5", "group6")));
    }

    @Test
    public void testCheckWritableNoGroups() {

        Property creator = getProperty(NodeProperties.CREATOR_URI, "user1");

        Node node = new DataNode();
        node.getProperties().add(creator);

        assertFalse(NodeUtils.checkIfWritable(node, "user2", List.of()));
    }

    @Test
    public void testGetVosPath() {

        Node node = new DataNode();
        node.setUri("vos://example.com!vospace/mynode/child1/child2");

        assertEquals("/mynode/child1/child2", NodeUtils.getVosPath(node));
    }

    @Test
    public void testGetParentPath() {
        assertEquals("/node1/node2", NodeUtils.getParentPath("/node1/node2/node2"));
    }

    @Test
    public void testIsBusy() {
        DataNode node = new DataNode();
        node.setBusy(true);
        assertTrue(NodeUtils.getIsBusy(node));
    }

    @Test
    public void testIsNeverBusy() {
        LinkNode node = new LinkNode();
        assertFalse(NodeUtils.getIsBusy(node));
    }

    @Test
    public void testGetNodeName() {
        Node node = new DataNode();
        node.setUri("vos://example.com!vospace/mynode/child1/child2");
        assertEquals("child2", NodeUtils.getNodeName(node));
    }

    private Property getProperty(String uri, String value) {
        Property property = new Property();
        property.setUri(uri);
        property.setValue(value);
        return property;
    }
}