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

Edge case: links in container node + tests

parent 0a0539e3
No related branches found
No related tags found
No related merge requests found
...@@ -7,14 +7,19 @@ package it.inaf.oats.vospace; ...@@ -7,14 +7,19 @@ package it.inaf.oats.vospace;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import it.inaf.ia2.aa.data.User; import it.inaf.ia2.aa.data.User;
import it.inaf.oats.vospace.datamodel.NodeUtils;
import it.inaf.oats.vospace.datamodel.Views; import it.inaf.oats.vospace.datamodel.Views;
import it.inaf.oats.vospace.exception.InvalidArgumentException; import it.inaf.oats.vospace.exception.InvalidArgumentException;
import it.inaf.oats.vospace.exception.NodeNotFoundException;
import it.inaf.oats.vospace.parent.exchange.ArchiveEntryDescriptor; import it.inaf.oats.vospace.parent.exchange.ArchiveEntryDescriptor;
import it.inaf.oats.vospace.persistence.NodeDAO;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import net.ivoa.xml.vospace.v2.ContainerNode;
import net.ivoa.xml.vospace.v2.Node;
import net.ivoa.xml.vospace.v2.Transfer; import net.ivoa.xml.vospace.v2.Transfer;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
...@@ -45,6 +50,9 @@ public class FileServiceClient { ...@@ -45,6 +50,9 @@ public class FileServiceClient {
@Autowired @Autowired
private LinkService linkService; private LinkService linkService;
@Autowired
private NodeDAO nodeDAO;
public String startArchiveJob(Transfer transfer, String jobId) { public String startArchiveJob(Transfer transfer, String jobId) {
String target = transfer.getTarget().substring("vos://".length() + authority.length()); String target = transfer.getTarget().substring("vos://".length() + authority.length());
...@@ -70,10 +78,32 @@ public class FileServiceClient { ...@@ -70,10 +78,32 @@ public class FileServiceClient {
vosPaths.add(target); vosPaths.add(target);
} }
// OK generate descriptors // Generate descriptors
// Expand container nodes into direct children list
List<String> expandedVosPaths = new ArrayList<>();
for (String vosPath : vosPaths) {
Node node
= nodeDAO.listNode(vosPath)
.orElseThrow(() -> {
throw new NodeNotFoundException(vosPath);
});
if (node instanceof ContainerNode) {
List<Node> nodes = ((ContainerNode) node).getNodes();
if (nodes.isEmpty()) {
expandedVosPaths.add(NodeUtils.getVosPath(node));
} else {
expandedVosPaths.addAll(nodes
.stream().map(n -> NodeUtils.getVosPath(n))
.collect(Collectors.toList()));
}
}
}
// follow links to links in vosPaths // follow links to links in vosPaths
List<ArchiveEntryDescriptor> entryDescriptors = linkService.followLinksForArchiveService(vosPaths); List<ArchiveEntryDescriptor> entryDescriptors = linkService.followLinksForArchiveService(expandedVosPaths);
ArchiveRequest archiveRequest = new ArchiveRequest(); ArchiveRequest archiveRequest = new ArchiveRequest();
archiveRequest.setJobId(jobId); archiveRequest.setJobId(jobId);
......
...@@ -11,12 +11,16 @@ import it.inaf.oats.vospace.FileServiceClient.ArchiveRequest; ...@@ -11,12 +11,16 @@ import it.inaf.oats.vospace.FileServiceClient.ArchiveRequest;
import it.inaf.oats.vospace.datamodel.Views; import it.inaf.oats.vospace.datamodel.Views;
import it.inaf.oats.vospace.exception.InvalidArgumentException; import it.inaf.oats.vospace.exception.InvalidArgumentException;
import it.inaf.oats.vospace.parent.exchange.ArchiveEntryDescriptor; import it.inaf.oats.vospace.parent.exchange.ArchiveEntryDescriptor;
import it.inaf.oats.vospace.persistence.NodeDAO;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.UncheckedIOException; import java.io.UncheckedIOException;
import java.net.URI; import java.net.URI;
import java.util.List; import java.util.List;
import java.util.Optional;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import net.ivoa.xml.vospace.v2.ContainerNode;
import net.ivoa.xml.vospace.v2.DataNode;
import net.ivoa.xml.vospace.v2.Param; import net.ivoa.xml.vospace.v2.Param;
import net.ivoa.xml.vospace.v2.Transfer; import net.ivoa.xml.vospace.v2.Transfer;
import net.ivoa.xml.vospace.v2.View; import net.ivoa.xml.vospace.v2.View;
...@@ -30,6 +34,7 @@ import static org.mockito.ArgumentMatchers.any; ...@@ -30,6 +34,7 @@ import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
import org.mockito.InjectMocks; import org.mockito.InjectMocks;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.Mockito;
import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
...@@ -60,6 +65,9 @@ public class FileServiceClientTest { ...@@ -60,6 +65,9 @@ public class FileServiceClientTest {
@Mock @Mock
private LinkService linkService; private LinkService linkService;
@Mock
private NodeDAO nodeDAO;
@InjectMocks @InjectMocks
private FileServiceClient fileServiceClient; private FileServiceClient fileServiceClient;
...@@ -98,6 +106,11 @@ public class FileServiceClientTest { ...@@ -98,6 +106,11 @@ public class FileServiceClientTest {
view.setUri(Views.ZIP_VIEW_URI); view.setUri(Views.ZIP_VIEW_URI);
transfer.setView(view); transfer.setView(view);
ContainerNode node = Mockito.mock(ContainerNode.class);
when(node.getNodes()).thenReturn(List.of());
when(node.getUri()).thenReturn("vos://example.com!vospace/mydir");
when(nodeDAO.listNode(eq("/mydir"))).thenReturn(Optional.of(node));
when(linkService.followLinksForArchiveService(any())) when(linkService.followLinksForArchiveService(any()))
.thenReturn(List.of(new ArchiveEntryDescriptor("/mydir"))); .thenReturn(List.of(new ArchiveEntryDescriptor("/mydir")));
...@@ -162,6 +175,14 @@ public class FileServiceClientTest { ...@@ -162,6 +175,14 @@ public class FileServiceClientTest {
param2.setValue("file2"); param2.setValue("file2");
view.getParam().add(param2); view.getParam().add(param2);
DataNode node1 = Mockito.mock(DataNode.class);
when(node1.getUri()).thenReturn("vos://example.com!vospace/parent_dir/file1");
when(nodeDAO.listNode(eq("/parent_dir/file1"))).thenReturn(Optional.of(node1));
DataNode node2 = Mockito.mock(DataNode.class);
when(node2.getUri()).thenReturn("vos://example.com!vospace/parent_dir/file2");
when(nodeDAO.listNode(eq("/parent_dir/file2"))).thenReturn(Optional.of(node2));
when(linkService.followLinksForArchiveService(any())).thenReturn( when(linkService.followLinksForArchiveService(any())).thenReturn(
List.of(new ArchiveEntryDescriptor("/parent_dir/file1"), List.of(new ArchiveEntryDescriptor("/parent_dir/file1"),
new ArchiveEntryDescriptor("/parent_dir/file2") new ArchiveEntryDescriptor("/parent_dir/file2")
......
/*
* This file is part of vospace-rest
* Copyright (C) 2021 Istituto Nazionale di Astrofisica
* SPDX-License-Identifier: GPL-3.0-or-later
*/
package it.inaf.oats.vospace;
import it.inaf.ia2.aa.data.User;
import it.inaf.oats.vospace.exception.NodeBusyException;
import it.inaf.oats.vospace.exception.NodeNotFoundException;
import it.inaf.oats.vospace.exception.PermissionDeniedException;
import it.inaf.oats.vospace.parent.exchange.ArchiveEntryDescriptor;
import it.inaf.oats.vospace.persistence.DataSourceConfigSingleton;
import it.inaf.oats.vospace.persistence.NodeDAO;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import net.ivoa.xml.vospace.v2.Transfer;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.ContextConfiguration;
@SpringBootTest
//@AutoConfigureMockMvc
@TestPropertySource(locations = "classpath:test.properties", properties = {"vospace-authority=example.com!vospace", "file-service-url=http://file-service"})
@ContextConfiguration(classes = {DataSourceConfigSingleton.class})
public class LinkServiceTest {
@Autowired
private LinkService linkService;
@Test
public void testFollowLinksForArchiveService() {
List<String> vosPaths = List.of("/test3/m1/link2", "/test3/m1/m2");
List<ArchiveEntryDescriptor> aed = linkService.followLinksForArchiveService(vosPaths);
assertEquals(2, aed.size());
List<String> targetVosPaths = aed.stream().map(a -> a.getTargetNodeVosPath())
.collect(Collectors.toList());
System.out.println(targetVosPaths);
assertTrue(targetVosPaths.containsAll(List.of("/test3/m1/m2", "/test1/f1/f2_renamed/f3")));
}
}
...@@ -43,6 +43,7 @@ INSERT INTO node (parent_path, parent_relative_path, name, type, creator_id, gro ...@@ -43,6 +43,7 @@ INSERT INTO node (parent_path, parent_relative_path, name, type, creator_id, gro
INSERT INTO node (parent_path, parent_relative_path, name, type, creator_id, group_write, group_read, is_public, location_id) VALUES ('19', '', 'destination2', 'container', 'user3', '{"group1"}', '{"group1"}', false, 3); -- /mycontainer/destination2 INSERT INTO node (parent_path, parent_relative_path, name, type, creator_id, group_write, group_read, is_public, location_id) VALUES ('19', '', 'destination2', 'container', 'user3', '{"group1"}', '{"group1"}', false, 3); -- /mycontainer/destination2
INSERT INTO node (parent_path, parent_relative_path, name, type, creator_id, group_write, group_read, is_public, location_id) VALUES ('19.21', '20', 'control', 'container', 'user3', '{"group1"}', '{"group1"}', false, 3); -- /mycontainer/destination2/control INSERT INTO node (parent_path, parent_relative_path, name, type, creator_id, group_write, group_read, is_public, location_id) VALUES ('19.21', '20', 'control', 'container', 'user3', '{"group1"}', '{"group1"}', false, 3); -- /mycontainer/destination2/control
INSERT INTO node (parent_path, parent_relative_path, name, type, creator_id, group_write, group_read, is_public, location_id, target) VALUES ('9.10', '', 'link2', 'link', 'user3', '{"group1"}', '{"group1"}', false, 3, 'vos://example.com!vospace/test1/f1/f2_renamed/f3'); -- /test3/m1/link2
DELETE FROM job; DELETE FROM job;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment