Commit 87fbb52d authored by Sara Bertocco's avatar Sara Bertocco
Browse files

Working on refactoring

parent af9345e9
Loading
Loading
Loading
Loading
+21 −3
Original line number Diff line number Diff line
@@ -19,6 +19,8 @@ public class NodeProperties {
    
    private NodeProperties() { }
    
    
    private static final String PROPERTY_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      
    public static final String INITIAL_CREATION_TIME_URI  = "ivo://ivoa.net/vospace/core#btime";       // the initial creation time
    public static final String CONTRIBUTOR_URI  = "ivo://ivoa.net/vospace/core#contributor"; // an entity responsible for making contributions to this resource
@@ -47,19 +49,30 @@ public class NodeProperties {

    

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

        for (Property property : node.getProperties()) {
            if (property.getUri().equals("ivo://ivoa.net/vospace/core#".concat(propertyName))) {
            if (property.getUri().equals(PROPERTY_BASE_URI.concat(propertyName))) {
                return property.getValue();
            }
        }
        return null;
    }
    

    public static String getPropertiesStringByURI(Node node, String uri) {
        
        for (Property property : node.getProperties()) {
            if (uri.equals(property.getUri())) {
                return property.getValue();
            }
        }
        return null;
        
    }
    // Returns all properties stored inside the node under the requested
    // property URI.    
    public static List<String> getNodePropertyByURI(Node node, String propertyURI) {
    public static List<String> getNodePropertiesListByURI(Node node, String propertyURI) {

        List<String> propertyList = node.getProperties().stream()
                .filter((i) -> i.getUri()
@@ -84,4 +97,9 @@ public class NodeProperties {

    }
   

    public static String getPropertyURI(String propertyName) {
        return PROPERTY_BASE_URI.concat(propertyName);
    }
    
}
+80 −7
Original line number Diff line number Diff line
@@ -8,7 +8,10 @@ package it.inaf.oats.vospace.datamodel;
import java.security.Principal;
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.Node;
import net.ivoa.xml.vospace.v2.StructuredDataNode;


public class NodeUtils {
@@ -87,9 +90,25 @@ public class NodeUtils {
    
    public static boolean checkIfWritable(Node myNode, String userName, List<String> userGroups) {
        
        return checkAccessPropery(myNode, userName, userGroups, NodeProperties.GROUP_WRITE_URI);
        
    }
     
    
    public static boolean checkIfRedeable(Node myNode, String userName, List<String> userGroups) {
        
        return checkAccessPropery(myNode, userName, userGroups, NodeProperties.GROUP_READ_URI);
        
    }
    
    
    
    public static boolean checkAccessPropery(Node myNode, String userName, 
                                             List<String> userGroups, String accessPropertyName) {
        
        // First check if parent node creator is == userid
        List<String> nodeOwner
                = NodeProperties.getNodePropertyByURI(myNode, NodeProperties.CREATOR_URI);
                = NodeProperties.getNodePropertiesListByURI(myNode, NodeProperties.CREATOR_URI);
                        
        
        if (nodeOwner == null
@@ -103,18 +122,18 @@ public class NodeUtils {
                return false;
            }

            List<String> groupWritePropValues
                    = NodeProperties.getNodePropertyByURI(myNode,
                            NodeProperties.GROUP_WRITE_URI);
            List<String> groupAccessPropValues
                    = NodeProperties.getNodePropertiesListByURI(myNode,
                            accessPropertyName);

            // If groupwrite property is absent in Parent Node throw exception
            if (groupWritePropValues == null
                    || groupWritePropValues.isEmpty()) {
            if (groupAccessPropValues == null
                    || groupAccessPropValues.isEmpty()) {
                return false;                
            }

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

            if (nodeGroups.isEmpty()
                    || !nodeGroups.stream()
@@ -127,4 +146,58 @@ public class NodeUtils {
        return true;
    }    
        

    public static String getDbNodeType(Node node) {
        if (node instanceof ContainerNode) {
            return "container";
        } else if (node instanceof DataNode) {
            return "data";
        }
        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) {

            DataNode dataNode = (DataNode) myNode;
            return dataNode.isBusy();
        }

        return false;
    }
    
    
    public static Node getTypedNode(String type) {
        Node node;
        switch (type) {
            case "container":
                node = new ContainerNode();
                break;
            case "data":
                node = new DataNode();
                break;
            case "structured":
                node = new StructuredDataNode();
                break;
            default:
                throw new UnsupportedOperationException("Node type " + type + " not supported yet");
        }
        return node;
    }
    
}
+93 −0
Original line number Diff line number Diff line
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.inaf.oats.vospace.datamodel;


import java.util.ArrayList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;


public class NodeUtilsTest {
    
    
    //@Test
    public void getPathFromRequestURLStringTest() {
        
        
        String urlForTest = "http://server.example.com/vospace/";
        String result = NodeUtils.getPathFromRequestURLString(urlForTest);
        assertEquals("/", result);
        
        urlForTest = "http://server.example.com/vospace/nodes";
        result = NodeUtils.getPathFromRequestURLString(urlForTest);
        assertEquals("/", result);
        
        urlForTest = "http://server.example.com/vospace/nodes/mydata";
        result = NodeUtils.getPathFromRequestURLString(urlForTest);
        assertEquals("/mydata", result);
        
        urlForTest = "http://server.example.com/vospace/nodes/mydata1/uffi/nonso/pappa.txt";
        result = NodeUtils.getPathFromRequestURLString(urlForTest);
        assertEquals("/mydata1/uffi/nonso/pappa.txt", result);
    }
     
    /* 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());
        
    }
    */
        
    @Test
    public void subPathComponentsTest2() {
        
        String pathForTest = "/";
        List result = NodeUtils.subPathComponents(pathForTest);
        List 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();
        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();
        expected.add("/mydata1");
        expected.add("/mydata1/uffi");
        expected.add("/mydata1/uffi/nonso");
        expected.add("/mydata1/uffi/nonso/pappa.txt");
        assertArrayEquals(expected.toArray(), result.toArray());
        
    }
    
}