Newer
Older
/*
* This file is part of vospace-rest
* Copyright (C) 2021 Istituto Nazionale di Astrofisica
* SPDX-License-Identifier: GPL-3.0-or-later
*/
import it.inaf.ia2.aa.ServletRapClient;
import it.inaf.ia2.aa.data.User;
import it.inaf.oats.vospace.datamodel.NodeProperties;
import it.inaf.oats.vospace.datamodel.Views;
Sonia Zorba
committed
import it.inaf.oats.vospace.exception.InvalidArgumentException;
Nicola Fulvio Calabria
committed
import it.inaf.oats.vospace.exception.NodeBusyException;
import it.inaf.oats.vospace.exception.PermissionDeniedException;
Sonia Zorba
committed
import it.inaf.oats.vospace.exception.ProtocolNotSupportedException;
import it.inaf.oats.vospace.persistence.LocationDAO;
import it.inaf.oats.vospace.persistence.NodeDAO;
import it.inaf.oats.vospace.persistence.model.Location;
import it.inaf.oats.vospace.persistence.model.LocationType;
import javax.servlet.http.HttpServletRequest;
Nicola Fulvio Calabria
committed
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.Property;
Sonia Zorba
committed
import net.ivoa.xml.vospace.v2.Protocol;
import net.ivoa.xml.vospace.v2.View;
import static org.junit.jupiter.api.Assertions.assertEquals;
Nicola Fulvio Calabria
committed
import static org.junit.jupiter.api.Assertions.assertThrows;
Sonia Zorba
committed
import static org.junit.jupiter.api.Assertions.assertTrue;
Sonia Zorba
committed
import static org.junit.jupiter.api.Assertions.fail;
import org.junit.jupiter.api.BeforeEach;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
Nicola Fulvio Calabria
committed
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.test.context.TestPropertySource;
@SpringBootTest
@AutoConfigureMockMvc
@TestPropertySource(properties = {"vospace-authority=example.com!vospace", "file-service-url=http://file-service"})
public class UriServiceTest {
@MockBean
@MockBean
private LocationDAO locationDAO;
Sonia Zorba
committed
@MockBean
private ServletRapClient rapClient;
Sonia Zorba
committed
Nicola Fulvio Calabria
committed
@MockBean
private CreateNodeService createNodeService;
@MockBean
private FileServiceClient fileServiceClient;
@Autowired
private HttpServletRequest servletRequest;
private UriService uriService;
@TestConfiguration
public static class TestConfig {
/**
* Necessary because MockBean doesn't work with HttpServletRequest.
*/
@Bean
@Primary
public HttpServletRequest servletRequest() {
Sonia Zorba
committed
HttpServletRequest request = mock(HttpServletRequest.class);
User user = new User().setUserId("anonymous");
when(request.getUserPrincipal()).thenReturn(user);
return request;
Sonia Zorba
committed
@BeforeEach
public void init() {
Location location = new Location();
location.setType(LocationType.ASYNC);
when(locationDAO.getNodeLocation(any())).thenReturn(Optional.of(location));
}
@Test
public void testPublicUrl() {
Node node = new DataNode();
Property property = new Property();
property.setUri(NodeProperties.PUBLIC_READ_URI);
property.setValue("true");
node.getProperties().add(property);
when(nodeDAO.listNode(eq("/mydata1"))).thenReturn(Optional.of(node));
JobSummary job = getJob();
Sonia Zorba
committed
Transfer negotiatedTransfer = uriService.getNegotiatedTransfer(job, uriService.getTransfer(job));
Sonia Zorba
committed
assertEquals("http://file-service/mydata1?jobId=job-id", negotiatedTransfer.getProtocols().get(0).getEndpoint());
public void testPrivateUrl() {
Nicola Fulvio Calabria
committed
Property creator = new Property();
creator.setUri(NodeProperties.CREATOR_URI);
creator.setValue("user1");
node.getProperties().add(creator);
Sonia Zorba
committed
Nicola Fulvio Calabria
committed
Property readgroup = new Property();
readgroup.setUri(NodeProperties.GROUP_READ_URI);
readgroup.setValue("group1");
node.getProperties().add(readgroup);
when(nodeDAO.listNode(eq("/mydata1"))).thenReturn(Optional.of(node));
User user = mock(User.class);
when(user.getAccessToken()).thenReturn("<token>");
Nicola Fulvio Calabria
committed
when(user.getName()).thenReturn("user1");
when(servletRequest.getUserPrincipal()).thenReturn(user);
when(rapClient.exchangeToken(argThat(req -> {
assertEquals("<token>", req.getSubjectToken());
assertEquals("http://file-service/mydata1", req.getResource());
return true;
}), any())).thenReturn("<new-token>");
JobSummary job = getJob();
Nicola Fulvio Calabria
committed
Transfer tr = uriService.getTransfer(job);
Sonia Zorba
committed
Transfer negotiatedTransfer = uriService.getNegotiatedTransfer(job, tr);
Sonia Zorba
committed
assertEquals("http://file-service/mydata1?jobId=job-id&token=<new-token>", negotiatedTransfer.getProtocols().get(0).getEndpoint());
Nicola Fulvio Calabria
committed
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
@Test
public void testPrivateUrlPermissionDenied() {
Node node = new DataNode();
Property creator = new Property();
creator.setUri(NodeProperties.CREATOR_URI);
creator.setValue("user3");
node.getProperties().add(creator);
Property readgroup = new Property();
readgroup.setUri(NodeProperties.GROUP_READ_URI);
readgroup.setValue("group1000");
node.getProperties().add(readgroup);
when(nodeDAO.listNode(eq("/mydata1"))).thenReturn(Optional.of(node));
User user = mock(User.class);
when(user.getAccessToken()).thenReturn("<token>");
when(user.getName()).thenReturn("user1");
when(servletRequest.getUserPrincipal()).thenReturn(user);
when(rapClient.exchangeToken(argThat(req -> {
assertEquals("<token>", req.getSubjectToken());
assertEquals("http://file-service/mydata1", req.getResource());
return true;
}), any())).thenReturn("<new-token>");
JobSummary job = getJob();
Transfer tr = uriService.getTransfer(job);
Sonia Zorba
committed
assertThrows(PermissionDeniedException.class, () -> {
uriService.getNegotiatedTransfer(job, tr);
});
Nicola Fulvio Calabria
committed
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
}
@Test
public void testPrivateUrlNodeBusy() {
DataNode node = new DataNode();
Property creator = new Property();
creator.setUri(NodeProperties.CREATOR_URI);
creator.setValue("user1");
node.getProperties().add(creator);
Property readgroup = new Property();
readgroup.setUri(NodeProperties.GROUP_READ_URI);
readgroup.setValue("group1");
node.getProperties().add(readgroup);
node.setBusy(Boolean.TRUE);
when(nodeDAO.listNode(eq("/mydata1"))).thenReturn(Optional.of(node));
User user = mock(User.class);
when(user.getAccessToken()).thenReturn("<token>");
when(user.getName()).thenReturn("user1");
when(servletRequest.getUserPrincipal()).thenReturn(user);
when(rapClient.exchangeToken(argThat(req -> {
assertEquals("<token>", req.getSubjectToken());
assertEquals("http://file-service/mydata1", req.getResource());
return true;
}), any())).thenReturn("<new-token>");
JobSummary job = getJob();
Transfer tr = uriService.getTransfer(job);
Sonia Zorba
committed
assertThrows(NodeBusyException.class, () -> {
uriService.getNegotiatedTransfer(job, tr);
});
Nicola Fulvio Calabria
committed
}
Sonia Zorba
committed
Nicola Fulvio Calabria
committed
@Test
public void pushToNonexistentNode() {
Sonia Zorba
committed
Nicola Fulvio Calabria
committed
ContainerNode node = new ContainerNode();
node.setUri("vos://example.com!vospace/mydata1");
Property creator = new Property();
creator.setUri(NodeProperties.CREATOR_URI);
creator.setValue("user1");
node.getProperties().add(creator);
Sonia Zorba
committed
Nicola Fulvio Calabria
committed
Property readgroup = new Property();
readgroup.setUri(NodeProperties.GROUP_READ_URI);
readgroup.setValue("group1");
node.getProperties().add(readgroup);
Sonia Zorba
committed
Nicola Fulvio Calabria
committed
DataNode dnode = new DataNode();
dnode.setUri("vos://example.com!vospace/mydata1/mydata2");
dnode.getProperties().add(creator);
dnode.getProperties().add(readgroup);
Sonia Zorba
committed
Nicola Fulvio Calabria
committed
Property writegroup = new Property();
writegroup.setUri(NodeProperties.GROUP_WRITE_URI);
writegroup.setValue("group1");
dnode.getProperties().add(writegroup);
Sonia Zorba
committed
Nicola Fulvio Calabria
committed
when(nodeDAO.listNode(eq("/mydata1"))).thenReturn(Optional.of(node));
when(nodeDAO.listNode(eq("/mydata1/mydata2"))).thenReturn(Optional.empty());
Sonia Zorba
committed
Nicola Fulvio Calabria
committed
User user = mock(User.class);
when(user.getAccessToken()).thenReturn("<token>");
when(user.getName()).thenReturn("user1");
Sonia Zorba
committed
Nicola Fulvio Calabria
committed
when(servletRequest.getUserPrincipal()).thenReturn(user);
when(rapClient.exchangeToken(argThat(req -> {
assertEquals("<token>", req.getSubjectToken());
assertEquals("http://file-service/mydata1/mydata2", req.getResource());
return true;
}), any())).thenReturn("<new-token>");
JobSummary job = getPushToVoSpaceJob();
Transfer tr = uriService.getTransfer(job);
Sonia Zorba
committed
when(createNodeService.createNode(any(), any(), eq(user))).thenReturn(dnode);
Sonia Zorba
committed
Sonia Zorba
committed
Transfer negotiatedTransfer = uriService.getNegotiatedTransfer(job, tr);
Sonia Zorba
committed
verify(createNodeService, times(1)).createNode(any(), any(), eq(user));
Sonia Zorba
committed
Sonia Zorba
committed
assertEquals("http://file-service/mydata1/mydata2?jobId=job-id2&token=<new-token>", negotiatedTransfer.getProtocols().get(0).getEndpoint());
Nicola Fulvio Calabria
committed
}
@Test
public void setNodeRemoteLocationTest() {
String nodeUri = "vos://example.com!vospace/test/f1/lbtfile.fits";
String contentUri = "http://archive.lbto.org/files/lbtfile.fits";
Location location = new Location();
location.setId(5);
when(locationDAO.findPortalLocation(eq("archive.lbto.org"))).thenReturn(Optional.of(location));
uriService.setNodeRemoteLocation(nodeUri, contentUri);
verify(nodeDAO).setNodeLocation(eq("/test/f1/lbtfile.fits"), eq(5), eq("lbtfile.fits"));
}
Sonia Zorba
committed
@Test
public void testSetSyncTransferEndpointsPullFromVoSpace() {
mockPublicNode();
Transfer transfer = new Transfer();
transfer.setTarget(Arrays.asList("vos://example.com!vospace/mydata1"));
Sonia Zorba
committed
transfer.setDirection("pullFromVoSpace");
Protocol protocol1 = new Protocol();
protocol1.setUri("ivo://ivoa.net/vospace/core#httpget");
transfer.getProtocols().add(protocol1);
Protocol protocol2 = new Protocol();
protocol2.setUri("invalid_protocol");
transfer.getProtocols().add(protocol2);
JobSummary job = new JobSummary();
JobSummary.JobInfo jobInfo = new JobSummary.JobInfo();
jobInfo.getAny().add(transfer);
job.setJobInfo(jobInfo);
assertEquals(2, transfer.getProtocols().size());
Sonia Zorba
committed
Transfer negotiatedTransfer = uriService.getNegotiatedTransfer(job, transfer);
Sonia Zorba
committed
// invalid protocol is removed
Sonia Zorba
committed
assertEquals(1, negotiatedTransfer.getProtocols().size());
assertEquals("ivo://ivoa.net/vospace/core#httpget", negotiatedTransfer.getProtocols().get(0).getUri());
Sonia Zorba
committed
}
@Test
public void testSetSyncTransferEndpointsPushToVoSpace() {
Node node = mockPublicNode();
Property creator = new Property();
creator.setUri(NodeProperties.CREATOR_URI);
creator.setValue("user1");
node.getProperties().add(creator);
User user = mock(User.class);
when(user.getName()).thenReturn("user1");
when(servletRequest.getUserPrincipal()).thenReturn(user);
Transfer transfer = new Transfer();
transfer.setTarget(Arrays.asList("vos://example.com!vospace/mydata1"));
Sonia Zorba
committed
transfer.setDirection("pushToVoSpace");
Protocol protocol1 = new Protocol();
protocol1.setUri("ivo://ivoa.net/vospace/core#httpput");
transfer.getProtocols().add(protocol1);
Protocol protocol2 = new Protocol();
protocol2.setUri("invalid_protocol");
transfer.getProtocols().add(protocol2);
JobSummary job = new JobSummary();
JobSummary.JobInfo jobInfo = new JobSummary.JobInfo();
jobInfo.getAny().add(transfer);
job.setJobInfo(jobInfo);
assertEquals(2, transfer.getProtocols().size());
Sonia Zorba
committed
Transfer negotiatedTransfer = uriService.getNegotiatedTransfer(job, transfer);
Sonia Zorba
committed
// invalid protocol is removed
Sonia Zorba
committed
assertEquals(1, negotiatedTransfer.getProtocols().size());
assertEquals("ivo://ivoa.net/vospace/core#httpput", negotiatedTransfer.getProtocols().get(0).getUri());
Sonia Zorba
committed
}
@Test
public void testSetSyncTransferEndpointsUnsupportedProtocol() {
Transfer transfer = new Transfer();
transfer.setTarget(Arrays.asList("vos://example.com!vospace/mydata1"));
Sonia Zorba
committed
transfer.setDirection("pullFromVoSpace");
Protocol protocol = new Protocol();
protocol.setUri("invalid_protocol");
transfer.getProtocols().add(protocol);
JobSummary job = new JobSummary();
JobSummary.JobInfo jobInfo = new JobSummary.JobInfo();
jobInfo.getAny().add(transfer);
job.setJobInfo(jobInfo);
try {
Sonia Zorba
committed
uriService.getNegotiatedTransfer(job, transfer);
Sonia Zorba
committed
fail("Expected ProtocolNotSupportedException");
} catch (ProtocolNotSupportedException ex) {
}
}
@Test
public void testSetSyncTransferEndpointsNoProtocols() {
Transfer transfer = new Transfer();
transfer.setTarget(Arrays.asList("vos://example.com!vospace/mydata1"));
Sonia Zorba
committed
transfer.setDirection("pullFromVoSpace");
JobSummary job = new JobSummary();
JobSummary.JobInfo jobInfo = new JobSummary.JobInfo();
jobInfo.getAny().add(transfer);
job.setJobInfo(jobInfo);
try {
Sonia Zorba
committed
uriService.getNegotiatedTransfer(job, transfer);
Sonia Zorba
committed
fail("Expected InvalidArgumentException");
} catch (InvalidArgumentException ex) {
}
}
@Test
public void testTarArchiveViewEndpoint() {
testArchiveViewEndpoint(Views.TAR_VIEW_URI);
}
@Test
public void testZipArchiveViewEndpoint() {
testArchiveViewEndpoint(Views.ZIP_VIEW_URI);
}
Sonia Zorba
committed
@Test
public void testInvalidTransferNoProtocols() {
Transfer transfer = new Transfer();
transfer.setDirection("pullFromVoSpace");
transfer.setTarget(Arrays.asList("vos://example.com!vospace/file1"));
JobSummary job = new JobSummary();
JobSummary.JobInfo jobInfo = new JobSummary.JobInfo();
jobInfo.getAny().add(transfer);
job.setJobInfo(jobInfo);
mockPublicNode("file1");
mockPublicNode("file2");
InvalidArgumentException ex = assertThrows(InvalidArgumentException.class, () -> {
uriService.getNegotiatedTransfer(job, transfer);
});
assertTrue(ex.getMessage().contains("no protocol"));
}
private void testArchiveViewEndpoint(String viewUri) {
Transfer transfer = new Transfer();
transfer.setDirection("pullFromVoSpace");
transfer.setTarget(Arrays.asList("vos://example.com!vospace/file1", "vos://example.com!vospace/file2"));
Sonia Zorba
committed
Protocol protocol = new Protocol();
protocol.setUri("ivo://ivoa.net/vospace/core#httpget");
transfer.getProtocols().add(protocol);
View view = new View();
view.setUri(viewUri);
transfer.setView(view);
JobSummary job = new JobSummary();
job.setJobId("archive-job-id");
JobSummary.JobInfo jobInfo = new JobSummary.JobInfo();
jobInfo.getAny().add(transfer);
job.setJobInfo(jobInfo);
mockPublicNode("file1");
mockPublicNode("file2");
Sonia Zorba
committed
uriService.getNegotiatedTransfer(job, transfer);
verify(fileServiceClient, times(1)).startArchiveJob(transfer, "archive-job-id");
}
Sonia Zorba
committed
private Node mockPublicNode() {
return mockPublicNode("mydata1");
}
private Node mockPublicNode(String name) {
Sonia Zorba
committed
DataNode node = new DataNode();
node.setUri("vos://example.com!vospace/" + name);
Sonia Zorba
committed
Property publicProperty = new Property();
publicProperty.setUri(NodeProperties.PUBLIC_READ_URI);
publicProperty.setValue(String.valueOf(true));
node.getProperties().add(publicProperty);
when(nodeDAO.listNode(eq("/" + name))).thenReturn(Optional.of(node));
Sonia Zorba
committed
return node;
}
Sonia Zorba
committed
private JobSummary getJob() {
transfer.setTarget(Arrays.asList("vos://example.com!vospace/mydata1"));
transfer.setDirection(JobService.JobDirection.pullFromVoSpace.toString());
Sonia Zorba
committed
Protocol protocol = new Protocol();
protocol.setUri("ivo://ivoa.net/vospace/core#httpget");
transfer.getProtocols().add(protocol);
JobSummary job = new JobSummary();
job.setJobId("job-id");
JobSummary.JobInfo jobInfo = new JobSummary.JobInfo();
jobInfo.getAny().add(transfer);
job.setJobInfo(jobInfo);
return job;
Sonia Zorba
committed
Nicola Fulvio Calabria
committed
private JobSummary getPushToVoSpaceJob() {
Transfer transfer = new Transfer();
transfer.setTarget(Arrays.asList("vos://example.com!vospace/mydata1/mydata2"));
transfer.setDirection(JobService.JobDirection.pushToVoSpace.toString());
Sonia Zorba
committed
Protocol protocol = new Protocol();
protocol.setUri("ivo://ivoa.net/vospace/core#httpput");
transfer.getProtocols().add(protocol);
Nicola Fulvio Calabria
committed
JobSummary job = new JobSummary();
job.setJobId("job-id2");
JobSummary.JobInfo jobInfo = new JobSummary.JobInfo();
jobInfo.getAny().add(transfer);
job.setJobInfo(jobInfo);
Sonia Zorba
committed
return job;
Nicola Fulvio Calabria
committed
}