Skip to content
Snippets Groups Projects
Commit a814126a authored by Nicola Fulvio Calabria's avatar Nicola Fulvio Calabria
Browse files

Added convenience method to NodeUtils for last vos path element extraction

parent 1506ac2f
No related branches found
No related tags found
No related merge requests found
Pipeline #1780 passed
......@@ -85,6 +85,22 @@ public class NodeUtils {
return sb.toString();
}
public static String getLastPathElement(String path) {
if(path == null || path.isBlank() || !path.startsWith("/"))
{
throw new IllegalArgumentException();
}
String[] pathComponents = path.split("[/]+");
if(pathComponents.length == 0)
{
return "/";
} else {
return pathComponents[pathComponents.length-1];
}
}
public static List<String> subPathComponents(String path) {
List resultList = new ArrayList<String>();
......
......@@ -2,7 +2,6 @@ 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;
......@@ -10,6 +9,7 @@ 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.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
......@@ -163,6 +163,24 @@ public class NodeUtilsTest {
assertTrue(NodeUtils.checkIfReadable(node, "user2", List.of("group2", "group3")));
}
@Test
public void testGetLastPathElement() {
assertThrows(IllegalArgumentException.class,() ->
{ NodeUtils.getLastPathElement(null);} );
assertThrows(IllegalArgumentException.class,() ->
{ NodeUtils.getLastPathElement(" ");} );
assertThrows(IllegalArgumentException.class,() ->
{ NodeUtils.getLastPathElement("http://asdasd");} );
assertEquals("/", NodeUtils.getLastPathElement("/"));
assertEquals("f1", NodeUtils.getLastPathElement("/pippo/test/f1"));
}
@Test
public void testCheckReadableGroupsFalse() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment